|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! [`ScalarUDFImpl`] definitions for array_max function. |
| 19 | +use crate::utils::make_scalar_function; |
| 20 | +use arrow::array::ArrayRef; |
| 21 | +use arrow::datatypes::DataType; |
| 22 | +use arrow::datatypes::DataType::List; |
| 23 | +use datafusion_common::cast::as_list_array; |
| 24 | +use datafusion_common::utils::take_function_args; |
| 25 | +use datafusion_common::{exec_err, ScalarValue}; |
| 26 | +use datafusion_doc::Documentation; |
| 27 | +use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; |
| 28 | +use datafusion_functions_aggregate::min_max; |
| 29 | +use datafusion_macros::user_doc; |
| 30 | +use itertools::Itertools; |
| 31 | +use std::any::Any; |
| 32 | + |
| 33 | +make_udf_expr_and_func!( |
| 34 | + ArrayMax, |
| 35 | + array_max, |
| 36 | + array, |
| 37 | + "returns the maximum value in the array.", |
| 38 | + array_max_udf |
| 39 | +); |
| 40 | + |
| 41 | +#[user_doc( |
| 42 | + doc_section(label = "Array Functions"), |
| 43 | + description = "Returns the maximum value in the array.", |
| 44 | + syntax_example = "array_max(array)", |
| 45 | + sql_example = r#"```sql |
| 46 | +> select array_max([3,1,4,2]); |
| 47 | ++-----------------------------------------+ |
| 48 | +| array_max(List([3,1,4,2])) | |
| 49 | ++-----------------------------------------+ |
| 50 | +| 4 | |
| 51 | ++-----------------------------------------+ |
| 52 | +```"#, |
| 53 | + argument( |
| 54 | + name = "array", |
| 55 | + description = "Array expression. Can be a constant, column, or function, and any combination of array operators." |
| 56 | + ) |
| 57 | +)] |
| 58 | +#[derive(Debug)] |
| 59 | +pub struct ArrayMax { |
| 60 | + signature: Signature, |
| 61 | + aliases: Vec<String>, |
| 62 | +} |
| 63 | + |
| 64 | +impl Default for ArrayMax { |
| 65 | + fn default() -> Self { |
| 66 | + Self::new() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl ArrayMax { |
| 71 | + pub fn new() -> Self { |
| 72 | + Self { |
| 73 | + signature: Signature::array(Volatility::Immutable), |
| 74 | + aliases: vec!["list_max".to_string()], |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl ScalarUDFImpl for ArrayMax { |
| 80 | + fn as_any(&self) -> &dyn Any { |
| 81 | + self |
| 82 | + } |
| 83 | + |
| 84 | + fn name(&self) -> &str { |
| 85 | + "array_max" |
| 86 | + } |
| 87 | + |
| 88 | + fn signature(&self) -> &Signature { |
| 89 | + &self.signature |
| 90 | + } |
| 91 | + |
| 92 | + fn return_type(&self, arg_types: &[DataType]) -> datafusion_common::Result<DataType> { |
| 93 | + match &arg_types[0] { |
| 94 | + List(field) => Ok(field.data_type().clone()), |
| 95 | + _ => exec_err!("Not reachable, data_type should be List"), |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fn invoke_batch( |
| 100 | + &self, |
| 101 | + args: &[ColumnarValue], |
| 102 | + _number_rows: usize, |
| 103 | + ) -> datafusion_common::Result<ColumnarValue> { |
| 104 | + make_scalar_function(array_max_inner)(args) |
| 105 | + } |
| 106 | + |
| 107 | + fn aliases(&self) -> &[String] { |
| 108 | + &self.aliases |
| 109 | + } |
| 110 | + |
| 111 | + fn documentation(&self) -> Option<&Documentation> { |
| 112 | + self.doc() |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/// array_max SQL function |
| 117 | +/// |
| 118 | +/// There is one argument for array_max as the array. |
| 119 | +/// `array_max(array)` |
| 120 | +/// |
| 121 | +/// For example: |
| 122 | +/// > array_max(\[1, 3, 2]) -> 3 |
| 123 | +pub fn array_max_inner(args: &[ArrayRef]) -> datafusion_common::Result<ArrayRef> { |
| 124 | + let [arg1] = take_function_args("array_max", args)?; |
| 125 | + |
| 126 | + match arg1.data_type() { |
| 127 | + List(_) => { |
| 128 | + let input_list_array = as_list_array(&arg1)?; |
| 129 | + let result_vec = input_list_array |
| 130 | + .iter() |
| 131 | + .flat_map(|arr| min_max::max_batch(&arr.unwrap())) |
| 132 | + .collect_vec(); |
| 133 | + ScalarValue::iter_to_array(result_vec) |
| 134 | + } |
| 135 | + _ => exec_err!("array_max does not support type: {:?}", arg1.data_type()), |
| 136 | + } |
| 137 | +} |
0 commit comments