forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_combination_of_columns.sql
40 lines (28 loc) · 1.09 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
38
39
40
{% macro test_unique_combination_of_columns(model, quote_columns = false) %}
{{ return(adapter.dispatch('test_unique_combination_of_columns', packages = dbt_utils._get_utils_namespaces())(model, quote_columns, **kwargs)) }}
{% endmacro %}
{% macro default__test_unique_combination_of_columns(model, quote_columns = false) %}
{%- set columns = kwargs.get('combination_of_columns', kwargs.get('arg')) %}
{% if not quote_columns %}
{%- set column_list=columns %}
{% elif quote_columns %}
{%- set column_list=[] %}
{% for column in 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 count(*)
from validation_errors
{% endmacro %}