Search code examples
google-bigquerydbt

In DBT BigQuery, how to both define & call macro from within the same file


trying_macro.sql

{# {% macro get_four() %}
  2 + 2
{% endmacro %} #}

with
  t1 as (select {{ get_four() }} as four)

select * from t1

The docs - https://docs.getdbt.com/docs/build/jinja-macros - don't touch on whether it is possible to define and call a macro from the same file. When run in DBT, it throws an error:

enter image description here

We understand that we can save these in a separate .sql file in our /macros directory, but we have some macros that are very-clearly file specific, and we'd strongly prefer having the macros defined at the top of the file. Is this possible?


Solution

  • Unfortunately, this isn't something that's currently possible in dbt. You can see some discussion on the possibility of supporting this in the future on GitHub.