Skip to content

Commit e684994

Browse files
authored
fix: cardinality() of an empty array should be zero (#20533)
## Which issue does this PR close? - Closes #20526. ## Rationale for this change Per Postgres and the SQL spec, `cardinality()` of an empty array should be zero; we previously returned `NULL`. Along the way, fix another bug: we previously returned `0` for the cardinality of an untyped `NULL` and `NULL` for the cardinality of a typed null (e.g., `NULL::int[]`). We should return `NULL` in both cases. ## What changes are included in this PR? Bug fixes, update SLT. ## Are these changes tested? Yes. ## Are there any user-facing changes? Yes: the behavior of `cardinality` has changed, albeit the previous behavior was incorrect.
1 parent d7d6461 commit e684994

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

datafusion/functions-nested/src/cardinality.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl ScalarUDFImpl for Cardinality {
120120
fn cardinality_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
121121
let [array] = take_function_args("cardinality", args)?;
122122
match array.data_type() {
123-
Null => Ok(Arc::new(UInt64Array::from_value(0, array.len()))),
123+
Null => Ok(Arc::new(UInt64Array::new_null(array.len()))),
124124
List(_) => {
125125
let list_array = as_list_array(array)?;
126126
generic_list_cardinality::<i32>(list_array)
@@ -152,9 +152,14 @@ fn generic_list_cardinality<O: OffsetSizeTrait>(
152152
) -> Result<ArrayRef> {
153153
let result = array
154154
.iter()
155-
.map(|arr| match crate::utils::compute_array_dims(arr)? {
156-
Some(vector) => Ok(Some(vector.iter().map(|x| x.unwrap()).product::<u64>())),
157-
None => Ok(None),
155+
.map(|arr| match arr {
156+
Some(arr) if arr.is_empty() => Ok(Some(0u64)),
157+
arr => match crate::utils::compute_array_dims(arr)? {
158+
Some(vector) => {
159+
Ok(Some(vector.iter().map(|x| x.unwrap()).product::<u64>()))
160+
}
161+
None => Ok(None),
162+
},
158163
})
159164
.collect::<Result<UInt64Array>>()?;
160165
Ok(Arc::new(result) as ArrayRef)

datafusion/sqllogictest/test_files/array.slt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5181,12 +5181,17 @@ select cardinality(arrow_cast([[1, 2], [3, 4], [5, 6]], 'FixedSizeList(3, List(I
51815181
query II
51825182
select cardinality(make_array()), cardinality(make_array(make_array()))
51835183
----
5184-
NULL 0
5184+
0 0
5185+
5186+
query II
5187+
select cardinality([]), cardinality([]::int[]) as with_cast
5188+
----
5189+
0 0
51855190

51865191
query II
51875192
select cardinality(arrow_cast(make_array(), 'LargeList(Int64)')), cardinality(arrow_cast(make_array(make_array()), 'LargeList(List(Int64))'))
51885193
----
5189-
NULL 0
5194+
0 0
51905195

51915196
#TODO
51925197
#https://github.com/apache/datafusion/issues/9158
@@ -5195,6 +5200,12 @@ NULL 0
51955200
#----
51965201
#NULL 0
51975202

5203+
# cardinality of NULL arrays should return NULL
5204+
query II
5205+
select cardinality(NULL), cardinality(arrow_cast(NULL, 'LargeList(Int64)'))
5206+
----
5207+
NULL NULL
5208+
51985209
# cardinality with columns
51995210
query III
52005211
select cardinality(column1), cardinality(column2), cardinality(column3) from arrays;

0 commit comments

Comments
 (0)