| 
17 | 17 |   {% do return(strategy) %}  | 
18 | 18 | {% endmacro %}  | 
19 | 19 | 
 
  | 
 | 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 %}  | 
20 | 28 | 
 
  | 
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  | 
23 | 31 |     Legacy behaviour was to pass the copy_partitions as part of the `partition_by` clause  | 
24 | 32 |     So we need to bring back that optionality into this validation.  | 
25 | 33 |   #}  | 
26 | 34 |   {%- set incremental_substrategy = config.get('incremental_substrategy', 'copy_partitions' if copy_partitions else 'merge') -%}  | 
27 | 35 | 
 
  | 
28 | 36 |   {% 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'] %}  | 
30 | 38 |       {% 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'  | 
32 | 40 |       {%- endset %}  | 
33 | 41 |       {% do exceptions.raise_compiler_error(wrong_fn) %}  | 
34 | 42 |     {% endif %}  | 
 | 
38 | 46 |       {%- endset %}  | 
39 | 47 |       {% do exceptions.raise_compiler_error(wrong_strategy_msg) %}  | 
40 | 48 |   {% 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 %}  | 
42 | 55 | {% endmacro %}  | 
43 | 56 | 
 
  | 
44 | 57 | {% macro source_sql_with_partition(partition_by, source_sql) %}  | 
 | 
112 | 125 |   {%- set cluster_by = config.get('cluster_by', none) -%}  | 
113 | 126 | 
 
  | 
114 | 127 |   {#-- 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) -%}  | 
116 | 129 | 
 
  | 
117 | 130 | 
 
  | 
118 | 131 |   {% set on_schema_change = incremental_validate_on_schema_change(config.get('on_schema_change'), default='ignore') %}  | 
 | 
0 commit comments