dbt-labs / dbt-spark

dbt-spark contains all of the code enabling dbt to work with Apache Spark and Databricks
https://getdbt.com
Apache License 2.0
399 stars 227 forks source link

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

Open siljamardla opened 2 months ago

siljamardla commented 2 months ago

Is this a new bug in dbt-core?

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") }}
    )
    … 
  2. 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

dbeatty10 commented 2 months ago

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: