Skip to content

Commit 613e760

Browse files
committed
add ut
Signed-off-by: Cai Zhang <[email protected]>
1 parent 026e59a commit 613e760

File tree

2 files changed

+141
-2
lines changed

2 files changed

+141
-2
lines changed

arrow/cdata/cdata.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ func (imp *cimporter) doImportChildren() error {
408408
for i, c := range children {
409409
imp.children[i].dt = st.Field(i).Type
410410
if err := imp.children[i].importChild(imp, c); err != nil {
411+
for j := 0; j < i; j++ {
412+
if imp.children[j].data != nil {
413+
imp.children[j].data.Release()
414+
}
415+
}
411416
return err
412417
}
413418
}
@@ -431,6 +436,11 @@ func (imp *cimporter) doImportChildren() error {
431436
for i, c := range children {
432437
imp.children[i].dt = dt.Fields()[i].Type
433438
if err := imp.children[i].importChild(imp, c); err != nil {
439+
for j := 0; j < i; j++ {
440+
if imp.children[j].data != nil {
441+
imp.children[j].data.Release()
442+
}
443+
}
434444
return err
435445
}
436446
}
@@ -439,6 +449,11 @@ func (imp *cimporter) doImportChildren() error {
439449
for i, c := range children {
440450
imp.children[i].dt = dt.Fields()[i].Type
441451
if err := imp.children[i].importChild(imp, c); err != nil {
452+
for j := 0; j < i; j++ {
453+
if imp.children[j].data != nil {
454+
imp.children[j].data.Release()
455+
}
456+
}
442457
return err
443458
}
444459
}

arrow/cdata/cdata_test.go

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,8 @@ func createTestDenseUnion() arrow.Array {
669669

670670
func createTestUnionArr(mode arrow.UnionMode) arrow.Array {
671671
fields := []arrow.Field{
672-
arrow.Field{Name: "u0", Type: arrow.PrimitiveTypes.Int32, Nullable: true},
673-
arrow.Field{Name: "u1", Type: arrow.PrimitiveTypes.Uint8, Nullable: true},
672+
{Name: "u0", Type: arrow.PrimitiveTypes.Int32, Nullable: true},
673+
{Name: "u1", Type: arrow.PrimitiveTypes.Uint8, Nullable: true},
674674
}
675675
typeCodes := []arrow.UnionTypeCode{5, 10}
676676
bld := array.NewBuilder(memory.DefaultAllocator, arrow.UnionOf(mode, fields, typeCodes)).(array.UnionBuilder)
@@ -785,6 +785,126 @@ func TestRecordBatch(t *testing.T) {
785785
assert.True(t, array.RecordEqual(rb, rec))
786786
}
787787

788+
func TestImportStructWithInvalidSchema(t *testing.T) {
789+
mem := mallocator.NewMallocator()
790+
defer mem.AssertSize(t, 0)
791+
792+
arr := createTestStructArr()
793+
defer arr.Release()
794+
795+
carr := createCArr(arr, mem)
796+
defer freeTestMallocatorArr(carr, mem)
797+
798+
sc := testStruct([]string{"+s", "c", "l"}, []string{"", "a", "b"}, []int64{0, flagIsNullable, flagIsNullable})
799+
defer freeMallocedSchemas(sc)
800+
801+
top := (*[1]*CArrowSchema)(unsafe.Pointer(sc))[0]
802+
_, err := ImportCRecordBatch(carr, top)
803+
assert.Error(t, err)
804+
}
805+
806+
func TestImportDenseUnionWithInvalidSchema(t *testing.T) {
807+
mem := mallocator.NewMallocator()
808+
defer mem.AssertSize(t, 0)
809+
810+
unionArr := createTestDenseUnion()
811+
defer unionArr.Release()
812+
813+
structBld := array.NewStructBuilder(memory.DefaultAllocator, arrow.StructOf(
814+
arrow.Field{Name: "union_field", Type: unionArr.DataType(), Nullable: false},
815+
))
816+
defer structBld.Release()
817+
818+
unionBld := structBld.FieldBuilder(0).(*array.DenseUnionBuilder)
819+
structBld.Append(true)
820+
du := unionArr.(*array.DenseUnion)
821+
for i := 0; i < du.Len(); i++ {
822+
unionBld.Append(du.TypeCode(i))
823+
if du.TypeCode(i) == 5 {
824+
unionBld.Child(0).(*array.Int32Builder).Append(du.Field(0).(*array.Int32).Value(int(du.ValueOffset(i))))
825+
} else {
826+
unionBld.Child(1).(*array.Uint8Builder).Append(du.Field(1).(*array.Uint8).Value(int(du.ValueOffset(i))))
827+
}
828+
}
829+
830+
structArr := structBld.NewArray()
831+
defer structArr.Release()
832+
833+
carr := createCArr(structArr, mem)
834+
defer freeTestMallocatorArr(carr, mem)
835+
836+
unionSc := testUnion([]string{"+ud:5,10", "i", "u"}, []string{"", "u0", "u1"}, []int64{0, flagIsNullable, flagIsNullable})
837+
defer freeMallocedSchemas(unionSc)
838+
839+
structSc := testStruct([]string{"+s", "+ud:5,10"}, []string{"", "union_field"}, []int64{0, 0})
840+
defer freeMallocedSchemas(structSc)
841+
842+
structTop := (*[1]*CArrowSchema)(unsafe.Pointer(structSc))[0]
843+
unionTop := (*[1]*CArrowSchema)(unsafe.Pointer(unionSc))[0]
844+
845+
children := unsafe.Slice(structTop.children, 1)
846+
oldChild := children[0]
847+
children[0] = unionTop
848+
849+
_, err := ImportCRecordBatch(carr, structTop)
850+
851+
children[0] = oldChild
852+
853+
assert.Error(t, err)
854+
}
855+
856+
func TestImportSPARSEUnionWithInvalidSchema(t *testing.T) {
857+
mem := mallocator.NewMallocator()
858+
defer mem.AssertSize(t, 0)
859+
860+
unionArr := createTestSparseUnion()
861+
defer unionArr.Release()
862+
863+
structBld := array.NewStructBuilder(memory.DefaultAllocator, arrow.StructOf(
864+
arrow.Field{Name: "union_field", Type: unionArr.DataType(), Nullable: false},
865+
))
866+
defer structBld.Release()
867+
868+
unionBld := structBld.FieldBuilder(0).(*array.SparseUnionBuilder)
869+
structBld.Append(true)
870+
su := unionArr.(*array.SparseUnion)
871+
for i := 0; i < su.Len(); i++ {
872+
unionBld.Append(su.TypeCode(i))
873+
if su.TypeCode(i) == 5 {
874+
unionBld.Child(0).(*array.Int32Builder).Append(su.Field(0).(*array.Int32).Value(i))
875+
unionBld.Child(1).(*array.Uint8Builder).AppendNull()
876+
} else {
877+
unionBld.Child(0).(*array.Int32Builder).AppendNull()
878+
unionBld.Child(1).(*array.Uint8Builder).Append(su.Field(1).(*array.Uint8).Value(i))
879+
}
880+
}
881+
882+
structArr := structBld.NewArray()
883+
defer structArr.Release()
884+
885+
carr := createCArr(structArr, mem)
886+
defer freeTestMallocatorArr(carr, mem)
887+
888+
unionSc := testUnion([]string{"+us:5,10", "i", "u"}, []string{"", "u0", "u1"}, []int64{0, flagIsNullable, flagIsNullable})
889+
defer freeMallocedSchemas(unionSc)
890+
891+
structSc := testStruct([]string{"+s", "+us:5,10"}, []string{"", "union_field"}, []int64{0, 0})
892+
defer freeMallocedSchemas(structSc)
893+
894+
structTop := (*[1]*CArrowSchema)(unsafe.Pointer(structSc))[0]
895+
unionTop := (*[1]*CArrowSchema)(unsafe.Pointer(unionSc))[0]
896+
897+
children := unsafe.Slice(structTop.children, 1)
898+
oldChild := children[0]
899+
children[0] = unionTop
900+
901+
_, err := ImportCRecordBatch(carr, structTop)
902+
903+
children[0] = oldChild
904+
905+
assert.Error(t, err)
906+
}
907+
788908
func TestRecordReaderStream(t *testing.T) {
789909
stream := arrayStreamTest()
790910
defer releaseStreamTest(stream)
@@ -1006,17 +1126,21 @@ func (r *failingReader) Schema() *arrow.Schema {
10061126
}
10071127
return arrdata.Records["primitives"][0].Schema()
10081128
}
1129+
10091130
func (r *failingReader) Next() bool {
10101131
r.opCount -= 1
10111132
return r.opCount > 0
10121133
}
1134+
10131135
func (r *failingReader) RecordBatch() arrow.RecordBatch {
10141136
arrdata.Records["primitives"][0].Retain()
10151137
return arrdata.Records["primitives"][0]
10161138
}
1139+
10171140
func (r *failingReader) Record() arrow.Record {
10181141
return r.RecordBatch()
10191142
}
1143+
10201144
func (r *failingReader) Err() error {
10211145
if r.opCount == 0 {
10221146
return fmt.Errorf("Expected error message")

0 commit comments

Comments
 (0)