Skip to content

fix(arrow-array): avoid returning corrupt array in GenericByteArray::into_builder - #11311

Merged
Jefffrey merged 3 commits into
apache:mainfrom
neilconway:neilc/fix-byte-array-into-builder-offset
Oct 2, 2026
Merged

Jefffrey merged 3 commits into
apache:mainfrom
neilconway:neilc/fix-byte-array-into-builder-offset

Conversation

@neilconway

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • N/A

Rationale for this change

GenericByteArray::into_builder attempts to convert the byte array into a builder without copying the underlying buffers, returning an error if that cannot be done. When it was called with a sliced array whose first offset was non-zero, it returned a corrupted array whose offsets pointed past the underlying values array.

To fix this, refactor into_builder so that it avoids modifying the values array until we are sure that into_builder will succeed.

Along the way, we arrange to copy the input null buffer if it can't be reused; the null buffer is small, so it doesn't seem worth forcing the caller to copy the rest of the array fields if the array could otherwise be reused.

What changes are included in this PR?

  • Refactor GenericByteArray::into_builder and fix bug, as described above
  • Add unit tests
  • Adjust GenericByteBuilder::new_from_buffer to truncate any bytes in the buffer beyond the last offset

Are these changes tested?

Yes; existing tests pass, new unit tests added.

Are there any user-facing changes?

No.

…nto_builder

`into_builder` cut the values buffer to `last - first` bytes from its
start, which is only correct when the first offset is zero:

- For a sliced array, the offsets cannot be reused, so it returned `Err`
  with an array rebuilt from the cut buffers, whose offsets point past
  the end of its values.
- For an unsliced array whose offsets start above zero, it returned a
  builder whose values were cut short.

Split the array with `into_parts` and try to reuse the offsets and
values without cutting them first. If either cannot be reused, return
the original array unchanged; offsets that were already converted are
turned back without copying. Offsets that start above zero now work, as
the builder keeps the values buffer from its first byte.

`GenericByteBuilder::new_from_buffer` now drops any bytes after the last
offset itself, as new values are appended at the end of the values
buffer.

The null buffer no longer causes `Err`: it is reused when possible and
copied otherwise, since it is only one bit per row. Previously, a shared
null buffer made `into_builder` return `Err`. That is common for arrays
built by kernels that pass their input's null buffer through, and the
caller then had to copy all the buffers instead.
@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-array labels Sep 30, 2026
@Jefffrey Jefffrey added the bug Squash those bugs label Oct 1, 2026
@Jefffrey
Jefffrey merged commit c02a2aa into apache:main Oct 2, 2026
41 checks passed
@Jefffrey

Jefffrey commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

thanks @neilconway

alamb pushed a commit that referenced this pull request Oct 8, 2026
…GenericByteArray::into_builder` (#11446)

- Backport #11311
- For inclusion in #11352

Cherry-pick of
c02a2aa

Co-authored-by: Neil Conway <[email protected]>
alamb pushed a commit to Jefffrey/arrow-rs that referenced this pull request Oct 21, 2026
…:into_builder` (apache#11311)

# Which issue does this PR close?

- N/A

# Rationale for this change

`GenericByteArray::into_builder` attempts to convert the byte array into
a builder without copying the underlying buffers, returning an error if
that cannot be done. When it was called with a sliced array whose first
offset was non-zero, it returned a corrupted array whose offsets pointed
past the underlying values array.

To fix this, refactor `into_builder` so that it avoids modifying the
values array until we are sure that `into_builder` will succeed.

Along the way, we arrange to copy the input null buffer if it can't be
reused; the null buffer is small, so it doesn't seem worth forcing the
caller to copy the rest of the array fields if the array could otherwise
be reused.

# What changes are included in this PR?

* Refactor `GenericByteArray::into_builder` and fix bug, as described
above
* Add unit tests
* Adjust `GenericByteBuilder::new_from_buffer` to truncate any bytes in
the buffer beyond the last offset


# Are these changes tested?

Yes; existing tests pass, new unit tests added.

# Are there any user-facing changes?

No.
Jefffrey added a commit that referenced this pull request Oct 22, 2026
…GenericByteArray::into_builder` (#11707)

- Backport #11311
- For inclusion in #11665

Cherry-pick of
c02a2aa

Co-authored-by: Neil Conway <[email protected]>
alamb added a commit that referenced this pull request Oct 22, 2026
…GenericByteArray::into_builder` (#11816)

- Backport #11311
- For inclusion in #11666

Cherry-pick of
c02a2aa

Co-authored-by: Neil Conway <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow Changes to the arrow crate arrow-array bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants