ShivamSharma43 opened a new pull request, #1641: URL: https://github.com/apache/arrow-go/pull/1641
### Rationale for this change Exporting a sliced sparse union through the C Data interface produces an array that consumers cannot read correctly. Importing it back in Go panics: ```go u, _ := array.NewSparseUnionFromArraysWithFieldCodes(typeIDs, children, names, codes) // 6 rows sliced := array.NewSlice(u, 1, 6) cdata.ExportArrowArray(sliced, &carr, &cschema) cdata.ImportCArray(&carr, &cschema) // panic: arrow/array: index out of range ``` This is the same problem #1600 fixed for structs. The exported union keeps the parent's offset, but its children are exported from `Field(i)`, and for a sparse union `Field(i)` has already been sliced by that offset in `union.setData`. Consumers apply the parent offset to sparse union children again, so they read values shifted by the offset and past the end of the exported children. Dense unions are not affected, since `Field(i)` there is the child data unchanged. ### What changes are included in this PR? The union case in `exportArray` now exports arrays built from the original child data, the same way the struct case does, and releases the temporary Go array after the export retains it. For dense unions this exports the same data as before. ### Are these changes tested? Yes. `TestSlicedUnionRoundTrip` builds sparse and dense unions, with and without an offset on the children, slices them at offsets 0, 1 and 4, exports them, imports them back, and checks that the result passes `ValidateFull` and equals the slice, using a checked allocator. On `main` every sparse case with a non-zero parent offset panics with the error above. All cases pass with this change. `go test -tags test ./arrow/cdata/...` and `go test -tags assert,test ./arrow/cdata` pass, and `go vet` and gofmt report nothing for the package. I ran these with `GOARCH=386`, since the only C compiler on my machine is 32-bit. ### Are there any user-facing changes? Yes, a bug fix. Sliced sparse union arrays can now be exported through the C Data interface and read back correctly. AI assistance: I used Claude Code (Anthropic) to find this bug and write the fix and the test. I reviewed the code and ran the tests locally. 🤖 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]
