dbt-labs / dbt-labs/dbt-adapters

[Bug] `dbt build --empty` results in a SQL compilation error when joining unaliased models

Open
#693 0 comments 4 reactions 0 assignees View on GitHub
pkg:dbt-snowflake type:bug
Dominant language
Python
Stars
233
Forks
362
Avg merge
3d 22h
Merged PRs (30d)
9

Description

### Is this a new bug in dbt-snowflake?

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

### Current Behavior

When running `dbt build --empty` (or `run` or possibly other variants) while using models with joins between ref-ed tables, Snowflake will return an error `002027 (42601): SQL compilation error: duplicate alias 'values'`. This occurs while running without the `--empty` flag will succeed.

### Expected Behavior

Running dbt with the `--empty` flag should have the same results as running without the flag.

### Steps To Reproduce

This error can be reproduced using three models:

repro1.sql:
```sql
select 1 as a
```

repro2.sql:
```sql
select 1 as a
```

repro3.sql:
```sql
select *
from {{ ref('repro1') }}
join {{ ref('repro2') }} using (a)
```

### Relevant log output

_No response_

### Environment

```markdown
- OS: macOS Sequoia 15.1
- Python: 3.12.1
- dbt-core: 1.8.8
- dbt-snowflake: 1.8.4
```

### Additional Context

The issue exists only when performing a join on "ref"-ed models, _without_ using an alias for any of the models.

The issue appears to be the result of both dbt using subqueries to template refs while using the `--empty` flag (in order to add a `limit` to the subquery), and Snowflake implicitly aliasing a subquery as `values` if that subquery does not have an explicit alias (see https://community.snowflake.com/s/article/A-query-joining-on-subquery-without-alias-fails-with-the-error).

The issue can be resolved by adding aliases, as suggested by the Snowflake article linked above, so this might be considered not worth fixing.

I had a quick look to see if this could be resolved by using [require_alias](https://github.com/dbt-labs/dbt-snowflake/blob/f6468f60a6eafff584799122e34608aabb7414e4/dbt/adapters/snowflake/relation.py#L37), however this conflicts with any joins where an alias is already explicitly defined (which will likely be most cases in a typical project).

Another solution to this might be to use the ephemeral-model approach of templating via a CTE rather than a subquery (although this might be a change in dbt-adapters).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.