Support for Multiple Actions in MERGE and Column-Specific Updates
Describe the feature
Currently, in dbt-databricks v1.9.7, the incremental merge strategy does not fully leverage the flexibility of Databricks' MERGE INTO statement.
According to the Databricks documentation, it is possible to:
- Define multiple MATCHED conditions with different actions (e.g., UPDATE, DELETE), each with its own condition.
- Define multiple NOT MATCHED BY SOURCE conditions with different actions (e.g., DELETE, UPDATE).
- Specify which columns to update in an UPDATE action instead of updating all columns by default.
However, in dbt-databricks v1.9.7, these capabilities are not fully supported:
It is not possible to specify multiple MATCHED or NOT MATCHED BY SOURCE actions. The action for MATCHED is not configurable. We cannot specify which columns should be updated in an UPDATE action.
Describe alternatives you've considered
Manually implementing a MERGE INTO statement using a custom post-hook, but this reduces the benefits of dbt’s incremental logic and maintainability. Using a two-step process: updating/deleting records separately by 2 merging, but this introduces additional complexity and performance overhead.
Additional context
Databricks supports defining multiple conditions within MERGE INTO, making it more flexible for incremental processing. Supporting these features in dbt-databricks would allow users to take full advantage of this flexibility and optimize their data pipelines.
Who will this benefit?
Data engineers & analysts using incremental models in dbt-databricks. Use cases where for example:
- Different update conditions need to be applied to different rows.
- Certain rows should be deleted instead of updated based on specific criteria.
- Only specific columns should be updated to prevent unnecessary writes.
Are you interested in contributing this feature?
The configurations could be like this: matched_delete_condition matched_update_condition not_matched_by_source_delete_condition not_matched_by_source_update_condition
@benc-db same issue for me, it would be really nice to have this feature within dbt!
Absolutely agree and support this request! It will be solve a lot of problems in the delete actions.
@mi-volodin is this truly missing or have I not documented appropriately?
@benc-db
TL;DR - it's truly missing.
Here's my rationale. From the docs we have the following description for the allowed blocks
WHEN MATCHED [ AND matched_condition ] THEN matched_action |
WHEN NOT MATCHED [BY TARGET] [ AND not_matched_condition ] THEN not_matched_action |
WHEN NOT MATCHED BY SOURCE [ AND not_matched_by_source_condition ] THEN not_matched_by_source_action }
First, something that stemmed from prior-1.9 implementation was that
- When
matchedit is always assumed to be update. - When
not matchedit is alwaysinsert.
That is easy to fix.
The not matched by source allows now both deletes and updates, so that's also kind of available already.
From this part
Define multiple MATCHED conditions with different actions (e.g., UPDATE, DELETE), each with its own condition.
I assume that the following is meant
...
when matched and condition1 then delete
when matched and condition2 then update ...
...
I can take a look into it next week.
@liangonli @mbahra @oberalppaolof
Meanwhile - it would be great, if you give some real-case example. Likely it will help with the testing. Like, some example (maybe made up) dataset and the logic that you are covering with multiple actions.
Thanks in advance.
I have a real case that, with the current version 1.9, I can only solve with a delete via post-hook... Change data feed: my source table is the result of ‘select * from table_changes(...)’. What I can't do is to define a matching condition “s._change_type = ‘delete’” and as action ”delete” At the same time define add a matching condition “s.attribx != t.attribx” with action “update”. This way I would perform a single commit and it would be more performant.
I hope my real case could help you. Thanks
Thank you for your answers.
@mi-volodin @benc-db our real use case is too complex to describe here, but here are 2 very simplified examples:
🔹 Example 1
Target table (target):
| id | color |
|---|---|
| 1 | blue |
| 2 | red |
New records (source):
| id | color |
|---|---|
| 1 | purple |
| 2 | null |
| 3 | green |
| 4 | null |
Expected output:
We want to:
- Update the color if it's different from the target table (id = 1)
- Delete the row from the target table if
coloris null in the new records (id = 2) - Insert rows whose id is not in the target table only if
coloris not null (id = 3 and 4)
| id | color |
|---|---|
| 1 | purple |
| 3 | green |
Configuration example:
unique_key: id
matched_delete_condition: source.color is null
matched_update_condition: source.color != target.color
not_matched_condition: source.color is not null
This only allows a single matched_update_condition - matched_update_action pair.
To handle multiple such rules, we would need support for a list of dictionaries, for example:
unique_key: id
matched:
- matched_condition: source.color is null
matched_action: DELETE # delete row
- matched_condition: source.color != target.color
matched_action: UPDATE # update all columns
not_matched_condition: source.color is not null
not_matched_action: INSERT *
Suggested config structure for matched:
matched:
- matched_condition: <condition or null>
matched_action: UPDATE or DELETE or null (default: UPDATE)
merge_update_columns: <list of columns or null> (default: "*")
merge_exclude_columns: <list of columns or null>
...
🔹 Example 2
Target table (target):
| id | color | date | is_soft_deleted |
|---|---|---|---|
| 1 | blue | 2025-01-01 | false |
| 2 | red | 2010-01-01 | false |
New records (source):
| id | color | date | is_soft_deleted |
|---|---|---|---|
| 3 | green | 2025-01-02 | false |
Expected output:
We want to:
- Update rows not in source with
SET is_soft_deleted = trueifcurrent_date - date <= 10 years(id = 1) - Delete rows not in source if
current_date - date > 10 years(id = 2) - Insert rows not in target table (id = 3)
| id | color | date | is_soft_deleted |
|---|---|---|---|
| 1 | blue | 2025-01-01 | true |
| 3 | green | 2025-01-02 | false |
Configuration example:
unique_key: id
not_matched_by_source_delete_condition: current_date - date > 10 years
not_matched_by_source_update_condition: current_date - date <= 10 years
not_matched_by_source_update_action: SET is_soft_deleted = true
Again, we can only handle one not_matched_by_source_update_condition - not_matched_by_source_update_action pair.
We would need support for a list of dictionaries like:
unique_key: id
not_matched_by_source:
- not_matched_by_source_condition: current_date - date > 10 years
not_matched_by_source_action: DELETE
- not_matched_by_source_condition: current_date - date <= 10 years
not_matched_by_source_action: UPDATE SET is_soft_deleted = true
Suggested config structure for not_matched_by_source:
not_matched_by_source:
- not_matched_by_source_condition: <condition or null>
not_matched_by_source_action: UPDATE EXPRESSION or DELETE or null (default: DELETE)
...
Would also love to see this feature implemented!
Thanks for the examples.
Do the answers above meet your needs ? @mi-volodin @benc-db
Thanks for the examples provided. @mbahra Thank you for the config proposal. I need to look more carefully how to make it backwards compatible.
I am not sure I like the proposed dicts though, looks a little bulky. But I'll build on top of your proposal and will try to look into something like this first
not_matched_by_source:
- condition: current_date - date > 10 years
action: DELETE
- condition: current_date - date <= 10 years
action: UPDATE ...
I would like to just have arrays, but maybe it will be too error-prone.
Ugh... ok. I looked into the keys that are used for configuration and sorted them as follows
.
├── (general config keys)
│ ├── target_alias
│ ├── source_alias
│ ├── incremental_predicates
│ ├── source_columns
│ ├── merge_with_schema_evolution
│ ├── on_schema_change (default == 'ignore')
│ ├── skip_matched_step
│ ├── skip_not_matched_step
│ └── .
├── (used in matched)
│ ├── skip_matched_step
│ ├── matched_condition
│ ├── update_columns <-- func
│ │ ├── merge_update_columns
│ │ ├── merge_exclude_columns
│ │ └── dest_columns
│ └── .
├── (used in not_matched)
│ ├── skip_not_matched_step
│ ├── not_matched_condition
│ ├── merge_insert_statement <-- func
│ │ ├── on_schema_change
│ │ ├── source_columns
│ │ └── source_alias
├── (used in not_matched_by_source)
│ ├── not_matched_by_source_action
│ ├── not_matched_by_source_condition
This is heavily used, and I see that we again hit the issue of backwards compatibility, as with skip_*_step flags which applies only for matched/non_matched.
Adding the new options complicates the configuration even more. Since we have to introduce arrays, that will be a separate groups of keys. Based on the SQL definition, it doesn't make sense to have this for not matched case, as there's a single possible action provided at the moment.
So, first draft of how we can control the further extension is (* - optional)
└── (new keys)
├── matched_actions: list(map<>)
│ ├── action: set{'upate', 'delete'}
│ ├── *condition: str
│ ├── *update_set_explicit: str -- e.g. "update t.a = s.a", if set - columns lists are ignored.
│ ├── *update_set_columns: list[str]
│ ├── *update_exclude_columns: list[str]
├── ??? not_matched_actions (single possible action, thus redundant )
└── not_matched_by_source_actions: list(map<>)
├── action: str
└── *condition: str
@benc-db what do you think? There's also an option actually to just implement a custom incremental strategy. It can be really a competitive option here.
If we were to create a new strategy, what would we call it? I understand that this is making the config extremely complicated, but since it is still just part of the Databricks merge operation, I think we should just expand merge unless there is a compelling name that makes it obvious that one is doing something different than the other. One thought I had was that instead of having every option be specified as its own thing, maybe we could have an alternate config mechanism that just takes the entire clauses as user provided strings for these more complex use cases? Kind of like providing custom constraints where the user just writes whatever expression they want, and dbt-databricks just pastes the whole thing in.
TBH - I always wanted it. It's much easier to just maintain a SQL expression for merge, than all this cryptic keys. At least when it comes to that level of heterogeneity.
Ok, I am committing to deliver it. Hope it won't take too much time 😄