|
16 | 16 | // under the License. |
17 | 17 |
|
18 | 18 | //! [`ScalarUDFImpl`] definitions for array_max function. |
19 | | -use crate::sort::array_sort_inner; |
20 | 19 | use crate::utils::make_scalar_function; |
21 | | -use arrow_array::{Array, ArrayRef, StringArray}; |
| 20 | +use arrow_array::{Array, ArrayRef}; |
22 | 21 | use arrow_schema::DataType; |
23 | 22 | use arrow_schema::DataType::{FixedSizeList, LargeList, List}; |
24 | 23 | use datafusion_common::cast::as_list_array; |
25 | 24 | use datafusion_common::exec_err; |
26 | 25 | use datafusion_doc::Documentation; |
27 | 26 | use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; |
| 27 | +use datafusion_functions::utils::take_function_args; |
| 28 | +use datafusion_functions_aggregate::min_max; |
28 | 29 | use datafusion_macros::user_doc; |
29 | 30 | use std::any::Any; |
30 | | -use std::sync::Arc; |
31 | 31 |
|
32 | 32 | make_udf_expr_and_func!( |
33 | 33 | ArrayMax, |
@@ -124,29 +124,14 @@ impl ScalarUDFImpl for ArrayMax { |
124 | 124 | /// For example: |
125 | 125 | /// > array_max(\[1, 3, 2]) -> 3 |
126 | 126 | pub fn array_max_inner(args: &[ArrayRef]) -> datafusion_common::Result<ArrayRef> { |
127 | | - if args.len() != 1 { |
128 | | - return exec_err!("array_max needs one argument"); |
129 | | - } |
| 127 | + let [arg1] = take_function_args("array_max", args)?; |
130 | 128 |
|
131 | | - match &args[0].data_type() { |
| 129 | + match arg1.data_type() { |
132 | 130 | List(_) | LargeList(_) | FixedSizeList(_, _) => { |
133 | | - let new_args = vec![ |
134 | | - Arc::<dyn Array>::clone(&args[0]), |
135 | | - Arc::new(StringArray::from_iter(vec![Some("DESC")])), |
136 | | - Arc::new(StringArray::from_iter(vec![Some("NULLS LAST")])), |
137 | | - ]; |
138 | | - array_max_internal(&new_args) |
| 131 | + let input_array = as_list_array(&arg1)?.value(0); |
| 132 | + let max_result = min_max::max_batch(&input_array); |
| 133 | + max_result?.to_array() |
139 | 134 | } |
140 | | - _ => exec_err!("array_max does not support type: {:?}", args[0].data_type()), |
141 | | - } |
142 | | -} |
143 | | - |
144 | | -fn array_max_internal(args: &[ArrayRef]) -> datafusion_common::Result<ArrayRef> { |
145 | | - let sorted_array = array_sort_inner(args)?; |
146 | | - let result_array = as_list_array(&sorted_array)?.value(0); |
147 | | - if result_array.is_empty() { |
148 | | - return exec_err!("array_max needs one argument as non-empty array"); |
| 135 | + _ => exec_err!("array_max does not support type: {:?}", arg1.data_type()), |
149 | 136 | } |
150 | | - let max_result = result_array.slice(0, 1); |
151 | | - Ok(max_result) |
152 | 137 | } |
0 commit comments