dbt-labs / dbt-labs/dbt

[v2 Bug] Static analysis dbt0227 false positive: Snowflake lateral column alias not resolved in same SELECT clause

Open
#16,141 0 comments 0 reactions 0 assignees View on GitHub
adapter:snowflake area:static-analysis engine:v2 type:bug
Dominant language
Rust
Stars
13.8k
Forks
2.6k
Avg merge
21h 31m
Merged PRs (30d)
56

Description

### Is this a new bug in dbt v2.x compared to the latest version of dbt 1.x?

- [x] I believe this is a new bug in dbt v2.x
- [x] I have searched the existing issues and could not find a duplicate

### Current Behavior

Fusion's static analyzer raises `UnresolvedIdentifier (dbt0227)` when a column alias defined earlier in the same `SELECT` clause is referenced by a later expression — i.e., a **lateral column alias**. Snowflake natively supports this pattern and executes the query without error.

Example SQL:

```sql
NULLIF(A.SOME_COLUMN, '**') AS col_alias,
CASE WHEN col_alias IS NOT NULL
THEN COALESCE(A.ANOTHER_COLUMN, '*')
ELSE NULL
END AS derived_col
```

Error:

```
[UnresolvedIdentifier (dbt0227)]:
No column COL_ALIAS found in the locally cached schema for the source
```

### Expected Behavior

No `dbt0227` error. Snowflake's SQL dialect permits referencing a column alias defined earlier in the same `SELECT` list (lateral column aliasing). Fusion's binder should recognize this construct and resolve the alias without flagging an unresolved identifier.

### Steps To Reproduce

1. Create a model that uses a lateral column alias in a `SELECT` clause against a Snowflake target:

```sql
-- models/repro_lateral_alias.sql
{{ config(materialized='view') }}

SELECT
NULLIF(col_a, '**') AS alias_col,
CASE WHEN alias_col IS NOT NULL THEN 'yes' ELSE 'no' END AS derived_col
FROM {{ source('my_source', 'my_table') }}
```

2. Run `dbt compile` (or `dbt build`).
3. Observe `dbt0227: No column ALIAS_COL found`.

### Relevant log output

```shell
[UnresolvedIdentifier (dbt0227)]:
No column COL_ALIAS found in the locally cached schema for the source
```

### Environment

```markdown
- dbt Fusion (v2)
- Adapter: Snowflake
```

### Which database adapter are you using?

snowflake

### Is this a discrepancy vs. dbt 1.x?

- [x] Yes — this works in dbt 1.x but not in dbt v2.x

### Workaround

Add `static_analysis: baseline` (or `static_analysis: off`) to the model config to bypass the static analyzer for the affected model:

```yaml
models:
my_project:
path:
to:
model:
+static_analysis: baseline
```

### Additional Context

Snowflake supports lateral column aliases in `SELECT` — see [Snowflake docs on lateral alias](https://docs.snowflake.com/en/sql-reference/sql/select#lateral-alias). The SQL binder in Fusion does not currently resolve aliases defined earlier in the same `SELECT` list, causing false `dbt0227` errors on valid Snowflake SQL.

Reported via internal customer feedback.

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.