From 05b6f93d89f831008202c16c5c5b83c37cc88d8a Mon Sep 17 00:00:00 2001 From: cht42 <42912042+cht42@users.noreply.github.com> Date: Wed, 8 Jan 2025 20:11:49 +0400 Subject: [PATCH] Fix error on `array_distinct` when input is empty #13810 (#14034) * fix * add test * oops --------- Co-authored-by: Cyprien Huet --- datafusion/functions-nested/src/set_ops.rs | 5 ++++- datafusion/sqllogictest/test_files/array.slt | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/datafusion/functions-nested/src/set_ops.rs b/datafusion/functions-nested/src/set_ops.rs index 70d69170fc1e..079e0e3ed214 100644 --- a/datafusion/functions-nested/src/set_ops.rs +++ b/datafusion/functions-nested/src/set_ops.rs @@ -513,7 +513,7 @@ fn general_array_distinct( array: &GenericListArray, field: &FieldRef, ) -> Result { - if array.len() == 0 { + if array.is_empty() { return Ok(Arc::new(array.clone()) as ArrayRef); } let dt = array.value_type(); @@ -542,6 +542,9 @@ fn general_array_distinct( }; new_arrays.push(array); } + if new_arrays.is_empty() { + return Ok(Arc::new(array.clone()) as ArrayRef); + } let offsets = OffsetBuffer::new(offsets.into()); let new_arrays_ref = new_arrays.iter().map(|v| v.as_ref()).collect::>(); let values = compute::concat(&new_arrays_ref)?; diff --git a/datafusion/sqllogictest/test_files/array.slt b/datafusion/sqllogictest/test_files/array.slt index 90003b28572a..83f228b90115 100644 --- a/datafusion/sqllogictest/test_files/array.slt +++ b/datafusion/sqllogictest/test_files/array.slt @@ -5681,6 +5681,11 @@ select array_distinct(a) from values ([1, 2, 3]), (null), ([1, 3, 1]) as X(a); NULL [1, 3] +query ? +select array_distinct(arrow_cast(null, 'LargeList(Int64)')); +---- +NULL + query ? select array_distinct([]); ----