|
17 | 17 |
|
18 | 18 |
|
19 | 19 | {% macro fabric__build_snapshot_table(strategy, relation) %} |
20 | | - |
| 20 | + {% set columns = config.get('snapshot_table_column_names') or get_snapshot_table_column_names() %} |
21 | 21 | select *, |
22 | | - {{ strategy.scd_id }} as dbt_scd_id, |
23 | | - {{ strategy.updated_at }} as dbt_updated_at, |
24 | | - {{ strategy.updated_at }} as dbt_valid_from, |
25 | | - nullif({{ strategy.updated_at }}, {{ strategy.updated_at }}) as dbt_valid_to |
| 22 | + {{ strategy.scd_id }} as {{ columns.dbt_scd_id }}, |
| 23 | + {{ strategy.updated_at }} as {{ columns.dbt_updated_at }}, |
| 24 | + {{ strategy.updated_at }} as {{ columns.dbt_valid_from }}, |
| 25 | + {{ get_dbt_valid_to_current(strategy, columns) }} |
| 26 | + {%- if strategy.hard_deletes == 'new_record' -%} |
| 27 | + , 'False' as {{ columns.dbt_is_deleted }} |
| 28 | + {% endif -%} |
26 | 29 | from ( |
27 | 30 | select * from {{ relation }} |
28 | 31 | ) sbq |
|
31 | 34 |
|
32 | 35 | {% macro fabric__snapshot_staging_table(strategy, temp_snapshot_relation, target_relation) -%} |
33 | 36 |
|
34 | | - with snapshot_query as ( |
| 37 | + {% set columns = config.get('snapshot_table_column_names') or get_snapshot_table_column_names() %} |
35 | 38 |
|
| 39 | + with snapshot_query as ( |
36 | 40 | select * from {{ temp_snapshot_relation }} |
37 | | - |
38 | 41 | ), |
39 | | - |
40 | 42 | snapshotted_data as ( |
41 | | - |
42 | 43 | select *, |
43 | | - {{ strategy.unique_key }} as dbt_unique_key |
44 | | - |
| 44 | + {{ unique_key_fields(strategy.unique_key) }} |
45 | 45 | from {{ target_relation }} |
46 | | - where dbt_valid_to is null |
47 | | - |
| 46 | + where |
| 47 | + {% if config.get('dbt_valid_to_current') %} |
| 48 | + {# Check for either dbt_valid_to_current OR null, in order to correctly update records with nulls #} |
| 49 | + ( {{ columns.dbt_valid_to }} = {{ config.get('dbt_valid_to_current') }} or {{ columns.dbt_valid_to }} is null) |
| 50 | + {% else %} |
| 51 | + {{ columns.dbt_valid_to }} is null |
| 52 | + {% endif %} |
48 | 53 | ), |
49 | | - |
50 | 54 | insertions_source_data as ( |
51 | | - |
52 | | - select |
53 | | - *, |
54 | | - {{ strategy.unique_key }} as dbt_unique_key, |
55 | | - {{ strategy.updated_at }} as dbt_updated_at, |
56 | | - {{ strategy.updated_at }} as dbt_valid_from, |
57 | | - nullif({{ strategy.updated_at }}, {{ strategy.updated_at }}) as dbt_valid_to, |
58 | | - {{ strategy.scd_id }} as dbt_scd_id |
59 | | - |
| 55 | + select *, |
| 56 | + {{ unique_key_fields(strategy.unique_key) }}, |
| 57 | + {{ strategy.updated_at }} as {{ columns.dbt_updated_at }}, |
| 58 | + {{ strategy.updated_at }} as {{ columns.dbt_valid_from }}, |
| 59 | + {{ get_dbt_valid_to_current(strategy, columns) }}, |
| 60 | + {{ strategy.scd_id }} as {{ columns.dbt_scd_id }} |
60 | 61 | from snapshot_query |
61 | 62 | ), |
62 | | - |
63 | 63 | updates_source_data as ( |
64 | | - |
65 | | - select |
66 | | - *, |
67 | | - {{ strategy.unique_key }} as dbt_unique_key, |
68 | | - {{ strategy.updated_at }} as dbt_updated_at, |
69 | | - {{ strategy.updated_at }} as dbt_valid_from, |
70 | | - {{ strategy.updated_at }} as dbt_valid_to |
71 | | - |
72 | | - from snapshot_query |
73 | | - ), |
74 | | - |
75 | | - {%- if strategy.invalidate_hard_deletes %} |
76 | | - |
77 | | - deletes_source_data as ( |
78 | | - |
79 | | - select |
80 | | - *, |
81 | | - {{ strategy.unique_key }} as dbt_unique_key |
| 64 | + select *, |
| 65 | + {{ unique_key_fields(strategy.unique_key) }}, |
| 66 | + {{ strategy.updated_at }} as {{ columns.dbt_updated_at }}, |
| 67 | + {{ strategy.updated_at }} as {{ columns.dbt_valid_from }}, |
| 68 | + {{ strategy.updated_at }} as {{ columns.dbt_valid_to }} |
82 | 69 | from snapshot_query |
83 | 70 | ), |
| 71 | + {%- if strategy.hard_deletes == 'invalidate' or strategy.hard_deletes == 'new_record' %} |
| 72 | + deletes_source_data as ( |
| 73 | + select *, {{ unique_key_fields(strategy.unique_key) }} |
| 74 | + from snapshot_query |
| 75 | + ), |
84 | 76 | {% endif %} |
85 | | - |
86 | 77 | insertions as ( |
87 | | - |
88 | | - select |
89 | | - 'insert' as dbt_change_type, |
90 | | - source_data.* |
91 | | - |
| 78 | + select 'insert' as dbt_change_type, source_data.* |
| 79 | + {%- if strategy.hard_deletes == 'new_record' -%} |
| 80 | + ,'False' as {{ columns.dbt_is_deleted }} |
| 81 | + {%- endif %} |
92 | 82 | from insertions_source_data as source_data |
93 | | - left outer join snapshotted_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key |
94 | | - where snapshotted_data.dbt_unique_key is null |
95 | | - or ( |
96 | | - snapshotted_data.dbt_unique_key is not null |
97 | | - and ( |
98 | | - {{ strategy.row_changed }} |
99 | | - ) |
100 | | - ) |
101 | | - |
| 83 | + left outer join snapshotted_data |
| 84 | + on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }} |
| 85 | + where {{ unique_key_is_null(strategy.unique_key, "snapshotted_data") }} |
| 86 | + or ({{ unique_key_is_not_null(strategy.unique_key, "snapshotted_data") }} and ({{ strategy.row_changed }})) |
102 | 87 | ), |
103 | | - |
104 | 88 | updates as ( |
105 | | - |
106 | | - select |
107 | | - 'update' as dbt_change_type, |
108 | | - source_data.*, |
109 | | - snapshotted_data.dbt_scd_id |
110 | | - |
| 89 | + select 'update' as dbt_change_type, source_data.*, |
| 90 | + snapshotted_data.{{ columns.dbt_scd_id }} |
| 91 | + {%- if strategy.hard_deletes == 'new_record' -%} |
| 92 | + , snapshotted_data.{{ columns.dbt_is_deleted }} |
| 93 | + {%- endif %} |
111 | 94 | from updates_source_data as source_data |
112 | | - join snapshotted_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key |
113 | | - where ( |
114 | | - {{ strategy.row_changed }} |
115 | | - ) |
| 95 | + join snapshotted_data |
| 96 | + on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }} |
| 97 | + where ({{ strategy.row_changed }}) |
116 | 98 | ) |
117 | | - |
118 | | - {%- if strategy.invalidate_hard_deletes -%} |
119 | | - , |
120 | | - |
121 | | - deletes as ( |
122 | | - |
123 | | - select |
124 | | - 'delete' as dbt_change_type, |
| 99 | + {%- if strategy.hard_deletes == 'invalidate' or strategy.hard_deletes == 'new_record' %} |
| 100 | + , |
| 101 | + deletes as ( |
| 102 | + select 'delete' as dbt_change_type, |
125 | 103 | source_data.*, |
126 | | - {{ snapshot_get_time() }} as dbt_valid_from, |
127 | | - {{ snapshot_get_time() }} as dbt_updated_at, |
128 | | - {{ snapshot_get_time() }} as dbt_valid_to, |
129 | | - snapshotted_data.dbt_scd_id |
130 | | - |
131 | | - from snapshotted_data |
132 | | - left join deletes_source_data as source_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key |
133 | | - where source_data.dbt_unique_key is null |
134 | | - ) |
| 104 | + {{ snapshot_get_time() }} as {{ columns.dbt_valid_from }}, |
| 105 | + {{ snapshot_get_time() }} as {{ columns.dbt_updated_at }}, |
| 106 | + {{ snapshot_get_time() }} as {{ columns.dbt_valid_to }}, |
| 107 | + snapshotted_data.{{ columns.dbt_scd_id }} |
| 108 | + {%- if strategy.hard_deletes == 'new_record' -%} |
| 109 | + , snapshotted_data.{{ columns.dbt_is_deleted }} |
| 110 | + {%- endif %} |
| 111 | + from snapshotted_data |
| 112 | + left join deletes_source_data as source_data |
| 113 | + on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }} |
| 114 | + where {{ unique_key_is_null(strategy.unique_key, "source_data") }} |
| 115 | + ) |
135 | 116 | {%- endif %} |
| 117 | + {%- if strategy.hard_deletes == 'new_record' %} |
| 118 | + {%set source_query = "select * from "~temp_snapshot_relation%} |
| 119 | + {% set source_sql_cols = get_column_schema_from_query(source_query) %} |
| 120 | + , |
| 121 | + deletion_records as ( |
136 | 122 |
|
| 123 | + select |
| 124 | + 'insert' as dbt_change_type, |
| 125 | + {%- for col in source_sql_cols -%} |
| 126 | + snapshotted_data.{{ adapter.quote(col.column) }}, |
| 127 | + {% endfor -%} |
| 128 | + {%- if strategy.unique_key | is_list -%} |
| 129 | + {%- for key in strategy.unique_key -%} |
| 130 | + snapshotted_data.{{ key }} as dbt_unique_key_{{ loop.index }}, |
| 131 | + {% endfor -%} |
| 132 | + {%- else -%} |
| 133 | + snapshotted_data.dbt_unique_key as dbt_unique_key, |
| 134 | + {% endif -%} |
| 135 | + {{ snapshot_get_time() }} as {{ columns.dbt_valid_from }}, |
| 136 | + {{ snapshot_get_time() }} as {{ columns.dbt_updated_at }}, |
| 137 | + snapshotted_data.{{ columns.dbt_valid_to }} as {{ columns.dbt_valid_to }}, |
| 138 | + snapshotted_data.{{ columns.dbt_scd_id }}, |
| 139 | + 'True' as {{ columns.dbt_is_deleted }} |
| 140 | + from snapshotted_data |
| 141 | + left join deletes_source_data as source_data |
| 142 | + on {{ unique_key_join_on(strategy.unique_key, "snapshotted_data", "source_data") }} |
| 143 | + where {{ unique_key_is_null(strategy.unique_key, "source_data") }} |
| 144 | + ) |
| 145 | + {%- endif %} |
137 | 146 | select * from insertions |
138 | 147 | union all |
139 | 148 | select * from updates |
140 | | - {%- if strategy.invalidate_hard_deletes %} |
141 | | - union all |
142 | | - select * from deletes |
| 149 | + {%- if strategy.hard_deletes == 'invalidate' or strategy.hard_deletes == 'new_record' %} |
| 150 | + union all |
| 151 | + select * from deletes |
| 152 | + {%- endif %} |
| 153 | + {%- if strategy.hard_deletes == 'new_record' %} |
| 154 | + union all |
| 155 | + select * from deletion_records |
143 | 156 | {%- endif %} |
144 | 157 |
|
145 | 158 | {%- endmacro %} |
|
0 commit comments