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/redshift.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/redshift.py')
-rw-r--r-- | sqlglot/dialects/redshift.py | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/sqlglot/dialects/redshift.py b/sqlglot/dialects/redshift.py index 7194d81..a64c1d4 100644 --- a/sqlglot/dialects/redshift.py +++ b/sqlglot/dialects/redshift.py @@ -9,6 +9,7 @@ from sqlglot.dialects.dialect import ( concat_ws_to_dpipe_sql, date_delta_sql, generatedasidentitycolumnconstraint_sql, + json_extract_segments, no_tablesample_sql, rename_func, ) @@ -20,10 +21,6 @@ if t.TYPE_CHECKING: from sqlglot._typing import E -def _json_sql(self: Redshift.Generator, expression: exp.JSONExtract | exp.JSONExtractScalar) -> str: - return f'{self.sql(expression, "this")}."{expression.expression.name}"' - - def _parse_date_delta(expr_type: t.Type[E]) -> t.Callable[[t.List], E]: def _parse_delta(args: t.List) -> E: expr = expr_type(this=seq_get(args, 2), expression=seq_get(args, 1), unit=seq_get(args, 0)) @@ -62,6 +59,7 @@ class Redshift(Postgres): "DATE_ADD": _parse_date_delta(exp.TsOrDsAdd), "DATEDIFF": _parse_date_delta(exp.TsOrDsDiff), "DATE_DIFF": _parse_date_delta(exp.TsOrDsDiff), + "GETDATE": exp.CurrentTimestamp.from_arg_list, "LISTAGG": exp.GroupConcat.from_arg_list, "STRTOL": exp.FromBase.from_arg_list, } @@ -69,6 +67,7 @@ class Redshift(Postgres): NO_PAREN_FUNCTION_PARSERS = { **Postgres.Parser.NO_PAREN_FUNCTION_PARSERS, "APPROXIMATE": lambda self: self._parse_approximate_count(), + "SYSDATE": lambda self: self.expression(exp.CurrentTimestamp, transaction=True), } def _parse_table( @@ -77,6 +76,7 @@ class Redshift(Postgres): joins: bool = False, alias_tokens: t.Optional[t.Collection[TokenType]] = None, parse_bracket: bool = False, + is_db_reference: bool = False, ) -> t.Optional[exp.Expression]: # Redshift supports UNPIVOTing SUPER objects, e.g. `UNPIVOT foo.obj[0] AS val AT attr` unpivot = self._match(TokenType.UNPIVOT) @@ -85,6 +85,7 @@ class Redshift(Postgres): joins=joins, alias_tokens=alias_tokens, parse_bracket=parse_bracket, + is_db_reference=is_db_reference, ) return self.expression(exp.Pivot, this=table, unpivot=True) if unpivot else table @@ -153,7 +154,6 @@ class Redshift(Postgres): **Postgres.Tokenizer.KEYWORDS, "HLLSKETCH": TokenType.HLLSKETCH, "SUPER": TokenType.SUPER, - "SYSDATE": TokenType.CURRENT_TIMESTAMP, "TOP": TokenType.TOP, "UNLOAD": TokenType.COMMAND, "VARBYTE": TokenType.VARBINARY, @@ -180,31 +180,29 @@ class Redshift(Postgres): exp.DataType.Type.VARBINARY: "VARBYTE", } - PROPERTIES_LOCATION = { - **Postgres.Generator.PROPERTIES_LOCATION, - exp.LikeProperty: exp.Properties.Location.POST_WITH, - } - TRANSFORMS = { **Postgres.Generator.TRANSFORMS, exp.Concat: concat_to_dpipe_sql, exp.ConcatWs: concat_ws_to_dpipe_sql, - exp.ApproxDistinct: lambda self, e: f"APPROXIMATE COUNT(DISTINCT {self.sql(e, 'this')})", - exp.CurrentTimestamp: lambda self, e: "SYSDATE", + exp.ApproxDistinct: lambda self, + e: f"APPROXIMATE COUNT(DISTINCT {self.sql(e, 'this')})", + exp.CurrentTimestamp: lambda self, e: ( + "SYSDATE" if e.args.get("transaction") else "GETDATE()" + ), exp.DateAdd: date_delta_sql("DATEADD"), exp.DateDiff: date_delta_sql("DATEDIFF"), exp.DistKeyProperty: lambda self, e: f"DISTKEY({e.name})", exp.DistStyleProperty: lambda self, e: self.naked_property(e), exp.FromBase: rename_func("STRTOL"), exp.GeneratedAsIdentityColumnConstraint: generatedasidentitycolumnconstraint_sql, - exp.JSONExtract: _json_sql, - exp.JSONExtractScalar: _json_sql, + exp.JSONExtract: json_extract_segments("JSON_EXTRACT_PATH_TEXT"), exp.GroupConcat: rename_func("LISTAGG"), exp.ParseJSON: rename_func("JSON_PARSE"), exp.Select: transforms.preprocess( [transforms.eliminate_distinct_on, transforms.eliminate_semi_and_anti_joins] ), - exp.SortKeyProperty: lambda self, e: f"{'COMPOUND ' if e.args['compound'] else ''}SORTKEY({self.format_args(*e.this)})", + exp.SortKeyProperty: lambda self, + e: f"{'COMPOUND ' if e.args['compound'] else ''}SORTKEY({self.format_args(*e.this)})", exp.TableSample: no_tablesample_sql, exp.TsOrDsAdd: date_delta_sql("DATEADD"), exp.TsOrDsDiff: date_delta_sql("DATEDIFF"), @@ -228,6 +226,13 @@ class Redshift(Postgres): """Redshift doesn't have `WITH` as part of their with_properties so we remove it""" return self.properties(properties, prefix=" ", suffix="") + def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str: + if expression.is_type(exp.DataType.Type.JSON): + # Redshift doesn't support a JSON type, so casting to it is treated as a noop + return self.sql(expression, "this") + + return super().cast_sql(expression, safe_prefix=safe_prefix) + def datatype_sql(self, expression: exp.DataType) -> str: """ Redshift converts the `TEXT` data type to `VARCHAR(255)` by default when people more generally mean |