forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstar.sql
More file actions
58 lines (40 loc) · 2.59 KB
/
star.sql
File metadata and controls
58 lines (40 loc) · 2.59 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
{#-
{{ star(
from=ref('transform_policy_endorsement_coverages'),
except=['Total_Premium']
relation_alias= 'prem',
contains_item = 'Premium', --OPTIONAL
include_or_exclude_contains_item = 'include' --OPTIONAL
) }}
This example would retrive all premium values like (bi_premium, pd_premium) while excluding 'total_premium'.
*column_contains makes it so we only retrieve columns that contain the value being passed*
-#}
{% macro star(from, relation_alias=False, except=[], contains_item = none, include_or_exclude_contains_item = none, separator = ',', column_prefix = none) -%}
{{ return(adapter.dispatch('star', 'cc_dbt_utils')(from, relation_alias, except, contains_item, include_or_exclude_contains_item, separator, column_prefix)) }}
{% endmacro %}
{% macro default__star(from, relation_alias=False, except=[], contains_item = none, include_or_exclude_contains_item = none, separator = ',', column_prefix = none) -%}
{%- do cc_dbt_utils._is_relation(from, 'star') -%}
{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
{%- if not execute -%}
{{ return('') }}
{% endif %}
{%- set include_cols = [] %}
{%- set cols = adapter.get_columns_in_relation(from) -%}
{% if contains_item is not none and include_or_exclude_contains_item is none %}
{%- do exceptions.raise_compiler_error("Macro " ~ macro ~ " expected a value for argument include_or_exclude_contains_item but received none: " ~ obj) -%}
{% endif %}
{%- set in_or_out = include_or_exclude_contains_item if include_or_exclude_contains_item is not none else [] %}
{%- for col in cols -%}
{% if contains_item is not none and col.column not in except and contains_item|upper in col.column and in_or_out == 'include' %}
{% do include_cols.append(col.column) %}
{%- elif contains_item is not none and col.column not in except and contains_item|upper not in col.column and in_or_out == 'exclude' -%}
{% do include_cols.append(col.column) %}
{%- elif contains_item is none and col.column not in except -%}
{% do include_cols.append(col.column) %}
{%- endif %}
{%- endfor %}
{%- for col in include_cols %}
{%- if relation_alias %}{{ relation_alias }}.{% else %}{%- endif -%}{{ adapter.quote(col)|trim }} {%- if column_prefix %} as {{ column_prefix }}{{ col|trim }}{% else %}{%- endif -%}
{%- if not loop.last %}{{ separator }} {{ '\n ' }}{% endif %}
{%- endfor -%}
{%- endmacro %}