Skip to content

Commit

Permalink
returned Datetime type for proton datetime and datetime64, implement …
Browse files Browse the repository at this point in the history
…Marshaler and Unmarshaler interface of json and yaml for types.Date and types.Datetime
  • Loading branch information
leo-cai-timeplus committed Jan 26, 2024
1 parent 47c96de commit a9750f6
Show file tree
Hide file tree
Showing 17 changed files with 307 additions and 136 deletions.
9 changes: 4 additions & 5 deletions lib/column/column_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions lib/column/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ func (dt *DateTime) Row(i int, ptr bool) interface{} {
func (dt *DateTime) ScanRow(dest interface{}, row int) error {
switch d := dest.(type) {
case *time.Time:
*d = dt.row(row)
*d = dt.row(row).Time
case **time.Time:
*d = new(time.Time)
**d = dt.row(row)
**d = dt.row(row).Time
case *types.Datetime:
*d = types.Datetime{dt.row(row)}
*d = dt.row(row)
case **types.Datetime:
*d = new(types.Datetime)
**d = types.Datetime{dt.row(row)}
**d = dt.row(row)
default:
return &ColumnConverterError{
Op: "ScanRow",
Expand Down Expand Up @@ -195,12 +195,12 @@ func (dt *DateTime) Encode(encoder *binary.Encoder) error {
return dt.values.Encode(encoder)
}

func (dt *DateTime) row(i int) time.Time {
func (dt *DateTime) row(i int) types.Datetime {
v := time.Unix(int64(dt.values[i]), 0)
if dt.timezone != nil {
v = v.In(dt.timezone)
}
return v
return types.Datetime{Time: v}
}

var _ Interface = (*DateTime)(nil)
12 changes: 6 additions & 6 deletions lib/column/datetime64.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func (dt *DateTime64) Row(i int, ptr bool) interface{} {
func (dt *DateTime64) ScanRow(dest interface{}, row int) error {
switch d := dest.(type) {
case *time.Time:
*d = dt.row(row)
*d = dt.row(row).Time
case **time.Time:
*d = new(time.Time)
**d = dt.row(row)
**d = dt.row(row).Time
case *types.Datetime:
*d = types.Datetime{dt.row(row)}
*d = dt.row(row)
case **types.Datetime:
*d = new(types.Datetime)
**d = types.Datetime{dt.row(row)}
**d = dt.row(row)
default:
return &ColumnConverterError{
Op: "ScanRow",
Expand Down Expand Up @@ -213,7 +213,7 @@ func (dt *DateTime64) Encode(encoder *binary.Encoder) error {
return dt.values.Encode(encoder)
}

func (dt *DateTime64) row(i int) time.Time {
func (dt *DateTime64) row(i int) types.Datetime {
var nano int64
if dt.precision < 19 {
nano = dt.values[i] * int64(math.Pow10(9-dt.precision))
Expand All @@ -226,7 +226,7 @@ func (dt *DateTime64) row(i int) time.Time {
if dt.timezone != nil {
time = time.In(dt.timezone)
}
return time
return types.Datetime{Time: time}
}

func (dt *DateTime64) timeToInt64(t time.Time) int64 {
Expand Down
15 changes: 8 additions & 7 deletions tests/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package tests

import (
"context"
"github.com/timeplus-io/proton-go-driver/v2/types"
"testing"
"time"

Expand Down Expand Up @@ -56,14 +57,14 @@ func TestArray(t *testing.T) {
if err := conn.Exec(ctx, ddl); assert.NoError(t, err) {
if batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_array (* except _tp_time)"); assert.NoError(t, err) {
var (
timestamp = time.Now().Truncate(time.Second)
timestamp = types.Datetime{Time: time.Now().Truncate(time.Second)}
col1Data = []string{"A", "b", "c"}
col2Data = [][]uint32{
{1, 2},
{3, 87},
{33, 3, 847},
}
col3Data = [][][]time.Time{
col3Data = [][][]types.Datetime{
{
{
timestamp,
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestArray(t *testing.T) {
var (
col1 []string
col2 [][]uint32
col3 [][][]time.Time
col3 [][][]types.Datetime
)
if err := rows.Scan(&col1, &col2, &col3); assert.NoError(t, err) {
assert.Equal(t, col1Data, col1)
Expand Down Expand Up @@ -143,14 +144,14 @@ func TestColumnarArray(t *testing.T) {
}()
if err := conn.Exec(ctx, ddl); assert.NoError(t, err) {
var (
timestamp = time.Now().Truncate(time.Second)
timestamp = types.Datetime{Time: time.Now().Truncate(time.Second)}
col1Data = []string{"A", "b", "c"}
col2Data = [][]uint32{
{1, 2},
{3, 87},
{33, 3, 847},
}
col3Data = [][][]time.Time{
col3Data = [][][]types.Datetime{
{
{
timestamp,
Expand All @@ -174,7 +175,7 @@ func TestColumnarArray(t *testing.T) {

col1DataColArr [][]string
col2DataColArr [][][]uint32
col3DataColArr [][][][]time.Time
col3DataColArr [][][][]types.Datetime
)

for i := 0; i < 10; i++ {
Expand All @@ -199,7 +200,7 @@ func TestColumnarArray(t *testing.T) {
var (
col1 []string
col2 [][]uint32
col3 [][][]time.Time
col3 [][][]types.Datetime
)
if err := rows.Scan(&col1, &col2, &col3); assert.NoError(t, err) {
assert.Equal(t, col1Data, col1)
Expand Down
75 changes: 38 additions & 37 deletions tests/datetime64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package tests

import (
"context"
"github.com/timeplus-io/proton-go-driver/v2/types"
"testing"
"time"

Expand Down Expand Up @@ -63,26 +64,26 @@ func TestDateTime64(t *testing.T) {
if err := conn.Exec(ctx, ddl); assert.NoError(t, err) {
if batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_datetime64 (* except _tp_time)"); assert.NoError(t, err) {
var (
datetime1 = time.Now().Truncate(time.Millisecond)
datetime2 = time.Now().Truncate(time.Nanosecond)
datetime3 = time.Now().Truncate(time.Second)
datetime1 = types.Datetime{time.Now().Truncate(time.Millisecond)}
datetime2 = types.Datetime{time.Now().Truncate(time.Nanosecond)}
datetime3 = types.Datetime{time.Now().Truncate(time.Second)}
)
if err := batch.Append(
datetime1,
datetime2,
datetime3,
&datetime1,
[]time.Time{datetime1, datetime1},
[]*time.Time{&datetime3, nil, &datetime3},
[]types.Datetime{datetime1, datetime1},
[]*types.Datetime{&datetime3, nil, &datetime3},
); assert.NoError(t, err) {
if err := batch.Send(); assert.NoError(t, err) {
var (
col1 time.Time
col2 time.Time
col3 time.Time
col4 *time.Time
col5 []time.Time
col6 []*time.Time
col1 types.Datetime
col2 types.Datetime
col3 types.Datetime
col4 *types.Datetime
col5 []types.Datetime
col6 []*types.Datetime
)
if err := conn.QueryRow(ctx, "SELECT (* except _tp_time) FROM test_datetime64 WHERE _tp_time > earliest_ts() LIMIT 1").Scan(&col1, &col2, &col3, &col4, &col5, &col6); assert.NoError(t, err) {
assert.Equal(t, datetime1, col1)
Expand Down Expand Up @@ -154,12 +155,12 @@ func TestNullableDateTime64(t *testing.T) {
if err := batch.Append(datetime1, datetime1, datetime2, datetime2, datetime3, datetime3); assert.NoError(t, err) {
if err := batch.Send(); assert.NoError(t, err) {
var (
col1 time.Time
col1Null *time.Time
col2 time.Time
col2Null *time.Time
col3 time.Time
col3Null *time.Time
col1 types.Datetime
col1Null *types.Datetime
col2 types.Datetime
col2Null *types.Datetime
col3 types.Datetime
col3Null *types.Datetime
)
if err := conn.QueryRow(ctx, "SELECT (* except _tp_time) FROM test_datetime64 WHERE _tp_time > earliest_ts() LIMIT 1").Scan(
&col1, &col1Null,
Expand Down Expand Up @@ -188,12 +189,12 @@ func TestNullableDateTime64(t *testing.T) {
if err := batch.Append(datetime1, nil, datetime2, nil, datetime3, nil); assert.NoError(t, err) {
if err := batch.Send(); assert.NoError(t, err) {
var (
col1 time.Time
col1Null *time.Time
col2 time.Time
col2Null *time.Time
col3 time.Time
col3Null *time.Time
col1 types.Datetime
col1Null *types.Datetime
col2 types.Datetime
col2Null *types.Datetime
col3 types.Datetime
col3Null *types.Datetime
)
if err := conn.QueryRow(ctx, "SELECT (* except _tp_time) FROM test_datetime64 WHERE _tp_time > earliest_ts() LIMIT 1").Scan(
&col1, &col1Null,
Expand Down Expand Up @@ -259,14 +260,14 @@ func TestColumnarDateTime64(t *testing.T) {
if batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_datetime64 (* except _tp_time)"); assert.NoError(t, err) {
var (
id []uint64
col1Data []time.Time
col2Data []*time.Time
col3Data [][]time.Time
col4Data [][]*time.Time
col1Data []types.Datetime
col2Data []*types.Datetime
col3Data [][]types.Datetime
col4Data [][]*types.Datetime
)
var (
datetime1 = time.Now().Truncate(time.Millisecond)
datetime2 = time.Now().Truncate(time.Second)
datetime1 = types.Datetime{Time: time.Now().Truncate(time.Millisecond)}
datetime2 = types.Datetime{Time: time.Now().Truncate(time.Second)}
)
for i := 0; i < 1000; i++ {
id = append(id, uint64(i))
Expand All @@ -276,10 +277,10 @@ func TestColumnarDateTime64(t *testing.T) {
} else {
col2Data = append(col2Data, nil)
}
col3Data = append(col3Data, []time.Time{
col3Data = append(col3Data, []types.Datetime{
datetime1, datetime2, datetime1,
})
col4Data = append(col4Data, []*time.Time{
col4Data = append(col4Data, []*types.Datetime{
&datetime2, nil, &datetime1,
})
}
Expand All @@ -302,16 +303,16 @@ func TestColumnarDateTime64(t *testing.T) {
}
if assert.NoError(t, batch.Send()) {
var result struct {
Col1 time.Time
Col2 *time.Time
Col3 []time.Time
Col4 []*time.Time
Col1 types.Datetime
Col2 *types.Datetime
Col3 []types.Datetime
Col4 []*types.Datetime
}
if err := conn.QueryRow(ctx, "SELECT Col1, Col2, Col3, Col4 FROM test_datetime64 WHERE ID = $1 AND _tp_time > earliest_ts() LIMIT 1", 11).ScanStruct(&result); assert.NoError(t, err) {
if assert.Nil(t, result.Col2) {
assert.Equal(t, datetime1, result.Col1)
assert.Equal(t, []time.Time{datetime1, datetime2, datetime1}, result.Col3)
assert.Equal(t, []*time.Time{&datetime2, nil, &datetime1}, result.Col4)
assert.Equal(t, []types.Datetime{datetime1, datetime2, datetime1}, result.Col3)
assert.Equal(t, []*types.Datetime{&datetime2, nil, &datetime1}, result.Col4)
}
}
}
Expand Down
Loading

0 comments on commit a9750f6

Please sign in to comment.