-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
8 deletions.
There are no files selected for viewing
55 changes: 47 additions & 8 deletions
55
...t_expectations/schema_tests/table_shape/expect_grouped_row_values_to_have_recent_data.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,27 +1,66 @@ | ||
{% macro sqlserver__test_expect_grouped_row_values_to_have_recent_data(model, group_by, timestamp_column, datepart, interval) %} | ||
{% macro sqlserver__test_expect_grouped_row_values_to_have_recent_data(model, group_by, timestamp_column, datepart, interval, row_condition=None) %} | ||
with latest_grouped_timestamps as ( | ||
|
||
select | ||
{%- for g in group_by %} | ||
{{ g }}, | ||
{%- endfor %} | ||
max(1) as join_key, | ||
max({{ timestamp_column }}) as latest_timestamp_column | ||
from | ||
{{ model }} | ||
group by | ||
{%- for g in group_by %} | ||
{{g}}{%- if not loop.last %}, {%- endif %} | ||
{%- endfor %} | ||
where | ||
-- to exclude erroneous future dates | ||
cast({{ timestamp_column }} as {{ type_timestamp() }}) <= {{ dbt_date.now() }} | ||
{% if row_condition %} | ||
and {{ row_condition }} | ||
{% endif %} | ||
group by | ||
{%- for g in group_by %} | ||
{{g}}{%- if not loop.last %}, {%- endif %} | ||
{%- endfor %} | ||
|
||
), | ||
validation_errors as ( | ||
total_row_counts as ( | ||
|
||
select | ||
max(1) as join_key, | ||
count(*) as row_count | ||
from | ||
latest_grouped_timestamps | ||
|
||
), | ||
outdated_grouped_timestamps as ( | ||
|
||
select * | ||
from | ||
latest_grouped_timestamps | ||
where | ||
latest_timestamp_column < {{ dbt_utils.dateadd(datepart, interval * -1, dbt_date.now()) }} | ||
-- are the max timestamps per group older than the specified cutoff? | ||
latest_timestamp_column < | ||
cast( | ||
{{ dateadd(datepart, interval * -1, dbt_date.now()) }} | ||
as {{ type_timestamp() }} | ||
) | ||
|
||
), | ||
validation_errors as ( | ||
|
||
select | ||
r.row_count, | ||
t.* | ||
from | ||
total_row_counts r | ||
left join | ||
outdated_grouped_timestamps t | ||
on r.join_key = t.join_key | ||
where | ||
-- fail if either no rows were returned due to row_condition, | ||
-- or the recency test returned failed rows | ||
r.row_count = 0 | ||
or | ||
t.join_key is not null | ||
|
||
) | ||
select count(*) from validation_errors | ||
select * from validation_errors | ||
{% endmacro %} |