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] The --empty flag produces broken SQL when code has {{ref...}}or {{source..}} with an alias #1098

Open
2 tasks done
siljamardla opened this issue Sep 2, 2024 · 2 comments · May be fixed by #1162
Open
2 tasks done
Labels
bug Something isn't working unit tests Issues related to built-in dbt unit testing functionality

Comments

@siljamardla
Copy link

Is this a new bug in dbt-core?

  • I believe this is a new bug in dbt-core
  • 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 siljamardla added bug Something isn't working triage labels Sep 2, 2024
@dbeatty10 dbeatty10 added the unit tests Issues related to built-in dbt unit testing functionality label Sep 3, 2024
@dbeatty10
Copy link
Contributor

Thanks for reporting this @siljamardla !

To align with the corresponding solutions for dbt-bigquery, dbt-snowflake, dbt-redshift, etc, I think the solution would be to add require_alias: bool = False here rather than changing the default value here.

Here are some reference implementations:

@dbeatty10 dbeatty10 removed the triage label Sep 3, 2024
@dbeatty10 dbeatty10 transferred this issue from dbt-labs/dbt-core Sep 3, 2024
@anaghshineh
Copy link

Hey, @dbeatty10! Hope you're well. I recently changed jobs and am going to be working more heavily with the Spark adapter (and less so the BigQuery one!). Hit this issue last week when trying to run --empty. Adopted the approach you outlined above. Hope to see this integrated soon, so my team can take advantage of dry runs as part of CI!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unit tests Issues related to built-in dbt unit testing functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants