-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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] Unit test compilation error 'var is undefined' when macro is used with project variables as input parameters #10539
Comments
Thanks for reporting this @canva-owen-yan ! The error message looks the same as in #10157, so I'm wondering if you are experiencing the same (or similar) thing. When I tried to replicate this though, I wasn't able to. Could you try out the following files and try tweaking them as needed to reproduce the error you are seeing?
vars:
columns:
- column_a
- column_b
- column_c
{%- macro get_columns(include=[]) -%}
{%- for col in include -%}
{{ col }}{% if not loop.last %}, {% endif %}
{%- endfor -%}
{%- endmacro -%}
select
1 as column_a,
2 as column_b,
3 as column_c
select
{{ get_columns(
include=var('columns')
)}}
from {{ ref("upstream_model") }}
unit_tests:
- name: test_10539
model: my_model
given:
- input: ref('upstream_model')
format: dict
rows:
- {column_a: 11}
expect:
rows:
- {column_a: 11} Commands: dbt build Output:
|
Thanks for looking into it @dbeatty10!
vars:
columns_list_one:
- column_a
- column_b
columns_list_two:
- column_c
select
{{ get_columns(
include=var('columns_list_one') + var('columns_list_two')
)}}
from {{ ref("upstream_model") }} Commands: dbt run -m my_model Output: 05:01:28 Concurrency: 1 threads (target='dev')
05:01:28
05:01:28 1 of 1 START sql view model dev.my_model ................................. [RUN]
05:01:31 1 of 1 OK created sql view model dev.my_model ............................ [SUCCESS 1 in 2.92s]
05:01:33
05:01:33 Finished running 1 view model, 1 project hook in 0 hours 0 minutes and 18.22 seconds (18.22s).
05:01:38
05:01:38 Completed successfully
05:01:38
05:01:38 Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1 dbt build -m my_model Output: 05:09:26 Concurrency: 1 threads (target='dev')
05:09:26
05:09:26 1 of 2 START unit_test dev.my_model::test_10539 ..................... [RUN]
05:09:26 1 of 2 ERROR dev.my_model::test_10539 ............................... [ERROR in 0.01s]
05:09:26 2 of 2 SKIP relation dev.my_model ............................. [SKIP]
05:09:29
05:09:29 Finished running 1 unit test, 1 view model, 1 project hook in 0 hours 0 minutes and 20.20 seconds (20.20s).
05:09:35
05:09:35 Completed with 1 error and 0 warnings:
05:09:35
05:09:35 Compilation Error in unit_test test_10539 (models/dev/my_model/_unit_tests.yml)
'var' is undefined. This can happen when calling a macro that does not exist. Check for typos and/or install package dependencies with "dbt deps".
05:09:35
05:09:35 Done. PASS=0 WARN=0 ERROR=1 SKIP=1 TOTAL=2 |
Thanks for updating that example @canva-owen-yan -- I was able to reproduce with the files and commands that you provided 👍 The error message looks the same / similar to #10353 and #10157, but it's not obvious if it is a duplicate issue or not, so I'll leave all three of those open as distinct issues for now. |
ReprexCreate these files:
# project name, etc here
vars:
columns_list_one:
- column_a
- column_b
columns_list_two:
- column_c
{%- macro get_columns(include=[]) -%}
{%- for col in include -%}
{{ col }}{% if not loop.last %}, {% endif %}
{%- endfor -%}
{%- endmacro -%}
select
{{ get_columns(
include=var('columns_list_one') + var('columns_list_two')
)}}
from {{ ref("upstream_model") }}
unit_tests:
- name: test_10539
model: my_model
overrides:
macros:
get_columns: ['column_a','column_b','column_c']
vars:
columns_list_one:
- column_a
- column_b
columns_list_two:
- column_c
given:
- input: ref('upstream_model')
format: dict
rows:
- {column_a: 11}
expect:
rows:
- {column_a: 11} Run these commands: dbt build --select my_model Get this output:
Note I got the same error message regardless if anything was supplied to |
@dbeatty10 Is anyone working on the fix for this? It would be nice if the fix could be rolled out as part of dbt 1.9. Our organisation is currently blocked from adopting unit tests because of this bug. |
Hi @jonathanneo @canva-owen-yan @dbeatty10, The important point is that I ran the code provided by @canva-owen-yan, and the root cause was the same. |
With the new changes in #10849, the code is working @canva-owen-yan @jonathanneo !
# project name, etc here
vars:
columns_list_one:
- column_a
- column_b
columns_list_two:
- column_c
|
Is this a new bug in dbt-core?
Current Behavior
When calling calling a macro that takes project variables as input, the configured unit tests fail with
var is undefined
error.Overriding the macro or project variables doesn't help but removing the variables resolves the issue. No issue with running the model without unit test.
Expected Behavior
Expecting unit test to work with macro taking project variables as input, or at least end user can override the macro or the project variables used as inputs for the macro.
Steps To Reproduce
get_columns.sql
my_model.sql
dbt_project.yml
unit_test__my_model.yml
Tried without override
Override macro:
Override input:
Relevant log output
Environment
Which database adapter are you using with dbt?
snowflake
Additional Context
No response
The text was updated successfully, but these errors were encountered: