forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_combination_of_columns.sql
37 lines (27 loc) · 1.05 KB
/
unique_combination_of_columns.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{% test unique_combination_of_columns(model, combination_of_columns, quote_columns=false) %}
{{ return(adapter.dispatch('test_unique_combination_of_columns', 'dbt_utils')(model, combination_of_columns, quote_columns)) }}
{% endtest %}
{% macro default__test_unique_combination_of_columns(model, combination_of_columns, quote_columns=false) %}
{% if not quote_columns %}
{%- set column_list=combination_of_columns %}
{% elif quote_columns %}
{%- set column_list=[] %}
{% for column in combination_of_columns -%}
{% set column_list = column_list.append( adapter.quote(column) ) %}
{%- endfor %}
{% else %}
{{ exceptions.raise_compiler_error(
"`quote_columns` argument for unique_combination_of_columns test must be one of [True, False] Got: '" ~ quote ~"'.'"
) }}
{% endif %}
{%- set columns_csv=column_list | join(', ') %}
with validation_errors as (
select
{{ columns_csv }}
from {{ model }}
group by {{ columns_csv }}
having count(*) > 1
)
select *
from validation_errors
{% endmacro %}