Skip to content

Commit

Permalink
Feat(clickhouse): add support for bracket map syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Dec 17, 2024
1 parent bc68289 commit 3bb70c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
24 changes: 23 additions & 1 deletion sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,19 +565,41 @@ def _parse_query_parameter(self) -> t.Optional[exp.Expression]:
Parse a placeholder expression like SELECT {abc: UInt32} or FROM {table: Identifier}
https://clickhouse.com/docs/en/sql-reference/syntax#defining-and-using-query-parameters
"""
index = self._index

this = self._parse_id_var()
self._match(TokenType.COLON)
kind = self._parse_types(check_func=False, allow_identifiers=False) or (
self._match_text_seq("IDENTIFIER") and "Identifier"
)

if not kind:
self.raise_error("Expecting a placeholder type or 'Identifier' for tables")
self._retreat(index)
return None
elif not self._match(TokenType.R_BRACE):
self.raise_error("Expecting }")

return self.expression(exp.Placeholder, this=this, kind=kind)

def _parse_bracket(
self, this: t.Optional[exp.Expression] = None
) -> t.Optional[exp.Expression]:
l_brace = self._match(TokenType.L_BRACE, advance=False)
bracket = super()._parse_bracket(this)

if l_brace and isinstance(bracket, exp.Struct):
varmap = exp.VarMap(keys=exp.Array(), values=exp.Array())
for expression in bracket.expressions:
if not isinstance(expression, exp.PropertyEQ):
break

varmap.args["keys"].append("expressions", exp.Literal.string(expression.name))
varmap.args["values"].append("expressions", expression.expression)

return varmap

return bracket

def _parse_in(self, this: t.Optional[exp.Expression], is_global: bool = False) -> exp.In:
this = super()._parse_in(this)
this.set("is_global", is_global)
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 @@ -154,6 +154,10 @@ def test_clickhouse(self):
self.validate_identity(
"CREATE TABLE t (foo String CODEC(LZ4HC(9), ZSTD, DELTA), size String ALIAS formatReadableSize(size_bytes), INDEX idx1 a TYPE bloom_filter(0.001) GRANULARITY 1, INDEX idx2 a TYPE set(100) GRANULARITY 2, INDEX idx3 a TYPE minmax GRANULARITY 3)"
)
self.validate_identity(
"INSERT INTO tab VALUES ({'key1': 1, 'key2': 10}), ({'key1': 2, 'key2': 20}), ({'key1': 3, 'key2': 30})",
"INSERT INTO tab VALUES (map('key1', 1, 'key2', 10)), (map('key1', 2, 'key2', 20)), (map('key1', 3, 'key2', 30))",
)
self.validate_identity(
"SELECT (toUInt8('1') + toUInt8('2')) IS NOT NULL",
"SELECT NOT ((toUInt8('1') + toUInt8('2')) IS NULL)",
Expand Down

0 comments on commit 3bb70c9

Please sign in to comment.