Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
arg_max_or_min_no_count,
build_date_delta,
build_formatted_time,
build_like,
inline_array_sql,
json_extract_segments,
json_path_key_only_name,
Expand Down Expand Up @@ -364,13 +365,16 @@ class Parser(parser.Parser):
"DATESUB": build_date_delta(exp.DateSub, default_unit=None),
"FORMATDATETIME": _build_datetime_format(exp.TimeToStr),
"HAS": exp.ArrayContains.from_arg_list,
"ILIKE": build_like(exp.ILike),
"JSONEXTRACTSTRING": build_json_extract_path(
exp.JSONExtractScalar, zero_based_indexing=False
),
"LENGTH": lambda args: exp.Length(this=seq_get(args, 0), binary=True),
"LIKE": build_like(exp.Like),
"L2Distance": exp.EuclideanDistance.from_arg_list,
"MAP": parser.build_var_map,
"MATCH": exp.RegexpLike.from_arg_list,
"NOTLIKE": build_like(exp.Like, not_like=True),
"PARSEDATETIME": _build_datetime_format(exp.ParseDatetime),
"RANDCANONICAL": exp.Rand.from_arg_list,
"STR_TO_DATE": _build_str_to_date,
Expand Down
18 changes: 13 additions & 5 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1787,11 +1787,19 @@ def sequence_sql(self: Generator, expression: exp.GenerateSeries | exp.GenerateD
return self.func("SEQUENCE", start, end, step)


def build_like(expr_type: t.Type[E]) -> t.Callable[[t.List], E | exp.Escape]:
def _builder(args: t.List) -> E | exp.Escape:
like_expr = expr_type(this=seq_get(args, 0), expression=seq_get(args, 1))
escape = seq_get(args, 2)
return exp.Escape(this=like_expr, expression=escape) if escape else like_expr
def build_like(
expr_type: t.Type[E], not_like: bool = False
) -> t.Callable[[t.List], exp.Expression]:
def _builder(args: t.List) -> exp.Expression:
like_expr: exp.Expression = expr_type(this=seq_get(args, 0), expression=seq_get(args, 1))

if escape := seq_get(args, 2):
like_expr = exp.Escape(this=like_expr, expression=escape)

if not_like:
like_expr = exp.Not(this=like_expr)

return like_expr

return _builder

Expand Down
4 changes: 4 additions & 0 deletions tests/dialects/test_clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ def test_clickhouse(self):
self.validate_identity("L2Distance(x, y)")
self.validate_identity("tuple(1 = 1, 'foo' = 'foo')")

self.validate_identity("SELECT LIKE(a, b)", "SELECT a LIKE b")
self.validate_identity("SELECT notLike(a, b)", "SELECT NOT a LIKE b")
self.validate_identity("SELECT ilike(a, b)", "SELECT a ILIKE b")

def test_clickhouse_values(self):
ast = self.parse_one("SELECT * FROM VALUES (1, 2, 3)")
self.assertEqual(len(list(ast.find_all(exp.Tuple))), 4)
Expand Down