| 
1 | 1 | {% macro bq_generate_incremental_insert_overwrite_build_sql(  | 
2 |  | -    tmp_relation, target_relation, sql, unique_key, partition_by, partitions, dest_columns, tmp_relation_exists, copy_partitions, insert_overwrite_fn  | 
 | 2 | +    tmp_relation, target_relation, sql, unique_key, partition_by, partitions, dest_columns, tmp_relation_exists, incremental_substrategy  | 
3 | 3 | ) %}  | 
4 | 4 |     {% if partition_by is none %}  | 
5 | 5 |       {% set missing_partition_msg -%}  | 
 | 
9 | 9 |     {% endif %}  | 
10 | 10 | 
 
  | 
11 | 11 |     {% set build_sql = bq_insert_overwrite_sql(  | 
12 |  | -        tmp_relation, target_relation, sql, unique_key, partition_by, partitions, dest_columns, tmp_relation_exists, copy_partitions, insert_overwrite_fn  | 
 | 12 | +        tmp_relation, target_relation, sql, unique_key, partition_by, partitions, dest_columns, tmp_relation_exists, incremental_substrategy  | 
13 | 13 |     ) %}  | 
14 | 14 | 
 
  | 
15 | 15 |     {{ return(build_sql) }}  | 
 | 
38 | 38 | {% endmacro %}  | 
39 | 39 | 
 
  | 
40 | 40 | {% macro bq_insert_overwrite_sql(  | 
41 |  | -    tmp_relation, target_relation, sql, unique_key, partition_by, partitions, dest_columns, tmp_relation_exists, copy_partitions, insert_overwrite_fn  | 
 | 41 | +    tmp_relation, target_relation, sql, unique_key, partition_by, partitions, dest_columns, tmp_relation_exists, incremental_substrategy  | 
42 | 42 | ) %}  | 
43 | 43 |   {% if partitions is not none and partitions != [] %} {# static #}  | 
44 |  | -      {{ bq_static_insert_overwrite_sql(tmp_relation, target_relation, sql, partition_by, partitions, dest_columns, tmp_relation_exists, copy_partitions, insert_overwrite_fn) }}  | 
 | 44 | +      {{ bq_static_insert_overwrite_sql(tmp_relation, target_relation, sql, partition_by, partitions, dest_columns, tmp_relation_exists, incremental_substrategy) }}  | 
45 | 45 |   {% else %} {# dynamic #}  | 
46 |  | -      {{ bq_dynamic_insert_overwrite_sql(tmp_relation, target_relation, sql, unique_key, partition_by, dest_columns, tmp_relation_exists, copy_partitions, insert_overwrite_fn) }}  | 
 | 46 | +      {{ bq_dynamic_insert_overwrite_sql(tmp_relation, target_relation, sql, unique_key, partition_by, dest_columns, tmp_relation_exists, incremental_substrategy) }}  | 
47 | 47 |   {% endif %}  | 
48 | 48 | {% endmacro %}  | 
49 | 49 | 
 
  | 
50 | 50 | {% macro bq_static_insert_overwrite_sql(  | 
51 |  | -    tmp_relation, target_relation, sql, partition_by, partitions, dest_columns, tmp_relation_exists, copy_partitions, insert_overwrite_fn  | 
 | 51 | +    tmp_relation, target_relation, sql, partition_by, partitions, dest_columns, tmp_relation_exists, incremental_substrategy  | 
52 | 52 | ) %}  | 
53 | 53 | 
 
  | 
54 | 54 |       {% set predicate -%}  | 
 | 
75 | 75 |         )  | 
76 | 76 |       {%- endset -%}  | 
77 | 77 | 
 
  | 
78 |  | -      {% if copy_partitions %}  | 
 | 78 | +      {% if incremental_substrategy == 'copy_partitions' %}  | 
79 | 79 |           {% do bq_copy_partitions(tmp_relation, target_relation, partitions, partition_by) %}  | 
80 | 80 |       {% else %}  | 
81 | 81 | 
 
  | 
82 |  | -      {#-- In case we're putting the model SQL _directly_ into the MERGE statement,  | 
83 |  | -         we need to prepend the MERGE statement with the user-configured sql_header,  | 
84 |  | -         which may be needed to resolve that model SQL (e.g. referencing a variable or UDF in the header)  | 
85 |  | -         in the "temporary table exists" case, we save the model SQL result as a temp table first, wherein the  | 
86 |  | -         sql_header is included by the create_table_as macro.  | 
87 |  | -      #}  | 
88 |  | -        | 
89 |  | -      {% if insert_overwrite_fn == 'delete+insert' %}  | 
90 |  | -        -- 1. run insert_overwrite with delete+insert transaction strategy optimisation  | 
91 |  | -        {{ bq_get_insert_overwrite_with_delete_and_insert_sql(target_relation, source_sql, dest_columns, [predicate], include_sql_header = not tmp_relation_exists) }};  | 
92 |  | -      {% else %}  | 
93 |  | -        -- 1. run insert_overwrite with merge strategy optimisation  | 
94 |  | -        {{ get_insert_overwrite_merge_sql(target_relation, source_sql, dest_columns, [predicate], include_sql_header = not tmp_relation_exists) }};  | 
95 |  | -      {% endif %}  | 
96 |  | - | 
97 |  | -      {%- if tmp_relation_exists -%}  | 
98 |  | -      -- 2. clean up the temp table  | 
99 |  | -      drop table if exists {{ tmp_relation }};  | 
100 |  | -      {%- endif -%}  | 
 | 82 | +        {#-- In case we're putting the model SQL _directly_ into the MERGE/insert+delete transaction,  | 
 | 83 | +          we need to prepend the merge/transaction statement with the user-configured sql_header,  | 
 | 84 | +          which may be needed to resolve that model SQL (e.g. referencing a variable or UDF in the header)  | 
 | 85 | +          in the "temporary table exists" case, we save the model SQL result as a temp table first, wherein the  | 
 | 86 | +          sql_header is included by the create_table_as macro.  | 
 | 87 | +        #}  | 
 | 88 | + | 
 | 89 | +        {% if incremental_substrategy == 'delete+insert' %}  | 
 | 90 | +          -- 1. run insert_overwrite with delete+insert transaction strategy optimisation  | 
 | 91 | +          {{ bq_get_insert_overwrite_with_delete_and_insert_sql(target_relation, source_sql, dest_columns, [predicate], include_sql_header = not tmp_relation_exists) }};  | 
 | 92 | +        {% else %}  | 
 | 93 | +          -- 1. run insert_overwrite with merge strategy optimisation  | 
 | 94 | +          {{ get_insert_overwrite_merge_sql(target_relation, source_sql, dest_columns, [predicate], include_sql_header = not tmp_relation_exists) }};  | 
 | 95 | +        {% endif %}  | 
 | 96 | + | 
 | 97 | +        {%- if tmp_relation_exists -%}  | 
 | 98 | +        -- 2. clean up the temp table  | 
 | 99 | +        drop table if exists {{ tmp_relation }};  | 
 | 100 | +        {%- endif -%}  | 
101 | 101 | 
 
  | 
102 |  | -  {% endif %}  | 
 | 102 | +      {% endif %}  | 
103 | 103 | {% endmacro %}  | 
104 | 104 | 
 
  | 
105 | 105 | {% macro bq_dynamic_copy_partitions_insert_overwrite_sql(  | 
106 |  | -  tmp_relation, target_relation, sql, unique_key, partition_by, dest_columns, tmp_relation_exists, copy_partitions  | 
 | 106 | +  tmp_relation, target_relation, sql, unique_key, partition_by, dest_columns, tmp_relation_exists  | 
107 | 107 |   ) %}  | 
108 | 108 |   {%- if tmp_relation_exists is false -%}  | 
109 | 109 |   {# We run temp table creation in a separated script to move to partitions copy if it does not already exist #}  | 
 | 
123 | 123 |   drop table if exists {{ tmp_relation }}  | 
124 | 124 | {% endmacro %}  | 
125 | 125 | 
 
  | 
126 |  | -{% macro bq_dynamic_insert_overwrite_sql(tmp_relation, target_relation, sql, unique_key, partition_by, dest_columns, tmp_relation_exists, copy_partitions) %}  | 
127 |  | -  {%- if copy_partitions is true %}  | 
128 |  | -     {{ bq_dynamic_copy_partitions_insert_overwrite_sql(tmp_relation, target_relation, sql, unique_key, partition_by, dest_columns, tmp_relation_exists, copy_partitions) }}  | 
 | 126 | +{% macro bq_dynamic_insert_overwrite_sql(tmp_relation, target_relation, sql, unique_key, partition_by, dest_columns, tmp_relation_exists, incremental_substrategy) %}  | 
 | 127 | +  {% if incremental_substrategy == 'copy_partitions' %}  | 
 | 128 | +     {{ bq_dynamic_copy_partitions_insert_overwrite_sql(tmp_relation, target_relation, sql, unique_key, partition_by, dest_columns, tmp_relation_exists) }}  | 
129 | 129 |   {% else -%}  | 
130 | 130 |       {% set predicate -%}  | 
131 | 131 |           {{ partition_by.render_wrapped(alias='DBT_INTERNAL_DEST') }} in unnest(dbt_partitions_for_replacement)  | 
 | 
161 | 161 |           from {{ tmp_relation }}  | 
162 | 162 |       );  | 
163 | 163 | 
 
  | 
164 |  | -      {% if  insert_overwrite_fn == 'delete+insert' %}  | 
 | 164 | +      {% if  incremental_substrategy == 'delete+insert' %}  | 
165 | 165 |         -- 3. run insert_overwrite with the delete+insert transaction strategy optimisation  | 
166 | 166 |         {{ bq_get_insert_overwrite_with_delete_and_insert_sql(target_relation, source_sql, dest_columns, [predicate]) }};  | 
167 | 167 |       {% else %}  | 
 | 
185 | 185 |     {{ sql_header if sql_header is not none and include_sql_header }}  | 
186 | 186 | 
 
  | 
187 | 187 |     begin  | 
188 |  | -        begin transaction;   | 
 | 188 | +        begin transaction;  | 
189 | 189 | 
 
  | 
190 | 190 |             -- (as of Nov 2024)  | 
191 |  | -            -- DELETE operations are free if the partition is a DATE   | 
192 |  | -            -- * Not free if the partitions are granular (hourly, monthly)   | 
 | 191 | +            -- DELETE operations are free if the partition is a DATE  | 
 | 192 | +            -- * Not free if the partitions are granular (hourly, monthly)  | 
193 | 193 |             --   or some other conditions like subqueries and so on.  | 
194 | 194 |             delete from {{ target }} as DBT_INTERNAL_DEST  | 
195 | 195 |             where true  | 
 | 
0 commit comments