From 8b032632a006fd6ca70a076ee891a3cf95dcb27c Mon Sep 17 00:00:00 2001 From: ajay-abrol2 <85120431+ajay-abrol2@users.noreply.github.com> Date: Wed, 22 Nov 2023 23:09:16 -0500 Subject: [PATCH] Adding support of multiple values for unique key col (#116) Co-authored-by: nrodriguezmicrofocus Co-authored-by: nrodriguezmicrofocus <44621128+nrodriguezmicrofocus@users.noreply.github.com> --- .../models/incremental/merge.sql | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/dbt/include/vertica/macros/materializations/models/incremental/merge.sql b/dbt/include/vertica/macros/materializations/models/incremental/merge.sql index d37456a..1dc3936 100644 --- a/dbt/include/vertica/macros/materializations/models/incremental/merge.sql +++ b/dbt/include/vertica/macros/materializations/models/incremental/merge.sql @@ -5,14 +5,19 @@ merge into {{ target_relation }} as DBT_INTERNAL_DEST using {{ tmp_relation }} as DBT_INTERNAL_SOURCE - + + {#-- Test 1, find the provided merge columns #} {% if merge_columns %} - on - {% for column in [merge_columns] %} + on + {% if merge_columns is string %} + DBT_INTERNAL_DEST.{{ adapter.quote(merge_columns) }} = DBT_INTERNAL_SOURCE.{{ adapter.quote(merge_columns) }} + {% else %} + {% for column in merge_columns -%} DBT_INTERNAL_DEST.{{ adapter.quote(column) }} = DBT_INTERNAL_SOURCE.{{ adapter.quote(column) }} {%- if not loop.last %} AND {% endif %} {%- endfor %} + {% endif %} {#-- Test 2, use all columns in the destination table #} {% else %} on @@ -54,12 +59,29 @@ {%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%} {% if unique_key %} + {% if unique_key is string %} + delete from {{ target }} + where + ({{ unique_key }}) in ( + select ({{ unique_key }}) + from {{ source }} + ) + + + ; + {% else %} delete from {{ target }} - where ( - {{ unique_key }}) in ( - select ({{ unique_key }}) - from {{ source }} - ); + where + {% for column in unique_key -%} + ({{ column }}) in ( + select ({{ column }}) + from {{ source }} + ) + {%- if not loop.last %} AND {% endif %} + {%- endfor %} + + ; + {% endif%} {% endif %}