Skip to content

Commit

Permalink
Fix!: exp.Merge condition for Trino/Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeWallis42 committed Jan 10, 2025
1 parent eb04a1f commit 697d2f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,11 @@ def normalize(identifier: t.Optional[exp.Identifier]) -> t.Optional[str]:
then.transform(
lambda node: (
exp.column(node.this)
if isinstance(node, exp.Column) and normalize(node.args.get("table")) in targets
if (
isinstance(node, exp.Column)
and normalize(node.args.get("table")) in targets
and not isinstance(node.parent, exp.Func)
)
else node
),
copy=False,
Expand Down
11 changes: 11 additions & 0 deletions tests/dialects/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2336,6 +2336,17 @@ def test_merge(self):
},
)

# needs to preserve the target alias in then WHEN condition and function but not in the THEN clause
self.validate_all(
"""MERGE INTO foo AS target USING (SELECT a, b FROM tbl) AS src ON src.a = target.a
WHEN MATCHED THEN UPDATE SET target.b = COALESCE(src.b, target.b)
WHEN NOT MATCHED THEN INSERT (target.a, target.b) VALUES (src.a, src.b)""",
write={
"trino": """MERGE INTO foo AS target USING (SELECT a, b FROM tbl) AS src ON src.a = target.a WHEN MATCHED THEN UPDATE SET b = COALESCE(src.b, target.b) WHEN NOT MATCHED THEN INSERT (a, b) VALUES (src.a, src.b)""",
"postgres": """MERGE INTO foo AS target USING (SELECT a, b FROM tbl) AS src ON src.a = target.a WHEN MATCHED THEN UPDATE SET b = COALESCE(src.b, target.b) WHEN NOT MATCHED THEN INSERT (a, b) VALUES (src.a, src.b)""",
},
)

def test_substring(self):
self.validate_all(
"SUBSTR('123456', 2, 3)",
Expand Down

0 comments on commit 697d2f3

Please sign in to comment.