datafusion_functions_nested/
max.rs

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.
19use crate::utils::make_scalar_function;
20use arrow::array::ArrayRef;
21use arrow::datatypes::DataType;
22use arrow::datatypes::DataType::List;
23use datafusion_common::cast::as_list_array;
24use datafusion_common::utils::take_function_args;
25use datafusion_common::{exec_err, ScalarValue};
26use datafusion_doc::Documentation;
27use datafusion_expr::{
28    ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
29};
30use datafusion_functions_aggregate::min_max;
31use datafusion_macros::user_doc;
32use itertools::Itertools;
33use std::any::Any;
34
35make_udf_expr_and_func!(
36    ArrayMax,
37    array_max,
38    array,
39    "returns the maximum value in the array.",
40    array_max_udf
41);
42
43#[user_doc(
44    doc_section(label = "Array Functions"),
45    description = "Returns the maximum value in the array.",
46    syntax_example = "array_max(array)",
47    sql_example = r#"```sql
48> select array_max([3,1,4,2]);
49+-----------------------------------------+
50| array_max(List([3,1,4,2]))              |
51+-----------------------------------------+
52| 4                                       |
53+-----------------------------------------+
54```"#,
55    argument(
56        name = "array",
57        description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
58    )
59)]
60#[derive(Debug)]
61pub struct ArrayMax {
62    signature: Signature,
63    aliases: Vec<String>,
64}
65
66impl Default for ArrayMax {
67    fn default() -> Self {
68        Self::new()
69    }
70}
71
72impl ArrayMax {
73    pub fn new() -> Self {
74        Self {
75            signature: Signature::array(Volatility::Immutable),
76            aliases: vec!["list_max".to_string()],
77        }
78    }
79}
80
81impl ScalarUDFImpl for ArrayMax {
82    fn as_any(&self) -> &dyn Any {
83        self
84    }
85
86    fn name(&self) -> &str {
87        "array_max"
88    }
89
90    fn signature(&self) -> &Signature {
91        &self.signature
92    }
93
94    fn return_type(&self, arg_types: &[DataType]) -> datafusion_common::Result<DataType> {
95        match &arg_types[0] {
96            List(field) => Ok(field.data_type().clone()),
97            _ => exec_err!("Not reachable, data_type should be List"),
98        }
99    }
100
101    fn invoke_with_args(
102        &self,
103        args: ScalarFunctionArgs,
104    ) -> datafusion_common::Result<ColumnarValue> {
105        make_scalar_function(array_max_inner)(&args.args)
106    }
107
108    fn aliases(&self) -> &[String] {
109        &self.aliases
110    }
111
112    fn documentation(&self) -> Option<&Documentation> {
113        self.doc()
114    }
115}
116
117/// array_max SQL function
118///
119/// There is one argument for array_max as the array.
120/// `array_max(array)`
121///
122/// For example:
123/// > array_max(\[1, 3, 2]) -> 3
124pub fn array_max_inner(args: &[ArrayRef]) -> datafusion_common::Result<ArrayRef> {
125    let [arg1] = take_function_args("array_max", args)?;
126
127    match arg1.data_type() {
128        List(_) => {
129            let input_list_array = as_list_array(&arg1)?;
130            let result_vec = input_list_array
131                .iter()
132                .flat_map(|arr| min_max::max_batch(&arr.unwrap()))
133                .collect_vec();
134            ScalarValue::iter_to_array(result_vec)
135        }
136        _ => exec_err!("array_max does not support type: {:?}", arg1.data_type()),
137    }
138}