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 2bea73fa fix(arrow/cdata): export extension arrays as their storage 
(#1526)
2bea73fa is described below

commit 2bea73fae8e79517457fcbd9fb1a3cf285f16d26
Author: Harrison Crosse <[email protected]>
AuthorDate: Thu Nov 12 11:43:52 2026 -0500

    fix(arrow/cdata): export extension arrays as their storage (#1526)
    
    ### Rationale for this change
    
    Exporting extension arrays through C data can lose storage buffers,
    children or dictionaries, producing arrays that cannot be imported or
    validated.
    
    ### What changes are included in this PR?
    
    `exportArray` unwraps the storage before choosing the buffer layout and
    children. The schema is exported first, so extension metadata stays on
    it. The schema exporter also uses the storage type to export dictionary
    schemas.
    
    Import behavior is unchanged: imported arrays still use storage types
    with extension metadata.
    
    The new `string_view` case exposed an existing `-asan` leak, so
    `releaseExportedArray` also frees view arrays' variadic buffer sizes.
    
    ### Are these changes tested?
    
    `TestExtensionArrayRoundTrip` checks `string_view`, list, `sparse_union`
    and dictionary storage, buffer counts, imported values and extension
    metadata. All four cases fail on main and pass with this change. A
    `string_view_inline` case covers views without data buffers.
    
    With Go 1.26.10, cgo race tests, Linux `-asan` tests of `arrow/cdata`,
    `golangci-lint` on the `arrow` module and the Release Audit Tool passed.
    `GOARCH=386` and full `./arrow/...` tests passed before the leak fix.
    
    ### Are there any user-facing changes?
    
    Yes. C data exports retain extension storage layout, children and
    dictionaries, and view exports no longer leak. There is no API change.
    
    Closes https://github.com/apache/arrow-go/issues/1523.
---
 arrow/cdata/cdata_exports.go | 16 +++++++++------
 arrow/cdata/cdata_test.go    | 48 ++++++++++++++++++++++++++++++++++++++++++++
 arrow/cdata/exports.go       | 10 +++++++--
 3 files changed, 66 insertions(+), 8 deletions(-)

diff --git a/arrow/cdata/cdata_exports.go b/arrow/cdata/cdata_exports.go
index 1e8e9bf9..ef2c8b39 100644
--- a/arrow/cdata/cdata_exports.go
+++ b/arrow/cdata/cdata_exports.go
@@ -288,12 +288,13 @@ func (exp *schemaExporter) exportFormat(dt 
arrow.DataType) string {
 
 func (exp *schemaExporter) export(field arrow.Field) {
        exp.name = field.Name
-       exp.format = exp.exportFormat(exp.handleExtension(field.Type))
+       storage := exp.handleExtension(field.Type)
+       exp.format = exp.exportFormat(storage)
        if field.Nullable {
                exp.flags |= C.ARROW_FLAG_NULLABLE
        }
 
-       switch dt := field.Type.(type) {
+       switch dt := storage.(type) {
        case *arrow.DictionaryType:
                exp.dict = new(schemaExporter)
                exp.dict.export(arrow.Field{Type: dt.ValueType})
@@ -346,6 +347,9 @@ func exportArray(arr arrow.Array, out *CArrowArray, 
outSchema *CArrowSchema) {
        if outSchema != nil {
                exportField(arrow.Field{Type: arr.DataType()}, outSchema)
        }
+       if ext, ok := arr.(array.ExtensionArray); ok {
+               arr = ext.Storage()
+       }
 
        buffers := arr.Data().Buffers()
        // Some types don't have validity bitmaps, but we keep them shifted
@@ -388,14 +392,14 @@ func exportArray(arr arrow.Array, out *CArrowArray, 
outSchema *CArrowSchema) {
                        cBufs[i] = (*C.void)(unsafe.Pointer(&buf.Bytes()[0]))
                }
 
-               if has_buffer_sizes_buffer {
+               // with no data buffers the sizes buffer would be empty, and 
the C data
+               // interface allows a NULL pointer for an empty buffer
+               if has_buffer_sizes_buffer && len(buffers) > 2 {
                        sizes := allocateBufferSizeArr(len(buffers[2:]))
                        for i, buf := range buffers[2:] {
                                sizes[i] = C.int64_t(buf.Len())
                        }
-                       if len(sizes) > 0 {
-                               cBufs[nbuffers-1] = 
(*C.void)(unsafe.Pointer(&sizes[0]))
-                       }
+                       cBufs[nbuffers-1] = (*C.void)(unsafe.Pointer(&sizes[0]))
                }
                out.buffers = (*unsafe.Pointer)(unsafe.Pointer(&cBufs[0]))
        }
diff --git a/arrow/cdata/cdata_test.go b/arrow/cdata/cdata_test.go
index dcc2851f..22ac6b2d 100644
--- a/arrow/cdata/cdata_test.go
+++ b/arrow/cdata/cdata_test.go
@@ -32,6 +32,7 @@ import (
        "math"
        "runtime"
        "runtime/cgo"
+       "strings"
        "sync"
        "testing"
        "time"
@@ -40,6 +41,7 @@ import (
        "github.com/apache/arrow-go/v18/arrow"
        "github.com/apache/arrow-go/v18/arrow/array"
        "github.com/apache/arrow-go/v18/arrow/decimal128"
+       "github.com/apache/arrow-go/v18/arrow/extensions"
        "github.com/apache/arrow-go/v18/arrow/internal/arrdata"
        "github.com/apache/arrow-go/v18/arrow/memory"
        "github.com/apache/arrow-go/v18/arrow/memory/mallocator"
@@ -1191,6 +1193,52 @@ func TestEmptyUnionExport(t *testing.T) {
        assert.NotEqualValues(t, unsafe.Pointer(nil), buffers[0])
 }
 
+func TestExtensionArrayRoundTrip(t *testing.T) {
+       tests := []struct {
+               name     string
+               storage  arrow.DataType
+               json     string
+               nbuffers int
+       }{
+               {"string_view", arrow.BinaryTypes.StringView, `["a", null, 
"longer than twelve bytes"]`, 4},
+               {"string_view_inline", arrow.BinaryTypes.StringView, `["a", 
null, "bc"]`, 3},
+               {"list", arrow.ListOf(arrow.PrimitiveTypes.Int32), `[[1, 2], 
null, []]`, 2},
+               {"sparse_union", arrow.SparseUnionOf([]arrow.Field{{Name: "i", 
Type: arrow.PrimitiveTypes.Int32, Nullable: true}}, []arrow.UnionTypeCode{0}), 
`[[0, 1], [0, null]]`, 1},
+               {"dictionary", &arrow.DictionaryType{IndexType: 
arrow.PrimitiveTypes.Int8, ValueType: arrow.BinaryTypes.String}, `["a", null, 
"b", "a"]`, 2},
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       mem := 
memory.NewCheckedAllocator(memory.DefaultAllocator)
+                       defer mem.AssertSize(t, 0)
+
+                       storage, _, err := array.FromJSON(mem, tt.storage, 
strings.NewReader(tt.json))
+                       require.NoError(t, err)
+                       defer storage.Release()
+
+                       arr, _, err := array.FromJSON(mem, 
extensions.NewOpaqueType(tt.storage, "t", "v"), strings.NewReader(tt.json))
+                       require.NoError(t, err)
+                       defer arr.Release()
+
+                       var out CArrowArray
+                       var sc CArrowSchema
+                       ExportArrowArray(arr, &out, &sc)
+                       defer ReleaseCArrowArray(&out)
+                       defer ReleaseCArrowSchema(&sc)
+                       assert.EqualValues(t, tt.nbuffers, out.n_buffers)
+
+                       field, imported, err := ImportCArray(&out, &sc)
+                       require.NoError(t, err)
+                       defer imported.Release()
+
+                       require.NoError(t, array.ValidateFull(imported))
+                       assert.Truef(t, array.Equal(storage, imported), 
"expected %s, got %s", storage, imported)
+                       name, _ := 
field.Metadata.GetValue("ARROW:extension:name")
+                       assert.Equal(t, "arrow.opaque", name)
+               })
+       }
+}
+
 func TestValidateCArrayHeader(t *testing.T) {
        const maxInt64 = int64(1<<63 - 1)
 
diff --git a/arrow/cdata/exports.go b/arrow/cdata/exports.go
index 6367a5b3..a3584046 100644
--- a/arrow/cdata/exports.go
+++ b/arrow/cdata/exports.go
@@ -24,6 +24,7 @@ import (
 
        "github.com/apache/arrow-go/v18/arrow"
        "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/arrow-go/v18/arrow/internal"
 )
 
 // #include <stdlib.h>
@@ -125,7 +126,13 @@ func releaseExportedArray(arr *CArrowArray) {
        }
        defer C.ArrowArrayMarkReleased(arr)
 
+       h := getHandle(arr.private_data)
+       data := h.Value().(arrow.ArrayData)
+
        if arr.n_buffers > 0 {
+               if internal.HasBufferSizesBuffer(data.DataType().ID()) {
+                       C.free(unsafe.Slice(arr.buffers, 
arr.n_buffers)[arr.n_buffers-1])
+               }
                C.free(unsafe.Pointer(arr.buffers))
        }
 
@@ -144,8 +151,7 @@ func releaseExportedArray(arr *CArrowArray) {
                C.free(unsafe.Pointer(arr.children))
        }
 
-       h := getHandle(arr.private_data)
-       h.Value().(arrow.ArrayData).Release()
+       data.Release()
        h.Delete()
        C.free(unsafe.Pointer(arr.private_data))
 }

Reply via email to