Skip to content

Conversation

@abesticode
Copy link

Description

This PR fixes an issue where columns generated via star expansion (e.g., SELECT *) or output qualification lost their original position metadata (line, col, start, end).

This metadata is essential for static analysis tools, linters, and error reporting to correctly point back to the source location of a column.

Changes

  • _expand_stars: Modified to copy metadata from the original Star expression to the newly expanded Column expressions.
  • qualify_outputs: Updated to ensure metadata is preserved when wrapping expressions in an Alias.

Example

Input SQL:

SELECT * FROM t
-- ^ "Star" is at line 1, col 8

Before:
Generated columns had valid names but lost their location info:

Column(a): meta={} 
Column(b): meta={}

After:
Generated columns inherit location info from the star:

Column(a): meta={'line': 1, 'col': 8, ...}
Column(b): meta={'line': 1, 'col': 8, ...}

Reproduction Script

import sqlglot
from sqlglot.optimizer.qualify_columns import qualify_columns

sql = 'SELECT * FROM t'
schema = {'t': {'a': 'INT'}}

expression = sqlglot.parse_one(sql)
qualified = qualify_columns(expression, schema=schema)

col_a = qualified.expressions[0]
print(f"Meta: {col_a.meta}") 
# Now prints: {'line': 1, 'col': 8, ...}

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