This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12251-112f01f195e080af7625b6420772240c8a9f9215 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 57071551e6329209356a550f7683b6b1e4572871 Author: mola <[email protected]> AuthorDate: Sun Nov 15 15:54:03 2026 +0000 fix(arrow-buffer): return None from NullBufferBuilder when it has no nulls (#12251) - Closes #12238 # Rationale for this change `NullBufferBuilder::finish` documents "Returns `None` if the builder only contains `true`s", but once the bitmap was materialized it returned `Some` even when the builder contained no nulls — for example after `append_n_nulls(0)`, or after `set_bit(i, true)` cleared the only null. `finish_cloned` and `build` behaved the same way. Kernels relying on the documented `None` to take their no-null paths would instead receive a null buffer with a zero null count. # What changes are included in this PR? - `append_n_nulls(0)` and `set_bit(i, true)` before the bitmap exists are now no-ops: neither materializes the bitmap. Out-of-bounds `set_bit` still panics, as before. - `finish`, `finish_cloned`, and `build` return `None` when `null_count() == 0`. This also covers nulls lost through `truncate` or `as_slice_mut`, and a bitmap passed to `new_from_buffer` with all bits set. `NullBuffer::new` already counts the nulls, so the check adds no work. # Are these changes tested? Yes. Six new tests in `arrow-buffer/src/builder/null.rs` covering the issue's exact repro (both the `finish`/`finish_cloned` case and the `set_bit` case), the no-materialization cases, nulls removed by `truncate` and `as_slice_mut`, an all-valid buffer from `new_from_buffer`, and the preserved out-of-bounds `set_bit` panic. The full crate suite and the downstream `arrow-data`, `arrow-array`, and `arrow-select` suites pass, and `cargo fmt` and `cargo clippy` are clean. <img width="971" height="654" alt="arrow-rs-12238-verify-arrow-buffer" src="https://github.com/user-attachments/assets/13d314d0-1161-4be2-96a9-3c059bee0bb8" /> <img width="899" height="171" alt="arrow-rs-12238-verify-downstream" src="https://github.com/user-attachments/assets/3b499584-04f8-4f9c-9b1b-6fa80febf84d" /> # Are there any significant user-facing changes? Behavior fix only: `finish`, `finish_cloned`, and `build` now return `None` when the builder contains no nulls, as documented. No public API change. # AI usage disclosure AI assistance was used to implement the fix and tests, following the approach described in #12238; the changes were verified with the crate's test suite, clippy and fmt. --- arrow-buffer/src/builder/null.rs | 85 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 4 deletions(-) diff --git a/arrow-buffer/src/builder/null.rs b/arrow-buffer/src/builder/null.rs index 7cf8b6f5eb..c1e61bf6e7 100644 --- a/arrow-buffer/src/builder/null.rs +++ b/arrow-buffer/src/builder/null.rs @@ -116,6 +116,9 @@ impl NullBufferBuilder { /// to indicate that these `n` items are nulls. #[inline] pub fn append_n_nulls(&mut self, n: usize) { + if n == 0 { + return; + } self.materialize_if_needed().append_n(n, false); } @@ -164,6 +167,12 @@ impl NullBufferBuilder { /// Panics for the same reasons as [`BooleanBufferBuilder::set_bit`] #[inline] pub fn set_bit(&mut self, index: usize, v: bool) { + // Setting a bit to `true` before the bitmap exists changes nothing: + // every bit is implicitly `true` until a null is appended. + // Out-of-bounds indices still panic, as before. + if v && self.bitmap_builder.is_none() && index < self.len { + return; + } self.materialize_if_needed().set_bit(index, v); } @@ -228,20 +237,27 @@ impl NullBufferBuilder { /// when you don't need to reuse this builder. pub fn finish(&mut self) -> Option<NullBuffer> { self.len = 0; - Some(NullBuffer::new(self.bitmap_builder.take()?.build())) + self.bitmap_builder + .take() + .map(|builder| NullBuffer::new(builder.build())) + .filter(|buffer| buffer.null_count() > 0) } /// Builds the [`NullBuffer`] without resetting the builder. /// /// This consumes the builder. Use [`Self::finish`] to reuse it. pub fn build(self) -> Option<NullBuffer> { - self.bitmap_builder.map(NullBuffer::from) + self.bitmap_builder + .map(NullBuffer::from) + .filter(|buffer| buffer.null_count() > 0) } /// Builds the [NullBuffer] without resetting the builder. pub fn finish_cloned(&self) -> Option<NullBuffer> { - let buffer = self.bitmap_builder.as_ref()?.finish_cloned(); - Some(NullBuffer::new(buffer)) + self.bitmap_builder + .as_ref() + .map(|builder| NullBuffer::new(builder.finish_cloned())) + .filter(|buffer| buffer.null_count() > 0) } /// Returns the inner bitmap builder as slice @@ -465,4 +481,65 @@ mod tests { assert_eq!(builder.finish(), None); } + + #[test] + fn test_finish_returns_none_when_no_nulls_were_appended() { + // append_n_nulls(0) must not materialize the bitmap (#12238) + let mut builder = NullBufferBuilder::new(0); + builder.append_n_nulls(0); + builder.append_non_null(); + assert_eq!(builder.allocated_size(), 0); + assert!(builder.finish_cloned().is_none()); + assert!(builder.finish().is_none()); + + // set_bit(i, true) can clear the only null (#12238) + let mut builder = NullBufferBuilder::new(0); + builder.append_null(); + builder.set_bit(0, true); + assert!(builder.build().is_none()); + } + + #[test] + fn test_set_bit_true_does_not_materialize() { + let mut builder = NullBufferBuilder::new(0); + builder.append_n_non_nulls(4); + builder.set_bit(2, true); + assert_eq!(builder.allocated_size(), 0); + assert!(builder.finish().is_none()); + } + + #[test] + #[should_panic(expected = "index out of bounds")] + fn test_set_bit_true_out_of_bounds_panics() { + let mut builder = NullBufferBuilder::new(0); + builder.append_n_non_nulls(2); + builder.set_bit(8, true); + } + + #[test] + fn test_finish_returns_none_after_truncate_removes_nulls() { + let mut builder = NullBufferBuilder::new(0); + builder.append_non_null(); + builder.append_null(); + builder.truncate(1); + assert!(builder.finish().is_none()); + } + + #[test] + fn test_finish_returns_none_after_clearing_nulls_via_slice_mut() { + let mut builder = NullBufferBuilder::new(0); + builder.append_null(); + builder.append_non_null(); + builder.as_slice_mut().unwrap()[0] = 0b11; + assert!(builder.finish().is_none()); + } + + #[test] + fn test_finish_returns_none_for_all_valid_buffer() { + let mut buffer = MutableBuffer::new(1); + buffer.push(0xFFu8); + let builder = NullBufferBuilder::new_from_buffer(buffer, 8); + assert!(builder.finish_cloned().is_none()); + assert!(builder.build().is_none()); + } }
