Commit 006d0c2
fix: match the whole key tuple in legacy delete+insert (#1612)
Resolves #1611
### Description
On the `delete+insert` strategy with a composite `unique_key`, the
legacy DBR < 17.1 path builds one
`IN (SELECT ...)` per key column and ANDs them, so the DELETE predicate
is the cross product of the
key columns' value sets rather than the set of key tuples. Rows whose
key tuple is absent from the
source are deleted and never re-inserted, silently.
With `unique_key: ['a', 'b']`, a target holding `(1,10) (2,20) (1,20)
(2,10)` and a source producing
only `(1,10) (2,20)`, the predicate `a IN (1,2) AND b IN (10,20)`
matches all four rows. `(1,20)` and
`(2,10)` are lost.
The DBR 17.1+ branch of the same macro is already row-wise, and
dbt-core's cross-adapter default is
`where (unique_key_str) in (select distinct unique_key_str from
source)`, so today the same model with
the same config produces different data depending on runtime version.
**Fix:** use a correlated `EXISTS` predicate that compares every key
column against the same source
row with null-safe equality.
```sql
delete from target
where exists (
select 1
from source
where target.`a` <=> source.`a`
and target.`b` <=> source.`b`
)
```
The single-key branch remains byte-for-byte identical, so its behavior
is unchanged and remains
covered by the existing single-key and non-ASCII tests.
**Note on the changed tests:**
`test_delete_insert_legacy_sql__multiple_unique_keys` previously
asserted the per-column form, so it encoded the bug and had to be
updated. Two neighbouring tests that
asserted against their own hardcoded strings are converted to real macro
renders, following your
request on #1595.
**Testing:**
- 17 focused unit tests cover composite keys, incremental predicates,
and the unchanged single-key
path.
- A functional regression forces the legacy `DELETE` + `INSERT` branch
on live Databricks and
verifies exact replacements, crossed-tuple preservation, and absent-row
preservation.
- The correlated `DELETE` statement was also run directly on DBR 16.4
LTS and produced the expected
final rows.
A full dbt end-to-end run on compute below DBR 17.1 would add
release-matrix confidence, but the
changed predicate and complete dbt execution path are covered by the
complementary checks above.
### Checklist
- [x] I have run this code in development and it appears to resolve the
stated issue
- [x] This PR includes tests, or tests are not required/relevant for
this PR
- [x] I have updated the `CHANGELOG.md` and added information about my
change to the "dbt-databricks next" section.
---------
Signed-off-by: Sreerama Yeshwanth Gowd <yeshwanthgowdsreerama@gmail.com>
Co-authored-by: Shubham Dhal <shubham.dhal@databricks.com>1 parent f170619 commit 006d0c2
5 files changed
Lines changed: 114 additions & 34 deletions
File tree
- dbt/include/databricks/macros/materializations/incremental
- tests
- functional/adapter/incremental
- unit/macros/materializations/incremental
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
Lines changed: 11 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
184 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
185 | 194 | | |
186 | | - | |
| 195 | + | |
187 | 196 | | |
188 | 197 | | |
189 | 198 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
309 | 309 | | |
310 | 310 | | |
311 | 311 | | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
312 | 319 | | |
313 | 320 | | |
314 | 321 | | |
| |||
442 | 449 | | |
443 | 450 | | |
444 | 451 | | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
445 | 494 | | |
446 | 495 | | |
447 | 496 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
451 | 451 | | |
452 | 452 | | |
453 | 453 | | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
454 | 478 | | |
455 | 479 | | |
456 | 480 | | |
| |||
Lines changed: 29 additions & 32 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
156 | | - | |
| 156 | + | |
157 | 157 | | |
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
181 | 166 | | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
189 | 184 | | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
194 | 191 | | |
195 | 192 | | |
196 | 193 | | |
| |||
0 commit comments