Skip to content

Commit 60bc550

Browse files
authored
[branch-54] backport #23192 array_compact handle edge case with NULLs (#23196)
## 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 #123` indicates that this PR will close issue #123. --> - Closes #. ## Rationale for this change Backport `array_compact` handle edge case with NULLs. The issue causing correctness issues and has no breaking API, therefore should fall into backport criteria <!-- 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. --> ## 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. --> ## Are these changes tested? <!-- 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 If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## 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 21e5d49 commit 60bc550

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

datafusion/functions-nested/src/array_compact.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,22 @@ fn compact_list<O: OffsetSizeTrait>(
130130
field: &Arc<arrow::datatypes::Field>,
131131
) -> Result<ArrayRef> {
132132
let values = list_array.values();
133-
134-
// Fast path: no nulls in values, return input unchanged
135-
if values.null_count() == 0 {
133+
// Use logical nulls so element types without a validity buffer
134+
// (e.g. NullArray) are still treated as null.
135+
let Some(values_nulls) = values.logical_nulls() else {
136+
// Fast path: no validity buffer, no nulls to remove
137+
return Ok(Arc::new(list_array.clone()));
138+
};
139+
let values_null_count = values_nulls.null_count();
140+
if values_null_count == 0 {
141+
// Fast path: validity buffer present but no nulls set
136142
return Ok(Arc::new(list_array.clone()));
137143
}
138144

145+
let list_nulls = list_array.nulls();
146+
let list_offsets = list_array.offsets();
139147
let original_data = values.to_data();
140-
let capacity = original_data.len() - values.null_count();
148+
let capacity = original_data.len() - values_null_count;
141149
let mut offsets = Vec::<O>::with_capacity(list_array.len() + 1);
142150
offsets.push(O::zero());
143151
let mut mutable = MutableArrayData::with_capacities(
@@ -147,25 +155,25 @@ fn compact_list<O: OffsetSizeTrait>(
147155
);
148156

149157
for row_index in 0..list_array.len() {
150-
if list_array.nulls().is_some_and(|n| n.is_null(row_index)) {
158+
if list_nulls.is_some_and(|n| n.is_null(row_index)) {
151159
offsets.push(offsets[row_index]);
152160
continue;
153161
}
154162

155-
let start = list_array.offsets()[row_index].as_usize();
156-
let end = list_array.offsets()[row_index + 1].as_usize();
157-
let mut copied = 0usize;
163+
let start = list_offsets[row_index].as_usize();
164+
let end = list_offsets[row_index + 1].as_usize();
165+
let row_null_count = values_nulls.slice(start, end - start).null_count();
166+
let kept = (end - start) - row_null_count;
158167

159168
// Batch consecutive non-null elements into single extend() calls
160169
// to reduce per-element overhead. For [1, 2, NULL, 3, 4] this
161170
// produces 2 extend calls (0..2, 3..5) instead of 4 individual ones.
162171
let mut batch_start: Option<usize> = None;
163172
for i in start..end {
164-
if values.is_null(i) {
173+
if values_nulls.is_null(i) {
165174
// Null breaks the current batch — flush it
166175
if let Some(bs) = batch_start {
167176
mutable.extend(0, bs, i);
168-
copied += i - bs;
169177
batch_start = None;
170178
}
171179
} else if batch_start.is_none() {
@@ -175,17 +183,16 @@ fn compact_list<O: OffsetSizeTrait>(
175183
// Flush any remaining batch after the loop
176184
if let Some(bs) = batch_start {
177185
mutable.extend(0, bs, end);
178-
copied += end - bs;
179186
}
180187

181-
offsets.push(offsets[row_index] + O::usize_as(copied));
188+
offsets.push(offsets[row_index] + O::usize_as(kept));
182189
}
183190

184191
let new_values = make_array(mutable.freeze());
185192
Ok(Arc::new(GenericListArray::<O>::try_new(
186193
Arc::clone(field),
187194
OffsetBuffer::new(offsets.into()),
188195
new_values,
189-
list_array.nulls().cloned(),
196+
list_nulls.cloned(),
190197
)?))
191198
}

datafusion/sqllogictest/test_files/array/array_distinct.slt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ select array_compact(arrow_cast([NULL, NULL, NULL], 'List(Int64)'));
137137
----
138138
[]
139139

140+
# all nulls with untyped NULLs (List(Null) inner values are a NullArray)
141+
query ?
142+
select array_compact(make_array(NULL, NULL, NULL));
143+
----
144+
[]
145+
140146
# empty array
141147
query ?
142148
select array_compact([]);
@@ -167,6 +173,17 @@ select array_compact([make_array(1, 2), NULL, make_array(3, 4)]);
167173
----
168174
[[1, 2], [3, 4]]
169175

176+
# nested array of all-null inner lists: outer elements are non-null, kept as-is
177+
query ?
178+
select array_compact(make_array(make_array(NULL, NULL, NULL), make_array(NULL, NULL)));
179+
----
180+
[[NULL, NULL, NULL], [NULL, NULL]]
181+
182+
query ?
183+
select array_compact(make_array(make_array(NULL, NULL, NULL), make_array(NULL, NULL, NULL)));
184+
----
185+
[[NULL, NULL, NULL], [NULL, NULL, NULL]]
186+
170187
# LargeList
171188
query ?
172189
select array_compact(arrow_cast([1, NULL, 2, NULL, 3], 'LargeList(Int64)'));

0 commit comments

Comments
 (0)