Skip to content

fix: handle Annotated nodes in lineage column resolution#73

Open
eitsupi wants to merge 1 commit intotobilg:mainfrom
eitsupi:fix/annotated-lineage-comment
Open

fix: handle Annotated nodes in lineage column resolution#73
eitsupi wants to merge 1 commit intotobilg:mainfrom
eitsupi:fix/annotated-lineage-comment

Conversation

@eitsupi
Copy link

@eitsupi eitsupi commented Mar 20, 2026

Summary

  • Fix get_alias_or_name() to unwrap Annotated expression nodes, which are produced when SQL comments appear between SELECT and the first column
  • Without this fix, lineage tracing fails with "Cannot find column" for queries like:
    WITH t AS (SELECT 1 AS a)
    SELECT
      -- comment
      a FROM t
  • This pattern is common in dbt compiled SQL where Jinja templates insert section comments

Details

When the parser encounters a comment before a SELECT expression, it wraps the expression in an Annotated node to preserve the comment. The get_alias_or_name() function in the lineage module only matched Alias, Column, Identifier, and Star variants — it did not handle Annotated, returning None and causing find_select_expr() to fail.

The fix adds a single recursive arm for Expression::Annotated(a) => get_alias_or_name(&a.this), consistent with how find_column_refs_in_expr already handles Annotated nodes elsewhere in the same file.

Test plan

  • Add test: line comment before first column in CTE query
  • Add test: block comment before first column
  • Add test: comment before first column with multiple columns (second column unaffected)
  • Add test: comment before aliased column
  • Add test (ignored): ported sqlglot test_node_name_doesnt_contain_comment (requires derived table star expansion, separate issue)
  • All 942 existing tests pass

🤖 Generated with Claude Code

When a SQL comment appears between SELECT and the first column
(e.g. `SELECT\n-- comment\na FROM t`), the parser wraps the column
expression in an Annotated node. The `get_alias_or_name` function
did not handle this variant, causing `find_select_expr` to fail
with "Cannot find column" errors.

Add an Annotated arm to `get_alias_or_name` that unwraps to the
inner expression. This is consistent with `find_column_refs_in_expr`
which already handles Annotated nodes.

Add tests for line comments, block comments, aliased columns with
comments, and multi-column queries with leading comments.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant