You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jaideeppyne (via GitHub)" <gi...@apache.org> on 2026/08/25 10:02:38 UTC
[PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
jaideeppyne opened a new pull request, #10835:
URL: https://github.com/apache/arrow-rs/pull/10835
# Which issue does this PR close?
- Closes #7750.
- Closes #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 #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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "jaideeppyne (via GitHub)" <gi...@apache.org>.
jaideeppyne commented on code in PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#discussion_r3890117147
##########
arrow-array/src/ffi.rs:
##########
@@ -1192,6 +1192,65 @@ mod tests_to_then_from_ffi {
Ok(())
}
+ #[test]
+ fn test_struct_array_sliced_by_producer() -> Result<()> {
Review Comment:
Agreed, removed. Without a real producer on the other side it was building the same shape as `test_slice_struct_data_with_existing_offset` and then pushing it through ffi, so it was not testing the boundary, just costing a round trip.
`ffi.rs` is untouched by the PR now. The import I had added for it is gone too.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "jaideeppyne (via GitHub)" <gi...@apache.org>.
jaideeppyne commented on PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#issuecomment-5446878782
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "jaideeppyne (via GitHub)" <gi...@apache.org>.
jaideeppyne commented on code in PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#discussion_r3879241528
##########
arrow-data/src/data.rs:
##########
@@ -696,19 +696,34 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ // As documented on [`Self::offset`], a struct has no buffers of its
+ // own and its offset composes with each child's offset: logical
+ // element `i` is element `self.offset + i` of every child.
+ //
+ // The slice must therefore be applied exactly once. Previously it
Review Comment:
Done, that block now just says what the code does.
##########
arrow-array/src/array/struct_array.rs:
##########
@@ -732,6 +735,97 @@ mod tests {
}
}
+ #[test]
+ fn test_struct_array_data_slice() {
+ // Slicing a struct's `ArrayData` and then rebuilding an array from it
+ // must window the children exactly once. Previously the offset was
Review Comment:
Agreed, dropped it here and everywhere else in the PR. The tests now just say the offset has to be applied to the children once.
##########
arrow-data/src/data.rs:
##########
@@ -637,13 +637,22 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ assert!(
+ self.buffers.is_empty(),
+ "StructArrays should not contain buffers"
+ );
+ // A struct's offset windows its child data (and null buffer), so
+ // the slice is applied by pushing `offset` down into the children
+ // rather than by also adding it to the parent's own offset. Doing
+ // both would double-count the offset (see #7595): the parent offset
+ // would then window children that have already been windowed. We
+ // therefore keep `self.offset` unchanged and let the cumulative
+ // child offsets carry the new slice.
Review Comment:
Took the terminology point, "window" is gone from the whole PR now.
I did not apply the suggestion literally, because it describes keeping the offset on the parent and the code no longer does that. After the earlier round with @alamb the parent offset is reset to 0 and the cumulative offset goes to the children, since leaving any of it on the parent is what let `From<ArrayData> for StructArray` apply it twice. So the comment reads:
```rust
// A struct has no buffers of its own, and reading child element `i`
// combines this array's offset with the child's own offset. Applying
// the slice to both would count it twice, so the cumulative offset
// goes to the children and this array's offset is reset to 0.
```
You were also right that the old wording was wrong about the null buffer. `nulls` belongs to this array, not the children, so it is sliced by `offset` alone and the comment says that now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#issuecomment-5416802175
I spent quite a while working on understanding what the current behavior is meant to be and updating the docs here
- https://github.com/apache/arrow-rs/pull/10838
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#issuecomment-5431419137
> 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 https://github.com/apache/arrow-rs/pull/10838
It seems as if the C++ implementation and Rust implementation differ in how the deal with sliced struct children, which is perhaps the root cause of this issue.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "jaideeppyne (via GitHub)" <gi...@apache.org>.
jaideeppyne commented on PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#issuecomment-5419322877
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey merged PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#issuecomment-5431425181
(sorry I forgot to post my review yesterday)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "jaideeppyne (via GitHub)" <gi...@apache.org>.
jaideeppyne commented on code in PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#discussion_r3879239847
##########
arrow-data/src/data.rs:
##########
@@ -637,13 +637,22 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ assert!(
+ self.buffers.is_empty(),
Review Comment:
Dropped it. The rewrite after your other comment removed that assert entirely, so there is no new panic path left.
##########
arrow-data/src/data.rs:
##########
@@ -637,13 +637,22 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ assert!(
+ self.buffers.is_empty(),
+ "StructArrays should not contain buffers"
+ );
+ // A struct's offset windows its child data (and null buffer), so
+ // the slice is applied by pushing `offset` down into the children
+ // rather than by also adding it to the parent's own offset. Doing
+ // both would double-count the offset (see #7595): the parent offset
+ // would then window children that have already been windowed. We
+ // therefore keep `self.offset` unchanged and let the cumulative
+ // child offsets carry the new slice.
ArrayData {
data_type: self.data_type().clone(),
len: length,
- offset: new_offset,
- buffers: self.buffers.clone(),
+ offset: self.offset,
+ buffers: vec![],
Review Comment:
Right, and it is back to `self.buffers.clone()` now. Setting it to an empty vec was doing nothing except hiding the assumption.
##########
arrow-array/src/array/struct_array.rs:
##########
@@ -732,6 +735,97 @@ mod tests {
}
}
+ #[test]
+ fn test_struct_array_data_slice() {
Review Comment:
Thanks for running it. Same failure I get on master.
##########
arrow-data/src/data.rs:
##########
@@ -637,13 +637,22 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ assert!(
+ self.buffers.is_empty(),
+ "StructArrays should not contain buffers"
+ );
+ // A struct's offset windows its child data (and null buffer), so
+ // the slice is applied by pushing `offset` down into the children
+ // rather than by also adding it to the parent's own offset. Doing
+ // both would double-count the offset (see #7595): the parent offset
+ // would then window children that have already been windowed. We
Review Comment:
Cut, along with the rest of the history in that block.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#discussion_r3872188269
##########
arrow-data/src/data.rs:
##########
@@ -696,19 +696,34 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ // As documented on [`Self::offset`], a struct has no buffers of its
+ // own and its offset composes with each child's offset: logical
+ // element `i` is element `self.offset + i` of every child.
+ //
+ // The slice must therefore be applied exactly once. Previously it
Review Comment:
theres no need to mention the previous behaviour; just justify the current behaviour
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#discussion_r3889545328
##########
arrow-array/src/ffi.rs:
##########
@@ -1192,6 +1192,65 @@ mod tests_to_then_from_ffi {
Ok(())
}
+ #[test]
+ fn test_struct_array_sliced_by_producer() -> Result<()> {
Review Comment:
i was more imagining an actual c++/pyarrow integration; as this test is, it does the same as `test_slice_struct_data_with_existing_offset` but just with extra roundtrip steps through ffi
i think its better to remove this test if we're not going to have an explicit c++/pyarrow test, as we can already mock the data structure they create as we did in `test_slice_struct_data_with_existing_offset`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "jaideeppyne (via GitHub)" <gi...@apache.org>.
jaideeppyne commented on PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#issuecomment-5433257518
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#discussion_r3861670193
##########
arrow-data/src/data.rs:
##########
@@ -637,13 +637,22 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ assert!(
+ self.buffers.is_empty(),
+ "StructArrays should not contain buffers"
+ );
+ // A struct's offset windows its child data (and null buffer), so
+ // the slice is applied by pushing `offset` down into the children
+ // rather than by also adding it to the parent's own offset. Doing
+ // both would double-count the offset (see #7595): the parent offset
+ // would then window children that have already been windowed. We
+ // therefore keep `self.offset` unchanged and let the cumulative
+ // child offsets carry the new slice.
Review Comment:
```suggestion
// Keep existing offset and only apply new offset to child_data,
// otherwise we double count the offset since indexing to children
// considers both offset of this StructArray and the offset of the
// child array
```
i find the `window` terminology confusing, also its incorrect to state it applies to the null buffer
##########
arrow-array/src/array/struct_array.rs:
##########
@@ -732,6 +735,97 @@ mod tests {
}
}
+ #[test]
+ fn test_struct_array_data_slice() {
+ // Slicing a struct's `ArrayData` and then rebuilding an array from it
+ // must window the children exactly once. Previously the offset was
Review Comment:
im finding this window terminology a little confusing, especially as i dont think we talk about offsets like this anywhere else?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#discussion_r3853232782
##########
arrow-data/src/data.rs:
##########
@@ -637,13 +637,22 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ assert!(
+ self.buffers.is_empty(),
+ "StructArrays should not contain buffers"
+ );
+ // A struct's offset windows its child data (and null buffer), so
+ // the slice is applied by pushing `offset` down into the children
+ // rather than by also adding it to the parent's own offset. Doing
+ // both would double-count the offset (see #7595): the parent offset
+ // would then window children that have already been windowed. We
Review Comment:
I thnk the last sentence here is redundant (as it repeats what the code immediately below it does)
##########
arrow-array/src/array/struct_array.rs:
##########
@@ -732,6 +735,97 @@ mod tests {
}
}
+ #[test]
+ fn test_struct_array_data_slice() {
Review Comment:
This test fails like this without the code change
```
andrewlamb@Andrews-MacBook-Pro-3:~/Software/arrow-rs$ cargo test -p arrow-array --lib test_struct_array_data_slice
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.07s
Running unittests src/lib.rs (target/debug/deps/arrow_array-03206b6bec814e8b)
running 1 test
test array::struct_array::tests::test_struct_array_data_slice ... FAILED
failures:
---- array::struct_array::tests::test_struct_array_data_slice stdout ----
thread 'array::struct_array::tests::test_struct_array_data_slice' (35050226) panicked at arrow-data/src/data.rs:637:9:
assertion failed: end <= self.len()
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
##########
arrow-data/src/data.rs:
##########
@@ -637,13 +637,22 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ assert!(
+ self.buffers.is_empty(),
Review Comment:
this will generate a new panic -- so it is now possible that some cases that used to not panic will start doing so. I don't think that is warranted in this case -- can we change this to debug_assert?
##########
arrow-data/src/data.rs:
##########
@@ -637,13 +637,22 @@ impl ArrayData {
assert!(end <= self.len());
if let DataType::Struct(_) = self.data_type() {
- // Slice into children
- let new_offset = self.offset + offset;
+ assert!(
+ self.buffers.is_empty(),
+ "StructArrays should not contain buffers"
+ );
+ // A struct's offset windows its child data (and null buffer), so
+ // the slice is applied by pushing `offset` down into the children
+ // rather than by also adding it to the parent's own offset. Doing
+ // both would double-count the offset (see #7595): the parent offset
+ // would then window children that have already been windowed. We
+ // therefore keep `self.offset` unchanged and let the cumulative
+ // child offsets carry the new slice.
ArrayData {
data_type: self.data_type().clone(),
len: length,
- offset: new_offset,
- buffers: self.buffers.clone(),
+ offset: self.offset,
+ buffers: vec![],
Review Comment:
technically self.buffers should always be an empty vec anyways, right? So this change should be a no-op
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(arrow-data): don't double-count offset when slicing struct ArrayData [arrow-rs]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #10835:
URL: https://github.com/apache/arrow-rs/pull/10835#issuecomment-5490602020
thanks @jaideeppyne & @alamb
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org