Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 06baa78

Browse files
committed
add optimize method
1 parent a405098 commit 06baa78

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

dbt/include/bigquery/macros/materializations/incremental.sql

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,30 @@
1717
{% do return(strategy) %}
1818
{% endmacro %}
1919

20+
{% macro dbt_bigquery_optimize_substrategy(partitions) %}
21+
{#-- Set the optimal substrategy based on the nature of the overwrite
22+
If static partitions: use delete+insert
23+
If dynamic partitions: use copy_partitions
24+
#}
25+
{% if partitions is not none %}
26+
{{ return("delete+insert") }}
27+
{% else %}
28+
{{ return("copy_partitions") }}
29+
{% endif %}
30+
{% endmacro %}
2031

21-
{% macro dbt_bigquery_validate_incremental_substrategy(config, strategy, copy_partitions) %}
22-
{#-- Find and validate the function used for insert_overwrite
32+
{% macro dbt_bigquery_validate_incremental_substrategy(config, strategy, copy_partitions, partitions) %}
33+
{#
34+
-- Find and validate the function used for insert_overwrite
2335
Legacy behaviour was to pass the copy_partitions as part of the `partition_by` clause
2436
So we need to bring back that optionality into this validation.
2537
#}
2638
{%- set incremental_substrategy = config.get('incremental_substrategy', 'copy_partitions' if copy_partitions else 'merge') -%}
2739

2840
{% if strategy in ['insert_overwrite', 'microbatch'] %}
29-
{% if incremental_substrategy not in ['merge', 'commit+delete+insert', 'delete+insert', 'copy_partitions'] %}
41+
{% if incremental_substrategy not in ['merge', 'commit+delete+insert', 'delete+insert', 'copy_partitions', 'auto'] %}
3042
{% set wrong_fn -%}
31-
The 'incremental_substrategy' option has to be either 'merge' (default), 'commit+delete+insert', 'delete+insert' or 'copy_partitions'.
43+
The 'incremental_substrategy' option has to be either 'merge' (default), 'commit+delete+insert', 'delete+insert', 'copy_partitions' or 'auto'
3244
{%- endset %}
3345
{% do exceptions.raise_compiler_error(wrong_fn) %}
3446
{% endif %}
@@ -38,7 +50,12 @@
3850
{%- endset %}
3951
{% do exceptions.raise_compiler_error(wrong_strategy_msg) %}
4052
{% endif %}
41-
{{ return(incremental_substrategy) }}
53+
54+
{% if incremental_substrategy == 'auto' %}
55+
{{ return(dbt_bigquery_optimize_substrategy(partitions)) }}
56+
{% else %}
57+
{{ return(incremental_substrategy) }}
58+
{% endif %}
4259
{% endmacro %}
4360

4461
{% macro source_sql_with_partition(partition_by, source_sql) %}
@@ -112,7 +129,7 @@
112129
{%- set cluster_by = config.get('cluster_by', none) -%}
113130

114131
{#-- Validate early that the incremental substrategy is set correctly for insert_overwrite or microbatch--#}
115-
{% set incremental_substrategy = dbt_bigquery_validate_incremental_substrategy(config, strategy, partition_by.copy_partitions) -%}
132+
{% set incremental_substrategy = dbt_bigquery_validate_incremental_substrategy(config, strategy, partition_by.copy_partitions, partitions) -%}
116133

117134

118135
{% set on_schema_change = incremental_validate_on_schema_change(config.get('on_schema_change'), default='ignore') %}

0 commit comments

Comments
 (0)