Skip to content

Commit 31d0a78

Browse files
Issue-14416 - feat: Add array_min function
1 parent 9730404 commit 31d0a78

File tree

5 files changed

+260
-1
lines changed

5 files changed

+260
-1
lines changed

datafusion/functions-aggregate/src/min_max.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ macro_rules! min_max_batch {
572572
}
573573

574574
/// dynamically-typed min(array) -> ScalarValue
575-
fn min_batch(values: &ArrayRef) -> Result<ScalarValue> {
575+
pub fn min_batch(values: &ArrayRef) -> Result<ScalarValue> {
576576
Ok(match values.data_type() {
577577
DataType::Utf8 => {
578578
typed_min_max_batch_string!(values, StringArray, Utf8, min_string)

datafusion/functions-nested/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub mod map_extract;
5454
pub mod map_keys;
5555
pub mod map_values;
5656
pub mod max;
57+
pub mod min;
5758
pub mod planner;
5859
pub mod position;
5960
pub mod range;
@@ -147,6 +148,7 @@ pub fn all_default_nested_functions() -> Vec<Arc<ScalarUDF>> {
147148
distance::array_distance_udf(),
148149
flatten::flatten_udf(),
149150
max::array_max_udf(),
151+
min::array_min_udf(),
150152
sort::array_sort_udf(),
151153
repeat::array_repeat_udf(),
152154
resize::array_resize_udf(),
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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_min function.
19+
20+
use crate::utils::make_scalar_function;
21+
use arrow::array::ArrayRef;
22+
use arrow::datatypes::DataType;
23+
use arrow::datatypes::DataType::List;
24+
use datafusion_common::cast::as_list_array;
25+
use datafusion_common::utils::take_function_args;
26+
use datafusion_common::{exec_err, ScalarValue};
27+
use datafusion_doc::Documentation;
28+
use datafusion_expr::{
29+
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
30+
};
31+
use datafusion_functions_aggregate::min_max;
32+
use datafusion_macros::user_doc;
33+
use itertools::Itertools;
34+
use std::any::Any;
35+
36+
make_udf_expr_and_func!(
37+
ArrayMin,
38+
array_min,
39+
array,
40+
"returns the minimum value in the array.",
41+
array_min_udf
42+
);
43+
44+
#[user_doc(
45+
doc_section(label = "Array Functions"),
46+
description = "Returns the minimum value in the array.",
47+
syntax_example = "array_min(array)",
48+
sql_example = r#"```sql
49+
> select array_min([3,1,4,2]);
50+
+-----------------------------------------+
51+
| array_min(List([3,1,4,2])) |
52+
+-----------------------------------------+
53+
| 1 |
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)]
62+
pub struct ArrayMin {
63+
signature: Signature,
64+
aliases: Vec<String>,
65+
}
66+
67+
impl Default for ArrayMin {
68+
fn default() -> Self {
69+
Self::new()
70+
}
71+
}
72+
73+
impl ArrayMin {
74+
pub fn new() -> Self {
75+
Self {
76+
signature: Signature::array(Volatility::Immutable),
77+
aliases: vec!["list_min".to_string()],
78+
}
79+
}
80+
}
81+
82+
impl ScalarUDFImpl for ArrayMin {
83+
fn as_any(&self) -> &dyn Any {
84+
self
85+
}
86+
87+
fn name(&self) -> &str {
88+
"array_min"
89+
}
90+
91+
fn signature(&self) -> &Signature {
92+
&self.signature
93+
}
94+
95+
fn return_type(&self, arg_types: &[DataType]) -> datafusion_common::Result<DataType> {
96+
match &arg_types[0] {
97+
List(field) => Ok(field.data_type().clone()),
98+
_ => exec_err!("Not reachable, data_type should be List"),
99+
}
100+
}
101+
102+
fn invoke_with_args(
103+
&self,
104+
args: ScalarFunctionArgs,
105+
) -> datafusion_common::Result<ColumnarValue> {
106+
make_scalar_function(array_min_inner)(&args.args)
107+
}
108+
109+
fn aliases(&self) -> &[String] {
110+
&self.aliases
111+
}
112+
113+
fn documentation(&self) -> Option<&Documentation> {
114+
self.doc()
115+
}
116+
}
117+
118+
/// array_min SQL function
119+
///
120+
/// There is one argument for array_min as the array.
121+
/// `array_min(array)`
122+
///
123+
/// For example:
124+
/// > array_min(\[3, 1, 2]) -> 1
125+
pub fn array_min_inner(args: &[ArrayRef]) -> datafusion_common::Result<ArrayRef> {
126+
let [arg1] = take_function_args("array_min", args)?;
127+
128+
match &arg1.data_type() {
129+
List(_) => {
130+
let input_list_array = as_list_array(&arg1)?;
131+
let result_vec = input_list_array
132+
.iter()
133+
.flat_map(|arr| min_max::min_batch(&arr.unwrap()))
134+
.collect_vec();
135+
136+
ScalarValue::iter_to_array(result_vec)
137+
}
138+
_ => exec_err!("array_min does not support type: {:?}", args[0].data_type()),
139+
}
140+
}

datafusion/sqllogictest/test_files/array.slt

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,90 @@ NULL
15211521
query error DataFusion error: Error during planning: 'array_max' does not support zero arguments
15221522
select array_max();
15231523

1524+
## array_min
1525+
# array_min scalar function #1 (with positive index)
1526+
query I
1527+
select array_min(make_array(5, 3, 4, 6));
1528+
----
1529+
3
1530+
1531+
query I
1532+
select array_min(make_array(5, 3, 4, NULL, 6, NULL));
1533+
----
1534+
3
1535+
1536+
query I
1537+
select array_min(make_array(NULL, NULL));
1538+
----
1539+
NULL
1540+
1541+
query T
1542+
select array_min(make_array('h', 'e', 'l', 'l', 'o'));
1543+
----
1544+
e
1545+
1546+
query T
1547+
select array_min(make_array('h', 'e', 'l', NULL, 'l', 'o', NULL));
1548+
----
1549+
e
1550+
1551+
query B
1552+
select array_min(make_array(true, true, false, true));
1553+
----
1554+
false
1555+
1556+
query B
1557+
select array_min(make_array(true, true, NULL, false, true));
1558+
----
1559+
false
1560+
1561+
query D
1562+
select array_min(make_array(DATE '1992-09-01', DATE '1993-03-01', DATE '1985-11-01', DATE '1999-05-01'));
1563+
----
1564+
1985-11-01
1565+
1566+
query D
1567+
select array_min(make_array(DATE '1995-09-01', DATE '1993-03-01', NULL, DATE '1999-05-01'));
1568+
----
1569+
1993-03-01
1570+
1571+
query P
1572+
select array_min(make_array(TIMESTAMP '1992-09-01', TIMESTAMP '1984-10-01', TIMESTAMP '1995-06-01'));
1573+
----
1574+
1984-10-01T00:00:00
1575+
1576+
query R
1577+
select array_min(make_array(5.1, -3.2, 6.3, 4.9));
1578+
----
1579+
-3.2
1580+
1581+
query P
1582+
select array_min(make_array(NULL, TIMESTAMP '1996-10-01', TIMESTAMP '1995-06-01'));
1583+
----
1584+
1995-06-01T00:00:00
1585+
1586+
query ?I
1587+
select input, array_min(input) from (select make_array(d - 1, d, d + 1) input from (values (0), (10), (20), (30), (NULL)) t(d))
1588+
----
1589+
[-1, 0, 1] -1
1590+
[9, 10, 11] 9
1591+
[19, 20, 21] 19
1592+
[29, 30, 31] 29
1593+
[NULL, NULL, NULL] NULL
1594+
1595+
query II
1596+
select array_min(arrow_cast(make_array(2, 1, 3), 'FixedSizeList(3, Int64)')), array_min(arrow_cast(make_array(2), 'FixedSizeList(1, Int64)'));
1597+
----
1598+
1 2
1599+
1600+
query I
1601+
select array_min(make_array());
1602+
----
1603+
NULL
1604+
1605+
# Testing with empty arguments should result in an error
1606+
query error DataFusion error: Error during planning: 'array_min' does not support zero arguments
1607+
select array_min();
15241608

15251609
## array_pop_back (aliases: `list_pop_back`)
15261610

docs/source/user-guide/sql/scalar_functions.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,7 @@ _Alias of [current_date](#current_date)._
25522552
- [array_join](#array_join)
25532553
- [array_length](#array_length)
25542554
- [array_max](#array_max)
2555+
- [array_min](#array_min)
25552556
- [array_ndims](#array_ndims)
25562557
- [array_pop_back](#array_pop_back)
25572558
- [array_pop_front](#array_pop_front)
@@ -2598,6 +2599,7 @@ _Alias of [current_date](#current_date)._
25982599
- [list_join](#list_join)
25992600
- [list_length](#list_length)
26002601
- [list_max](#list_max)
2602+
- [list_min](#list_min)
26012603
- [list_ndims](#list_ndims)
26022604
- [list_pop_back](#list_pop_back)
26032605
- [list_pop_front](#list_pop_front)
@@ -3058,6 +3060,33 @@ array_max(array)
30583060

30593061
- list_max
30603062

3063+
### `array_min`
3064+
3065+
Returns the minimum value in the array.
3066+
3067+
```sql
3068+
array_min(array)
3069+
```
3070+
3071+
#### Arguments
3072+
3073+
- **array**: Array expression. Can be a constant, column, or function, and any combination of array operators.
3074+
3075+
#### Example
3076+
3077+
```sql
3078+
> select array_min([3,1,4,2]);
3079+
+-----------------------------------------+
3080+
| array_min(List([3,1,4,2])) |
3081+
+-----------------------------------------+
3082+
| 1 |
3083+
+-----------------------------------------+
3084+
```
3085+
3086+
#### Aliases
3087+
3088+
- list_min
3089+
30613090
### `array_ndims`
30623091

30633092
Returns the number of dimensions of the array.
@@ -3819,6 +3848,10 @@ _Alias of [array_length](#array_length)._
38193848

38203849
_Alias of [array_max](#array_max)._
38213850

3851+
### `list_min`
3852+
3853+
_Alias of [array_min](#array_min)._
3854+
38223855
### `list_ndims`
38233856

38243857
_Alias of [array_ndims](#array_ndims)._

0 commit comments

Comments
 (0)