forked from dbt-labs/dbt-project-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint_direct_relationships.sql
More file actions
84 lines (77 loc) · 2.89 KB
/
int_direct_relationships.sql
File metadata and controls
84 lines (77 loc) · 2.89 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-- one record for each resource in the graph and its direct parent
with
all_graph_resources as (
select
resource_id,
resource_name,
resource_type,
file_path,
directory_path,
file_name,
model_type,
materialized,
is_public,
access,
source_name,
is_excluded
from {{ ref('int_all_graph_resources') }}
),
direct_model_relationships as (
select
resource_id,
direct_parent_id,
is_primary_relationship
from {{ ref('stg_node_relationships')}}
),
direct_exposure_relationships as (
select
resource_id,
direct_parent_id,
is_primary_relationship
from {{ ref('stg_exposure_relationships')}}
),
direct_metrics_relationships as (
select
resource_id,
direct_parent_id,
is_primary_relationship
from {{ ref('stg_metric_relationships')}}
),
-- for all resources in the graph, find their direct parent
direct_relationships as (
select
all_graph_resources.resource_id as resource_id,
all_graph_resources.resource_name as resource_name,
all_graph_resources.resource_type as resource_type,
all_graph_resources.file_path as file_path,
all_graph_resources.directory_path as directory_path,
all_graph_resources.file_name as file_name,
all_graph_resources.model_type as model_type,
all_graph_resources.materialized as materialized,
all_graph_resources.is_public as is_public,
all_graph_resources.access as access,
all_graph_resources.source_name as source_name,
all_graph_resources.is_excluded as is_excluded,
case
when all_graph_resources.resource_type = 'source' then null
when all_graph_resources.resource_type = 'exposure' then exposures.direct_parent_id
when all_graph_resources.resource_type = 'metric' then metrics.direct_parent_id
when all_graph_resources.resource_type in ('model', 'snapshot', 'test') then models.direct_parent_id
else null
end as direct_parent_id,
case when all_graph_resources.resource_type = 'test' and models.is_primary_relationship = cast(1 as {{ dbt.type_boolean() }}) then cast(1 as {{ dbt.type_boolean() }}) else cast(0 as {{ dbt.type_boolean() }}) end as is_primary_test_relationship
from all_graph_resources
left join direct_model_relationships as models
on all_graph_resources.resource_id = models.resource_id
left join direct_exposure_relationships as exposures
on all_graph_resources.resource_id = exposures.resource_id
left join direct_metrics_relationships as metrics
on all_graph_resources.resource_id = metrics.resource_id
),
final as (
select
{{ dbt_utils.generate_surrogate_key(['resource_id', 'direct_parent_id']) }} as unique_id,
*
from direct_relationships
)
select * from final