Skip to content

Commit

Permalink
adjust code to support the new SQLNULL type
Browse files Browse the repository at this point in the history
  • Loading branch information
taniabogatsch committed Sep 24, 2024
1 parent 86f2491 commit ca261cf
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 35 deletions.
9 changes: 1 addition & 8 deletions data_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ func (chunk *DataChunk) GetValue(colIdx int, rowIdx int) (any, error) {
if colIdx >= len(chunk.columns) {
return nil, getError(errAPI, columnCountError(colIdx, len(chunk.columns)))
}

column := &chunk.columns[colIdx]
if column.isSQLNull {
return nil, nil
}
return column.getFn(column, C.idx_t(rowIdx)), nil
}

Expand All @@ -62,11 +60,6 @@ func (chunk *DataChunk) SetValue(colIdx int, rowIdx int, val any) error {
}

column := &chunk.columns[colIdx]
if column.isSQLNull {
return getError(errAPI, errSetSQLNULLValue)
}

// Set the value.
return column.setFn(column, C.idx_t(rowIdx), val)
}

Expand Down
2 changes: 2 additions & 0 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
TYPE_TIMESTAMP_TZ Type = C.DUCKDB_TYPE_TIMESTAMP_TZ
TYPE_ANY Type = C.DUCKDB_TYPE_ANY
TYPE_VARINT Type = C.DUCKDB_TYPE_VARINT
TYPE_SQLNULL Type = C.DUCKDB_TYPE_SQLNULL
)

// FIXME: Implement support for these types.
Expand Down Expand Up @@ -96,4 +97,5 @@ var typeToStringMap = map[Type]string{
TYPE_TIMESTAMP_TZ: "TIMESTAMPTZ",
TYPE_ANY: "ANY",
TYPE_VARINT: "VARINT",
TYPE_SQLNULL: "SQLNULL",
}
2 changes: 2 additions & 0 deletions type_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func NewTypeInfo(t Type) (TypeInfo, error) {
return nil, getError(errAPI, tryOtherFuncError(funcName(NewStructInfo)))
case TYPE_MAP:
return nil, getError(errAPI, tryOtherFuncError(funcName(NewMapInfo)))
case TYPE_SQLNULL:
return nil, getError(errAPI, unsupportedTypeError(typeToStringMap[t]))
}

return &typeInfo{
Expand Down
3 changes: 2 additions & 1 deletion type_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func getTypeInfos(t *testing.T, useAny bool) []testTypeInfo {
continue
}
switch k {
case TYPE_DECIMAL, TYPE_ENUM, TYPE_LIST, TYPE_STRUCT, TYPE_MAP:
case TYPE_DECIMAL, TYPE_ENUM, TYPE_LIST, TYPE_STRUCT, TYPE_MAP, TYPE_SQLNULL:
continue
}
primitiveTypes = append(primitiveTypes, k)
Expand Down Expand Up @@ -190,6 +190,7 @@ func TestErrTypeInfo(t *testing.T) {
unsupportedTypes = append(unsupportedTypes, k)
}
}
unsupportedTypes = append(unsupportedTypes, TYPE_SQLNULL)

for _, unsupported := range unsupportedTypes {
_, err := NewTypeInfo(unsupported)
Expand Down
26 changes: 12 additions & 14 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ type vector struct {
// The child vectors of nested data types.
childVectors []vector

// FIXME: This is a workaround until the C API exposes SQLNULL.
// FIXME: Then, SQLNULL becomes another Type value (C.DUCKDB_TYPE_SQLNULL).
isSQLNull bool

// The vector's type information.
vectorTypeInfo
}
Expand All @@ -45,12 +41,6 @@ func (*vector) canNil(val reflect.Value) bool {

func (vec *vector) init(logicalType C.duckdb_logical_type, colIdx int) error {
t := Type(C.duckdb_get_type_id(logicalType))

if t == TYPE_INVALID {
vec.isSQLNull = true
return nil
}

name, inMap := unsupportedTypeToStringMap[t]
if inMap {
return addIndexToError(unsupportedTypeError(name), colIdx)
Expand Down Expand Up @@ -103,6 +93,8 @@ func (vec *vector) init(logicalType C.duckdb_logical_type, colIdx int) error {
return vec.initMap(logicalType, colIdx)
case TYPE_UUID:
vec.initUUID()
case TYPE_SQLNULL:
vec.initSQLNull()
default:
return addIndexToError(unsupportedTypeError(unknownTypeErrMsg), colIdx)
}
Expand All @@ -123,10 +115,6 @@ func (vec *vector) resetChildData() {
}

func (vec *vector) initVectors(v C.duckdb_vector, writable bool) {
if vec.isSQLNull {
return
}

vec.duckdbVector = v
vec.ptr = C.duckdb_vector_get_data(v)
if writable {
Expand Down Expand Up @@ -482,3 +470,13 @@ func (vec *vector) initUUID() {
}
vec.Type = TYPE_UUID
}

func (vec *vector) initSQLNull() {
vec.getFn = func(vec *vector, rowIdx C.idx_t) any {
return nil
}
vec.setFn = func(vec *vector, rowIdx C.idx_t, val any) error {
return errSetSQLNULLValue
}
vec.Type = TYPE_SQLNULL
}
16 changes: 4 additions & 12 deletions vector_getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,8 @@ func (vec *vector) getList(rowIdx C.idx_t) []any {

// Fill the slice with all child values.
for i := C.idx_t(0); i < entry.length; i++ {
if child.isSQLNull {
slice = append(slice, nil)
} else {
val := child.getFn(child, i+entry.offset)
slice = append(slice, val)
}
val := child.getFn(child, i+entry.offset)
slice = append(slice, val)
}
return slice
}
Expand All @@ -162,12 +158,8 @@ func (vec *vector) getStruct(rowIdx C.idx_t) map[string]any {
m := map[string]any{}
for i := 0; i < len(vec.childVectors); i++ {
child := &vec.childVectors[i]
if child.isSQLNull {
m[vec.structEntries[i].Name()] = nil
} else {
val := child.getFn(child, rowIdx)
m[vec.structEntries[i].Name()] = val
}
val := child.getFn(child, rowIdx)
m[vec.structEntries[i].Name()] = val
}
return m
}
Expand Down

0 comments on commit ca261cf

Please sign in to comment.