Skip to content

fix(optimizer): handle ColumnDef in typed table alias columns#7542

Merged
georgesittas merged 1 commit intomainfrom
fix/qualify-tables-typed-alias-columns
Apr 22, 2026
Merged

fix(optimizer): handle ColumnDef in typed table alias columns#7542
georgesittas merged 1 commit intomainfrom
fix/qualify-tables-typed-alias-columns

Conversation

@tobymao
Copy link
Copy Markdown
Owner

@tobymao tobymao commented Apr 22, 2026

Summary

  • qualify_tables._set_alias unconditionally called exp.to_identifier(c) on every entry in TableAlias.columns, which only accepts str/Identifier. Typed table-function signatures like FROM fn() AS t(col INT) produce ColumnDef nodes and tripped ValueError: Name needs to be a string or an Identifier, got: <class '...ColumnDef'> when the column-assignment branch ran (e.g. under canonicalize_table_aliases=True).
  • Route only strings through to_identifier; copy any other expression (Identifier, ColumnDef) to preserve the original copy-before-reparent semantics.

Repro (before):

qualify_tables(
    sqlglot.parse_one('SELECT * FROM JSON_TO_RECORDSET(z) AS y("rank" INT)'),
    canonicalize_table_aliases=True,
)
# ValueError: Name needs to be a string or an Identifier, got: <class '...ColumnDef'>

After: SELECT * FROM JSON_TO_RECORDSET(z) AS _0("rank" INT)

Test plan

  • New test_qualify_tables_copies_typed_alias_columns exercises the ColumnDef path via canonicalize_table_aliases=True and asserts the column is preserved as ColumnDef, is a different object than the original (the copy check), and has identical SQL.
  • Confirmed the test fails on a reverted-copy variant and passes with the fix.
  • python -m unittest tests.test_optimizer — 77 tests pass.

@tobymao tobymao force-pushed the fix/qualify-tables-typed-alias-columns branch from e3eaf1d to 58c0dbe Compare April 22, 2026 01:10
@tobymao tobymao changed the title fix(optimizer): handle ColumnDef in typed table alias columns [CLAUDE] fix(optimizer): handle ColumnDef in typed table alias columns Apr 22, 2026
@tobymao tobymao force-pushed the fix/qualify-tables-typed-alias-columns branch from 58c0dbe to 38e2646 Compare April 22, 2026 01:21
qualify_tables._set_alias called exp.to_identifier() on every column,
which rejects ColumnDef nodes produced by typed table-function signatures
like FROM fn() AS t(col INT). Route only strings through to_identifier
and copy any other expression (Identifier, ColumnDef) to preserve the
existing copy-before-reparent semantics.

Widens the columns/function_columns type annotations to include
exp.ColumnDef so mypyc's runtime type checks accept it.
@tobymao tobymao force-pushed the fix/qualify-tables-typed-alias-columns branch from 38e2646 to 93e1b52 Compare April 22, 2026 01:23
@github-actions
Copy link
Copy Markdown
Contributor

SQLGlot Integration Test Results

Comparing:

  • this branch (sqlglot:fix/qualify-tables-typed-alias-columns, sqlglot version: fix/qualify-tables-typed-alias-columns)
  • baseline (main, sqlglot version: 0.0.1.dev1)

By Dialect

dialect main sqlglot:fix/qualify-tables-typed-alias-columns transitions links
bigquery -> bigquery 24645/24650 passed (100.0%) 23495/23495 passed (100.0%) No change full result / delta
bigquery -> duckdb 867/1154 passed (75.1%) 0/0 passed (0.0%) Results not found full result / delta
duckdb -> duckdb 5823/5823 passed (100.0%) 0/0 passed (0.0%) Results not found full result / delta
snowflake -> duckdb 1063/1961 passed (54.2%) 0/0 passed (0.0%) Results not found full result / delta
snowflake -> snowflake 65133/65133 passed (100.0%) 63027/63027 passed (100.0%) No change full result / delta
databricks -> databricks 1370/1370 passed (100.0%) 1370/1370 passed (100.0%) No change full result / delta
postgres -> postgres 6042/6042 passed (100.0%) 6042/6042 passed (100.0%) No change full result / delta
redshift -> redshift 7101/7101 passed (100.0%) 7101/7101 passed (100.0%) No change full result / delta

Overall

main: 113234 total, 112044 passed (pass rate: 98.9%), sqlglot version: 0.0.1.dev1

sqlglot:fix/qualify-tables-typed-alias-columns: 101035 total, 101035 passed (pass rate: 100.0%), sqlglot version: fix/qualify-tables-typed-alias-columns

Transitions:
No change

Dialect pair changes: 0 previous results not found, 3 current results not found

✅ 32 test(s) passed

@georgesittas georgesittas merged commit 0bd0c46 into main Apr 22, 2026
8 checks passed
@georgesittas georgesittas deleted the fix/qualify-tables-typed-alias-columns branch April 22, 2026 14:52
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.

2 participants