Repository navigation
fix: avoid unnecessary null bitmap allocation in MutableArrayData for Null and parent-null children - #12150
fix: avoid unnecessary null bitmap allocation in MutableArrayData for Null and parent-null children#12150lichuang wants to merge 2 commits into
Conversation
| self.data.null_count += len; | ||
|
|
||
| if let Some(nulls) = self.data.null_buffer.as_mut() { | ||
| utils::resize_for_bits(nulls, self.data.len); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
… Null and parent-null children (apache#11902)
…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.
Which issue does this PR close?
MutableArrayData::try_with_capacitiesshould avoid allocating a null bitmap when possible #11902Rationale for this change
MutableArrayData::try_with_capacitiesused a singleuse_nullsflag for the whole recursive output tree. A nullable parent therefore forced every child to allocate a validity bitmap, even when the child wasDataType::Nullor 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?
DataType::Null.use_nullsflag.Dictionary,UnionandRunEndEncoded, where placeholder values or top-level validity are not sufficient.try_extend_nulls_inner(len, mark_null)path:mark_null = truemarks appended slots as null.mark_null = falseappends valid placeholders covered by a parent null mask.DataType::Nullonly updates length and null count.StructandFixedSizeListnull extension to use the placeholder path for children rather than requiring child validity bitmaps.try_extend_nullsfallible. The previous revision routed it throughresize_for_bits, which calls the infallibleMutableBuffer::resizeand panics on an allocation failure. A newtry_resize_for_bitshelper returnsArrowError::MemoryErrorinstead, 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.Are these changes tested?
Yes. New regression tests cover:
DataType::Nullnever allocating a null bitmap, including whenuse_nulls = true.FixedSizeList<FixedSizeList<Null>>, asserting that neither the inner list nor the null leaf allocates a null bitmap.Structwhose non-null child does not allocate a null bitmap.ArrayData.try_extend_nullsreturningArrowError::MemoryErrorand leaving the builder unchanged.ArrowError::MemoryErrorrather than panicking, also leaving the builder unchanged.The
arrow-datatests were run with and withoutforce_validate, and the existingarrowarray 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_nullson aMutableArrayDataof typeDataType::Nullnow succeeds even when it was created withuse_nulls = false, because aNullarray 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.