Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic setters to void casting values to any #276

Merged
merged 8 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions appender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"math/big"
"math/rand"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -542,6 +543,42 @@ func TestAppenderUUID(t *testing.T) {
cleanupAppender(t, c, con, a)
}

func newAppenderHugeIntTest[T numericType](val T, c *Connector, a *Appender) func(t *testing.T) {
return func(t *testing.T) {
typeName := reflect.TypeOf(val).String()
require.NoError(t, a.AppendRow(val, typeName))
require.NoError(t, a.Flush())

// Verify results.
row := sql.OpenDB(c).QueryRowContext(context.Background(), fmt.Sprintf("SELECT val FROM test WHERE id=='%s'", typeName))

var res *big.Int
require.NoError(t, row.Scan(&res))
require.Equal(t, big.NewInt(int64(val)), res)
}
}

func TestAppenderHugeInt(t *testing.T) {
t.Parallel()
c, con, a := prepareAppender(t, `CREATE TABLE test (val HUGEINT, id VARCHAR)`)
tests := map[string]func(t *testing.T){
"int8": newAppenderHugeIntTest[int8](1, c, a),
"int16": newAppenderHugeIntTest[int16](2, c, a),
"int32": newAppenderHugeIntTest[int32](3, c, a),
"int64": newAppenderHugeIntTest[int64](4, c, a),
"uint8": newAppenderHugeIntTest[uint8](5, c, a),
"uint16": newAppenderHugeIntTest[uint16](6, c, a),
"uint32": newAppenderHugeIntTest[uint32](7, c, a),
"uint64": newAppenderHugeIntTest[uint64](8, c, a),
"float32": newAppenderHugeIntTest[float32](9, c, a),
"float64": newAppenderHugeIntTest[float64](10, c, a),
}
for name, test := range tests {
t.Run(name, test)
}
cleanupAppender(t, c, con, a)
}

func TestAppenderTsNs(t *testing.T) {
t.Parallel()
c, con, a := prepareAppender(t, `CREATE TABLE test (timestamp TIMESTAMP_NS)`)
Expand Down
23 changes: 11 additions & 12 deletions data_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,18 @@ func (chunk *DataChunk) SetValue(colIdx int, rowIdx int, val any) error {
}
column := &chunk.columns[colIdx]

// 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.
// FIXME: Can we improve efficiency here? We are casting back-and-forth to any A LOT.
// FIXME: Maybe we can make columnar insertions unsafe, i.e., we always assume a correct type.
v, err := column.tryCast(val)
if err != nil {
return addIndexToError(err, colIdx)
}

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

// SetValue writes a single value to a column in a data chunk.
// Note that this requires casting the type for each invocation.
// NOTE: Custom ENUM types must be passed as string.
func SetChunkValue[T any](chunk DataChunk, colIdx int, rowIdx int, val T) error {
taniabogatsch marked this conversation as resolved.
Show resolved Hide resolved
if colIdx >= len(chunk.columns) {
return getError(errAPI, columnCountError(colIdx, len(chunk.columns)))
}
return setVectorVal(&chunk.columns[colIdx], C.idx_t(rowIdx), val)
}

func (chunk *DataChunk) initFromTypes(ptr unsafe.Pointer, types []C.duckdb_logical_type, writable bool) error {
Expand Down
2 changes: 2 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ func TestErrAPISetValue(t *testing.T) {
var chunk DataChunk
err := chunk.SetValue(1, 42, "hello")
testError(t, err, errAPI.Error(), columnCountErrMsg)
err = SetChunkValue(chunk, 1, 42, "hello")
testError(t, err, errAPI.Error(), columnCountErrMsg)
}

func TestDuckDBErrors(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions type_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type baseTypeInfo struct {
structEntries []StructEntry
decimalWidth uint8
decimalScale uint8
internalType Type // This is the internal type for enum and decimal values
}

type vectorTypeInfo struct {
Expand Down
Loading