Skip to content

Star expansion loses position metadata on generated columns #6642

@doripo

Description

@doripo

Note: This issue is about optimizer behavior, not dialect-specific parsing/transpilation.

Fully reproducible code snippet (v28.5.0 / main):

import sqlglot
from sqlglot.optimizer.qualify_columns import qualify_columns

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

parsed = sqlglot.parse_one(sql)
print(f"Star meta: {parsed.expressions[0].meta}")
# {'line': 1, 'col': 8, 'start': 7, 'end': 7}

qualified = qualify_columns(parsed, schema=schema)
for col in qualified.expressions:
    print(f"{col}: meta={col.meta}")
# t.a AS a: meta={}
# t.b AS b: meta={}

Expected

Expanded columns inherit the star's position metadata.

Actual

Expanded columns have empty meta={}.

References

  • Expression.update_positions() exists for copying position metadata (used in parser, BigQuery dialect)
  • tests/test_parser.py:985 verifies star position metadata is preserved during parsing
  • Root cause is in _expand_stars() in qualify_columns.py (line 868), but similar issues exist in the pivot, coalesce (USING), and struct expansion code paths within that function

Happy to submit a PR if this is considered a bug worth fixing. Would appreciate feedback on whether to address just the common case (regular SELECT *) or the other code paths as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions