Skip to content

Commit

Permalink
Add Snowflake for assertions() and assertions_filter()
Browse files Browse the repository at this point in the history
  • Loading branch information
b-per authored and AxelThevenot committed Jan 29, 2024
1 parent 84c0e7f commit 3ebf216
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
30 changes: 30 additions & 0 deletions macros/_assertions_expression.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,33 @@ ARRAY_CONCAT(
) AS {{ column }}

{%- endmacro %}


{%- macro snowflake___assertions_expression(column, assertions) -%}

ARRAY_CONSTRUCT_COMPACT(
{%- for assertion_id, assertion_config in assertions.items() %}

{%- set expression =
assertion_config.expression
if '\n' not in assertion_config.expression
else assertion_config.expression | indent(12) -%}

{%- set description= assertion_config.description -%}
{%- set null_as_exception =
'FALSE'
if (assertion_config.null_as_exception is not defined
or assertion_config.null_as_exception is true)
else 'TRUE' %}

/* {{ assertion_id }}: {{ description }} */
IFF(
COALESCE({{ expression }}, {{ null_as_exception }}) = FALSE,
'{{ assertion_id }}',
CAST(NULL AS STRING)
),
{%- endfor %}
CAST(NULL AS STRING)
) AS {{ column }}

{%- endmacro %}
26 changes: 26 additions & 0 deletions macros/assertions_filter.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,29 @@ EXISTS (
{%- endif -%}

{%- endmacro %}


{%- macro snowflake__assertions_filter(column, exclude_list, include_list, reverse) -%}

{#- Check if both exclude_list and include_list are provided -#}
{%- if exclude_list is not none and include_list is not none -%}
{{
exceptions.raise_compiler_error(
'exclude_list or include_list must be provided. Not both.'
~ 'Got (exclude_list: ' ~ exclude_list
~ ', include_list: ' ~ include_list ~ ')'
)
}}
{%- endif -%}

{#- Generate filtering expression -#}
{{- 'NOT ' if reverse else '' -}}
{%- if include_list is not none -%}
ARRAY_SIZE(ARRAY_INTERSECTION({{ column }},ARRAY_CONSTRUCT('{{ include_list | join("\', \'")}}'))) = 0
{%- elif exclude_list is not none -%}
ARRAY_SIZE(ARRAY_EXCEPT({{ column }},ARRAY_CONSTRUCT('{{ exclude_list | join("\', \'")}}'))) = 0
{%- else -%}
ARRAY_SIZE({{ column }}) = 0
{%- endif -%}

{%- endmacro %}

0 comments on commit 3ebf216

Please sign in to comment.