ShivamSharma43 opened a new pull request, #1704:
URL: https://github.com/apache/arrow-go/pull/1704

   ### Rationale for this change
   
   Casting boolean, numeric, date, time or timestamp values to `utf8`, 
`large_utf8` or `string_view` leaks memory. With a checked allocator passed 
through `compute.WithAllocator`, casting `int32` `[-1, null, 2]` to `utf8` 
leaves several buffers allocated:
   
   ```
   LEAK of 64 bytes FROM
        github.com/apache/arrow-go/v18/arrow/array.(*bufferBuilder).resize+b0
   LEAK of 64 bytes FROM
        
github.com/apache/arrow-go/v18/arrow/compute/exec.(*KernelCtx).Allocate+b0
   ...
   ```
   
   There are two causes in `string_casts.go`:
   
   - `boolToStringCastExec`, `numericToStringCastExec`, `timeToStringCastExec` 
and `castTimestampToString` build the output with a builder and pass it to 
`out.TakeOwnership(arr.Data())`, which retains the buffers. They never release 
`arr`, so the builder's validity, offsets and data buffers stay allocated after 
the result is released. The other kernels that use `TakeOwnership` release 
their array afterwards.
   - These kernels keep the default `MemPrealloc`, so the executor preallocates 
an output data buffer, and `TakeOwnership` then replaces it without releasing 
it. The binary and view cast kernels in the same file already use 
`MemNoPrealloc`.
   
   With the Go allocator the GC eventually frees these buffers. With an 
allocator that needs an explicit free, such as the mallocator, they leak.
   
   The existing to-string cast tests didn't catch this because `checkCast` runs 
the cast with `context.Background()`, so the output used the default allocator, 
not the checked one.
   
   ### What changes are included in this PR?
   
   - The four kernels release the builder's array after `TakeOwnership`.
   - `addNumericAndTemporalToStringCasts` sets `MemNoPrealloc` on its kernels, 
since they always build their output themselves.
   
   Both are needed. With only one of them, every cast still leaks.
   
   ### Are these changes tested?
   
   Yes. `TestCastToStringReleasesBuilderArray` casts boolean, every integer and 
float type, `date32`, `date64`, `time32`, `time64` and timestamps with and 
without a time zone to `utf8`, `large_utf8` and `string_view`, with a checked 
allocator in the context. It checks the values and that nothing is left 
allocated. Without the fix all 51 subtests report leaks.
   
   On Windows amd64 with Go 1.26.11, `go test -tags assert,test 
./arrow/compute/...` passes and `go vet -tags assert ./arrow/compute/...` is 
clean.
   
   ### Are there any user-facing changes?
   
   No API changes. Casts to string types no longer leak memory.
   
   This PR was prepared with AI assistance (Claude Code).
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to