forked from dbt-labs/dbt-project-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_relationship_values.sql
More file actions
61 lines (44 loc) · 2.52 KB
/
get_relationship_values.sql
File metadata and controls
61 lines (44 loc) · 2.52 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
59
60
61
{%- macro get_relationship_values(node_type) -%}
{{ return(adapter.dispatch('get_relationship_values', 'dbt_project_evaluator')(node_type)) }}
{%- endmacro -%}
{%- macro default__get_relationship_values(node_type) -%}
{%- if execute -%}
{%- if node_type == 'nodes' %}
{% set nodes_list = graph.nodes.values() %}
{%- elif node_type == 'exposures' -%}
{% set nodes_list = graph.exposures.values() %}
{%- elif node_type == 'metrics' -%}
{% set nodes_list = graph.metrics.values() %}
{%- else -%}
{{ exceptions.raise_compiler_error("node_type needs to be either nodes, exposures or metrics, got " ~ node_type) }}
{% endif -%}
{%- set values = [] -%}
{%- for node in nodes_list -%}
{%- if node.get('depends_on',{}).get('nodes',[]) |length == 0 -%}
{%- set values_line =
[
"cast('" ~ node.unique_id ~ "' as " ~ dbt_project_evaluator.type_string_dpe() ~ ")",
"cast(NULL as " ~ dbt_project_evaluator.type_string_dpe() ~ ")",
"cast(" ~ dbt_project_evaluator.bool_literal(false) | trim ~ " as " ~ dbt.type_boolean() ~ ")",
]
%}
{%- do values.append(values_line) -%}
{%- else -%}
{%- for parent in node.get('depends_on',{}).get('nodes',[]) -%}
{# Use attached_node to determine primary relationship for tests (works in both Core and Fusion) #}
{# Fall back to loop.last for backwards compatibility if attached_node is not available #}
{%- set is_primary = (parent == node.get('attached_node')) if node.get('attached_node') else loop.last -%}
{%- set values_line =
[
"cast('" ~ node.unique_id ~ "' as " ~ dbt_project_evaluator.type_string_dpe() ~ ")",
"cast('" ~ parent ~ "' as " ~ dbt_project_evaluator.type_string_dpe() ~ ")",
"cast(" ~ dbt_project_evaluator.bool_literal(is_primary if node.unique_id.split('.')[0] == 'test' else false) | trim ~ " as " ~ dbt.type_boolean() ~ ")"
]
%}
{%- do values.append(values_line) -%}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{{ return(values) }}
{%- endif -%}
{%- endmacro -%}