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

zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 9ee67061 fix(arrow/ipc): bound dense union rebasing to the logical 
slice (#1385)
9ee67061 is described below

commit 9ee670619924ce834c178c4d631db5be3e91435b
Author: Kyon <[email protected]>
AuthorDate: Wed Oct 21 04:23:50 2026 +0900

    fix(arrow/ipc): bound dense union rebasing to the logical slice (#1385)
    
    ### Rationale for this change
    
    Writing a middle slice of a List or LargeList containing dense unions
    fails even when the input is valid. The stream writer returns an
    index-out-of-range error, and the file writer panics. `RawTypeCodes()`
    includes the backing buffer's remaining entries, while the rebased
    output buffer holds only the logical slice length.
    
    Using the exported `array.ValidateFull` in the regression also exposes
    an empty-array issue: an empty dense-union child decoded from IPC can
    have no value-offset buffer, but union validation unconditionally
    accesses that buffer.
    
    ### What changes are included in this PR?
    
    - Limit dense union rebasing to `arr.Len()` type codes.
    - Exercise IPC roundtrips with the exported `array.ValidateFull` helper.
    - In `arrow/array/union.go`, run per-value dense offset validation only
    when the union has values. Child-type checks and nonempty offset
    validation remain unchanged.
    - Add a direct `TestDenseUnionValidateEmpty` regression in
    `arrow/array/union_test.go` for an empty dense union with a nil
    value-offset buffer.
    
    ### Are these changes tested?
    
    On Linux amd64 with Go 1.27.2:
    
    - The middle-slice regression fails without the IPC rebasing fix.
    - The requested helper replacements reproduce a panic for an empty
    dense-union List stream roundtrip before the empty-array guard.
    - The direct empty-union validation test reproduces the nil-offset panic
    when only that guard is removed, and passes with the guard.
    - All 24 roundtrip combinations pass with both fixes: List/LargeList,
    dense/sparse unions, full/middle/empty slices, and File/Stream,
    including custom type codes and a selected null child.
    - `go test ./arrow/array ./arrow/ipc -count=1` passes.
    - `go vet ./arrow/array ./arrow/ipc` passes.
    
    ### Are there any user-facing changes?
    
    Valid middle slices containing dense unions can be written and read back
    through IPC, and full validation handles empty decoded dense unions. No
    public API changes.
    
    AI assistance: Codex generated the patch, regression test, and this
    description, and ran the recorded checks.
---
 arrow/array/union.go      |  2 +-
 arrow/array/union_test.go | 12 +++++++++
 arrow/ipc/ipc_test.go     | 64 +++++++++++++++++++++++++++++++++++++++++++++++
 arrow/ipc/writer.go       |  2 +-
 4 files changed, 78 insertions(+), 2 deletions(-)

diff --git a/arrow/array/union.go b/arrow/array/union.go
index 17138f8b..cdb4179d 100644
--- a/arrow/array/union.go
+++ b/arrow/array/union.go
@@ -185,7 +185,7 @@ func (a *union) ValidateFull() error {
                }
        }
 
-       if a.unionType.Mode() == arrow.DenseMode {
+       if a.unionType.Mode() == arrow.DenseMode && a.data.length > 0 {
                // validate offsets
 
                // map logical typeid to child length
diff --git a/arrow/array/union_test.go b/arrow/array/union_test.go
index 391348ed..4ff848f0 100644
--- a/arrow/array/union_test.go
+++ b/arrow/array/union_test.go
@@ -554,6 +554,18 @@ func TestSparseUnionValidate(t *testing.T) {
        })
 }
 
+func TestDenseUnionValidateEmpty(t *testing.T) {
+       child := array.NewNull(0)
+       defer child.Release()
+
+       dt := arrow.DenseUnionOf([]arrow.Field{{Name: "null", Type: 
arrow.Null}}, []arrow.UnionTypeCode{0})
+       arr := array.NewDenseUnion(dt, 0, []arrow.Array{child}, nil, nil, 0)
+       defer arr.Release()
+
+       require.Nil(t, arr.Data().Buffers()[2])
+       assert.NoError(t, arr.ValidateFull())
+}
+
 type UnionFactorySuite struct {
        suite.Suite
 
diff --git a/arrow/ipc/ipc_test.go b/arrow/ipc/ipc_test.go
index dd635226..0f4b9213 100644
--- a/arrow/ipc/ipc_test.go
+++ b/arrow/ipc/ipc_test.go
@@ -1080,3 +1080,67 @@ func TestRecordBatchNoMetadataRoundtrip(t *testing.T) {
        // Metadata should be empty, not nil
        require.Equal(t, 0, rm.Metadata().Len())
 }
+
+func TestListOfUnionSliceRoundTrip(t *testing.T) {
+       fields := []arrow.Field{
+               {Name: "a", Type: arrow.PrimitiveTypes.Int32, Nullable: true},
+               {Name: "b", Type: arrow.PrimitiveTypes.Int32, Nullable: true},
+       }
+       codes := []arrow.UnionTypeCode{3, 7}
+       for _, unionType := range []arrow.UnionType{
+               arrow.DenseUnionOf(fields, codes), arrow.SparseUnionOf(fields, 
codes),
+       } {
+               for _, listType := range 
[]arrow.DataType{arrow.ListOf(unionType), arrow.LargeListOf(unionType)} {
+                       t.Run(listType.String(), func(t *testing.T) {
+                               mem := 
memory.NewCheckedAllocator(memory.DefaultAllocator)
+                               defer mem.AssertSize(t, 0)
+                               arr, _, err := array.FromJSON(mem, listType, 
strings.NewReader(`[[[3,11]],[[7,101],[3,null]],[[7,202],[3,33]]]`))
+                               require.NoError(t, err)
+                               defer arr.Release()
+                               list := arr.(array.ListLike)
+                               require.NoError(t, array.ValidateFull(arr))
+                               require.NoError(t, 
list.ListValues().(array.Union).ValidateFull())
+                               schema := arrow.NewSchema([]arrow.Field{{Name: 
"list", Type: listType}}, nil)
+                               for _, bounds := range [][2]int64{{0, 3}, {1, 
2}, {2, 2}} {
+                                       t.Run(fmt.Sprint(bounds), func(t 
*testing.T) {
+                                               slice := array.NewSlice(arr, 
bounds[0], bounds[1])
+                                               defer slice.Release()
+                                               rec := 
array.NewRecordBatch(schema, []arrow.Array{slice}, int64(slice.Len()))
+                                               defer rec.Release()
+                                               t.Run("stream", func(t 
*testing.T) {
+                                                       var buf bytes.Buffer
+                                                       w := 
ipc.NewWriter(&buf, ipc.WithSchema(schema), ipc.WithAllocator(mem))
+                                                       defer w.Close()
+                                                       require.NoError(t, 
w.Write(rec))
+                                                       require.NoError(t, 
w.Close())
+                                                       r, err := 
ipc.NewReader(bytes.NewReader(buf.Bytes()), ipc.WithAllocator(mem))
+                                                       require.NoError(t, err)
+                                                       defer r.Release()
+                                                       require.True(t, 
r.Next())
+                                                       assert.True(t, 
array.RecordEqual(rec, r.RecordBatch()))
+                                                       require.NoError(t, 
array.ValidateFull(r.RecordBatch().Column(0)))
+                                                       assert.False(t, 
r.Next())
+                                                       assert.NoError(t, 
r.Err())
+                                               })
+                                               t.Run("file", func(t 
*testing.T) {
+                                                       var buf bytes.Buffer
+                                                       w, err := 
ipc.NewFileWriter(&buf, ipc.WithSchema(schema), ipc.WithAllocator(mem))
+                                                       require.NoError(t, err)
+                                                       defer w.Close()
+                                                       require.NotPanics(t, 
func() { require.NoError(t, w.Write(rec)) })
+                                                       require.NoError(t, 
w.Close())
+                                                       r, err := 
ipc.NewFileReader(bytes.NewReader(buf.Bytes()), ipc.WithAllocator(mem))
+                                                       require.NoError(t, err)
+                                                       defer r.Close()
+                                                       require.Equal(t, 1, 
r.NumRecords())
+                                                       out, err := 
r.RecordBatch(0)
+                                                       require.NoError(t, err)
+                                                       assert.True(t, 
array.RecordEqual(rec, out))
+                                                       require.NoError(t, 
array.ValidateFull(out.Column(0)))
+                                               })
+                                       })
+                               }
+                       })
+               }
+       }
+}
diff --git a/arrow/ipc/writer.go b/arrow/ipc/writer.go
index 2e693b59..97eb76a8 100644
--- a/arrow/ipc/writer.go
+++ b/arrow/ipc/writer.go
@@ -1076,7 +1076,7 @@ func (w *recordEncoder) rebaseDenseUnionValueOffsets(arr 
*array.DenseUnion, offs
        // child array, when we have a sliced array, we need to re-base
        // the value offsets for each array! ew.
        unshiftedOffsets := arr.RawValueOffsets()
-       codes := arr.RawTypeCodes()
+       codes := arr.RawTypeCodes()[:arr.Len()]
 
        shiftedOffsetsBuf := memory.NewResizableBuffer(w.mem)
        shiftedOffsetsBuf.Resize(arrow.Int32Traits.BytesRequired(arr.Len()))

Reply via email to