Skip to content

feat: add support for time/time64 #1596

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
60 changes: 50 additions & 10 deletions TYPES.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.0
toolchain go1.24.1

require (
github.com/ClickHouse/ch-go v0.66.1
github.com/ClickHouse/ch-go v0.66.2-0.20250716011122-97e525b8cb83
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to bring changes from ClickHouse/ch-go#1074

this is using commit id, a new tag is not created.

github.com/andybalholm/brotli v1.2.0
github.com/docker/docker v28.3.2+incompatible
github.com/docker/go-units v0.5.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8af
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/ClickHouse/ch-go v0.66.1 h1:LQHFslfVYZsISOY0dnOYOXGkOUvpv376CCm8g7W74A4=
github.com/ClickHouse/ch-go v0.66.1/go.mod h1:NEYcg3aOFv2EmTJfo4m2WF7sHB/YFbLUuIWv9iq76xY=
github.com/ClickHouse/ch-go v0.66.2-0.20250716011122-97e525b8cb83 h1:9EI1ln/5ilLTH7ExWFZtaHrQ+AXenyJoloSxDyycnuw=
github.com/ClickHouse/ch-go v0.66.2-0.20250716011122-97e525b8cb83/go.mod h1:LaCKZZLe4gr9VFJk+mPgPJhBzt0R5qEaogaLhK8faO8=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
Expand Down Expand Up @@ -173,8 +173,8 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMey
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU=
go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/WgbsdpcPoZE=
go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E=
go.opentelemetry.io/otel/sdk v1.36.0 h1:b6SYIuLRs88ztox4EyrvRti80uXIFy+Sqzoh9kFULbs=
go.opentelemetry.io/otel/sdk v1.36.0/go.mod h1:+lC+mTgD+MUWfjJubi2vvXWcVxyr9rmlshZni72pXeY=
go.opentelemetry.io/otel/sdk v1.37.0 h1:ItB0QUqnjesGRvNcmAcU0LyvkVyGJ2xftD29bWdDvKI=
go.opentelemetry.io/otel/sdk v1.37.0/go.mod h1:VredYzxUvuo2q3WRcDnKDjbdvmO0sCzOvVAiY+yUkAg=
go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4=
go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0=
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
Expand Down
348 changes: 348 additions & 0 deletions lib/column/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,348 @@
// Licensed to ClickHouse, Inc. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. ClickHouse, Inc. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package column

import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"strconv"
"strings"
"time"

"github.com/ClickHouse/ch-go/proto"
"github.com/ClickHouse/clickhouse-go/v2/lib/timezone"
)

const defaultTimeFormat = "15:04:05"

type Time struct {
chType Type
timezone *time.Location
name string
col proto.ColTime
}

func (col *Time) Reset() {
col.col.Reset()
}

func (col *Time) Name() string {
return col.name
}

func (col *Time) parse(t Type, tz *time.Location) (_ Interface, err error) {
col.chType = t
if strings.HasPrefix(string(t), "Time('") {
timezoneName := strings.TrimSuffix(strings.TrimPrefix(string(t), "Time('"), "')")
timezone, err := timezone.Load(timezoneName)
if err != nil {
return nil, err
}
col.timezone = timezone
return col, nil
}
if string(t) == "Time" {
col.timezone = tz
return col, nil
}
return nil, &UnsupportedColumnTypeError{t: t}
}

func (col *Time) Type() Type {
return col.chType
}

func (col *Time) ScanType() reflect.Type {
return scanTypeTime
}

func (col *Time) Rows() int {
return col.col.Rows()
}

func (col *Time) Row(i int, ptr bool) any {
value := col.row(i)
if ptr {
return &value
}
return value
}

func (col *Time) ScanRow(dest any, row int) error {
switch d := dest.(type) {
case *time.Time:
*d = col.row(row)
case **time.Time:
*d = new(time.Time)
**d = col.row(row)
case *int64:
t := col.row(row)
*d = int64(t.Hour()*3600 + t.Minute()*60 + t.Second())
case **int64:
*d = new(int64)
t := col.row(row)
**d = int64(t.Hour()*3600 + t.Minute()*60 + t.Second())
case *sql.NullTime:
return d.Scan(col.row(row))
case *string:
*d = col.row(row).Format(defaultTimeFormat)
case **string:
*d = new(string)
**d = col.row(row).Format(defaultTimeFormat)
default:
if scan, ok := dest.(sql.Scanner); ok {
return scan.Scan(col.row(row))
}
return &ColumnConverterError{
Op: "ScanRow",
To: fmt.Sprintf("%T", dest),
From: "Time",
}
}
return nil
}

func (col *Time) Append(v any) (nulls []uint8, err error) {
switch v := v.(type) {
case []int64:
nulls = make([]uint8, len(v))
for i := range v {
seconds := v[i]
hours := seconds / 3600
minutes := (seconds % 3600) / 60
secs := seconds % 60
col.col.Append(proto.FromTime32(time.Date(1970, 1, 1, int(hours), int(minutes), int(secs), 0, time.UTC)))
}
case []*int64:
nulls = make([]uint8, len(v))
for i := range v {
switch {
case v[i] != nil:
seconds := *v[i]
hours := seconds / 3600
minutes := (seconds % 3600) / 60
secs := seconds % 60
col.col.Append(proto.FromTime32(time.Date(1970, 1, 1, int(hours), int(minutes), int(secs), 0, time.UTC)))
default:
col.col.Append(proto.FromTime32(time.Time{}))
nulls[i] = 1
}
}
case []time.Time:
nulls = make([]uint8, len(v))
for i := range v {
col.col.Append(proto.FromTime32(v[i]))
}
case []*time.Time:
nulls = make([]uint8, len(v))
for i := range v {
switch {
case v[i] != nil:
col.col.Append(proto.FromTime32(*v[i]))
default:
col.col.Append(proto.FromTime32(time.Time{}))
nulls[i] = 1
}
}
case []sql.NullTime:
nulls = make([]uint8, len(v))
for i := range v {
col.AppendRow(v[i])
}
case []*sql.NullTime:
nulls = make([]uint8, len(v))
for i := range v {
if v[i] == nil {
nulls[i] = 1
}
col.AppendRow(v[i])
}
case []string:
nulls = make([]uint8, len(v))
for i := range v {
value, err := col.parseTime(v[i])
if err != nil {
return nil, err
}
col.col.Append(proto.FromTime32(value))
}
default:
if valuer, ok := v.(driver.Valuer); ok {
val, err := valuer.Value()
if err != nil {
return nil, &ColumnConverterError{
Op: "Append",
To: "Time",
From: fmt.Sprintf("%T", v),
Hint: "could not get driver.Valuer value",
}
}
return col.Append(val)
}
return nil, &ColumnConverterError{
Op: "Append",
To: "Time",
From: fmt.Sprintf("%T", v),
}
}
return
}

func (col *Time) AppendRow(v any) error {
switch v := v.(type) {
case int64:
seconds := v
hours := seconds / 3600
minutes := (seconds % 3600) / 60
secs := seconds % 60
col.col.Append(proto.FromTime32(time.Date(1970, 1, 1, int(hours), int(minutes), int(secs), 0, time.UTC)))
case *int64:
switch {
case v != nil:
seconds := *v
hours := seconds / 3600
minutes := (seconds % 3600) / 60
secs := seconds % 60
col.col.Append(proto.FromTime32(time.Date(1970, 1, 1, int(hours), int(minutes), int(secs), 0, time.UTC)))
default:
col.col.Append(proto.FromTime32(time.Time{}))
}
case time.Time:
col.col.Append(proto.FromTime32(v))
case *time.Time:
switch {
case v != nil:
col.col.Append(proto.FromTime32(*v))
default:
col.col.Append(proto.FromTime32(time.Time{}))
}
case sql.NullTime:
if v.Valid {
col.col.Append(proto.FromTime32(v.Time))
} else {
col.col.Append(proto.FromTime32(time.Time{}))
}
case *sql.NullTime:
switch {
case v != nil && v.Valid:
col.col.Append(proto.FromTime32(v.Time))
default:
col.col.Append(proto.FromTime32(time.Time{}))
}
case string:
value, err := col.parseTime(v)
if err != nil {
return err
}
col.col.Append(proto.FromTime32(value))
case *string:
switch {
case v != nil:
value, err := col.parseTime(*v)
if err != nil {
return err
}
col.col.Append(proto.FromTime32(value))
default:
col.col.Append(proto.FromTime32(time.Time{}))
}
default:
if valuer, ok := v.(driver.Valuer); ok {
val, err := valuer.Value()
if err != nil {
return &ColumnConverterError{
Op: "AppendRow",
To: "Time",
From: fmt.Sprintf("%T", v),
Hint: "could not get driver.Valuer value",
}
}
return col.AppendRow(val)
}
return &ColumnConverterError{
Op: "AppendRow",
To: "Time",
From: fmt.Sprintf("%T", v),
}
}
return nil
}

func (col *Time) Decode(reader *proto.Reader, rows int) error {
return col.col.DecodeColumn(reader, rows)
}

func (col *Time) Encode(buffer *proto.Buffer) {
col.col.EncodeColumn(buffer)
}

func (col *Time) row(i int) time.Time {
return col.col.Row(i).ToTime32()
}

func (col *Time) parseTime(value string) (tv time.Time, err error) {
value = strings.TrimSpace(value)
if value == "" {
return time.Time{}, nil
}

formats := []string{
"15:04:05",
"15:04",
"15:04:05.999",
"15:04:05.999999",
"15:04:05.999999999",
"3:04:05 PM",
"3:04 PM",
"15:04:05 -07:00",
"15:04:05.999 -07:00",
}

for _, format := range formats {
if tv, err = time.Parse(format, value); err == nil {
timezone := time.UTC
if col.timezone != nil {
timezone = col.timezone
}
return time.Date(1970, 1, 1, tv.Hour(), tv.Minute(), tv.Second(), tv.Nanosecond(), timezone), nil
}
}

if seconds, err := strconv.ParseInt(value, 10, 64); err == nil {
hours := seconds / 3600
minutes := (seconds % 3600) / 60
secs := seconds % 60
timezone := time.UTC
if col.timezone != nil {
timezone = col.timezone
}
return time.Date(1970, 1, 1, int(hours), int(minutes), int(secs), 0, timezone), nil
}

return time.Time{}, fmt.Errorf("cannot parse time value: %s", value)
}

func (col *Time) WriteStatePrefix(buffer *proto.Buffer) error {
return nil
}

func (col *Time) ReadStatePrefix(reader *proto.Reader) error {
return nil
}
Loading