forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_between.sql
46 lines (32 loc) · 865 Bytes
/
is_between.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
41
42
43
44
45
46
{% macro test_is_between(model, column_name, allow_nulls=false) %}
{% set min = kwargs.get('min', kwargs.get('arg')) %}
{% set max = kwargs.get('max', kwargs.get('arg')) %}
{# Make sure at at least one boundary is defined #}
{% if min is none and max is none %}
{{ exceptions.raise_compiler_error(
"You have to define at least one of `min` or `max` for the test!")
}}
{% endif %}
with validation as (
select
{{ column_name }} as column_to_test
from {{ model }}
),
validation_errors as (
select
column_to_test
from validation
where false
{% if min is not none %}
or column_to_test < {{ min }}
{% endif %}
{% if max is not none %}
or column_to_test > {{ max }}
{% endif %}
{% if allow_nulls is false %}
or column_to_test is null
{% endif %}
)
select count(*)
from validation_errors
{% endmacro %}