Skip to content

Commit

Permalink
feat(clickhouse): Add support for DATE_FORMAT / formatDateTime (tobym…
Browse files Browse the repository at this point in the history
…ao#3329)

* feat(clickhouse): Add support for DATE_FORMAT / formatDateTime

* Add timezone (3rd arg)

* Make style fix, remove closure

* Rename helper func, remove lambdas
  • Loading branch information
VaggelisD authored Apr 16, 2024
1 parent cd1cd61 commit 7f9cb2d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
16 changes: 16 additions & 0 deletions sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sqlglot.dialects.dialect import (
Dialect,
arg_max_or_min_no_count,
build_formatted_time,
date_delta_sql,
inline_array_sql,
json_extract_segments,
Expand All @@ -19,6 +20,16 @@
from sqlglot.tokens import Token, TokenType


def _build_date_format(args: t.List) -> exp.TimeToStr:
expr = build_formatted_time(exp.TimeToStr, "clickhouse")(args)

timezone = seq_get(args, 2)
if timezone:
expr.set("timezone", timezone)

return expr


def _lower_func(sql: str) -> str:
index = sql.index("(")
return sql[:index].lower() + sql[index:]
Expand Down Expand Up @@ -124,6 +135,8 @@ class Parser(parser.Parser):
"DATEDIFF": lambda args: exp.DateDiff(
this=seq_get(args, 2), expression=seq_get(args, 1), unit=seq_get(args, 0)
),
"DATE_FORMAT": _build_date_format,
"FORMATDATETIME": _build_date_format,
"JSONEXTRACTSTRING": build_json_extract_path(
exp.JSONExtractScalar, zero_based_indexing=False
),
Expand Down Expand Up @@ -653,6 +666,9 @@ class Generator(generator.Generator):
exp.StrPosition: lambda self, e: self.func(
"position", e.this, e.args.get("substr"), e.args.get("position")
),
exp.TimeToStr: lambda self, e: self.func(
"DATE_FORMAT", e.this, self.format_time(e), e.args.get("timezone")
),
exp.VarMap: lambda self, e: _lower_func(var_map_sql(self, e)),
exp.Xor: lambda self, e: self.func("xor", e.this, e.expression, *e.expressions),
}
Expand Down
2 changes: 1 addition & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5684,7 +5684,7 @@ class StddevSamp(AggFunc):


class TimeToStr(Func):
arg_types = {"this": True, "format": True, "culture": False}
arg_types = {"this": True, "format": True, "culture": False, "timezone": False}


class TimeToTimeStr(Func):
Expand Down
13 changes: 13 additions & 0 deletions tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,19 @@ def test_clickhouse(self):
self.validate_identity("SELECT FORMAT")
self.validate_identity("1 AS FORMAT").assert_is(exp.Alias)

self.validate_identity("SELECT DATE_FORMAT(NOW(), '%Y-%m-%d', '%T')")
self.validate_all(
"SELECT DATE_FORMAT(NOW(), '%Y-%m-%d')",
read={
"clickhouse": "SELECT formatDateTime(NOW(), '%Y-%m-%d')",
"mysql": "SELECT DATE_FORMAT(NOW(), '%Y-%m-%d')",
},
write={
"clickhouse": "SELECT DATE_FORMAT(NOW(), '%Y-%m-%d')",
"mysql": "SELECT DATE_FORMAT(NOW(), '%Y-%m-%d')",
},
)

def test_cte(self):
self.validate_identity("WITH 'x' AS foo SELECT foo")
self.validate_identity("WITH ['c'] AS field_names SELECT field_names")
Expand Down

0 comments on commit 7f9cb2d

Please sign in to comment.