|
| 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::sort::array_sort_inner; |
| 20 | +use crate::utils::make_scalar_function; |
| 21 | +use arrow_array::{Array, ArrayRef, StringArray}; |
| 22 | +use arrow_schema::DataType; |
| 23 | +use arrow_schema::DataType::{FixedSizeList, LargeList, List}; |
| 24 | +use datafusion_common::cast::as_list_array; |
| 25 | +use datafusion_common::exec_err; |
| 26 | +use datafusion_doc::Documentation; |
| 27 | +use datafusion_expr::{ColumnarValue, Expr, ScalarUDFImpl, Signature, Volatility}; |
| 28 | +use datafusion_macros::user_doc; |
| 29 | +use std::any::Any; |
| 30 | +use std::sync::Arc; |
| 31 | + |
| 32 | +make_udf_expr_and_func!( |
| 33 | + ArrayMax, |
| 34 | + array_max, |
| 35 | + array, |
| 36 | + "returns the maximum value in the array.", |
| 37 | + array_max_udf |
| 38 | +); |
| 39 | + |
| 40 | +#[user_doc( |
| 41 | + doc_section(label = "Array Functions"), |
| 42 | + description = "Returns the maximum value in the array.", |
| 43 | + syntax_example = "array_max(array)", |
| 44 | + sql_example = r#"```sql |
| 45 | +> select array_max([3,1,4,2]); |
| 46 | ++-----------------------------------------+ |
| 47 | +| array_max(List([3,1,4,2])) | |
| 48 | ++-----------------------------------------+ |
| 49 | +| 4 | |
| 50 | ++-----------------------------------------+ |
| 51 | +```"#, |
| 52 | + argument( |
| 53 | + name = "array", |
| 54 | + description = "Array expression. Can be a constant, column, or function, and any combination of array operators." |
| 55 | + ) |
| 56 | +)] |
| 57 | +#[derive(Debug)] |
| 58 | +pub struct ArrayMax { |
| 59 | + signature: Signature, |
| 60 | + aliases: Vec<String>, |
| 61 | +} |
| 62 | + |
| 63 | +impl Default for ArrayMax { |
| 64 | + fn default() -> Self { |
| 65 | + Self::new() |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl ArrayMax { |
| 70 | + pub fn new() -> Self { |
| 71 | + Self { |
| 72 | + signature: Signature::array(Volatility::Immutable), |
| 73 | + aliases: vec!["list_max".to_string()], |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl ScalarUDFImpl for ArrayMax { |
| 79 | + fn as_any(&self) -> &dyn Any { |
| 80 | + self |
| 81 | + } |
| 82 | + |
| 83 | + fn name(&self) -> &str { |
| 84 | + "array_max" |
| 85 | + } |
| 86 | + |
| 87 | + fn display_name(&self, args: &[Expr]) -> datafusion_common::Result<String> { |
| 88 | + let args_name = args.iter().map(ToString::to_string).collect::<Vec<_>>(); |
| 89 | + if args_name.len() != 1 { |
| 90 | + return exec_err!("expects 1 arg, got {}", args_name.len()); |
| 91 | + } |
| 92 | + |
| 93 | + Ok(format!("{}", args_name[0])) |
| 94 | + } |
| 95 | + |
| 96 | + fn schema_name(&self, args: &[Expr]) -> datafusion_common::Result<String> { |
| 97 | + let args_name = args |
| 98 | + .iter() |
| 99 | + .map(|e| e.schema_name().to_string()) |
| 100 | + .collect::<Vec<_>>(); |
| 101 | + if args_name.len() != 1 { |
| 102 | + return exec_err!("expects 1 arg, got {}", args_name.len()); |
| 103 | + } |
| 104 | + |
| 105 | + Ok(format!("{}", args_name[0])) |
| 106 | + } |
| 107 | + |
| 108 | + fn signature(&self) -> &Signature { |
| 109 | + &self.signature |
| 110 | + } |
| 111 | + |
| 112 | + fn return_type(&self, arg_types: &[DataType]) -> datafusion_common::Result<DataType> { |
| 113 | + match &arg_types[0] { |
| 114 | + List(field) | LargeList(field) | FixedSizeList(field, _) => { |
| 115 | + Ok(field.data_type().clone()) |
| 116 | + } |
| 117 | + _ => exec_err!( |
| 118 | + "Not reachable, data_type should be List, LargeList or FixedSizeList" |
| 119 | + ), |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + fn invoke_batch( |
| 124 | + &self, |
| 125 | + args: &[ColumnarValue], |
| 126 | + _number_rows: usize, |
| 127 | + ) -> datafusion_common::Result<ColumnarValue> { |
| 128 | + make_scalar_function(array_max_inner)(args) |
| 129 | + } |
| 130 | + |
| 131 | + fn aliases(&self) -> &[String] { |
| 132 | + &self.aliases |
| 133 | + } |
| 134 | + |
| 135 | + fn documentation(&self) -> Option<&Documentation> { |
| 136 | + self.doc() |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/// array_max SQL function |
| 141 | +/// |
| 142 | +/// There is one argument for array_max as the array. |
| 143 | +/// `array_max(array)` |
| 144 | +/// |
| 145 | +/// For example: |
| 146 | +/// > array_max(\[1, 3, 2]) -> 3 |
| 147 | +pub fn array_max_inner(args: &[ArrayRef]) -> datafusion_common::Result<ArrayRef> { |
| 148 | + if args.len() != 1 { |
| 149 | + return exec_err!("array_max needs one argument"); |
| 150 | + } |
| 151 | + |
| 152 | + match &args[0].data_type() { |
| 153 | + List(_) | LargeList(_) | FixedSizeList(_, _) => { |
| 154 | + let new_args = vec![ |
| 155 | + args[0].clone(), |
| 156 | + Arc::new(StringArray::from_iter(vec![Some("DESC")])), |
| 157 | + Arc::new(StringArray::from_iter(vec![Some("NULLS LAST")])), |
| 158 | + ]; |
| 159 | + array_max_internal(&new_args) |
| 160 | + } |
| 161 | + _ => exec_err!("array_max does not support type: {:?}", args[0].data_type()), |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +fn array_max_internal(args: &[ArrayRef]) -> datafusion_common::Result<ArrayRef> { |
| 166 | + let sorted_array = array_sort_inner(args)?; |
| 167 | + let result_array = as_list_array(&sorted_array)?.value(0); |
| 168 | + if result_array.is_empty() { |
| 169 | + return exec_err!("array_max needs one argument as non-empty array"); |
| 170 | + } |
| 171 | + let max_result = result_array.slice(0, 1); |
| 172 | + Ok(max_result) |
| 173 | +} |
0 commit comments