diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-09-07 11:39:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-09-07 11:39:48 +0000 |
commit | f73e9af131151f1e058446361c35b05c4c90bf10 (patch) | |
tree | ed425b89f12d3f5e4709290bdc03d876f365bc97 /sqlglot/dialects/postgres.py | |
parent | Releasing debian version 17.12.0-1. (diff) | |
download | sqlglot-f73e9af131151f1e058446361c35b05c4c90bf10.tar.xz sqlglot-f73e9af131151f1e058446361c35b05c4c90bf10.zip |
Merging upstream version 18.2.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/dialects/postgres.py')
-rw-r--r-- | sqlglot/dialects/postgres.py | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py index 73ca4e5..c26e121 100644 --- a/sqlglot/dialects/postgres.py +++ b/sqlglot/dialects/postgres.py @@ -5,6 +5,7 @@ import typing as t from sqlglot import exp, generator, parser, tokens, transforms from sqlglot.dialects.dialect import ( Dialect, + any_value_to_max_sql, arrow_json_extract_scalar_sql, arrow_json_extract_sql, datestrtodate_sql, @@ -39,8 +40,8 @@ DATE_DIFF_FACTOR = { } -def _date_add_sql(kind: str) -> t.Callable[[generator.Generator, exp.DateAdd | exp.DateSub], str]: - def func(self: generator.Generator, expression: exp.DateAdd | exp.DateSub) -> str: +def _date_add_sql(kind: str) -> t.Callable[[Postgres.Generator, exp.DateAdd | exp.DateSub], str]: + def func(self: Postgres.Generator, expression: exp.DateAdd | exp.DateSub) -> str: expression = expression.copy() this = self.sql(expression, "this") @@ -56,7 +57,7 @@ def _date_add_sql(kind: str) -> t.Callable[[generator.Generator, exp.DateAdd | e return func -def _date_diff_sql(self: generator.Generator, expression: exp.DateDiff) -> str: +def _date_diff_sql(self: Postgres.Generator, expression: exp.DateDiff) -> str: unit = expression.text("unit").upper() factor = DATE_DIFF_FACTOR.get(unit) @@ -82,7 +83,7 @@ def _date_diff_sql(self: generator.Generator, expression: exp.DateDiff) -> str: return f"CAST({unit} AS BIGINT)" -def _substring_sql(self: generator.Generator, expression: exp.Substring) -> str: +def _substring_sql(self: Postgres.Generator, expression: exp.Substring) -> str: this = self.sql(expression, "this") start = self.sql(expression, "start") length = self.sql(expression, "length") @@ -93,7 +94,7 @@ def _substring_sql(self: generator.Generator, expression: exp.Substring) -> str: return f"SUBSTRING({this}{from_part}{for_part})" -def _string_agg_sql(self: generator.Generator, expression: exp.GroupConcat) -> str: +def _string_agg_sql(self: Postgres.Generator, expression: exp.GroupConcat) -> str: expression = expression.copy() separator = expression.args.get("separator") or exp.Literal.string(",") @@ -107,7 +108,7 @@ def _string_agg_sql(self: generator.Generator, expression: exp.GroupConcat) -> s return f"STRING_AGG({self.format_args(this, separator)}{order})" -def _datatype_sql(self: generator.Generator, expression: exp.DataType) -> str: +def _datatype_sql(self: Postgres.Generator, expression: exp.DataType) -> str: if expression.is_type("array"): return f"{self.expressions(expression, flat=True)}[]" return self.datatype_sql(expression) @@ -254,6 +255,7 @@ class Postgres(Dialect): "~~*": TokenType.ILIKE, "~*": TokenType.IRLIKE, "~": TokenType.RLIKE, + "@@": TokenType.DAT, "@>": TokenType.AT_GT, "<@": TokenType.LT_AT, "BEGIN": TokenType.COMMAND, @@ -273,6 +275,18 @@ class Postgres(Dialect): "SMALLSERIAL": TokenType.SMALLSERIAL, "TEMP": TokenType.TEMPORARY, "CSTRING": TokenType.PSEUDO_TYPE, + "OID": TokenType.OBJECT_IDENTIFIER, + "REGCLASS": TokenType.OBJECT_IDENTIFIER, + "REGCOLLATION": TokenType.OBJECT_IDENTIFIER, + "REGCONFIG": TokenType.OBJECT_IDENTIFIER, + "REGDICTIONARY": TokenType.OBJECT_IDENTIFIER, + "REGNAMESPACE": TokenType.OBJECT_IDENTIFIER, + "REGOPER": TokenType.OBJECT_IDENTIFIER, + "REGOPERATOR": TokenType.OBJECT_IDENTIFIER, + "REGPROC": TokenType.OBJECT_IDENTIFIER, + "REGPROCEDURE": TokenType.OBJECT_IDENTIFIER, + "REGROLE": TokenType.OBJECT_IDENTIFIER, + "REGTYPE": TokenType.OBJECT_IDENTIFIER, } SINGLE_TOKENS = { @@ -312,6 +326,9 @@ class Postgres(Dialect): RANGE_PARSERS = { **parser.Parser.RANGE_PARSERS, TokenType.DAMP: binary_range_parser(exp.ArrayOverlaps), + TokenType.DAT: lambda self, this: self.expression( + exp.MatchAgainst, this=self._parse_bitwise(), expressions=[this] + ), TokenType.AT_GT: binary_range_parser(exp.ArrayContains), TokenType.LT_AT: binary_range_parser(exp.ArrayContained), } @@ -343,6 +360,7 @@ class Postgres(Dialect): JOIN_HINTS = False TABLE_HINTS = False QUERY_HINTS = False + NVL2_SUPPORTED = False PARAMETER_TOKEN = "$" TYPE_MAPPING = { @@ -357,6 +375,8 @@ class Postgres(Dialect): TRANSFORMS = { **generator.Generator.TRANSFORMS, + exp.AnyValue: any_value_to_max_sql, + exp.ArrayConcat: rename_func("ARRAY_CAT"), exp.BitwiseXor: lambda self, e: self.binary(e, "#"), exp.ColumnDef: transforms.preprocess([_auto_increment_to_serial, _serial_to_generated]), exp.Explode: rename_func("UNNEST"), @@ -416,3 +436,9 @@ class Postgres(Dialect): expression.set("this", exp.paren(expression.this, copy=False)) return super().bracket_sql(expression) + + def matchagainst_sql(self, expression: exp.MatchAgainst) -> str: + this = self.sql(expression, "this") + expressions = [f"{self.sql(e)} @@ {this}" for e in expression.expressions] + sql = " OR ".join(expressions) + return f"({sql})" if len(expressions) > 1 else sql |