dbt-labs / dbt-core

dbt enables data analysts and engineers to transform their data using the same practices that software engineers use to build applications.
https://getdbt.com
Apache License 2.0
9.25k stars 1.53k forks source link

[Bug] Unit test compilation error 'var is undefined' when combining a variable with math operators #10353

Open adamribaudo-velir opened 6 days ago

adamribaudo-velir commented 6 days ago

Is this a new bug in dbt-core?

Current Behavior

When running a unit test for a model that contains a Jinja math operation on a variable, a compilation error occurs

Example operation

select {{ var('foo', 10 ) * 1}} as result

Error:

Compilation Error in unit_test test_unit_test (models\staging\test.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".

Running the unit test on a Jinja variable without the math operation works successfully. Ex: select {{ var('foo', 10 ) }} as result

Expected Behavior

Expected behavior would be to compile unit test and executing the math operation on the variable.

Steps To Reproduce

  1. In any dbt project, create a model called test with the following SQL:
    select {{ var('foo', 10 ) * 1}} as result
  2. In that model's .yml file, create a unit test as follows (the 'input' is not used, but required):
    
    version: 2

unit_tests:

Relevant log output

(python312env) C:\GitHub\velir-ga4-test-project>dbt test -s test_unit_test
22:04:52  Running with dbt=1.8.2
22:04:53  Registered adapter: bigquery=1.8.1
22:04:54  Found 59 models, 28 data tests, 1 seed, 1 source, 604 macros, 11 unit tests
22:04:54  
22:04:55  Concurrency: 4 threads (target='dev')
22:04:55  
22:04:55  1 of 1 START unit_test test::test_unit_test .................................... [RUN]
22:04:55  1 of 1 ERROR test::test_unit_test .............................................. [ERROR in 0.01s]
22:04:55  
22:04:55  Finished running 1 unit test in 0 hours 0 minutes and 1.22 seconds (1.22s).
22:04:55  
22:04:55  Completed with 1 error and 0 warnings:
22:04:55
22:04:55    Compilation Error in unit_test test_unit_test (models\staging\test.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".
22:04:55
22:04:55  Done. PASS=0 WARN=0 ERROR=1 SKIP=0 TOTAL=1

Environment

- OS: Windows
- Python: 3.12
- dbt-core: 1.8.3, bigquery: 1.8.3

Which database adapter are you using with dbt?

bigquery

Additional Context

No response