From a3fac3ea7383e6be29538784fb5349db7fae2ce4 Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Wed, 13 Dec 2023 13:08:25 -0500 Subject: [PATCH] refactor(clickhouse): use isoformat instead of manual specification --- ibis/backends/clickhouse/compiler/values.py | 20 ++++++++++++------- .../test_timestamp_truncate/d/out.sql | 2 +- .../test_timestamp_truncate/h/out.sql | 2 +- .../test_timestamp_truncate/m/out.sql | 2 +- .../test_timestamp_truncate/minute/out.sql | 2 +- .../test_timestamp_truncate/w/out.sql | 2 +- .../test_timestamp_truncate/y/out.sql | 2 +- .../micros/out.sql | 2 +- .../micros_tz/out.sql | 2 +- .../millis/out.sql | 2 +- .../millis_tz/out.sql | 2 +- .../test_timestamp_literals/expr0/out.sql | 2 +- .../test_timestamp_literals/expr1/out.sql | 2 +- .../test_timestamp_literals/expr2/out.sql | 2 +- 14 files changed, 26 insertions(+), 20 deletions(-) diff --git a/ibis/backends/clickhouse/compiler/values.py b/ibis/backends/clickhouse/compiler/values.py index ba4163229135..88589b5620f3 100644 --- a/ibis/backends/clickhouse/compiler/values.py +++ b/ibis/backends/clickhouse/compiler/values.py @@ -346,18 +346,24 @@ def _literal(op, *, value, dtype, **kw): return interval(value, unit=dtype.resolution.upper()) elif dtype.is_timestamp(): - funcname = "toDateTime" - fmt = "%Y-%m-%dT%H:%M:%S" - + funcname = "makeDateTime" if micros := value.microsecond: funcname += "64" - fmt += ".%f" - args = [value.strftime(fmt)] + args = [ + value.year, + value.month, + value.day, + value.hour, + value.minute, + value.second, + ] if micros % 1000: + args.append(micros) args.append(6) - elif micros // 1000: + elif millis := micros // 1000: + args.append(millis) args.append(3) if (timezone := dtype.timezone) is not None: @@ -365,7 +371,7 @@ def _literal(op, *, value, dtype, **kw): return F[funcname](*args) elif dtype.is_date(): - return F.toDate(value.strftime("%Y-%m-%d")) + return F.toDate(value.isoformat()) elif dtype.is_array(): value_type = dtype.value_type values = [ diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/d/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/d/out.sql index 62efc1ba5da3..4915f891c49c 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/d/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/d/out.sql @@ -1,2 +1,2 @@ SELECT - toDate(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file + toDate(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/h/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/h/out.sql index 151ab1446fcc..03b8c49d440b 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/h/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/h/out.sql @@ -1,2 +1,2 @@ SELECT - toStartOfHour(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file + toStartOfHour(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/m/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/m/out.sql index d8b699a2fad5..ec74669167e9 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/m/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/m/out.sql @@ -1,2 +1,2 @@ SELECT - toStartOfMinute(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file + toStartOfMinute(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/minute/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/minute/out.sql index d8b699a2fad5..ec74669167e9 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/minute/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/minute/out.sql @@ -1,2 +1,2 @@ SELECT - toStartOfMinute(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file + toStartOfMinute(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/w/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/w/out.sql index c470182f8e7f..97e90c95e2ed 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/w/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/w/out.sql @@ -1,2 +1,2 @@ SELECT - toMonday(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file + toMonday(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/y/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/y/out.sql index 627ecf3e76d4..ad634a785ac5 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/y/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_functions/test_timestamp_truncate/y/out.sql @@ -1,2 +1,2 @@ SELECT - toStartOfYear(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file + toStartOfYear(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/micros/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/micros/out.sql index ce21e32a643d..28640cbe6add 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/micros/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/micros/out.sql @@ -1,2 +1,2 @@ SELECT - toDateTime64('2015-01-01T12:34:56.789321', 6) AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789321)" \ No newline at end of file + makeDateTime64(2015, 1, 1, 12, 34, 56, 789321, 6) AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789321)" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/micros_tz/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/micros_tz/out.sql index dca34dda04fe..1868fd9c61af 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/micros_tz/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/micros_tz/out.sql @@ -1,2 +1,2 @@ SELECT - toDateTime64('2015-01-01T12:34:56.789321', 6, 'UTC') AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789321, tzinfo=tzutc())" \ No newline at end of file + makeDateTime64(2015, 1, 1, 12, 34, 56, 789321, 6, 'UTC') AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789321, tzinfo=tzutc())" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/millis/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/millis/out.sql index 94e5de1704d9..5a3692e3a003 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/millis/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/millis/out.sql @@ -1,2 +1,2 @@ SELECT - toDateTime64('2015-01-01T12:34:56.789000', 3) AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789000)" \ No newline at end of file + makeDateTime64(2015, 1, 1, 12, 34, 56, 789, 3) AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789000)" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/millis_tz/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/millis_tz/out.sql index 948f20c31431..61b9edaea475 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/millis_tz/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_fine_grained_timestamp_literals/millis_tz/out.sql @@ -1,2 +1,2 @@ SELECT - toDateTime64('2015-01-01T12:34:56.789000', 3, 'UTC') AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789000, tzinfo=tzutc())" \ No newline at end of file + makeDateTime64(2015, 1, 1, 12, 34, 56, 789, 3, 'UTC') AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789000, tzinfo=tzutc())" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr0/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr0/out.sql index 2ba743636e07..b3b6c498b3e9 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr0/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr0/out.sql @@ -1,2 +1,2 @@ SELECT - toDateTime('2015-01-01T12:34:56') AS "datetime.datetime(2015, 1, 1, 12, 34, 56)" \ No newline at end of file + makeDateTime(2015, 1, 1, 12, 34, 56) AS "datetime.datetime(2015, 1, 1, 12, 34, 56)" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr1/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr1/out.sql index 2ba743636e07..b3b6c498b3e9 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr1/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr1/out.sql @@ -1,2 +1,2 @@ SELECT - toDateTime('2015-01-01T12:34:56') AS "datetime.datetime(2015, 1, 1, 12, 34, 56)" \ No newline at end of file + makeDateTime(2015, 1, 1, 12, 34, 56) AS "datetime.datetime(2015, 1, 1, 12, 34, 56)" \ No newline at end of file diff --git a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr2/out.sql b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr2/out.sql index 2ba743636e07..b3b6c498b3e9 100644 --- a/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr2/out.sql +++ b/ibis/backends/clickhouse/tests/snapshots/test_literals/test_timestamp_literals/expr2/out.sql @@ -1,2 +1,2 @@ SELECT - toDateTime('2015-01-01T12:34:56') AS "datetime.datetime(2015, 1, 1, 12, 34, 56)" \ No newline at end of file + makeDateTime(2015, 1, 1, 12, 34, 56) AS "datetime.datetime(2015, 1, 1, 12, 34, 56)" \ No newline at end of file