From 9e8f826f5b40b82b70e7d998634ef3bcab7866dd Mon Sep 17 00:00:00 2001 From: amardatar Date: Thu, 11 May 2023 14:29:41 +0100 Subject: [PATCH] Updating incremental merge where condition to handle nullable fields with an appropriate null comparison. --- .changes/unreleased/Fixes-20230511-142935.yaml | 7 +++++++ .../macros/materializations/models/incremental/merge.sql | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .changes/unreleased/Fixes-20230511-142935.yaml diff --git a/.changes/unreleased/Fixes-20230511-142935.yaml b/.changes/unreleased/Fixes-20230511-142935.yaml new file mode 100644 index 00000000000..61e0ec78636 --- /dev/null +++ b/.changes/unreleased/Fixes-20230511-142935.yaml @@ -0,0 +1,7 @@ +kind: Fixes +body: Fix incremental merge to use an `is null` check to allow for handling of nullable + fields in the unique_key +time: 2023-05-11T14:29:35.400734+01:00 +custom: + Author: amardatar + Issue: "7597" diff --git a/core/dbt/include/global_project/macros/materializations/models/incremental/merge.sql b/core/dbt/include/global_project/macros/materializations/models/incremental/merge.sql index ca972c9f258..afdb8dec635 100644 --- a/core/dbt/include/global_project/macros/materializations/models/incremental/merge.sql +++ b/core/dbt/include/global_project/macros/materializations/models/incremental/merge.sql @@ -62,12 +62,12 @@ {% if unique_key %} {% if unique_key is sequence and unique_key is not string %} - delete from {{target }} + delete from {{ target }} using {{ source }} where ( {% for key in unique_key %} - {{ source }}.{{ key }} = {{ target }}.{{ key }} - {{ "and " if not loop.last}} + ({{ source }}.{{ key }} = {{ target }}.{{ key }} or ({{ source }}.{{ key }} is null and {{ target }}.{{ key }} is null)) + {{ "and " if not loop.last }} {% endfor %} {% if incremental_predicates %} {% for predicate in incremental_predicates %}