Repository navigation
feat(duckdb): use native MERGE for incremental by unique key models #6216
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
Open
Vamsi-klu
wants to merge
3
commits into
SQLMesh:main
Choose a base branch
from
Vamsi-klu:feat/4985-duckdb-native-merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
84af4c2
feat(duckdb): use native MERGE for incremental by unique key models
Vamsi-klu 195a384
feat(duckdb): native MERGE improvements for catalog gating, type coer…
Vamsi-klu 127a9dd
fix(duckdb): resolve 2-part catalog targets and use SELECT * REPLACE …
Vamsi-klu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve matching for nullable composite keys.
With
unique_key=(id, name), an existing row(1, NULL, 10)and source row(1, NULL, 11)previously produced one updated row._replace_by_keyusesCONCAT_WSfor composite keys (base.py, Line 3013), which skips NULL arguments. (duckdb.org)The native path instead uses the base adapter's per-column equality predicates.
NULL = NULLdoes not match, so each evaluation inserts another(1, NULL, ...)row. Source deduplication does not remove existing target rows. (duckdb.org)Preserve the previous matching behavior for nullable composite keys, or retain the logical path for those models.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in commit 195a384 via Option A (Standard SQL
=Equality Semantics):EXPLAINprofiling on DuckDB 1.4+ and DuckLake confirms that standard SQL=equality predicates inMERGE ... ON target.k = source.kgenerateDYNAMIC_FILTERpushdown directly intoSEQ_SCANover Parquet/DuckLake zone maps (filtering matching min/max row groups at scan time). Replacing=withIS NOT DISTINCT FROMcompletely disables dynamic join filters and zone-map pruning in DuckDB's optimizer, forcing full physical table scans.=aligns DuckDB with all other nativeMERGEadapters in SQLMesh (Postgres >= 15, Snowflake, BigQuery, Databricks, Spark, Redshift, Trino, MSSQL, MySQL; seebase.py:2272andmysql.py:227). The legacy logical merge behavior was an artifact ofCONCAT_WS('__SQLMESH_DELIM__', k1, k2), which silently treated(1, NULL)and(NULL, 1)as identical'1'keys, introducing silent collisions.docs/integrations/engines/duckdb.mdanddocs/concepts/models/model_kinds.mdto document standard SQL=null-handling on nativeMERGE, advising users requiring null-safe composite key matching to define explicit sentinel expressions (e.g.unique_key=[COALESCE(k1, '<sentinel>'), k2]).tests/core/engine_adapter/test_duckdb.pyvalidating standard composite null non-matching without synthetic collisions, as well as nativeMERGEexecution withCOALESCEexpressions inunique_key.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Analysis chain
Script executed:
Length of output: 25976
Script executed:
Length of output: 10558
Script executed:
#!/bin/bash cat -n CLAUDE.mdLength of output: 16939
@Vamsi-kluThanks for explaining the tradeoff. I see the documentation and tests for=andCOALESCEmatching.I acknowledge the decision to retain
=. The change does not preserve previous matching for nullable composite keys, so I’m not marking that compatibility finding as addressed.You are interacting with an AI system.