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 49f0ac44 fix(arrow/compute): release the binary memo table after 
unique (#1697)
49f0ac44 is described below

commit 49f0ac44849c953b74aafcebcb342be11e62e8b7
Author: Shivam Sharma <[email protected]>
AuthorDate: Fri Nov 27 22:35:43 2026 +0530

    fix(arrow/compute): release the binary memo table after unique (#1697)
    
    ### Rationale for this change
    
    `compute.Unique` leaks memory for binary, string, large binary/string,
    fixed size binary, decimal128/256 and `month_day_nano_interval` inputs.
    With a checked allocator passed through `compute.WithAllocator`:
    
    ```
    LEAK of 64 bytes FROM
    LEAK of 128 bytes FROM
    ...
    ```
    
    These types are hashed with a `hashing.BinaryMemoTable`, and its builder
    allocates from the context allocator. `dictionaryEncodeFinalize`
    releases that table through `releaseHashMemo`, but `uniqueFinalize`
    never does, so each `unique` call leaves the memo table's buffers
    allocated. With the Go allocator the GC eventually frees them. With an
    allocator that needs an explicit free, such as the mallocator, they
    leak.
    
    The existing unique tests didn't catch this because they call
    `compute.UniqueArray(context.TODO(), ...)`, so the memo table used the
    default allocator, not the checked one.
    
    ### What changes are included in this PR?
    
    `uniqueFinalize` now defers `releaseHashMemo(impl)`, the same way
    `dictionaryEncodeFinalize` does. `GetDictionary` copies the values out
    of the memo table into new buffers, so releasing the table after that is
    safe. `regularHashState.Reset` already makes a new table if the old one
    was released.
    
    ### Are these changes tested?
    
    Yes. `TestUniqueReleasesBinaryMemoTable` runs `unique` on array and
    chunked input for each affected type with a checked allocator in the
    context, and checks that the results are right and nothing is left
    allocated. Without the fix every subtest reports leaks.
    
    On Windows amd64 with Go 1.26.11, `go test -tags assert,test
    ./arrow/compute/...` and `go test -short -tags assert,test,noasm
    ./arrow/compute/...` pass, and `go vet` is clean for the changed
    packages.
    
    ### Are there any user-facing changes?
    
    No API changes. `unique` no longer leaks memory for these types.
    
    This PR was prepared with AI assistance (Claude Code).
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 arrow/compute/internal/kernels/vector_hash.go |  3 ++
 arrow/compute/vector_hash_test.go             | 54 +++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/arrow/compute/internal/kernels/vector_hash.go 
b/arrow/compute/internal/kernels/vector_hash.go
index e1a89e0f..a6fe6918 100644
--- a/arrow/compute/internal/kernels/vector_hash.go
+++ b/arrow/compute/internal/kernels/vector_hash.go
@@ -823,6 +823,9 @@ func uniqueFinalize(ctx *exec.KernelCtx, results 
[]*exec.ArraySpan) ([]*exec.Arr
        if !ok {
                return nil, fmt.Errorf("%w: HashState in invalid state", 
arrow.ErrInvalid)
        }
+       // GetDictionary copies the values out of the memo table, so the
+       // table's buffers can be released once the result is built.
+       defer releaseHashMemo(impl)
 
        for _, r := range results {
                // release any pre-allocation we did
diff --git a/arrow/compute/vector_hash_test.go 
b/arrow/compute/vector_hash_test.go
index 2dc44f8c..0b20c54d 100644
--- a/arrow/compute/vector_hash_test.go
+++ b/arrow/compute/vector_hash_test.go
@@ -318,6 +318,60 @@ func TestUniqueBoolean(t *testing.T) {
        assert.True(t, array.Equal(expected, result))
 }
 
+// The types below are hashed with a binary memo table, whose buffers come
+// from the context allocator and have to be released once unique is done.
+func TestUniqueReleasesBinaryMemoTable(t *testing.T) {
+       tests := []struct {
+               dt       arrow.DataType
+               input    string
+               expected string
+       }{
+               {arrow.BinaryTypes.String, `["foo", null, "bar", "foo"]`, 
`["foo", null, "bar"]`},
+               {arrow.BinaryTypes.LargeString, `["foo", null, "bar", "foo"]`, 
`["foo", null, "bar"]`},
+               {arrow.BinaryTypes.Binary, `["Zm9v", null, "YmFy", "Zm9v"]`, 
`["Zm9v", null, "YmFy"]`},
+               {arrow.BinaryTypes.LargeBinary, `["Zm9v", null, "YmFy", 
"Zm9v"]`, `["Zm9v", null, "YmFy"]`},
+               {&arrow.FixedSizeBinaryType{ByteWidth: 3}, `["Zm9v", null, 
"YmFy", "Zm9v"]`, `["Zm9v", null, "YmFy"]`},
+               {&arrow.Decimal128Type{Precision: 5, Scale: 2}, `["1.25", null, 
"-3.50", "1.25"]`, `["1.25", null, "-3.50"]`},
+               {&arrow.Decimal256Type{Precision: 5, Scale: 2}, `["1.25", null, 
"-3.50", "1.25"]`, `["1.25", null, "-3.50"]`},
+               {arrow.FixedWidthTypes.MonthDayNanoInterval,
+                       `[{"months": 1, "days": 2, "nanoseconds": 3}, null, 
{"months": 4, "days": 5, "nanoseconds": 6}, {"months": 1, "days": 2, 
"nanoseconds": 3}]`,
+                       `[{"months": 1, "days": 2, "nanoseconds": 3}, null, 
{"months": 4, "days": 5, "nanoseconds": 6}]`},
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.dt.String(), func(t *testing.T) {
+                       mem := 
memory.NewCheckedAllocator(memory.DefaultAllocator)
+                       defer mem.AssertSize(t, 0)
+                       ctx := compute.WithAllocator(context.Background(), mem)
+
+                       input, _, err := array.FromJSON(mem, tt.dt, 
strings.NewReader(tt.input))
+                       require.NoError(t, err)
+                       defer input.Release()
+                       expected, _, err := array.FromJSON(mem, tt.dt, 
strings.NewReader(tt.expected))
+                       require.NoError(t, err)
+                       defer expected.Release()
+
+                       result, err := compute.UniqueArray(ctx, input)
+                       require.NoError(t, err)
+                       defer result.Release()
+
+                       assertArraysEqual(t, expected, result)
+
+                       // chunked input runs the kernel once per chunk and 
finalizes once
+                       chunked := arrow.NewChunked(tt.dt, []arrow.Array{input, 
input})
+                       defer chunked.Release()
+
+                       out, err := compute.Unique(ctx, 
&compute.ChunkedDatum{Value: chunked})
+                       require.NoError(t, err)
+                       defer out.Release()
+
+                       chunkedResult := out.(*compute.ArrayDatum).MakeArray()
+                       defer chunkedResult.Release()
+                       assertArraysEqual(t, expected, chunkedResult)
+               })
+       }
+}
+
 func TestUniqueTimeTimestamp(t *testing.T) {
        mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
        defer mem.AssertSize(t, 0)

Reply via email to