Skip to content

Commit

Permalink
fix(arrow/cdata): handle export struct with no fields (#175)
Browse files Browse the repository at this point in the history
Fixes #172 

Includes a test that reproduced the original reported issue
  • Loading branch information
zeroshade authored Oct 28, 2024
1 parent b6b7cd6 commit b7e68db
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
7 changes: 7 additions & 0 deletions arrow/cdata/cdata_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ func exportArray(arr arrow.Array, out *CArrowArray, outSchema *CArrowSchema) {
out.children = (**CArrowArray)(unsafe.Pointer(&childPtrs[0]))
case *array.Struct:
out.n_children = C.int64_t(arr.NumField())
if arr.NumField() == 0 {
return
}
childPtrs := allocateArrowArrayPtrArr(arr.NumField())
children := allocateArrowArrayArr(arr.NumField())
for i := 0; i < arr.NumField(); i++ {
Expand All @@ -421,6 +424,10 @@ func exportArray(arr arrow.Array, out *CArrowArray, outSchema *CArrowSchema) {
exportArray(arr.Dictionary(), out.dictionary, nil)
case array.Union:
out.n_children = C.int64_t(arr.NumFields())
if arr.NumFields() == 0 {
return
}

childPtrs := allocateArrowArrayPtrArr(arr.NumFields())
children := allocateArrowArrayArr(arr.NumFields())
for i := 0; i < arr.NumFields(); i++ {
Expand Down
25 changes: 25 additions & 0 deletions arrow/cdata/cdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,28 @@ func createTestStructArr() arrow.Array {
return bld.NewArray()
}

func createTestEmptyStructArr() arrow.Array {
bld := array.NewStructBuilder(memory.DefaultAllocator, arrow.StructOf())
defer bld.Release()

bld.AppendNull()
return bld.NewArray()
}

func createTestEmptyDenseUnionArr() arrow.Array {
bld := array.NewEmptyDenseUnionBuilder(memory.DefaultAllocator)
defer bld.Release()

return bld.NewArray()
}

func createTestEmptySparseUnionArr() arrow.Array {
bld := array.NewEmptySparseUnionBuilder(memory.DefaultAllocator)
defer bld.Release()

return bld.NewArray()
}

func createTestRunEndsArr() arrow.Array {
bld := array.NewRunEndEncodedBuilder(memory.DefaultAllocator,
arrow.PrimitiveTypes.Int32, arrow.PrimitiveTypes.Int8)
Expand Down Expand Up @@ -687,6 +709,9 @@ func TestNestedArrays(t *testing.T) {
{"sparse union", createTestSparseUnion},
{"dense union", createTestDenseUnion},
{"run-end encoded", createTestRunEndsArr},
{"empty struct", createTestEmptyStructArr},
{"empty dense union", createTestEmptyDenseUnionArr},
{"empty sparse union", createTestEmptySparseUnionArr},
}

for _, tt := range tests {
Expand Down
8 changes: 7 additions & 1 deletion arrow/cdata/cdata_test_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ func createCArr(arr arrow.Array, alloc *mallocator.Mallocator) *CArrowArray {
children = (**CArrowArray)(unsafe.Pointer(&clist[0]))
nchildren += 1
case *array.Struct:
if arr.NumField() == 0 {
break
}
clist := allocateChildrenPtrArr(alloc, arr.NumField())
for i := 0; i < arr.NumField(); i++ {
clist[i] = createCArr(arr.Field(i), alloc)
Expand All @@ -322,6 +325,9 @@ func createCArr(arr arrow.Array, alloc *mallocator.Mallocator) *CArrowArray {
children = (**CArrowArray)(unsafe.Pointer(&clist[0]))
nchildren += 2
case array.Union:
if arr.NumFields() == 0 {
break
}
clist := allocateChildrenPtrArr(alloc, arr.NumFields())
for i := 0; i < arr.NumFields(); i++ {
clist[i] = createCArr(arr.Field(i), alloc)
Expand Down Expand Up @@ -356,7 +362,7 @@ func createCArr(arr arrow.Array, alloc *mallocator.Mallocator) *CArrowArray {
tr.bufs = make([][]byte, 0, nbuffers)
cbufs := allocateBufferMallocatorPtrArr(alloc, nbuffers)
for i, b := range buffers[bufOffset:] {
if b != nil {
if b != nil && b.Len() > 0 {
raw := alloc.Allocate(b.Len())
copy(raw, b.Bytes())
tr.bufs = append(tr.bufs, raw)
Expand Down

0 comments on commit b7e68db

Please sign in to comment.