Skip to content

Commit 9f7aec9

Browse files
committed
Merge branch 'release/0.7.8'
2 parents c8f3f2d + f75b38b commit 9f7aec9

20 files changed

+222
-186
lines changed

.circleci/config.yml

Lines changed: 0 additions & 98 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<img src="https://user-images.githubusercontent.com/25080503/65772647-89525700-e132-11e9-80ff-12ad30a25466.png">
2+
<img src="https://user-images.githubusercontent.com/25080503/65772647-89525700-e132-11e9-80ff-12ad30a25466.png" alt="dbtvault">
33
</p>
44

55
<p align="center">

dbt_project.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name: 'dbtvault'
2-
version: '0.7.7'
3-
require-dbt-version: [">=0.18.0", "<0.21.0"]
1+
name: dbtvault
2+
version: 0.7.8
3+
require-dbt-version: [">=0.20.0", "<0.22.0"]
44
config-version: 2
55

66
source-paths: ["models"]
7-
analysis-paths: ["analysis"]
7+
analysis-paths: ["analysis"]
88
test-paths: ["tests"]
99
data-paths: ["data"]
1010
macro-paths: ["macros"]
@@ -16,4 +16,5 @@ clean-targets:
1616
- "dbt_modules"
1717

1818
vars:
19-
hash: MD5
19+
hash: MD5
20+
max_datetime: '{{ dbtvault.max_datetime() }}'

macros/internal/as_constant.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
{%- macro default__as_constant(column_str) -%}
88

9-
{% if column_str is not none and column_str is string and column_str %}
9+
{%- if column_str is not none and column_str is string and column_str -%}
1010

1111
{%- if column_str | first == "!" -%}
1212

macros/internal/expand_column_list.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
{%- if dbtvault.is_list(columns) -%}
1212

13+
{%- set columns = columns | reject("none") %}
14+
1315
{%- for col in columns -%}
1416

1517
{%- if col is string -%}

macros/internal/get_package_namespaces.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.

macros/materialisations/period_mat_helpers.sql

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
TO_DATE({{ timestamp_field }}) < DATE_TRUNC('{{ period }}', TO_DATE('{{ start_timestamp }}') + INTERVAL '{{ offset }} {{ period }}' + INTERVAL '1 {{ period }}'))
2424
AND (TO_DATE({{ timestamp_field }}) >= TO_DATE('{{ start_timestamp }}'))
2525
{%- endset -%}
26-
2726
{%- set filtered_sql = core_sql | replace("__PERIOD_FILTER__", period_filter) -%}
2827

2928
{% do return(filtered_sql) %}
3029
{% endmacro %}
3130

32-
3331
{#-- GET_PERIOD_FILTER_SQL #}
3432

3533
{%- macro get_period_filter_sql(target_cols_csv, base_sql, timestamp_field, period, start_timestamp, stop_timestamp, offset) -%}
@@ -57,7 +55,6 @@
5755
select {{ target_cols_csv }} from ({{ filtered_sql.sql }})
5856
{%- endmacro %}
5957

60-
6158
{#-- GET_PERIOD_BOUNDARIES #}
6259

6360
{%- macro get_period_boundaries(target_schema, target_table, timestamp_field, start_date, stop_date, period) -%}
@@ -73,23 +70,25 @@
7370
{% do return(macro) %}
7471
{%- endmacro %}
7572

73+
74+
7675
{% macro default__get_period_boundaries(target_schema, target_table, timestamp_field, start_date, stop_date, period) -%}
7776

7877
{% set period_boundary_sql -%}
79-
with data as (
80-
select
81-
coalesce(max({{ timestamp_field }}), '{{ start_date }}')::timestamp as start_timestamp,
82-
coalesce({{ dbt_utils.dateadd('millisecond', 86399999, "nullif('" ~ stop_date | lower ~ "','none')::timestamp") }},
83-
{{ dbt_utils.current_timestamp() }} ) as stop_timestamp
84-
from {{ target_schema }}.{{ target_table }}
78+
WITH period_data AS (
79+
SELECT
80+
COALESCE(MAX({{ timestamp_field }}), '{{ start_date }}')::TIMESTAMP AS start_timestamp,
81+
COALESCE({{ dbt_utils.dateadd('millisecond', 86399999, "NULLIF('" ~ stop_date | lower ~ "','none')::TIMESTAMP") }},
82+
{{ dbtvault.current_timestamp() }} ) AS stop_timestamp
83+
FROM {{ target_schema }}.{{ target_table }}
8584
)
86-
select
85+
SELECT
8786
start_timestamp,
8887
stop_timestamp,
8988
{{ dbt_utils.datediff('start_timestamp',
9089
'stop_timestamp',
91-
period) }} + 1 as num_periods
92-
from data
90+
period) }} + 1 AS num_periods
91+
FROM period_data
9392
{%- endset %}
9493

9594
{% set period_boundaries_dict = dbt_utils.get_query_results_as_dict(period_boundary_sql) %}
@@ -114,10 +113,11 @@
114113
{% do return(macro) %}
115114
{%- endmacro %}
116115

116+
117117
{%- macro default__get_period_of_load(period, offset, start_timestamp) -%}
118118

119119
{% set period_of_load_sql -%}
120-
SELECT DATE_TRUNC('{{ period }}', DATEADD({{ period }}, {{ offset }}, TO_DATE('{{start_timestamp}}'))) AS period_of_load
120+
SELECT DATE_TRUNC('{{ period }}', DATEADD({{ period }}, {{ offset }}, TO_DATE('{{ start_timestamp }}'))) AS period_of_load
121121
{%- endset %}
122122

123123
{% set period_of_load_dict = dbt_utils.get_query_results_as_dict(period_of_load_sql) %}

macros/materialisations/vault_insert_by_period_materialization.sql

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
start_stop_dates.stop_date,
2929
0, period) %}
3030
{% set build_sql = create_table_as(False, target_relation, filtered_sql) %}
31-
3231
{% do to_drop.append(tmp_relation) %}
3332

3433
{% elif existing_relation.is_view or full_refresh_mode %}
@@ -73,18 +72,18 @@
7372
period_boundaries.stop_timestamp, i) %}
7473

7574
{% call statement() -%}
76-
{{ dbt.create_table_as(True, tmp_relation, tmp_table_sql) }}
75+
{{ create_table_as(True, tmp_relation, tmp_table_sql) }}
7776
{%- endcall %}
7877

7978
{{ adapter.expand_target_column_types(from_relation=tmp_relation,
8079
to_relation=target_relation) }}
8180

8281
{%- set insert_query_name = 'main-' ~ i -%}
8382
{% call statement(insert_query_name, fetch_result=True) -%}
84-
insert into {{ target_relation }} ({{ target_cols_csv }})
83+
INSERT INTO {{ target_relation }} ({{ target_cols_csv }})
8584
(
86-
select {{ target_cols_csv }}
87-
from {{ tmp_relation.include(schema=True) }}
85+
SELECT {{ target_cols_csv }}
86+
FROM {{ tmp_relation.include(schema=True) }}
8887
);
8988
{%- endcall %}
9089

macros/materialisations/vault_insert_by_rank_materialization.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
{% if existing_relation is none %}
2424

2525
{% set filtered_sql = dbtvault.replace_placeholder_with_rank_filter(sql, rank_column, 1) %}
26-
2726
{% set build_sql = create_table_as(False, target_relation, filtered_sql) %}
2827

2928
{% do to_drop.append(tmp_relation) %}
@@ -57,19 +56,22 @@
5756

5857
{% set tmp_relation = make_temp_relation(this) %}
5958

59+
{# This call statement drops and then creates a temporary table #}
60+
{# but MSSQL will fail to drop any temporary table created by a previous loop iteration #}
61+
{# See MSSQL note and drop code below #}
6062
{% call statement() -%}
61-
{{ dbt.create_table_as(True, tmp_relation, filtered_sql) }}
63+
{{ create_table_as(True, tmp_relation, filtered_sql) }}
6264
{%- endcall %}
6365

6466
{{ adapter.expand_target_column_types(from_relation=tmp_relation,
6567
to_relation=target_relation) }}
6668

6769
{%- set insert_query_name = 'main-' ~ i -%}
6870
{% call statement(insert_query_name, fetch_result=True) -%}
69-
insert into {{ target_relation }} ({{ target_cols_csv }})
71+
INSERT INTO {{ target_relation }} ({{ target_cols_csv }})
7072
(
71-
select {{ target_cols_csv }}
72-
from {{ tmp_relation.include(schema=True) }}
73+
SELECT {{ target_cols_csv }}
74+
FROM {{ tmp_relation.include(schema=True) }}
7375
);
7476
{%- endcall %}
7577

@@ -89,12 +91,10 @@
8991
rows_inserted,
9092
model.unique_id)) }}
9193

92-
9394
{% do to_drop.append(tmp_relation) %}
9495
{% do adapter.commit() %}
9596

9697
{% endfor %}
97-
9898
{% call noop_statement('main', "INSERT {}".format(loop_vars['sum_rows_inserted']) ) -%}
9999
{{ filtered_sql }}
100100
{%- endcall %}

macros/staging/rank_columns.sql

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,57 @@
1212

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

15-
{%- if dbtvault.is_list(columns[col].order_by) -%}
16-
{%- set order_by_str = columns[col].order_by | join(", ") -%}
15+
{%- set order_by = columns[col].order_by -%}
16+
{%- set partition_by = columns[col].partition_by -%}
17+
{%- set dense_rank = columns[col].dense_rank -%}
18+
19+
{%- if dbtvault.is_nothing(dense_rank) %}
20+
{%- set rank_type = "RANK()" -%}
21+
{%- elif dense_rank is true -%}
22+
{%- set rank_type = "DENSE_RANK()" -%}
23+
{%- else -%}
24+
{%- if execute -%}
25+
{%- do exceptions.raise_compiler_error('If dense_rank is provided, it must be true or false, not {}'.format(dense_rank)) -%}
26+
{% endif %}
27+
{%- endif -%}
28+
29+
{%- if dbtvault.is_list(order_by) -%}
30+
31+
{%- set order_by_str_lst = [] -%}
32+
33+
{% for order_by_col in order_by %}
34+
35+
{%- if order_by_col is mapping %}
36+
{%- set column_name, direction = order_by_col.items()|first -%}
37+
{%- set order_by_str = "{} {}".format(column_name, direction) | trim -%}
38+
{%- else -%}
39+
{%- set order_by_str = order_by_col -%}
40+
{%- endif -%}
41+
42+
{%- do order_by_str_lst.append(order_by_str) -%}
43+
{%- endfor -%}
44+
45+
{%- set order_by_str = order_by_str_lst | join(", ") -%}
46+
1747
{%- else -%}
18-
{%- set order_by_str = columns[col].order_by -%}
48+
49+
{%- if order_by is mapping %}
50+
{%- set column_name, direction = order_by.items()|first -%}
51+
{%- else -%}
52+
{%- set column_name = order_by -%}
53+
{%- set direction = '' -%}
54+
{%- endif -%}
55+
56+
{%- set order_by_str = "{} {}".format(column_name, direction) | trim -%}
1957
{%- endif -%}
2058

21-
{%- if dbtvault.is_list(columns[col].partition_by) -%}
22-
{%- set partition_by_str = columns[col].partition_by | join(", ") -%}
59+
{%- if dbtvault.is_list(partition_by) -%}
60+
{%- set partition_by_str = partition_by | join(", ") -%}
2361
{%- else -%}
24-
{%- set partition_by_str = columns[col].partition_by -%}
62+
{%- set partition_by_str = partition_by -%}
2563
{%- endif -%}
2664

27-
{{- "RANK() OVER (PARTITION BY {} ORDER BY {}) AS {}".format(partition_by_str, order_by_str, col) | indent(4) -}}
65+
{{- "{} OVER (PARTITION BY {} ORDER BY {}) AS {}".format(rank_type, partition_by_str, order_by_str, col) | indent(4) -}}
2866

2967
{%- endif -%}
3068

macros/staging/stage.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
{%- set error_message -%}
2121
Staging error: Missing source_model configuration. A source model name must be provided.
22-
e.g.
22+
e.g.
2323
[REF STYLE]
2424
source_model: model_name
2525
OR
2626
[SOURCES STYLE]
2727
source_model:
28-
source_name: source_table_name"
28+
source_name: source_table_name
2929
{%- endset -%}
3030

3131
{{- exceptions.raise_compiler_error(error_message) -}}

0 commit comments

Comments
 (0)