Skip to content

Commit 854d31e

Browse files
authored
feat(go/adbc/sqldriver): read from union types (#2637)
Implements reading from union types and adds a unit test. All tests pass, except those with C shared object dependencies. I did not build the code outside go/adbc - let me know if needed for this PR. In addition to the added unit test, I tested the scenario from the issue with DuckDB. Closes #2636
1 parent 028d22f commit 854d31e

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

Diff for: go/adbc/sqldriver/driver.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,9 @@ func (r *rows) Next(dest []driver.Value) error {
606606
dest[i] = nil
607607
continue
608608
}
609-
609+
if colUnion, ok := col.(array.Union); ok {
610+
col = colUnion.Field(colUnion.ChildID(int(r.curRow)))
611+
}
610612
switch col := col.(type) {
611613
case *array.Boolean:
612614
dest[i] = col.Value(int(r.curRow))

Diff for: go/adbc/sqldriver/driver_internals_test.go

+52-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,16 @@ func TestColumnTypeDatabaseTypeName(t *testing.T) {
136136
}
137137

138138
var (
139-
tz = time.FixedZone("North Idaho", -int((8 * time.Hour).Seconds()))
140-
testTime = time.Date(2023, time.January, 26, 15, 40, 39, 123456789, tz)
139+
tz = time.FixedZone("North Idaho", -int((8 * time.Hour).Seconds()))
140+
testTime = time.Date(2023, time.January, 26, 15, 40, 39, 123456789, tz)
141+
stringField = arrow.Field{
142+
Name: "str",
143+
Type: arrow.BinaryTypes.String,
144+
}
145+
int32Field = arrow.Field{
146+
Name: "int",
147+
Type: arrow.PrimitiveTypes.Int32,
148+
}
141149
)
142150

143151
func TestNextRowTypes(t *testing.T) {
@@ -254,6 +262,48 @@ func TestNextRowTypes(t *testing.T) {
254262
},
255263
golangValue: decimal256.FromU64(10),
256264
},
265+
{
266+
arrowType: arrow.SparseUnionOf([]arrow.Field{stringField, int32Field}, []arrow.UnionTypeCode{0, 1}),
267+
arrowValueFunc: func(t *testing.T, b array.Builder) {
268+
t.Helper()
269+
ub := b.(array.UnionBuilder)
270+
ub.Append(0)
271+
ub.Child(0).(*array.StringBuilder).Append("my-string")
272+
ub.Child(1).AppendEmptyValue()
273+
},
274+
golangValue: "my-string",
275+
},
276+
{
277+
arrowType: arrow.SparseUnionOf([]arrow.Field{stringField, int32Field}, []arrow.UnionTypeCode{0, 1}),
278+
arrowValueFunc: func(t *testing.T, b array.Builder) {
279+
t.Helper()
280+
ub := b.(array.UnionBuilder)
281+
ub.Append(1)
282+
ub.Child(1).(*array.Int32Builder).Append(100)
283+
ub.Child(0).AppendEmptyValue()
284+
285+
},
286+
golangValue: int32(100),
287+
},
288+
{
289+
arrowType: arrow.DenseUnionOf([]arrow.Field{int32Field, stringField}, []arrow.UnionTypeCode{10, 20}),
290+
arrowValueFunc: func(t *testing.T, b array.Builder) {
291+
t.Helper()
292+
ub := b.(array.UnionBuilder)
293+
ub.Append(20)
294+
ub.Child(1).(*array.StringBuilder).Append("my-string")
295+
},
296+
golangValue: "my-string",
297+
},
298+
{
299+
arrowType: arrow.DenseUnionOf([]arrow.Field{int32Field, stringField}, []arrow.UnionTypeCode{10, 20}),
300+
arrowValueFunc: func(t *testing.T, b array.Builder) {
301+
t.Helper()
302+
b.(array.UnionBuilder).Append(10)
303+
b.(array.UnionBuilder).Child(0).(*array.Int32Builder).Append(100)
304+
},
305+
golangValue: int32(100),
306+
},
257307
}
258308

259309
for i, test := range tests {

0 commit comments

Comments
 (0)