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

[Bug] Pre- and post-hooks use parse phase values during the execution phase #10148

Closed
2 tasks done
Klimmy opened this issue May 15, 2024 · 3 comments
Closed
2 tasks done
Labels
bug Something isn't working wontfix Not a bug or out of scope for dbt-core

Comments

@Klimmy
Copy link

Klimmy commented May 15, 2024

Is this a new bug in dbt-core?

  • I believe this is a new bug in dbt-core
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

I'm unsure if that's expected, but when I run pre/post-hook with a variable, dbt compiles the hook statement using variable value from the parse phase.

In the following example, the pre-hook will be SELECT 999 instead of SELECT 111:

{% if execute %}
    {% set val = 111 %}
{% else %}
    {% set val = 999 %}
{% endif %}

{{
    config(
      pre_hook="SELECT " ~ val
    )
}}

SELECT 1 AS id

Expected Behavior

To have execution-phase values when the hook runs.

Steps To Reproduce

  1. Add the model:
    hook_issue.sql
{% if execute %}
    {% set val = 111 %}
{% else %}
    {% set val = 999 %}
{% endif %}

{{ print("Model. Execute: " ~ execute ~ "; Value: " ~ val) }}

{{
    config(
      pre_hook="{{ print('Pre-hook. Execute: " ~ execute ~ "; Value: " ~ val ~ "') }}",
      post_hook="{{ print('Post-hook. Execute: " ~ execute ~ "; Value: " ~ val ~ "') }}",
    )
}}

SELECT 1 AS id
  1. run dbt --no-partial-parse run --select models/hook_issue.sql
  2. In the command line output you will see that the model will have 111 value during the execution, when hooks will still have 999.

Relevant log output

dbt --no-partial-parse run --select models/hook_issue.sql 
01:02:52  Running with dbt=1.8.0
01:02:52  Registered adapter: postgres=1.8.0
Model. Execute: False; Value: 999
Pre-hook. Execute: False; Value: 999
Post-hook. Execute: False; Value: 999
01:02:54  Found 8 models, 1 snapshot, 2 sources, 1 metric, 533 macros, 1 semantic model
01:02:54  
01:02:55  Concurrency: 1 threads (target='dev')
01:02:55  
01:02:55  1 of 1 START sql view model analytics.hook_issue ............................... [RUN]
Model. Execute: True; Value: 111
Pre-hook. Execute: False; Value: 999
Post-hook. Execute: False; Value: 999
01:02:55  1 of 1 OK created sql view model analytics.hook_issue .......................... [CREATE VIEW in 0.11s]
01:02:55  
01:02:55  Finished running 1 view model in 0 hours 0 minutes and 0.41 seconds (0.41s).
01:02:55  
01:02:55  Completed successfully
01:02:55  
01:02:55  Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1

Environment

- OS: macOS 13.6.3
- Python: 3.11.9
- dbt-core: 1.8.0
- dbt-postgres: 1.8.0

Which database adapter are you using with dbt?

postgres

Additional Context

No response

@Klimmy Klimmy added bug Something isn't working triage labels May 15, 2024
@dbeatty10 dbeatty10 self-assigned this May 15, 2024
@dbeatty10
Copy link
Contributor

Thanks for reaching out about this and providing such a nice example @Klimmy 🤩

This is expected, and you're not the first to be surprised. See #6837 (comment) and other linked issues for explanations.

Example

Try out the following to see how you can still get the behavior you are after.

models/my_model.sql

{# jinja for parsing phase #}
{% if execute %}
    {% set val = 111 %}
{% else %}
    {% set val = 999 %}
{% endif %}

{# jinja for execution phase #}
{% set jinja_for_execution = """
{% if execute %}
    {% set val = 111 %}
{% else %}
    {% set val = 999 %}
{% endif %}
""" %}

{{
    config(
      pre_hook=[
        jinja_for_execution ~ "{{ print('❌ Pre-hook X. Execute: " ~ execute ~ "; Value: " ~ val ~ "') }}",
        jinja_for_execution ~ "{{ print('✅ Pre-hook Y. Execute: ' ~ execute ~ '; Value: ' ~ val ~ '') }}",
        jinja_for_execution ~ "SELECT {{ val }} AS pre_hook_sql",
        ],
    )
}}

SELECT 1 AS id
dbt --no-partial-parse run -s my_model

Output:

(dbt_1.8) $ dbt --no-partial-parse run -s my_model

17:13:05  Running with dbt=1.8.0
17:13:05  Registered adapter: postgres=1.8.0
❌ Pre-hook X. Execute: False; Value: 999
✅ Pre-hook Y. Execute: False; Value: 999
17:13:06  Found 2 models, 413 macros
17:13:06  
17:13:07  Concurrency: 5 threads (target='postgres')
17:13:07  
17:13:07  1 of 1 START sql view model dbt_dbeatty.my_model ............................... [RUN]
❌ Pre-hook X. Execute: False; Value: 999
✅ Pre-hook Y. Execute: True; Value: 111
17:13:07  1 of 1 OK created sql view model dbt_dbeatty.my_model .......................... [CREATE VIEW in 0.13s]
17:13:07  
17:13:07  Finished running 1 view model in 0 hours 0 minutes and 0.49 seconds (0.49s).
17:13:07  
17:13:07  Completed successfully
17:13:07  
17:13:07  Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1

Since this is expected behavior rather than a bug, I'm going to close this issue as not planned.

But just let me know if you have any follow-up thoughts or questions.

@dbeatty10 dbeatty10 closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2024
@dbeatty10 dbeatty10 added wontfix Not a bug or out of scope for dbt-core and removed triage labels May 15, 2024
@dbeatty10 dbeatty10 removed their assignment May 15, 2024
@Klimmy
Copy link
Author

Klimmy commented May 15, 2024

Wow, thank you, @dbeatty10, for such a quick and detailed response!
It's clear now, apologize that I haven't found these discussions beforehand :)

@dbeatty10
Copy link
Contributor

No need to apologize -- glad to connect you with helpful information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix Not a bug or out of scope for dbt-core
Projects
None yet
Development

No branches or pull requests

2 participants