-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from mikaelene/v0.18.0
V0.18.0
- Loading branch information
Showing
10 changed files
with
83 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
dbt/include/sqlserver/macros/materializations/incremental/incremental.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 0 additions & 118 deletions
118
dbt/include/sqlserver/macros/materializations/snapshot/snapshot.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,4 @@ | ||
{% macro snapshot_staging_table_inserts(strategy, source_sql, target_relation) -%} | ||
|
||
select | ||
'insert' as dbt_change_type, | ||
source_data.* | ||
|
||
from ( | ||
|
||
select *, | ||
COALESCE({{ strategy.scd_id }}, NULL) as dbt_scd_id, | ||
COALESCE({{ strategy.unique_key }}, NULL) as dbt_unique_key, | ||
COALESCE({{ strategy.updated_at }}, NULL) as dbt_updated_at, | ||
COALESCE({{ strategy.updated_at }}, NULL) as dbt_valid_from, | ||
nullif({{ strategy.updated_at }}, {{ strategy.updated_at }}) as dbt_valid_to | ||
|
||
from ( | ||
{{ source_sql }} | ||
) snapshot_query | ||
) source_data | ||
left outer join ( | ||
|
||
select *, | ||
{{ strategy.unique_key }} as dbt_unique_key | ||
|
||
from {{ target_relation }} | ||
|
||
) snapshotted_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key | ||
where snapshotted_data.dbt_unique_key is null | ||
or ( | ||
snapshotted_data.dbt_unique_key is not null | ||
and snapshotted_data.dbt_valid_to is null | ||
and ( | ||
{{ strategy.row_changed }} | ||
) | ||
) | ||
|
||
{%- endmacro %} | ||
|
||
|
||
{% macro snapshot_staging_table_updates(strategy, source_sql, target_relation) -%} | ||
|
||
select | ||
'update' as dbt_change_type, | ||
snapshotted_data.dbt_scd_id, | ||
source_data.dbt_valid_from as dbt_valid_to | ||
|
||
from ( | ||
|
||
select | ||
*, | ||
COALESCE({{ strategy.scd_id }}, NULL) as dbt_scd_id, | ||
COALESCE({{ strategy.unique_key }}, NULL) as dbt_unique_key, | ||
COALESCE({{ strategy.updated_at }}, NULL) as dbt_updated_at, | ||
COALESCE({{ strategy.updated_at }}, NULL) as dbt_valid_from | ||
|
||
from ( | ||
{{ source_sql }} | ||
) snapshot_query | ||
) source_data | ||
join ( | ||
|
||
select *, | ||
{{ strategy.unique_key }} as dbt_unique_key | ||
|
||
from {{ target_relation }} | ||
|
||
) snapshotted_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key | ||
where snapshotted_data.dbt_valid_to is null | ||
and ( | ||
{{ strategy.row_changed }} | ||
) | ||
|
||
{%- endmacro %} | ||
|
||
|
||
{% macro build_snapshot_staging_table(strategy, sql, target_relation) %} | ||
{% set tmp_relation = make_temp_relation(target_relation) %} | ||
|
||
{% set inserts_select = snapshot_staging_table_inserts(strategy, sql, target_relation) %} | ||
{% set updates_select = snapshot_staging_table_updates(strategy, sql, target_relation) %} | ||
|
||
{% call statement('build_snapshot_staging_relation_inserts') %} | ||
{{ create_table_as(False, tmp_relation, inserts_select) }} | ||
{% endcall %} | ||
|
||
{% call statement('build_snapshot_staging_relation_updates') %} | ||
insert into {{ tmp_relation }} (dbt_change_type, dbt_scd_id, dbt_valid_to) | ||
select dbt_change_type, dbt_scd_id, dbt_valid_to from ( | ||
{{ updates_select }} | ||
) dbt_sbq; | ||
{% endcall %} | ||
|
||
{% do return(tmp_relation) %} | ||
{% endmacro %} | ||
|
||
|
||
{% macro sqlserver__post_snapshot(staging_relation) %} | ||
-- Clean up the snapshot temp table | ||
{% do drop_relation(staging_relation) %} | ||
{% endmacro %} | ||
|
||
|
||
{% macro build_snapshot_table(strategy, sql) %} | ||
|
||
select *, | ||
COALESCE({{ strategy.scd_id }}, NULL) as dbt_scd_id, | ||
COALESCE({{ strategy.updated_at }}, NULL) as dbt_updated_at, | ||
COALESCE({{ strategy.updated_at }}, NULL) as dbt_valid_from, | ||
nullif({{ strategy.updated_at }}, {{ strategy.updated_at }}) as dbt_valid_to | ||
from ( | ||
{{ sql }} | ||
) sbq | ||
|
||
{% endmacro %} | ||
|
||
{% macro sqlserver__create_columns(relation, columns) %} | ||
{% for column in columns %} | ||
{% call statement() %} | ||
alter table {{ relation }} add "{{ column.name }}" {{ column.data_type }}; | ||
{% endcall %} | ||
{% endfor %} | ||
{% endmacro %} |
78 changes: 39 additions & 39 deletions
78
dbt/include/sqlserver/macros/materializations/snapshot/strategies.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
{% macro sqlserver__snapshot_hash_arguments(args) %} | ||
CONVERT(VARCHAR(32), HashBytes('MD5', {% for arg in args %} | ||
coalesce(cast({{ arg }} as varchar ), '') {% if not loop.last %} + '|' + {% endif %} | ||
{% endfor %}), 2) | ||
{% endmacro %} | ||
|
||
{% macro snapshot_check_strategy(node, snapshotted_rel, current_rel, config, target_exists) %} | ||
{% set check_cols_config = config['check_cols'] %} | ||
{% set primary_key = config['unique_key'] %} | ||
{% set updated_at = snapshot_get_time() %} | ||
|
||
{% if check_cols_config == 'all' %} | ||
{% set check_cols = get_columns_in_query(node['injected_sql']) %} | ||
{% elif check_cols_config is iterable and (check_cols_config | length) > 0 %} | ||
{% set check_cols = check_cols_config %} | ||
{% else %} | ||
{% do exceptions.raise_compiler_error("Invalid value for 'check_cols': " ~ check_cols_config) %} | ||
{% endif %} | ||
|
||
{% set row_changed_expr -%} | ||
( | ||
{% for col in check_cols %} | ||
{{ snapshotted_rel }}.{{ col }} != {{ current_rel }}.{{ col }} | ||
or | ||
({{ snapshotted_rel }}.{{ col }} is null) and not ({{ current_rel }}.{{ col }} is null) | ||
{%- if not loop.last %} or {% endif %} | ||
|
||
{% endfor %} | ||
) | ||
{%- endset %} | ||
|
||
{% set scd_id_expr = snapshot_hash_arguments([primary_key, updated_at]) %} | ||
|
||
{% do return({ | ||
"unique_key": primary_key, | ||
"updated_at": updated_at, | ||
"row_changed": row_changed_expr, | ||
"scd_id": scd_id_expr | ||
}) %} | ||
{% macro sqlserver__snapshot_hash_arguments(args) %} | ||
CONVERT(VARCHAR(32), HashBytes('MD5', {% for arg in args %} | ||
coalesce(cast({{ arg }} as varchar ), '') {% if not loop.last %} + '|' + {% endif %} | ||
{% endfor %}), 2) | ||
{% endmacro %} | ||
|
||
{% macro snapshot_check_strategy(node, snapshotted_rel, current_rel, config, target_exists) %} | ||
{% set check_cols_config = config['check_cols'] %} | ||
{% set primary_key = config['unique_key'] %} | ||
{% set updated_at = snapshot_get_time() %} | ||
|
||
{% if check_cols_config == 'all' %} | ||
{% set check_cols = get_columns_in_query(node['injected_sql']) %} | ||
{% elif check_cols_config is iterable and (check_cols_config | length) > 0 %} | ||
{% set check_cols = check_cols_config %} | ||
{% else %} | ||
{% do exceptions.raise_compiler_error("Invalid value for 'check_cols': " ~ check_cols_config) %} | ||
{% endif %} | ||
|
||
{% set row_changed_expr -%} | ||
( | ||
{% for col in check_cols %} | ||
{{ snapshotted_rel }}.{{ col }} != {{ current_rel }}.{{ col }} | ||
or | ||
({{ snapshotted_rel }}.{{ col }} is null) and not ({{ current_rel }}.{{ col }} is null) | ||
{%- if not loop.last %} or {% endif %} | ||
|
||
{% endfor %} | ||
) | ||
{%- endset %} | ||
|
||
{% set scd_id_expr = sqlserver__snapshot_hash_arguments([primary_key, updated_at]) %} | ||
|
||
{% do return({ | ||
"unique_key": primary_key, | ||
"updated_at": updated_at, | ||
"row_changed": row_changed_expr, | ||
"scd_id": scd_id_expr | ||
}) %} | ||
{% endmacro %} |
Oops, something went wrong.