Skip to content

fix(arrow-data): don't double-count offset when slicing struct ArrayData - #10835

Merged
Jefffrey merged 6 commits into
apache:mainfrom
jaideeppyne:fix/struct-arraydata-slice-double-offset
Sep 1, 2026
Merged

Jefffrey merged 6 commits into
apache:mainfrom
jaideeppyne:fix/struct-arraydata-slice-double-offset

Conversation

@jaideeppyne

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Slicing a Struct ArrayData pushed the new offset into the child data and also added it to the parent's own offset. Rebuilding an array from the sliced data (make_array / From<ArrayData> for StructArray) then windowed the already-windowed children a second time and panicked with (offset + length) <= self.len(). This is a regression from 54.3.0 (it panics on 55.0.0 through current main).

This finishes the approach from #7596, which @alamb approved pending additional testing before it went stale.

What changes are included in this PR?

In the Struct arm of ArrayData::slice, keep self.offset unchanged and let the cumulative child offsets carry the slice (a struct's ArrayData has no buffers of its own, now guarded by an assert).

Are these changes tested?

Yes. Added test_struct_array_data_slice (the C data interface offset representation) and test_make_array_sliced_struct_data (the exact #7750 reproducer), both failing before the change and passing after. Verified no regressions across arrow-data, arrow-array, arrow-select, arrow-ord, arrow-cast, and arrow-ipc.

Are there any user-facing changes?

Slicing a struct ArrayData no longer panics on rebuild. The sliced ArrayData now represents the offset on its children with the parent offset unchanged — consistent with how From<ArrayData> for StructArray already interprets it.

Slicing a `Struct` `ArrayData` pushed the new offset into the child data
*and* also added it to the parent's own `offset`. Rebuilding an array from
the sliced data (e.g. via `make_array` / `From<ArrayData> for StructArray`)
then windowed the already-windowed children a second time and panicked with
`assertion failed: (offset + length) <= self.len()`.

Keep the parent `offset` unchanged and let the cumulative child offsets carry
the slice, matching how `From<ArrayData> for StructArray` interprets a struct's
offset. Struct `ArrayData` never carries buffers of its own, so the sliced
result now holds an empty buffer set (guarded by an assertion).

Closes apache#7750.
Closes apache#7595.
@alamb

alamb commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

I spent quite a while working on understanding what the current behavior is meant to be and updating the docs here

@jaideeppyne

Copy link
Copy Markdown
Contributor Author

Thanks for digging into this — surfacing the offset/child_data invariants in the public docs is exactly what this area needed (I'd been reasoning from the private field comments too). Once #10838 lands I'll re-check my struct-slice change against it and cite the invariants in the PR. Happy to adjust the approach (currently: keep the parent offset, push the slice into the children) if the documented semantics point elsewhere — or to rebase on #10838 if that's easier to review.

Comment thread arrow-data/src/data.rs Resolved
Comment thread arrow-array/src/array/struct_array.rs Resolved
alamb added a commit that referenced this pull request Aug 26, 2026
…, child_data and nulls (#10838)

# Which issue does this PR close?

- Part of #7595.

# Rationale for this change

The invariants governing `ArrayData::offset` are currently documented
only on the **private fields** of `ArrayData`, so they never appear in
rendered rustdoc.

- offset field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L215-L219
- child_data field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L235-L244

This was making it hard for me to reason about what a correct fix looks
like when offset-handling like #7595 / #7750 and #10835, where the
correct output of `ArrayData::slice` depends on these definitions


# What changes are included in this PR?

This PR surfaces those invariants on the public accessors so they are
visible in the docs and can be cited as the authority in code and
reviews.

# Are these changes tested?

Docs only; covered by CI doc builds.

# Are there any user-facing changes?

Documentation only — no behavior changes.
@alamb

alamb commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for digging into this — surfacing the offset/child_data invariants in the public docs is exactly what this area needed (I'd been reasoning from the private field comments too). Once #10838 lands I'll re-check my struct-slice change against it and cite the invariants in the PR. Happy to adjust the approach (currently: keep the parent offset, push the slice into the children) if the documented semantics point elsewhere — or to rebase on #10838 if that's easier to review.

Ok, have merged #10838

It seems as if the C++ implementation and Rust implementation differ in how they deal with sliced struct children, which is perhaps the root cause of this issue.

@alamb alamb left a comment

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.

It seems like the slice is potentially applied again

if parent_offset != 0 || parent_len != cd.len() {
make_array(cd.slice(parent_offset, parent_len))
} else {

Comment thread arrow-data/src/data.rs Resolved
Comment thread arrow-array/src/array/struct_array.rs Resolved
Comment thread arrow-data/src/data.rs Resolved
Comment thread arrow-data/src/data.rs Resolved
@alamb

alamb commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

(sorry I forgot to post my review yesterday)

The previous revision left `self.offset` on the parent and windowed the
children, which still applied the slice twice whenever the input already
carried a non-zero offset (as an FFI/C-data-interface import does):
`From<ArrayData> for StructArray` re-windows every child by the parent
offset, so `slice()` on such data panicked with the same
`(offset + length) <= self.len()` assertion.

Per the invariants documented in apache#10838, a struct's offset composes with
each child's offset, so the window has to be recorded in exactly one
place. Push the whole cumulative offset (`self.offset + offset`) into the
children and reset the parent's offset to 0.

Keeping the window on the children (rather than only on the parent) also
preserves what the IPC writer assumes: it serialises struct `child_data`
without applying the parent's offset, so a parent-offset-only slice would
have written the wrong values and defeated buffer truncation.

Add a regression test for slicing struct data that already carries an
offset.
@jaideeppyne

Copy link
Copy Markdown
Contributor Author

You're right, thanks for catching that. My version left self.offset on the parent, so for a struct that already carries an offset (an FFI import, say) From<ArrayData> for StructArray still re-windowed children I'd already windowed — same (offset + length) <= self.len() panic, just moved to a rarer input.

Per the ArrayData::offset docs you added in #10838, the struct offset composes cumulatively with each child's, so the window has to be recorded once. I've pushed self.offset + offset down into the children and reset the parent offset to 0, which makes lines 437-439 a no-op.

I did try the simpler "only bump the parent offset, leave children alone" version first, but the IPC writer serialises struct child_data without applying the parent offset, so that wrote wrong values for sliced maps — hence keeping the window on the children. Added a test for slicing struct data that already has an offset.

@Jefffrey Jefffrey added the bug Squash those bugs label Aug 27, 2026

@Jefffrey Jefffrey left a comment

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.

perhaps we should try add a c/c++ reproducer per comment from the previous PR to ensure we catch all edge cases:

Comment thread arrow-data/src/data.rs Resolved
Add a regression test that goes through `to_ffi`/`from_ffi` instead of
hand-building the `ArrayData`, so the offset layout under test is the one
the C data interface actually carries.

C++ slices by adjusting `offset`/`length` on the array data and leaving
`child_data` untouched, so a sliced struct crosses the boundary as a
parent with a non-zero offset over whole children. Verified against
pyarrow 25.0.1: exporting `arr.slice(1, 5)` of a 6-element struct gives
`length=5 offset=1` on the parent and `length=6 offset=0` on the child.
The test asserts the exported `FFI_ArrowArray` has that same layout, that
`from_ffi` keeps the offset on the parent, and that slicing the imported
data yields the right values. It panics without the fix in 984c306.

This is FFI-specific: the IPC format has no per-array offset, so the
writer applies the parent offset when serialising and the reader always
hands back `offset = 0` with truncated children.
@jaideeppyne

Copy link
Copy Markdown
Contributor Author

Added a test that round-trips through to_ffi/from_ffi instead of hand-building the ArrayData. I checked the layout against pyarrow 25.0.1: exporting arr.slice(1, 5) of a 6-element struct gives length=5 offset=1 on the parent and length=6 offset=0 on the child, and the test asserts the exported FFI_ArrowArray matches that before importing and slicing it. It panics without the fix.

On the IPC question, it doesn't happen there. There's no per-array offset in the IPC format, so the writer applies the parent offset and the reader always gives back offset 0 with truncated children, which is why this only shows up over the C data interface.

Drops the window terminology, which is not used elsewhere for offsets,
and the descriptions of what the code used to do.
Comment thread arrow-array/src/ffi.rs Resolved
The struct_array test already covers the same data shape without going
through the C data interface.
@jaideeppyne
jaideeppyne force-pushed the fix/struct-arraydata-slice-double-offset branch from a07ac4b to 5e70d51 Compare August 30, 2026 18:01
@Jefffrey
Jefffrey merged commit c44f8d4 into apache:main Sep 1, 2026
40 checks passed
@Jefffrey

Jefffrey commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

thanks @jaideeppyne & @alamb

Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 2, 2026
…, child_data and nulls (apache#10838)

# Which issue does this PR close?

- Part of apache#7595.

# Rationale for this change

The invariants governing `ArrayData::offset` are currently documented
only on the **private fields** of `ArrayData`, so they never appear in
rendered rustdoc.

- offset field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L215-L219
- child_data field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L235-L244

This was making it hard for me to reason about what a correct fix looks
like when offset-handling like apache#7595 / apache#7750 and apache#10835, where the
correct output of `ArrayData::slice` depends on these definitions


# What changes are included in this PR?

This PR surfaces those invariants on the public accessors so they are
visible in the docs and can be cited as the authority in code and
reviews.

# Are these changes tested?

Docs only; covered by CI doc builds.

# Are there any user-facing changes?

Documentation only — no behavior changes.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 18, 2026
…, child_data and nulls (apache#10838)

# Which issue does this PR close?

- Part of apache#7595.

# Rationale for this change

The invariants governing `ArrayData::offset` are currently documented
only on the **private fields** of `ArrayData`, so they never appear in
rendered rustdoc.

- offset field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L215-L219
- child_data field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L235-L244

This was making it hard for me to reason about what a correct fix looks
like when offset-handling like apache#7595 / apache#7750 and apache#10835, where the
correct output of `ArrayData::slice` depends on these definitions


# What changes are included in this PR?

This PR surfaces those invariants on the public accessors so they are
visible in the docs and can be cited as the authority in code and
reviews.

# Are these changes tested?

Docs only; covered by CI doc builds.

# Are there any user-facing changes?

Documentation only — no behavior changes.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 18, 2026
…ata (apache#10835)

# Which issue does this PR close?

- Closes apache#7750.
- Closes apache#7595.

# Rationale for this change

Slicing a `Struct` `ArrayData` pushed the new offset into the child data
*and* also added it to the parent's own `offset`. Rebuilding an array
from the sliced data (`make_array` / `From<ArrayData> for StructArray`)
then windowed the already-windowed children a second time and panicked
with `(offset + length) <= self.len()`. This is a regression from 54.3.0
(it panics on 55.0.0 through current main).

This finishes the approach from apache#7596, which @alamb approved pending
additional testing before it went stale.

# What changes are included in this PR?

In the `Struct` arm of `ArrayData::slice`, keep `self.offset` unchanged
and let the cumulative child offsets carry the slice (a struct's
`ArrayData` has no buffers of its own, now guarded by an assert).

# Are these changes tested?

Yes. Added `test_struct_array_data_slice` (the C data interface offset
representation) and `test_make_array_sliced_struct_data` (the exact
apache#7750 reproducer), both failing before the change and passing after.
Verified no regressions across arrow-data, arrow-array, arrow-select,
arrow-ord, arrow-cast, and arrow-ipc.

# Are there any user-facing changes?

Slicing a struct `ArrayData` no longer panics on rebuild. The sliced
`ArrayData` now represents the offset on its children with the parent
offset unchanged — consistent with how `From<ArrayData> for StructArray`
already interprets it.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 21, 2026
…, child_data and nulls (apache#10838)

# Which issue does this PR close?

- Part of apache#7595.

# Rationale for this change

The invariants governing `ArrayData::offset` are currently documented
only on the **private fields** of `ArrayData`, so they never appear in
rendered rustdoc.

- offset field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L215-L219
- child_data field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L235-L244

This was making it hard for me to reason about what a correct fix looks
like when offset-handling like apache#7595 / apache#7750 and apache#10835, where the
correct output of `ArrayData::slice` depends on these definitions


# What changes are included in this PR?

This PR surfaces those invariants on the public accessors so they are
visible in the docs and can be cited as the authority in code and
reviews.

# Are these changes tested?

Docs only; covered by CI doc builds.

# Are there any user-facing changes?

Documentation only — no behavior changes.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 21, 2026
…ata (apache#10835)

# Which issue does this PR close?

- Closes apache#7750.
- Closes apache#7595.

# Rationale for this change

Slicing a `Struct` `ArrayData` pushed the new offset into the child data
*and* also added it to the parent's own `offset`. Rebuilding an array
from the sliced data (`make_array` / `From<ArrayData> for StructArray`)
then windowed the already-windowed children a second time and panicked
with `(offset + length) <= self.len()`. This is a regression from 54.3.0
(it panics on 55.0.0 through current main).

This finishes the approach from apache#7596, which @alamb approved pending
additional testing before it went stale.

# What changes are included in this PR?

In the `Struct` arm of `ArrayData::slice`, keep `self.offset` unchanged
and let the cumulative child offsets carry the slice (a struct's
`ArrayData` has no buffers of its own, now guarded by an assert).

# Are these changes tested?

Yes. Added `test_struct_array_data_slice` (the C data interface offset
representation) and `test_make_array_sliced_struct_data` (the exact
apache#7750 reproducer), both failing before the change and passing after.
Verified no regressions across arrow-data, arrow-array, arrow-select,
arrow-ord, arrow-cast, and arrow-ipc.

# Are there any user-facing changes?

Slicing a struct `ArrayData` no longer panics on rebuild. The sliced
`ArrayData` now represents the offset on its children with the parent
offset unchanged — consistent with how `From<ArrayData> for StructArray`
already interprets it.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 25, 2026
…, child_data and nulls (apache#10838)

# Which issue does this PR close?

- Part of apache#7595.

# Rationale for this change

The invariants governing `ArrayData::offset` are currently documented
only on the **private fields** of `ArrayData`, so they never appear in
rendered rustdoc.

- offset field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L215-L219
- child_data field 

https://github.com/apache/arrow-rs/blob/7d9bdfd8a83eb66672826757c324e6fc73d20d53/arrow-data/src/data.rs#L235-L244

This was making it hard for me to reason about what a correct fix looks
like when offset-handling like apache#7595 / apache#7750 and apache#10835, where the
correct output of `ArrayData::slice` depends on these definitions


# What changes are included in this PR?

This PR surfaces those invariants on the public accessors so they are
visible in the docs and can be cited as the authority in code and
reviews.

# Are these changes tested?

Docs only; covered by CI doc builds.

# Are there any user-facing changes?

Documentation only — no behavior changes.
Rich-T-kid pushed a commit to Rich-T-kid/arrow-rs that referenced this pull request Sep 25, 2026
…ata (apache#10835)

# Which issue does this PR close?

- Closes apache#7750.
- Closes apache#7595.

# Rationale for this change

Slicing a `Struct` `ArrayData` pushed the new offset into the child data
*and* also added it to the parent's own `offset`. Rebuilding an array
from the sliced data (`make_array` / `From<ArrayData> for StructArray`)
then windowed the already-windowed children a second time and panicked
with `(offset + length) <= self.len()`. This is a regression from 54.3.0
(it panics on 55.0.0 through current main).

This finishes the approach from apache#7596, which @alamb approved pending
additional testing before it went stale.

# What changes are included in this PR?

In the `Struct` arm of `ArrayData::slice`, keep `self.offset` unchanged
and let the cumulative child offsets carry the slice (a struct's
`ArrayData` has no buffers of its own, now guarded by an assert).

# Are these changes tested?

Yes. Added `test_struct_array_data_slice` (the C data interface offset
representation) and `test_make_array_sliced_struct_data` (the exact
apache#7750 reproducer), both failing before the change and passing after.
Verified no regressions across arrow-data, arrow-array, arrow-select,
arrow-ord, arrow-cast, and arrow-ipc.

# Are there any user-facing changes?

Slicing a struct `ArrayData` no longer panics on rebuild. The sliced
`ArrayData` now represents the offset on its children with the parent
offset unchanged — consistent with how `From<ArrayData> for StructArray`
already interprets it.
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 arrow-data bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic (offset + length) <= self.len() in make_array on sliced struct in arrow 55 Slicing a struct ArrayData with an offset double-counts the offset

3 participants