datafusion_functions_nested/
min_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, GenericListArray, OffsetSizeTrait};
21use arrow::datatypes::DataType;
22use arrow::datatypes::DataType::{LargeList, List};
23use datafusion_common::Result;
24use datafusion_common::cast::{as_large_list_array, as_list_array};
25use datafusion_common::utils::take_function_args;
26use datafusion_common::{ScalarValue, exec_err, plan_err};
27use datafusion_doc::Documentation;
28use datafusion_expr::{
29    ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
30};
31use datafusion_functions_aggregate_common::min_max::{max_batch, min_batch};
32use datafusion_macros::user_doc;
33use itertools::Itertools;
34use std::any::Any;
35
36make_udf_expr_and_func!(
37    ArrayMax,
38    array_max,
39    array,
40    "returns the maximum value in the array.",
41    array_max_udf
42);
43
44#[user_doc(
45    doc_section(label = "Array Functions"),
46    description = "Returns the maximum value in the array.",
47    syntax_example = "array_max(array)",
48    sql_example = r#"```sql
49> select array_max([3,1,4,2]);
50+-----------------------------------------+
51| array_max(List([3,1,4,2]))              |
52+-----------------------------------------+
53| 4                                       |
54+-----------------------------------------+
55```"#,
56    argument(
57        name = "array",
58        description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
59    )
60)]
61#[derive(Debug, PartialEq, Eq, Hash)]
62pub struct ArrayMax {
63    signature: Signature,
64    aliases: Vec<String>,
65}
66
67impl Default for ArrayMax {
68    fn default() -> Self {
69        Self::new()
70    }
71}
72
73impl ArrayMax {
74    pub fn new() -> Self {
75        Self {
76            signature: Signature::array(Volatility::Immutable),
77            aliases: vec!["list_max".to_string()],
78        }
79    }
80}
81
82impl ScalarUDFImpl for ArrayMax {
83    fn as_any(&self) -> &dyn Any {
84        self
85    }
86
87    fn name(&self) -> &str {
88        "array_max"
89    }
90
91    fn signature(&self) -> &Signature {
92        &self.signature
93    }
94
95    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
96        let [array] = take_function_args(self.name(), arg_types)?;
97        match array {
98            List(field) | LargeList(field) => Ok(field.data_type().clone()),
99            arg_type => plan_err!("{} does not support type {arg_type}", self.name()),
100        }
101    }
102
103    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
104        make_scalar_function(array_max_inner)(&args.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
116fn array_max_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
117    let [array] = take_function_args("array_max", args)?;
118    match array.data_type() {
119        List(_) => array_min_max_helper(as_list_array(array)?, max_batch),
120        LargeList(_) => array_min_max_helper(as_large_list_array(array)?, max_batch),
121        arg_type => exec_err!("array_max does not support type: {arg_type}"),
122    }
123}
124
125make_udf_expr_and_func!(
126    ArrayMin,
127    array_min,
128    array,
129    "returns the minimum value in the array",
130    array_min_udf
131);
132#[user_doc(
133    doc_section(label = "Array Functions"),
134    description = "Returns the minimum value in the array.",
135    syntax_example = "array_min(array)",
136    sql_example = r#"```sql
137> select array_min([3,1,4,2]);
138+-----------------------------------------+
139| array_min(List([3,1,4,2]))              |
140+-----------------------------------------+
141| 1                                       |
142+-----------------------------------------+
143```"#,
144    argument(
145        name = "array",
146        description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
147    )
148)]
149#[derive(Debug, PartialEq, Eq, Hash)]
150struct ArrayMin {
151    signature: Signature,
152}
153
154impl Default for ArrayMin {
155    fn default() -> Self {
156        Self::new()
157    }
158}
159
160impl ArrayMin {
161    fn new() -> Self {
162        Self {
163            signature: Signature::array(Volatility::Immutable),
164        }
165    }
166}
167
168impl ScalarUDFImpl for ArrayMin {
169    fn as_any(&self) -> &dyn Any {
170        self
171    }
172
173    fn name(&self) -> &str {
174        "array_min"
175    }
176
177    fn signature(&self) -> &Signature {
178        &self.signature
179    }
180
181    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
182        let [array] = take_function_args(self.name(), arg_types)?;
183        match array {
184            List(field) | LargeList(field) => Ok(field.data_type().clone()),
185            arg_type => plan_err!("{} does not support type {}", self.name(), arg_type),
186        }
187    }
188
189    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
190        make_scalar_function(array_min_inner)(&args.args)
191    }
192
193    fn documentation(&self) -> Option<&Documentation> {
194        self.doc()
195    }
196}
197
198fn array_min_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
199    let [array] = take_function_args("array_min", args)?;
200    match array.data_type() {
201        List(_) => array_min_max_helper(as_list_array(array)?, min_batch),
202        LargeList(_) => array_min_max_helper(as_large_list_array(array)?, min_batch),
203        arg_type => exec_err!("array_min does not support type: {arg_type}"),
204    }
205}
206
207fn array_min_max_helper<O: OffsetSizeTrait>(
208    array: &GenericListArray<O>,
209    agg_fn: fn(&ArrayRef) -> Result<ScalarValue>,
210) -> Result<ArrayRef> {
211    let null_value = ScalarValue::try_from(array.value_type())?;
212    let result_vec: Vec<ScalarValue> = array
213        .iter()
214        .map(|arr| arr.as_ref().map_or_else(|| Ok(null_value.clone()), agg_fn))
215        .try_collect()?;
216    ScalarValue::iter_to_array(result_vec)
217}