perf: Optimize NULL handling in array_remove#21532
Merged
mbutrovich merged 2 commits intoapache:mainfrom Apr 10, 2026
Merged
Conversation
Contributor
Author
|
@Jefffrey Thanks for the review! Shameless plug: If you enjoyed reviewing this PR, there are many such similar PRs you might also enjoy 😉 |
mbutrovich
approved these changes
Apr 10, 2026
Contributor
mbutrovich
left a comment
There was a problem hiding this comment.
Thanks @neilconway! These are great improvements across the codebase.
alamb
added a commit
to apache/arrow-rs
that referenced
this pull request
Apr 14, 2026
…on_many` (#9692) ## Which issue does this PR close? - Closes #8809. ## Rationale for this change Several DataFusion PRs ([#21464](apache/datafusion#21464), [#21468](apache/datafusion#21468), [#21471](apache/datafusion#21471), [#21475](apache/datafusion#21475), [#21477](apache/datafusion#21477), [#21482](apache/datafusion#21482), [#21532](apache/datafusion#21532)) optimize NULL handling in scalar functions by replacing row-by-row null buffer construction with bulk `NullBuffer::union`. When 3+ null buffers need combining, they chain binary `union` calls, each allocating a new `BooleanBuffer`. `NullBuffer::union_many` reduces this to 1 allocation (clone + in-place ANDs). For example, from [#21482](apache/datafusion#21482): Before: ```rust [array.nulls(), from_array.nulls(), to_array.nulls(), stride.and_then(|s| s.nulls())] .into_iter() .fold(None, |acc, nulls| NullBuffer::union(acc.as_ref(), nulls)) ``` After: ```rust NullBuffer::union_many([ array.nulls(), from_array.nulls(), to_array.nulls(), stride.and_then(|s| s.nulls()), ]) ``` Per @alamb's [suggestion](#9692 (comment)), this PR also implements the general-purpose mutable bitwise operations on `BooleanArray` from #8809, following the `PrimitiveArray::unary` / `unary_mut` pattern. This builds on the `BitAndAssign`/`BitOrAssign`/`BitXorAssign` operators added to `BooleanBuffer` in #9567. ## What changes are included in this PR? **`NullBuffer::union_many(impl IntoIterator<Item = Option<&NullBuffer>>)`**: combines multiple null buffers in a single allocation (clone + in-place `&=`). Used by DataFusion for bulk null handling. **`BooleanArray` bitwise operations** (6 new public methods): Unary (`op: FnMut(u64) -> u64`): - `bitwise_unary(&self, op)` — always allocates a new array - `bitwise_unary_mut(self, op) -> Result<Self, Self>` — in-place if uniquely owned, `Err(self)` if shared - `bitwise_unary_mut_or_clone(self, op)` — in-place if uniquely owned, allocates if shared Binary (`op: FnMut(u64, u64) -> u64`): - `bitwise_bin_op(&self, rhs, op)` — always allocates, unions null buffers - `bitwise_bin_op_mut(self, rhs, op) -> Result<Self, Self>` — in-place if uniquely owned, `Err(self)` if shared, unions null buffers - `bitwise_bin_op_mut_or_clone(self, rhs, op)` — in-place if uniquely owned, allocates if shared, unions null buffers Note: #8809 proposed the binary variants take a raw buffer and `right_offset_in_bits`. This PR takes `&BooleanArray` instead, which encapsulates both and matches existing patterns like `BooleanArray::from_binary`. ## Are these changes tested? Yes. 23 tests for the `BooleanArray` bitwise methods and 6 tests for `union_many`, covering: - Basic correctness (AND, OR, NOT) - Null handling (both nullable, one nullable, no nulls, null union) - Buffer ownership (uniquely owned → in-place, shared → `Err` / fallback) - Edge cases (empty arrays, sliced arrays with non-zero offset, misaligned left/right offsets) ## Are there any user-facing changes? Six new public methods on `BooleanArray` and one new public method on `NullBuffer`. --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Rich-T-kid
pushed a commit
to Rich-T-kid/datafusion
that referenced
this pull request
Apr 21, 2026
## Which issue does this PR close? - Closes apache#21531. ## Rationale for this change `array_remove` computes the result NULL bitmap incrementally (row-by-row). This is inefficient; it is faster to compute the result NULL bitmap via the bitwise AND of the input NULL bitmaps. Benchmarks (Arm64): ``` - array_remove_binary/remove/10: 6.8ms → 6.6ms (-2.9%) - array_remove_binary/remove/100: 13.0ms → 12.7ms (-2.3%) - array_remove_binary/remove/500: 75.2ms → 76.1ms (+1.2%) - array_remove_boolean/remove/10: 4.5ms → 4.5ms (-0.6%) - array_remove_boolean/remove/100: 8.0ms → 7.8ms (-2.5%) - array_remove_boolean/remove/500: 21.0ms → 21.3ms (+1.4%) - array_remove_decimal64/remove/10: 5.6ms → 5.7ms (+1.8%) - array_remove_decimal64/remove/100: 10.0ms → 10.2ms (+2.0%) - array_remove_decimal64/remove/500: 58.6ms → 56.2ms (-4.1%) - array_remove_f64/remove/10: 5.4ms → 5.4ms (0.0%) - array_remove_f64/remove/100: 7.7ms → 7.8ms (+1.3%) - array_remove_f64/remove/500: 36.5ms → 36.8ms (+0.8%) - array_remove_fixed_size_binary/remove/10: 6.1ms → 5.4ms (-11.5%) - array_remove_fixed_size_binary/remove/100: 12.4ms → 11.6ms (-6.5%) - array_remove_fixed_size_binary/remove/500: 66.7ms → 64.4ms (-3.4%) - array_remove_int64/remove/10: 5.6ms → 5.7ms (+1.8%) - array_remove_int64/remove/100: 7.8ms → 7.8ms (-1.0%) - array_remove_int64/remove/500: 35.5ms → 36.3ms (+2.3%) - array_remove_strings/remove/10: 6.6ms → 6.8ms (+3.0%) - array_remove_strings/remove/100: 18.0ms → 18.3ms (+1.7%) - array_remove_strings/remove/500: 78.0ms → 79.7ms (+2.2%) ``` Not a massive win, but I think still worth making the change; the resulting code is also more idiomatic. ## What changes are included in this PR? * Implement optimization * Tweak `array_remove` benchmark for f64: the previous code resulted in never actually removing anything from the array, which was inconsistent with the other benchmarks and probably unintentional. ## Are these changes tested? Yes. ## Are there any user-facing changes? No.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
array_remove#21531.Rationale for this change
array_removecomputes the result NULL bitmap incrementally (row-by-row). This is inefficient; it is faster to compute the result NULL bitmap via the bitwise AND of the input NULL bitmaps.Benchmarks (Arm64):
Not a massive win, but I think still worth making the change; the resulting code is also more idiomatic.
What changes are included in this PR?
array_removebenchmark for f64: the previous code resulted in never actually removing anything from the array, which was inconsistent with the other benchmarks and probably unintentional.Are these changes tested?
Yes.
Are there any user-facing changes?
No.