Skip to content

Commit 7e2272c

Browse files
authored
Adds the ability to Append a zero valued time.Time (#1228)
* Bump ch-go to v0.61.5 * Adds the ability to Append a zero valued time.Time
1 parent 45dbc8f commit 7e2272c

8 files changed

+94
-34
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/ClickHouse/clickhouse-go/v2
33
go 1.21
44

55
require (
6-
github.com/ClickHouse/ch-go v0.61.3
6+
github.com/ClickHouse/ch-go v0.61.5
77
github.com/ClickHouse/clickhouse-go v1.5.4
88
github.com/andybalholm/brotli v1.1.0
99
github.com/docker/docker v25.0.3+incompatible

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9
44
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
55
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
66
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
7-
github.com/ClickHouse/ch-go v0.61.3 h1:MmBwUhXrAOBZK7n/sWBzq6FdIQ01cuF2SaaO8KlDRzI=
8-
github.com/ClickHouse/ch-go v0.61.3/go.mod h1:1PqXjMz/7S1ZUaKvwPA3i35W2bz2mAMFeCi6DIXgGwQ=
7+
github.com/ClickHouse/ch-go v0.61.5 h1:zwR8QbYI0tsMiEcze/uIMK+Tz1D3XZXLdNrlaOpeEI4=
8+
github.com/ClickHouse/ch-go v0.61.5/go.mod h1:s1LJW/F/LcFs5HJnuogFMta50kKDO0lf9zzfrbl0RQg=
99
github.com/ClickHouse/clickhouse-go v1.5.4 h1:cKjXeYLNWVJIx2J1K6H2CqyRmfwVJVY1OV1coaaFcI0=
1010
github.com/ClickHouse/clickhouse-go v1.5.4/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
1111
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
@@ -179,8 +179,8 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMey
179179
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU=
180180
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
181181
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
182-
go.opentelemetry.io/otel/sdk v1.23.1 h1:O7JmZw0h76if63LQdsBMKQDWNb5oEcOThG9IrxscV+E=
183-
go.opentelemetry.io/otel/sdk v1.23.1/go.mod h1:LzdEVR5am1uKOOwfBWFef2DCi1nu3SA8XQxx2IerWFk=
182+
go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw=
183+
go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg=
184184
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
185185
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
186186
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=

lib/column/date_helpers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
const secInDay = 24 * 60 * 60
2626

2727
func dateOverflow(min, max, v time.Time, format string) error {
28-
if v.Before(min) || v.After(max) {
28+
if !v.IsZero() && (v.Before(min) || v.After(max)) {
2929
return &DateOverflowError{
3030
Min: min,
3131
Max: max,

lib/column/date_helpers_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package column
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestDateOverflow(t *testing.T) {
9+
t.Parallel()
10+
zeroTime := time.Time{}
11+
tests := []struct {
12+
v time.Time
13+
name string
14+
err bool
15+
}{
16+
{
17+
name: "valid date",
18+
v: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
19+
err: false,
20+
},
21+
{
22+
name: "before min date",
23+
v: minDate.Add(-time.Second),
24+
err: true,
25+
},
26+
{
27+
name: "after max date",
28+
v: maxDate.Add(time.Second),
29+
err: true,
30+
},
31+
{
32+
name: "zero value date",
33+
v: zeroTime,
34+
err: false,
35+
},
36+
{
37+
name: "non-zero value equal to zero date",
38+
v: time.UnixMilli(zeroTime.UnixMilli()),
39+
err: false,
40+
},
41+
}
42+
43+
for _, test := range tests {
44+
test := test
45+
t.Run(test.name, func(t *testing.T) {
46+
t.Parallel()
47+
err := dateOverflow(minDateTime, maxDateTime, test.v, defaultDateFormatNoZone)
48+
if (err != nil) != test.err {
49+
t.Errorf("expected error: %v, got: %v", test.err, err)
50+
}
51+
})
52+
}
53+
}

tests/std/date32_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import (
2121
"context"
2222
"database/sql"
2323
"fmt"
24-
"github.com/ClickHouse/clickhouse-go/v2"
25-
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
26-
"github.com/stretchr/testify/require"
2724
"strconv"
2825
"testing"
2926
"time"
3027

28+
"github.com/ClickHouse/clickhouse-go/v2"
29+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
3130
"github.com/stretchr/testify/assert"
31+
"github.com/stretchr/testify/require"
3232
)
3333

3434
func TestStdDate32(t *testing.T) {
@@ -78,7 +78,7 @@ func TestStdDate32(t *testing.T) {
7878
)
7979
_, err = batch.Exec(uint8(1), date1, &date2, []time.Time{date2}, []*time.Time{&date2, nil, &date1}, sql.NullTime{Time: time.Time{}, Valid: false}, sql.NullTime{Time: date1, Valid: true})
8080
require.NoError(t, err)
81-
_, err = batch.Exec(uint8(2), date2, nil, []time.Time{date1}, []*time.Time{nil, nil, &date2}, sql.NullTime{Time: time.Time{}, Valid: false}, sql.NullTime{Time: date2, Valid: true})
81+
_, err = batch.Exec(uint8(2), time.Time{}, nil, []time.Time{date1}, []*time.Time{nil, nil, &date2}, sql.NullTime{Time: time.Time{}, Valid: false}, sql.NullTime{Time: date2, Valid: true})
8282
require.NoError(t, err)
8383
require.NoError(t, scope.Commit())
8484
var (
@@ -110,7 +110,7 @@ func TestStdDate32(t *testing.T) {
110110
&result1.Col5,
111111
&result1.Col6,
112112
))
113-
require.Equal(t, date2, result2.Col1)
113+
require.Equal(t, time.Unix(0, 0).UTC(), result2.Col1)
114114
require.Equal(t, "UTC", result2.Col1.Location().String())
115115
require.Nil(t, result2.Col2)
116116
assert.Equal(t, []time.Time{date1}, result2.Col3)

tests/std/date_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import (
2121
"context"
2222
"database/sql"
2323
"fmt"
24-
"github.com/ClickHouse/clickhouse-go/v2"
25-
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
26-
"github.com/stretchr/testify/require"
2724
"strconv"
2825
"testing"
2926
"time"
3027

28+
"github.com/ClickHouse/clickhouse-go/v2"
29+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
3130
"github.com/stretchr/testify/assert"
31+
"github.com/stretchr/testify/require"
3232
)
3333

3434
func TestStdDate(t *testing.T) {
@@ -72,7 +72,7 @@ func TestStdDate(t *testing.T) {
7272
require.NoError(t, err)
7373
_, err = batch.Exec(uint8(1), date, &date, []time.Time{date}, []*time.Time{&date, nil, &date}, sql.NullTime{Time: date, Valid: true}, sql.NullTime{Time: time.Time{}, Valid: false})
7474
require.NoError(t, err)
75-
_, err = batch.Exec(uint8(2), date, nil, []time.Time{date}, []*time.Time{nil, nil, &date}, sql.NullTime{Time: date, Valid: true}, sql.NullTime{Time: time.Time{}, Valid: false})
75+
_, err = batch.Exec(uint8(2), time.Time{}, nil, []time.Time{date}, []*time.Time{nil, nil, &date}, sql.NullTime{Time: date, Valid: true}, sql.NullTime{Time: time.Time{}, Valid: false})
7676
require.NoError(t, err)
7777
require.NoError(t, scope.Commit())
7878
var (
@@ -104,7 +104,7 @@ func TestStdDate(t *testing.T) {
104104
&result2.Col5,
105105
&result2.Col6,
106106
))
107-
require.Equal(t, date, result2.Col1)
107+
require.Equal(t, time.Unix(0, 0).UTC(), result2.Col1)
108108
assert.Equal(t, "UTC", result2.Col1.Location().String())
109109
require.Nil(t, result2.Col2)
110110
assert.Equal(t, []time.Time{date}, result2.Col3)

tests/std/datetime64_test.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ package std
2020
import (
2121
"database/sql"
2222
"fmt"
23-
"github.com/ClickHouse/clickhouse-go/v2"
24-
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
25-
"github.com/stretchr/testify/require"
2623
"strconv"
2724
"testing"
2825
"time"
2926

27+
"github.com/ClickHouse/clickhouse-go/v2"
28+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
3029
"github.com/stretchr/testify/assert"
30+
"github.com/stretchr/testify/require"
3131
)
3232

3333
func TestStdDateTime64(t *testing.T) {
@@ -53,6 +53,7 @@ func TestStdDateTime64(t *testing.T) {
5353
, Col7 DateTime64(0, 'Europe/London')
5454
, Col8 Nullable(DateTime64(3, 'Europe/Moscow'))
5555
, Col9 DateTime64(9)
56+
, Col10 DateTime64(9)
5657
) Engine MergeTree() ORDER BY tuple()
5758
`
5859
defer func() {
@@ -81,21 +82,23 @@ func TestStdDateTime64(t *testing.T) {
8182
sql.NullTime{Time: datetime3, Valid: true},
8283
sql.NullTime{Time: time.Time{}, Valid: false},
8384
expectedMinDateTime,
85+
time.Time{},
8486
)
8587
require.NoError(t, err)
8688
require.NoError(t, scope.Commit())
8789
var (
88-
col1 time.Time
89-
col2 time.Time
90-
col3 time.Time
91-
col4 *time.Time
92-
col5 []time.Time
93-
col6 []*time.Time
94-
col7 sql.NullTime
95-
col8 sql.NullTime
96-
col9 time.Time
90+
col1 time.Time
91+
col2 time.Time
92+
col3 time.Time
93+
col4 *time.Time
94+
col5 []time.Time
95+
col6 []*time.Time
96+
col7 sql.NullTime
97+
col8 sql.NullTime
98+
col9 time.Time
99+
col10 time.Time
97100
)
98-
require.NoError(t, conn.QueryRow("SELECT * FROM test_datetime64").Scan(&col1, &col2, &col3, &col4, &col5, &col6, &col7, &col8, &col9))
101+
require.NoError(t, conn.QueryRow("SELECT * FROM test_datetime64").Scan(&col1, &col2, &col3, &col4, &col5, &col6, &col7, &col8, &col9, &col10))
99102
assert.Equal(t, datetime1.In(time.UTC), col1)
100103
assert.Equal(t, datetime2.UnixNano(), col2.UnixNano())
101104
assert.Equal(t, datetime3.UnixNano(), col3.UnixNano())
@@ -113,6 +116,7 @@ func TestStdDateTime64(t *testing.T) {
113116
require.Equal(t, sql.NullTime{Time: datetime3.In(col7.Time.Location()), Valid: true}, col7)
114117
require.Equal(t, sql.NullTime{Time: time.Time{}, Valid: false}, col8)
115118
require.Equal(t, time.Date(1900, 01, 01, 0, 0, 0, 0, time.UTC), col9)
119+
require.Equal(t, time.Unix(0, 0).UTC(), col10)
116120
})
117121
}
118122
}

tests/std/datetime_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ package std
2020
import (
2121
"database/sql"
2222
"fmt"
23-
"github.com/ClickHouse/clickhouse-go/v2"
24-
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
25-
"github.com/stretchr/testify/require"
2623
"strconv"
2724
"testing"
2825
"time"
2926

27+
"github.com/ClickHouse/clickhouse-go/v2"
28+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
3029
"github.com/stretchr/testify/assert"
30+
"github.com/stretchr/testify/require"
3131
)
3232

3333
func TestStdDateTime(t *testing.T) {
@@ -48,6 +48,7 @@ func TestStdDateTime(t *testing.T) {
4848
, Col6 Array(Nullable(DateTime('Europe/Moscow')))
4949
, Col7 DateTime
5050
, Col8 Nullable(DateTime)
51+
,Col9 DateTime
5152
) Engine MergeTree() ORDER BY tuple()`
5253
defer func() {
5354
conn.Exec("DROP TABLE test_datetime")
@@ -74,6 +75,7 @@ func TestStdDateTime(t *testing.T) {
7475
Time: time.Time{},
7576
Valid: false,
7677
},
78+
time.Time{},
7779
)
7880
require.NoError(t, err)
7981
require.NoError(t, scope.Commit())
@@ -86,8 +88,9 @@ func TestStdDateTime(t *testing.T) {
8688
col6 []*time.Time
8789
col7 sql.NullTime
8890
col8 sql.NullTime
91+
col9 time.Time
8992
)
90-
require.NoError(t, conn.QueryRow("SELECT * FROM test_datetime").Scan(&col1, &col2, &col3, &col4, &col5, &col6, &col7, &col8))
93+
require.NoError(t, conn.QueryRow("SELECT * FROM test_datetime").Scan(&col1, &col2, &col3, &col4, &col5, &col6, &col7, &col8, &col9))
9194
assert.Equal(t, datetime.In(time.UTC), col1)
9295
assert.Equal(t, datetime.Unix(), col2.Unix())
9396
assert.Equal(t, datetime.Unix(), col3.Unix())
@@ -120,7 +123,7 @@ func TestStdDateTime(t *testing.T) {
120123
Time: time.Time{},
121124
Valid: false,
122125
}, col8)
123-
126+
assert.Equal(t, time.Unix(0, 0).UTC(), col9)
124127
})
125128
}
126129
}

0 commit comments

Comments
 (0)