Skip to content

Commit

Permalink
feat(clickhouse): support subsecond timestamp literals
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and kszucs committed Jun 16, 2022
1 parent 469c8b2 commit e8698a6
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions ibis/backends/clickhouse/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,14 @@ def _literal(translator, expr):
return _interval_format(translator, expr)
elif isinstance(expr, ir.TimestampValue):
if isinstance(value, datetime):
if value.microsecond != 0:
msg = 'Unsupported subsecond accuracy {}'
raise ValueError(msg.format(value))
value = value.strftime('%Y-%m-%d %H:%M:%S')
return f"toDateTime('{value!s}')"
micros = value.microsecond
value = repr(value.isoformat())

if micros % 1000:
return f"toDateTime64({value}, 6)"
elif micros // 1000:
return f"toDateTime64({value}, 3)"
return f"toDateTime({value})"
elif isinstance(expr, ir.DateValue):
if isinstance(value, date):
value = value.strftime('%Y-%m-%d')
Expand Down

0 comments on commit e8698a6

Please sign in to comment.