-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cherry Pick: not null proportion schema test (#411)
* Add not_null_proportion schema test and related integration tests * Update CHANGELOG * Fix csv formatting and numeric typecasting Co-authored-by: Simo Tumelius <simo.tumelius@gmail.com>
- Loading branch information
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
integration_tests/data/schema_tests/data_not_null_proportion.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
point_5,point_9 | ||
1,1 | ||
,2 | ||
,3 | ||
4,4 | ||
5,5 | ||
6,6 | ||
,7 | ||
,8 | ||
, | ||
10,10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{% macro test_not_null_proportion(model) %} | ||
{{ return(adapter.dispatch('test_not_null_proportion', packages = dbt_utils._get_utils_namespaces())(model, **kwargs)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__test_not_null_proportion(model) %} | ||
|
||
{% set column_name = kwargs.get('column_name', kwargs.get('arg')) %} | ||
{% set at_least = kwargs.get('at_least', kwargs.get('arg')) %} | ||
{% set at_most = kwargs.get('at_most', kwargs.get('arg', 1)) %} | ||
|
||
with validation as ( | ||
select | ||
sum(case when {{ column_name }} is null then 0 else 1 end) / cast(count(*) as numeric) as not_null_proportion | ||
from {{ model }} | ||
), | ||
validation_errors as ( | ||
select | ||
not_null_proportion | ||
from validation | ||
where not_null_proportion < {{ at_least }} or not_null_proportion > {{ at_most }} | ||
) | ||
select | ||
count(*) | ||
from validation_errors | ||
|
||
{% endmacro %} |