Skip to content

Commit

Permalink
Handle empty rows for array_distinct (#13810)
Browse files Browse the repository at this point in the history
* handle empty array distinct

* ignore

* fix

---------

Co-authored-by: Cyprien Huet <chuet@palantir.com>
  • Loading branch information
cht42 and cht42 authored Dec 17, 2024
1 parent 452a8f4 commit 5500b11
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions datafusion/functions-nested/src/set_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,9 @@ fn general_array_distinct<OffsetSize: OffsetSizeTrait>(
array: &GenericListArray<OffsetSize>,
field: &FieldRef,
) -> Result<ArrayRef> {
if array.len() == 0 {
return Ok(Arc::new(array.clone()) as ArrayRef);
}
let dt = array.value_type();
let mut offsets = Vec::with_capacity(array.len());
offsets.push(OffsetSize::usize_as(0));
Expand Down
16 changes: 15 additions & 1 deletion datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -5660,6 +5660,20 @@ select count(*) from arrays where 'X'>any(column3);
#----
#NULL

# test with empty row, the row that does not match the condition has row count 0
statement ok
create table t1(a int, b int) as values (100, 1), (101, 2), (102, 3), (101, 2);

# rowsort is to ensure the order of group by is deterministic, array_sort has no effect here, since the sum() always returns single row.
query ? rowsort
select array_distinct([sum(a)]) from t1 where a > 100 group by b;
----
[102]
[202]

statement ok
drop table t1;

query ?
select array_distinct([]);
----
Expand Down Expand Up @@ -7129,7 +7143,7 @@ select array_resize(arrow_cast(NULL, 'List(Int8)'), 1);
NULL

statement ok
CREATE TABLE array_resize_values
CREATE TABLE array_resize_values
AS VALUES
(make_array(1, NULL, 3, 4, 5, 6, 7, 8, 9, 10), 2, 1),
(make_array(11, 12, NULL, 14, 15, 16, 17, 18, 19, 20), 5, 2),
Expand Down

0 comments on commit 5500b11

Please sign in to comment.