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

Commit 241119a

Browse files
committed
add optimize method
1 parent a405098 commit 241119a

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,26 @@
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 partitions is not none and partitions != [] %} {# static #}
23+
{{ return("delete+insert") }}
24+
{% else %} {# dynamic #}
25+
{{ return("copy_partitions") }}
26+
{% endif %}
27+
{% endmacro %}
2028

21-
{% macro dbt_bigquery_validate_incremental_substrategy(config, strategy, copy_partitions) %}
22-
{#-- Find and validate the function used for insert_overwrite
29+
{% macro dbt_bigquery_validate_incremental_substrategy(config, strategy, copy_partitions, partitions) %}
30+
{# -- Find and validate the function used for insert_overwrite
2331
Legacy behaviour was to pass the copy_partitions as part of the `partition_by` clause
2432
So we need to bring back that optionality into this validation.
2533
#}
2634
{%- set incremental_substrategy = config.get('incremental_substrategy', 'copy_partitions' if copy_partitions else 'merge') -%}
2735

2836
{% if strategy in ['insert_overwrite', 'microbatch'] %}
29-
{% if incremental_substrategy not in ['merge', 'commit+delete+insert', 'delete+insert', 'copy_partitions'] %}
37+
{% if incremental_substrategy not in ['merge', 'commit+delete+insert', 'delete+insert', 'copy_partitions', 'auto'] %}
3038
{% set wrong_fn -%}
31-
The 'incremental_substrategy' option has to be either 'merge' (default), 'commit+delete+insert', 'delete+insert' or 'copy_partitions'.
39+
The 'incremental_substrategy' option has to be either 'merge' (default), 'commit+delete+insert', 'delete+insert', 'copy_partitions' or 'auto'
3240
{%- endset %}
3341
{% do exceptions.raise_compiler_error(wrong_fn) %}
3442
{% endif %}
@@ -38,7 +46,12 @@
3846
{%- endset %}
3947
{% do exceptions.raise_compiler_error(wrong_strategy_msg) %}
4048
{% endif %}
41-
{{ return(incremental_substrategy) }}
49+
50+
{% if incremental_substrategy == 'auto' %}
51+
{{ return(dbt_bigquery_optimize_substrategy(partitions)) }}
52+
{% else %}
53+
{{ return(incremental_substrategy) }}
54+
{% endif %}
4255
{% endmacro %}
4356

4457
{% macro source_sql_with_partition(partition_by, source_sql) %}
@@ -112,7 +125,7 @@
112125
{%- set cluster_by = config.get('cluster_by', none) -%}
113126

114127
{#-- 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) -%}
128+
{% set incremental_substrategy = dbt_bigquery_validate_incremental_substrategy(config, strategy, partition_by.copy_partitions, partitions) -%}
116129

117130

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

0 commit comments

Comments
 (0)