From 3e69fadae7f2b0cb9bc395b992aa71bb94d3a4a7 Mon Sep 17 00:00:00 2001 From: Adrian Burus Date: Fri, 21 Feb 2025 09:38:18 +0200 Subject: [PATCH] Merged changes regarding null-equality checks into existing PR from ataft --- .../unreleased/Fixes-20250221-092716.yaml | 6 +++ .../models/incremental/merge.sql | 45 +++++++------------ 2 files changed, 22 insertions(+), 29 deletions(-) create mode 100644 dbt-adapters/.changes/unreleased/Fixes-20250221-092716.yaml diff --git a/dbt-adapters/.changes/unreleased/Fixes-20250221-092716.yaml b/dbt-adapters/.changes/unreleased/Fixes-20250221-092716.yaml new file mode 100644 index 000000000..7ee76deab --- /dev/null +++ b/dbt-adapters/.changes/unreleased/Fixes-20250221-092716.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fix incremental delete+insert SQL +time: 2025-02-21T09:27:16.166423+02:00 +custom: + Author: ataft, adrianburusdbt + Issue: 150, 364 diff --git a/dbt-adapters/src/dbt/include/global_project/macros/materializations/models/incremental/merge.sql b/dbt-adapters/src/dbt/include/global_project/macros/materializations/models/incremental/merge.sql index 9df17def7..dac5544f6 100644 --- a/dbt-adapters/src/dbt/include/global_project/macros/materializations/models/incremental/merge.sql +++ b/dbt-adapters/src/dbt/include/global_project/macros/materializations/models/incremental/merge.sql @@ -61,36 +61,23 @@ {%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%} {% if unique_key %} - {% if unique_key is sequence and unique_key is not string %} - delete from {{ target }} - using {{ source }} - where ( - {% for key in unique_key %} - {% set source_unique_key = (source ~ "." ~ key) | trim %} - {% set target_unique_key = (target ~ "." ~ key) | trim %} - {{ equals(source_unique_key, target_unique_key) }} - {{ "and " if not loop.last}} - {% endfor %} - {% if incremental_predicates %} - {% for predicate in incremental_predicates %} - and {{ predicate }} - {% endfor %} - {% endif %} - ); - {% else %} - delete from {{ target }} - where ( - {{ unique_key }}) in ( - select ({{ unique_key }}) - from {{ source }} - ) - {%- if incremental_predicates %} - {% for predicate in incremental_predicates %} - and {{ predicate }} - {% endfor %} - {%- endif -%}; - + {% if unique_key is string %} + {% set unique_key = [unique_key] %} {% endif %} + + {%- set unique_key_str = unique_key|join(', ') -%} + + delete from {{ target }} + where ({{ unique_key_str }}) in ( + select distinct {{ unique_key_str }} + from {{ source }} + ) + {%- if incremental_predicates %} + {% for predicate in incremental_predicates %} + and {{ predicate }} + {% endfor %} + {%- endif -%}; + {% endif %} insert into {{ target }} ({{ dest_cols_csv }})