Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recency truncate date option #731

Merged
merged 19 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ jobs:
name: "Run OG Tests - Postgres"
command: ./run_test.sh postgres
- store_artifacts:
path: ./logs
path: integration_tests/logs
- store_artifacts:
path: integration_tests/target

integration-redshift:
docker:
Expand All @@ -35,7 +37,9 @@ jobs:
name: "Run OG Tests - Redshift"
command: ./run_test.sh redshift
- store_artifacts:
path: ./logs
path: integration_tests/logs
- store_artifacts:
path: integration_tests/target

integration-snowflake:
docker:
Expand All @@ -47,8 +51,10 @@ jobs:
name: "Run OG Tests - Snowflake"
command: ./run_test.sh snowflake
- store_artifacts:
path: ./logs

path: integration_tests/logs
- store_artifacts:
path: integration_tests/target

integration-bigquery:
environment:
BIGQUERY_SERVICE_KEY_PATH: "/home/circleci/bigquery-service-key.json"
Expand All @@ -64,7 +70,9 @@ jobs:
name: "Run OG Tests - BigQuery"
command: ./run_test.sh bigquery
- store_artifacts:
path: ./logs
path: integration_tests/logs
- store_artifacts:
path: integration_tests/target

workflows:
version: 2
Expand Down
12 changes: 12 additions & 0 deletions integration_tests/models/generic_tests/recency_time_excluded.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
with yesterday_time as (
select
1 as col1,
2 as col2,
{{ dbt.dateadd('day', -1, dbt.current_timestamp()) }} as created_at
)

select
col1,
col2,
{{ dbt.date_trunc('day', 'created_at') }} as created_at
from yesterday_time
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select
1 as col1,
2 as col2,
cast({{ dbt.dateadd('hour', -23, dbt.current_timestamp()) }} as {{ dbt.type_timestamp() }}) as created_at
23 changes: 19 additions & 4 deletions integration_tests/models/generic_tests/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,38 @@ seeds:
at_least: 0.9

models:
- name: test_recency
- name: recency_time_included
tests:
- dbt_utils.recency:
datepart: day
field: today
field: created_at
interval: 1
- dbt_utils.recency:
datepart: day
field: today
field: created_at
interval: 1
group_by_columns: ['col1']
- dbt_utils.recency:
datepart: day
field: today
field: created_at
interval: 1
group_by_columns: ['col1', 'col2']

- name: recency_time_excluded
tests:
- dbt_utils.recency:
datepart: day
field: created_at
interval: 1
ignore_time_component: true
- dbt_utils.recency:
datepart: day
field: created_at
interval: 1
ignore_time_component: false
error_if: "<1" #sneaky way to ensure that the test is returning failing rows
warn_if: "<0"

- name: test_equal_rowcount
tests:
- dbt_utils.equal_rowcount:
Expand Down
16 changes: 0 additions & 16 deletions integration_tests/models/generic_tests/test_recency.sql

This file was deleted.

14 changes: 9 additions & 5 deletions macros/generic_tests/recency.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% test recency(model, field, datepart, interval, group_by_columns = []) %}
{{ return(adapter.dispatch('test_recency', 'dbt_utils')(model, field, datepart, interval, group_by_columns)) }}
{% test recency(model, field, datepart, interval, ignore_time_component=False, group_by_columns = []) %}
{{ return(adapter.dispatch('test_recency', 'dbt_utils')(model, field, datepart, interval, ignore_time_component, group_by_columns)) }}
{% endtest %}

{% macro default__test_recency(model, field, datepart, interval, group_by_columns) %}
{% macro default__test_recency(model, field, datepart, interval, ignore_time_component, group_by_columns) %}

{% set threshold = dbt.dateadd(datepart, interval * -1, current_timestamp_backcompat()) %}
{% set threshold = 'cast(' ~ dbt.dateadd(datepart, interval * -1, dbt.current_timestamp()) ~ ' as ' ~ ('date' if ignore_time_component else dbt.type_timestamp()) ~ ')' %}

{% if group_by_columns|length() > 0 %}
{% set select_gb_cols = group_by_columns|join(' ,') + ', ' %}
Expand All @@ -17,7 +17,11 @@ with recency as (
select

{{ select_gb_cols }}
max({{field}}) as most_recent
{% if ignore_time_component %}
cast(max({{ field }}) as date) as most_recent
{%- else %}
max({{ field }}) as most_recent
{%- endif %}

from {{ model }}

Expand Down