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 10cebc8e fix(arrow): give scalar cast results consistent ownership 
(#1628)
10cebc8e is described below

commit 10cebc8e59787ae4c9c3328a994613d6a782cf86
Author: LxxxxxxxL <[email protected]>
AuthorDate: Fri Nov 20 06:50:10 2026 +0800

    fix(arrow): give scalar cast results consistent ownership (#1628)
    
    ### Rationale for this change
    
    A caller cannot release a successful scalar cast uniformly. For example,
    a binary-to-string cast owns a buffer reference, but a list identity
    cast borrows one. The difference propagates through run-end encoded
    casts. A scalar expression batch currently compensates by retaining a
    valid result, which leaks the reference of the binary-to-string cast.
    
    ### What changes are included in this PR?
    
    Define successful casts as returning an owned reference and retain the
    receiver in identity casts of reference-counted lists, list views,
    extensions, and run-end encoded scalars. The expression batch takes
    ownership of a successful cast and still retains an input field when no
    cast was needed.
    
    ### Are these changes tested?
    
    On main the expression regression leaves 64 bytes in a checked allocator
    after executing an int32 literal with a binary input field cast to utf8.
    The three run-end encoded cases also fail because releasing the input
    frees the list buffers before the result is released. The tests pass
    with this change, including the UUID extension identity case. I ran the
    repository tests with both data submodules and ran the scalar and
    expression packages with the race detector and the assert,test build
    tags.
    
    ### Are there any user-facing changes?
    
    Callers of CastTo own the returned scalar and should release it when it
    implements scalar.Releasable, including when the receiver and result are
    the same object. Callers that previously treated list or extension
    identity casts as borrowed need to release the result.
    
    Fixes #1563
    
    Co-authored-by: powerman2026 <[email protected]>
---
 arrow/compute/exprs/exec.go               |  9 +++----
 arrow/compute/exprs/exec_internal_test.go | 18 +++++++++++++
 arrow/scalar/nested.go                    |  4 +++
 arrow/scalar/scalar.go                    |  3 +++
 arrow/scalar/scalar_test.go               | 43 +++++++++++++++++++++++++++++++
 5 files changed, 71 insertions(+), 6 deletions(-)

diff --git a/arrow/compute/exprs/exec.go b/arrow/compute/exprs/exec.go
index 5fffa8c3..b0626ed7 100644
--- a/arrow/compute/exprs/exec.go
+++ b/arrow/compute/exprs/exec.go
@@ -120,13 +120,10 @@ func makeExecBatch(ctx context.Context, schema 
*arrow.Schema, partial compute.Da
                                        if err != nil {
                                                return out, err
                                        }
-                                       if !s.Value[idx].IsValid() {
-                                               // CastTo of a null returns a 
new scalar, which the datum owns
-                                               out.Values[i] = 
&compute.ScalarDatum{Value: val}
-                                               continue
-                                       }
+                                       out.Values[i] = 
&compute.ScalarDatum{Value: val}
+                               } else {
+                                       out.Values[i] = compute.NewDatum(val)
                                }
-                               out.Values[i] = compute.NewDatum(val)
                        }
                        return
                }
diff --git a/arrow/compute/exprs/exec_internal_test.go 
b/arrow/compute/exprs/exec_internal_test.go
index d7bf3403..4d24c3c5 100644
--- a/arrow/compute/exprs/exec_internal_test.go
+++ b/arrow/compute/exprs/exec_internal_test.go
@@ -361,3 +361,21 @@ func TestLiteralToDatumNullUserDefinedRelease(t 
*testing.T) {
        got.Release()
        assert.Nil(t, dict.Buffers())
 }
+
+func TestExecuteLiteralReleasesCastedScalar(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       defer mem.AssertSize(t, 0)
+       inputType := arrow.StructOf(arrow.Field{Name: "a", Type: 
arrow.BinaryTypes.Binary})
+       arr, _, err := array.FromJSON(mem, inputType, 
strings.NewReader(`[{"a":"eA=="}]`))
+       require.NoError(t, err)
+       defer arr.Release()
+       input, err := scalar.GetScalar(arr, 0)
+       require.NoError(t, err)
+       defer input.(scalar.Releasable).Release()
+
+       schema := arrow.NewSchema([]arrow.Field{{Name: "a", Type: 
arrow.BinaryTypes.String}}, nil)
+       result, err := ExecuteScalarExpression(context.Background(), schema,
+               expr.NewPrimitiveLiteral(int32(1), false), 
compute.NewDatumWithoutOwning(input))
+       require.NoError(t, err)
+       result.Release()
+}
diff --git a/arrow/scalar/nested.go b/arrow/scalar/nested.go
index 0a6c740b..a9f88c20 100644
--- a/arrow/scalar/nested.go
+++ b/arrow/scalar/nested.go
@@ -91,6 +91,7 @@ func (l *List) CastTo(to arrow.DataType) (Scalar, error) {
        }
 
        if arrow.TypeEqual(l.Type, to) {
+               l.Retain()
                return l, nil
        }
 
@@ -142,6 +143,7 @@ type ListView struct {
 
 func (l *ListView) CastTo(to arrow.DataType) (Scalar, error) {
        if l.Valid && arrow.TypeEqual(l.Type, to) {
+               l.Retain()
                return l, nil
        }
        return l.List.CastTo(to)
@@ -165,6 +167,7 @@ type LargeListView struct {
 
 func (l *LargeListView) CastTo(to arrow.DataType) (Scalar, error) {
        if l.Valid && arrow.TypeEqual(l.Type, to) {
+               l.Retain()
                return l, nil
        }
        return l.List.CastTo(to)
@@ -886,6 +889,7 @@ func (s *RunEndEncoded) CastTo(to arrow.DataType) (Scalar, 
error) {
        }
 
        if arrow.TypeEqual(s.Type, to) {
+               s.Retain()
                return s, nil
        }
 
diff --git a/arrow/scalar/scalar.go b/arrow/scalar/scalar.go
index 7eda7592..7798d8a1 100644
--- a/arrow/scalar/scalar.go
+++ b/arrow/scalar/scalar.go
@@ -59,6 +59,8 @@ type Scalar interface {
        ValidateFull() error
        // Cast the value to the desired DataType (returns an error if unable 
to do so)
        // should take semantics into account and modify the value accordingly.
+       // The caller owns the result and must release it if it is Releasable,
+       // including when the result is the receiver.
        CastTo(arrow.DataType) (Scalar, error)
 
        // internal only functions for delegation
@@ -471,6 +473,7 @@ func (s *Extension) CastTo(to arrow.DataType) (Scalar, 
error) {
        }
 
        if arrow.TypeEqual(s.Type, to) {
+               s.Retain()
                return s, nil
        }
 
diff --git a/arrow/scalar/scalar_test.go b/arrow/scalar/scalar_test.go
index 9e64b313..0b5654d4 100644
--- a/arrow/scalar/scalar_test.go
+++ b/arrow/scalar/scalar_test.go
@@ -1680,6 +1680,7 @@ func TestListViewScalarEqualsAndCastTo(t *testing.T) {
 
                        got, err := a.CastTo(a.DataType())
                        require.NoError(t, err)
+                       defer releaseScalar(got)
                        assert.Same(t, a, got)
 
                        _, err = scalar.MakeScalarParam(oneTwo, v.badType)
@@ -3759,3 +3760,45 @@ func 
TestRunEndEncodedScalarValidateRejectsMissingNonNullValue(t *testing.T) {
        assert.ErrorContains(t, sc.Validate(), "has no value")
        assert.ErrorContains(t, sc.ValidateFull(), "has no value")
 }
+
+func TestRunEndEncodedCastOwnsValue(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       defer mem.AssertSize(t, 0)
+       for _, target := range []string{"list", "same encoding", "different run 
ends"} {
+               t.Run(target, func(t *testing.T) {
+                       arr, _, err := array.FromJSON(mem, 
arrow.PrimitiveTypes.Int32, strings.NewReader("[1, 2]"))
+                       require.NoError(t, err)
+                       list := scalar.NewListScalar(arr)
+                       arr.Release()
+                       from := scalar.NewRunEndEncodedScalar(list, 
arrow.RunEndEncodedOf(arrow.PrimitiveTypes.Int16, list.DataType()))
+                       to := list.DataType()
+                       switch target {
+                       case "same encoding":
+                               to = from.DataType()
+                       case "different run ends":
+                               to = 
arrow.RunEndEncodedOf(arrow.PrimitiveTypes.Int32, to)
+                       }
+                       got, err := from.CastTo(to)
+                       require.NoError(t, err)
+                       from.Release()
+                       require.Positive(t, mem.CurrentAlloc(), "cast result 
should still own the list buffers")
+                       releaseScalar(got)
+               })
+       }
+}
+
+func TestExtensionIdentityCastOwnsStorage(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       defer mem.AssertSize(t, 0)
+       typ := extensions.NewUUIDType()
+       buf := memory.NewResizableBuffer(mem)
+       buf.Resize(16)
+       storage := scalar.NewFixedSizeBinaryScalar(buf, typ.StorageType())
+       buf.Release()
+       input := scalar.NewExtensionScalar(storage, typ)
+       result, err := input.CastTo(typ)
+       require.NoError(t, err)
+       input.Release()
+       require.Positive(t, mem.CurrentAlloc(), "cast result should still own 
the UUID storage")
+       releaseScalar(result)
+}

Reply via email to