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 f8f9467e fix(arrow/cdata): return an error for Map imports with null
keys or entries (#1570)
f8f9467e is described below
commit f8f9467e095dbbd41c0c248292d8633608fac485
Author: Ayaan <[email protected]>
AuthorDate: Mon Nov 16 23:53:00 2026 +0530
fix(arrow/cdata): return an error for Map imports with null keys or entries
(#1570)
### Rationale for this change
Fixes #1557. Importing a C Data Map array whose keys or entries child
has nulls panics in `array.MakeFromData`. On the record batch path, a
checked allocator also reports leaked bytes.
### What changes are included in this PR?
Before building the Map, the importer checks the null counts of the
entries and keys children and returns `arrow.ErrInvalid` if either is
nonzero. An unknown null count (-1) is computed from the validity bitmap
using the child's offset, so valid children with an unknown count still
import.
### Are these changes tested?
Yes. `TestImportMapNullability` covers direct array, record batch,
stream and nested (List of Map) imports, with nulls in keys or entries,
known and unknown null counts, and sliced children. Each failure case
checks that the producer's release callback runs once and that the
checked allocator ends at zero; each one panics on main. The small C
helpers only count release calls and build a single-array stream,
because the Go export path validates the Map before exporting it. `go
test -race -short` with `-tags assert,test` and `-tags
assert,test,noasm` passes for `./arrow/cdata`. I couldn't run the
`ccalloc` variant locally (no Arrow C++ install).
### Are there any user-facing changes?
Importing a Map with null keys or entries now returns an error instead
of panicking.
---
arrow/cdata/cdata.go | 23 +++++++
arrow/cdata/cdata_fulltest.c | 58 +++++++++++++++++
arrow/cdata/cdata_test.go | 126 ++++++++++++++++++++++++++++++++++++
arrow/cdata/cdata_test_framework.go | 39 +++++++++++
4 files changed, 246 insertions(+)
diff --git a/arrow/cdata/cdata.go b/arrow/cdata/cdata.go
index 299f359e..b62cb012 100644
--- a/arrow/cdata/cdata.go
+++ b/arrow/cdata/cdata.go
@@ -744,6 +744,14 @@ func (imp *cimporter) doImport() error {
case *arrow.LargeListViewType:
return imp.importListViewLike()
case *arrow.MapType:
+ entries := imp.children[0].data.(*array.Data)
+ if resolveNullCount(entries) != 0 {
+ return fmt.Errorf("%w: map array child array should
have no nulls", arrow.ErrInvalid)
+ }
+ keys := entries.Children()[0].(*array.Data)
+ if resolveNullCount(keys) != 0 {
+ return fmt.Errorf("%w: map array keys array should have
no nulls", arrow.ErrInvalid)
+ }
return imp.importListLike()
case *arrow.FixedSizeListType:
if err := imp.checkNumChildren(1); err != nil {
@@ -973,6 +981,21 @@ func (imp *cimporter) importListLike() (err error) {
return
}
+// Map validation rejects unknown (-1) null counts.
+func resolveNullCount(d *array.Data) int {
+ nulls := d.NullN()
+ if nulls < 0 {
+ bufs := d.Buffers()
+ if len(bufs) > 0 && bufs[0] != nil {
+ nulls = d.Len() - bitutil.CountSetBits(bufs[0].Bytes(),
d.Offset(), d.Len())
+ } else {
+ nulls = 0
+ }
+ d.SetNullN(nulls)
+ }
+ return nulls
+}
+
func (imp *cimporter) importListViewLike() (err error) {
offsetSize := int64(imp.dt.Layout().Buffers[1].ByteWidth)
diff --git a/arrow/cdata/cdata_fulltest.c b/arrow/cdata/cdata_fulltest.c
index d9e67e93..5a3b0f37 100644
--- a/arrow/cdata/cdata_fulltest.c
+++ b/arrow/cdata/cdata_fulltest.c
@@ -525,3 +525,61 @@ int confuse_go_gc(struct ArrowArrayStream* stream,
unsigned int seed) {
return 0;
#undef DORAND
}
+
+typedef struct {
+ void (*release)(struct ArrowArray*);
+ void* private_data;
+ int count;
+} CountingReleaser;
+static void counting_release(struct ArrowArray* arr) {
+ CountingReleaser* cr = arr->private_data;
+ cr->count++;
+ arr->private_data = cr->private_data;
+ arr->release = cr->release;
+ arr->release(arr);
+}
+void* wrap_counting_release(struct ArrowArray* arr) {
+ CountingReleaser* cr = calloc(1, sizeof(*cr));
+ cr->release = arr->release;
+ cr->private_data = arr->private_data;
+ arr->release = counting_release;
+ arr->private_data = cr;
+ return cr;
+}
+int get_counting_release_count(void* cr) {
+ return ((CountingReleaser*)cr)->count;
+}
+typedef struct {
+ struct ArrowArray arr;
+ struct ArrowSchema schema;
+ int releases;
+} SingleArrayStream;
+static int single_get_schema(struct ArrowArrayStream* stream, struct
ArrowSchema* out) {
+ ArrowSchemaMove(&((SingleArrayStream*)stream->private_data)->schema, out);
+ return 0;
+}
+static int single_get_next(struct ArrowArrayStream* stream, struct ArrowArray*
out) {
+ ArrowArrayMove(&((SingleArrayStream*)stream->private_data)->arr, out);
+ return 0;
+}
+static void single_release(struct ArrowArrayStream* stream) {
+ SingleArrayStream* state = stream->private_data;
+ state->releases++;
+ ArrowArrayRelease(&state->arr);
+ ArrowSchemaRelease(&state->schema);
+ ArrowArrayStreamMarkReleased(stream);
+}
+void* setup_single_array_stream(struct ArrowArray* arr, struct ArrowSchema*
schema,
+ struct ArrowArrayStream* stream) {
+ SingleArrayStream* state = calloc(1, sizeof(*state));
+ ArrowArrayMove(arr, &state->arr);
+ ArrowSchemaMove(schema, &state->schema);
+ stream->get_schema = single_get_schema;
+ stream->get_next = single_get_next;
+ stream->release = single_release;
+ stream->private_data = state;
+ return state;
+}
+int get_single_array_stream_release_count(void* state) {
+ return ((SingleArrayStream*)state)->releases;
+}
diff --git a/arrow/cdata/cdata_test.go b/arrow/cdata/cdata_test.go
index 2bd10d4b..ca7a78e5 100644
--- a/arrow/cdata/cdata_test.go
+++ b/arrow/cdata/cdata_test.go
@@ -1702,3 +1702,129 @@ func TestAsyncPropagateError(t *testing.T) {
<-wait
}
+
+func TestImportMapNullability(t *testing.T) {
+ for _, path := range []string{"array", "record", "stream", "nested"} {
+ for _, child := range []string{"keys", "entries"} {
+ for _, count := range []string{"known", "unknown",
"valid", "offset", "offsetnull"} {
+ if (child == "entries" && count == "valid") ||
(path == "nested" && (child != "keys" || count != "unknown")) {
+ continue // covered by another case
+ }
+ t.Run(path+"/"+child+"/"+count, func(t
*testing.T) {
+ mem :=
memory.NewCheckedAllocator(memory.NewGoAllocator())
+ defer mem.AssertSize(t, 0)
+ b := array.NewMapBuilder(mem,
arrow.BinaryTypes.String, arrow.PrimitiveTypes.Int64, false)
+ b.Append(true)
+
b.KeyBuilder().(*array.StringBuilder).AppendValues([]string{"a", "b"}, nil)
+
b.ItemBuilder().(*array.Int64Builder).AppendValues([]int64{1, 2}, nil)
+ arr := b.NewMapArray()
+ b.Release()
+ defer arr.Release()
+ schema :=
arrow.NewSchema([]arrow.Field{{Name: "map", Type: arr.DataType(), Nullable:
true}}, nil)
+ var root CArrowArray
+ var nested arrow.Array
+ if path == "nested" {
+ lb := array.NewListBuilder(mem,
arr.DataType())
+ lb.Append(true)
+ mb :=
lb.ValueBuilder().(*array.MapBuilder)
+ mb.Append(true)
+
mb.KeyBuilder().(*array.StringBuilder).AppendValues([]string{"a", "b"}, nil)
+
mb.ItemBuilder().(*array.Int64Builder).AppendValues([]int64{1, 2}, nil)
+ nested = lb.NewArray()
+ lb.Release()
+ defer nested.Release()
+ ExportArrowArray(nested, &root,
nil)
+ } else if path == "array" {
+ ExportArrowArray(arr, &root,
nil)
+ } else {
+ rec :=
array.NewRecordBatch(schema, []arrow.Array{arr}, int64(arr.Len()))
+ ExportArrowRecordBatch(rec,
&root, nil)
+ rec.Release()
+ }
+ cr := wrapCountingRelease(&root)
+ defer freeCountingRelease(cr)
+ defer ReleaseCArrowArray(&root)
+ m := &root
+ if path != "array" {
+ m = unsafe.Slice(root.children,
root.n_children)[0]
+ }
+ entries := unsafe.Slice(m.children,
m.n_children)[0]
+ target := entries
+ if child == "keys" {
+ target =
unsafe.Slice(entries.children, entries.n_children)[0]
+ }
+ // The exported key bitmap is present;
entries need their own bitmap.
+ buffers := unsafe.Slice(target.buffers,
target.n_buffers)
+ if child == "entries" {
+ buffers[0] = testBitmap()
+ defer freeTestBitmap(buffers[0])
+ }
+ require.NotNil(t, buffers[0])
+ bits :=
unsafe.Slice((*byte)(buffers[0]), 1)
+ switch count {
+ case "known": // A producer reports a
known null count.
+ target.null_count = 1
+ case "unknown": // Resolve a null from
the bitmap.
+ target.null_count = -1
+ bits[0] &^= 1
+ case "valid": // An unknown count with
all bits set is valid.
+ target.null_count = -1
+ case "offset": // The null is outside
the visible window.
+ target.null_count = -1
+ bits[0] &^= 1
+ target.offset, target.length =
1, 1
+ if child == "keys" {
+ entries.offset,
entries.length = 1, 1
+ }
+ case "offsetnull": // The null is
inside the visible window.
+ target.null_count = -1
+ bits[0] &^= 2
+ target.offset, target.length =
1, 1
+ if child == "keys" {
+ entries.offset,
entries.length = 1, 1
+ }
+ }
+ var err error
+ switch path {
+ case "array":
+ var imported arrow.Array
+ imported, err =
ImportCArrayWithType(&root, arr.DataType())
+ if imported != nil {
+ imported.Release()
+ }
+ case "nested":
+ var imported arrow.Array
+ imported, err =
ImportCArrayWithType(&root, nested.DataType())
+ if imported != nil {
+ imported.Release()
+ }
+ case "record":
+ var imported arrow.RecordBatch
+ imported, err =
ImportCRecordBatchWithSchema(&root, schema)
+ if imported != nil {
+ imported.Release()
+ }
+ case "stream":
+ var csc CArrowSchema
+ ExportArrowSchema(schema, &csc)
+ defer ReleaseCArrowSchema(&csc)
+ var stream CArrowArrayStream
+ state :=
setupSingleArrayStream(&root, &csc, &stream)
+ defer
freeSingleArrayStreamState(state)
+ rdr, e :=
ImportCRecordReader(&stream, schema)
+ require.NoError(t, e)
+ _, err = rdr.Read()
+ rdr.(interface{ Release()
}).Release()
+ assert.Equal(t, 1,
getSingleArrayStreamReleaseCount(state))
+ }
+ if count == "known" || count ==
"unknown" || count == "offsetnull" {
+ assert.ErrorIs(t, err,
arrow.ErrInvalid)
+ } else {
+ assert.NoError(t, err)
+ }
+ assert.Equal(t, 1,
getCountingReleaseCount(cr))
+ })
+ }
+ }
+ }
+}
diff --git a/arrow/cdata/cdata_test_framework.go
b/arrow/cdata/cdata_test_framework.go
index ea180468..7dfb95cd 100644
--- a/arrow/cdata/cdata_test_framework.go
+++ b/arrow/cdata/cdata_test_framework.go
@@ -83,6 +83,10 @@ package cdata
// int confuse_go_gc(struct ArrowArrayStream* stream, unsigned int seed);
// extern void releaseTestArr(struct ArrowArray* array);
// extern void goReleaseTestArray(struct ArrowArray* array);
+// void* wrap_counting_release(struct ArrowArray* arr);
+// int get_counting_release_count(void* cr);
+// void* setup_single_array_stream(struct ArrowArray* arr, struct ArrowSchema*
sc, struct ArrowArrayStream* out);
+// int get_single_array_stream_release_count(void* stream_state);
import "C"
import (
@@ -541,3 +545,38 @@ func confuseGoGc(reader array.RecordReader) error {
}
return fmt.Errorf("Exported stream test failed with return code %d",
int(rc))
}
+
+func wrapCountingRelease(arr *CArrowArray) unsafe.Pointer {
+ return
C.wrap_counting_release((*C.struct_ArrowArray)(unsafe.Pointer(arr)))
+}
+
+func getCountingReleaseCount(cr unsafe.Pointer) int {
+ return int(C.get_counting_release_count(cr))
+}
+
+func freeCountingRelease(cr unsafe.Pointer) {
+ C.free(cr)
+}
+
+func setupSingleArrayStream(arr *CArrowArray, sc *CArrowSchema, out
*CArrowArrayStream) unsafe.Pointer {
+ return
C.setup_single_array_stream((*C.struct_ArrowArray)(unsafe.Pointer(arr)),
(*C.struct_ArrowSchema)(unsafe.Pointer(sc)),
(*C.struct_ArrowArrayStream)(unsafe.Pointer(out)))
+}
+
+func getSingleArrayStreamReleaseCount(state unsafe.Pointer) int {
+ return int(C.get_single_array_stream_release_count(state))
+}
+
+func freeSingleArrayStreamState(state unsafe.Pointer) {
+ C.free(state)
+}
+
+func testBitmap() unsafe.Pointer {
+ p := C.malloc(1)
+ const twoValidRows = 0b11
+ *(*byte)(p) = twoValidRows
+ return p
+}
+
+func freeTestBitmap(p unsafe.Pointer) {
+ C.free(p)
+}