|
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 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 %} |
20 | 31 |
|
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 |
23 | 35 | Legacy behaviour was to pass the copy_partitions as part of the `partition_by` clause |
24 | 36 | So we need to bring back that optionality into this validation. |
25 | 37 | #} |
26 | 38 | {%- set incremental_substrategy = config.get('incremental_substrategy', 'copy_partitions' if copy_partitions else 'merge') -%} |
27 | 39 |
|
28 | 40 | {% 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'] %} |
30 | 42 | {% 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' |
32 | 44 | {%- endset %} |
33 | 45 | {% do exceptions.raise_compiler_error(wrong_fn) %} |
34 | 46 | {% endif %} |
|
38 | 50 | {%- endset %} |
39 | 51 | {% do exceptions.raise_compiler_error(wrong_strategy_msg) %} |
40 | 52 | {% 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 %} |
42 | 59 | {% endmacro %} |
43 | 60 |
|
44 | 61 | {% macro source_sql_with_partition(partition_by, source_sql) %} |
|
112 | 129 | {%- set cluster_by = config.get('cluster_by', none) -%} |
113 | 130 |
|
114 | 131 | {#-- 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) -%} |
116 | 133 |
|
117 | 134 |
|
118 | 135 | {% set on_schema_change = incremental_validate_on_schema_change(config.get('on_schema_change'), default='ignore') %} |
|
0 commit comments