Skip to content

Commit

Permalink
fix(clickhouse): use correct functions for milli and micro extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Jun 22, 2023
1 parent 32b8759 commit 49b3136
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ibis/backends/clickhouse/compiler/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,14 +1102,26 @@ def _fmt(op, _name: str = _name, **kw):
def _extract_microsecond(op, **kw):
arg = translate_val(op.arg, **kw)
dtype = serialize(op.output_dtype)
return f"CAST(substring(formatDateTime({arg}, '%f'), 1, 3) AS {dtype})"

datetime_type_args = ["6"]
if (tz := op.arg.output_dtype.timezone) is not None:
datetime_type_args.append(f"'{tz}'")

datetime_type = f"DateTime64({', '.join(datetime_type_args)})"
return f"CAST(toUnixTimestamp64Micro(CAST({arg} AS {datetime_type})) % 1000000 AS {dtype})"


@translate_val.register(ops.ExtractMillisecond)
def _extract_millisecond(op, **kw):
arg = translate_val(op.arg, **kw)
dtype = serialize(op.output_dtype)
return f"CAST(substring(formatDateTime({arg}, '%f'), 1, 3) AS {dtype})"

datetime_type_args = ["3"]
if (tz := op.arg.output_dtype.timezone) is not None:
datetime_type_args.append(f"'{tz}'")

datetime_type = f"DateTime64({', '.join(datetime_type_args)})"
return f"CAST(toUnixTimestamp64Milli(CAST({arg} AS {datetime_type})) % 1000 AS {dtype})"


@translate_val.register
Expand Down

0 comments on commit 49b3136

Please sign in to comment.