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 all 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
27 changes: 14 additions & 13 deletions data_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,28 @@ func (chunk *DataChunk) GetValue(colIdx int, rowIdx int) (any, error) {
return column.getFn(column, C.idx_t(rowIdx)), nil
}

// SetValue writes a single value to a column in a data chunk. Note that this requires casting the type for each invocation.
// 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 (chunk *DataChunk) SetValue(colIdx int, rowIdx int, val any) error {
if colIdx >= len(chunk.columns) {
return getError(errAPI, columnCountError(colIdx, len(chunk.columns)))
}
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)
}

// SetChunkValue writes a single value to a column in a data chunk.
// The difference with `chunk.SetValue` is that `SetChunkValue` does not
// require casting the value to `any` (implicitly).
// NOTE: Custom ENUM types must be passed as string.
func SetChunkValue[T any](chunk DataChunk, colIdx int, rowIdx int, val T) error {
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
2 changes: 2 additions & 0 deletions type_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type baseTypeInfo struct {
structEntries []StructEntry
decimalWidth uint8
decimalScale uint8
// The internal type for ENUM and DECIMAL values.
internalType Type
}

type vectorTypeInfo struct {
Expand Down
7 changes: 5 additions & 2 deletions types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duckdb

import (
"bytes"
"context"
"database/sql"
"fmt"
Expand Down Expand Up @@ -104,10 +105,12 @@ func testTypesGenerateRow[T require.TestingT](t T, i int) testTypesRow {
dateUTC := time.Date(1992, 9, 20, 0, 0, 0, 0, time.UTC)
timeUTC := time.Date(1970, 1, 1, 11, 42, 7, 0, time.UTC)

varcharCol := ""
var buffer bytes.Buffer
for j := 0; j < i; j++ {
varcharCol += "hello!"
buffer.WriteString("hello!")
}
varcharCol := buffer.String()

listCol := Composite[[]int32]{
[]int32{int32(i)},
}
Expand Down
Loading