-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This PR adds a new incremental strategy `replace_where`. The strategy resolves to an `INSERT INTO ... REPLACE WHERE` statement. It completes the feature set explained here: https://docs.databricks.com/delta/selective-overwrite.html#replace-where&language-python A lot of the code change is to bring macros from dbt-spark over to dbt-databricks. The only real code change was in validating incremental strategies and adding the replace_where strategy. #### Why do we need it? It enables use cases where part of the data is always replaced and where MERGE is not possible, such as when there is no primary key. E.g.: events table where we want to always replace the last 3 days. #### Difference from insert_overwrite Insert overwrite only works with dynamic partition pruning spark setting, which is not available in sql warehouses or any Unity Catalog-enabled cluster. It also only works with whole partitions, making it difficult to set up and assure that the correct data is dropped. Signed-off-by: Andre Furlan <[email protected]>
- Loading branch information
1 parent
f7b74e4
commit ed59030
Showing
26 changed files
with
450 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 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 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
53 changes: 53 additions & 0 deletions
53
dbt/include/databricks/macros/materializations/incremental/validate.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{% macro dbt_databricks_validate_get_file_format(raw_file_format) %} | ||
{#-- Validate the file format #} | ||
|
||
{% set accepted_formats = ['text', 'csv', 'json', 'jdbc', 'parquet', 'orc', 'hive', 'delta', 'libsvm', 'hudi'] %} | ||
|
||
{% set invalid_file_format_msg -%} | ||
Invalid file format provided: {{ raw_file_format }} | ||
Expected one of: {{ accepted_formats | join(', ') }} | ||
{%- endset %} | ||
|
||
{% if raw_file_format not in accepted_formats %} | ||
{% do exceptions.raise_compiler_error(invalid_file_format_msg) %} | ||
{% endif %} | ||
|
||
{% do return(raw_file_format) %} | ||
{% endmacro %} | ||
|
||
|
||
{% macro dbt_databricks_validate_get_incremental_strategy(raw_strategy, file_format) %} | ||
{#-- Validate the incremental strategy #} | ||
|
||
{% set invalid_strategy_msg -%} | ||
Invalid incremental strategy provided: {{ raw_strategy }} | ||
Expected one of: 'merge', 'replace_where', 'append', 'insert_overwrite' | ||
{%- endset %} | ||
|
||
{% set invalid_delta_only_msg -%} | ||
Invalid incremental strategy provided: {{ raw_strategy }} | ||
You can only choose this strategy when file_format is set to 'delta' | ||
{%- endset %} | ||
|
||
{% set invalid_insert_overwrite_endpoint_msg -%} | ||
Invalid incremental strategy provided: {{ raw_strategy }} | ||
You cannot use this strategy when connecting via warehouse | ||
Use the 'merge' or 'replace_where' strategy instead | ||
{%- endset %} | ||
|
||
{% if raw_strategy not in ['append', 'merge', 'insert_overwrite', 'replace_where'] %} | ||
{% do exceptions.raise_compiler_error(invalid_strategy_msg) %} | ||
{%-else %} | ||
{% if raw_strategy == 'merge' and file_format not in ['delta', 'hudi'] %} | ||
{% do exceptions.raise_compiler_error(invalid_delta_only_msg) %} | ||
{% endif %} | ||
{% if raw_strategy == 'replace_where' and file_format not in ['delta'] %} | ||
{% do exceptions.raise_compiler_error(invalid_delta_only_msg) %} | ||
{% endif %} | ||
{% if raw_strategy == 'insert_overwrite' and target.endpoint %} | ||
{% do exceptions.raise_compiler_error(invalid_insert_overwrite_endpoint_msg) %} | ||
{% endif %} | ||
{% endif %} | ||
|
||
{% do return(raw_strategy) %} | ||
{% endmacro %} |
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions
19
...s/integration/incremental_strategies/models_delta_cluster/insert_overwrite_partitions.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
incremental_strategy = 'insert_overwrite', | ||
partition_by = 'id', | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg | ||
|
||
{% else %} | ||
|
||
select cast(2 as bigint) as id, 'yo' as msg | ||
union all | ||
select cast(3 as bigint) as id, 'anyway' as msg | ||
|
||
{% endif %} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
tests/integration/incremental_strategies/models_delta_cluster/replace_where.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
unique_key = 'id', | ||
incremental_strategy = 'replace_where', | ||
incremental_predicates = "id >= 2" | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg, 'blue' as color | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg, 'red' as color | ||
|
||
{% else %} | ||
|
||
select cast(3 as bigint) as id, 'anyway' as msg, 'purple' as color | ||
|
||
{% endif %} |
18 changes: 18 additions & 0 deletions
18
tests/integration/incremental_strategies/models_delta_cluster_uc/append_delta.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
incremental_strategy = 'append', | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg | ||
|
||
{% else %} | ||
|
||
select cast(2 as bigint) as id, 'yo' as msg | ||
union all | ||
select cast(3 as bigint) as id, 'anyway' as msg | ||
|
||
{% endif %} |
22 changes: 22 additions & 0 deletions
22
tests/integration/incremental_strategies/models_delta_cluster_uc/merge_exclude_columns.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
incremental_strategy = 'merge', | ||
file_format = 'delta', | ||
unique_key = 'id', | ||
merge_exclude_columns = ['msg'], | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg, 'blue' as color | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg, 'red' as color | ||
|
||
{% else %} | ||
|
||
-- msg will be ignored, color will be updated | ||
select cast(2 as bigint) as id, 'yo' as msg, 'green' as color | ||
union all | ||
select cast(3 as bigint) as id, 'anyway' as msg, 'purple' as color | ||
|
||
{% endif %} |
17 changes: 17 additions & 0 deletions
17
tests/integration/incremental_strategies/models_delta_cluster_uc/merge_no_key.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg | ||
|
||
{% else %} | ||
|
||
select cast(2 as bigint) as id, 'yo' as msg | ||
union all | ||
select cast(3 as bigint) as id, 'anyway' as msg | ||
|
||
{% endif %} |
18 changes: 18 additions & 0 deletions
18
tests/integration/incremental_strategies/models_delta_cluster_uc/merge_unique_key.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
unique_key = 'id', | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg | ||
|
||
{% else %} | ||
|
||
select cast(2 as bigint) as id, 'yo' as msg | ||
union all | ||
select cast(3 as bigint) as id, 'anyway' as msg | ||
|
||
{% endif %} |
20 changes: 20 additions & 0 deletions
20
tests/integration/incremental_strategies/models_delta_cluster_uc/merge_update_columns.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
unique_key = 'id', | ||
merge_update_columns = ['msg'], | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg, 'blue' as color | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg, 'red' as color | ||
|
||
{% else %} | ||
|
||
-- msg will be updated, color will be ignored | ||
select cast(2 as bigint) as id, 'yo' as msg, 'green' as color | ||
union all | ||
select cast(3 as bigint) as id, 'anyway' as msg, 'purple' as color | ||
|
||
{% endif %} |
18 changes: 18 additions & 0 deletions
18
tests/integration/incremental_strategies/models_delta_cluster_uc/replace_where.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
unique_key = 'id', | ||
incremental_strategy = 'replace_where', | ||
incremental_predicates = "id >= 2" | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg, 'blue' as color | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg, 'red' as color | ||
|
||
{% else %} | ||
|
||
select cast(3 as bigint) as id, 'anyway' as msg, 'purple' as color | ||
|
||
{% endif %} |
18 changes: 18 additions & 0 deletions
18
tests/integration/incremental_strategies/models_delta_warehouse/append_delta.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
incremental_strategy = 'append', | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg | ||
|
||
{% else %} | ||
|
||
select cast(2 as bigint) as id, 'yo' as msg | ||
union all | ||
select cast(3 as bigint) as id, 'anyway' as msg | ||
|
||
{% endif %} |
22 changes: 22 additions & 0 deletions
22
tests/integration/incremental_strategies/models_delta_warehouse/merge_exclude_columns.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
incremental_strategy = 'merge', | ||
file_format = 'delta', | ||
unique_key = 'id', | ||
merge_exclude_columns = ['msg'], | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg, 'blue' as color | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg, 'red' as color | ||
|
||
{% else %} | ||
|
||
-- msg will be ignored, color will be updated | ||
select cast(2 as bigint) as id, 'yo' as msg, 'green' as color | ||
union all | ||
select cast(3 as bigint) as id, 'anyway' as msg, 'purple' as color | ||
|
||
{% endif %} |
17 changes: 17 additions & 0 deletions
17
tests/integration/incremental_strategies/models_delta_warehouse/merge_no_key.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{{ config( | ||
materialized = 'incremental', | ||
) }} | ||
|
||
{% if not is_incremental() %} | ||
|
||
select cast(1 as bigint) as id, 'hello' as msg | ||
union all | ||
select cast(2 as bigint) as id, 'goodbye' as msg | ||
|
||
{% else %} | ||
|
||
select cast(2 as bigint) as id, 'yo' as msg | ||
union all | ||
select cast(3 as bigint) as id, 'anyway' as msg | ||
|
||
{% endif %} |
Oops, something went wrong.