-
Notifications
You must be signed in to change notification settings - Fork 609
Add support to time
and time64
datatypes
#1669
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
base: main
Are you sure you want to change the base?
Changes from all commits
621a570
8eba9b9
cf78e2a
aeadbc7
7c74e19
8b7ef7c
3b9562e
64d22b6
ee17487
82defd1
dc62229
f4524e1
47dc5e9
ef43736
f7bc653
b3bc01d
5325af3
bd2fa17
b5d2ab8
5d8c820
4b25e30
52eac54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ pipeline.auto.tfvars | |
*.tfvars | ||
|
||
.env | ||
go.work |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,8 @@ func (t Type) Column(name string, sc *ServerContext) (Interface, error) { | |
return &String{name: name, col: colStrProvider(name)}, nil | ||
case "SharedVariant": | ||
return &SharedVariant{name: name}, nil | ||
case "Time": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the changes here are inconsistent with what was generated. Was Also in |
||
return &Time{name: name, chType: "Time"}, nil | ||
} | ||
|
||
switch strType := string(t); { | ||
|
@@ -143,6 +145,8 @@ func (t Type) Column(name string, sc *ServerContext) (Interface, error) { | |
return (&DateTime64{name: name}).parse(t, sc.Timezone) | ||
case strings.HasPrefix(strType, "DateTime") && !strings.HasPrefix(strType, "DateTime64"): | ||
return (&DateTime{name: name}).parse(t, sc.Timezone) | ||
case strings.HasPrefix(strType, "Time64"): | ||
return (&Time64{name: name}).parse(t) | ||
} | ||
return nil, &UnsupportedColumnTypeError{ | ||
t: t, | ||
|
@@ -162,6 +166,8 @@ var ( | |
{{- range . }} | ||
_ Interface = (*{{ .ChType }})(nil) | ||
{{- end }} | ||
_ Interface = (*Time)(nil) | ||
_ Interface = (*Time64)(nil) | ||
) | ||
|
||
var ( | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package column | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
"time" | ||
|
||
"github.com/ClickHouse/ch-go/proto" | ||
) | ||
|
||
type Time struct { | ||
chType Type | ||
name string | ||
col proto.ColTime | ||
} | ||
|
||
func (col *Time) Reset() { | ||
col.col.Reset() | ||
} | ||
|
||
func (col *Time) Name() string { | ||
return col.name | ||
} | ||
|
||
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 | ||
} | ||
|
||
// ScanRow implements column.Interface. | ||
// It is used to read a single column value of the row and store in | ||
// `dest` Go variable. | ||
func (col *Time) ScanRow(dest any, row int) error { | ||
switch d := dest.(type) { | ||
case *time.Duration: | ||
*d = col.row(row) | ||
case **time.Duration: | ||
*d = new(time.Duration) | ||
**d = col.row(row) | ||
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 | ||
} | ||
|
||
// Append implements column.Interface. | ||
// It is used for columnar inserts. Insert multiple Go value for | ||
// single ClickHouse Time type. | ||
func (col *Time) Append(v any) (nulls []uint8, err error) { | ||
switch v := v.(type) { | ||
case []time.Duration: | ||
nulls = make([]uint8, len(v)) // default all zeros, meaning no null values | ||
for i := range v { | ||
col.col.Append(proto.IntoTime32(v[i])) | ||
} | ||
case []*time.Duration: | ||
nulls = make([]uint8, len(v)) | ||
for i := range v { | ||
switch { | ||
case v[i] != nil: | ||
col.col.Append(proto.IntoTime32(*v[i])) | ||
default: | ||
col.col.Append(proto.IntoTime32(time.Duration(0))) | ||
nulls[i] = 1 | ||
} | ||
} | ||
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 | ||
} | ||
|
||
// AppendRow implements column.Interface. | ||
// It is used to insert column value in a row. | ||
// Converts Go type into ClickHouse type to be inserted. | ||
func (col *Time) AppendRow(v any) error { | ||
switch v := v.(type) { | ||
case time.Duration: | ||
col.col.Append(proto.IntoTime32(v)) | ||
case *time.Duration: | ||
switch { | ||
case v != nil: | ||
col.col.Append(proto.IntoTime32(*v)) | ||
default: | ||
col.col.Append(proto.IntoTime32(time.Duration(0))) | ||
} | ||
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.Duration { | ||
return col.col.Row(i).Duration() | ||
} | ||
|
||
func (col *Time) parseTime(value string) (time.Duration, error) { | ||
return parseDuration(value) | ||
} | ||
|
||
// helpers | ||
|
||
func parseDuration(value string) (time.Duration, error) { | ||
value = strings.TrimSpace(value) | ||
if value == "" { | ||
return time.Duration(0), nil | ||
} | ||
|
||
return time.ParseDuration(value) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: This points to this PR commit in ch-go. Once it's merged we can point
main
(or after releasing point to specific newer version)