diff --git a/ibis/backends/sql/compilers/clickhouse.py b/ibis/backends/sql/compilers/clickhouse.py index 9459e90ef50ca..824526d70e0ae 100644 --- a/ibis/backends/sql/compilers/clickhouse.py +++ b/ibis/backends/sql/compilers/clickhouse.py @@ -2,7 +2,6 @@ import calendar import math -from functools import partial from string import whitespace from typing import TYPE_CHECKING, Any @@ -378,18 +377,22 @@ def visit_TimestampFromUNIX(self, op, *, arg, unit): def visit_TimestampTruncate(self, op, *, arg, unit): if (short := unit.short) == "W": - func = "toMonday" + funcname = "toMonday" else: - func = f"toStartOf{unit.singular.capitalize()}" + funcname = f"toStartOf{unit.singular.capitalize()}" + + func = self.f[funcname] if short in ("Y", "Q", "M", "W", "D"): - cast = partial(self.cast, to=op.dtype) - else: - cast = lambda x: x + # these units return `Date` so we have to cast back to the + # corresponding Ibis type + return self.cast(func(arg), op.dtype) - if short in ("s", "ms", "us", "ns"): - arg = self.f.toDateTime64(arg, op.arg.dtype.scale or 0) - return cast(self.f[func](arg)) + elif short in ("s", "ms", "us", "ns"): + return func(self.f.toDateTime64(arg, op.arg.dtype.scale or 0)) + else: + assert short in ("h", "m"), short + return func(arg) visit_TimeTruncate = visit_TimestampTruncate