diff --git a/clickhouse_std.go b/clickhouse_std.go index 466d345fb2..e747c0ffe4 100644 --- a/clickhouse_std.go +++ b/clickhouse_std.go @@ -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 diff --git a/lib/column/datetime64.go b/lib/column/datetime64.go index 3e1f438b9f..668bc2cea8 100644 --- a/lib/column/datetime64.go +++ b/lib/column/datetime64.go @@ -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() } diff --git a/tests/std/datetime64_test.go b/tests/std/datetime64_test.go index 20ba9432d3..1dd05f7edb 100644 --- a/tests/std/datetime64_test.go +++ b/tests/std/datetime64_test.go @@ -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) @@ -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) + }) } }