This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 3327fbef1c fix(arrow-data): account for sparse union parent offset in
MutableArrayData (#11693)
3327fbef1c is described below
commit 3327fbef1c3381a149e9a413a5bb22813e11639e
Author: ump45nose <[email protected]>
AuthorDate: Mon Oct 19 01:18:27 2026 +0000
fix(arrow-data): account for sparse union parent offset in MutableArrayData
(#11693)
# Which issue does this PR close?
- Closes #11686.
# Rationale for this change
`MutableArrayData::extend` on sparse union `ArrayData` with a nonzero
offset took the type ids from the right rows but copied the children's
values from the rows before the offset. So each type id was paired with
the wrong value. A sparse union's children are not sliced by the
parent's offset, and `array.buffer::<i8>(0)` applies the offset only to
the type ids.
# What changes are included in this PR?
`build_extend_sparse` in `arrow-data/src/transform/union.rs` adds
`array.offset()` to `start` before it extends the children, the same way
#10934 did for structs and #11636 did for fixed size lists. The overflow
checks match those two fixes. Dense unions are not changed, because
their offsets buffer already gives each row's child index.
# Are these changes tested?
Yes. `test_extend_sparse_union_accounts_for_parent_offset` in
`arrow-data/src/data.rs` (next to the struct and fixed size list tests)
builds a sparse union with two children and offset 1, extends it, and
checks the type ids and both children. Without the fix, child `a` comes
back as `[10, 20]` instead of `[20, 30]`.
I also ran `cargo test -p arrow-data`, `cargo test -p arrow --test
array_transform`, `cargo test -p arrow-select --lib` and `cargo clippy
-p arrow-data --all-targets -- -D warnings`.
# Are there any significant user-facing changes?
No.
AI disclosure: I used an AI coding assistant to write the fix, the test
and this description, and I reviewed all of it. The part worth a
reviewer's look is that the type ids keep using the already offset
buffer while only the children get the added offset.
---
arrow-data/src/data.rs | 41 +++++++++++++++++++++++++++++++++++++++
arrow-data/src/transform/union.rs | 15 +++++++++++++-
2 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/arrow-data/src/data.rs b/arrow-data/src/data.rs
index 6d34884808..8fea7f327d 100644
--- a/arrow-data/src/data.rs
+++ b/arrow-data/src/data.rs
@@ -2783,6 +2783,47 @@ mod tests {
assert_eq!(output.child_data()[0].buffer::<i32>(0), &[2, 3, 4, 5]);
}
+ #[test]
+ fn test_extend_sparse_union_accounts_for_parent_offset() {
+ let fields = UnionFields::try_new(
+ [0, 1],
+ [
+ Field::new("a", DataType::Int32, false),
+ Field::new("b", DataType::Int32, false),
+ ],
+ )
+ .unwrap();
+ let child_a = ArrayData::builder(DataType::Int32)
+ .len(3)
+ .add_buffer(Buffer::from_slice_ref([10, 20, 30]))
+ .build()
+ .unwrap();
+ let child_b = ArrayData::builder(DataType::Int32)
+ .len(3)
+ .add_buffer(Buffer::from_slice_ref([100, 200, 300]))
+ .build()
+ .unwrap();
+
+ // The children are left whole, as `ArrayData::slice` and C Data
+ // Interface producers leave them for a sparse union
+ let data = ArrayData::builder(DataType::Union(fields,
UnionMode::Sparse))
+ .len(2)
+ .offset(1)
+ .add_buffer(Buffer::from_slice_ref([0i8, 1, 0]))
+ .add_child_data(child_a)
+ .add_child_data(child_b)
+ .build()
+ .unwrap();
+
+ let mut mutable = MutableArrayData::new(vec![&data], false,
data.len());
+ mutable.try_extend(0, 0, data.len()).unwrap();
+ let output = mutable.freeze();
+
+ assert_eq!(output.buffer::<i8>(0), &[1, 0]);
+ assert_eq!(output.child_data()[0].buffer::<i32>(0), &[20, 30]);
+ assert_eq!(output.child_data()[1].buffer::<i32>(0), &[200, 300]);
+ }
+
#[test]
fn test_null_count() {
let mut bit_v: [u8; 2] = [0; 2];
diff --git a/arrow-data/src/transform/union.rs
b/arrow-data/src/transform/union.rs
index 410941c97d..0439c8a898 100644
--- a/arrow-data/src/transform/union.rs
+++ b/arrow-data/src/transform/union.rs
@@ -34,7 +34,10 @@ fn nullable_union_field(fields: &arrow_schema::UnionFields)
-> Result<(usize, i8
}
pub(super) fn build_extend_sparse(array: &ArrayData) -> Extend<'_> {
+ // `buffer` already applies the parent offset to the type ids
let type_ids = array.buffer::<i8>(0);
+ // but a sparse union's children are not sliced by the parent offset
+ let offset = array.offset();
Box::new(
move |mutable: &mut _MutableArrayData, index: usize, start: usize,
len: usize| {
@@ -43,8 +46,18 @@ pub(super) fn build_extend_sparse(array: &ArrayData) ->
Extend<'_> {
.buffer1
.extend_from_slice(&type_ids[start..start + len]);
+ let child_start = start.checked_add(offset).ok_or_else(|| {
+ ArrowError::InvalidArgumentError(format!(
+ "sparse union offset {offset} with start {start} overflows
usize"
+ ))
+ })?;
+ let child_end = child_start.checked_add(len).ok_or_else(|| {
+ ArrowError::InvalidArgumentError(format!(
+ "sparse union start {child_start} with length {len}
overflows usize"
+ ))
+ })?;
for child in &mut mutable.child_data {
- child.try_extend(index, start, start + len)?;
+ child.try_extend(index, child_start, child_end)?;
}
Ok(())
},