From 07fef6455348192da3cbecb403002994533475aa Mon Sep 17 00:00:00 2001 From: Brian Franklin Date: Thu, 31 Oct 2024 18:33:48 -0400 Subject: [PATCH] Update where.md to enhance the usability of the example The example on the page is useful; however, it is rigid in only allowing a 3 day look back. Adjusting the pattern to include a number value and a macro that performs a regex search to set the dateadd logic to whatever value is configured in the test. This allows for reusbaility of the macro for different lookback days. --- .../docs/reference/resource-configs/where.md | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/website/docs/reference/resource-configs/where.md b/website/docs/reference/resource-configs/where.md index 63c40b99902..f20ea483080 100644 --- a/website/docs/reference/resource-configs/where.md +++ b/website/docs/reference/resource-configs/where.md @@ -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. @@ -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 ``` @@ -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 @@ -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 %} ```