Skip to content

Apache DataFusion SQL Query Engine

fix: Fix make_array null input handling (#25924)

Yongting You8d2b42f1439e
diff --git a/datafusion/functions-nested/src/make_array.rs b/datafusion/functions-nested/src/make_array.rs
index a63217cd5c..2b90aca62e 100644
--- a/datafusion/functions-nested/src/make_array.rs
+++ b/datafusion/functions-nested/src/make_array.rs
@@ -142,21 +142,14 @@ pub(crate) fn make_array_inner(arrays: &[ArrayRef]) -> Result<ArrayRef> {
});
let data_type = data_type.unwrap_or(&Null);
- if data_type.is_null() {
- // Either an empty array or all nulls:
- let length = arrays.iter().map(|a| a.len()).sum();
- let array = new_null_array(&Null, length);
- Ok(Arc::new(
- SingleRowListArrayBuilder::new(array).build_list_array(),
- ))
- } else {
- array_array::<i32>(arrays, data_type.clone(), Field::LIST_FIELD_DEFAULT_NAME)
- }
+ array_array::<i32>(arrays, data_type.clone(), Field::LIST_FIELD_DEFAULT_NAME)
}
-/// Convert one or more [`ArrayRef`] of the same type into a
+/// Convert zero or more [`ArrayRef`] of the same type into a
/// `ListArray` or 'LargeListArray' depending on the offset size.
///
+/// Returns empty `ListArray` if input has zero array.
+///
/// # Example (non nested)
///
/// Calling `array(col1, col2)` where col1 and col2 are non nested
@@ -194,14 +187,22 @@ pub(crate) fn make_array_inner(arrays: &[ArrayRef]) -> Result<ArrayRef> {
/// └──────────────┘ └──────────────┘ └─────────────────────────────┘
/// col1 col2 output
/// ```
+///
+/// # Public Only for Internal Use:
+/// `datafusion-spark` uses this helper to construct arrays with its element field
+/// name. This implementation detail is not part of the supported public API.
+#[doc(hidden)]
pub fn array_array<O: OffsetSizeTrait>(
args: &[ArrayRef],
data_type: DataType,
field_name: &str,
) -> Result<ArrayRef> {
- // do not accept 0 arguments.
if args.is_empty() {
- return plan_err!("Array requires at least one argument");
+ // If input is empty, combined output is an empty array with `Null` data type
+ let array = new_null_array(&Null, 0);
+ return Ok(Arc::new(
+ SingleRowListArrayBuilder::new(array).build_list_array(),
+ ));
}
let mut data = vec![];
diff --git a/datafusion/sqllogictest/test_files/array/make_array.slt b/datafusion/sqllogictest/test_files/array/make_array.slt
index fa91dcbaab..2b2da98927 100644
--- a/datafusion/sqllogictest/test_files/array/make_array.slt
+++ b/datafusion/sqllogictest/test_files/array/make_array.slt
@@ -88,6 +88,54 @@ select make_array(NULL), make_array(NULL, NULL, NULL), make_array(make_array(NUL
----
[NULL] [NULL, NULL, NULL] [[NULL, NULL], [NULL, NULL]]
+# https://github.com/apache/datafusion/issues/21841
+# An all-NULL column still contains one input value per row.
+query ?
+SELECT make_array(n) FROM (VALUES (NULL), (NULL), (NULL)) AS t(n);
+----
+[NULL]
+[NULL]
+[NULL]
+
+query ??
+SELECT make_array(n, n), [n, NULL] FROM (VALUES (NULL), (NULL)) AS t(n);
+----
+[NULL, NULL] [NULL, NULL]
+[NULL, NULL] [NULL, NULL]
+
+query ???
+SELECT make_array(), make_array(NULL), make_array(NULL, NULL);
+----
+[] [NULL] [NULL, NULL]
+
+query ?
+SELECT make_array(n) FROM (VALUES (CAST(NULL AS INT)), (NULL)) AS t(n);
+----
+[NULL]
+[NULL]
+
+query I
+SELECT count(*) FROM (SELECT make_array(n) FROM (VALUES (NULL)) AS t(n) WHERE false);
+----
+0
+
+# Mix an all-NULL column with a non-NULL column in either argument order.
+query ??
+SELECT make_array(n, v), make_array(v, n)
+FROM (VALUES (NULL, 1), (NULL, 2)) AS t(n, v);
+----
+[NULL, 1] [1, NULL]
+[NULL, 2] [2, NULL]
+
+# Preserve NULL and non-NULL rows when combining a column with scalar arguments.
+query ???
+SELECT make_array(v), make_array(v, NULL), make_array(NULL, v, 42)
+FROM (VALUES (1), (NULL), (3)) AS t(v);
+----
+[1] [1, NULL] [NULL, 1, 42]
+[NULL] [NULL, NULL] [NULL, NULL, 42]
+[3] [3, NULL] [NULL, 3, 42]
+
# make_array with 1 columns
query ???
select make_array(a), make_array(d), make_array(e) from values;