Skip to content

Commit

Permalink
refactor(clickhouse): use isoformat instead of manual specification
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Dec 18, 2023
1 parent 66220c7 commit a3fac3e
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 20 deletions.
20 changes: 13 additions & 7 deletions ibis/backends/clickhouse/compiler/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,26 +346,32 @@ 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:
args.append(timezone)

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 = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toDate(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
toDate(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toStartOfHour(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
toStartOfHour(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toStartOfMinute(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
toStartOfMinute(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toStartOfMinute(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
toStartOfMinute(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toMonday(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
toMonday(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toStartOfYear(toDateTime('2009-05-17T12:34:56')) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
toStartOfYear(makeDateTime(2009, 5, 17, 12, 34, 56)) AS "TimestampTruncate(datetime.datetime(2009, 5, 17, 12, 34, 56))"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toDateTime64('2015-01-01T12:34:56.789321', 6) AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789321)"
makeDateTime64(2015, 1, 1, 12, 34, 56, 789321, 6) AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789321)"
Original file line number Diff line number Diff line change
@@ -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())"
makeDateTime64(2015, 1, 1, 12, 34, 56, 789321, 6, 'UTC') AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789321, tzinfo=tzutc())"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toDateTime64('2015-01-01T12:34:56.789000', 3) AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789000)"
makeDateTime64(2015, 1, 1, 12, 34, 56, 789, 3) AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789000)"
Original file line number Diff line number Diff line change
@@ -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())"
makeDateTime64(2015, 1, 1, 12, 34, 56, 789, 3, 'UTC') AS "datetime.datetime(2015, 1, 1, 12, 34, 56, 789000, tzinfo=tzutc())"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toDateTime('2015-01-01T12:34:56') AS "datetime.datetime(2015, 1, 1, 12, 34, 56)"
makeDateTime(2015, 1, 1, 12, 34, 56) AS "datetime.datetime(2015, 1, 1, 12, 34, 56)"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toDateTime('2015-01-01T12:34:56') AS "datetime.datetime(2015, 1, 1, 12, 34, 56)"
makeDateTime(2015, 1, 1, 12, 34, 56) AS "datetime.datetime(2015, 1, 1, 12, 34, 56)"
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT
toDateTime('2015-01-01T12:34:56') AS "datetime.datetime(2015, 1, 1, 12, 34, 56)"
makeDateTime(2015, 1, 1, 12, 34, 56) AS "datetime.datetime(2015, 1, 1, 12, 34, 56)"

0 comments on commit a3fac3e

Please sign in to comment.