dbt-spark icon indicating copy to clipboard operation
dbt-spark copied to clipboard

[Bug] The `--empty` flag produces broken SQL when code has `{{ref...}}`or `{{source..}}` with an alias

Open siljamardla opened this issue 5 months ago • 1 comments

Is this a new bug in dbt-core?

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

Current Behavior

If you have a dbt model that uses the {{ref...}}or {{source..}} macro together with an alias, for example:

select * from {{ ref("dim_car") }} as cars 

and you try to run it with the --empty flag then the compiled SQL will have double aliasing, resulting in broken SQL

Expected Behavior

The alias should not be changed as to not alter the script

Steps To Reproduce

Create a dbt model with a {{ref...}} macro and try to compile its code with the --empty flag.

Relevant log output

No response

Environment

- OS: macOS
- Python: 3.11
- dbt: 1.8

Which database adapter are you using with dbt?

spark

Additional Context

There are 2 workarounds.

  1. Write all your dbt models with CTEs so that there are no aliases right after the macros, something like
with cars as (
    select * from {{ ref("dim_car") }}
)
… 
  1. Update the BaseRelation class to have require_alias be False by default. It's in the .venv/lib/python3.11/site-packages/dbt/adapters/base/relation.py file, line 51

siljamardla avatar Sep 02 '24 12:09 siljamardla