55
66{%- macro databricks__eff_sat(src_pk, src_dfk, src_sfk, src_extra_columns, src_start_date, src_end_date, src_eff, src_ldts, src_source, source_model) - %}
77
8- {{- automate_dv .default__eff_sat (src_pk= src_pk, src_dfk= src_dfk, src_sfk= src_sfk,
9- src_extra_columns= src_extra_columns,
10- src_start_date= src_start_date, src_end_date= src_end_date,
11- src_eff= src_eff, src_ldts= src_ldts, src_source= src_source,
12- source_model= source_model) - }}
8+ {%- set source_cols = automate_dv .expand_column_list (columns= [src_pk, src_dfk, src_sfk, src_extra_columns, src_start_date, src_end_date, src_eff, src_ldts, src_source]) - %}
9+ {%- set fk_cols = automate_dv .expand_column_list (columns= [src_dfk, src_sfk]) - %}
10+ {%- set dfk_cols = automate_dv .expand_column_list (columns= [src_dfk]) - %}
11+ {%- set is_auto_end_dating = automate_dv .config_meta_get (' is_auto_end_dating' , default= false) %}
12+
13+ {%- set max_datetime = automate_dv .max_datetime () %}
14+
15+ WITH source_data AS (
16+ SELECT {{ automate_dv .prefix (source_cols, ' a' , alias_target= ' source' ) }}
17+ FROM {{ ref(source_model) }} AS a
18+ WHERE {{ automate_dv .multikey (src_dfk, prefix= ' a' , condition= ' IS NOT NULL' ) }}
19+ AND {{ automate_dv .multikey (src_sfk, prefix= ' a' , condition= ' IS NOT NULL' ) }}
20+ {%- if model .config .materialized == ' vault_insert_by_period' %}
21+ AND __PERIOD_FILTER__
22+ {%- elif model .config .materialized == ' vault_insert_by_rank' %}
23+ AND __RANK_FILTER__
24+ {%- endif %}
25+ ),
26+
27+ {%- if automate_dv .is_any_incremental () %}
28+
29+ {# Selecting the most recent records for each link hashkey -#}
30+ latest_records AS (
31+ SELECT {{ automate_dv .alias_all (source_cols, ' b' ) }}
32+ FROM {{ this }} AS b
33+ QUALIFY ROW_NUMBER() OVER (
34+ PARTITION BY {{ automate_dv .prefix ([src_pk], ' b' ) }}
35+ ORDER BY b.{{ src_ldts }} DESC
36+ ) = 1
37+ ),
38+
39+ {# Selecting the open records of the most recent records for each link hashkey -#}
40+ latest_open AS (
41+ SELECT {{ automate_dv .alias_all (source_cols, ' c' ) }}
42+ FROM latest_records AS c
43+ WHERE {{ automate_dv .cast_date (automate_dv .alias (src_end_date, ' c' )) }} = {{ automate_dv .cast_date (automate_dv .cast_datetime (max_datetime, as_string= true)) }}
44+ ),
45+
46+ {# Selecting the closed records of the most recent records for each link hashkey -#}
47+ latest_closed AS (
48+ SELECT {{ automate_dv .alias_all (source_cols, ' d' ) }}
49+ FROM latest_records AS d
50+ WHERE {{ automate_dv .cast_date (automate_dv .alias (src_end_date, ' d' )) }} != {{ automate_dv .cast_date (automate_dv .cast_datetime (max_datetime, as_string= true)) }}
51+ ),
52+
53+ {# Identifying the completely new link relationships to be opened in eff sat -#}
54+ new_open_records AS (
55+ SELECT DISTINCT
56+ {{ automate_dv .alias_all ([src_pk, fk_cols], ' f' ) }},
57+ {%- if automate_dv .is_something (src_extra_columns) %}
58+ {{ automate_dv .prefix ([src_extra_columns], ' f' ) }},
59+ {% endif - %}
60+ {%- if is_auto_end_dating %}
61+ f.{{ src_eff }} AS {{ src_start_date }},
62+ {%- else %}
63+ f.{{ src_start_date }} AS {{ src_start_date }},
64+ {%- endif %}
65+ f.{{ src_end_date }} AS {{ src_end_date }},
66+ f.{{ src_eff }} AS {{ src_eff }},
67+ f.{{ src_ldts }},
68+ f.{{ src_source }}
69+ FROM source_data AS f
70+ LEFT ANTI JOIN latest_records AS lr
71+ ON {{ automate_dv .multikey (src_pk, prefix= [' f' ,' lr' ], condition= ' =' ) }}
72+ ),
73+
74+ {# Identifying the currently closed link relationships to be reopened in eff sat -#}
75+ new_reopened_records AS (
76+ SELECT DISTINCT
77+ {{ automate_dv .alias_all ([src_pk, fk_cols], ' lc' ) }},
78+ {%- if automate_dv .is_something (src_extra_columns) %}
79+ {{ automate_dv .prefix ([src_extra_columns], ' g' ) }},
80+ {% endif - %}
81+ {%- if is_auto_end_dating %}
82+ g.{{ src_eff }} AS {{ src_start_date }},
83+ {%- else %}
84+ g.{{ src_start_date }} AS {{ src_start_date }},
85+ {%- endif %}
86+ g.{{ src_end_date }} AS {{ src_end_date }},
87+ g.{{ src_eff }} AS {{ src_eff }},
88+ g.{{ src_ldts }},
89+ g.{{ src_source }}
90+ FROM source_data AS g
91+ INNER JOIN latest_closed AS lc
92+ ON {{ automate_dv .multikey (src_pk, prefix= [' g' ,' lc' ], condition= ' =' ) }}
93+ WHERE {{ automate_dv .cast_date (automate_dv .alias (src_end_date, ' g' )) }} = {{ automate_dv .cast_date (automate_dv .cast_datetime (max_datetime, as_string= true)) }}
94+ ),
95+
96+ {# Creating the closing records -#}
97+ {# Identifying the currently open relationships that need to be closed due to change in SFK(s) -#}
98+
99+ {%- if is_auto_end_dating %}
100+
101+ new_closed_records AS (
102+ SELECT DISTINCT
103+ {{ automate_dv .alias_all ([src_pk, fk_cols], ' lo' ) }},
104+ {% if automate_dv .is_something (src_extra_columns) %}
105+ {{ automate_dv .prefix ([src_extra_columns], ' h' ) }},
106+ {% endif - %}
107+ lo.{{ src_start_date }} AS {{ src_start_date }},
108+ h.{{ src_eff }} AS {{ src_end_date }},
109+ h.{{ src_eff }} AS {{ src_eff }},
110+ h.{{ src_ldts }},
111+ lo.{{ src_source }}
112+ FROM source_data AS h
113+ INNER JOIN latest_open AS lo
114+ ON {{ automate_dv .multikey (src_dfk, prefix= [' lo' , ' h' ], condition= ' =' ) }}
115+ WHERE NOT EXISTS (SELECT 1 FROM source_data AS stg WHERE {{ automate_dv .multikey (src_pk, prefix= [' stg' ,' lo' ], condition= ' =' ) }}
116+ AND {{ automate_dv .multikey (src_end_date, prefix= [' stg' , ' lo' ], condition= ' =' ) }} )
117+ ),
118+
119+ {# - else if (not) is_auto_end_dating -#}
120+ {% else %}
121+
122+ new_closed_records AS (
123+ SELECT DISTINCT
124+ {{ automate_dv .alias_all ([src_pk, fk_cols], ' lo' ) }},
125+ {% if automate_dv .is_something (src_extra_columns) %}
126+ {{ automate_dv .prefix ([src_extra_columns], ' h' ) }},
127+ {% endif - %}
128+ h.{{ src_start_date }} AS {{ src_start_date }},
129+ h.{{ src_end_date }} AS {{ src_end_date }},
130+ h.{{ src_eff }} AS {{ src_eff }},
131+ h.{{ src_ldts }},
132+ lo.{{ src_source }}
133+ FROM source_data AS h
134+ LEFT OUTER JOIN latest_open AS lo
135+ ON {{ automate_dv .multikey (src_pk, prefix= [' lo' , ' h' ], condition= ' =' ) }}
136+ LEFT ANTI JOIN latest_closed AS lc
137+ ON {{ automate_dv .multikey (src_pk, prefix= [' lc' , ' h' ], condition= ' =' ) }}
138+ WHERE {{ automate_dv .cast_date (automate_dv .alias (src_end_date, ' h' )) }} != {{ automate_dv .cast_date (automate_dv .cast_datetime (max_datetime, as_string= true)) }}
139+ AND {{ automate_dv .multikey (src_pk, prefix= ' lo' , condition= ' IS NOT NULL' ) }}
140+ ),
141+
142+ {# - end if is_auto_end_dating -#}
143+ {%- endif %}
144+
145+ records_to_insert AS (
146+ SELECT * FROM new_open_records
147+ UNION
148+ SELECT * FROM new_reopened_records
149+ UNION
150+ SELECT * FROM new_closed_records
151+ )
152+
153+ {# - else if not automate_dv.is_any_incremental() -#}
154+ {%- else %}
155+
156+ records_to_insert AS (
157+ SELECT {{ automate_dv .alias_all (source_cols, ' i' ) }}
158+ FROM source_data AS i
159+ )
160+
161+ {# - end if not automate_dv.is_any_incremental() -#}
162+ {%- endif %}
163+
164+ SELECT * FROM records_to_insert
13165
14166{%- endmacro - %}
0 commit comments