Skip to content

Commit eaddab7

Browse files
2010YOUY01Omega359
authored andcommitted
fix: Fix make_array null input handling (apache#25924)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Closes apache#21841 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. Please explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the implementation. "COUNT(DISTINCT) returns wrong results when the column contains nulls" is the user-visible problem. --> See issue for the target bug. The change is local and small inside `make_array()` function, for special case handling, should be easy to read directly from the diff. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR. --> ## What is the testing strategy for this PR? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code Briefly describe how this PR is tested, and point to the specific tests you added. For example: 'This new feature is covered by the `sqllogictest` cases added in `foo.slt`'. If this PR does not add tests, explain why. For example, if the change is already covered by existing tests, please mention it. You should also check the `codecov` bot reply on this PR to confirm the changed code is exercised. --> slts ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 501c51c commit eaddab7

2 files changed

Lines changed: 62 additions & 13 deletions

File tree

‎datafusion/functions-nested/src/make_array.rs‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,14 @@ pub(crate) fn make_array_inner(arrays: &[ArrayRef]) -> Result<ArrayRef> {
142142
});
143143

144144
let data_type = data_type.unwrap_or(&Null);
145-
if data_type.is_null() {
146-
// Either an empty array or all nulls:
147-
let length = arrays.iter().map(|a| a.len()).sum();
148-
let array = new_null_array(&Null, length);
149-
Ok(Arc::new(
150-
SingleRowListArrayBuilder::new(array).build_list_array(),
151-
))
152-
} else {
153-
array_array::<i32>(arrays, data_type.clone(), Field::LIST_FIELD_DEFAULT_NAME)
154-
}
145+
array_array::<i32>(arrays, data_type.clone(), Field::LIST_FIELD_DEFAULT_NAME)
155146
}
156147

157-
/// Convert one or more [`ArrayRef`] of the same type into a
148+
/// Convert zero or more [`ArrayRef`] of the same type into a
158149
/// `ListArray` or 'LargeListArray' depending on the offset size.
159150
///
151+
/// Returns empty `ListArray` if input has zero array.
152+
///
160153
/// # Example (non nested)
161154
///
162155
/// 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> {
194187
/// └──────────────┘ └──────────────┘ └─────────────────────────────┘
195188
/// col1 col2 output
196189
/// ```
190+
///
191+
/// # Public Only for Internal Use:
192+
/// `datafusion-spark` uses this helper to construct arrays with its element field
193+
/// name. This implementation detail is not part of the supported public API.
194+
#[doc(hidden)]
197195
pub fn array_array<O: OffsetSizeTrait>(
198196
args: &[ArrayRef],
199197
data_type: DataType,
200198
field_name: &str,
201199
) -> Result<ArrayRef> {
202-
// do not accept 0 arguments.
203200
if args.is_empty() {
204-
return plan_err!("Array requires at least one argument");
201+
// If input is empty, combined output is an empty array with `Null` data type
202+
let array = new_null_array(&Null, 0);
203+
return Ok(Arc::new(
204+
SingleRowListArrayBuilder::new(array).build_list_array(),
205+
));
205206
}
206207

207208
let mut data = vec![];

‎datafusion/sqllogictest/test_files/array/make_array.slt‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,54 @@ select make_array(NULL), make_array(NULL, NULL, NULL), make_array(make_array(NUL
8888
----
8989
[NULL] [NULL, NULL, NULL] [[NULL, NULL], [NULL, NULL]]
9090

91+
# https://github.com/apache/datafusion/issues/21841
92+
# An all-NULL column still contains one input value per row.
93+
query ?
94+
SELECT make_array(n) FROM (VALUES (NULL), (NULL), (NULL)) AS t(n);
95+
----
96+
[NULL]
97+
[NULL]
98+
[NULL]
99+
100+
query ??
101+
SELECT make_array(n, n), [n, NULL] FROM (VALUES (NULL), (NULL)) AS t(n);
102+
----
103+
[NULL, NULL] [NULL, NULL]
104+
[NULL, NULL] [NULL, NULL]
105+
106+
query ???
107+
SELECT make_array(), make_array(NULL), make_array(NULL, NULL);
108+
----
109+
[] [NULL] [NULL, NULL]
110+
111+
query ?
112+
SELECT make_array(n) FROM (VALUES (CAST(NULL AS INT)), (NULL)) AS t(n);
113+
----
114+
[NULL]
115+
[NULL]
116+
117+
query I
118+
SELECT count(*) FROM (SELECT make_array(n) FROM (VALUES (NULL)) AS t(n) WHERE false);
119+
----
120+
0
121+
122+
# Mix an all-NULL column with a non-NULL column in either argument order.
123+
query ??
124+
SELECT make_array(n, v), make_array(v, n)
125+
FROM (VALUES (NULL, 1), (NULL, 2)) AS t(n, v);
126+
----
127+
[NULL, 1] [1, NULL]
128+
[NULL, 2] [2, NULL]
129+
130+
# Preserve NULL and non-NULL rows when combining a column with scalar arguments.
131+
query ???
132+
SELECT make_array(v), make_array(v, NULL), make_array(NULL, v, 42)
133+
FROM (VALUES (1), (NULL), (3)) AS t(v);
134+
----
135+
[1] [1, NULL] [NULL, 1, 42]
136+
[NULL] [NULL, NULL] [NULL, NULL, 42]
137+
[3] [3, NULL] [NULL, 3, 42]
138+
91139
# make_array with 1 columns
92140
query ???
93141
select make_array(a), make_array(d), make_array(e) from values;

0 commit comments

Comments
 (0)