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 1ce80a57 fix(arrow/compute/exprs): retain nested field ref results
(#1470)
1ce80a57 is described below
commit 1ce80a57bee38998af8a8ccecc514425c953ea72
Author: Harrison Crosse <[email protected]>
AuthorDate: Thu Nov 5 16:50:33 2026 -0500
fix(arrow/compute/exprs): retain nested field ref results (#1470)
### Rationale for this change
A nested struct field reference returns a value borrowed from the input
without retaining it. Releasing the result can free buffers the input
still uses. With a struct scalar input, the result can be freed before
`ExecuteScalarExpression` returns.
On main, the issue's `equal(s.a, 1)` filter panics with `too many
releases` under `-tags assert,test` when the record is released. Without
assertions, reading a binary field from a struct scalar input silently
returns an empty value.
### What changes are included in this PR?
`execFieldRef` takes a reference after the type check when the remaining
path contains only struct fields. This extends the retain that
https://github.com/apache/arrow-go/pull/1466 added for top-level
columns, while keeping its type-mismatch behavior. On scalar inputs,
list-element and map-value references return new scalars that the caller
already owns, so they are not retained again. `GetReferencedValue` is
unchanged.
This change leaves three related problems alone:
- A struct-typed result from a scalar input is still released more times
than it is retained and can be freed before return, because
`scalar.Struct` has `Release` but no `Retain`.
- Array-input list elements are returned already released by
`GetReferencedValue`.
- A type mismatch on a list or map path leaves the newly created scalar
unreleased.
### Are these changes tested?
Yes. I added `TestNestedFieldRefResultOwnership` with three subtests,
all using a checked allocator:
- `array struct field` reads `s.a`, releases the record and checks that
the result still holds its buffers and values. It fails on main because
the buffers have been freed.
- `scalar struct field` reads the binary field `s.b` from a struct
scalar input and checks that the result still holds `hello` after the
input is released. It fails on main. A separate probe showed that on
main the buffer is already freed when `ExecuteScalarExpression` returns.
- `scalar list element` reads `s.l[1]` and checks that it contains
`world` without leaking. It passes on main, and it fails if the new
list-element scalar is retained again.
All three pass with this fix, with and without `-tags assert,test`. I
also ran the issue's filter with the fix, and it passes without leaks.
On macOS arm64 with Go `1.26.10`, `go test ./arrow/compute/exprs/...`
passed, along with `go test -race -tags assert,test
./arrow/compute/exprs/`, `go vet`, `gofmt`, `golangci-lint` `2.13.2` in
`arrow` and the Release Audit Tool.
### Are there any user-facing changes?
Yes. Nested struct field references that return arrays, or scalars with
buffers such as string, binary and list scalars, keep their buffers
until the result is released. Filters such as `equal(s.a, 1)` no longer
release a child the input still uses. There is no API change.
Closes https://github.com/apache/arrow-go/issues/1451.
---
arrow/compute/exprs/exec.go | 19 ++++++---
arrow/compute/exprs/exec_test.go | 87 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 101 insertions(+), 5 deletions(-)
diff --git a/arrow/compute/exprs/exec.go b/arrow/compute/exprs/exec.go
index 4af4a627..2e320e76 100644
--- a/arrow/compute/exprs/exec.go
+++ b/arrow/compute/exprs/exec.go
@@ -523,8 +523,7 @@ func execFieldRef(ctx context.Context, e
*expr.FieldReference, input compute.Exe
}
out, err := GetReferencedValue(compute.GetAllocator(ctx), ref, param,
ext)
- refersToParam := err == compute.ErrEmpty
- if refersToParam {
+ if err == compute.ErrEmpty {
out = param
} else if err != nil {
return nil, err
@@ -548,13 +547,23 @@ 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
+ // A struct field path borrows its value from the input, so take a
+ // reference after the type check, where a mismatch has nothing to
release.
+ if isStructFieldPath(ref) {
+ return compute.NewDatum(out), nil
}
return out, nil
}
+func isStructFieldPath(ref expr.ReferenceSegment) bool {
+ for ; ref != nil; ref = ref.GetChild() {
+ if _, ok := ref.(*expr.StructFieldRef); !ok {
+ return false
+ }
+ }
+ return true
+}
+
func executeScalarBatch(ctx context.Context, input compute.ExecBatch, exp
expr.Expression, ext ExtensionIDSet) (compute.Datum, error) {
if !exp.IsScalar() {
return nil, fmt.Errorf("%w: ExecuteScalarExpression cannot
execute non-scalar expressions",
diff --git a/arrow/compute/exprs/exec_test.go b/arrow/compute/exprs/exec_test.go
index 370add2f..14ae8d16 100644
--- a/arrow/compute/exprs/exec_test.go
+++ b/arrow/compute/exprs/exec_test.go
@@ -899,6 +899,93 @@ func TestFieldRefTypeMismatchReleases(t *testing.T) {
}
}
+func TestNestedFieldRefResultOwnership(t *testing.T) {
+ scalarInput := func(t *testing.T, mem memory.Allocator, dt
*arrow.StructType, json string) *scalar.Struct {
+ arr, _, err := array.FromJSON(mem, dt, strings.NewReader(json))
+ require.NoError(t, err)
+ defer arr.Release()
+ sc, err := scalar.GetScalar(arr, 0)
+ require.NoError(t, err)
+ return sc.(*scalar.Struct)
+ }
+
+ execute := func(t *testing.T, mem memory.Allocator, sc *arrow.Schema, e
expr.Expression, input compute.Datum) compute.Datum {
+ ctx :=
exprs.WithExtensionIDSet(compute.WithAllocator(context.Background(), mem),
extSet)
+ result, err := exprs.ExecuteScalarExpression(ctx, sc, e, input)
+ require.NoError(t, err)
+ return result
+ }
+
+ t.Run("array struct field", func(t *testing.T) {
+ mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+ defer mem.AssertSize(t, 0)
+
+ sc := arrow.NewSchema([]arrow.Field{{Name: "s", Type:
arrow.StructOf(
+ arrow.Field{Name: "a", Type:
arrow.PrimitiveTypes.Int32, Nullable: true}), Nullable: true}}, nil)
+ rec, _, err := array.RecordFromJSON(mem, sc,
strings.NewReader(`[{"s": {"a": 1}}, {"s": {"a": 2}}]`))
+ require.NoError(t, err)
+
+ bldr := exprs.NewExprBuilder(extSet)
+ require.NoError(t, bldr.SetInputSchema(sc))
+ e, err := bldr.FieldRefList("s", "a").BuildExpr()
+ require.NoError(t, err)
+
+ result := execute(t, mem, sc, e,
compute.NewDatumWithoutOwning(rec))
+ rec.Release()
+ require.NotZero(t, mem.CurrentAlloc(), "result no longer holds
its buffers")
+
+ expected, _, err := array.FromJSON(mem,
arrow.PrimitiveTypes.Int32, strings.NewReader(`[1, 2]`))
+ require.NoError(t, err)
+ defer expected.Release()
+ assertEqual(t, expected, result)
+ result.Release()
+ })
+
+ t.Run("scalar struct field", func(t *testing.T) {
+ mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+ defer mem.AssertSize(t, 0)
+
+ dt := arrow.StructOf(arrow.Field{Name: "s", Type:
arrow.StructOf(
+ arrow.Field{Name: "b", Type: arrow.BinaryTypes.Binary,
Nullable: true}), Nullable: true})
+ input := scalarInput(t, mem, dt, `[{"s": {"b": "aGVsbG8="}}]`)
+ sc := arrow.NewSchema(dt.Fields(), nil)
+
+ bldr := exprs.NewExprBuilder(extSet)
+ require.NoError(t, bldr.SetInputSchema(sc))
+ e, err := bldr.FieldRefList("s", "b").BuildExpr()
+ require.NoError(t, err)
+
+ result := execute(t, mem, sc, e,
compute.NewDatumWithoutOwning(input))
+ input.Release()
+ require.NotZero(t, mem.CurrentAlloc(), "result no longer holds
its buffers")
+
+ assert.Equal(t, []byte("hello"),
result.(*compute.ScalarDatum).Value.(*scalar.Binary).Data())
+ result.Release()
+ })
+
+ t.Run("scalar list element", func(t *testing.T) {
+ mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+ defer mem.AssertSize(t, 0)
+
+ dt := arrow.StructOf(arrow.Field{Name: "s", Type:
arrow.StructOf(
+ arrow.Field{Name: "l", Type:
arrow.ListOf(arrow.BinaryTypes.Binary), Nullable: true}), Nullable: true})
+ input := scalarInput(t, mem, dt, `[{"s": {"l": ["aGVsbG8=",
"d29ybGQ="]}}]`)
+ defer input.Release()
+ sc := arrow.NewSchema(dt.Fields(), nil)
+
+ st, err := exprs.ToSubstraitType(dt, false, extSet)
+ require.NoError(t, err)
+ e, err := expr.NewRootFieldRef(&expr.StructFieldRef{Field: 0,
Child: &expr.StructFieldRef{Field: 0,
+ Child: &expr.ListElementRef{Offset: 1}}},
+ types.NewRecordTypeFromStruct(*st.(*types.StructType)))
+ require.NoError(t, err)
+
+ result := execute(t, mem, sc, e,
compute.NewDatumWithoutOwning(input))
+ defer result.Release()
+ assert.Equal(t, []byte("world"),
result.(*compute.ScalarDatum).Value.(*scalar.Binary).Data())
+ })
+}
+
func TestDecimalFilterLarge(t *testing.T) {
t.Parallel()