Skip to content

Commit

Permalink
Merge pull request #418 from basedosdados/feat/not-null-test
Browse files Browse the repository at this point in the history
[feat] add `not_null_multiple_columns` test
  • Loading branch information
arthurfg authored Feb 15, 2024
2 parents 612b00b + 5b5aaaa commit 0def7a2
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/generic/not_null_proportion_multiple_columns.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% test not_null_proportion_multiple_columns(model, threshold=0.99) %}

{%- set columns = adapter.get_columns_in_relation(model) -%}
{% set suffix = '_nulls' %}
{% set pivot_columns_query %}

with null_counts as(

select
{% for column in columns -%}
SUM(CASE WHEN {{ column.name }} IS NULL THEN 1 ELSE 0 END) AS {{ column.name }}{{ suffix }},
{%- endfor %}
count(*) as total_records
from {{ model }}
),

pivot_columns as (

{% for column in columns -%}
select '{{ column.name }}' as column_name, {{ column.name }}{{ suffix }} as quantity, total_records
from null_counts
{% if not loop.last %}union all {% endif %}
{%- endfor %}
),

faulty_columns as (
select
*
from pivot_columns
where
quantity / total_records > (1 - {{ threshold }})


)
select * from faulty_columns
{% endset %}
with validation_errors as (
{%- set errors = dbt_utils.get_query_results_as_dict(pivot_columns_query) -%}
{% for e in errors['column_name'] | unique %}
{{ log("LOG: Coluna com porcentagem de não nulos menor que " ~ threshold * 100 ~ "% ---> " ~ e ~ " [FAIL]", info = True) }}
select '{{e}}' as column
{% if not loop.last %}union all {% endif %}
{% endfor %})

select * from validation_errors
{% endtest %}

0 comments on commit 0def7a2

Please sign in to comment.