Replies: 1 comment
-
|
@Kronossos Thanks for opening the discussion, and sorry for the delay getting back to you! This is a limitation of I just opened a separate discussion (#6280) to imagine a pattern that would enable users to distinguish between pure macros ("snippets" with static inputs/outputs), versus macros that modify the DAG, or require database state as an input. We don't have a first-class way to distinguish between those in dbt today, and they're really quite different. WorkaroundIn the meantime, there is a workaround for this, specific to the Following your example: version: 2
models:
- name: your_model
columns:
- name: value
tests:
- not_null:
where: "__country_filter__" # placeholder: static string for config value{% macro get_where_subquery(relation) -%}
{% set where = config.get('where', '') %}
{% if where == "__country_specific__" %}
{% set where %}
{% if var('country_cd', []) %}
country_cd in (
{% for ccd in var('country_cd') %}
'{{ ccd }}' {{ ',' if not loop.last }}
{% endfor %}
)
{% else %}
1=1
{% endif %}
{% endset %}
{% endif %}
{% if where %}
{%- set filtered -%}
(select * from {{ relation }} where {{ where }}) dbt_subquery
{%- endset -%}
{% do return(filtered) %}
{%- else -%}
{% do return(relation) %}
{%- endif -%}
{%- endmacro %}Voila: -- compiled code, with some manual whitespace cleanup
select
count(*) as failures,
count(*) != 0 as should_warn,
count(*) != 0 as should_error
from (
select value
from (select * from "jerco"."dbt_jcohen"."your_model" where
country_cd in (
'USA' ,
'CA'
)
) dbt_subquery
where value is null
) dbt_internal_testNext stepsI've opened a PR to update the docs (dbt-labs/docs.getdbt.com#2418), in the hopes of both:
Let's also keep the conversation going in the other linked discussion, toward a more general-purpose solution for static code reuse in |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I have problems with using macro inside of dbt where.
I know that when you try to use one you will get error:
But WHY?
I do not see any logical explanation of such a behaviour.
Why I want to use macro in where?
Imagine I have view like this:
I have some ML pipeline that modifies\uses only part of the data that is annotated with given country code (this is dbt variable like
('ES','TW')). At the end of this pipeline I want to test if part of the data I was using is errorfree so I want to test if value is not_null:So when I run two pipelines, one for
ITand second forES,ITwill fail because ofnullvalue, butESwill be okay.And now imagine that I have multiple models and multiple columns that look like this:
Nice idea would be to just write simple macro to avoid copypaste code:
But when you do this I get error:
I also do not want to rewrite default tests just to use country_code:
Is there any reason not to use macros in
where? Do u know sane workaround for this?Beta Was this translation helpful? Give feedback.
All reactions