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 50801830 fix(arrow/scalar): release child scalars when appending list 
scalars (#1604)
50801830 is described below

commit 508018302f400830182ac746f76e85eb8002bf44
Author: Harrison Crosse <[email protected]>
AuthorDate: Wed Nov 18 12:27:07 2026 -0500

    fix(arrow/scalar): release child scalars when appending list scalars (#1604)
    
    ### Rationale for this change
    
    `Append` obtains each child of a list scalar through `GetScalar` without
    releasing it. Children that hold buffers, such as binary values and
    nested lists, keep their references even after the caller releases
    everything it owns.
    
    ### What changes are included in this PR?
    
    `Append` releases each child that implements `Releasable` after
    appending it, including when the append fails.
    
    ### Are these changes tested?
    
    `TestAppendListScalarReleasesChildren` covers `list<binary>` and
    `large_list<binary>`. `TestAppendListScalarReleasesChildrenOnError`
    covers failed appends. `TestAppendNestedSlicedListScalar` now uses a
    checked allocator. All three fail on `main` with 64 or 128 bytes still
    allocated and pass with this change.
    
    I ran `go test -race ./arrow/scalar/ ./arrow/compute/...` and
    `golangci-lint` 2.13.2 on `./arrow/scalar/...` with Go 1.26.10 on macOS
    arm64. Both passed.
    
    ### Are there any user-facing changes?
    
    Yes. `Append` and `AppendSlice` no longer keep the buffers of list
    scalar children allocated after the caller releases everything. There is
    no API change.
    
    Closes #1603
---
 arrow/scalar/append.go      |  6 ++++-
 arrow/scalar/append_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++++
 arrow/scalar/scalar_test.go |  5 ++--
 3 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/arrow/scalar/append.go b/arrow/scalar/append.go
index 9e412249..61025dc4 100644
--- a/arrow/scalar/append.go
+++ b/arrow/scalar/append.go
@@ -211,7 +211,11 @@ func appendToBldr(bldr array.Builder, scalars []Scalar) 
error {
                                if err != nil {
                                        return err
                                }
-                               if err := Append(bldr.ValueBuilder(), sc); err 
!= nil {
+                               err = Append(bldr.ValueBuilder(), sc)
+                               if r, ok := sc.(Releasable); ok {
+                                       r.Release()
+                               }
+                               if err != nil {
                                        return err
                                }
                        }
diff --git a/arrow/scalar/append_test.go b/arrow/scalar/append_test.go
index a2ea9a1f..6ea3a062 100644
--- a/arrow/scalar/append_test.go
+++ b/arrow/scalar/append_test.go
@@ -286,3 +286,62 @@ func TestAppendNullScalars(t *testing.T) {
                })
        }
 }
+
+func TestAppendListScalarReleasesChildren(t *testing.T) {
+       tests := []struct {
+               name string
+               dt   arrow.DataType
+       }{
+               {"list_binary", arrow.ListOf(arrow.BinaryTypes.Binary)},
+               {"large_list_binary", 
arrow.LargeListOf(arrow.BinaryTypes.Binary)},
+       }
+
+       for _, tc := range tests {
+               t.Run(tc.name, func(t *testing.T) {
+                       mem := 
memory.NewCheckedAllocator(memory.DefaultAllocator)
+                       defer mem.AssertSize(t, 0)
+
+                       expected, _, err := array.FromJSON(mem, tc.dt, 
strings.NewReader(`[["YWFi", "YmNkZQ=="], null, []]`))
+                       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()
+
+                       assert.Truef(t, array.Equal(expected, result), 
"expected: %s, got: %s", expected, result)
+               })
+       }
+}
+
+func TestAppendListScalarReleasesChildrenOnError(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       defer mem.AssertSize(t, 0)
+
+       // Append has no case for a dictionary element, so the child scalar 
fails to
+       // append after GetScalar has allocated it.
+       dt := arrow.ListOf(&arrow.DictionaryType{IndexType: 
arrow.PrimitiveTypes.Int32, ValueType: arrow.BinaryTypes.String})
+       expected, _, err := array.FromJSON(mem, dt, strings.NewReader(`[["ab", 
"cd"]]`))
+       require.NoError(t, err)
+       defer expected.Release()
+
+       sc, err := scalar.GetScalar(expected, 0)
+       require.NoError(t, err)
+       defer releaseScalar(sc)
+
+       bldr := array.NewBuilder(mem, dt)
+       defer bldr.Release()
+
+       err = scalar.Append(bldr, sc)
+       require.ErrorIs(t, err, arrow.ErrNotImplemented)
+}
diff --git a/arrow/scalar/scalar_test.go b/arrow/scalar/scalar_test.go
index fbbcbb24..d402360c 100644
--- a/arrow/scalar/scalar_test.go
+++ b/arrow/scalar/scalar_test.go
@@ -3162,9 +3162,8 @@ func TestListScalarHashNestedSliced(t *testing.T) {
 }
 
 func TestAppendNestedSlicedListScalar(t *testing.T) {
-       // scalar.Append does not release the child scalars it unboxes, so a
-       // checked allocator would report them.
-       mem := memory.NewGoAllocator()
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       defer mem.AssertSize(t, 0)
 
        dt := arrow.ListOf(arrow.ListOf(arrow.PrimitiveTypes.Int32))
        full, _, err := array.FromJSON(mem, dt, strings.NewReader(`[[[1]], [[2, 
3], []]]`))

Reply via email to