Skip to content

Commit

Permalink
Added extension to the unique_combination_of_columns schema test in…
Browse files Browse the repository at this point in the history
… order to support a `WHERE` clause that can be used to isolate the tuples on which the uniqueness for the combination of columns needs to be verified.
  • Loading branch information
findinpath committed Nov 24, 2020
1 parent 163251d commit 6e934d7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# dbt-utils v0.7.0 (unreleased)
* Added optional `where` clause in `unique_combination_of_columns` test macro [#295](https://github.com/fishtown-analytics/dbt-utils/pull/295) [findinpath](https://github.com/findinpath)

## Features
* Add new `accepted_range` test ([#276](https://github.com/fishtown-analytics/dbt-utils/pull/276) [@joellabes](https://github.com/joellabes))
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,19 @@ An optional `quote_columns` parameter (`default=false`) can also be used if a co
quote_columns: true
```

An optional `where` parameter can also be used in order to isolate the rows of the data set on which the uniqueness
constraint needs to be verified.

```yaml
- name: revenue_by_product_by_month
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- month
- group
where: year >= 2020
```


#### accepted_range ([source](macros/schema_tests/accepted_range.sql))
This test checks that a column's values fall inside an expected range. Any combination of `min_value` and `max_value` is allowed, and the range can be inclusive or exclusive. Provide a `where` argument to filter to specific records only.
Expand Down
9 changes: 4 additions & 5 deletions macros/schema_tests/unique_combination_of_columns.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{% macro test_unique_combination_of_columns(model, quote_columns = false) %}
{% macro test_unique_combination_of_columns(model, combination_of_columns, quote_columns = false, where = None) %}

{%- set columns = kwargs.get('combination_of_columns', kwargs.get('arg')) %}

{% if not quote_columns %}
{%- set column_list=columns %}
{%- set column_list=combination_of_columns %}
{% elif quote_columns %}
{%- set column_list=[] %}
{% for column in columns -%}
{% for column in combination_of_columns -%}
{% set column_list = column_list.append( adapter.quote(column) ) %}
{%- endfor %}
{% else %}
Expand All @@ -23,7 +22,7 @@ with validation_errors as (
select
{{ columns_csv }}
from {{ model }}

{% if where %}where {{ where }} {% endif %}
group by {{ columns_csv }}
having count(*) > 1

Expand Down

0 comments on commit 6e934d7

Please sign in to comment.