|
10 | 10 | {% set lf_grants = config.get('lf_grants') %} |
11 | 11 | {% set partitioned_by = config.get('partitioned_by') %} |
12 | 12 | {% set force_batch = config.get('force_batch', False) | as_bool -%} |
| 13 | + {% set build_with_subquery = config.get('build_with_subquery', False) | as_bool -%} |
13 | 14 | {% set unique_tmp_table_suffix = config.get('unique_tmp_table_suffix', False) | as_bool -%} |
14 | 15 | {% set temp_schema = config.get('temp_schema') %} |
15 | 16 | {% set target_relation = this.incorporate(type='table') %} |
|
46 | 47 | {% endif %} |
47 | 48 | {% endif %} |
48 | 49 |
|
| 50 | + {% if build_with_subquery %} |
| 51 | + {% if model_language == 'python' %} |
| 52 | + {% do exceptions.raise_compiler_error('build_with_subquery is not supported with Python models.') %} |
| 53 | + {% endif %} |
| 54 | + {% if force_batch %} |
| 55 | + {% do exceptions.raise_compiler_error('build_with_subquery is incompatible with force_batch. Batching requires data in the tmp table.') %} |
| 56 | + {% endif %} |
| 57 | + {% if strategy == 'insert_overwrite' %} |
| 58 | + {% do exceptions.raise_compiler_error('build_with_subquery is not supported with insert_overwrite strategy.') %} |
| 59 | + {% endif %} |
| 60 | + {% endif %} |
| 61 | + |
49 | 62 | {{ run_hooks(pre_hooks, inside_transaction=False) }} |
50 | 63 |
|
51 | 64 | -- `BEGIN` happens here: |
|
146 | 159 |
|
147 | 160 | -- Append Strategy -- |
148 | 161 | {% elif strategy == 'append' %} |
149 | | - {% if old_tmp_relation is not none %} |
150 | | - {% do drop_relation(old_tmp_relation) %} |
| 162 | + {% if build_with_subquery %} |
| 163 | + {% if old_tmp_relation is not none %} |
| 164 | + {% do drop_relation(old_tmp_relation) %} |
| 165 | + {% endif %} |
| 166 | + {%- set empty_sql = 'SELECT * FROM (' ~ compiled_code ~ ') _dbt_sbq WITH NO DATA' -%} |
| 167 | + {% do run_query(create_table_as(True, tmp_relation, empty_sql)) %} |
| 168 | + |
| 169 | + {% set build_sql = incremental_insert( |
| 170 | + on_schema_change, tmp_relation, target_relation, existing_relation, false, source_sql=compiled_code |
| 171 | + ) |
| 172 | + %} |
| 173 | + {% do to_drop.append(tmp_relation) %} |
| 174 | + {% else %} |
| 175 | + {% if old_tmp_relation is not none %} |
| 176 | + {% do drop_relation(old_tmp_relation) %} |
| 177 | + {% endif %} |
| 178 | + {% set query_result = safe_create_table_as(True, tmp_relation, compiled_code, model_language, force_batch) -%} |
| 179 | + {%- if model_language == 'python' -%} |
| 180 | + {% call statement('create_table', language=model_language) %} |
| 181 | + {{ query_result }} |
| 182 | + {% endcall %} |
| 183 | + {%- endif -%} |
| 184 | + {% set build_sql = incremental_insert( |
| 185 | + on_schema_change, tmp_relation, target_relation, existing_relation, force_batch |
| 186 | + ) |
| 187 | + %} |
| 188 | + {% do to_drop.append(tmp_relation) %} |
151 | 189 | {% endif %} |
152 | | - {% set query_result = safe_create_table_as(True, tmp_relation, compiled_code, model_language, force_batch) -%} |
153 | | - {%- if model_language == 'python' -%} |
154 | | - {% call statement('create_table', language=model_language) %} |
155 | | - {{ query_result }} |
156 | | - {% endcall %} |
157 | | - {%- endif -%} |
158 | | - {% set build_sql = incremental_insert( |
159 | | - on_schema_change, tmp_relation, target_relation, existing_relation, force_batch |
160 | | - ) |
161 | | - %} |
162 | | - {% do to_drop.append(tmp_relation) %} |
163 | 190 |
|
164 | | - -- Iceberge Merge Stategy -- |
| 191 | + -- Iceberg Merge Strategy -- |
165 | 192 | {% elif strategy == 'merge' and table_type == 'iceberg' %} |
166 | 193 | {% set unique_key = config.get('unique_key') %} |
167 | 194 | {% set incremental_predicates = config.get('incremental_predicates') %} |
|
186 | 213 | {% do exceptions.raise_compiler_error(inc_predicates_not_list) %} |
187 | 214 | {% endif %} |
188 | 215 | {% endif %} |
189 | | - {% if old_tmp_relation is not none %} |
190 | | - {% do drop_relation(old_tmp_relation) %} |
| 216 | + |
| 217 | + {% if build_with_subquery %} |
| 218 | + -- Create empty tmp table for schema comparison (no data scan) |
| 219 | + {% if old_tmp_relation is not none %} |
| 220 | + {% do drop_relation(old_tmp_relation) %} |
| 221 | + {% endif %} |
| 222 | + {%- set empty_sql = 'SELECT * FROM (' ~ compiled_code ~ ') _dbt_sbq WITH NO DATA' -%} |
| 223 | + {% do run_query(create_table_as(True, tmp_relation, empty_sql)) %} |
| 224 | + |
| 225 | + {% set build_sql = iceberg_merge( |
| 226 | + on_schema_change=on_schema_change, |
| 227 | + tmp_relation=tmp_relation, |
| 228 | + target_relation=target_relation, |
| 229 | + unique_key=unique_key, |
| 230 | + incremental_predicates=incremental_predicates, |
| 231 | + existing_relation=existing_relation, |
| 232 | + delete_condition=delete_condition, |
| 233 | + update_condition=update_condition, |
| 234 | + insert_condition=insert_condition, |
| 235 | + force_batch=false, |
| 236 | + source_sql=compiled_code, |
| 237 | + ) |
| 238 | + %} |
| 239 | + {% do to_drop.append(tmp_relation) %} |
| 240 | + {% else %} |
| 241 | + {% if old_tmp_relation is not none %} |
| 242 | + {% do drop_relation(old_tmp_relation) %} |
| 243 | + {% endif %} |
| 244 | + {% set query_result = safe_create_table_as(True, tmp_relation, compiled_code, model_language, force_batch) -%} |
| 245 | + {%- if model_language == 'python' -%} |
| 246 | + {% call statement('create_table', language=model_language) %} |
| 247 | + {{ query_result }} |
| 248 | + {% endcall %} |
| 249 | + {%- endif -%} |
| 250 | + {% set build_sql = iceberg_merge( |
| 251 | + on_schema_change=on_schema_change, |
| 252 | + tmp_relation=tmp_relation, |
| 253 | + target_relation=target_relation, |
| 254 | + unique_key=unique_key, |
| 255 | + incremental_predicates=incremental_predicates, |
| 256 | + existing_relation=existing_relation, |
| 257 | + delete_condition=delete_condition, |
| 258 | + update_condition=update_condition, |
| 259 | + insert_condition=insert_condition, |
| 260 | + force_batch=force_batch, |
| 261 | + ) |
| 262 | + %} |
| 263 | + {% do to_drop.append(tmp_relation) %} |
191 | 264 | {% endif %} |
192 | | - {% set query_result = safe_create_table_as(True, tmp_relation, compiled_code, model_language, force_batch) -%} |
193 | | - {%- if model_language == 'python' -%} |
194 | | - {% call statement('create_table', language=model_language) %} |
195 | | - {{ query_result }} |
196 | | - {% endcall %} |
197 | | - {%- endif -%} |
198 | | - {% set build_sql = iceberg_merge( |
199 | | - on_schema_change=on_schema_change, |
200 | | - tmp_relation=tmp_relation, |
201 | | - target_relation=target_relation, |
202 | | - unique_key=unique_key, |
203 | | - incremental_predicates=incremental_predicates, |
204 | | - existing_relation=existing_relation, |
205 | | - delete_condition=delete_condition, |
206 | | - update_condition=update_condition, |
207 | | - insert_condition=insert_condition, |
208 | | - force_batch=force_batch, |
209 | | - ) |
210 | | - %} |
211 | | - {% do to_drop.append(tmp_relation) %} |
212 | 265 | {% endif %} |
213 | 266 |
|
214 | 267 | {% call statement("main", language=model_language) %} |
|
0 commit comments