Skip to content

Commit

Permalink
Migrate to dbt-utils 0.9.0 and dbt-date 0.6.0 (#189)
Browse files Browse the repository at this point in the history
* Upgrade to dbt-date 0.6.0

* Remove dbt_utils refs

* Requires dbt 1.2.x
  • Loading branch information
clausherther authored Aug 29, 2022
1 parent 5817cd1 commit abdda53
Show file tree
Hide file tree
Showing 22 changed files with 55 additions and 55 deletions.
4 changes: 2 additions & 2 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: 'dbt_expectations'
version: '0.5.0'
version: '0.6.0'

require-dbt-version: [">=1.0.0", "<2.0.0"]
require-dbt-version: [">=1.2.0", "<2.0.0"]
config-version: 2

target-path: "target"
Expand Down
6 changes: 3 additions & 3 deletions integration_tests/models/schema_tests/data_test.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
select
1 as idx,
'2020-10-21' as date_col,
cast(0 as {{ dbt_utils.type_float() }}) as col_numeric_a,
cast(1 as {{ dbt_utils.type_float() }}) as col_numeric_b,
cast(0 as {{ type_float() }}) as col_numeric_a,
cast(1 as {{ type_float() }}) as col_numeric_b,
'a' as col_string_a,
'b' as col_string_b,
cast(null as {{ dbt_utils.type_string() }}) as col_null
cast(null as {{ type_string() }}) as col_null

union all

Expand Down
4 changes: 2 additions & 2 deletions integration_tests/models/schema_tests/timeseries_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ add_row_values as (
select
d.date_day,
cast(d.date_day as {{ dbt_expectations.type_datetime() }}) as date_datetime,
cast(d.date_day as {{ dbt_utils.type_timestamp() }}) as date_timestamp,
cast(abs({{ dbt_expectations.rand() }}) as {{ dbt_utils.type_float() }}) as row_value
cast(d.date_day as {{ type_timestamp() }}) as date_timestamp,
cast(abs({{ dbt_expectations.rand() }}) as {{ type_float() }}) as row_value
from
dates d

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ add_row_values as (

select
cast(d.date_day as {{ dbt_expectations.type_datetime() }}) as date_day,
cast(abs({{ dbt_expectations.rand() }}) as {{ dbt_utils.type_float() }}) as row_value
cast(abs({{ dbt_expectations.rand() }}) as {{ type_float() }}) as row_value
from
dates d
cross join
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ add_row_values as (
select
cast(d.date_day as {{ dbt_expectations.type_datetime() }}) as date_day,
cast(d.date_day as {{ dbt_expectations.type_timestamp() }}) as date_timestamp,
cast(g.generated_number as {{ dbt_utils.type_int() }}) as group_id,
cast(floor(100 * r.generated_number) as {{ dbt_utils.type_int() }}) as row_value
cast(g.generated_number as {{ type_int() }}) as group_id,
cast(floor(100 * r.generated_number) as {{ type_int() }}) as row_value
from
dates d
cross join
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ add_row_values as (

select
cast(d.date_hour as {{ dbt_expectations.type_datetime() }}) as date_hour,
cast(abs({{ dbt_expectations.rand() }}) as {{ dbt_utils.type_float() }}) as row_value
cast(abs({{ dbt_expectations.rand() }}) as {{ type_float() }}) as row_value
from
dates d
cross join
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ with data_example as (
select
1 as idx,
'2020-10-21' as date_col,
cast(0 as {{ dbt_utils.type_float() }}) as col_numeric_a
cast(0 as {{ type_float() }}) as col_numeric_a

union all

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ set_values as (
{% for value in value_set -%}
select
{% if quote_values -%}
cast('{{ value }}' as {{ dbt_utils.type_string() }})
cast('{{ value }}' as {{ type_string() }})
{%- else -%}
{{ value }}
{%- endif %} as value_field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

{% for column in columns_in_relation %}
select
cast('{{ column.name | upper }}' as {{ dbt_utils.type_string() }}) as relation_column,
cast('{{ column.dtype | upper }}' as {{ dbt_utils.type_string() }}) as relation_column_type
cast('{{ column.name | upper }}' as {{ type_string() }}) as relation_column,
cast('{{ column.dtype | upper }}' as {{ type_string() }}) as relation_column_type
{% if not loop.last %}union all{% endif %}
{% endfor %}
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ set_values as (
{% for value in value_set -%}
select
{% if quote_values -%}
cast('{{ value }}' as {{ dbt_utils.type_string() }})
cast('{{ value }}' as {{ type_string() }})
{%- else -%}
{{ value }}
{%- endif %} as value_field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ with metric_values as (
with grouped_metric_values as (

select
{{ dbt_utils.date_trunc(period, date_column_name) }} as metric_period,
{{ date_trunc(period, date_column_name) }} as metric_period,
sum({{ column_name }}) as agg_metric_value
from
{{ model }}
Expand Down Expand Up @@ -129,10 +129,10 @@ from
where

metric_period >= cast(
{{ dbt_utils.dateadd(period, -test_periods, dbt_utils.date_trunc(period, dbt_date.now())) }}
as {{ dbt_utils.type_timestamp() }})
{{ dateadd(period, -test_periods, date_trunc(period, dbt_date.now())) }}
as {{ type_timestamp() }})
and
metric_period < {{ dbt_utils.date_trunc(period, dbt_date.now()) }}
metric_period < {{ date_trunc(period, dbt_date.now()) }}
and

not (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ with base_dates as (

{{ dbt_date.get_base_dates(start_date=start_date, end_date=end_date, datepart=date_part) }}
{% if interval %}
{#
{#
Filter the date spine created above down to the interval granularity using a modulo operation.
The number of date_parts after the start_date divided by the integer interval will produce no remainder for the desired intervals,
The number of date_parts after the start_date divided by the integer interval will produce no remainder for the desired intervals,
e.g. for 2-day interval from a starting Jan 1, 2020:
params: start_date = '2020-01-01', date_part = 'day', interval = 2
date spine created above: [2020-01-01, 2020-01-02, 2020-01-03, 2020-01-04, 2020-01-05, ...]
Expand All @@ -57,8 +57,8 @@ with base_dates as (
Filtering the spine only where this remainder == 0 will return a spine with every other day as desired, i.e. [2020-01-01, 2020-01-03, 2020-01-05, ...]
#}
where mod(
cast({{ dbt_utils.datediff("'" ~ start_date ~ "'", 'date_' ~ date_part, date_part) }} as {{ dbt_utils.type_int() }}),
cast({{interval}} as {{ dbt_utils.type_int() }})
cast({{ datediff("'" ~ start_date ~ "'", 'date_' ~ date_part, date_part) }} as {{ type_int() }}),
cast({{interval}} as {{ type_int() }})
) = 0
{% endif %}

Expand All @@ -68,29 +68,29 @@ model_data as (
select
{% if not interval %}

cast({{ dbt_utils.date_trunc(date_part, date_col) }} as {{ dbt_expectations.type_datetime() }}) as date_{{ date_part }},
cast({{ date_trunc(date_part, date_col) }} as {{ dbt_expectations.type_datetime() }}) as date_{{ date_part }},

{% else %}
{#
Use a modulo operator to determine the number of intervals that a date_col is away from the interval-date spine
{#
Use a modulo operator to determine the number of intervals that a date_col is away from the interval-date spine
and subtracts that amount to effectively slice each date_col record into its corresponding spine bucket,
e.g. given a date_col of with records [2020-01-01, 2020-01-02, 2020-01-03, 2020-01-11, 2020-01-12]
if we want to slice these dates into their 2-day buckets starting Jan 1, 2020 (start_date = '2020-01-01', date_part='day', interval=2),
the modulo operation described above will produce these remainders: [0, 1, 0, 0, 1]
subtracting that number of days from the observations will produce records [2020-01-01, 2020-01-01, 2020-01-03, 2020-01-11, 2020-01-11],
all of which align with records from the interval-date spine
#}
{{dbt_utils.dateadd(
date_part,
{{dateadd(
date_part,
"mod(
cast(" ~ dbt_utils.datediff("'" ~ start_date ~ "'", date_col, date_part) ~ " as " ~ dbt_utils.type_int() ~ " ),
cast(" ~ interval ~ " as " ~ dbt_utils.type_int() ~ " )
) * (-1)",
"cast( " ~ dbt_utils.date_trunc(date_part, date_col) ~ " as " ~ dbt_expectations.type_datetime() ~ ")"
cast(" ~ datediff("'" ~ start_date ~ "'", date_col, date_part) ~ " as " ~ type_int() ~ " ),
cast(" ~ interval ~ " as " ~ type_int() ~ " )
) * (-1)",
"cast( " ~ date_trunc(date_part, date_col) ~ " as " ~ dbt_expectations.type_datetime() ~ ")"
)}} as date_{{ date_part }},

{% endif %}

count(*) as row_cnt
from
{{ model }} f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
strictly=False
) %}
{% set expression %}
{{ dbt_utils.length(column_name) }}
{{ length(column_name) }}
{% endset %}

{{ dbt_expectations.expression_between(model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
row_condition=None
) %}

{% set expression = dbt_utils.length(column_name) ~ " = " ~ value %}
{% set expression = length(column_name) ~ " = " ~ value %}

{{ dbt_expectations.expression_is_true(model,
expression=expression,
Expand Down
2 changes: 1 addition & 1 deletion macros/schema_tests/table_shape/expect_column_to_exist.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
with test_data as (

select
cast('{{ column_name }}' as {{ dbt_utils.type_string() }}) as column_name,
cast('{{ column_name }}' as {{ type_string() }}) as column_name,
{{ matching_column_index }} as matching_column_index,
{{ column_index_matches }} as column_index_matches

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ with latest_grouped_timestamps as (
{{ g }},
{%- endfor %}
max(1) as join_key,
max(cast({{ timestamp_column }} as {{ dbt_utils.type_timestamp() }})) as latest_timestamp_column
max(cast({{ timestamp_column }} as {{ type_timestamp() }})) as latest_timestamp_column
from
{{ model }}
where
-- to exclude erroneous future dates
cast({{ timestamp_column }} as {{ dbt_utils.type_timestamp() }}) <= {{ dbt_date.now() }}
cast({{ timestamp_column }} as {{ type_timestamp() }}) <= {{ dbt_date.now() }}
{% if row_condition %}
and {{ row_condition }}
{% endif %}
Expand All @@ -58,8 +58,8 @@ outdated_grouped_timestamps as (
-- are the max timestamps per group older than the specified cutoff?
latest_timestamp_column <
cast(
{{ dbt_utils.dateadd(datepart, interval * -1, dbt_date.now()) }}
as {{ dbt_utils.type_timestamp() }}
{{ dateadd(datepart, interval * -1, dbt_date.now()) }}
as {{ type_timestamp() }}
)

),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
{%- set default_start_date = '1970-01-01' -%}
with max_recency as (

select max(cast({{ column_name }} as {{ dbt_utils.type_timestamp() }})) as max_timestamp
select max(cast({{ column_name }} as {{ type_timestamp() }})) as max_timestamp
from
{{ model }}
where
-- to exclude erroneous future dates
cast({{ column_name }} as {{ dbt_utils.type_timestamp() }}) <= {{ dbt_date.now() }}
cast({{ column_name }} as {{ type_timestamp() }}) <= {{ dbt_date.now() }}
{% if row_condition %}
and {{ row_condition }}
{% endif %}
Expand All @@ -33,8 +33,8 @@ from
where
-- if the row_condition excludes all rows, we need to compare against a default date
-- to avoid false negatives
coalesce(max_timestamp, cast('{{ default_start_date }}' as {{ dbt_utils.type_timestamp() }}))
coalesce(max_timestamp, cast('{{ default_start_date }}' as {{ type_timestamp() }}))
<
cast({{ dbt_utils.dateadd(datepart, interval * -1, dbt_date.now()) }} as {{ dbt_utils.type_timestamp() }})
cast({{ dateadd(datepart, interval * -1, dbt_date.now()) }} as {{ type_timestamp() }})

{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
with relation_columns as (

{% for col_name in relation_column_names %}
select cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as relation_column
select cast('{{ col_name }}' as {{ type_string() }}) as relation_column
{% if not loop.last %}union all{% endif %}
{% endfor %}
),
input_columns as (

{% for col_name in column_list %}
select cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as input_column
select cast('{{ col_name }}' as {{ type_string() }}) as input_column
{% if not loop.last %}union all{% endif %}
{% endfor %}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{% for col_name in relation_column_names %}
select
{{ loop.index }} as relation_column_idx,
cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as relation_column
cast('{{ col_name }}' as {{ type_string() }}) as relation_column
{% if not loop.last %}union all{% endif %}
{% endfor %}
),
Expand All @@ -17,7 +17,7 @@
{% for col_name in column_list %}
select
{{ loop.index }} as input_column_idx,
cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as input_column
cast('{{ col_name }}' as {{ type_string() }}) as input_column
{% if not loop.last %}union all{% endif %}
{% endfor %}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
with relation_columns as (

{% for col_name in relation_column_names %}
select cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as relation_column
select cast('{{ col_name }}' as {{ type_string() }}) as relation_column
{% if not loop.last %}union all{% endif %}
{% endfor %}
),
input_columns as (

{% for col_name in column_list %}
select cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as input_column
select cast('{{ col_name }}' as {{ type_string() }}) as input_column
{% if not loop.last %}union all{% endif %}
{% endfor %}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
with relation_columns as (

{% for col_name in relation_column_names %}
select cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as relation_column
select cast('{{ col_name }}' as {{ type_string() }}) as relation_column
{% if not loop.last %}union all{% endif %}
{% endfor %}
),
input_columns as (

{% for col_name in column_list %}
select cast('{{ col_name }}' as {{ dbt_utils.type_string() }}) as input_column
select cast('{{ col_name }}' as {{ type_string() }}) as input_column
{% if not loop.last %}union all{% endif %}
{% endfor %}
)
Expand Down
2 changes: 1 addition & 1 deletion packages.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages:
- package: calogica/dbt_date
version: [">=0.5.0", "<0.6.0"]
version: [">=0.6.0", "<0.7.0"]

0 comments on commit abdda53

Please sign in to comment.