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] on-start-run executes during compilation #4445

Closed
1 task done
tekumara opened this issue Dec 7, 2021 · 5 comments
Closed
1 task done

[Bug] on-start-run executes during compilation #4445

tekumara opened this issue Dec 7, 2021 · 5 comments
Labels
bug Something isn't working wontfix Not a bug or out of scope for dbt-core

Comments

@tekumara
Copy link

tekumara commented Dec 7, 2021

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

on-start-run executes macros during dbt docs and dbt compile even when inside an if execute conditional.

Expected Behavior

dbt docs and dbt compile don't run macro code inside an if execute conditional

Steps To Reproduce

macros/run_start.sql

{% macro run_start() %}
  {% if execute %}
  {{ dbt_utils.log_info("🏁 \033[1m Let's go! 🏁 \033[0m")}}
  {% set results = run_query('select 1 from foobar') %}
  {% endif %}
{% endmacro %}

dbt_project.yml

on-run-start: "{{ run_start() }}"

Relevant log output

Running with dbt=0.21.1
Found 9 models, 7 tests, 0 snapshots, 0 analyses, 572 macros, 2 operations, 8 seed files, 4 sources, 0 exposures

12:24:46 | Concurrency: 4 threads (target='dev')
12:24:46 | 
12:24:46 + 🏁  Let's go! 🏁 
Encountered an error:
Runtime Error
  Database Error in operation jaffleshop-on-run-start-0 (./dbt_project.yml)
    002003 (42S02): SQL compilation error:
    Object 'FOOBAR' does not exist or not authorized.

Environment

- OS: darwin
- Python: 3.9
- dbt: 0.21.0

What database are you using dbt with?

snowflake

Additional Context

No response

@tekumara tekumara added bug Something isn't working triage labels Dec 7, 2021
@jtcohen6
Copy link
Contributor

jtcohen6 commented Dec 7, 2021

@tekumara Thanks for opening!

The compile task (and thereby the docs generate task, too) compiles SQL, including macros + hooks, in execute mode. The distinction between run and compile is subtle: compile is a subset of run functionality; model SQL is fully compiled for both (including execute-time introspective queries); the only step excluded in compile is the materialization logic itself.

We do need to update the docs for on-run-* hooks to reflect this reality. I just opened a PR to do that: dbt-labs/docs.getdbt.com#948

The good news is, in v1.0, there is now a context variable available from the flags module (docs), flags.WHICH, which will return the name of the currently running task/command. So your macro code could look like:

{% macro run_start() %}
  {% if execute and flags.WHICH in ('run', 'build') %}
  {{ dbt_utils.log_info("🏁 \033[1m Let's go! 🏁 \033[0m")}}
  {% set results = run_query('select 1 from foobar') %}
  {% endif %}
{% endmacro %}

Be advised, you should not use any values of flags in code needed at parse time (dependencies + configurations).

@jtcohen6 jtcohen6 closed this as completed Dec 7, 2021
@jtcohen6
Copy link
Contributor

jtcohen6 commented Dec 7, 2021

Since there's a viable workaround in v1.0, I'm going to close this as a wontfix

@jtcohen6 jtcohen6 added the wontfix Not a bug or out of scope for dbt-core label Dec 7, 2021
@tekumara
Copy link
Author

tekumara commented Dec 8, 2021

Thanks for the explanation and workaround @jtcohen6!

If the on-run-* hooks run for most everything, and not specifically run, maybe there's a better name for them?

@jtcohen6
Copy link
Contributor

jtcohen6 commented Dec 8, 2021

That's fair! on-task-start? on-execute-start? There are other places where we use the word run to refer to any task / execution, such as run_results.json

@tekumara
Copy link
Author

Or how about on-dbt-start perhaps?

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