Repository navigation
fix(arrow-data): don't double-count offset when slicing struct ArrayData - #10835
Conversation
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.
|
I spent quite a while working on understanding what the current behavior is meant to be and updating the docs here |
|
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. |
…, 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.
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
left a comment
There was a problem hiding this comment.
It seems like the slice is potentially applied again
arrow-rs/arrow-array/src/array/struct_array.rs
Lines 437 to 439 in 576cc10
|
(sorry I forgot to post my review yesterday) |
…slice-double-offset
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.
|
You're right, thanks for catching that. My version left Per the I did try the simpler "only bump the parent offset, leave children alone" version first, but the IPC writer serialises struct |
Jefffrey
left a comment
There was a problem hiding this comment.
perhaps we should try add a c/c++ reproducer per comment from the previous PR to ensure we catch all edge cases:
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.
|
Added a test that round-trips through 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.
The struct_array test already covers the same data shape without going through the C data interface.
a07ac4b to
5e70d51
Compare
|
thanks @jaideeppyne & @alamb |
…, 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.
…, 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.
…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.
…, 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.
…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.
…, 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.
…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.
Which issue does this PR close?
make_arrayon sliced struct in arrow 55 #7750.Rationale for this change
Slicing a
StructArrayDatapushed the new offset into the child data and also added it to the parent's ownoffset. 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
Structarm ofArrayData::slice, keepself.offsetunchanged and let the cumulative child offsets carry the slice (a struct'sArrayDatahas 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) andtest_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
ArrayDatano longer panics on rebuild. The slicedArrayDatanow represents the offset on its children with the parent offset unchanged — consistent with howFrom<ArrayData> for StructArrayalready interprets it.