Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
taniabogatsch committed Nov 5, 2024
1 parent 58273a9 commit 3c9d295
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion type_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var testPrimitiveSQLValues = map[Type]testTypeValues{
TYPE_TIMESTAMP: {input: `TIMESTAMP '1992-09-20 11:30:00.123456'`, output: `1992-09-20 11:30:00.123456`},
TYPE_DATE: {input: `DATE '1992-09-20 11:30:00.123456789'`, output: `1992-09-20`},
TYPE_TIME: {input: `TIME '1992-09-20 11:30:00.123456'`, output: `11:30:00.123456`},
TYPE_TIME_TZ: {input: `TIMETZ '1992-09-20 11:30:00.123456+06'`, output: `05:30:00.123456+00`},
TYPE_INTERVAL: {input: `INTERVAL 1 YEAR`, output: `1 year`},
TYPE_HUGEINT: {input: `44::HUGEINT`, output: `44`},
TYPE_VARCHAR: {input: `'hello world'::VARCHAR`, output: `hello world`},
Expand All @@ -40,6 +39,7 @@ var testPrimitiveSQLValues = map[Type]testTypeValues{
TYPE_TIMESTAMP_MS: {input: `TIMESTAMP_MS '1992-09-20 11:30:00.123'`, output: `1992-09-20 11:30:00.123`},
TYPE_TIMESTAMP_NS: {input: `TIMESTAMP_NS '1992-09-20 11:30:00.123456789'`, output: `1992-09-20 11:30:00.123456789`},
TYPE_UUID: {input: `uuid()`, output: ``},
TYPE_TIME_TZ: {input: `TIMETZ '1992-09-20 11:30:00.123456+06'`, output: `05:30:00.123456+00`},
TYPE_TIMESTAMP_TZ: {input: `TIMESTAMPTZ '1992-09-20 11:30:00.123456'`, output: `1992-09-20 11:30:00.123456+00`},
}

Expand Down
17 changes: 12 additions & 5 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type testTypesRow struct {
List_col Composite[[]int32]
Struct_col Composite[testTypesStruct]
Map_col Map
Time_tz_col time.Time
Timestamp_tz_col time.Time
}

Expand Down Expand Up @@ -81,6 +82,7 @@ const testTypesTableSQL = `CREATE TABLE test (
List_col INTEGER[],
Struct_col STRUCT(A INTEGER, B VARCHAR),
Map_col MAP(INTEGER, VARCHAR),
Time_tz_col TIMETZ,
Timestamp_tz_col TIMESTAMPTZ
)`

Expand All @@ -89,6 +91,7 @@ func (r *testTypesRow) toUTC() {
r.Timestamp_s_col = r.Timestamp_s_col.UTC()
r.Timestamp_ms_col = r.Timestamp_ms_col.UTC()
r.Timestamp_ns_col = r.Timestamp_ns_col.UTC()
r.Time_tz_col = r.Time_tz_col.UTC()
r.Timestamp_tz_col = r.Timestamp_tz_col.UTC()
}

Expand All @@ -101,9 +104,10 @@ func testTypesGenerateRow[T require.TestingT](t T, i int) testTypesRow {
ts, err := time.ParseInLocation(longForm, "2016-01-17 20:04:05 IST", IST)
require.NoError(t, err)

// Get the DATE and TIME column values.
// Get the DATE, TIME, and TIMETZ column values.
dateUTC := time.Date(1992, time.September, 20, 0, 0, 0, 0, time.UTC)
timeUTC := time.Date(1, time.January, 1, 11, 42, 7, 0, time.UTC)
timeTZ := time.Date(1, time.January, 1, 11, 42, 7, 0, IST)

var buffer bytes.Buffer
for j := 0; j < i; j++ {
Expand Down Expand Up @@ -147,6 +151,7 @@ func testTypesGenerateRow[T require.TestingT](t T, i int) testTypesRow {
listCol,
structCol,
mapCol,
timeTZ,
ts,
}
}
Expand Down Expand Up @@ -195,6 +200,7 @@ func testTypes[T require.TestingT](t T, c *Connector, a *Appender, expectedRows
r.List_col.Get(),
r.Struct_col.Get(),
r.Map_col,
r.Time_tz_col,
r.Timestamp_tz_col)
require.NoError(t, err)
}
Expand Down Expand Up @@ -233,6 +239,7 @@ func testTypes[T require.TestingT](t T, c *Connector, a *Appender, expectedRows
&r.List_col,
&r.Struct_col,
&r.Map_col,
&r.Time_tz_col,
&r.Timestamp_tz_col)
require.NoError(t, err)
actualRows = append(actualRows, r)
Expand Down Expand Up @@ -291,7 +298,7 @@ func TestDecimal(t *testing.T) {

t.Run("SELECT all possible DECIMAL widths", func(t *testing.T) {
for i := 1; i <= 38; i++ {
r := db.QueryRow(fmt.Sprintf("SELECT 0::DECIMAL(%d, 1)", i))
r := db.QueryRow(fmt.Sprintf(`SELECT 0::DECIMAL(%d, 1)`, i))
var actual Decimal
require.NoError(t, r.Scan(&actual))
expected := Decimal{Width: uint8(i), Value: big.NewInt(0), Scale: 1}
Expand Down Expand Up @@ -379,9 +386,9 @@ func TestBlob(t *testing.T) {
db := openDB(t)

// Scan a hexadecimal value.
var bytes []byte
require.NoError(t, db.QueryRow("SELECT '\\xAA'::BLOB").Scan(&bytes))
require.Equal(t, []byte{0xAA}, bytes)
var b []byte
require.NoError(t, db.QueryRow("SELECT '\\xAA'::BLOB").Scan(&b))
require.Equal(t, []byte{0xAA}, b)
require.NoError(t, db.Close())
}

Expand Down
1 change: 1 addition & 0 deletions vector_getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func getTimeTZ(ti C.duckdb_time_tz) time.Time {
hour := int(timeTZ.time.hour)
minute := int(timeTZ.time.min)
sec := int(timeTZ.time.sec)
// TIMETZ has microsecond precision.
nanos := int(timeTZ.time.micros) * 1000
loc := time.FixedZone("", int(timeTZ.offset))
return time.Date(1, time.January, 1, hour, minute, sec, nanos, loc).UTC()
Expand Down
5 changes: 4 additions & 1 deletion vector_setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ func setTime[S any](vec *vector, rowIdx C.idx_t, val S) error {
return castError(reflect.TypeOf(val).String(), reflect.TypeOf(ti).String())
}

base := time.Date(1970, time.January, 1, ti.Hour(), ti.Minute(), ti.Second(), ti.Nanosecond(), ti.Location())
// DuckDB stores time as microseconds since 00:00:00.
ti = ti.UTC()
base := time.Date(1970, time.January, 1, ti.Hour(), ti.Minute(), ti.Second(), ti.Nanosecond(), time.UTC)
ticks := base.UnixMicro()

switch vec.Type {
Expand All @@ -155,6 +157,7 @@ func setTime[S any](vec *vector, rowIdx C.idx_t, val S) error {
duckTime.micros = C.int64_t(ticks)
setPrimitive(vec, rowIdx, duckTime)
case TYPE_TIME_TZ:
// The UTC offset is 0.
duckTimeTZ := C.duckdb_create_time_tz(C.int64_t(ticks), 0)
setPrimitive(vec, rowIdx, duckTimeTZ)
}
Expand Down

0 comments on commit 3c9d295

Please sign in to comment.