diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-03 14:11:07 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-03 14:11:07 +0000 |
commit | 42a1548cecf48d18233f56e3385cf9c89abcb9c2 (patch) | |
tree | 5e0fff4ecbd1fd7dd1022a7580139038df2a824c /sqlglot/dialects/postgres.py | |
parent | Releasing debian version 21.1.2-1. (diff) | |
download | sqlglot-42a1548cecf48d18233f56e3385cf9c89abcb9c2.tar.xz sqlglot-42a1548cecf48d18233f56e3385cf9c89abcb9c2.zip |
Merging upstream version 22.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 | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py index c78f8a3..b53ae07 100644 --- a/sqlglot/dialects/postgres.py +++ b/sqlglot/dialects/postgres.py @@ -138,7 +138,9 @@ def _auto_increment_to_serial(expression: exp.Expression) -> exp.Expression: def _serial_to_generated(expression: exp.Expression) -> exp.Expression: - kind = expression.args.get("kind") + if not isinstance(expression, exp.ColumnDef): + return expression + kind = expression.kind if not kind: return expression @@ -279,6 +281,7 @@ class Postgres(Dialect): "TEMP": TokenType.TEMPORARY, "CSTRING": TokenType.PSEUDO_TYPE, "OID": TokenType.OBJECT_IDENTIFIER, + "ONLY": TokenType.ONLY, "OPERATOR": TokenType.OPERATOR, "REGCLASS": TokenType.OBJECT_IDENTIFIER, "REGCOLLATION": TokenType.OBJECT_IDENTIFIER, @@ -451,6 +454,7 @@ class Postgres(Dialect): exp.JSONBExtract: lambda self, e: self.binary(e, "#>"), exp.JSONBExtractScalar: lambda self, e: self.binary(e, "#>>"), exp.JSONBContains: lambda self, e: self.binary(e, "?"), + exp.ParseJSON: lambda self, e: self.sql(exp.cast(e.this, exp.DataType.Type.JSON)), exp.JSONPathKey: json_path_key_only_name, exp.JSONPathRoot: lambda *_: "", exp.JSONPathSubscript: lambda self, e: self.json_path_part(e.this), @@ -506,6 +510,26 @@ class Postgres(Dialect): exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED, } + def unnest_sql(self, expression: exp.Unnest) -> str: + if len(expression.expressions) == 1: + from sqlglot.optimizer.annotate_types import annotate_types + + this = annotate_types(expression.expressions[0]) + if this.is_type("array<json>"): + while isinstance(this, exp.Cast): + this = this.this + + arg = self.sql(exp.cast(this, exp.DataType.Type.JSON)) + alias = self.sql(expression, "alias") + alias = f" AS {alias}" if alias else "" + + if expression.args.get("offset"): + self.unsupported("Unsupported JSON_ARRAY_ELEMENTS with offset") + + return f"JSON_ARRAY_ELEMENTS({arg}){alias}" + + return super().unnest_sql(expression) + def bracket_sql(self, expression: exp.Bracket) -> str: """Forms like ARRAY[1, 2, 3][3] aren't allowed; we need to wrap the ARRAY.""" if isinstance(expression.this, exp.Array): |