-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[CT-1506] [Bug] Nested {{ this }} not working as expected within macro #6249
Comments
@jtcohen6 please excuse the highlight but given your response to the linked issue, I have a feeling you'll intuitively know what is going on here! |
@adamcunnington-mlg Thanks for providing the reproduction case. I share your suspicion on this being a bug, versus the intersection of a few tricky moving pieces. If I'm understanding right, it's really about:
To confirm, are you seeing this error when you I tried putting together an even simpler reproduction case (on Postgres for convenience), and I wasn't able to: -- models/model_a.sql
select 1 as id
union all
select 2 as id -- macros/some_macro.sql
{% macro select_from_table(table) -%}
-- this is my post hook
{% for value in dbt_utils.get_column_values(table=table, column="id") %}
select '{{ value }}' from {{ table }}
{{ "union all" if not loop.last }}
{% endfor %}
{%- endmacro %} models:
+post-hook: "{{ select_from_table( this ) }}"
Everything runs successfully (SQL copied from create view "jerco"."dbt_jcohen"."model_a__dbt_tmp" as (
select 1 as id
union all
select 2 as id
); select
id as value
from "jerco"."dbt_jcohen"."model_a"
group by id
order by count(*) desc -- this is my post hook
select '1' from "jerco"."dbt_jcohen"."model_a"
union all
select '2' from "jerco"."dbt_jcohen"."model_a" |
Update: I was able to reproduce this after switching to The error is the one being returned by the I'll share more details as I figure them out. |
I was also able to reproduce this issue on Postgres might be the special case here. Its adapter cache does work slightly differently, because of the need to link dependent relations (bound views). Summary
There are two potential resolutions here:
|
@jtcohen6 thank you so much for the thorough investigation and detail. I was in some back to back meets so missed your progress in between. Thank you for the report - we'll use the workaround for now whilst we awaiting a conclusion on which resolution is best. Edge cases eh?! |
Closing (for now) in favor of an update to documentation: dbt-labs/docs.getdbt.com#2818 |
Is this a new bug in dbt-core?
Current Behavior
I should call out at the outset that I'm suspicious this is actually a bug - possibly more the interplay of complex nuances that I don't fully understand.
I have a very similar use case to the one that is described by this exception in the docs about the need to nest
{{ this }}
when you want delayed evaluation. Indeed, it was also pointed out to the submitter of another bug which was a false negative here.I am trying to grant a BigQuery row-level access policy on a table as a post-hook.
I understand that I need to nest
{{ this }}
inside a jinja expression to stop a compilation failure due to dbt-jinja not knowing about the just-created object.The only difference between my use case and the docs is:
dbt_project.yml
dbt_utils.get_column_values
dbt_project.yml:
macros/grant_row_level_access.sql:
However, at run-time, IF this is the first time this model is being created, I get this output:
So dbt_utils is still doing some "check" against dbt graph and semi-correctly identifying that it doesn't exist in the updated graph yet - despite the fact that the database relation DOES exist!
Maybe it needs double nesting?!
Expected Behavior
No error. The database relation DOES exist.
Steps To Reproduce
In a model's post-hook, call a custom macro that utilises dbt_utils.get_column_values
Relevant log output
No response
Environment
Which database adapter are you using with dbt?
bigquery
Additional Context
No response
The text was updated successfully, but these errors were encountered: