Skip to content

Commit f92ecc4

Browse files
Add array_product UDF (apache#22703)
## Which issue does this PR close? - Part of apache#21536 (vector math + array aggregate functions family). ## Rationale for this change ## What changes are included in this PR? - datafusion/functions-nested/src/array_product.rs — new ArrayProduct UDF and kernel. - datafusion/functions-nested/src/lib.rs — three registration sites (module, expr_fn, all_default_nested_functions). - datafusion/sqllogictest/test_files/array_product.slt — SLT coverage. ## Are these changes tested? yes using UT ## Are there any user-facing changes? Yes — adds two new SQL scalar functions, array_product and its alias list_product. No API breakage.
1 parent 9aebcea commit f92ecc4

4 files changed

Lines changed: 355 additions & 0 deletions

File tree

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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_product function.
19+
20+
use crate::utils::make_scalar_function;
21+
use arrow::array::{Array, ArrayRef, Float64Array, OffsetSizeTrait};
22+
use arrow::datatypes::{
23+
DataType,
24+
DataType::{FixedSizeList, LargeList, List, Null},
25+
Field,
26+
};
27+
use datafusion_common::cast::{as_float64_array, as_generic_list_array};
28+
use datafusion_common::utils::{ListCoercion, coerced_type_with_base_type_only};
29+
use datafusion_common::{Result, internal_err, plan_err, utils::take_function_args};
30+
use datafusion_expr::{
31+
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
32+
Volatility,
33+
};
34+
use datafusion_macros::user_doc;
35+
use std::sync::Arc;
36+
37+
make_udf_expr_and_func!(
38+
ArrayProduct,
39+
array_product,
40+
array,
41+
"returns the product of the elements of a numeric array.",
42+
array_product_udf
43+
);
44+
45+
#[user_doc(
46+
doc_section(label = "Array Functions"),
47+
description = "Returns the product of the elements in the input numeric array. \
48+
NULL elements inside the array are skipped (matching SQL aggregate \
49+
convention). Returns NULL if the input is NULL, every element is \
50+
NULL, or the array is empty. The result is always returned as \
51+
`Float64`.",
52+
syntax_example = "array_product(array)",
53+
sql_example = r#"```sql
54+
> select array_product([1.0, 2.0, 3.0]);
55+
+------------------------------------+
56+
| array_product(List([1.0,2.0,3.0])) |
57+
+------------------------------------+
58+
| 6.0 |
59+
+------------------------------------+
60+
```"#,
61+
argument(
62+
name = "array",
63+
description = "Array expression. Can be a constant, column, or function, and any combination of array operators."
64+
)
65+
)]
66+
#[derive(Debug, PartialEq, Eq, Hash)]
67+
pub struct ArrayProduct {
68+
signature: Signature,
69+
aliases: Vec<String>,
70+
}
71+
72+
impl Default for ArrayProduct {
73+
fn default() -> Self {
74+
Self::new()
75+
}
76+
}
77+
78+
impl ArrayProduct {
79+
pub fn new() -> Self {
80+
Self {
81+
signature: Signature::user_defined(Volatility::Immutable),
82+
aliases: vec!["list_product".to_string()],
83+
}
84+
}
85+
}
86+
87+
impl ScalarUDFImpl for ArrayProduct {
88+
fn name(&self) -> &str {
89+
"array_product"
90+
}
91+
92+
fn signature(&self) -> &Signature {
93+
&self.signature
94+
}
95+
96+
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
97+
Ok(DataType::Float64)
98+
}
99+
100+
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
101+
let [arg_type] = take_function_args(self.name(), arg_types)?;
102+
let coercion = Some(&ListCoercion::FixedSizedListToList);
103+
104+
if !matches!(arg_type, Null | List(_) | LargeList(_) | FixedSizeList(..)) {
105+
return plan_err!("{} does not support type {arg_type}", self.name());
106+
}
107+
108+
let coerced = if matches!(arg_type, Null) {
109+
List(Arc::new(Field::new_list_field(DataType::Float64, true)))
110+
} else {
111+
coerced_type_with_base_type_only(arg_type, &DataType::Float64, coercion)
112+
};
113+
114+
Ok(vec![coerced])
115+
}
116+
117+
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
118+
make_scalar_function(array_product_inner)(&args.args)
119+
}
120+
121+
fn aliases(&self) -> &[String] {
122+
&self.aliases
123+
}
124+
125+
fn documentation(&self) -> Option<&Documentation> {
126+
self.doc()
127+
}
128+
}
129+
130+
fn array_product_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
131+
let [array] = take_function_args("array_product", args)?;
132+
match array.data_type() {
133+
List(_) => general_array_product::<i32>(args),
134+
LargeList(_) => general_array_product::<i64>(args),
135+
arg_type => internal_err!(
136+
"array_product received unexpected type after coercion: {arg_type}"
137+
),
138+
}
139+
}
140+
141+
fn general_array_product<O: OffsetSizeTrait>(arrays: &[ArrayRef]) -> Result<ArrayRef> {
142+
let list_array = as_generic_list_array::<O>(&arrays[0])?;
143+
let values = as_float64_array(list_array.values())?;
144+
let offsets = list_array.value_offsets();
145+
146+
let mut builder = Float64Array::builder(list_array.len());
147+
148+
for row in 0..list_array.len() {
149+
if list_array.is_null(row) {
150+
builder.append_null();
151+
continue;
152+
}
153+
154+
let start = offsets[row].as_usize();
155+
let end = offsets[row + 1].as_usize();
156+
157+
let mut prod = 1.0_f64;
158+
let mut any_valid = false;
159+
for i in start..end {
160+
if values.is_valid(i) {
161+
prod *= values.value(i);
162+
any_valid = true;
163+
}
164+
}
165+
166+
if any_valid {
167+
builder.append_value(prod);
168+
} else {
169+
builder.append_null();
170+
}
171+
}
172+
173+
Ok(Arc::new(builder.finish()))
174+
}

‎datafusion/functions-nested/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub mod array_compact;
4646
pub mod array_filter;
4747
pub mod array_has;
4848
pub mod array_normalize;
49+
pub mod array_product;
4950
pub mod array_scale;
5051
pub mod array_subtract;
5152
pub mod array_transform;
@@ -99,6 +100,7 @@ pub mod expr_fn {
99100
pub use super::array_has::array_has_all;
100101
pub use super::array_has::array_has_any;
101102
pub use super::array_normalize::array_normalize;
103+
pub use super::array_product::array_product;
102104
pub use super::array_scale::array_scale;
103105
pub use super::array_subtract::array_subtract;
104106
pub use super::array_transform::array_transform;
@@ -177,6 +179,7 @@ pub fn all_default_nested_functions() -> Vec<Arc<ScalarUDF>> {
177179
length::array_length_udf(),
178180
array_normalize::array_normalize_udf(),
179181
array_add::array_add_udf(),
182+
array_product::array_product_udf(),
180183
array_scale::array_scale_udf(),
181184
array_subtract::array_subtract_udf(),
182185
cosine_distance::cosine_distance_udf(),
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
## array_product
19+
20+
# Basic product of three floats
21+
query R
22+
select array_product([1.0, 2.0, 3.0]);
23+
----
24+
6
25+
26+
# Negative values: signs multiply
27+
query R
28+
select array_product([-2.0, 3.0]);
29+
----
30+
-6
31+
32+
# Single element returns itself
33+
query R
34+
select array_product([5.0]);
35+
----
36+
5
37+
38+
# Zero element produces zero (no short-circuit; we still multiply)
39+
query R
40+
select array_product([0.0, 3.0, 4.0]);
41+
----
42+
0
43+
44+
# NULL elements inside the list are skipped (SQL aggregate convention)
45+
query R
46+
select array_product([2.0, NULL, 3.0]);
47+
----
48+
6
49+
50+
# All-NULL elements: no data to reduce, returns NULL
51+
query R
52+
select array_product([CAST(NULL AS DOUBLE), CAST(NULL AS DOUBLE)]);
53+
----
54+
NULL
55+
56+
# Bare NULL input returns NULL
57+
query R
58+
select array_product(NULL);
59+
----
60+
NULL
61+
62+
# Empty array: no data to reduce, returns NULL
63+
query R
64+
select array_product(arrow_cast(make_array(), 'List(Float64)'));
65+
----
66+
NULL
67+
68+
# LargeList input
69+
query R
70+
select array_product(arrow_cast([2.0, 3.0, 4.0], 'LargeList(Float64)'));
71+
----
72+
24
73+
74+
# FixedSizeList input (coerced to List)
75+
query R
76+
select array_product(arrow_cast([2.0, 3.0, 4.0], 'FixedSizeList(3, Float64)'));
77+
----
78+
24
79+
80+
# Float32 inner type (coerced to Float64)
81+
query R
82+
select array_product(arrow_cast([2.0, 3.0, 4.0], 'List(Float32)'));
83+
----
84+
24
85+
86+
# Int64 inner type (coerced to Float64)
87+
query R
88+
select array_product(arrow_cast([2, 3, 4], 'List(Int64)'));
89+
----
90+
24
91+
92+
# Integer literals (coerced to Float64)
93+
query R
94+
select array_product([2, 3, 4]);
95+
----
96+
24
97+
98+
# Unsupported non-list input (plan error)
99+
query error array_product does not support type
100+
select array_product(1);
101+
102+
# No arguments error
103+
query error array_product function requires 1 argument, got 0
104+
select array_product();
105+
106+
# Multi-row query: normal row, NULL row, empty list, all-NULL elements,
107+
# element-NULL skip, single zero
108+
query R
109+
select array_product(column1) from (values
110+
(make_array(2.0, 3.0, 4.0)),
111+
(NULL),
112+
(arrow_cast(make_array(), 'List(Float64)')),
113+
(make_array(CAST(NULL AS DOUBLE), CAST(NULL AS DOUBLE))),
114+
(make_array(CAST(2.0 AS DOUBLE), CAST(NULL AS DOUBLE), CAST(5.0 AS DOUBLE))),
115+
(make_array(0.0, 7.0))
116+
) as t(column1);
117+
----
118+
24
119+
NULL
120+
NULL
121+
NULL
122+
10
123+
0
124+
125+
# Return type is always Float64 (scalar, not List)
126+
query RT
127+
select array_product([2.0, 3.0]), arrow_typeof(array_product([2.0, 3.0]));
128+
----
129+
6 Float64
130+
131+
# list_product alias produces the same result
132+
query R
133+
select list_product([2.0, 3.0, 4.0]);
134+
----
135+
24
136+
137+
# list_product alias multi-row
138+
query R
139+
select list_product(column1) from (values
140+
(make_array(2.0, 3.0)),
141+
(NULL)
142+
) as t(column1);
143+
----
144+
6
145+
NULL

0 commit comments

Comments
 (0)