Skip to content

Commit ca41065

Browse files
committed
resolve comment
1 parent b1b69cd commit ca41065

2 files changed

Lines changed: 9 additions & 20 deletions

File tree

datafusion/functions-nested/src/array_product.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ make_udf_expr_and_func!(
4646
doc_section(label = "Array Functions"),
4747
description = "Returns the product of the elements in the input numeric array. \
4848
NULL elements inside the array are skipped (matching SQL aggregate \
49-
convention). Returns NULL if the whole input is NULL or if every \
50-
element is NULL. Returns 1.0 for an empty array (multiplicative \
51-
identity). The result is always returned as `Float64`.",
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`.",
5252
syntax_example = "array_product(array)",
5353
sql_example = r#"```sql
5454
> select array_product([1.0, 2.0, 3.0]);
@@ -153,23 +153,12 @@ fn general_array_product<O: OffsetSizeTrait>(arrays: &[ArrayRef]) -> Result<Arra
153153

154154
let start = offsets[row].as_usize();
155155
let end = offsets[row + 1].as_usize();
156-
let len = end - start;
157-
158-
// Empty list -> multiplicative identity. Distinguished here from
159-
// all-NULL elements (which yield NULL): we have no data either way,
160-
// but `[]` is structurally a known-empty product, while `[NULL,NULL]`
161-
// means every value was unknown.
162-
if len == 0 {
163-
builder.append_value(1.0);
164-
continue;
165-
}
166156

167-
let slice = values.slice(start, len);
168157
let mut prod = 1.0_f64;
169158
let mut any_valid = false;
170-
for i in 0..len {
171-
if slice.is_valid(i) {
172-
prod *= slice.value(i);
159+
for i in start..end {
160+
if values.is_valid(i) {
161+
prod *= values.value(i);
173162
any_valid = true;
174163
}
175164
}

datafusion/sqllogictest/test_files/array_product.slt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ select array_product(NULL);
5959
----
6060
NULL
6161

62-
# Empty array: returns 1.0 (multiplicative identity)
62+
# Empty array: no data to reduce, returns NULL
6363
query R
6464
select array_product(arrow_cast(make_array(), 'List(Float64)'));
6565
----
66-
1
66+
NULL
6767

6868
# LargeList input
6969
query R
@@ -117,7 +117,7 @@ select array_product(column1) from (values
117117
----
118118
24
119119
NULL
120-
1
120+
NULL
121121
NULL
122122
10
123123
0

0 commit comments

Comments
 (0)