-
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-2004] [Bug] Insert statement in Post hook not working as expected #6837
Comments
Hey @jaswanthColibri ! Without seeing an example of how you are specifying your post-hook, it's a bit hard for me to tell. Is there any chance you could provide that? Is it similar to this by any chance? {{
config(
post_hook=post_hook_audit_macro(this, source('your_source_name', 'your_table_name')),
)
}}
... If so, you'll actually want to express it like this: {{
config(
post_hook="{{ post_hook_audit_macro(this, source('your_source_name', 'your_table_name')) }}",
)
}}
... @jtcohen6 has a such nice explanation here that I won't even try to improve upon it: Note in particular where he compares Either way, will be eager to hear if any of this is helpful for you. |
@dbeatty10 thank you for the response. It is working can i put source() expression in a variable and pass it to macro like below. {% set src = source('src_name', 'table_name')) %} post_hook = "{{ post_hook_audit_macro(this, src }}" when i try this src is empty string in macro. I want to put source expression in a variable and pass to macro in post hook. |
Great news @jaswanthColibri ! 🎉 Putting an expression in a variable and passing it into a macro in the configThese workCould you try something like this? Note all the quotes -- they are crucial! {% set source_macro_string = "source('your_source_name', 'your_table_name')" %}
{{
config(
post_hook="{{ post_hook_audit_macro(this, " ~ source_macro_string ~ ") }}",
)
}} See below for an explanation why this works. Side note: as unreadable as it is, this actually works too! {{
config(
post_hook="{% set src_table = source('your_source_name', 'your_table_name') %}{{ post_hook_audit_macro(this, src_table) }}",
)
}} These don't workThis won't have the effect you intend: {% set source_relation = source('your_source_name', 'your_table_name') %}
{{
config(
post_hook="{{ post_hook_audit_macro(this, source_relation) }}",
)
}} And neither will this: {% set source_relation = source('your_source_name', 'your_table_name') %}
{{
config(
post_hook="{{ post_hook_audit_macro(this, '" ~ source_relation ~ "') }}",
)
}} The reason why neither of these work is explained nicely here. An explanationAs @jtcohen6 mentioned here, dbt Jinja-renders your model twice:
Both 👉 So you want the entirety of your post-hook to be re-rendered at execute time. Working backwardsIt's helpful to think about it backwards. Ultimately, you want the following string template to be rendered during that 2nd execution phase (which will yield the SQL to execute as a post-hook): {% set final_string = "{{ post_hook_audit_macro(this, source('your_source_name', 'your_table_name')) }}" %} Separating the {% set source_macro_string = "source('your_source_name', 'your_table_name')" %}
{% set final_string = "{{ post_hook_audit_macro(this, " ~ source_macro_string ~ ") }}" %} Renaming
Now we can see how to separate the last bit into the {% set source_macro_string = "source('your_source_name', 'your_table_name')" %}
{{
config(
post_hook="{{ post_hook_audit_macro(this, " ~ source_macro_string ~ ") }}",
)
}} |
@dbeatty10 and yet you did! I think we simply must update documentation for this in a way that preempts this same issue coming up over & over: dbt-labs/docs.getdbt.com#2818 Going to close this for now as a |
Is this a new bug in dbt-core?
Current Behavior
Hi Team
I have a macro that takes two arguements
and i call this macro in a post hook to insert a record in my audit table.
macro looks like below
`
{% macro post_hook_audit_macro( tgt_tbl,src_tbl) -%}
{{ print("Target Table " ~ tgt_tbl)}}
{{ print("Source Table " ~ src_tbl)}}
insert into {{ var('audit_table') }}
select sysdate,'{{tgt_tbl}}',(select count() from {{tgt_tbl}}),
'{{src_tbl}}',(select count() from {{src_tbl}} );
{{ print("Target Table after" ~ tgt_tbl)}}
{{ print("Source Table after" ~ src_tbl)}}
{%- endmacro %}
The problem is i could see proper target table and source table in printed logs.
But after insert is done instead of below data
sysdate | target_table_name|target_table_count|src_table_name|src_table_count
I am seeing below in Warehouse audit table
sysdate | target_table_name| target_table_count | target_table_name| target_table_count
how can i insert record as mentioned in first format?
`
Expected Behavior
Audit table should have below data
sysdate | target_table_name|target_table_count|src_table_name|src_table_count
instead of
sysdate | target_table_name| target_table_count | target_table_name| target_table_count
Steps To Reproduce
create a model and have post hook to insert record into a audit table.
audit table should have
sysdate
target model name
target model count
source model name
source model count
to macro pass {{this}} and source(src_name, tbl_name)
Relevant log output
No response
Environment
Which database adapter are you using with dbt?
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: