-
Notifications
You must be signed in to change notification settings - Fork 45
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 #143 from dbt-labs/removing_dbt_utils
Removing dbt utils dependency
- Loading branch information
Showing
77 changed files
with
314 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
kind: Under the Hood | ||
body: Removing dbt_utils | ||
time: 2022-10-12T11:41:17.757328-05:00 | ||
custom: | ||
Author: callum-mcdata | ||
Issue: "143" | ||
PR: "143" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
packages: | ||
- local: ../ | ||
- package: calogica/dbt_expectations | ||
version: [">=0.5.0", "<0.6.0"] | ||
version: [">=0.6.0", "<0.7.0"] |
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
{% macro metric_get_intervals_between(start_date, end_date, datepart) -%} | ||
{{ return(adapter.dispatch('metric_get_intervals_between', 'metrics')(start_date, end_date, datepart)) }} | ||
{%- endmacro %} | ||
|
||
{% macro default__metric_get_intervals_between(start_date, end_date, datepart) -%} | ||
{%- call statement('metric_get_intervals_between', fetch_result=True) %} | ||
|
||
select {{ datediff(start_date, end_date, datepart) }} | ||
|
||
{%- endcall -%} | ||
|
||
{%- set value_list = load_result('metric_get_intervals_between') -%} | ||
|
||
{%- if value_list and value_list['data'] -%} | ||
{%- set values = value_list['data'] | map(attribute=0) | list %} | ||
{{ return(values[0]) }} | ||
{%- else -%} | ||
{{ return(1) }} | ||
{%- endif -%} | ||
|
||
{%- endmacro %} | ||
|
||
|
||
{% macro metric_date_spine(datepart, start_date, end_date) %} | ||
{{ return(adapter.dispatch('metric_date_spine', 'metrics')(datepart, start_date, end_date)) }} | ||
{%- endmacro %} | ||
|
||
{% macro default__metric_date_spine(datepart, start_date, end_date) %} | ||
|
||
|
||
{# call as follows: | ||
|
||
metric_date_spine( | ||
"day", | ||
"to_date('01/01/2016', 'mm/dd/yyyy')", | ||
"dateadd(week, 1, current_date)" | ||
) #} | ||
|
||
|
||
with rawdata as ( | ||
|
||
{{metrics.metric_generate_series( | ||
metrics.metric_get_intervals_between(start_date, end_date, datepart) | ||
)}} | ||
|
||
), | ||
|
||
all_periods as ( | ||
|
||
select ( | ||
{{ | ||
dateadd( | ||
datepart, | ||
"row_number() over (order by 1) - 1", | ||
start_date | ||
) | ||
}} | ||
) as date_{{datepart}} | ||
from rawdata | ||
|
||
), | ||
|
||
filtered as ( | ||
|
||
select * | ||
from all_periods | ||
where date_{{datepart}} <= {{ end_date }} | ||
|
||
) | ||
|
||
select * from filtered | ||
|
||
{% endmacro %} | ||
|
||
|
||
{% macro metric_get_powers_of_two(upper_bound) %} | ||
{{ return(adapter.dispatch('metric_get_powers_of_two', 'metrics')(upper_bound)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__metric_get_powers_of_two(upper_bound) %} | ||
|
||
{% if upper_bound <= 0 %} | ||
{{ exceptions.raise_compiler_error("upper bound must be positive") }} | ||
{% endif %} | ||
|
||
{% for _ in range(1, 100) %} | ||
{% if upper_bound <= 2 ** loop.index %}{{ return(loop.index) }}{% endif %} | ||
{% endfor %} | ||
|
||
{% endmacro %} | ||
|
||
|
||
{% macro metric_generate_series(upper_bound) %} | ||
{{ return(adapter.dispatch('metric_generate_series', 'metrics')(upper_bound)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__metric_generate_series(upper_bound) %} | ||
|
||
{% set n = metrics.metric_get_powers_of_two(upper_bound) %} | ||
|
||
with p as ( | ||
select 0 as generated_number union all select 1 | ||
), unioned as ( | ||
|
||
select | ||
|
||
{% for i in range(n) %} | ||
p{{i}}.generated_number * power(2, {{i}}) | ||
{% if not loop.last %} + {% endif %} | ||
{% endfor %} | ||
+ 1 | ||
as generated_number | ||
|
||
from | ||
|
||
{% for i in range(n) %} | ||
p as p{{i}} | ||
{% if not loop.last %} cross join {% endif %} | ||
{% endfor %} | ||
|
||
) | ||
|
||
select * | ||
from unioned | ||
where generated_number <= {{upper_bound}} | ||
order by generated_number | ||
|
||
{% endmacro %} |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
{% test metric_equality(model, compare_model, compare_columns=None) %} | ||
{{ return(adapter.dispatch('test_metric_equality', 'metrics')(model, compare_model, compare_columns)) }} | ||
{% endtest %} | ||
|
||
{% macro default__test_metric_equality(model, compare_model, compare_columns=None) %} | ||
|
||
{% set set_diff %} | ||
count(*) + coalesce(abs( | ||
sum(case when which_diff = 'a_minus_b' then 1 else 0 end) - | ||
sum(case when which_diff = 'b_minus_a' then 1 else 0 end) | ||
), 0) | ||
{% endset %} | ||
|
||
{#-- Needs to be set at parse time, before we return '' below --#} | ||
{{ config(fail_calc = set_diff) }} | ||
|
||
{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #} | ||
{%- if not execute -%} | ||
{{ return('') }} | ||
{% endif %} | ||
|
||
-- setup | ||
{%- do metrics._metric_is_relation(model, 'test_metric_equality') -%} | ||
|
||
{#- | ||
If the compare_cols arg is provided, we can run this test without querying the | ||
information schema — this allows the model to be an ephemeral model | ||
-#} | ||
|
||
{%- if not compare_columns -%} | ||
{%- do metrics._metric_is_ephemeral(model, 'test_metric_equality') -%} | ||
{%- set compare_columns = adapter.get_columns_in_relation(model) | map(attribute='quoted') -%} | ||
{%- endif -%} | ||
|
||
{% set compare_cols_csv = compare_columns | join(', ') %} | ||
|
||
with a as ( | ||
|
||
select * from {{ model }} | ||
|
||
), | ||
|
||
b as ( | ||
|
||
select * from {{ compare_model }} | ||
|
||
), | ||
|
||
a_minus_b as ( | ||
|
||
select {{compare_cols_csv}} from a | ||
{{ except() }} | ||
select {{compare_cols_csv}} from b | ||
|
||
), | ||
|
||
b_minus_a as ( | ||
|
||
select {{compare_cols_csv}} from b | ||
{{ except() }} | ||
select {{compare_cols_csv}} from a | ||
|
||
), | ||
|
||
unioned as ( | ||
|
||
select 'a_minus_b' as which_diff, a_minus_b.* from a_minus_b | ||
union all | ||
select 'b_minus_a' as which_diff, b_minus_a.* from b_minus_a | ||
|
||
) | ||
|
||
select * from unioned | ||
|
||
{% endmacro %} | ||
|
||
|
||
{% macro _metric_is_relation(obj, macro) %} | ||
{%- if not (obj is mapping and obj.get('metadata', {}).get('type', '').endswith('Relation')) -%} | ||
{%- do exceptions.raise_compiler_error("Macro " ~ macro ~ " expected a Relation but received the value: " ~ obj) -%} | ||
{%- endif -%} | ||
{% endmacro %} | ||
|
||
{% macro _metric_is_ephemeral(obj, macro) %} | ||
{%- if obj.is_cte -%} | ||
{% set ephemeral_prefix = api.Relation.add_ephemeral_prefix('') %} | ||
{% if obj.name.startswith(ephemeral_prefix) %} | ||
{% set model_name = obj.name[(ephemeral_prefix|length):] %} | ||
{% else %} | ||
{% set model_name = obj.name %} | ||
{%- endif -%} | ||
{% set error_message %} | ||
The `{{ macro }}` macro cannot be used with ephemeral models, as it relies on the information schema. | ||
|
||
`{{ model_name }}` is an ephemeral model. Consider making it a view or table instead. | ||
{% endset %} | ||
{%- do exceptions.raise_compiler_error(error_message) -%} | ||
{%- endif -%} | ||
{% endmacro %} |
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 was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.