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 f1368259 fix(arrow/cdata): preserve unsliced STRUCT children on export 
(#1600)
f1368259 is described below

commit f13682594a172e2fd8f78ff33d60c1bd82b9a5b2
Author: Albert T. Wong <[email protected]>
AuthorDate: Wed Nov 18 09:12:00 2026 -0800

    fix(arrow/cdata): preserve unsliced STRUCT children on export (#1600)
    
    ### Rationale for this change
    
    Exporting a sliced STRUCT preserves the parent's offset but exports
    `Field(i)`, which has already applied that offset. Consumers apply it
    again, shifting/truncating values. The original two-row reproduction
    expects `[202, 303]` and gets `[303]`.
    
    ### What changes are included in this PR?
    
    Export arrays built from the original child data, releasing the
    temporary Go array after the C export retains it. This preserves each
    child's own offset and lets the consumer apply the parent offset once.
    
    Adds 12 round-trip cases covering primitive/nested STRUCT children,
    child offsets, parent slices (including empty slices), parent/child
    NULLs, structural validation and checked allocator balance.
    
    ### Are these changes tested?
    
    The new regression fails against upstream main before the fix. After the
    fix, both full C Data package runs pass on macOS arm64 with Go 1.27.3:
    
    - `go test -count=1 -p 1 -race -gcflags=all=-d=checkptr=2 -tags
    assert,test ./arrow/cdata`
    - `go test -count=1 -p 1 -tags assert,test,noasm ./arrow/cdata`
    
    These are C Data package checks, not a claim of a complete repository or
    cross-language/sanitizer matrix.
    
    ### Are there any user-facing changes?
    
    C Data export/import preserves sliced STRUCT values and child lengths.
    No public API change.
    
    Closes #1598.
---
 arrow/cdata/cdata_exports.go |  6 +++++-
 arrow/cdata/cdata_test.go    | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/arrow/cdata/cdata_exports.go b/arrow/cdata/cdata_exports.go
index a760ec53..f89d0e25 100644
--- a/arrow/cdata/cdata_exports.go
+++ b/arrow/cdata/cdata_exports.go
@@ -427,7 +427,11 @@ func exportArray(arr arrow.Array, out *CArrowArray, 
outSchema *CArrowSchema) {
                childPtrs := allocateArrowArrayPtrArr(arr.NumField())
                children := allocateArrowArrayArr(arr.NumField())
                for i := 0; i < arr.NumField(); i++ {
-                       exportArray(arr.Field(i), &children[i], nil)
+                       // Field(i) already applies the parent slice. C Data 
consumers apply
+                       // the exported parent offset, so retain the original 
child data.
+                       child := array.MakeFromData(arr.Data().Children()[i])
+                       exportArray(child, &children[i], nil)
+                       child.Release()
                        childPtrs[i] = &children[i]
                }
                out.children = (**CArrowArray)(unsafe.Pointer(&childPtrs[0]))
diff --git a/arrow/cdata/cdata_test.go b/arrow/cdata/cdata_test.go
index ca7a78e5..c2d12159 100644
--- a/arrow/cdata/cdata_test.go
+++ b/arrow/cdata/cdata_test.go
@@ -1077,6 +1077,48 @@ func TestEmptyListExport(t *testing.T) {
        assert.NotNil(t, out.children)
 }
 
+func TestSlicedStructRoundTrip(t *testing.T) {
+       for _, childType := range []arrow.DataType{
+               arrow.PrimitiveTypes.Int64,
+               arrow.StructOf(arrow.Field{Name: "x", Type: 
arrow.PrimitiveTypes.Int64, Nullable: true}),
+       } {
+               for _, childOffset := range []int64{0, 1} {
+                       for _, parentOffset := range []int64{0, 1, 4} {
+                               t.Run(fmt.Sprintf("%s/child=%d/parent=%d", 
childType, childOffset, parentOffset), func(t *testing.T) {
+                                       mem := 
memory.NewCheckedAllocator(memory.DefaultAllocator)
+                                       defer mem.AssertSize(t, 0)
+                                       json := `[101,202,null,404,505,606]`
+                                       if childType.ID() == arrow.STRUCT {
+                                               json = 
`[{"x":101},{"x":202},null,{"x":404},{"x":505},{"x":606}]`
+                                       }
+                                       values, _, err := array.FromJSON(mem, 
childType, strings.NewReader(json))
+                                       require.NoError(t, err)
+                                       defer values.Release()
+                                       child := array.NewSlice(values, 
childOffset, int64(values.Len()))
+                                       defer child.Release()
+                                       validity := 
memory.NewBufferBytes([]byte{0b00101101})
+                                       defer validity.Release()
+                                       parent, err := 
array.NewStructArrayWithNulls([]arrow.Array{child}, []string{"value"}, 
validity, array.UnknownNullCount, 0)
+                                       require.NoError(t, err)
+                                       defer parent.Release()
+                                       sliced := array.NewSlice(parent, 
parentOffset, 4)
+                                       defer sliced.Release()
+                                       var data CArrowArray
+                                       var schema CArrowSchema
+                                       ExportArrowArray(sliced, &data, &schema)
+                                       defer ReleaseCArrowArray(&data)
+                                       defer ReleaseCArrowSchema(&schema)
+                                       _, imported, err := ImportCArray(&data, 
&schema)
+                                       require.NoError(t, err)
+                                       defer imported.Release()
+                                       require.NoError(t, 
imported.(*array.Struct).ValidateFull())
+                                       assert.True(t, array.Equal(sliced, 
imported), "expected %s; got %s", sliced, imported)
+                               })
+                       }
+               }
+       }
+}
+
 func TestZeroSizeFixedSizeListRoundTrip(t *testing.T) {
        for _, element := range []arrow.DataType{arrow.Null, 
arrow.PrimitiveTypes.Int64, arrow.ListOf(arrow.BinaryTypes.String)} {
                t.Run(element.String(), func(t *testing.T) {

Reply via email to