Skip to content

Commit b616c26

Browse files
authored
Merge pull request #137 from oracle/dev/v1.7.3
Updated "delete+insert" incremental strategy macros
2 parents 5cb6a86 + 8216c6e commit b616c26

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

dbt/adapters/oracle/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
version = "1.7.9"
17+
version = "1.7.10"

dbt/include/oracle/macros/materializations/incremental/strategies.sql

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,32 @@
150150
{% do return(get_incremental_merge_sql(arg_dict)) %}
151151
{% endmacro %}
152152
153+
154+
{% macro oracle__get_delete_sql_for_delete_insert_strategy(target, source, unique_key, incremental_predicates) %}
155+
{%- if unique_key -%}
156+
{%- set unique_key_result = oracle_check_and_quote_unique_key_for_incremental_merge(unique_key, incremental_predicates) -%}
157+
{%- set unique_key_list = unique_key_result['unique_key_list'] -%}
158+
DELETE FROM {{ target }} DBT_INTERNAL_DEST
159+
WHERE ({% for key in unique_key_list %}
160+
DBT_INTERNAL_DEST.{{ key }} {{ ", " if not loop.last}}
161+
{% endfor %})
162+
IN (SELECT {% for key in unique_key_list %}
163+
DBT_INTERNAL_SOURCE.{{ key }} {{ ", " if not loop.last}}
164+
{% endfor %} FROM {{source}} DBT_INTERNAL_SOURCE)
165+
{%- if incremental_predicates -%}
166+
{% for predicate in incremental_predicates %}
167+
AND {{ predicate }}
168+
{% endfor %}
169+
{%- endif -%}
170+
{%- elif incremental_predicates -%}
171+
DELETE FROM {{ target }} DBT_INTERNAL_DEST
172+
WHERE {%- for predicate in incremental_predicates -%}
173+
{{ "AND" if not loop.first}} {{ predicate }}
174+
{%- endfor -%}
175+
{%- endif -%}
176+
{% endmacro %}
177+
178+
153179
{% macro oracle__get_incremental_delete_insert_sql(args_dict) %}
154180
{%- set parallel = config.get('parallel', none) -%}
155181
{%- set dest_columns = args_dict["dest_columns"] -%}
@@ -158,30 +184,16 @@
158184
{%- set unique_key = args_dict["unique_key"] -%}
159185
{%- set dest_column_names = dest_columns | map(attribute='name') | list -%}
160186
{%- set dest_cols_csv = get_quoted_column_csv(model, dest_column_names) -%}
161-
{%- set merge_update_columns = config.get('merge_update_columns') -%}
162-
{%- set merge_exclude_columns = config.get('merge_exclude_columns') -%}
163187
{%- set incremental_predicates = args_dict["incremental_predicates"] -%}
164-
{%- set update_columns = get_merge_update_columns(merge_update_columns, merge_exclude_columns, dest_columns) -%}
165-
{%- if unique_key -%}
166-
{%- set unique_key_result = oracle_check_and_quote_unique_key_for_incremental_merge(unique_key, incremental_predicates) -%}
167-
{%- set unique_key_list = unique_key_result['unique_key_list'] -%}
168-
{%- set unique_key_merge_predicates = unique_key_result['unique_key_merge_predicates'] -%}
188+
{%- if unique_key or incremental_predicates -%}
169189
BEGIN
170-
EXECUTE IMMEDIATE 'merge {% if parallel %} /*+parallel({{ parallel }})*/ {% endif %} into {{ target_relation }} DBT_INTERNAL_DEST
171-
using {{ temp_relation }} DBT_INTERNAL_SOURCE
172-
on ({{ unique_key_merge_predicates | join(' AND ') }})
173-
when matched then
174-
update set
175-
{% for col in update_columns if (col.upper() not in unique_key_list and col not in unique_key_list) -%}
176-
DBT_INTERNAL_DEST.{{ col }} = DBT_INTERNAL_SOURCE.{{ col }}{% if not loop.last %}, {% endif %}
177-
{% endfor -%}
178-
DELETE WHERE 1=1';
190+
EXECUTE IMMEDIATE '{{ oracle__get_delete_sql_for_delete_insert_strategy(target_relation, temp_relation, unique_key, incremental_predicates) }}';
179191
EXECUTE IMMEDIATE 'insert {% if parallel %} /*+parallel({{ parallel }})*/ {% endif %} into {{ target_relation }} ({{ dest_cols_csv }})(
180192
select {{ dest_cols_csv }}
181193
from {{ temp_relation }})';
182194
END;
183195
{%- else -%}
184-
insert {% if parallel %} /*+parallel({{ parallel }})*/ {% endif %} into {{ target_relation }} ({{ dest_cols_csv }})
196+
insert {%- if parallel -%} /*+parallel({{ parallel }})*/ {%- endif -%} into {{ target_relation }} ({{ dest_cols_csv }})
185197
(
186198
select {{ dest_cols_csv }}
187199
from {{ temp_relation }}

dbt_adbs_test_project/models/us_product_delete_insert.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
{{
1717
config(
1818
materialized='incremental',
19+
unique_key='group_id',
20+
incremental_predicates=["DBT_INTERNAL_DEST.calendar_month_desc > TO_CHAR(sysdate, ''yyyy/mm/dd'')"],
1921
incremental_strategy='delete+insert',
2022
parallel=4,
2123
partition_config={"clause": "PARTITION BY HASH(PROD_NAME) PARTITIONS 4"},

0 commit comments

Comments
 (0)