Skip to content

Commit

Permalink
add workaround for SQLNULL
Browse files Browse the repository at this point in the history
  • Loading branch information
taniabogatsch committed Sep 17, 2024
1 parent cd7dc56 commit 31ef67c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions data_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (chunk *DataChunk) GetValue(colIdx int, rowIdx int) (any, error) {
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 @@ -58,6 +61,10 @@ func (chunk *DataChunk) SetValue(colIdx int, rowIdx int, val any) error {
}
column := &chunk.columns[colIdx]

if column.isSQLNull {
return getError(errAPI, errSetSQLNULLValue)
}

// Ensure that the types match before attempting to set anything.
// This is done to prevent failures 'halfway through' writing column values,
// potentially corrupting data in that column.
Expand Down
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ var (
errScalarUDFResultTypeIsNil = fmt.Errorf("%w: result type is nil", errScalarUDFCreate)
errScalarUDFResultTypeIsANY = fmt.Errorf("%w: result type is ANY", errScalarUDFCreate)

errSetSQLNULLValue = errors.New("cannot write to a NULL column")

// Errors not covered in tests.
errConnect = errors.New("could not connect to database")
errCreateConfig = errors.New("could not create config for database")
Expand Down
3 changes: 0 additions & 3 deletions scalar_udf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,6 @@ func (*anyScalarUDF) ExecuteRow(args []driver.Value) (any, error) {
}

func TestANYScalarUDF(t *testing.T) {
// TODO: once DuckDB has SQLNull type.
return

db, err := sql.Open("duckdb", "")
require.NoError(t, err)

Expand Down
12 changes: 12 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type vector struct {
// The child vectors of nested data types.
childVectors []vector

// FIXME: this is a workaround until the C API exposes SQLNULL.
isSQLNull bool

// The vector's type information.
vectorTypeInfo
}
Expand Down Expand Up @@ -253,6 +256,11 @@ func (vec *vector) tryCastUUID(val any) (UUID, error) {
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 @@ -312,6 +320,10 @@ func (vec *vector) init(logicalType C.duckdb_logical_type, colIdx int) error {
}

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

0 comments on commit 31ef67c

Please sign in to comment.