Skip to content

Commit

Permalink
feat(clickhouse): implement ops.TimestampFromYMDHMS, ops.DateFromYMD
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-kwitt authored and cpcloud committed Jan 30, 2023
1 parent 8493604 commit 05f5ae5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
38 changes: 38 additions & 0 deletions ibis/backends/clickhouse/compiler/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,44 @@ def _truncate(op, **kw):
return f"{converter}({arg})"


@translate_val.register(ops.DateFromYMD)
def _date_from_ymd(op, **kw):
y = translate_val(op.year, **kw)
m = translate_val(op.month, **kw)
d = translate_val(op.day, **kw)
return (
f"toDate(concat("
f"toString({y}), '-', "
f"leftPad(toString({m}), 2, '0'), '-', "
f"leftPad(toString({d}), 2, '0')"
f"))"
)


@translate_val.register(ops.TimestampFromYMDHMS)
def _timestamp_from_ymdhms(op, **kw):
y = translate_val(op.year, **kw)
m = translate_val(op.month, **kw)
d = translate_val(op.day, **kw)
h = translate_val(op.hours, **kw)
min = translate_val(op.minutes, **kw)
s = translate_val(op.seconds, **kw)
timezone_arg = ''
if timezone := op.output_dtype.timezone:
timezone_arg = f', {timezone}'

return (
f"toDateTime("
f"concat(toString({y}), '-', "
f"leftPad(toString({m}), 2, '0'), '-', "
f"leftPad(toString({d}), 2, '0'), ' ', "
f"leftPad(toString({h}), 2, '0'), ':', "
f"leftPad(toString({min}), 2, '0'), ':', "
f"leftPad(toString({s}), 2, '0')"
f"), {timezone_arg})"
)


@translate_val.register(ops.ExistsSubquery)
@translate_val.register(ops.NotExistsSubquery)
def _exists_subquery(op, **kw):
Expand Down
5 changes: 3 additions & 2 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,10 @@ def test_exists(batting, awards_players, method_name):
id="binary",
),
param(
ibis.date(12, 12, 12),
ibis.date(2022, 12, 12),
{
'bigquery': "DATE",
'clickhouse': 'Date',
'snowflake': 'DATE',
'sqlite': "text",
'trino': 'date',
Expand All @@ -1055,7 +1056,7 @@ def test_exists(batting, awards_players, method_name):
},
marks=[
pytest.mark.broken(
['clickhouse', 'impala'],
['impala'],
"No translation rule for <class 'ibis.expr.operations.temporal.DateFromYMD'>",
raises=com.OperationNotDefinedError,
),
Expand Down
8 changes: 4 additions & 4 deletions ibis/backends/tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,15 +773,15 @@ def test_now_from_projection(alltypes):


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


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


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


@pytest.mark.notimpl(["pandas", "datafusion", "mysql", "dask", "pyspark"])
@pytest.mark.notyet(["clickhouse", "impala"])
@pytest.mark.notyet(["impala"])
def test_timestamp_column_from_ymdhms(con, alltypes, df):
c = alltypes.timestamp_col
expr = ibis.timestamp(
Expand Down

0 comments on commit 05f5ae5

Please sign in to comment.