Merging upstream version 20.1.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
d4fe7bdb16
commit
90988d8258
127 changed files with 73384 additions and 73067 deletions
|
@ -4,8 +4,10 @@ import typing as t
|
|||
|
||||
from sqlglot import exp, transforms
|
||||
from sqlglot.dialects.dialect import (
|
||||
NormalizationStrategy,
|
||||
concat_to_dpipe_sql,
|
||||
concat_ws_to_dpipe_sql,
|
||||
date_delta_sql,
|
||||
generatedasidentitycolumnconstraint_sql,
|
||||
rename_func,
|
||||
ts_or_ds_to_date_sql,
|
||||
|
@ -14,30 +16,28 @@ from sqlglot.dialects.postgres import Postgres
|
|||
from sqlglot.helper import seq_get
|
||||
from sqlglot.tokens import TokenType
|
||||
|
||||
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_add(args: t.List) -> exp.DateAdd:
|
||||
return exp.DateAdd(
|
||||
this=exp.TsOrDsToDate(this=seq_get(args, 2)),
|
||||
expression=seq_get(args, 1),
|
||||
unit=seq_get(args, 0),
|
||||
)
|
||||
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))
|
||||
if expr_type is exp.TsOrDsAdd:
|
||||
expr.set("return_type", exp.DataType.build("TIMESTAMP"))
|
||||
|
||||
return expr
|
||||
|
||||
def _parse_datediff(args: t.List) -> exp.DateDiff:
|
||||
return exp.DateDiff(
|
||||
this=exp.TsOrDsToDate(this=seq_get(args, 2)),
|
||||
expression=exp.TsOrDsToDate(this=seq_get(args, 1)),
|
||||
unit=seq_get(args, 0),
|
||||
)
|
||||
return _parse_delta
|
||||
|
||||
|
||||
class Redshift(Postgres):
|
||||
# https://docs.aws.amazon.com/redshift/latest/dg/r_names.html
|
||||
RESOLVES_IDENTIFIERS_AS_UPPERCASE = None
|
||||
NORMALIZATION_STRATEGY = NormalizationStrategy.CASE_INSENSITIVE
|
||||
|
||||
SUPPORTS_USER_DEFINED_TYPES = False
|
||||
INDEX_OFFSET = 0
|
||||
|
@ -52,15 +52,16 @@ class Redshift(Postgres):
|
|||
class Parser(Postgres.Parser):
|
||||
FUNCTIONS = {
|
||||
**Postgres.Parser.FUNCTIONS,
|
||||
"ADD_MONTHS": lambda args: exp.DateAdd(
|
||||
this=exp.TsOrDsToDate(this=seq_get(args, 0)),
|
||||
"ADD_MONTHS": lambda args: exp.TsOrDsAdd(
|
||||
this=seq_get(args, 0),
|
||||
expression=seq_get(args, 1),
|
||||
unit=exp.var("month"),
|
||||
return_type=exp.DataType.build("TIMESTAMP"),
|
||||
),
|
||||
"DATEADD": _parse_date_add,
|
||||
"DATE_ADD": _parse_date_add,
|
||||
"DATEDIFF": _parse_datediff,
|
||||
"DATE_DIFF": _parse_datediff,
|
||||
"DATEADD": _parse_date_delta(exp.TsOrDsAdd),
|
||||
"DATE_ADD": _parse_date_delta(exp.TsOrDsAdd),
|
||||
"DATEDIFF": _parse_date_delta(exp.TsOrDsDiff),
|
||||
"DATE_DIFF": _parse_date_delta(exp.TsOrDsDiff),
|
||||
"LISTAGG": exp.GroupConcat.from_arg_list,
|
||||
"STRTOL": exp.FromBase.from_arg_list,
|
||||
}
|
||||
|
@ -169,12 +170,8 @@ class Redshift(Postgres):
|
|||
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.DateAdd: lambda self, e: self.func(
|
||||
"DATEADD", exp.var(e.text("unit") or "day"), e.expression, e.this
|
||||
),
|
||||
exp.DateDiff: lambda self, e: self.func(
|
||||
"DATEDIFF", exp.var(e.text("unit") or "day"), e.expression, e.this
|
||||
),
|
||||
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"),
|
||||
|
@ -183,11 +180,12 @@ class Redshift(Postgres):
|
|||
exp.JSONExtractScalar: _json_sql,
|
||||
exp.GroupConcat: rename_func("LISTAGG"),
|
||||
exp.ParseJSON: rename_func("JSON_PARSE"),
|
||||
exp.SafeConcat: concat_to_dpipe_sql,
|
||||
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.TsOrDsAdd: date_delta_sql("DATEADD"),
|
||||
exp.TsOrDsDiff: date_delta_sql("DATEDIFF"),
|
||||
exp.TsOrDsToDate: ts_or_ds_to_date_sql("redshift"),
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue