Skip to content

Commit e8698a6

Browse files
cpcloudkszucs
authored andcommitted
feat(clickhouse): support subsecond timestamp literals
1 parent 469c8b2 commit e8698a6

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

ibis/backends/clickhouse/registry.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,14 @@ def _literal(translator, expr):
371371
return _interval_format(translator, expr)
372372
elif isinstance(expr, ir.TimestampValue):
373373
if isinstance(value, datetime):
374-
if value.microsecond != 0:
375-
msg = 'Unsupported subsecond accuracy {}'
376-
raise ValueError(msg.format(value))
377-
value = value.strftime('%Y-%m-%d %H:%M:%S')
378-
return f"toDateTime('{value!s}')"
374+
micros = value.microsecond
375+
value = repr(value.isoformat())
376+
377+
if micros % 1000:
378+
return f"toDateTime64({value}, 6)"
379+
elif micros // 1000:
380+
return f"toDateTime64({value}, 3)"
381+
return f"toDateTime({value})"
379382
elif isinstance(expr, ir.DateValue):
380383
if isinstance(value, date):
381384
value = value.strftime('%Y-%m-%d')

0 commit comments

Comments
 (0)