forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accepted_range.sql
33 lines (26 loc) · 1.09 KB
/
accepted_range.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
{% test accepted_range(model, column_name, min_value=none, max_value=none, inclusive=true) %}
{{ return(adapter.dispatch('test_accepted_range', 'dbt_utils')(model, column_name, min_value, max_value, inclusive)) }}
{% endtest %}
{% macro default__test_accepted_range(model, column_name, min_value=none, max_value=none, inclusive=true) %}
with meet_condition as(
select *
from {{ model }}
),
validation_errors as (
select *
from meet_condition
where
-- never true, defaults to an empty result set. Exists to ensure any combo of the `or` clauses below succeeds
1 = 2
{%- if min_value is not none %}
-- records with a value >= min_value are permitted. The `not` flips this to find records that don't meet the rule.
or not {{ column_name }} > {{- "=" if inclusive }} {{ min_value }}
{%- endif %}
{%- if max_value is not none %}
-- records with a value <= max_value are permitted. The `not` flips this to find records that don't meet the rule.
or not {{ column_name }} < {{- "=" if inclusive }} {{ max_value }}
{%- endif %}
)
select *
from validation_errors
{% endmacro %}