dbt-adapters 0.1.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of dbt-adapters might be problematic. Click here for more details.
- dbt/__init__.py +0 -0
- dbt/adapters/__about__.py +1 -0
- dbt/adapters/__init__.py +7 -0
- dbt/adapters/base/README.md +13 -0
- dbt/adapters/base/__init__.py +15 -0
- dbt/adapters/base/column.py +166 -0
- dbt/adapters/base/connections.py +426 -0
- dbt/adapters/base/impl.py +1654 -0
- dbt/adapters/base/meta.py +131 -0
- dbt/adapters/base/plugin.py +32 -0
- dbt/adapters/base/query_headers.py +101 -0
- dbt/adapters/base/relation.py +471 -0
- dbt/adapters/cache.py +521 -0
- dbt/adapters/capability.py +52 -0
- dbt/adapters/clients/__init__.py +0 -0
- dbt/adapters/clients/jinja.py +24 -0
- dbt/adapters/contracts/__init__.py +0 -0
- dbt/adapters/contracts/connection.py +228 -0
- dbt/adapters/contracts/macros.py +11 -0
- dbt/adapters/contracts/relation.py +125 -0
- dbt/adapters/events/README.md +57 -0
- dbt/adapters/events/__init__.py +0 -0
- dbt/adapters/events/adapter_types.proto +517 -0
- dbt/adapters/events/adapter_types_pb2.py +208 -0
- dbt/adapters/events/base_types.py +40 -0
- dbt/adapters/events/logging.py +83 -0
- dbt/adapters/events/types.py +423 -0
- dbt/adapters/exceptions/__init__.py +40 -0
- dbt/adapters/exceptions/alias.py +24 -0
- dbt/adapters/exceptions/cache.py +68 -0
- dbt/adapters/exceptions/compilation.py +255 -0
- dbt/adapters/exceptions/connection.py +16 -0
- dbt/adapters/exceptions/database.py +51 -0
- dbt/adapters/factory.py +246 -0
- dbt/adapters/protocol.py +173 -0
- dbt/adapters/reference_keys.py +39 -0
- dbt/adapters/relation_configs/README.md +25 -0
- dbt/adapters/relation_configs/__init__.py +12 -0
- dbt/adapters/relation_configs/config_base.py +44 -0
- dbt/adapters/relation_configs/config_change.py +24 -0
- dbt/adapters/relation_configs/config_validation.py +57 -0
- dbt/adapters/sql/__init__.py +2 -0
- dbt/adapters/sql/connections.py +195 -0
- dbt/adapters/sql/impl.py +273 -0
- dbt/adapters/utils.py +69 -0
- dbt/include/global_project/__init__.py +4 -0
- dbt/include/global_project/dbt_project.yml +7 -0
- dbt/include/global_project/docs/overview.md +43 -0
- dbt/include/global_project/macros/adapters/apply_grants.sql +167 -0
- dbt/include/global_project/macros/adapters/columns.sql +137 -0
- dbt/include/global_project/macros/adapters/freshness.sql +16 -0
- dbt/include/global_project/macros/adapters/indexes.sql +41 -0
- dbt/include/global_project/macros/adapters/metadata.sql +96 -0
- dbt/include/global_project/macros/adapters/persist_docs.sql +33 -0
- dbt/include/global_project/macros/adapters/relation.sql +79 -0
- dbt/include/global_project/macros/adapters/schema.sql +20 -0
- dbt/include/global_project/macros/adapters/show.sql +22 -0
- dbt/include/global_project/macros/adapters/timestamps.sql +44 -0
- dbt/include/global_project/macros/adapters/validate_sql.sql +10 -0
- dbt/include/global_project/macros/etc/datetime.sql +62 -0
- dbt/include/global_project/macros/etc/statement.sql +52 -0
- dbt/include/global_project/macros/generic_test_sql/accepted_values.sql +27 -0
- dbt/include/global_project/macros/generic_test_sql/not_null.sql +9 -0
- dbt/include/global_project/macros/generic_test_sql/relationships.sql +23 -0
- dbt/include/global_project/macros/generic_test_sql/unique.sql +12 -0
- dbt/include/global_project/macros/get_custom_name/get_custom_alias.sql +36 -0
- dbt/include/global_project/macros/get_custom_name/get_custom_database.sql +32 -0
- dbt/include/global_project/macros/get_custom_name/get_custom_schema.sql +60 -0
- dbt/include/global_project/macros/materializations/configs.sql +21 -0
- dbt/include/global_project/macros/materializations/hooks.sql +35 -0
- dbt/include/global_project/macros/materializations/models/clone/can_clone_table.sql +7 -0
- dbt/include/global_project/macros/materializations/models/clone/clone.sql +67 -0
- dbt/include/global_project/macros/materializations/models/clone/create_or_replace_clone.sql +7 -0
- dbt/include/global_project/macros/materializations/models/incremental/column_helpers.sql +80 -0
- dbt/include/global_project/macros/materializations/models/incremental/incremental.sql +92 -0
- dbt/include/global_project/macros/materializations/models/incremental/is_incremental.sql +13 -0
- dbt/include/global_project/macros/materializations/models/incremental/merge.sql +131 -0
- dbt/include/global_project/macros/materializations/models/incremental/on_schema_change.sql +144 -0
- dbt/include/global_project/macros/materializations/models/incremental/strategies.sql +79 -0
- dbt/include/global_project/macros/materializations/models/materialized_view.sql +121 -0
- dbt/include/global_project/macros/materializations/models/table.sql +64 -0
- dbt/include/global_project/macros/materializations/models/view.sql +72 -0
- dbt/include/global_project/macros/materializations/seeds/helpers.sql +128 -0
- dbt/include/global_project/macros/materializations/seeds/seed.sql +60 -0
- dbt/include/global_project/macros/materializations/snapshots/helpers.sql +181 -0
- dbt/include/global_project/macros/materializations/snapshots/snapshot.sql +99 -0
- dbt/include/global_project/macros/materializations/snapshots/snapshot_merge.sql +25 -0
- dbt/include/global_project/macros/materializations/snapshots/strategies.sql +174 -0
- dbt/include/global_project/macros/materializations/tests/helpers.sql +14 -0
- dbt/include/global_project/macros/materializations/tests/test.sql +60 -0
- dbt/include/global_project/macros/materializations/tests/where_subquery.sql +15 -0
- dbt/include/global_project/macros/python_model/python.sql +103 -0
- dbt/include/global_project/macros/relations/column/columns_spec_ddl.sql +89 -0
- dbt/include/global_project/macros/relations/create.sql +23 -0
- dbt/include/global_project/macros/relations/create_backup.sql +17 -0
- dbt/include/global_project/macros/relations/create_intermediate.sql +17 -0
- dbt/include/global_project/macros/relations/drop.sql +41 -0
- dbt/include/global_project/macros/relations/drop_backup.sql +14 -0
- dbt/include/global_project/macros/relations/materialized_view/alter.sql +55 -0
- dbt/include/global_project/macros/relations/materialized_view/create.sql +10 -0
- dbt/include/global_project/macros/relations/materialized_view/drop.sql +14 -0
- dbt/include/global_project/macros/relations/materialized_view/refresh.sql +9 -0
- dbt/include/global_project/macros/relations/materialized_view/rename.sql +10 -0
- dbt/include/global_project/macros/relations/materialized_view/replace.sql +10 -0
- dbt/include/global_project/macros/relations/rename.sql +35 -0
- dbt/include/global_project/macros/relations/rename_intermediate.sql +14 -0
- dbt/include/global_project/macros/relations/replace.sql +50 -0
- dbt/include/global_project/macros/relations/schema.sql +8 -0
- dbt/include/global_project/macros/relations/table/create.sql +60 -0
- dbt/include/global_project/macros/relations/table/drop.sql +14 -0
- dbt/include/global_project/macros/relations/table/rename.sql +10 -0
- dbt/include/global_project/macros/relations/table/replace.sql +10 -0
- dbt/include/global_project/macros/relations/view/create.sql +27 -0
- dbt/include/global_project/macros/relations/view/drop.sql +14 -0
- dbt/include/global_project/macros/relations/view/rename.sql +10 -0
- dbt/include/global_project/macros/relations/view/replace.sql +66 -0
- dbt/include/global_project/macros/utils/any_value.sql +9 -0
- dbt/include/global_project/macros/utils/array_append.sql +8 -0
- dbt/include/global_project/macros/utils/array_concat.sql +7 -0
- dbt/include/global_project/macros/utils/array_construct.sql +12 -0
- dbt/include/global_project/macros/utils/bool_or.sql +9 -0
- dbt/include/global_project/macros/utils/cast_bool_to_text.sql +7 -0
- dbt/include/global_project/macros/utils/concat.sql +7 -0
- dbt/include/global_project/macros/utils/data_types.sql +129 -0
- dbt/include/global_project/macros/utils/date_spine.sql +75 -0
- dbt/include/global_project/macros/utils/date_trunc.sql +7 -0
- dbt/include/global_project/macros/utils/dateadd.sql +14 -0
- dbt/include/global_project/macros/utils/datediff.sql +14 -0
- dbt/include/global_project/macros/utils/escape_single_quotes.sql +8 -0
- dbt/include/global_project/macros/utils/except.sql +9 -0
- dbt/include/global_project/macros/utils/generate_series.sql +53 -0
- dbt/include/global_project/macros/utils/hash.sql +7 -0
- dbt/include/global_project/macros/utils/intersect.sql +9 -0
- dbt/include/global_project/macros/utils/last_day.sql +15 -0
- dbt/include/global_project/macros/utils/length.sql +11 -0
- dbt/include/global_project/macros/utils/listagg.sql +30 -0
- dbt/include/global_project/macros/utils/literal.sql +7 -0
- dbt/include/global_project/macros/utils/position.sql +11 -0
- dbt/include/global_project/macros/utils/replace.sql +14 -0
- dbt/include/global_project/macros/utils/right.sql +12 -0
- dbt/include/global_project/macros/utils/safe_cast.sql +9 -0
- dbt/include/global_project/macros/utils/split_part.sql +26 -0
- dbt/include/global_project/tests/generic/builtin.sql +30 -0
- dbt_adapters-0.1.0a1.dist-info/METADATA +81 -0
- dbt_adapters-0.1.0a1.dist-info/RECORD +147 -0
- dbt_adapters-0.1.0a1.dist-info/WHEEL +4 -0
- dbt_adapters-0.1.0a1.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
{#
|
|
2
|
+
Add new columns to the table if applicable
|
|
3
|
+
#}
|
|
4
|
+
{% macro create_columns(relation, columns) %}
|
|
5
|
+
{{ adapter.dispatch('create_columns', 'dbt')(relation, columns) }}
|
|
6
|
+
{% endmacro %}
|
|
7
|
+
|
|
8
|
+
{% macro default__create_columns(relation, columns) %}
|
|
9
|
+
{% for column in columns %}
|
|
10
|
+
{% call statement() %}
|
|
11
|
+
alter table {{ relation }} add column "{{ column.name }}" {{ column.data_type }};
|
|
12
|
+
{% endcall %}
|
|
13
|
+
{% endfor %}
|
|
14
|
+
{% endmacro %}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
{% macro post_snapshot(staging_relation) %}
|
|
18
|
+
{{ adapter.dispatch('post_snapshot', 'dbt')(staging_relation) }}
|
|
19
|
+
{% endmacro %}
|
|
20
|
+
|
|
21
|
+
{% macro default__post_snapshot(staging_relation) %}
|
|
22
|
+
{# no-op #}
|
|
23
|
+
{% endmacro %}
|
|
24
|
+
|
|
25
|
+
{% macro get_true_sql() %}
|
|
26
|
+
{{ adapter.dispatch('get_true_sql', 'dbt')() }}
|
|
27
|
+
{% endmacro %}
|
|
28
|
+
|
|
29
|
+
{% macro default__get_true_sql() %}
|
|
30
|
+
{{ return('TRUE') }}
|
|
31
|
+
{% endmacro %}
|
|
32
|
+
|
|
33
|
+
{% macro snapshot_staging_table(strategy, source_sql, target_relation) -%}
|
|
34
|
+
{{ adapter.dispatch('snapshot_staging_table', 'dbt')(strategy, source_sql, target_relation) }}
|
|
35
|
+
{% endmacro %}
|
|
36
|
+
|
|
37
|
+
{% macro default__snapshot_staging_table(strategy, source_sql, target_relation) -%}
|
|
38
|
+
|
|
39
|
+
with snapshot_query as (
|
|
40
|
+
|
|
41
|
+
{{ source_sql }}
|
|
42
|
+
|
|
43
|
+
),
|
|
44
|
+
|
|
45
|
+
snapshotted_data as (
|
|
46
|
+
|
|
47
|
+
select *,
|
|
48
|
+
{{ strategy.unique_key }} as dbt_unique_key
|
|
49
|
+
|
|
50
|
+
from {{ target_relation }}
|
|
51
|
+
where dbt_valid_to is null
|
|
52
|
+
|
|
53
|
+
),
|
|
54
|
+
|
|
55
|
+
insertions_source_data as (
|
|
56
|
+
|
|
57
|
+
select
|
|
58
|
+
*,
|
|
59
|
+
{{ strategy.unique_key }} as dbt_unique_key,
|
|
60
|
+
{{ strategy.updated_at }} as dbt_updated_at,
|
|
61
|
+
{{ strategy.updated_at }} as dbt_valid_from,
|
|
62
|
+
nullif({{ strategy.updated_at }}, {{ strategy.updated_at }}) as dbt_valid_to,
|
|
63
|
+
{{ strategy.scd_id }} as dbt_scd_id
|
|
64
|
+
|
|
65
|
+
from snapshot_query
|
|
66
|
+
),
|
|
67
|
+
|
|
68
|
+
updates_source_data as (
|
|
69
|
+
|
|
70
|
+
select
|
|
71
|
+
*,
|
|
72
|
+
{{ strategy.unique_key }} as dbt_unique_key,
|
|
73
|
+
{{ strategy.updated_at }} as dbt_updated_at,
|
|
74
|
+
{{ strategy.updated_at }} as dbt_valid_from,
|
|
75
|
+
{{ strategy.updated_at }} as dbt_valid_to
|
|
76
|
+
|
|
77
|
+
from snapshot_query
|
|
78
|
+
),
|
|
79
|
+
|
|
80
|
+
{%- if strategy.invalidate_hard_deletes %}
|
|
81
|
+
|
|
82
|
+
deletes_source_data as (
|
|
83
|
+
|
|
84
|
+
select
|
|
85
|
+
*,
|
|
86
|
+
{{ strategy.unique_key }} as dbt_unique_key
|
|
87
|
+
from snapshot_query
|
|
88
|
+
),
|
|
89
|
+
{% endif %}
|
|
90
|
+
|
|
91
|
+
insertions as (
|
|
92
|
+
|
|
93
|
+
select
|
|
94
|
+
'insert' as dbt_change_type,
|
|
95
|
+
source_data.*
|
|
96
|
+
|
|
97
|
+
from insertions_source_data as source_data
|
|
98
|
+
left outer join snapshotted_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key
|
|
99
|
+
where snapshotted_data.dbt_unique_key is null
|
|
100
|
+
or (
|
|
101
|
+
snapshotted_data.dbt_unique_key is not null
|
|
102
|
+
and (
|
|
103
|
+
{{ strategy.row_changed }}
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
),
|
|
108
|
+
|
|
109
|
+
updates as (
|
|
110
|
+
|
|
111
|
+
select
|
|
112
|
+
'update' as dbt_change_type,
|
|
113
|
+
source_data.*,
|
|
114
|
+
snapshotted_data.dbt_scd_id
|
|
115
|
+
|
|
116
|
+
from updates_source_data as source_data
|
|
117
|
+
join snapshotted_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key
|
|
118
|
+
where (
|
|
119
|
+
{{ strategy.row_changed }}
|
|
120
|
+
)
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
{%- if strategy.invalidate_hard_deletes -%}
|
|
124
|
+
,
|
|
125
|
+
|
|
126
|
+
deletes as (
|
|
127
|
+
|
|
128
|
+
select
|
|
129
|
+
'delete' as dbt_change_type,
|
|
130
|
+
source_data.*,
|
|
131
|
+
{{ snapshot_get_time() }} as dbt_valid_from,
|
|
132
|
+
{{ snapshot_get_time() }} as dbt_updated_at,
|
|
133
|
+
{{ snapshot_get_time() }} as dbt_valid_to,
|
|
134
|
+
snapshotted_data.dbt_scd_id
|
|
135
|
+
|
|
136
|
+
from snapshotted_data
|
|
137
|
+
left join deletes_source_data as source_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key
|
|
138
|
+
where source_data.dbt_unique_key is null
|
|
139
|
+
)
|
|
140
|
+
{%- endif %}
|
|
141
|
+
|
|
142
|
+
select * from insertions
|
|
143
|
+
union all
|
|
144
|
+
select * from updates
|
|
145
|
+
{%- if strategy.invalidate_hard_deletes %}
|
|
146
|
+
union all
|
|
147
|
+
select * from deletes
|
|
148
|
+
{%- endif %}
|
|
149
|
+
|
|
150
|
+
{%- endmacro %}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
{% macro build_snapshot_table(strategy, sql) -%}
|
|
154
|
+
{{ adapter.dispatch('build_snapshot_table', 'dbt')(strategy, sql) }}
|
|
155
|
+
{% endmacro %}
|
|
156
|
+
|
|
157
|
+
{% macro default__build_snapshot_table(strategy, sql) %}
|
|
158
|
+
|
|
159
|
+
select *,
|
|
160
|
+
{{ strategy.scd_id }} as dbt_scd_id,
|
|
161
|
+
{{ strategy.updated_at }} as dbt_updated_at,
|
|
162
|
+
{{ strategy.updated_at }} as dbt_valid_from,
|
|
163
|
+
nullif({{ strategy.updated_at }}, {{ strategy.updated_at }}) as dbt_valid_to
|
|
164
|
+
from (
|
|
165
|
+
{{ sql }}
|
|
166
|
+
) sbq
|
|
167
|
+
|
|
168
|
+
{% endmacro %}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
{% macro build_snapshot_staging_table(strategy, sql, target_relation) %}
|
|
172
|
+
{% set temp_relation = make_temp_relation(target_relation) %}
|
|
173
|
+
|
|
174
|
+
{% set select = snapshot_staging_table(strategy, sql, target_relation) %}
|
|
175
|
+
|
|
176
|
+
{% call statement('build_snapshot_staging_relation') %}
|
|
177
|
+
{{ create_table_as(True, temp_relation, select) }}
|
|
178
|
+
{% endcall %}
|
|
179
|
+
|
|
180
|
+
{% do return(temp_relation) %}
|
|
181
|
+
{% endmacro %}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
{% materialization snapshot, default %}
|
|
2
|
+
{%- set config = model['config'] -%}
|
|
3
|
+
|
|
4
|
+
{%- set target_table = model.get('alias', model.get('name')) -%}
|
|
5
|
+
|
|
6
|
+
{%- set strategy_name = config.get('strategy') -%}
|
|
7
|
+
{%- set unique_key = config.get('unique_key') %}
|
|
8
|
+
-- grab current tables grants config for comparision later on
|
|
9
|
+
{%- set grant_config = config.get('grants') -%}
|
|
10
|
+
|
|
11
|
+
{% set target_relation_exists, target_relation = get_or_create_relation(
|
|
12
|
+
database=model.database,
|
|
13
|
+
schema=model.schema,
|
|
14
|
+
identifier=target_table,
|
|
15
|
+
type='table') -%}
|
|
16
|
+
|
|
17
|
+
{%- if not target_relation.is_table -%}
|
|
18
|
+
{% do exceptions.relation_wrong_type(target_relation, 'table') %}
|
|
19
|
+
{%- endif -%}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
{{ run_hooks(pre_hooks, inside_transaction=False) }}
|
|
23
|
+
|
|
24
|
+
{{ run_hooks(pre_hooks, inside_transaction=True) }}
|
|
25
|
+
|
|
26
|
+
{% set strategy_macro = strategy_dispatch(strategy_name) %}
|
|
27
|
+
{% set strategy = strategy_macro(model, "snapshotted_data", "source_data", config, target_relation_exists) %}
|
|
28
|
+
|
|
29
|
+
{% if not target_relation_exists %}
|
|
30
|
+
|
|
31
|
+
{% set build_sql = build_snapshot_table(strategy, model['compiled_code']) %}
|
|
32
|
+
{% set final_sql = create_table_as(False, target_relation, build_sql) %}
|
|
33
|
+
|
|
34
|
+
{% else %}
|
|
35
|
+
|
|
36
|
+
{{ adapter.valid_snapshot_target(target_relation) }}
|
|
37
|
+
|
|
38
|
+
{% set staging_table = build_snapshot_staging_table(strategy, sql, target_relation) %}
|
|
39
|
+
|
|
40
|
+
-- this may no-op if the database does not require column expansion
|
|
41
|
+
{% do adapter.expand_target_column_types(from_relation=staging_table,
|
|
42
|
+
to_relation=target_relation) %}
|
|
43
|
+
|
|
44
|
+
{% set missing_columns = adapter.get_missing_columns(staging_table, target_relation)
|
|
45
|
+
| rejectattr('name', 'equalto', 'dbt_change_type')
|
|
46
|
+
| rejectattr('name', 'equalto', 'DBT_CHANGE_TYPE')
|
|
47
|
+
| rejectattr('name', 'equalto', 'dbt_unique_key')
|
|
48
|
+
| rejectattr('name', 'equalto', 'DBT_UNIQUE_KEY')
|
|
49
|
+
| list %}
|
|
50
|
+
|
|
51
|
+
{% do create_columns(target_relation, missing_columns) %}
|
|
52
|
+
|
|
53
|
+
{% set source_columns = adapter.get_columns_in_relation(staging_table)
|
|
54
|
+
| rejectattr('name', 'equalto', 'dbt_change_type')
|
|
55
|
+
| rejectattr('name', 'equalto', 'DBT_CHANGE_TYPE')
|
|
56
|
+
| rejectattr('name', 'equalto', 'dbt_unique_key')
|
|
57
|
+
| rejectattr('name', 'equalto', 'DBT_UNIQUE_KEY')
|
|
58
|
+
| list %}
|
|
59
|
+
|
|
60
|
+
{% set quoted_source_columns = [] %}
|
|
61
|
+
{% for column in source_columns %}
|
|
62
|
+
{% do quoted_source_columns.append(adapter.quote(column.name)) %}
|
|
63
|
+
{% endfor %}
|
|
64
|
+
|
|
65
|
+
{% set final_sql = snapshot_merge_sql(
|
|
66
|
+
target = target_relation,
|
|
67
|
+
source = staging_table,
|
|
68
|
+
insert_cols = quoted_source_columns
|
|
69
|
+
)
|
|
70
|
+
%}
|
|
71
|
+
|
|
72
|
+
{% endif %}
|
|
73
|
+
|
|
74
|
+
{% call statement('main') %}
|
|
75
|
+
{{ final_sql }}
|
|
76
|
+
{% endcall %}
|
|
77
|
+
|
|
78
|
+
{% set should_revoke = should_revoke(target_relation_exists, full_refresh_mode=False) %}
|
|
79
|
+
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}
|
|
80
|
+
|
|
81
|
+
{% do persist_docs(target_relation, model) %}
|
|
82
|
+
|
|
83
|
+
{% if not target_relation_exists %}
|
|
84
|
+
{% do create_indexes(target_relation) %}
|
|
85
|
+
{% endif %}
|
|
86
|
+
|
|
87
|
+
{{ run_hooks(post_hooks, inside_transaction=True) }}
|
|
88
|
+
|
|
89
|
+
{{ adapter.commit() }}
|
|
90
|
+
|
|
91
|
+
{% if staging_table is defined %}
|
|
92
|
+
{% do post_snapshot(staging_table) %}
|
|
93
|
+
{% endif %}
|
|
94
|
+
|
|
95
|
+
{{ run_hooks(post_hooks, inside_transaction=False) }}
|
|
96
|
+
|
|
97
|
+
{{ return({'relations': [target_relation]}) }}
|
|
98
|
+
|
|
99
|
+
{% endmaterialization %}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
{% macro snapshot_merge_sql(target, source, insert_cols) -%}
|
|
3
|
+
{{ adapter.dispatch('snapshot_merge_sql', 'dbt')(target, source, insert_cols) }}
|
|
4
|
+
{%- endmacro %}
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
{% macro default__snapshot_merge_sql(target, source, insert_cols) -%}
|
|
8
|
+
{%- set insert_cols_csv = insert_cols | join(', ') -%}
|
|
9
|
+
|
|
10
|
+
merge into {{ target }} as DBT_INTERNAL_DEST
|
|
11
|
+
using {{ source }} as DBT_INTERNAL_SOURCE
|
|
12
|
+
on DBT_INTERNAL_SOURCE.dbt_scd_id = DBT_INTERNAL_DEST.dbt_scd_id
|
|
13
|
+
|
|
14
|
+
when matched
|
|
15
|
+
and DBT_INTERNAL_DEST.dbt_valid_to is null
|
|
16
|
+
and DBT_INTERNAL_SOURCE.dbt_change_type in ('update', 'delete')
|
|
17
|
+
then update
|
|
18
|
+
set dbt_valid_to = DBT_INTERNAL_SOURCE.dbt_valid_to
|
|
19
|
+
|
|
20
|
+
when not matched
|
|
21
|
+
and DBT_INTERNAL_SOURCE.dbt_change_type = 'insert'
|
|
22
|
+
then insert ({{ insert_cols_csv }})
|
|
23
|
+
values ({{ insert_cols_csv }})
|
|
24
|
+
|
|
25
|
+
{% endmacro %}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
{#
|
|
2
|
+
Dispatch strategies by name, optionally qualified to a package
|
|
3
|
+
#}
|
|
4
|
+
{% macro strategy_dispatch(name) -%}
|
|
5
|
+
{% set original_name = name %}
|
|
6
|
+
{% if '.' in name %}
|
|
7
|
+
{% set package_name, name = name.split(".", 1) %}
|
|
8
|
+
{% else %}
|
|
9
|
+
{% set package_name = none %}
|
|
10
|
+
{% endif %}
|
|
11
|
+
|
|
12
|
+
{% if package_name is none %}
|
|
13
|
+
{% set package_context = context %}
|
|
14
|
+
{% elif package_name in context %}
|
|
15
|
+
{% set package_context = context[package_name] %}
|
|
16
|
+
{% else %}
|
|
17
|
+
{% set error_msg %}
|
|
18
|
+
Could not find package '{{package_name}}', called with '{{original_name}}'
|
|
19
|
+
{% endset %}
|
|
20
|
+
{{ exceptions.raise_compiler_error(error_msg | trim) }}
|
|
21
|
+
{% endif %}
|
|
22
|
+
|
|
23
|
+
{%- set search_name = 'snapshot_' ~ name ~ '_strategy' -%}
|
|
24
|
+
|
|
25
|
+
{% if search_name not in package_context %}
|
|
26
|
+
{% set error_msg %}
|
|
27
|
+
The specified strategy macro '{{name}}' was not found in package '{{ package_name }}'
|
|
28
|
+
{% endset %}
|
|
29
|
+
{{ exceptions.raise_compiler_error(error_msg | trim) }}
|
|
30
|
+
{% endif %}
|
|
31
|
+
{{ return(package_context[search_name]) }}
|
|
32
|
+
{%- endmacro %}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
{#
|
|
36
|
+
Create SCD Hash SQL fields cross-db
|
|
37
|
+
#}
|
|
38
|
+
{% macro snapshot_hash_arguments(args) -%}
|
|
39
|
+
{{ adapter.dispatch('snapshot_hash_arguments', 'dbt')(args) }}
|
|
40
|
+
{%- endmacro %}
|
|
41
|
+
|
|
42
|
+
{% macro default__snapshot_hash_arguments(args) -%}
|
|
43
|
+
md5({%- for arg in args -%}
|
|
44
|
+
coalesce(cast({{ arg }} as varchar ), '')
|
|
45
|
+
{% if not loop.last %} || '|' || {% endif %}
|
|
46
|
+
{%- endfor -%})
|
|
47
|
+
{%- endmacro %}
|
|
48
|
+
|
|
49
|
+
{#
|
|
50
|
+
Core strategy definitions
|
|
51
|
+
#}
|
|
52
|
+
{% macro snapshot_timestamp_strategy(node, snapshotted_rel, current_rel, config, target_exists) %}
|
|
53
|
+
{% set primary_key = config['unique_key'] %}
|
|
54
|
+
{% set updated_at = config['updated_at'] %}
|
|
55
|
+
{% set invalidate_hard_deletes = config.get('invalidate_hard_deletes', false) %}
|
|
56
|
+
|
|
57
|
+
{#/*
|
|
58
|
+
The snapshot relation might not have an {{ updated_at }} value if the
|
|
59
|
+
snapshot strategy is changed from `check` to `timestamp`. We
|
|
60
|
+
should use a dbt-created column for the comparison in the snapshot
|
|
61
|
+
table instead of assuming that the user-supplied {{ updated_at }}
|
|
62
|
+
will be present in the historical data.
|
|
63
|
+
|
|
64
|
+
See https://github.com/dbt-labs/dbt-core/issues/2350
|
|
65
|
+
*/ #}
|
|
66
|
+
{% set row_changed_expr -%}
|
|
67
|
+
({{ snapshotted_rel }}.dbt_valid_from < {{ current_rel }}.{{ updated_at }})
|
|
68
|
+
{%- endset %}
|
|
69
|
+
|
|
70
|
+
{% set scd_id_expr = snapshot_hash_arguments([primary_key, updated_at]) %}
|
|
71
|
+
|
|
72
|
+
{% do return({
|
|
73
|
+
"unique_key": primary_key,
|
|
74
|
+
"updated_at": updated_at,
|
|
75
|
+
"row_changed": row_changed_expr,
|
|
76
|
+
"scd_id": scd_id_expr,
|
|
77
|
+
"invalidate_hard_deletes": invalidate_hard_deletes
|
|
78
|
+
}) %}
|
|
79
|
+
{% endmacro %}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
{% macro snapshot_string_as_time(timestamp) -%}
|
|
83
|
+
{{ adapter.dispatch('snapshot_string_as_time', 'dbt')(timestamp) }}
|
|
84
|
+
{%- endmacro %}
|
|
85
|
+
|
|
86
|
+
{% macro default__snapshot_string_as_time(timestamp) %}
|
|
87
|
+
{% do exceptions.raise_not_implemented(
|
|
88
|
+
'snapshot_string_as_time macro not implemented for adapter '+adapter.type()
|
|
89
|
+
) %}
|
|
90
|
+
{% endmacro %}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
{% macro snapshot_check_all_get_existing_columns(node, target_exists, check_cols_config) -%}
|
|
94
|
+
{%- if not target_exists -%}
|
|
95
|
+
{#-- no table yet -> return whatever the query does --#}
|
|
96
|
+
{{ return((false, query_columns)) }}
|
|
97
|
+
{%- endif -%}
|
|
98
|
+
|
|
99
|
+
{#-- handle any schema changes --#}
|
|
100
|
+
{%- set target_relation = adapter.get_relation(database=node.database, schema=node.schema, identifier=node.alias) -%}
|
|
101
|
+
|
|
102
|
+
{% if check_cols_config == 'all' %}
|
|
103
|
+
{%- set query_columns = get_columns_in_query(node['compiled_code']) -%}
|
|
104
|
+
|
|
105
|
+
{% elif check_cols_config is iterable and (check_cols_config | length) > 0 %}
|
|
106
|
+
{#-- query for proper casing/quoting, to support comparison below --#}
|
|
107
|
+
{%- set select_check_cols_from_target -%}
|
|
108
|
+
{#-- N.B. The whitespace below is necessary to avoid edge case issue with comments --#}
|
|
109
|
+
{#-- See: https://github.com/dbt-labs/dbt-core/issues/6781 --#}
|
|
110
|
+
select {{ check_cols_config | join(', ') }} from (
|
|
111
|
+
{{ node['compiled_code'] }}
|
|
112
|
+
) subq
|
|
113
|
+
{%- endset -%}
|
|
114
|
+
{% set query_columns = get_columns_in_query(select_check_cols_from_target) %}
|
|
115
|
+
|
|
116
|
+
{% else %}
|
|
117
|
+
{% do exceptions.raise_compiler_error("Invalid value for 'check_cols': " ~ check_cols_config) %}
|
|
118
|
+
{% endif %}
|
|
119
|
+
|
|
120
|
+
{%- set existing_cols = adapter.get_columns_in_relation(target_relation) | map(attribute = 'name') | list -%}
|
|
121
|
+
{%- set ns = namespace() -%} {#-- handle for-loop scoping with a namespace --#}
|
|
122
|
+
{%- set ns.column_added = false -%}
|
|
123
|
+
|
|
124
|
+
{%- set intersection = [] -%}
|
|
125
|
+
{%- for col in query_columns -%}
|
|
126
|
+
{%- if col in existing_cols -%}
|
|
127
|
+
{%- do intersection.append(adapter.quote(col)) -%}
|
|
128
|
+
{%- else -%}
|
|
129
|
+
{% set ns.column_added = true %}
|
|
130
|
+
{%- endif -%}
|
|
131
|
+
{%- endfor -%}
|
|
132
|
+
{{ return((ns.column_added, intersection)) }}
|
|
133
|
+
{%- endmacro %}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
{% macro snapshot_check_strategy(node, snapshotted_rel, current_rel, config, target_exists) %}
|
|
137
|
+
{% set check_cols_config = config['check_cols'] %}
|
|
138
|
+
{% set primary_key = config['unique_key'] %}
|
|
139
|
+
{% set invalidate_hard_deletes = config.get('invalidate_hard_deletes', false) %}
|
|
140
|
+
{% set updated_at = config.get('updated_at', snapshot_get_time()) %}
|
|
141
|
+
|
|
142
|
+
{% set column_added = false %}
|
|
143
|
+
|
|
144
|
+
{% set column_added, check_cols = snapshot_check_all_get_existing_columns(node, target_exists, check_cols_config) %}
|
|
145
|
+
|
|
146
|
+
{%- set row_changed_expr -%}
|
|
147
|
+
(
|
|
148
|
+
{%- if column_added -%}
|
|
149
|
+
{{ get_true_sql() }}
|
|
150
|
+
{%- else -%}
|
|
151
|
+
{%- for col in check_cols -%}
|
|
152
|
+
{{ snapshotted_rel }}.{{ col }} != {{ current_rel }}.{{ col }}
|
|
153
|
+
or
|
|
154
|
+
(
|
|
155
|
+
(({{ snapshotted_rel }}.{{ col }} is null) and not ({{ current_rel }}.{{ col }} is null))
|
|
156
|
+
or
|
|
157
|
+
((not {{ snapshotted_rel }}.{{ col }} is null) and ({{ current_rel }}.{{ col }} is null))
|
|
158
|
+
)
|
|
159
|
+
{%- if not loop.last %} or {% endif -%}
|
|
160
|
+
{%- endfor -%}
|
|
161
|
+
{%- endif -%}
|
|
162
|
+
)
|
|
163
|
+
{%- endset %}
|
|
164
|
+
|
|
165
|
+
{% set scd_id_expr = snapshot_hash_arguments([primary_key, updated_at]) %}
|
|
166
|
+
|
|
167
|
+
{% do return({
|
|
168
|
+
"unique_key": primary_key,
|
|
169
|
+
"updated_at": updated_at,
|
|
170
|
+
"row_changed": row_changed_expr,
|
|
171
|
+
"scd_id": scd_id_expr,
|
|
172
|
+
"invalidate_hard_deletes": invalidate_hard_deletes
|
|
173
|
+
}) %}
|
|
174
|
+
{% endmacro %}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{% macro get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
|
|
2
|
+
{{ adapter.dispatch('get_test_sql', 'dbt')(main_sql, fail_calc, warn_if, error_if, limit) }}
|
|
3
|
+
{%- endmacro %}
|
|
4
|
+
|
|
5
|
+
{% macro default__get_test_sql(main_sql, fail_calc, warn_if, error_if, limit) -%}
|
|
6
|
+
select
|
|
7
|
+
{{ fail_calc }} as failures,
|
|
8
|
+
{{ fail_calc }} {{ warn_if }} as should_warn,
|
|
9
|
+
{{ fail_calc }} {{ error_if }} as should_error
|
|
10
|
+
from (
|
|
11
|
+
{{ main_sql }}
|
|
12
|
+
{{ "limit " ~ limit if limit != none }}
|
|
13
|
+
) dbt_internal_test
|
|
14
|
+
{%- endmacro %}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{%- materialization test, default -%}
|
|
2
|
+
|
|
3
|
+
{% set relations = [] %}
|
|
4
|
+
|
|
5
|
+
{% if should_store_failures() %}
|
|
6
|
+
|
|
7
|
+
{% set identifier = model['alias'] %}
|
|
8
|
+
{% set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) %}
|
|
9
|
+
|
|
10
|
+
{% set store_failures_as = config.get('store_failures_as') %}
|
|
11
|
+
-- if `--store-failures` is invoked via command line and `store_failures_as` is not set,
|
|
12
|
+
-- config.get('store_failures_as', 'table') returns None, not 'table'
|
|
13
|
+
{% if store_failures_as == none %}{% set store_failures_as = 'table' %}{% endif %}
|
|
14
|
+
{% if store_failures_as not in ['table', 'view'] %}
|
|
15
|
+
{{ exceptions.raise_compiler_error(
|
|
16
|
+
"'" ~ store_failures_as ~ "' is not a valid value for `store_failures_as`. "
|
|
17
|
+
"Accepted values are: ['ephemeral', 'table', 'view']"
|
|
18
|
+
) }}
|
|
19
|
+
{% endif %}
|
|
20
|
+
|
|
21
|
+
{% set target_relation = api.Relation.create(
|
|
22
|
+
identifier=identifier, schema=schema, database=database, type=store_failures_as) -%} %}
|
|
23
|
+
|
|
24
|
+
{% if old_relation %}
|
|
25
|
+
{% do adapter.drop_relation(old_relation) %}
|
|
26
|
+
{% endif %}
|
|
27
|
+
|
|
28
|
+
{% call statement(auto_begin=True) %}
|
|
29
|
+
{{ get_create_sql(target_relation, sql) }}
|
|
30
|
+
{% endcall %}
|
|
31
|
+
|
|
32
|
+
{% do relations.append(target_relation) %}
|
|
33
|
+
|
|
34
|
+
{% set main_sql %}
|
|
35
|
+
select *
|
|
36
|
+
from {{ target_relation }}
|
|
37
|
+
{% endset %}
|
|
38
|
+
|
|
39
|
+
{{ adapter.commit() }}
|
|
40
|
+
|
|
41
|
+
{% else %}
|
|
42
|
+
|
|
43
|
+
{% set main_sql = sql %}
|
|
44
|
+
|
|
45
|
+
{% endif %}
|
|
46
|
+
|
|
47
|
+
{% set limit = config.get('limit') %}
|
|
48
|
+
{% set fail_calc = config.get('fail_calc') %}
|
|
49
|
+
{% set warn_if = config.get('warn_if') %}
|
|
50
|
+
{% set error_if = config.get('error_if') %}
|
|
51
|
+
|
|
52
|
+
{% call statement('main', fetch_result=True) -%}
|
|
53
|
+
|
|
54
|
+
{{ get_test_sql(main_sql, fail_calc, warn_if, error_if, limit)}}
|
|
55
|
+
|
|
56
|
+
{%- endcall %}
|
|
57
|
+
|
|
58
|
+
{{ return({'relations': relations}) }}
|
|
59
|
+
|
|
60
|
+
{%- endmaterialization -%}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{% macro get_where_subquery(relation) -%}
|
|
2
|
+
{% do return(adapter.dispatch('get_where_subquery', 'dbt')(relation)) %}
|
|
3
|
+
{%- endmacro %}
|
|
4
|
+
|
|
5
|
+
{% macro default__get_where_subquery(relation) -%}
|
|
6
|
+
{% set where = config.get('where', '') %}
|
|
7
|
+
{% if where %}
|
|
8
|
+
{%- set filtered -%}
|
|
9
|
+
(select * from {{ relation }} where {{ where }}) dbt_subquery
|
|
10
|
+
{%- endset -%}
|
|
11
|
+
{% do return(filtered) %}
|
|
12
|
+
{%- else -%}
|
|
13
|
+
{% do return(relation) %}
|
|
14
|
+
{%- endif -%}
|
|
15
|
+
{%- endmacro %}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
{% macro resolve_model_name(input_model_name) %}
|
|
2
|
+
{{ return(adapter.dispatch('resolve_model_name', 'dbt')(input_model_name)) }}
|
|
3
|
+
{% endmacro %}
|
|
4
|
+
|
|
5
|
+
{%- macro default__resolve_model_name(input_model_name) -%}
|
|
6
|
+
{{ input_model_name | string | replace('"', '\"') }}
|
|
7
|
+
{%- endmacro -%}
|
|
8
|
+
|
|
9
|
+
{% macro build_ref_function(model) %}
|
|
10
|
+
|
|
11
|
+
{%- set ref_dict = {} -%}
|
|
12
|
+
{%- for _ref in model.refs -%}
|
|
13
|
+
{% set _ref_args = [_ref.get('package'), _ref['name']] if _ref.get('package') else [_ref['name'],] %}
|
|
14
|
+
{%- set resolved = ref(*_ref_args, v=_ref.get('version')) -%}
|
|
15
|
+
{%- if _ref.get('version') -%}
|
|
16
|
+
{% do _ref_args.extend(["v" ~ _ref['version']]) %}
|
|
17
|
+
{%- endif -%}
|
|
18
|
+
{%- do ref_dict.update({_ref_args | join('.'): resolve_model_name(resolved)}) -%}
|
|
19
|
+
{%- endfor -%}
|
|
20
|
+
|
|
21
|
+
def ref(*args, **kwargs):
|
|
22
|
+
refs = {{ ref_dict | tojson }}
|
|
23
|
+
key = '.'.join(args)
|
|
24
|
+
version = kwargs.get("v") or kwargs.get("version")
|
|
25
|
+
if version:
|
|
26
|
+
key += f".v{version}"
|
|
27
|
+
dbt_load_df_function = kwargs.get("dbt_load_df_function")
|
|
28
|
+
return dbt_load_df_function(refs[key])
|
|
29
|
+
|
|
30
|
+
{% endmacro %}
|
|
31
|
+
|
|
32
|
+
{% macro build_source_function(model) %}
|
|
33
|
+
|
|
34
|
+
{%- set source_dict = {} -%}
|
|
35
|
+
{%- for _source in model.sources -%}
|
|
36
|
+
{%- set resolved = source(*_source) -%}
|
|
37
|
+
{%- do source_dict.update({_source | join('.'): resolve_model_name(resolved)}) -%}
|
|
38
|
+
{%- endfor -%}
|
|
39
|
+
|
|
40
|
+
def source(*args, dbt_load_df_function):
|
|
41
|
+
sources = {{ source_dict | tojson }}
|
|
42
|
+
key = '.'.join(args)
|
|
43
|
+
return dbt_load_df_function(sources[key])
|
|
44
|
+
|
|
45
|
+
{% endmacro %}
|
|
46
|
+
|
|
47
|
+
{% macro build_config_dict(model) %}
|
|
48
|
+
{%- set config_dict = {} -%}
|
|
49
|
+
{% set config_dbt_used = zip(model.config.config_keys_used, model.config.config_keys_defaults) | list %}
|
|
50
|
+
{%- for key, default in config_dbt_used -%}
|
|
51
|
+
{# weird type testing with enum, would be much easier to write this logic in Python! #}
|
|
52
|
+
{%- if key == "language" -%}
|
|
53
|
+
{%- set value = "python" -%}
|
|
54
|
+
{%- endif -%}
|
|
55
|
+
{%- set value = model.config.get(key, default) -%}
|
|
56
|
+
{%- do config_dict.update({key: value}) -%}
|
|
57
|
+
{%- endfor -%}
|
|
58
|
+
config_dict = {{ config_dict }}
|
|
59
|
+
{% endmacro %}
|
|
60
|
+
|
|
61
|
+
{% macro py_script_postfix(model) %}
|
|
62
|
+
# This part is user provided model code
|
|
63
|
+
# you will need to copy the next section to run the code
|
|
64
|
+
# COMMAND ----------
|
|
65
|
+
# this part is dbt logic for get ref work, do not modify
|
|
66
|
+
|
|
67
|
+
{{ build_ref_function(model ) }}
|
|
68
|
+
{{ build_source_function(model ) }}
|
|
69
|
+
{{ build_config_dict(model) }}
|
|
70
|
+
|
|
71
|
+
class config:
|
|
72
|
+
def __init__(self, *args, **kwargs):
|
|
73
|
+
pass
|
|
74
|
+
|
|
75
|
+
@staticmethod
|
|
76
|
+
def get(key, default=None):
|
|
77
|
+
return config_dict.get(key, default)
|
|
78
|
+
|
|
79
|
+
class this:
|
|
80
|
+
"""dbt.this() or dbt.this.identifier"""
|
|
81
|
+
database = "{{ this.database }}"
|
|
82
|
+
schema = "{{ this.schema }}"
|
|
83
|
+
identifier = "{{ this.identifier }}"
|
|
84
|
+
{% set this_relation_name = resolve_model_name(this) %}
|
|
85
|
+
def __repr__(self):
|
|
86
|
+
return '{{ this_relation_name }}'
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class dbtObj:
|
|
90
|
+
def __init__(self, load_df_function) -> None:
|
|
91
|
+
self.source = lambda *args: source(*args, dbt_load_df_function=load_df_function)
|
|
92
|
+
self.ref = lambda *args, **kwargs: ref(*args, **kwargs, dbt_load_df_function=load_df_function)
|
|
93
|
+
self.config = config
|
|
94
|
+
self.this = this()
|
|
95
|
+
self.is_incremental = {{ is_incremental() }}
|
|
96
|
+
|
|
97
|
+
# COMMAND ----------
|
|
98
|
+
{{py_script_comment()}}
|
|
99
|
+
{% endmacro %}
|
|
100
|
+
|
|
101
|
+
{#-- entry point for add instuctions for running compiled_code --#}
|
|
102
|
+
{%macro py_script_comment()%}
|
|
103
|
+
{%endmacro%}
|