-
Notifications
You must be signed in to change notification settings - Fork 145
/
expect_column_to_exist.sql
35 lines (23 loc) · 1.07 KB
/
expect_column_to_exist.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
{%- test expect_column_to_exist(model, column_name, column_index=None, transform="upper") -%}
{%- if execute -%}
{%- set column_name = column_name | map(transform) | join -%}
{%- set relation_column_names = dbt_expectations._get_column_list(model, transform) -%}
{%- set matching_column_index = relation_column_names.index(column_name) if column_name in relation_column_names else -1 %}
{%- if column_index -%}
{%- set column_index_0 = column_index - 1 if column_index > 0 else 0 -%}
{%- set column_index_matches = true if matching_column_index == column_index_0 else false %}
{%- else -%}
{%- set column_index_matches = true -%}
{%- endif %}
with test_data as (
select
cast('{{ column_name }}' as {{ dbt.type_string() }}) as column_name,
{{ matching_column_index }} as matching_column_index,
{{ column_index_matches }} as column_index_matches
)
select *
from test_data
where
not(matching_column_index >= 0 and column_index_matches)
{%- endif -%}
{%- endtest -%}