Skip to content

Commit

Permalink
Merge pull request #6 from k-anshul/enum_support
Browse files Browse the repository at this point in the history
scan enum to string
  • Loading branch information
k-anshul authored Nov 7, 2023
2 parents da18111 + 492d863 commit 52995e1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion duckdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,20 @@ func TestENUMs(t *testing.T) {

var name string
var env string
require.NoError(t, db.QueryRow("SELECT name, CAST(environment AS text) FROM vehicles WHERE environment = ?", "Air").Scan(&name, &env))
require.NoError(t, db.QueryRow("SELECT name, environment FROM vehicles WHERE environment = ?", "Air").Scan(&name, &env))
require.Equal(t, "Aircraft", name)
require.Equal(t, "Air", env)

// enum list
_, err = db.Exec("CREATE TABLE all_enums (environments element[])")
require.NoError(t, err)

_, err = db.Exec("INSERT INTO all_enums VALUES (['Air', 'Sea', 'Land'])")
require.NoError(t, err)

var row Composite[[]string]
require.NoError(t, db.QueryRow("SELECT environments FROM all_enums").Scan(&row))
require.ElementsMatch(t, []string{"Air", "Land", "Sea"}, row.Get())
}

func TestHugeInt(t *testing.T) {
Expand Down
25 changes: 25 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ func scan(vector C.duckdb_vector, rowIdx C.idx_t) (any, error) {
return hugeIntToNative(hi), nil
case C.DUCKDB_TYPE_VARCHAR:
return scanString(vector, rowIdx), nil
case C.DUCKDB_TYPE_ENUM:
return scanENUM(ty, vector, rowIdx)
case C.DUCKDB_TYPE_BLOB:
return scanBlob(vector, rowIdx), nil
case C.DUCKDB_TYPE_DECIMAL:
Expand Down Expand Up @@ -211,6 +213,8 @@ func (r *rows) ColumnTypeScanType(index int) reflect.Type {
return reflect.TypeOf(big.NewInt(0))
case C.DUCKDB_TYPE_VARCHAR:
return reflect.TypeOf("")
case C.DUCKDB_TYPE_ENUM:
return reflect.TypeOf("")
case C.DUCKDB_TYPE_BLOB:
return reflect.TypeOf([]byte{})
case C.DUCKDB_TYPE_DECIMAL:
Expand Down Expand Up @@ -401,6 +405,27 @@ func scanInterval(vector C.duckdb_vector, rowIdx C.idx_t) (Interval, error) {
return data, nil
}

func scanENUM(ty C.duckdb_logical_type, vector C.duckdb_vector, rowIdx C.idx_t) (string, error) {
var idx uint64
internalType := C.duckdb_enum_internal_type(ty)
switch internalType {
case C.DUCKDB_TYPE_UTINYINT:
idx = uint64(get[uint8](vector, rowIdx))
case C.DUCKDB_TYPE_USMALLINT:
idx = uint64(get[uint16](vector, rowIdx))
case C.DUCKDB_TYPE_UINTEGER:
idx = uint64(get[uint32](vector, rowIdx))
case C.DUCKDB_TYPE_UBIGINT:
idx = get[uint64](vector, rowIdx)
default:
return "", errInvalidType
}

val := C.duckdb_enum_dictionary_value(ty, (C.idx_t)(idx))
defer C.duckdb_free(unsafe.Pointer(val))
return C.GoString(val), nil
}

var (
errInvalidType = errors.New("invalid data type")
errMissingKeyOrValue = errors.New("missing key and/or value for map item")
Expand Down

0 comments on commit 52995e1

Please sign in to comment.