Skip to content

Don't double-count the offset when slicing struct data from C++ - #7596

Closed
westonpace wants to merge 6 commits into
apache:mainfrom
westonpace:fix/slice-struct-with-offset
Closed

westonpace wants to merge 6 commits into
apache:mainfrom
westonpace:fix/slice-struct-with-offset

Conversation

@westonpace

@westonpace westonpace commented Jun 3, 2025 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

See PR

What changes are included in this PR?

ArrayData.slice was changed so that the new offset is only pushed into the child arrays during slicing. It is not added to the parent data's offset.

Are there any user-facing changes?

No

@github-actions github-actions Bot added the arrow Changes to the arrow crate label Jun 3, 2025

@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.

Thank you @westonpace -- I had some questions about the test, but otherwise this seems reasonable to me

Some(NullBuffer::from(vec![true, false, true, true, true, true])),
);

let cpp_sliced_array = make_array(

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.

I wonder if there is some way to add a reproducer with actual data that came from C/C++. I am not sure how to verify that this reproducer accurately reflects the C/C++ code (not that I doubt you, I just don't know how to double check it)

Maybe we can add something to https://github.com/apache/arrow-testing/tree/master/data?

Or does it only happen when passing across IPC boundaries? If so maybe we can add a test to the integration tests?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good question. I believe (but am not 100% sure) that the IPC format does not have an equivalent of array offsets.

However, I agree that some kind of C data integration test would be a good idea. I'll think on how best to add this. Are there any integration tests today that are able to utilize datafusion-python? If so, pyarrow makes it pretty easy to create this kind of integration testing.

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.

}

#[test]
fn test_struct_array_data_slice() {

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.

I did verify that this test fails without the change in this PR


assertion failed: (offset + length) <= self.len()
thread 'array::struct_array::tests::test_struct_array_data_slice' panicked at arrow-data/src/data.rs:550:9:
assertion failed: (offset + length) <= self.len()

@rluvaton

Copy link
Copy Markdown
Member

It seems like it also fixes my issue:

@alamb

alamb commented Jun 23, 2025

Copy link
Copy Markdown
Contributor

Thanks @rluvaton -- I think the only thing we are waiting on for this PR is some additional testing

@rluvaton rluvaton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread arrow-data/src/data.rs
data_type: self.data_type().clone(),
len: length,
offset: new_offset,
offset: self.offset,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment that offsets are for buffers and child data,
buffers are empty for struct (I would add an assertion for that) and offset for child data is cumulative, so keeping this offset as we already update the offset for the children.

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.

Seeing as the offsets propagate, wouldn't it maybe be better to just keep this as offset: new_offset and not touch child_data at all?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does seem simpler. It looks like impl From<ArrayData> for StructArray already pushes offsets into children so it isn't less work but it keeps the work in one spot instead of two as this entire if statement could be removed.

Sorry though, I've been away from this for so long I don't even really remember the source issue.

@Jefffrey

Copy link
Copy Markdown
Contributor

Should we create a followup issue for creating an integration test for this case, to get this PR moving along?

@alamb

alamb commented Jan 12, 2026

Copy link
Copy Markdown
Contributor

Should we create a followup issue for creating an integration test for this case, to get this PR moving along?

Yeah -- or maybe ask a tool like codex or claude to try and make one

@Jefffrey

Copy link
Copy Markdown
Contributor

Took the liberty of merging up from main & applying the comments as suggested by @rluvaton

@github-actions

Copy link
Copy Markdown

Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.

@github-actions github-actions Bot added the Stale Used by workflow to mark PRs that will be auto-closed label Jun 26, 2026
@github-actions github-actions Bot closed this Jul 5, 2026
Jefffrey pushed a commit that referenced this pull request Sep 1, 2026
…ata (#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.
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
…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
…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 Stale Used by workflow to mark PRs that will be auto-closed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slicing a struct ArrayData with an offset double-counts the offset

5 participants