diff options
author | Daniel Baumann <mail@daniel-baumann.ch> | 2023-12-10 10:46:01 +0000 |
---|---|---|
committer | Daniel Baumann <mail@daniel-baumann.ch> | 2023-12-10 10:46:01 +0000 |
commit | 8fe30fd23dc37ec3516e530a86d1c4b604e71241 (patch) | |
tree | 6e2ebbf565b0351fd0f003f488a8339e771ad90c /sqlglot/dialects/clickhouse.py | |
parent | Releasing debian version 19.0.1-1. (diff) | |
download | sqlglot-8fe30fd23dc37ec3516e530a86d1c4b604e71241.tar.xz sqlglot-8fe30fd23dc37ec3516e530a86d1c4b604e71241.zip |
Merging upstream version 20.1.0.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'sqlglot/dialects/clickhouse.py')
-rw-r--r-- | sqlglot/dialects/clickhouse.py | 36 |
1 files changed, 14 insertions, 22 deletions
diff --git a/sqlglot/dialects/clickhouse.py b/sqlglot/dialects/clickhouse.py index 394a922..da182aa 100644 --- a/sqlglot/dialects/clickhouse.py +++ b/sqlglot/dialects/clickhouse.py @@ -35,8 +35,8 @@ def _quantile_sql(self, e): class ClickHouse(Dialect): NORMALIZE_FUNCTIONS: bool | str = False NULL_ORDERING = "nulls_are_last" - STRICT_STRING_CONCAT = True SUPPORTS_USER_DEFINED_TYPES = False + SAFE_DIVISION = True ESCAPE_SEQUENCES = { "\\0": "\0", @@ -63,11 +63,7 @@ class ClickHouse(Dialect): "FLOAT32": TokenType.FLOAT, "FLOAT64": TokenType.DOUBLE, "GLOBAL": TokenType.GLOBAL, - "INT16": TokenType.SMALLINT, "INT256": TokenType.INT256, - "INT32": TokenType.INT, - "INT64": TokenType.BIGINT, - "INT8": TokenType.TINYINT, "LOWCARDINALITY": TokenType.LOWCARDINALITY, "MAP": TokenType.MAP, "NESTED": TokenType.NESTED, @@ -112,6 +108,7 @@ class ClickHouse(Dialect): FUNCTION_PARSERS = { **parser.Parser.FUNCTION_PARSERS, + "ARRAYJOIN": lambda self: self.expression(exp.Explode, this=self._parse_expression()), "QUANTILE": lambda self: self._parse_quantile(), } @@ -223,12 +220,13 @@ class ClickHouse(Dialect): except ParseError: # WITH <expression> AS <identifier> self._retreat(index) - statement = self._parse_statement() - if statement and isinstance(statement.this, exp.Alias): - self.raise_error("Expected CTE to have alias") - - return self.expression(exp.CTE, this=statement, alias=statement and statement.this) + return self.expression( + exp.CTE, + this=self._parse_field(), + alias=self._parse_table_alias(), + scalar=True, + ) def _parse_join_parts( self, @@ -385,9 +383,11 @@ class ClickHouse(Dialect): exp.DateDiff: lambda self, e: self.func( "DATE_DIFF", exp.Literal.string(e.text("unit") or "day"), e.expression, e.this ), + exp.Explode: rename_func("arrayJoin"), exp.Final: lambda self, e: f"{self.sql(e, 'this')} FINAL", exp.IsNan: rename_func("isNaN"), exp.Map: lambda self, e: _lower_func(var_map_sql(self, e)), + exp.Nullif: rename_func("nullIf"), exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}", exp.Pivot: no_pivot_sql, exp.Quantile: _quantile_sql, @@ -459,19 +459,11 @@ class ClickHouse(Dialect): return super().datatype_sql(expression) - def safeconcat_sql(self, expression: exp.SafeConcat) -> str: - # Clickhouse errors out if we try to cast a NULL value to TEXT - return self.func( - "CONCAT", - *[ - exp.func("if", e.is_(exp.null()), e, exp.cast(e, "text")) - for e in t.cast(t.List[exp.Condition], expression.expressions) - ], - ) - def cte_sql(self, expression: exp.CTE) -> str: - if isinstance(expression.this, exp.Alias): - return self.sql(expression, "this") + if expression.args.get("scalar"): + this = self.sql(expression, "this") + alias = self.sql(expression, "alias") + return f"{this} AS {alias}" return super().cte_sql(expression) |