Skip to content

fix: avoid unnecessary null bitmap allocation in MutableArrayData for Null and parent-null children - #12150

Open
lichuang wants to merge 2 commits into
apache:mainfrom
lichuang:issue-11902
Open

lichuang wants to merge 2 commits into
apache:mainfrom
lichuang:issue-11902

Conversation

@lichuang

@lichuang lichuang commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

MutableArrayData::try_with_capacities used a single use_nulls flag for the whole recursive output tree. A nullable parent therefore forced every child to allocate a validity bitmap, even when the child was DataType::Null or when the child source had no nulls and its values were already covered by the parent null mask.

For nested types such as FixedSizeList<FixedSizeList<Null>>, fixed-size list capacities multiply, so a valid input with large list sizes could request an enormous null bitmap for a child that stores no data. This is an avoidable allocation and memory-exhaustion vector.

What changes are included in this PR?

  • Never allocate a null bitmap for DataType::Null.
  • Decide child null-bitmap allocation from the child source data and child type instead of blindly propagating the parent use_nulls flag.
  • Allow parent nulls to be represented without forcing child validity bitmaps for types that can be filled with valid placeholders.
  • Keep the existing null handling for Dictionary, Union and RunEndEncoded, where placeholder values or top-level validity are not sufficient.
  • Add an internal try_extend_nulls_inner(len, mark_null) path:
    • mark_null = true marks appended slots as null.
    • mark_null = false appends valid placeholders covered by a parent null mask.
    • DataType::Null only updates length and null count.
  • Update Struct and FixedSizeList null extension to use the placeholder path for children rather than requiring child validity bitmaps.
  • Keep the validity bitmap growth in try_extend_nulls fallible. The previous revision routed it through resize_for_bits, which calls the infallible MutableBuffer::resize and panics on an allocation failure. A new try_resize_for_bits helper returns ArrowError::MemoryError instead, and the bitmap is grown before the data buffers, so a failed allocation leaves the array unchanged. A length overflow is reported the same way.
  • Update the related documentation comments.

Are these changes tested?

Yes. New regression tests cover:

  • DataType::Null never allocating a null bitmap, including when use_nulls = true.
  • The issue reproducer with nested FixedSizeList<FixedSizeList<Null>>, asserting that neither the inner list nor the null leaf allocates a null bitmap.
  • A nullable Struct whose non-null child does not allocate a null bitmap.
  • Extending the nested fixed-size list with nulls, freezing the result, and validating the resulting ArrayData.
  • A length overflow in try_extend_nulls returning ArrowError::MemoryError and leaving the builder unchanged.
  • A validity bitmap allocation failure (64-bit only) returning ArrowError::MemoryError rather than panicking, also leaving the builder unchanged.

The arrow-data tests were run with and without force_validate, and the existing arrow array transform tests were also run. Clippy and formatting checks pass.

Are there any significant user-facing changes?

No public API changes.

One behavior difference: try_extend_nulls on a MutableArrayData of type DataType::Null now succeeds even when it was created with use_nulls = false, because a Null array can always represent nulls and needs no validity bitmap.

AI usage disclosure

This change and its tests were prepared with AI assistance. I reviewed the implementation, understand the change, and verified the relevant tests, Clippy, and formatting locally.

@boring-cyborg boring-cyborg Bot added the arrow-core Core crates related to representing arrow data label Nov 10, 2026
@Rich-T-kid
Rich-T-kid self-requested a review November 10, 2026 18:47
Comment thread arrow-data/src/transform/mod.rs Outdated
self.data.null_count += len;

if let Some(nulls) = self.data.null_buffer.as_mut() {
utils::resize_for_bits(nulls, self.data.len);

@Rich-T-kid Rich-T-kid Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try_extend_nulls previously used MutableBuffer::try_resize and returned ArrowError::MemoryError when growing the validity buffer failed. resize_for_bits calls the infallible resize, so this now panics on the same allocation failure.

Since this is the fallible API and this change is meant to handle hostile allocation sizes can we keep this resize fallible, either here or with a fallible resize_for_bits helper?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 99406ad.

try_resize_for_bits now returns ArrowError::MemoryError and is used by try_extend_nulls. The validity bitmap is also grown before the data buffers, so a failed allocation leaves the array unchanged. Added regression tests for both the allocation failure and length overflow paths.

@Rich-T-kid Rich-T-kid added the bug Squash those bugs label Nov 16, 2026
…end_nulls`

`try_extend_nulls` used to grow the validity bitmap with
`MutableBuffer::try_resize` and return `ArrowError::MemoryError`. The
`resize_for_bits` helper it switched to calls the infallible `resize`,
which panics on an allocation failure, so the fallible API started
panicking again.

Add `try_resize_for_bits`, use it in `try_extend_nulls`, and grow the
validity bitmap before the data buffers so a failed allocation leaves the
array untouched. Report a length overflow as an error as well.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

data: MutableArrayData::try_with_capacities should avoid allocating a null bitmap when possible

2 participants