Skip to content
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

stdlib sql return precision for DateTime64 #1469

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions clickhouse_std.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,16 @@ func (r *stdRows) ColumnTypePrecisionScale(idx int) (precision, scale int64, ok
switch col := r.rows.block.Columns[idx].(type) {
case *column.Decimal:
return col.Precision(), col.Scale(), true
case *column.DateTime64:
p, ok := col.Precision()
return p, 0, ok
case interface{ Base() column.Interface }:
switch col := col.Base().(type) {
case *column.Decimal:
return col.Precision(), col.Scale(), true
case *column.DateTime64:
p, ok := col.Precision()
return p, 0, ok
}
}
return 0, 0, false
Expand Down
4 changes: 4 additions & 0 deletions lib/column/datetime64.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (col *DateTime64) ScanType() reflect.Type {
return scanTypeTime
}

func (col *DateTime64) Precision() (int64, bool) {
return int64(col.col.Precision), col.col.PrecisionSet
}

func (col *DateTime64) Rows() int {
return col.col.Rows()
}
Expand Down
21 changes: 21 additions & 0 deletions tests/std/datetime64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,26 @@ func TestStdDateTime64(t *testing.T) {
require.Equal(t, time.Date(1900, 01, 01, 0, 0, 0, 0, time.UTC), col9)
require.Equal(t, time.Unix(0, 0).UTC(), col10)
})

t.Run("DateTime64 precision", func(t *testing.T) {
conn, err := GetStdDSNConnection(protocol, useSSL, nil)
require.NoError(t, err)
if !CheckMinServerVersion(conn, 20, 3, 0) {
t.Skip(fmt.Errorf("unsupported clickhouse version"))
return
}
SpencerTorres marked this conversation as resolved.
Show resolved Hide resolved

rows, err := conn.Query("SELECT toDateTime64(1546300800.123, 3)")
require.NoError(t, err)

columnTypes, err := rows.ColumnTypes()
require.NoError(t, err)
require.Len(t, columnTypes, 1)

precision, scale, ok := columnTypes[0].DecimalSize()
require.Equal(t, int64(3), precision)
require.Equal(t, int64(0), scale)
require.True(t, ok)
})
}
}
Loading