Skip to content

fix(engine_adapter): put the merge insert before NOT MATCHED BY SOURCE - #6111

Open
manan28025 wants to merge 1 commit into
SQLMesh:mainfrom
manan28025:merge-not-matched-by-source
Open

manan28025 wants to merge 1 commit into
SQLMesh:mainfrom
manan28025:merge-not-matched-by-source

Conversation

@manan28025

Copy link
Copy Markdown
Contributor

Description

Fixes #5355.

EngineAdapter.merge() always appends its WHEN NOT MATCHED THEN INSERT clause after the when_matched clauses. If those include WHEN NOT MATCHED BY SOURCE, the insert ends up after it. Databricks requires the order WHEN MATCHED, WHEN NOT MATCHED [BY TARGET], WHEN NOT MATCHED BY SOURCE, so the generated MERGE fails with a syntax error.

The insert now goes in front of the first WHEN NOT MATCHED BY SOURCE clause. Without such a clause it is still appended last, so other generated SQL doesn't change.

Test Plan

  • Added test_merge_when_not_matched_by_source. It fails on main (the insert comes after the BY SOURCE clause) and passes with the fix.
  • Ran tests/core/engine_adapter (including the Spark and Databricks tests) and tests/core/test_snapshot_evaluator.py.

Checklist

  • I have run make style and fixed any issues (ruff and ruff-format pass)
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test) (ran the engine adapter and snapshot evaluator suites)
  • My commits are signed off (git commit -s) per the DCO

merge() always appended its WHEN NOT MATCHED THEN INSERT clause after the
when_matched clauses. If those include WHEN NOT MATCHED BY SOURCE, the
insert ended up after it, which Databricks rejects because it requires
WHEN MATCHED, WHEN NOT MATCHED [BY TARGET], WHEN NOT MATCHED BY SOURCE
in that order. The insert now goes before the first BY SOURCE clause.

Signed-off-by: neatninja <manan81140@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Base engine adapter's merge method does not support WHEN NOT MATCHED BY SOURCE in Databricks

2 participants