Skip to content

Commit 05f5ae5

Browse files
krzysztof-kwittcpcloud
authored andcommitted
feat(clickhouse): implement ops.TimestampFromYMDHMS, ops.DateFromYMD
1 parent 8493604 commit 05f5ae5

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

ibis/backends/clickhouse/compiler/values.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,44 @@ def _truncate(op, **kw):
561561
return f"{converter}({arg})"
562562

563563

564+
@translate_val.register(ops.DateFromYMD)
565+
def _date_from_ymd(op, **kw):
566+
y = translate_val(op.year, **kw)
567+
m = translate_val(op.month, **kw)
568+
d = translate_val(op.day, **kw)
569+
return (
570+
f"toDate(concat("
571+
f"toString({y}), '-', "
572+
f"leftPad(toString({m}), 2, '0'), '-', "
573+
f"leftPad(toString({d}), 2, '0')"
574+
f"))"
575+
)
576+
577+
578+
@translate_val.register(ops.TimestampFromYMDHMS)
579+
def _timestamp_from_ymdhms(op, **kw):
580+
y = translate_val(op.year, **kw)
581+
m = translate_val(op.month, **kw)
582+
d = translate_val(op.day, **kw)
583+
h = translate_val(op.hours, **kw)
584+
min = translate_val(op.minutes, **kw)
585+
s = translate_val(op.seconds, **kw)
586+
timezone_arg = ''
587+
if timezone := op.output_dtype.timezone:
588+
timezone_arg = f', {timezone}'
589+
590+
return (
591+
f"toDateTime("
592+
f"concat(toString({y}), '-', "
593+
f"leftPad(toString({m}), 2, '0'), '-', "
594+
f"leftPad(toString({d}), 2, '0'), ' ', "
595+
f"leftPad(toString({h}), 2, '0'), ':', "
596+
f"leftPad(toString({min}), 2, '0'), ':', "
597+
f"leftPad(toString({s}), 2, '0')"
598+
f"), {timezone_arg})"
599+
)
600+
601+
564602
@translate_val.register(ops.ExistsSubquery)
565603
@translate_val.register(ops.NotExistsSubquery)
566604
def _exists_subquery(op, **kw):

ibis/backends/tests/test_generic.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,9 +1044,10 @@ def test_exists(batting, awards_players, method_name):
10441044
id="binary",
10451045
),
10461046
param(
1047-
ibis.date(12, 12, 12),
1047+
ibis.date(2022, 12, 12),
10481048
{
10491049
'bigquery': "DATE",
1050+
'clickhouse': 'Date',
10501051
'snowflake': 'DATE',
10511052
'sqlite': "text",
10521053
'trino': 'date',
@@ -1055,7 +1056,7 @@ def test_exists(batting, awards_players, method_name):
10551056
},
10561057
marks=[
10571058
pytest.mark.broken(
1058-
['clickhouse', 'impala'],
1059+
['impala'],
10591060
"No translation rule for <class 'ibis.expr.operations.temporal.DateFromYMD'>",
10601061
raises=com.OperationNotDefinedError,
10611062
),

ibis/backends/tests/test_temporal.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,15 +773,15 @@ def test_now_from_projection(alltypes):
773773

774774

775775
@pytest.mark.notimpl(["pandas", "datafusion", "mysql", "dask", "pyspark"])
776-
@pytest.mark.notyet(["clickhouse", "impala"])
776+
@pytest.mark.notyet(["impala"])
777777
def test_date_literal(con):
778778
expr = ibis.date(2022, 2, 4)
779779
result = con.execute(expr)
780780
assert result.strftime('%Y-%m-%d') == '2022-02-04'
781781

782782

783783
@pytest.mark.notimpl(["pandas", "datafusion", "mysql", "dask", "pyspark"])
784-
@pytest.mark.notyet(["clickhouse", "impala"])
784+
@pytest.mark.notyet(["impala"])
785785
def test_timestamp_literal(con):
786786
expr = ibis.timestamp(2022, 2, 4, 16, 20, 0)
787787
result = con.execute(expr)
@@ -810,7 +810,7 @@ def test_time_literal(con):
810810

811811

812812
@pytest.mark.notimpl(["pandas", "datafusion", "mysql", "dask", "pyspark"])
813-
@pytest.mark.notyet(["clickhouse", "impala"])
813+
@pytest.mark.notyet(["impala"])
814814
def test_date_column_from_ymd(con, alltypes, df):
815815
c = alltypes.timestamp_col
816816
expr = ibis.date(c.year(), c.month(), c.day())
@@ -824,7 +824,7 @@ def test_date_column_from_ymd(con, alltypes, df):
824824

825825

826826
@pytest.mark.notimpl(["pandas", "datafusion", "mysql", "dask", "pyspark"])
827-
@pytest.mark.notyet(["clickhouse", "impala"])
827+
@pytest.mark.notyet(["impala"])
828828
def test_timestamp_column_from_ymdhms(con, alltypes, df):
829829
c = alltypes.timestamp_col
830830
expr = ibis.timestamp(

0 commit comments

Comments
 (0)