Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix!: exp.Merge condition for Trino/Postgres #4596

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1570,19 +1570,25 @@ def normalize(identifier: t.Optional[exp.Identifier]) -> t.Optional[str]:
targets.add(normalize(alias.this))

for when in expression.args["whens"].expressions:
# only remove the target names from the THEN clause
# theyre still valid in the <condition> part of WHEN MATCHED / WHEN NOT MATCHED
# ref: https://github.com/TobikoData/sqlmesh/issues/2934
then = when.args.get("then")
# only remove the target table names from certain parts of WHEN MATCHED / WHEN NOT MATCHED
# they are still valid in the <condition>, the right hand side of each UPDATE and the VALUES part
# (not the column list) of the INSERT
then: exp.Insert | exp.Update | None = when.args.get("then")
if then:
then.transform(
lambda node: (
exp.column(node.this)
if isinstance(node, exp.Column) and normalize(node.args.get("table")) in targets
else node
),
copy=False,
)
if isinstance(then, exp.Update):
for equals in then.find_all(exp.EQ):
equal_lhs = equals.this
if (
isinstance(equal_lhs, exp.Column)
and normalize(equal_lhs.args.get("table")) in targets
):
equal_lhs.replace(exp.column(equal_lhs.this))
if isinstance(then, exp.Insert):
column_list = then.args.get("this")
if column_list is not None:
for column in column_list.find_all(exp.Column):
georgesittas marked this conversation as resolved.
Show resolved Hide resolved
if normalize(column.args.get("table")) in targets:
column.replace(exp.column(column.this))

return self.merge_sql(expression)

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