summaryrefslogtreecommitdiffstats
path: root/sqlglot/dialects/postgres.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-07-06 07:28:12 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-07-06 07:28:12 +0000
commit374a0f6318bcf423b1b784d30b25a8327c65cb24 (patch)
tree9303a1cbdba85b5d9781ebef32eb1902d3790c99 /sqlglot/dialects/postgres.py
parentReleasing debian version 16.7.7-1. (diff)
downloadsqlglot-374a0f6318bcf423b1b784d30b25a8327c65cb24.tar.xz
sqlglot-374a0f6318bcf423b1b784d30b25a8327c65cb24.zip
Merging upstream version 17.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.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py
index 766b584..6d78a07 100644
--- a/sqlglot/dialects/postgres.py
+++ b/sqlglot/dialects/postgres.py
@@ -183,6 +183,29 @@ def _to_timestamp(args: t.List) -> exp.Expression:
return format_time_lambda(exp.StrToTime, "postgres")(args)
+def _remove_target_from_merge(expression: exp.Expression) -> exp.Expression:
+ """Remove table refs from columns in when statements."""
+ if isinstance(expression, exp.Merge):
+ alias = expression.this.args.get("alias")
+
+ normalize = lambda identifier: Postgres.normalize_identifier(identifier).name
+
+ targets = {normalize(expression.this.this)}
+
+ if alias:
+ targets.add(normalize(alias.this))
+
+ for when in expression.expressions:
+ when.transform(
+ lambda node: exp.column(node.name)
+ if isinstance(node, exp.Column) and normalize(node.args.get("table")) in targets
+ else node,
+ copy=False,
+ )
+
+ return expression
+
+
class Postgres(Dialect):
INDEX_OFFSET = 1
NULL_ORDERING = "nulls_are_large"
@@ -315,6 +338,7 @@ class Postgres(Dialect):
LOCKING_READS_SUPPORTED = True
JOIN_HINTS = False
TABLE_HINTS = False
+ QUERY_HINTS = False
PARAMETER_TOKEN = "$"
TYPE_MAPPING = {
@@ -352,7 +376,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: transforms.preprocess([transforms.remove_target_from_merge]),
+ exp.Merge: transforms.preprocess([_remove_target_from_merge]),
exp.Pivot: no_pivot_sql,
exp.RegexpLike: lambda self, e: self.binary(e, "~"),
exp.RegexpILike: lambda self, e: self.binary(e, "~*"),