You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2026/10/19 19:04:03 UTC

(arrow-rs) branch 59_maintenance updated: [59_maintenance] Fix sliced sparse union ArrayData conversion (#11679)

This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch 59_maintenance
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/59_maintenance by this push:
     new 7d225a4286 [59_maintenance] Fix sliced sparse union ArrayData conversion (#11679)
7d225a4286 is described below

commit 7d225a4286a4c866011b66fea8bfc05c73a0f5f1
Author: Jeffrey Vo <je...@gmail.com>
AuthorDate: Tue Oct 20 04:03:57 2026 +0900

    [59_maintenance] Fix sliced sparse union ArrayData conversion (#11679)
    
    - Backport https://github.com/apache/arrow-rs/pull/11649
    - For inclusion in https://github.com/apache/arrow-rs/issues/11664
    
    Cherry-pick of
    https://github.com/apache/arrow-rs/commit/a9341121687aea7881063ff07b88c14967ee9a9d
    
    ---------
    
    Co-authored-by: Kyon <25...@users.noreply.github.com>
---
 arrow-array/src/array/union_array.rs | 53 ++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/arrow-array/src/array/union_array.rs b/arrow-array/src/array/union_array.rs
index cc94adb958..b46d4aef41 100644
--- a/arrow-array/src/array/union_array.rs
+++ b/arrow-array/src/array/union_array.rs
@@ -706,6 +706,10 @@ impl From<ArrayData> for UnionArray {
         let max_id = fields.iter().map(|(i, _)| i).max().unwrap_or_default() as usize;
         let mut boxed_fields = vec![None; max_id + 1];
         for (cd, (field_id, _)) in child_data.into_iter().zip(fields.iter()) {
+            let cd = match mode {
+                UnionMode::Sparse => cd.slice(offset, len),
+                UnionMode::Dense => cd,
+            };
             boxed_fields[field_id as usize] = Some(make_array(cd));
         }
         Self {
@@ -1700,6 +1704,55 @@ mod tests {
         test_slice_union(record_batch_slice);
     }
 
+    #[test]
+    fn test_union_from_sliced_array_data() {
+        for mode in [UnionMode::Sparse, UnionMode::Dense] {
+            let fields = UnionFields::try_new(
+                [3, 7],
+                [
+                    Field::new("ints", DataType::Int32, false),
+                    Field::new("floats", DataType::Float64, false),
+                ],
+            )
+            .unwrap();
+            let offsets = (mode == UnionMode::Dense).then(|| vec![0, 1, 2, 3].into());
+            let array = UnionArray::try_new(
+                fields,
+                vec![3, 7, 3, 7].into(),
+                offsets,
+                vec![
+                    Arc::new(Int32Array::from(vec![10, 20, 30, 40])),
+                    Arc::new(Float64Array::from(vec![1.0, 2.0, 3.0, 4.0])),
+                ],
+            )
+            .unwrap();
+            for (offset, len) in [(1, 2), (0, 2), (4, 0), (0, 4)] {
+                let expected = array.slice(offset, len);
+                let actual = make_array(array.to_data().slice(offset, len));
+                let actual = actual.as_any().downcast_ref::<UnionArray>().unwrap();
+                for i in 0..len {
+                    assert_eq!(actual.value(i).to_data(), expected.value(i).to_data());
+                }
+                assert_eq!(actual.to_data(), expected.to_data());
+            }
+        }
+    }
+
+    #[test]
+    fn test_sparse_union_from_sliced_array_data_logical_nulls() {
+        let child = Int32Array::from(vec![Some(10), None, Some(30)]);
+        let fields =
+            UnionFields::try_new([0], [Field::new("ints", DataType::Int32, true)]).unwrap();
+        let array =
+            UnionArray::try_new(fields, vec![0, 0, 0].into(), None, vec![Arc::new(child)]).unwrap();
+        for (offset, len) in [(0, 1), (1, 1), (2, 1), (3, 0)] {
+            let expected = array.slice(offset, len);
+            let actual = make_array(array.to_data().slice(offset, len));
+            assert_eq!(actual.logical_nulls(), expected.logical_nulls());
+            assert_eq!(actual.to_data(), expected.to_data());
+        }
+    }
+
     #[test]
     fn test_custom_type_ids() {
         let data_type = DataType::Union(