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

Update where.md to enhance the usability of the example #6399

Open
wants to merge 3 commits into
base: current
Choose a base branch
from
Open
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
24 changes: 19 additions & 5 deletions website/docs/reference/resource-configs/where.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ You can override this behavior by:

Within this macro definition, you can reference whatever custom macros you want, based on static inputs from the configuration. At simplest, this enables you to DRY up code that you'd otherwise need to repeat across many different `.yml` files. Because the `get_where_subquery` macro is resolved at runtime, your custom macros can also include [fetching the results of introspective database queries](https://docs.getdbt.com/reference/dbt-jinja-functions/run_query).

**Example:** Filter your test to the past three days of data, using dbt's cross-platform [`dateadd()`](https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros#dateadd) utility macro.
**Example:** Filter your test to the past N days of data, using dbt's cross-platform [`dateadd()`](https://docs.getdbt.com/reference/dbt-jinja-functions/cross-database-macros#dateadd) utility macro. You control how many days ago by setting the number in the placeholder string.

<File name='models/config.yml'>

Expand All @@ -147,7 +147,7 @@ models:
tests:
- unique:
config:
where: "date_column > __three_days_ago__" # placeholder string for static config
where: "date_column > __3_days_ago__" # placeholder string for static config
```

</File>
Expand All @@ -158,10 +158,9 @@ models:
{% macro get_where_subquery(relation) -%}
{% set where = config.get('where') %}
{% if where %}
{% if "__three_days_ago__" in where %}
{% if "_days_ago__" in where %}
{# replace placeholder string with result of custom macro #}
{% set three_days_ago = dbt.dateadd('day', -3, current_timestamp()) %}
{% set where = where | replace("__three_days_ago__", three_days_ago) %}
{% set where = replace_days_ago(where) %}
{% endif %}
{%- set filtered -%}
(select * from {{ relation }} where {{ where }}) dbt_subquery
Expand All @@ -171,6 +170,21 @@ models:
{% do return(relation) %}
{%- endif -%}
{%- endmacro %}

{% macro replace_days_ago(where_string) %}
{# Use regex to search the pattern for the number days #}
{# Default to 3 days when no number found #}
{% set re = modules.re %}
{% set days = 3 %}
{% set pattern = '__(\d+)_days_ago__' %}
{% set match = re.search(pattern, where_string) %}
{% if match %}
{% set days = match.group(1) | int %}
{% endif %}
{% set n_days_ago = dbt.dateadd('day', -days, current_timestamp()) %}
{% set result = re.sub(pattern, n_days_ago, where_string) %}
{{ return(result) }}
{% endmacro %}
```

</File>
Loading