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 c0b55e23 fix(arrow/scalar): build empty arrays from dictionary scalars 
of any value type (#1486)
c0b55e23 is described below

commit c0b55e234d6fbcac52b6f10e2a07974f26b99a63
Author: Harrison Crosse <[email protected]>
AuthorDate: Mon Nov 9 12:18:35 2026 -0500

    fix(arrow/scalar): build empty arrays from dictionary scalars of any value 
type (#1486)
    
    ### Rationale for this change
    
    `MakeArrayFromScalar` panics at length zero for dictionary scalars whose
    value types the builder doesn't support.
    
    ### What changes are included in this PR?
    
    Length zero now uses `MakeArrayOfNull`, removing the run-end encoded
    special case.
    
    `MakeArrayOfNull` sized its zeroed buffer, shared with nested children,
    without considering list, map or list view value types, leaving dense
    union children too little space. This bug is reachable on main. [C++
    includes the value
    
type](https://github.com/apache/arrow/blob/1ebb9379b2f635755e773b79e88cb99852c70455/cpp/src/arrow/array/util.cc#L351-L364),
    and this change does too.
    
    A valid dictionary scalar produces an empty dictionary, as the old
    builder did, whereas C++ keeps the scalar's.
    
    ### Are these changes tested?
    
    `TestMakeArrayFromDictionaryScalarZeroLength` and
    `TestMakeArrayOfNullDenseUnionUnderList` fail on main and pass with this
    change. `TestMakeArrayFromScalarSupportsZeroLength` gains a
    `list<dense_union<i: int64>>` case.
    
    On macOS arm64 with Go 1.26.10, `go test ./arrow/...` with arrow-testing
    and parquet-testing checked out, `-race -tags assert,test` tests, vet,
    lint and the Release Audit Tool passed.
    
    ### Are there any user-facing changes?
    
    Yes. Both functions return valid arrays for these types instead of
    panicking or producing invalid arrays. There is no API change.
    
    Closes https://github.com/apache/arrow-go/issues/1482.
---
 arrow/array/util.go         |  7 ++++++
 arrow/array/util_test.go    | 28 ++++++++++++++++++++++++
 arrow/scalar/scalar.go      | 22 +------------------
 arrow/scalar/scalar_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 89 insertions(+), 21 deletions(-)

diff --git a/arrow/array/util.go b/arrow/array/util.go
index a77fa5c8..df3125e0 100644
--- a/arrow/array/util.go
+++ b/arrow/array/util.go
@@ -466,10 +466,17 @@ func getMaxBufferLen(dt arrow.DataType, length int) int {
                }
                return bufferLen
        case *arrow.ListViewType:
+               // list values are empty, but a dense union among them still
+               // creates children of length 1 that share this buffer
+               bufferLen = maxOf(getMaxBufferLen(dt.Elem(), 0))
                return maxOf(dt.OffsetTypeTraits().BytesRequired(length))
        case *arrow.LargeListViewType:
+               bufferLen = maxOf(getMaxBufferLen(dt.Elem(), 0))
                return maxOf(dt.OffsetTypeTraits().BytesRequired(length))
        case arrow.OffsetsDataType:
+               if list, ok := dt.(arrow.ListLikeType); ok {
+                       bufferLen = maxOf(getMaxBufferLen(list.Elem(), 0))
+               }
                return maxOf(dt.OffsetTypeTraits().BytesRequired(length + 1))
        case arrow.BinaryViewDataType:
                return maxOf(arrow.ViewHeaderSizeBytes * length)
diff --git a/arrow/array/util_test.go b/arrow/array/util_test.go
index 5631ff19..3abd1a57 100644
--- a/arrow/array/util_test.go
+++ b/arrow/array/util_test.go
@@ -193,6 +193,34 @@ func TestMakeArrayOfNullUnions(t *testing.T) {
        }
 }
 
+func TestMakeArrayOfNullDenseUnionUnderList(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       defer mem.AssertSize(t, 0)
+
+       union := arrow.DenseUnionOf(
+               []arrow.Field{{Name: "b", Type: 
&arrow.FixedSizeBinaryType{ByteWidth: 16}, Nullable: true}},
+               []arrow.UnionTypeCode{0},
+       )
+       for _, typ := range []arrow.DataType{
+               arrow.ListOf(union),
+               arrow.LargeListOf(union),
+               arrow.MapOf(arrow.BinaryTypes.String, union),
+               arrow.ListViewOf(union),
+               arrow.LargeListViewOf(union),
+               arrow.StructOf(arrow.Field{Name: "u", Type: union, Nullable: 
true}),
+               arrow.FixedSizeListOf(2, union),
+       } {
+               t.Run(typ.String(), func(t *testing.T) {
+                       for _, length := range []int{0, 1} {
+                               arr := array.MakeArrayOfNull(mem, typ, length)
+                               assert.Equal(t, length, arr.Len())
+                               assert.NoError(t, array.ValidateFull(arr), 
"length %d", length)
+                               arr.Release()
+                       }
+               })
+       }
+}
+
 func TestMakeArrayOfNullNestedNull(t *testing.T) {
        mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
        defer mem.AssertSize(t, 0)
diff --git a/arrow/scalar/scalar.go b/arrow/scalar/scalar.go
index 34166df6..42e48af4 100644
--- a/arrow/scalar/scalar.go
+++ b/arrow/scalar/scalar.go
@@ -781,27 +781,7 @@ func MakeArrayFromScalar(sc Scalar, length int, mem 
memory.Allocator) (arrow.Arr
                return nil, fmt.Errorf("%w: array length must be non-negative, 
got %d", arrow.ErrInvalid, length)
        }
 
-       if length == 0 {
-               if dt, ok := sc.DataType().(*arrow.RunEndEncodedType); ok {
-                       runEndsBuilder := array.NewBuilder(mem, dt.RunEnds())
-                       defer runEndsBuilder.Release()
-                       runEnds := runEndsBuilder.NewArray()
-                       defer runEnds.Release()
-
-                       valuesBuilder := array.NewBuilder(mem, dt.Encoded())
-                       defer valuesBuilder.Release()
-                       values := valuesBuilder.NewArray()
-                       defer values.Release()
-
-                       return array.NewRunEndEncodedArray(runEnds, values, 0, 
0), nil
-               }
-
-               builder := array.NewBuilder(mem, sc.DataType())
-               defer builder.Release()
-               return builder.NewArray(), nil
-       }
-
-       if !sc.IsValid() {
+       if length == 0 || !sc.IsValid() {
                return MakeArrayOfNull(sc.DataType(), length, mem), nil
        }
 
diff --git a/arrow/scalar/scalar_test.go b/arrow/scalar/scalar_test.go
index d1078974..50fd9f4a 100644
--- a/arrow/scalar/scalar_test.go
+++ b/arrow/scalar/scalar_test.go
@@ -32,6 +32,7 @@ import (
        "github.com/apache/arrow-go/v18/arrow/decimal"
        "github.com/apache/arrow-go/v18/arrow/decimal128"
        "github.com/apache/arrow-go/v18/arrow/decimal256"
+       "github.com/apache/arrow-go/v18/arrow/extensions"
        "github.com/apache/arrow-go/v18/arrow/memory"
        "github.com/apache/arrow-go/v18/arrow/scalar"
        "github.com/apache/arrow-go/v18/internal/types"
@@ -1717,6 +1718,58 @@ func TestMakeArrayFromScalarSupportsZeroLength(t 
*testing.T) {
        assert.Equal(t, 0, nullArr.Len())
        assert.Equal(t, 0, nullArr.NullN())
        require.NoError(t, array.ValidateFull(nullArr))
+
+       unionType := arrow.DenseUnionOf([]arrow.Field{{Name: "i", Type: 
arrow.PrimitiveTypes.Int64, Nullable: true}}, []arrow.UnionTypeCode{0})
+       listType := arrow.ListOf(unionType)
+       unionValues := array.MakeArrayOfNull(mem, unionType, 1)
+       defer unionValues.Release()
+       listOfUnion := scalar.NewListScalar(unionValues)
+       defer listOfUnion.Release()
+       for _, s := range []scalar.Scalar{listOfUnion, 
scalar.MakeNullScalar(listType)} {
+               arr, err := scalar.MakeArrayFromScalar(s, 0, mem)
+               require.NoError(t, err)
+               assert.Equal(t, 0, arr.Len())
+               assert.Truef(t, arrow.TypeEqual(listType, arr.DataType()), 
"type %s", arr.DataType())
+               assert.NoError(t, array.ValidateFull(arr), "valid scalar: %t", 
s.IsValid())
+               arr.Release()
+       }
+}
+
+func TestMakeArrayFromDictionaryScalarZeroLength(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+       defer mem.AssertSize(t, 0)
+
+       int32Field := arrow.Field{Name: "i", Type: arrow.PrimitiveTypes.Int32, 
Nullable: true}
+       for _, valueType := range []arrow.DataType{
+               arrow.BinaryTypes.LargeBinary,
+               arrow.BinaryTypes.StringView,
+               arrow.FixedWidthTypes.Boolean,
+               arrow.ListOf(arrow.PrimitiveTypes.Int32),
+               arrow.StructOf(int32Field),
+               arrow.SparseUnionOf([]arrow.Field{int32Field}, 
[]arrow.UnionTypeCode{0}),
+               arrow.DenseUnionOf([]arrow.Field{int32Field}, 
[]arrow.UnionTypeCode{0}),
+               arrow.RunEndEncodedOf(arrow.PrimitiveTypes.Int32, 
arrow.PrimitiveTypes.Int32),
+               extensions.NewUUIDType(),
+       } {
+               dictType := &arrow.DictionaryType{IndexType: 
arrow.PrimitiveTypes.Int8, ValueType: valueType}
+               t.Run(valueType.String(), func(t *testing.T) {
+                       dict := array.MakeArrayOfNull(mem, valueType, 1)
+                       defer dict.Release()
+                       valid := scalar.NewDictScalar(scalar.NewInt8Scalar(0), 
dict)
+                       defer valid.Release()
+                       null := scalar.NewNullDictScalar(dictType)
+                       defer null.Release()
+
+                       for _, sc := range []*scalar.Dictionary{valid, null} {
+                               arr, err := scalar.MakeArrayFromScalar(sc, 0, 
mem)
+                               require.NoError(t, err)
+                               assert.Equal(t, 0, arr.Len())
+                               assert.Truef(t, arrow.TypeEqual(dictType, 
arr.DataType()), "type %s", arr.DataType())
+                               assert.NoError(t, array.ValidateFull(arr))
+                               arr.Release()
+                       }
+               })
+       }
 }
 
 func TestMakeArrayFromDictionaryScalar(t *testing.T) {

Reply via email to