This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-11651-ccc426cc27770b1327cc0c0c9d7e6584579965b6 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 3658fc01e8fed4179d476aa3efc34b2723f89dd3 Author: Kyon <[email protected]> AuthorDate: Sun Oct 18 09:46:46 2026 +0000 Fix IPC serialization of unions in sliced lists (#11651) # Which issue does this PR close? - Closes #11586 # Rationale for this change Writing a union inside a sliced List or LargeList currently writes unsliced union buffers despite the sliced row count. Dense unions can read back values from different rows, while sparse unions can fail with a child-length mismatch. # What changes are included in this PR? Slice union type-ID and dense-offset buffers to the selected row range, and apply that row slice to sparse children before encoding. Retain dense children and the absolute offset values within their existing child views. # Are these changes tested? A regression test round-trips List arrays containing dense or sparse unions through both File and Stream writers. It covers full, nonzero-offset and empty slices, nullable values, custom type IDs, and nonmonotonic dense offsets, comparing decoded rows and validating input/output data. AI assistance was used to analyze the serialization path and draft the code, regression test, and this description. # Are there any significant user-facing changes? No public API changes. --- arrow-ipc/src/writer.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/arrow-ipc/src/writer.rs b/arrow-ipc/src/writer.rs index 5eed1f726c..541f8e8f2e 100644 --- a/arrow-ipc/src/writer.rs +++ b/arrow-ipc/src/writer.rs @@ -2649,6 +2649,24 @@ fn write_array_data( write_options, )?; return Ok(offset); + } else if matches!(data_type, DataType::Union(_, _)) { + for (buffer, byte_width) in array_data + .buffers() + .iter() + .zip([size_of::<i8>(), size_of::<i32>()]) + { + let buffer = + buffer.slice_with_length(array_data.offset() * byte_width, num_rows * byte_width); + offset = encode_sink_buffer( + buffer, + meta, + sink, + offset, + compression_codec, + ipc_write_context, + write_options.alignment, + )?; + } } else { for buffer in array_data.buffers() { offset = encode_sink_buffer( @@ -2682,6 +2700,20 @@ fn write_array_data( )?; } } + DataType::Union(_, UnionMode::Sparse) => { + for data_ref in array_data.child_data() { + let child_data = data_ref.slice(array_data.offset(), num_rows); + offset = write_array_data( + &child_data, + meta, + sink, + offset, + compression_codec, + ipc_write_context, + write_options, + )?; + } + } _ => { // recursively write out nested structures for data_ref in array_data.child_data() { @@ -2835,6 +2867,77 @@ mod tests { use super::*; + #[test] + fn test_roundtrip_sliced_list_of_union() { + fn decoded(array: &ArrayRef) -> Vec<Vec<Option<i32>>> { + (0..array.len()) + .map(|row| { + let values = array.as_list::<i32>().value(row); + let union = values.as_union(); + (0..union.len()) + .map(|index| { + union + .value(index) + .as_primitive::<Int32Type>() + .iter() + .next() + .unwrap() + }) + .collect() + }) + .collect() + } + + for mode in [UnionMode::Dense, UnionMode::Sparse] { + let fields = UnionFields::try_new( + [3, 7], + [ + Field::new("a", DataType::Int32, true), + Field::new("b", DataType::Int32, true), + ], + ) + .unwrap(); + let offsets = (mode == UnionMode::Dense).then(|| vec![2, 0, 1].into()); + let union: ArrayRef = Arc::new( + UnionArray::try_new( + fields, + vec![3_i8, 7, 3].into(), + offsets, + vec![ + Arc::new(Int32Array::from(vec![Some(11), None, Some(33)])), + Arc::new(Int32Array::from(vec![Some(101), Some(202), None])), + ], + ) + .unwrap(), + ); + let item = Arc::new(Field::new("item", union.data_type().clone(), true)); + let list: ArrayRef = Arc::new(ListArray::new( + item, + arrow_buffer::OffsetBuffer::from_lengths([1, 2]), + union, + None, + )); + for (offset, len) in [(0, 2), (1, 1), (2, 0)] { + let array = list.slice(offset, len); + array.to_data().validate_full().unwrap(); + let expected = decoded(&array); + let schema = Arc::new(Schema::new(vec![Field::new( + "v", + array.data_type().clone(), + true, + )])); + let batch = RecordBatch::try_new(schema, vec![array]).unwrap(); + for output in [ + deserialize_file(serialize_file(&batch)), + deserialize_stream(serialize_stream(&batch)), + ] { + output.column(0).to_data().validate_full().unwrap(); + assert_eq!(decoded(output.column(0)), expected); + } + } + } + } + fn serialize_file(rb: &RecordBatch) -> Vec<u8> { let mut writer = FileWriter::try_new(vec![], rb.schema_ref()).unwrap(); writer.write(rb).unwrap();
