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 d281c95c fix(arrow/scalar): use AppendNull when appending null scalars 
(#1528)
d281c95c is described below

commit d281c95c6af0ee358d1deeec5dc6f9e0f1bb6203
Author: Harrison Crosse <[email protected]>
AuthorDate: Wed Nov 11 17:52:41 2026 -0500

    fix(arrow/scalar): use AppendNull when appending null scalars (#1528)
    
    ### Rationale for this change
    
    Appending null scalars can leave value bytes, offsets or child slots
    missing, causing builder panics or corrupted arrays.
    
    ### What changes are included in this PR?
    
    `scalar.Append` and `scalar.AppendSlice` use each builder's `AppendNull`
    for fixed size binary and fixed size list nulls, null struct fields and
    union children. This advances value bytes and child slots and supplies
    offsets where required. Nested run end encoded children no longer hit
    `UnsafeAppendBoolToBitmap`.
    
    Validity handling stays unchanged where the bitmap update already
    matches `AppendNull`.
    
    ### Are these changes tested?
    
    `TestAppendNullScalars` has five rows covering fixed size binary, fixed
    size list, struct, sparse union and dense union arrays, checking
    validation and equality. All five fail on main and pass with this
    change. The cases in the issue build valid arrays and preserve `"efgh"`
    after the null.
    
    With Go 1.26.10, vet, `go test -race -tags assert,test ./arrow/...` with
    pinned test data, lint and the Release Audit Tool passed.
    
    ### Are there any user-facing changes?
    
    Yes. Null appends preserve builder layout instead of producing invalid
    arrays or panicking. There is no API change.
    
    Closes https://github.com/apache/arrow-go/issues/1525.
---
 arrow/scalar/append.go      | 42 ++++++++++++++++++++---------------------
 arrow/scalar/append_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 22 deletions(-)

diff --git a/arrow/scalar/append.go b/arrow/scalar/append.go
index d35e236a..8e5db229 100644
--- a/arrow/scalar/append.go
+++ b/arrow/scalar/append.go
@@ -131,7 +131,7 @@ func appendToBldr(bldr array.Builder, scalars []Scalar) 
error {
                        if s.Valid {
                                bldr.UnsafeAppend(s.Value.Bytes())
                        } else {
-                               bldr.UnsafeAppendBoolToBitmap(false)
+                               bldr.AppendNull()
                        }
                }
        case *array.Int8Builder:
@@ -191,17 +191,19 @@ func appendToBldr(bldr array.Builder, scalars []Scalar) 
error {
                bldr.ValueBuilder().Reserve(numChildren)
 
                for _, s := range scalars {
-                       bldr.Append(s.IsValid())
-                       if s.IsValid() {
-                               list := s.(ListScalar).GetList()
-                               for i := 0; i < list.Len(); i++ {
-                                       sc, err := GetScalar(list, i)
-                                       if err != nil {
-                                               return err
-                                       }
-                                       if err := Append(bldr.ValueBuilder(), 
sc); err != nil {
-                                               return err
-                                       }
+                       if !s.IsValid() {
+                               bldr.AppendNull()
+                               continue
+                       }
+                       bldr.Append(true)
+                       list := s.(ListScalar).GetList()
+                       for i := 0; i < list.Len(); i++ {
+                               sc, err := GetScalar(list, i)
+                               if err != nil {
+                                       return err
+                               }
+                               if err := Append(bldr.ValueBuilder(), sc); err 
!= nil {
+                                       return err
                                }
                        }
                }
@@ -210,7 +212,7 @@ func appendToBldr(bldr array.Builder, scalars []Scalar) 
error {
                        s := sc.(*Struct)
                        for i := 0; i < bldr.NumField(); i++ {
                                if !s.Valid || s.Value[i] == nil {
-                                       
bldr.FieldBuilder(i).UnsafeAppendBoolToBitmap(false)
+                                       bldr.FieldBuilder(i).AppendNull()
                                } else {
                                        if err := Append(bldr.FieldBuilder(i), 
s.Value[i]); err != nil {
                                                return err
@@ -234,16 +236,12 @@ func appendToBldr(bldr array.Builder, scalars []Scalar) 
error {
                        bldr.Append(s.TypeCode)
                        for i := range ty.Fields() {
                                child := bldr.Child(i)
-                               if s.ChildID == i {
-                                       if s.Valid {
-                                               if err := Append(child, 
s.Value[i]); err != nil {
-                                                       return err
-                                               }
-                                       } else {
-                                               
child.UnsafeAppendBoolToBitmap(false)
+                               if s.ChildID == i && s.Valid {
+                                       if err := Append(child, s.Value[i]); 
err != nil {
+                                               return err
                                        }
                                } else {
-                                       child.UnsafeAppendBoolToBitmap(false)
+                                       child.AppendNull()
                                }
                        }
                }
@@ -266,7 +264,7 @@ func appendToBldr(bldr array.Builder, scalars []Scalar) 
error {
                                                        return err
                                                }
                                        } else {
-                                               
child.UnsafeAppendBoolToBitmap(false)
+                                               child.AppendNull()
                                        }
                                }
                        }
diff --git a/arrow/scalar/append_test.go b/arrow/scalar/append_test.go
index b61013d7..a2ea9a1f 100644
--- a/arrow/scalar/append_test.go
+++ b/arrow/scalar/append_test.go
@@ -240,3 +240,49 @@ func TestAppendMapScalar(t *testing.T) {
 
        assert.Truef(t, array.Equal(expected, result), "expected: %s, got: %s", 
expected, result)
 }
+
+func TestAppendNullScalars(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       defer mem.AssertSize(t, 0)
+
+       unionFields := []arrow.Field{
+               {Name: "i", Type: arrow.PrimitiveTypes.Int32, Nullable: true},
+               {Name: "s", Type: arrow.BinaryTypes.String, Nullable: true},
+       }
+       tests := []struct {
+               name string
+               dt   arrow.DataType
+               json string
+       }{
+               {"null fixed_size_binary", 
&arrow.FixedSizeBinaryType{ByteWidth: 4}, `["YWJjZA==", null, "ZWZnaA=="]`},
+               {"null fixed_size_list", arrow.FixedSizeListOf(2, 
arrow.PrimitiveTypes.Int32), `[[1, 2], null]`},
+               {"null struct", arrow.StructOf(arrow.Field{Name: "s", Type: 
arrow.BinaryTypes.String, Nullable: true}), `[{"s": "a"}, null, {"s": "b"}]`},
+               {"sparse union", arrow.SparseUnionOf(unionFields, 
[]arrow.UnionTypeCode{0, 1}), `[[0, 5], [1, null]]`},
+               {"dense union", arrow.DenseUnionOf(unionFields, 
[]arrow.UnionTypeCode{0, 1}), `[[0, 5], [1, null]]`},
+       }
+
+       for _, tc := range tests {
+               t.Run(tc.name, func(t *testing.T) {
+                       expected, _, err := array.FromJSON(mem, tc.dt, 
strings.NewReader(tc.json))
+                       require.NoError(t, err)
+                       defer expected.Release()
+
+                       scalars := make([]scalar.Scalar, expected.Len())
+                       for i := range scalars {
+                               sc, err := scalar.GetScalar(expected, i)
+                               require.NoError(t, err)
+                               defer releaseScalar(sc)
+                               scalars[i] = sc
+                       }
+
+                       bldr := array.NewBuilder(mem, tc.dt)
+                       defer bldr.Release()
+                       require.NoError(t, scalar.AppendSlice(bldr, scalars))
+                       result := bldr.NewArray()
+                       defer result.Release()
+
+                       require.NoError(t, array.ValidateFull(result))
+                       assert.Truef(t, array.Equal(expected, result), 
"expected: %s, got: %s", expected, result)
+               })
+       }
+}

Reply via email to