diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-08 08:11:53 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-08 08:12:02 +0000 |
commit | 8d36f5966675e23bee7026ba37ae0647fbf47300 (patch) | |
tree | df4227bbb3b07cb70df87237bcff03c8efd7822d /sqlglot/dialects/clickhouse.py | |
parent | Releasing debian version 22.2.0-1. (diff) | |
download | sqlglot-8d36f5966675e23bee7026ba37ae0647fbf47300.tar.xz sqlglot-8d36f5966675e23bee7026ba37ae0647fbf47300.zip |
Merging upstream version 23.7.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/dialects/clickhouse.py')
-rw-r--r-- | sqlglot/dialects/clickhouse.py | 77 |
1 files changed, 67 insertions, 10 deletions
diff --git a/sqlglot/dialects/clickhouse.py b/sqlglot/dialects/clickhouse.py index 90167f6..631dc30 100644 --- a/sqlglot/dialects/clickhouse.py +++ b/sqlglot/dialects/clickhouse.py @@ -15,7 +15,6 @@ from sqlglot.dialects.dialect import ( rename_func, var_map_sql, ) -from sqlglot.errors import ParseError from sqlglot.helper import is_int, seq_get from sqlglot.tokens import Token, TokenType @@ -49,8 +48,9 @@ class ClickHouse(Dialect): NULL_ORDERING = "nulls_are_last" SUPPORTS_USER_DEFINED_TYPES = False SAFE_DIVISION = True + LOG_BASE_FIRST: t.Optional[bool] = None - ESCAPE_SEQUENCES = { + UNESCAPED_SEQUENCES = { "\\0": "\0", } @@ -105,6 +105,7 @@ class ClickHouse(Dialect): # * select x from t1 union all select x from t2 limit 1; # * select x from t1 union all (select x from t2 limit 1); MODIFIERS_ATTACHED_TO_UNION = False + INTERVAL_SPANS = False FUNCTIONS = { **parser.Parser.FUNCTIONS, @@ -260,6 +261,11 @@ class ClickHouse(Dialect): "ArgMax", ] + FUNC_TOKENS = { + *parser.Parser.FUNC_TOKENS, + TokenType.SET, + } + AGG_FUNC_MAPPING = ( lambda functions, suffixes: { f"{f}{sfx}": (f, sfx) for sfx in (suffixes + [""]) for f in functions @@ -305,6 +311,10 @@ class ClickHouse(Dialect): TokenType.SETTINGS, } + ALIAS_TOKENS = parser.Parser.ALIAS_TOKENS - { + TokenType.FORMAT, + } + LOG_DEFAULTS_TO_LN = True QUERY_MODIFIER_PARSERS = { @@ -316,6 +326,17 @@ class ClickHouse(Dialect): TokenType.FORMAT: lambda self: ("format", self._advance() or self._parse_id_var()), } + CONSTRAINT_PARSERS = { + **parser.Parser.CONSTRAINT_PARSERS, + "INDEX": lambda self: self._parse_index_constraint(), + "CODEC": lambda self: self._parse_compress(), + } + + SCHEMA_UNNAMED_CONSTRAINTS = { + *parser.Parser.SCHEMA_UNNAMED_CONSTRAINTS, + "INDEX", + } + def _parse_conjunction(self) -> t.Optional[exp.Expression]: this = super()._parse_conjunction() @@ -381,21 +402,20 @@ class ClickHouse(Dialect): # https://clickhouse.com/docs/en/sql-reference/statements/select/with/ def _parse_cte(self) -> exp.CTE: - index = self._index - try: - # WITH <identifier> AS <subquery expression> - return super()._parse_cte() - except ParseError: - # WITH <expression> AS <identifier> - self._retreat(index) + # WITH <identifier> AS <subquery expression> + cte: t.Optional[exp.CTE] = self._try_parse(super()._parse_cte) - return self.expression( + if not cte: + # WITH <expression> AS <identifier> + cte = self.expression( exp.CTE, this=self._parse_conjunction(), alias=self._parse_table_alias(), scalar=True, ) + return cte + def _parse_join_parts( self, ) -> t.Tuple[t.Optional[Token], t.Optional[Token], t.Optional[Token]]: @@ -508,6 +528,27 @@ class ClickHouse(Dialect): self._retreat(index) return None + def _parse_index_constraint( + self, kind: t.Optional[str] = None + ) -> exp.IndexColumnConstraint: + # INDEX name1 expr TYPE type1(args) GRANULARITY value + this = self._parse_id_var() + expression = self._parse_conjunction() + + index_type = self._match_text_seq("TYPE") and ( + self._parse_function() or self._parse_var() + ) + + granularity = self._match_text_seq("GRANULARITY") and self._parse_term() + + return self.expression( + exp.IndexColumnConstraint, + this=this, + expression=expression, + index_type=index_type, + granularity=granularity, + ) + class Generator(generator.Generator): QUERY_HINTS = False STRUCT_DELIMITER = ("(", ")") @@ -517,6 +558,7 @@ class ClickHouse(Dialect): TABLESAMPLE_KEYWORDS = "SAMPLE" LAST_DAY_SUPPORTS_DATE_PART = False CAN_IMPLEMENT_ARRAY_ANY = True + SUPPORTS_TO_NUMBER = False STRING_TYPE_MAPPING = { exp.DataType.Type.CHAR: "String", @@ -585,6 +627,9 @@ class ClickHouse(Dialect): exp.Array: inline_array_sql, exp.CastToStrType: rename_func("CAST"), exp.CountIf: rename_func("countIf"), + exp.CompressColumnConstraint: lambda self, + e: f"CODEC({self.expressions(e, key='this', flat=True)})", + exp.ComputedColumnConstraint: lambda self, e: f"ALIAS {self.sql(e, 'this')}", exp.CurrentDate: lambda self, e: self.func("CURRENT_DATE"), exp.DateAdd: date_delta_sql("DATE_ADD"), exp.DateDiff: date_delta_sql("DATE_DIFF"), @@ -737,3 +782,15 @@ class ClickHouse(Dialect): def prewhere_sql(self, expression: exp.PreWhere) -> str: this = self.indent(self.sql(expression, "this")) return f"{self.seg('PREWHERE')}{self.sep()}{this}" + + def indexcolumnconstraint_sql(self, expression: exp.IndexColumnConstraint) -> str: + this = self.sql(expression, "this") + this = f" {this}" if this else "" + expr = self.sql(expression, "expression") + expr = f" {expr}" if expr else "" + index_type = self.sql(expression, "index_type") + index_type = f" TYPE {index_type}" if index_type else "" + granularity = self.sql(expression, "granularity") + granularity = f" GRANULARITY {granularity}" if granularity else "" + + return f"INDEX{this}{expr}{index_type}{granularity}" |