forked from dbt-labs/dbt-project-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fct_rejoining_of_upstream_concepts.sql
67 lines (60 loc) · 2.14 KB
/
fct_rejoining_of_upstream_concepts.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
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
with all_relationships as (
select
*
from {{ ref('int_all_dag_relationships') }}
where parent_resource_type not in ('exposures', 'metrics')
and child_resource_type not in ('exposures', 'metrics')
),
-- all parent/child relationships where the parent is BOTH the direct parent of the child and the second level parent of the child
rejoined as (
select
parent,
child
from all_relationships
group by 1, 2
having (sum(case when distance = 1 then 1 else 0 end) >= 1
and sum(case when distance = 2 then 1 else 0 end) >= 1)
),
-- resources with only one direct child
single_use_resources as (
select
parent
from all_relationships
where distance = 1
group by 1
having count(*) = 1
),
-- all cases where one of the parent's direct children (child) is ALSO the direct child of ANOTHER one of the parent's direct childen (parent_and_child)
triad_relationships as (
select
rejoined.parent,
rejoined.child as child,
direct_child.parent as parent_and_child
from rejoined
left join all_relationships as direct_child
on rejoined.child = direct_child.child
and direct_child.distance = 1
left join all_relationships as direct_parent
on rejoined.parent = direct_parent.parent
and direct_parent.distance = 1
where direct_child.parent = direct_parent.child
),
-- additionally, only includes cases where the model "in between" the parent and parent_and_child has NO other downstream dependencies
-- Note: when the "in between" model DOES have downstream dependencies, it's possible this DAG choice has been made to avoid duplicated code and as such is OKAY
final as (
select
triad_relationships.*,
case
when single_use_resources.parent is not null then true
else false
end as is_loop_independent
from triad_relationships
left join single_use_resources
on triad_relationships.parent_and_child = single_use_resources.parent
),
final_filtered as (
select * from final
where is_loop_independent
)
select * from final_filtered
{{ filter_exceptions(this) }}