Skip to content

Commit

Permalink
chore: clean up impl
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 25, 2024
1 parent 66c85ac commit a3846d4
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions ibis/backends/sql/compilers/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import calendar
import math
from functools import partial
from string import whitespace
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a3846d4

Please sign in to comment.