Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into json_type
  • Loading branch information
SpencerTorres committed Jan 13, 2025
2 parents 5ee295b + 96b7866 commit b123e14
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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
22 changes: 18 additions & 4 deletions tests/std/datetime64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ func TestStdDateTime64(t *testing.T) {
t.Run(fmt.Sprintf("%s Protocol", name), 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
}

const ddl = `
CREATE TABLE test_datetime64 (
Col1 DateTime64(3)
Expand Down Expand Up @@ -118,5 +115,22 @@ 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)

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)
})
}
}

0 comments on commit b123e14

Please sign in to comment.