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 b56738c1 fix(arrow/array): handle zero-length dense union slices at an
offset (#1657)
b56738c1 is described below
commit b56738c1dcd0a00fb3f33ca3a28b875454b17f82
Author: Shivam Sharma <[email protected]>
AuthorDate: Mon Nov 23 23:36:46 2026 +0530
fix(arrow/array): handle zero-length dense union slices at an offset (#1657)
### Rationale for this change
A zero-length slice of a dense union at a nonzero offset cannot be used.
`String()` panics, and writing it with IPC fails:
```
arrow/ipc: unknown error while writing: runtime error: slice bounds out of
range [3:0]
```
`DenseUnion.setData` leaves `offsets` empty when the length is 0, but
`RawValueOffsets` slices it by the array's offset.
### What changes are included in this PR?
`RawValueOffsets` returns an empty slice when the length is 0.
`RawTypeCodes` already does the same.
### Are these changes tested?
Yes. `TestDenseUnionEmptySlice` checks `RawValueOffsets`, `String`,
`MarshalJSON`, `ValidateFull` and `Equal` on empty slices at every
offset; without the fix it panics with `slice bounds out of range
[1:0]`. `TestWriteSlicedUnion` now also covers empty slices; without the
fix its dense `3-3` and `10-10` cases fail with the error above.
On Windows amd64 with Go 1.26.9, `go test -tags assert` passes for
`./arrow/array/`, `./arrow/ipc/`, `./arrow/compute/`, `./arrow/scalar/`
and `./arrow/internal/arrjson/`, and `go vet` is clean for the two
changed packages. The full test suite was not run locally.
### Are there any user-facing changes?
Empty slices of dense unions can be printed and written with IPC. No API
changes.
This PR was prepared with AI assistance (Claude Code).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
arrow/array/union.go | 7 ++++++-
arrow/array/union_test.go | 30 ++++++++++++++++++++++++++++++
arrow/ipc/writer_test.go | 2 +-
3 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/arrow/array/union.go b/arrow/array/union.go
index cdb4179d..8e624961 100644
--- a/arrow/array/union.go
+++ b/arrow/array/union.go
@@ -617,7 +617,12 @@ func (a *DenseUnion) ValueOffsets() *memory.Buffer {
return a.data.buffers[2] }
func (a *DenseUnion) ValueOffset(i int) int32 { return
a.offsets[i+a.data.offset] }
-func (a *DenseUnion) RawValueOffsets() []int32 { return
a.offsets[a.data.offset:] }
+func (a *DenseUnion) RawValueOffsets() []int32 {
+ if a.data.length > 0 {
+ return a.offsets[a.data.offset:]
+ }
+ return []int32{}
+}
func (a *DenseUnion) setData(data *Data) {
a.union.setData(data)
diff --git a/arrow/array/union_test.go b/arrow/array/union_test.go
index 4ff848f0..7c729b13 100644
--- a/arrow/array/union_test.go
+++ b/arrow/array/union_test.go
@@ -554,6 +554,36 @@ func TestSparseUnionValidate(t *testing.T) {
})
}
+func TestDenseUnionEmptySlice(t *testing.T) {
+ mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+ defer mem.AssertSize(t, 0)
+
+ dt := arrow.DenseUnionOf([]arrow.Field{
+ {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true},
+ }, []arrow.UnionTypeCode{0})
+ bldr := array.NewDenseUnionBuilder(mem, dt)
+ defer bldr.Release()
+ for i := range int32(3) {
+ bldr.Append(0)
+ bldr.Child(0).(*array.Int32Builder).Append(i)
+ }
+ arr := bldr.NewDenseUnionArray()
+ defer arr.Release()
+
+ for offset := range int64(4) {
+ slice := array.NewSlice(arr, offset, offset).(*array.DenseUnion)
+ assert.Empty(t, slice.RawValueOffsets())
+ assert.Equal(t, "[]", slice.String())
+ assert.NoError(t, slice.ValidateFull())
+ assert.True(t, array.Equal(slice, slice))
+
+ out, err := slice.MarshalJSON()
+ assert.NoError(t, err)
+ assert.JSONEq(t, "[]", string(out))
+ slice.Release()
+ }
+}
+
func TestDenseUnionValidateEmpty(t *testing.T) {
child := array.NewNull(0)
defer child.Release()
diff --git a/arrow/ipc/writer_test.go b/arrow/ipc/writer_test.go
index 31b95159..ca948b3c 100644
--- a/arrow/ipc/writer_test.go
+++ b/arrow/ipc/writer_test.go
@@ -537,7 +537,7 @@ func TestWriteSlicedUnion(t *testing.T) {
for _, arr := range []arrow.Array{sparse, dense} {
t.Run(arr.DataType().Name(), func(t *testing.T) {
- for _, sl := range []struct{ beg, end int64 }{{0, 4},
{3, 7}, {5, n}, {9, n}} {
+ for _, sl := range []struct{ beg, end int64 }{{0, 4},
{3, 7}, {5, n}, {9, n}, {0, 0}, {3, 3}, {n, n}} {
t.Run(fmt.Sprintf("%d-%d", sl.beg, sl.end),
func(t *testing.T) {
slice := array.NewSlice(arr, sl.beg,
sl.end)
defer slice.Release()