Skip to content

Commit

Permalink
Merge pull request #251 from dbt-msft/fix-unique-incremental-array
Browse files Browse the repository at this point in the history
fix incremental unique array
  • Loading branch information
sdebruyn authored Jun 7, 2022
2 parents b5f901d + 2f050f6 commit 3f0c1e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,39 @@
{% endmacro %}

{% macro sqlserver__get_delete_insert_merge_sql(target, source, unique_key, dest_columns) %}
{{ default__get_delete_insert_merge_sql(target, source, unique_key, dest_columns) }};

{%- 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 }}
where exists (
SELECT NULL
FROM
{{ source }}
WHERE
{% for key in unique_key %}
{{ source }}.{{ key }} = {{ target }}.{{ key }}
{{ "and " if not loop.last }}
{% endfor %}
);
{% else %}
delete from {{ target }}
where (
{{ unique_key }}) in (
select ({{ unique_key }})
from {{ source }}
);

{% endif %}
{% endif %}

insert into {{ target }} ({{ dest_cols_csv }})
(
select {{ dest_cols_csv }}
from {{ source }}
)

{% endmacro %}

{% macro sqlserver__get_insert_overwrite_merge_sql(target, source, dest_columns, predicates, include_sql_header) %}
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/adapter/test_incremental.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from dbt.tests.adapter.incremental.test_incremental_unique_id import BaseIncrementalUniqueKey


class TestBaseIncrementalUniqueKeySQLServer(BaseIncrementalUniqueKey):
pass

0 comments on commit 3f0c1e0

Please sign in to comment.