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 %}