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

Commit 130d15e

Browse files
axel_thevenotAxelThevenot
authored andcommitted
refactor(incremental): optimize 'insert_overwrite' strategy (#1409)
1 parent 0995665 commit 130d15e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Under the Hood
2+
body: 'refactor(incremental): optimize ''insert_overwrite'' strategy'
3+
time: 2024-11-21T19:10:41.341213+01:00
4+
custom:
5+
Author: AxelThevenot
6+
Issue: "1409"

dbt/include/bigquery/macros/materializations/incremental_strategy/insert_overwrite.sql

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
{% macro bigquery__get_insert_overwrite_merge_sql(target, source, dest_columns, predicates, include_sql_header) -%}
2+
{#-- The only time include_sql_header is True: --#}
3+
{#-- BigQuery + insert_overwrite strategy + "static" partitions config --#}
4+
{#-- We should consider including the sql header at the materialization level instead --#}
5+
6+
{%- set predicates = [] if predicates is none else [] + predicates -%}
7+
{%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%}
8+
{%- set sql_header = config.get('sql_header', none) -%}
9+
10+
{{ sql_header if sql_header is not none and include_sql_header }}
11+
12+
begin
13+
begin transaction;
14+
15+
delete from {{ target }} as DBT_INTERNAL_DEST
16+
where true
17+
{%- if predicates %}
18+
{% for predicate in predicates %}
19+
and {{ predicate }}
20+
{% endfor %}
21+
{%- endif -%};
22+
23+
insert into {{ target }} ({{ dest_cols_csv }})
24+
(
25+
select {{ dest_cols_csv }}
26+
from {{ source }}
27+
);
28+
29+
commit transaction;
30+
31+
exception when error then
32+
-- Roll if any error to avoid deleting rows.
33+
raise using message = @@error.message;
34+
rollback transaction;
35+
end
36+
37+
{% endmacro %}
38+
139
{% macro bq_generate_incremental_insert_overwrite_build_sql(
240
tmp_relation, target_relation, sql, unique_key, partition_by, partitions, dest_columns, tmp_relation_exists, copy_partitions
341
) %}

0 commit comments

Comments
 (0)