diff options
Diffstat (limited to 'sqlglot/dialects/postgres.py')
-rw-r--r-- | sqlglot/dialects/postgres.py | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py index 31b7e45..c47ff51 100644 --- a/sqlglot/dialects/postgres.py +++ b/sqlglot/dialects/postgres.py @@ -5,6 +5,7 @@ from sqlglot.dialects.dialect import ( Dialect, arrow_json_extract_scalar_sql, arrow_json_extract_sql, + datestrtodate_sql, format_time_lambda, max_or_greatest, min_or_least, @@ -19,7 +20,7 @@ from sqlglot.dialects.dialect import ( from sqlglot.helper import seq_get from sqlglot.parser import binary_range_parser from sqlglot.tokens import TokenType -from sqlglot.transforms import delegate, preprocess +from sqlglot.transforms import preprocess, remove_target_from_merge DATE_DIFF_FACTOR = { "MICROSECOND": " * 1000000", @@ -239,7 +240,6 @@ class Postgres(Dialect): "SERIAL": TokenType.SERIAL, "SMALLSERIAL": TokenType.SMALLSERIAL, "TEMP": TokenType.TEMPORARY, - "UUID": TokenType.UUID, "CSTRING": TokenType.PSEUDO_TYPE, } @@ -248,18 +248,25 @@ class Postgres(Dialect): "$": TokenType.PARAMETER, } + VAR_SINGLE_TOKENS = {"$"} + class Parser(parser.Parser): STRICT_CAST = False FUNCTIONS = { **parser.Parser.FUNCTIONS, # type: ignore - "NOW": exp.CurrentTimestamp.from_arg_list, - "TO_TIMESTAMP": _to_timestamp, - "TO_CHAR": format_time_lambda(exp.TimeToStr, "postgres"), - "GENERATE_SERIES": _generate_series, "DATE_TRUNC": lambda args: exp.TimestampTrunc( this=seq_get(args, 1), unit=seq_get(args, 0) ), + "GENERATE_SERIES": _generate_series, + "NOW": exp.CurrentTimestamp.from_arg_list, + "TO_CHAR": format_time_lambda(exp.TimeToStr, "postgres"), + "TO_TIMESTAMP": _to_timestamp, + } + + FUNCTION_PARSERS = { + **parser.Parser.FUNCTION_PARSERS, + "DATE_PART": lambda self: self._parse_date_part(), } BITWISE = { @@ -279,8 +286,21 @@ class Postgres(Dialect): TokenType.LT_AT: binary_range_parser(exp.ArrayContained), } + def _parse_date_part(self) -> exp.Expression: + part = self._parse_type() + self._match(TokenType.COMMA) + value = self._parse_bitwise() + + if part and part.is_string: + part = exp.Var(this=part.name) + + return self.expression(exp.Extract, this=part, expression=value) + class Generator(generator.Generator): + INTERVAL_ALLOWS_PLURAL_FORM = False LOCKING_READS_SUPPORTED = True + JOIN_HINTS = False + TABLE_HINTS = False PARAMETER_TOKEN = "$" TYPE_MAPPING = { @@ -301,7 +321,6 @@ class Postgres(Dialect): _auto_increment_to_serial, _serial_to_generated, ], - delegate("columndef_sql"), ), exp.JSONExtract: arrow_json_extract_sql, exp.JSONExtractScalar: arrow_json_extract_scalar_sql, @@ -312,6 +331,7 @@ class Postgres(Dialect): exp.CurrentDate: no_paren_current_date_sql, exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP", exp.DateAdd: _date_add_sql("+"), + exp.DateStrToDate: datestrtodate_sql, exp.DateSub: _date_add_sql("-"), exp.DateDiff: _date_diff_sql, exp.LogicalOr: rename_func("BOOL_OR"), @@ -321,6 +341,7 @@ class Postgres(Dialect): exp.ArrayOverlaps: lambda self, e: self.binary(e, "&&"), exp.ArrayContains: lambda self, e: self.binary(e, "@>"), exp.ArrayContained: lambda self, e: self.binary(e, "<@"), + exp.Merge: preprocess([remove_target_from_merge]), exp.RegexpLike: lambda self, e: self.binary(e, "~"), exp.RegexpILike: lambda self, e: self.binary(e, "~*"), exp.StrPosition: str_position_sql, @@ -344,4 +365,5 @@ class Postgres(Dialect): PROPERTIES_LOCATION = { **generator.Generator.PROPERTIES_LOCATION, # type: ignore exp.TransientProperty: exp.Properties.Location.UNSUPPORTED, + exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED, } |