Skip to content

Apache DataFusion SQL Query Engine

minor: handle NULL array input in array_remove and array_replace (#22790)

linfeng8f0635438fe6
diff --git a/datafusion/functions-nested/src/remove.rs b/datafusion/functions-nested/src/remove.rs
index 44ef56c039..9d7dd4f44d 100644
--- a/datafusion/functions-nested/src/remove.rs
+++ b/datafusion/functions-nested/src/remove.rs
@@ -403,6 +403,7 @@ fn array_remove_internal(
let list_array = array.as_list::<i64>();
general_remove::<i64>(list_array, element_array, arr_n)
}
+ DataType::Null => Ok(new_null_array(array.data_type(), array.len())),
array_type => {
exec_err!("array_remove_all does not support type '{array_type}'.")
}
@@ -425,6 +426,7 @@ fn array_remove_with_scalar_args(
let list_array = array.as_list::<i64>();
general_remove_with_scalar::<i64>(list_array, scalar_needle, max_removals)
}
+ DataType::Null => Ok(new_null_array(array.data_type(), array.len())),
array_type => exec_err!(
"array_remove/array_remove_n/array_remove_all does not support type '{array_type}'."
),
diff --git a/datafusion/functions-nested/src/replace.rs b/datafusion/functions-nested/src/replace.rs
index f129972fc7..e8a95cff96 100644
--- a/datafusion/functions-nested/src/replace.rs
+++ b/datafusion/functions-nested/src/replace.rs
@@ -631,7 +631,7 @@ fn array_replace_with_scalar_args(
let list = list_array.as_list::<i64>();
general_replace_with_scalar::<i64>(list, &needle, scalar_to, max_replacements)
}
- DataType::Null => Ok(new_null_array(list_array.data_type(), 1)),
+ DataType::Null => Ok(new_null_array(list_array.data_type(), list_array.len())),
array_type => exec_err!("array_replace does not support type '{array_type}'."),
}
}
@@ -651,7 +651,7 @@ fn array_replace_internal(
let list_array = array.as_list::<i64>();
general_replace::<i64>(list_array, from, to, arr_n)
}
- DataType::Null => Ok(new_null_array(array.data_type(), 1)),
+ DataType::Null => Ok(new_null_array(array.data_type(), array.len())),
array_type => exec_err!("array_replace does not support type '{array_type}'."),
}
}
diff --git a/datafusion/sqllogictest/test_files/array/array_remove.slt b/datafusion/sqllogictest/test_files/array/array_remove.slt
index 23ebf00239..3088042ff1 100644
--- a/datafusion/sqllogictest/test_files/array/array_remove.slt
+++ b/datafusion/sqllogictest/test_files/array/array_remove.slt
@@ -63,13 +63,32 @@ select
----
[1, NULL, 3] [NULL, 2.2, 3.3] [NULL, bc]
-#TODO: https://github.com/apache/datafusion/issues/7142
# follow PostgreSQL behavior
-#query ?
-#select
-# array_remove(NULL, 1)
-#----
-#NULL
+# A NULL-typed array argument returns NULL, matching array_replace and SQL
+# three-valued logic.
+query ?
+select array_remove(NULL, 1);
+----
+NULL
+
+query ?
+select array_remove_n(NULL, 1, 2);
+----
+NULL
+
+query ?
+select array_remove(column1, 1) from (values (NULL), (NULL), (NULL));
+----
+NULL
+NULL
+NULL
+
+query ?
+select array_remove(column1, column2) from (values (NULL, 1), (NULL, 2), (NULL, 3)) as t(column1, column2);
+----
+NULL
+NULL
+NULL
query ??
select
@@ -406,12 +425,11 @@ select array_remove_n(make_array([1, 2, 3], [4, 5, 6], [4, 5, 6], [10, 11, 12],
## array_remove_all (aliases: `list_removes`)
-#TODO: https://github.com/apache/datafusion/issues/7142
# array_remove_all with NULL elements
-#query ?
-#select array_remove_all(NULL, 1);
-#----
-#NULL
+query ?
+select array_remove_all(NULL, 1);
+----
+NULL
query ?
select array_remove_all(make_array(1, 2, 2, 1, 1), NULL);
diff --git a/datafusion/sqllogictest/test_files/array/array_replace.slt b/datafusion/sqllogictest/test_files/array/array_replace.slt
index 77793228c9..cab84007bc 100644
--- a/datafusion/sqllogictest/test_files/array/array_replace.slt
+++ b/datafusion/sqllogictest/test_files/array/array_replace.slt
@@ -118,6 +118,37 @@ select array_replace(arrow_cast(make_array(1, 2, 3, 4, 5), 'LargeList(Int64)'),
----
[1, 2, 3, 4, 5]
+# A NULL-typed array argument returns NULL, for both literal and multi-row
+# column inputs.
+query ?
+select array_replace(NULL, 1, 2);
+----
+NULL
+
+query ?
+select array_replace_n(NULL, 1, 2, 3);
+----
+NULL
+
+query ?
+select array_replace_all(NULL, 1, 2);
+----
+NULL
+
+query ?
+select array_replace(column1, 1, 2) from (values (NULL), (NULL), (NULL));
+----
+NULL
+NULL
+NULL
+
+query ?
+select array_replace(column1, column2, column3) from (values (NULL, 1, 2), (NULL, 3, 4), (NULL, 5, 6)) as t(column1, column2, column3);
+----
+NULL
+NULL
+NULL
+
# array_replace scalar function with columns #1
query ?
select array_replace(column1, column2, column3) from arrays_with_repeating_elements;