Skip to content

Commit a2e5780

Browse files
author
Alex Higgs
committed
Merge branch 'release/0.7.5'
2 parents d15aeab + 6ac0c90 commit a2e5780

File tree

11 files changed

+261
-115
lines changed

11 files changed

+261
-115
lines changed

.circleci/config.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ workflows:
8686
only:
8787
- develop
8888
- /^int.*/
89+
- /^fix.*/
8990
test-integration:
9091
jobs:
9192
- integration:
9293
filters:
9394
branches:
9495
only:
9596
- develop
96-
- /^int.*/
97+
- /^int.*/
98+
- /^fix.*/

dbt_project.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: 'dbtvault'
2-
version: '0.7.4'
2+
version: '0.7.5'
33
require-dbt-version: [">=0.18.0", "<0.20.0"]
44
config-version: 2
55

macros/internal/multikey.sql

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
{%- if condition in ['<>', '!=', '='] -%}
1818
{%- for col in columns -%}
19-
{{ (prefix[0] ~ '.') if prefix }}{{ col }} {{ condition }} {{ (prefix[1] ~ '.') if prefix }}{{ col }}
19+
{%- if prefix -%}
20+
{{- dbtvault.prefix([col], prefix[0], alias_target='target') }} {{ condition }} {{ dbtvault.prefix([col], prefix[1]) -}}
21+
{%- endif %}
2022
{%- if not loop.last %} {{ operator }} {% endif %}
2123
{% endfor -%}
2224
{%- else -%}
+12-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
{% macro check_placeholder(model_sql, placeholder='__PERIOD_FILTER__') %}
1+
{%- macro check_placeholder(model_sql, placeholder='__PERIOD_FILTER__') -%}
22

33
{%- if model_sql.find(placeholder) == -1 -%}
44
{%- set error_message -%}
55
Model '{{ model.unique_id }}' does not include the required string '{{ placeholder }}' in its sql
66
{%- endset -%}
7-
{{ exceptions.raise_compiler_error(error_message) }}
7+
{{- exceptions.raise_compiler_error(error_message) -}}
88
{%- endif -%}
99

10-
{% endmacro %}
10+
{%- endmacro -%}
11+
12+
13+
{%- macro is_any_incremental() -%}
14+
{%- if dbtvault.is_vault_insert_by_period() or dbtvault.is_vault_insert_by_rank() or is_incremental() -%}
15+
{%- do return(true) -%}
16+
{%- else -%}
17+
{%- do return(false) -%}
18+
{%- endif -%}
19+
{%- endmacro -%}

macros/staging/rank_columns.sql

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@
1212

1313
{%- if columns[col] is mapping and columns[col].partition_by and columns[col].order_by -%}
1414

15-
{{- "RANK() OVER (PARTITION BY {} ORDER BY {}) AS {}".format(columns[col].partition_by, columns[col].order_by, col) | indent(4) -}}
15+
{%- if dbtvault.is_list(columns[col].order_by) -%}
16+
{%- set order_by_str = columns[col].order_by | join(", ") -%}
17+
{%- else -%}
18+
{%- set order_by_str = columns[col].order_by -%}
19+
{%- endif -%}
20+
21+
{%- if dbtvault.is_list(columns[col].partition_by) -%}
22+
{%- set partition_by_str = columns[col].partition_by | join(", ") -%}
23+
{%- else -%}
24+
{%- set partition_by_str = columns[col].partition_by -%}
25+
{%- endif -%}
26+
27+
{{- "RANK() OVER (PARTITION BY {} ORDER BY {}) AS {}".format(partition_by_str, order_by_str, col) | indent(4) -}}
1628

1729
{%- endif -%}
1830

macros/tables/eff_sat.sql

+74-69
Original file line numberDiff line numberDiff line change
@@ -21,104 +21,109 @@
2121
{{- dbtvault.prepend_generated_by() }}
2222

2323
WITH source_data AS (
24-
SELECT *
25-
FROM {{ ref(source_model) }}
24+
SELECT {{ dbtvault.prefix(source_cols, 'a', alias_target='source') }}
25+
FROM {{ ref(source_model) }} AS a
26+
WHERE {{ dbtvault.multikey(src_dfk, prefix='a', condition='IS NOT NULL') }}
27+
AND {{ dbtvault.multikey(src_sfk, prefix='a', condition='IS NOT NULL') }}
2628
{%- if model.config.materialized == 'vault_insert_by_period' %}
27-
WHERE __PERIOD_FILTER__
28-
{% endif %}
29-
{%- set source_cte = "source_data" %}
30-
),
31-
32-
{%- if model.config.materialized == 'vault_insert_by_rank' %}
33-
rank_col AS (
34-
SELECT * FROM source_data
35-
WHERE __RANK_FILTER__
36-
{%- set source_cte = "rank_col" %}
29+
AND __PERIOD_FILTER__
30+
{%- elif model.config.materialized == 'vault_insert_by_rank' %}
31+
AND __RANK_FILTER__
32+
{%- endif %}
3733
),
38-
{% endif -%}
3934

40-
{%- if load_relation(this) is none %}
35+
{%- if dbtvault.is_any_incremental() %}
4136

42-
records_to_insert AS (
43-
SELECT {{ dbtvault.alias_all(source_cols, 'e') }}
44-
FROM {{ source_cte }} AS e
45-
)
46-
{%- else %}
47-
48-
latest_open_eff AS
49-
(
37+
{# Selecting the most recent records for each link hashkey -#}
38+
latest_records AS (
5039
SELECT {{ dbtvault.alias_all(source_cols, 'b') }},
5140
ROW_NUMBER() OVER (
52-
PARTITION BY
53-
{%- for driving_key in dfk_cols %}
54-
{{ driving_key }}{{ ", " if not loop.last }}
55-
{%- endfor %}
41+
PARTITION BY b.{{ src_pk }}
5642
ORDER BY b.{{ src_ldts }} DESC
57-
) AS row_number
43+
) AS row_num
5844
FROM {{ this }} AS b
59-
WHERE TO_DATE(b.{{ src_end_date }}) = TO_DATE('9999-12-31')
60-
QUALIFY row_number = 1
45+
QUALIFY row_num = 1
6146
),
6247

63-
stage_slice AS
64-
(
65-
SELECT {{ dbtvault.alias_all(source_cols, 'stage') }}
66-
FROM {{ "rank_col" if model.config.materialized == 'vault_insert_by_rank' else "source_data" }} AS stage
48+
{# Selecting the open records of the most recent records for each link hashkey -#}
49+
latest_open AS (
50+
SELECT {{ dbtvault.alias_all(source_cols, 'c') }}
51+
FROM latest_records AS c
52+
WHERE TO_DATE(c.{{ src_end_date }}) = TO_DATE('9999-12-31')
6753
),
6854

69-
new_open_records AS (
70-
SELECT DISTINCT
71-
{{ dbtvault.alias_all(source_cols, 'stage') }}
72-
FROM stage_slice AS stage
73-
LEFT JOIN latest_open_eff AS e
74-
ON stage.{{ src_pk }} = e.{{ src_pk }}
75-
WHERE e.{{ src_pk }} IS NULL
76-
AND {{ dbtvault.multikey(src_dfk, prefix='stage', condition='IS NOT NULL') }}
77-
AND {{ dbtvault.multikey(src_sfk, prefix='stage', condition='IS NOT NULL') }}
55+
{# Selecting the closed records of the most recent records for each link hashkey -#}
56+
latest_closed AS (
57+
SELECT {{ dbtvault.alias_all(source_cols, 'd') }}
58+
FROM latest_records AS d
59+
WHERE TO_DATE(d.{{ src_end_date }}) != TO_DATE('9999-12-31')
7860
),
79-
{%- if is_auto_end_dating %}
8061

81-
links_to_end_date AS (
82-
SELECT a.*
83-
FROM latest_open_eff AS a
84-
LEFT JOIN stage_slice AS b
85-
ON {{ dbtvault.multikey(src_dfk, prefix=['a', 'b'], condition='=') }}
86-
WHERE {{ dbtvault.multikey(src_sfk, prefix='b', condition='IS NULL', operator='OR') }}
87-
OR {{ dbtvault.multikey(src_sfk, prefix=['a', 'b'], condition='<>', operator='OR') }}
62+
{# Identifying the completely new link relationships to be opened in eff sat -#}
63+
new_open_records AS (
64+
SELECT DISTINCT
65+
{{ dbtvault.alias_all(source_cols, 'f') }}
66+
FROM source_data AS f
67+
LEFT JOIN latest_records AS lr
68+
ON f.{{ src_pk }} = lr.{{ src_pk }}
69+
WHERE lr.{{ src_pk }} IS NULL
8870
),
8971

90-
new_end_dated_records AS (
72+
{# Identifying the currently closed link relationships to be reopened in eff sat -#}
73+
new_reopened_records AS (
9174
SELECT DISTINCT
92-
h.{{ src_pk }},
93-
{{ dbtvault.alias_all(fk_cols, 'g') }},
94-
h.EFFECTIVE_FROM AS {{ src_start_date }}, h.{{ src_source }}
95-
FROM latest_open_eff AS h
96-
INNER JOIN links_to_end_date AS g
97-
ON g.{{ src_pk }} = h.{{ src_pk }}
75+
lc.{{ src_pk }},
76+
{{ dbtvault.alias_all(fk_cols, 'lc') }},
77+
lc.{{ src_start_date }} AS {{ src_start_date }},
78+
g.{{ src_end_date }} AS {{ src_end_date }},
79+
g.{{ src_eff }} AS {{ src_eff }},
80+
g.{{ src_ldts }},
81+
g.{{ src_source }}
82+
FROM source_data AS g
83+
INNER JOIN latest_closed lc
84+
ON g.{{ src_pk }} = lc.{{ src_pk }}
9885
),
9986

100-
amended_end_dated_records AS (
87+
{%- if is_auto_end_dating %}
88+
89+
{# Creating the closing records -#}
90+
{# Identifying the currently open relationships that need to be closed due to change in SFK(s) -#}
91+
new_closed_records AS (
10192
SELECT DISTINCT
102-
a.{{ src_pk }},
103-
{{ dbtvault.alias_all(fk_cols, 'a') }},
104-
a.{{ src_start_date }},
105-
stage.{{ src_eff }} AS END_DATE, stage.{{ src_eff }}, stage.{{ src_ldts }},
106-
a.{{ src_source }}
107-
FROM new_end_dated_records AS a
108-
INNER JOIN stage_slice AS stage
109-
ON {{ dbtvault.multikey(src_dfk, prefix=['stage', 'a'], condition='=') }}
110-
WHERE {{ dbtvault.multikey(src_sfk, prefix='stage', condition='IS NOT NULL') }}
111-
AND {{ dbtvault.multikey(src_dfk, prefix='stage', condition='IS NOT NULL') }}
93+
lo.{{ src_pk }},
94+
{{ dbtvault.alias_all(fk_cols, 'lo') }},
95+
lo.{{ src_start_date }} AS {{ src_start_date }},
96+
h.{{ src_eff }} AS {{ src_end_date }},
97+
h.{{ src_eff }} AS {{ src_eff }},
98+
h.{{ src_ldts }},
99+
lo.{{ src_source }}
100+
FROM source_data AS h
101+
INNER JOIN latest_open AS lo
102+
ON {{ dbtvault.multikey(src_dfk, prefix=['lo', 'h'], condition='=') }}
103+
WHERE ({{ dbtvault.multikey(src_sfk, prefix=['lo', 'h'], condition='<>', operator='OR') }})
112104
),
105+
106+
{#- if is_auto_end_dating -#}
113107
{%- endif %}
114108

115109
records_to_insert AS (
116110
SELECT * FROM new_open_records
111+
UNION
112+
SELECT * FROM new_reopened_records
117113
{%- if is_auto_end_dating %}
118114
UNION
119-
SELECT * FROM amended_end_dated_records
115+
SELECT * FROM new_closed_records
120116
{%- endif %}
121117
)
118+
119+
{%- else %}
120+
121+
records_to_insert AS (
122+
SELECT {{ dbtvault.alias_all(source_cols, 'i') }}
123+
FROM source_data AS i
124+
)
125+
126+
{#- if not dbtvault.is_any_incremental() -#}
122127
{%- endif %}
123128

124129
SELECT * FROM records_to_insert

macros/tables/hub.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ row_rank_union AS (
9191
records_to_insert AS (
9292
SELECT {{ dbtvault.prefix(source_cols, 'a', alias_target='target') }}
9393
FROM {{ ns.last_cte }} AS a
94-
{%- if dbtvault.is_vault_insert_by_period() or is_incremental() %}
94+
{%- if dbtvault.is_any_incremental() %}
9595
LEFT JOIN {{ this }} AS d
9696
ON a.{{ src_pk }} = d.{{ src_pk }}
9797
WHERE {{ dbtvault.prefix([src_pk], 'd') }} IS NULL

macros/tables/link.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ row_rank_{{ source_number }} AS (
4141
{%- endif %}
4242
ROW_NUMBER() OVER(
4343
PARTITION BY {{ src_pk }}
44-
ORDER BY {{ src_ldts }} ASC
44+
ORDER BY {{ src_ldts }}
4545
) AS row_number
4646
FROM {{ ref(src) }}
4747
{%- if source_model | length == 1 %}
@@ -96,7 +96,7 @@ row_rank_union AS (
9696
records_to_insert AS (
9797
SELECT {{ dbtvault.prefix(source_cols, 'a', alias_target='target') }}
9898
FROM {{ ns.last_cte }} AS a
99-
{%- if dbtvault.is_vault_insert_by_period() or is_incremental() %}
99+
{%- if dbtvault.is_any_incremental() %}
100100
LEFT JOIN {{ this }} AS d
101101
ON a.{{ src_pk }} = d.{{ src_pk }}
102102
WHERE {{ dbtvault.prefix([src_pk], 'd') }} IS NULL

0 commit comments

Comments
 (0)