diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-02-08 05:38:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-02-08 05:38:42 +0000 |
commit | c66e4a33e1a07c439f03fe47f146a6c6482bf6df (patch) | |
tree | cfdf01111c063b3e50841695e6c2768833aea4dc /sqlglot/dialects/clickhouse.py | |
parent | Releasing debian version 20.11.0-1. (diff) | |
download | sqlglot-c66e4a33e1a07c439f03fe47f146a6c6482bf6df.tar.xz sqlglot-c66e4a33e1a07c439f03fe47f146a6c6482bf6df.zip |
Merging upstream version 21.0.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/dialects/clickhouse.py')
-rw-r--r-- | sqlglot/dialects/clickhouse.py | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/sqlglot/dialects/clickhouse.py b/sqlglot/dialects/clickhouse.py index 1248edc..1ec15c5 100644 --- a/sqlglot/dialects/clickhouse.py +++ b/sqlglot/dialects/clickhouse.py @@ -8,12 +8,15 @@ from sqlglot.dialects.dialect import ( arg_max_or_min_no_count, date_delta_sql, inline_array_sql, + json_extract_segments, + json_path_key_only_name, no_pivot_sql, + parse_json_extract_path, rename_func, var_map_sql, ) from sqlglot.errors import ParseError -from sqlglot.helper import seq_get +from sqlglot.helper import is_int, seq_get from sqlglot.parser import parse_var_map from sqlglot.tokens import Token, TokenType @@ -120,6 +123,9 @@ class ClickHouse(Dialect): "DATEDIFF": lambda args: exp.DateDiff( this=seq_get(args, 2), expression=seq_get(args, 1), unit=seq_get(args, 0) ), + "JSONEXTRACTSTRING": parse_json_extract_path( + exp.JSONExtractScalar, zero_based_indexing=False + ), "MAP": parse_var_map, "MATCH": exp.RegexpLike.from_arg_list, "RANDCANONICAL": exp.Rand.from_arg_list, @@ -354,9 +360,14 @@ class ClickHouse(Dialect): joins: bool = False, alias_tokens: t.Optional[t.Collection[TokenType]] = None, parse_bracket: bool = False, + is_db_reference: bool = False, ) -> t.Optional[exp.Expression]: this = super()._parse_table( - schema=schema, joins=joins, alias_tokens=alias_tokens, parse_bracket=parse_bracket + schema=schema, + joins=joins, + alias_tokens=alias_tokens, + parse_bracket=parse_bracket, + is_db_reference=is_db_reference, ) if self._match(TokenType.FINAL): @@ -518,6 +529,12 @@ class ClickHouse(Dialect): exp.DataType.Type.VARCHAR: "String", } + SUPPORTED_JSON_PATH_PARTS = { + exp.JSONPathKey, + exp.JSONPathRoot, + exp.JSONPathSubscript, + } + TYPE_MAPPING = { **generator.Generator.TYPE_MAPPING, **STRING_TYPE_MAPPING, @@ -570,6 +587,10 @@ class ClickHouse(Dialect): exp.Explode: rename_func("arrayJoin"), exp.Final: lambda self, e: f"{self.sql(e, 'this')} FINAL", exp.IsNan: rename_func("isNaN"), + exp.JSONExtract: json_extract_segments("JSONExtractString", quoted_index=False), + exp.JSONExtractScalar: json_extract_segments("JSONExtractString", quoted_index=False), + exp.JSONPathKey: json_path_key_only_name, + exp.JSONPathRoot: lambda *_: "", 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')}", @@ -579,7 +600,8 @@ class ClickHouse(Dialect): exp.Rand: rename_func("randCanonical"), exp.Select: transforms.preprocess([transforms.eliminate_qualify]), exp.StartsWith: rename_func("startsWith"), - exp.StrPosition: lambda self, e: f"position({self.format_args(e.this, e.args.get('substr'), e.args.get('position'))})", + exp.StrPosition: lambda self, + e: f"position({self.format_args(e.this, e.args.get('substr'), e.args.get('position'))})", 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), } @@ -608,6 +630,13 @@ class ClickHouse(Dialect): "NAMED COLLECTION", } + def _jsonpathsubscript_sql(self, expression: exp.JSONPathSubscript) -> str: + this = self.json_path_part(expression.this) + return str(int(this) + 1) if is_int(this) else this + + def likeproperty_sql(self, expression: exp.LikeProperty) -> str: + return f"AS {self.sql(expression, 'this')}" + def _any_to_has( self, expression: exp.EQ | exp.NEQ, |