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 75fc0a35 fix(arrow/compute/exprs): avoid leaking field ref on type 
mismatch (#1466)
75fc0a35 is described below

commit 75fc0a35caa84c5ab3b2de4e826b22c47e17f585
Author: Harrison Crosse <[email protected]>
AuthorDate: Thu Nov 5 12:17:24 2026 -0500

    fix(arrow/compute/exprs): avoid leaking field ref on type mismatch (#1466)
    
    ### Rationale for this change
    
    `ExecuteScalarExpression` leaks the input column when a top-level field
    reference has the wrong type. It returns `arrow.ErrInvalid`, but
    releasing the input does not free the column's buffers. This affects any
    column type.
    
    ### What changes are included in this PR?
    
    `execFieldRef` wraps the referenced input with `compute.NewDatum`, which
    retains it, before checking its type. A mismatch returns without
    releasing that reference. The fix checks the borrowed input's type first
    and retains it only on success.
    
    Nested references are unchanged. For a nested struct field,
    `GetReferencedValue` returns the child without retaining it, so
    releasing the result on a mismatch would release a reference that
    `execFieldRef` does not own. Successful nested references are released
    more times than they are retained. That is a separate bug,
    https://github.com/apache/arrow-go/issues/1451, and this change leaves
    it alone.
    
    The review of https://github.com/apache/arrow-go/pull/1449 suggested the
    checked allocator for the rejected cases in `TestDictionaryTypes`. They
    now use it for both the arrays and the execution context.
    
    ### Are these changes tested?
    
    Yes. I added `TestFieldRefTypeMismatchReleases` with top-level and
    nested type mismatches. On main, the top-level case leaves 128 bytes
    allocated after the input is released. The nested case passes on main.
    It catches a fix that releases the result on every mismatch, which
    over-releases the borrowed child and panics with `too many releases`
    under `-tags assert,test`. Both cases pass with this fix.
    
    With the checked allocator, the two type-mismatch cases in
    `TestDictionaryTypes` leave 576 bytes allocated on main. With this fix,
    the test passes without leaks.
    
    On macOS arm64 with Go `1.26.10`, `go test ./arrow/compute/exprs/...`
    passed with and without `-tags assert,test`, along with `go test -race
    ./arrow/compute/exprs/`, `go vet`, `gofmt`, `golangci-lint` in `arrow`
    and the Release Audit Tool.
    
    ### Are there any user-facing changes?
    
    Yes. A top-level field reference with a mismatched type still returns
    `arrow.ErrInvalid`, but no longer keeps the input column alive after it
    is released. There is no API change.
    
    Closes https://github.com/apache/arrow-go/issues/1450.
---
 arrow/compute/exprs/exec.go      |  9 ++++++--
 arrow/compute/exprs/exec_test.go | 46 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/arrow/compute/exprs/exec.go b/arrow/compute/exprs/exec.go
index 97483f1a..722147f6 100644
--- a/arrow/compute/exprs/exec.go
+++ b/arrow/compute/exprs/exec.go
@@ -518,8 +518,9 @@ func execFieldRef(ctx context.Context, e 
*expr.FieldReference, input compute.Exe
        }
 
        out, err := GetReferencedValue(compute.GetAllocator(ctx), ref, param, 
ext)
-       if err == compute.ErrEmpty {
-               out = compute.NewDatum(param)
+       refersToParam := err == compute.ErrEmpty
+       if refersToParam {
+               out = param
        } else if err != nil {
                return nil, err
        }
@@ -542,6 +543,10 @@ func execFieldRef(ctx context.Context, e 
*expr.FieldReference, input compute.Exe
                }
        }
 
+       if refersToParam {
+               // retain only after the type check so a mismatch has nothing 
to release
+               return compute.NewDatum(param), nil
+       }
        return out, nil
 }
 
diff --git a/arrow/compute/exprs/exec_test.go b/arrow/compute/exprs/exec_test.go
index c2814310..370add2f 100644
--- a/arrow/compute/exprs/exec_test.go
+++ b/arrow/compute/exprs/exec_test.go
@@ -827,7 +827,7 @@ func TestDictionaryTypes(t *testing.T) {
 
        for _, tt := range rejected {
                t.Run(tt.name, func(t *testing.T) {
-                       col, err := 
array.DictArrayFromJSON(memory.DefaultAllocator, tt.dictType, `[0]`, tt.dict)
+                       col, err := array.DictArrayFromJSON(mem, tt.dictType, 
`[0]`, tt.dict)
                        require.NoError(t, err)
                        defer col.Release()
 
@@ -849,7 +849,7 @@ func TestDictionaryTypes(t *testing.T) {
                                require.NoError(t, err)
                        }
 
-                       ctx := exprs.WithExtensionIDSet(context.Background(), 
extSet)
+                       ctx := 
exprs.WithExtensionIDSet(compute.WithAllocator(context.Background(), mem), 
extSet)
                        _, err = exprs.ExecuteScalarExpression(ctx, dictSchema, 
ex, compute.NewDatumWithoutOwning(rec))
                        assert.ErrorIs(t, err, tt.err)
                        assert.ErrorContains(t, err, "dictionary<")
@@ -857,6 +857,48 @@ func TestDictionaryTypes(t *testing.T) {
        }
 }
 
+func TestFieldRefTypeMismatchReleases(t *testing.T) {
+       structOf := func(inner arrow.DataType) arrow.DataType {
+               return arrow.StructOf(arrow.Field{Name: "a", Type: inner, 
Nullable: true})
+       }
+
+       tests := []struct {
+               name               string
+               exprType, dataType arrow.DataType
+               json               string
+               path               []any
+       }{
+               {"top level", arrow.BinaryTypes.String, 
arrow.PrimitiveTypes.Int32,
+                       `[{"col": 1}, {"col": 2}]`, []any{"col"}},
+               {"nested", structOf(arrow.BinaryTypes.String), 
structOf(arrow.PrimitiveTypes.Int32),
+                       `[{"col": {"a": 1}}, {"col": {"a": 2}}]`, []any{"col", 
"a"}},
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       mem := 
memory.NewCheckedAllocator(memory.DefaultAllocator)
+                       defer mem.AssertSize(t, 0)
+
+                       exprSchema := arrow.NewSchema([]arrow.Field{{Name: 
"col", Type: tt.exprType, Nullable: true}}, nil)
+                       dataSchema := arrow.NewSchema([]arrow.Field{{Name: 
"col", Type: tt.dataType, Nullable: true}}, nil)
+
+                       rec, _, err := array.RecordFromJSON(mem, dataSchema, 
strings.NewReader(tt.json))
+                       require.NoError(t, err)
+                       defer rec.Release()
+
+                       bldr := exprs.NewExprBuilder(extSet)
+                       require.NoError(t, bldr.SetInputSchema(exprSchema))
+                       e, err := bldr.FieldRefList(tt.path...).BuildExpr()
+                       require.NoError(t, err)
+
+                       ctx := compute.WithAllocator(context.Background(), mem)
+                       result, err := exprs.ExecuteScalarExpression(ctx, 
dataSchema, e, compute.NewDatumWithoutOwning(rec))
+                       assert.ErrorIs(t, err, arrow.ErrInvalid)
+                       assert.Nil(t, result)
+               })
+       }
+}
+
 func TestDecimalFilterLarge(t *testing.T) {
        t.Parallel()
 

Reply via email to