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 06b57e0c fix(arrow/scalar): validate missing binary values and null 
REE scalars (#1541)
06b57e0c is described below

commit 06b57e0cbdb6ef8846ca72ec42464485de8e8eab
Author: Harrison Crosse <[email protected]>
AuthorDate: Thu Nov 12 12:06:46 2026 -0500

    fix(arrow/scalar): validate missing binary values and null REE scalars 
(#1541)
    
    ### Rationale for this change
    
    `Validate` accepts missing binary values and panics on valid null REE
    scalars with primitive values.
    
    ### What changes are included in this PR?
    
    `validateOptional` takes a plain `bool` instead of inspecting an
    interface with reflection. REE validation checks that its validity
    matches its value's and runs type checks for null scalars too, matching
    Arrow C++ for these two checks.
    
    ### Are these changes tested?
    
    Corrected binary and string assertions check the intended scalar, and
    view tests require errors. `TestRunEndEncodedGetScalar` adds a null
    primitive run. `TestRunEndEncodedScalarValidateRejectsMismatches` covers
    validity mismatches and wrong or missing types. The new rows fail on
    main and pass with this change.
    
    With Go 1.26.10, vet, race tests on `arrow/scalar` and `arrow/compute`,
    full `go test -race -tags assert,test ./arrow/...` with pinned
    `arrow-testing` and `parquet-testing` checked out, `golangci-lint`
    2.13.2 and the Release Audit Tool passed.
    
    ### Are there any user-facing changes?
    
    Yes. `Validate` returns errors for invalid scalars instead of passing or
    panicking, and accepts valid null REE scalars. There is no API change.
    
    Closes https://github.com/apache/arrow-go/issues/1536.
---
 arrow/scalar/binary.go      |  4 ++--
 arrow/scalar/nested.go      | 18 +++++++++++-------
 arrow/scalar/scalar.go      | 10 +++++-----
 arrow/scalar/scalar_test.go | 41 ++++++++++++++++++++++++++++++++++++-----
 4 files changed, 54 insertions(+), 19 deletions(-)

diff --git a/arrow/scalar/binary.go b/arrow/scalar/binary.go
index 76d09f6a..36484689 100644
--- a/arrow/scalar/binary.go
+++ b/arrow/scalar/binary.go
@@ -97,9 +97,9 @@ func (b *Binary) CastTo(to arrow.DataType) (Scalar, error) {
 func (b *Binary) Validate() (err error) {
        err = b.scalar.Validate()
        if err == nil {
-               err = validateOptional(&b.scalar, b.Value, "value")
+               err = validateOptional(&b.scalar, b.Value != nil)
        }
-       if err == nil && b.Valid && b.Value != nil && b.Value.Len() > 
math.MaxInt32 {
+       if err == nil && b.Valid && b.Value.Len() > math.MaxInt32 {
                switch b.Type.ID() {
                case arrow.BINARY_VIEW, arrow.STRING_VIEW:
                        // A view header stores the size as an int32.
diff --git a/arrow/scalar/nested.go b/arrow/scalar/nested.go
index 8081613c..858b4fa1 100644
--- a/arrow/scalar/nested.go
+++ b/arrow/scalar/nested.go
@@ -60,7 +60,7 @@ func (l *List) Validate() (err error) {
        if err = l.scalar.Validate(); err != nil {
                return
        }
-       if err = validateOptional(&l.scalar, l.Value, "value"); err != nil {
+       if err = validateOptional(&l.scalar, l.Value != nil); err != nil {
                return
        }
 
@@ -777,24 +777,28 @@ func (s *RunEndEncoded) Validate() (err error) {
                return fmt.Errorf("%w: non-null run-end-encoded scalar has no 
value", arrow.ErrInvalid)
        }
 
-       if err = s.Value.Validate(); err != nil {
+       if err = s.scalar.Validate(); err != nil {
                return
        }
 
-       if err = validateOptional(&s.scalar, s.value(), "value"); err != nil {
+       if err = s.Value.Validate(); err != nil {
                return
        }
 
-       if !s.Valid {
-               return
+       switch {
+       case !s.Valid && s.Value.IsValid():
+               return fmt.Errorf("%w: null %s scalar has non-null storage 
value", arrow.ErrInvalid, s.Type)
+       case s.Valid && !s.Value.IsValid():
+               return fmt.Errorf("%w: non-null %s scalar has null storage 
value", arrow.ErrInvalid, s.Type)
        }
 
-       if s.Type.ID() != arrow.RUN_END_ENCODED {
+       dt, ok := s.Type.(*arrow.RunEndEncodedType)
+       if !ok || dt == nil {
                return fmt.Errorf("%w: run-end-encoded scalar should not have 
type %s",
                        arrow.ErrInvalid, s.Type)
        }
 
-       if !arrow.TypeEqual(s.Value.DataType(), 
s.Type.(*arrow.RunEndEncodedType).Encoded()) {
+       if !arrow.TypeEqual(s.Value.DataType(), dt.Encoded()) {
                return fmt.Errorf("%w: run-end-encoded scalar value type %s 
does not match type %s",
                        arrow.ErrInvalid, s.Value.DataType(), s.Type)
        }
diff --git a/arrow/scalar/scalar.go b/arrow/scalar/scalar.go
index 94386022..a827d75a 100644
--- a/arrow/scalar/scalar.go
+++ b/arrow/scalar/scalar.go
@@ -71,12 +71,12 @@ type Releasable interface {
        Retain()
 }
 
-func validateOptional(s *scalar, value interface{}, valueDesc string) error {
-       if s.Valid && value == nil {
-               return fmt.Errorf("%s scalar is marked valid but doesn't have a 
%s", s.Type, valueDesc)
+func validateOptional(s *scalar, hasValue bool) error {
+       if s.Valid && !hasValue {
+               return fmt.Errorf("%s scalar is marked valid but doesn't have a 
value", s.Type)
        }
-       if !s.Valid && value != nil && !reflect.ValueOf(value).IsNil() {
-               return fmt.Errorf("%s scalar is marked null but has a %s", 
s.Type, valueDesc)
+       if !s.Valid && hasValue {
+               return fmt.Errorf("%s scalar is marked null but has a value", 
s.Type)
        }
        return nil
 }
diff --git a/arrow/scalar/scalar_test.go b/arrow/scalar/scalar_test.go
index 70a1a859..fe6dc2f9 100644
--- a/arrow/scalar/scalar_test.go
+++ b/arrow/scalar/scalar_test.go
@@ -347,8 +347,8 @@ func TestBinaryScalarValidateErrors(t *testing.T) {
 
        nullScalar := scalar.MakeNullScalar(arrow.BinaryTypes.Binary)
        nullScalar.(*scalar.Binary).Valid = true
-       assert.Error(t, sc.Validate())
-       assert.Error(t, sc.ValidateFull())
+       assert.Error(t, nullScalar.Validate())
+       assert.Error(t, nullScalar.ValidateFull())
 }
 
 func TestBinaryScalarCastToBinaryUsesTargetType(t *testing.T) {
@@ -387,8 +387,8 @@ func TestStringScalarValidateErrors(t *testing.T) {
 
        nullScalar := scalar.MakeNullScalar(arrow.BinaryTypes.String)
        nullScalar.(*scalar.String).Valid = true
-       assert.Error(t, sc.Validate())
-       assert.Error(t, sc.ValidateFull())
+       assert.Error(t, nullScalar.Validate())
+       assert.Error(t, nullScalar.ValidateFull())
 
        // invalid utf8
        sc = 
scalar.NewStringScalarFromBuffer(memory.NewBufferBytes([]byte{0xff}))
@@ -2254,7 +2254,7 @@ func TestViewScalarValidateWithoutValue(t *testing.T) {
                        v.Valid = true
                }
                for _, sc := range []scalar.Scalar{plain, view} {
-                       assert.NotPanics(t, func() { _ = sc.Validate() }, "%T 
%s", sc, dt)
+                       assert.Error(t, sc.Validate(), "%T %s", sc, dt)
                }
        }
 }
@@ -3330,6 +3330,15 @@ func TestRunEndEncodedGetScalar(t *testing.T) {
        slice := array.NewSlice(reeArray, 199, 404).(*array.RunEndEncoded)
        defer slice.Release()
 
+       nullRunEnds, _, _ := array.FromJSON(mem, arrow.PrimitiveTypes.Int32, 
strings.NewReader(`[1, 2]`))
+       defer nullRunEnds.Release()
+
+       nullValues, _, _ := array.FromJSON(mem, arrow.PrimitiveTypes.Int64, 
strings.NewReader(`[null, 7]`))
+       defer nullValues.Release()
+
+       nullRun := array.NewRunEndEncodedArray(nullRunEnds, nullValues, 2, 0)
+       defer nullRun.Release()
+
        tests := []struct {
                name  string
                arr   arrow.Array
@@ -3338,6 +3347,7 @@ func TestRunEndEncodedGetScalar(t *testing.T) {
        }{
                {"simple", reeArray, 225, "world"},
                {"offset", slice, 125, "of"},
+               {"null primitive", nullRun, 0, "null"},
        }
 
        for _, tt := range tests {
@@ -3367,6 +3377,27 @@ func TestRunEndEncodedNullScalar(t *testing.T) {
        assert.NoError(t, sc.ValidateFull())
 }
 
+func TestRunEndEncodedScalarValidateRejectsMismatches(t *testing.T) {
+       dt := arrow.RunEndEncodedOf(arrow.PrimitiveTypes.Int32, 
arrow.PrimitiveTypes.Int64)
+
+       nullWithValue := 
scalar.NewRunEndEncodedScalar(scalar.NewInt64Scalar(7), dt)
+       nullWithValue.Valid = false
+       assert.ErrorContains(t, nullWithValue.Validate(), "has non-null storage 
value")
+
+       validWithNull := 
scalar.NewRunEndEncodedScalar(scalar.MakeNullScalar(arrow.PrimitiveTypes.Int64),
 dt)
+       validWithNull.Valid = true
+       assert.ErrorContains(t, validWithNull.Validate(), "has null storage 
value")
+
+       nullWrongType := 
scalar.NewRunEndEncodedScalar(scalar.MakeNullScalar(arrow.PrimitiveTypes.Int32),
 dt)
+       assert.ErrorContains(t, nullWrongType.Validate(), "does not match type")
+
+       noType := &scalar.RunEndEncoded{Value: 
scalar.MakeNullScalar(arrow.BinaryTypes.String)}
+       assert.ErrorContains(t, noType.Validate(), "lacks a type")
+
+       nilType := 
scalar.NewRunEndEncodedScalar(scalar.MakeNullScalar(arrow.BinaryTypes.String), 
nil)
+       assert.ErrorContains(t, nilType.Validate(), "should not have type")
+}
+
 func TestRunEndEncodedScalarValidateRejectsMissingNonNullValue(t *testing.T) {
        sc := 
scalar.MakeNullScalar(arrow.RunEndEncodedOf(arrow.PrimitiveTypes.Int16, 
arrow.BinaryTypes.String)).(*scalar.RunEndEncoded)
        sc.Valid = true

Reply via email to