Skip to content

fix: preserve aliases on cast columns and fix star selection in sqlglot (#17394)#17455

Merged
shuoweil merged 5 commits into
mainfrom
shuowei-fix-cast-alias
Jun 12, 2026
Merged

fix: preserve aliases on cast columns and fix star selection in sqlglot (#17394)#17455
shuoweil merged 5 commits into
mainfrom
shuowei-fix-cast-alias

Conversation

@shuoweil

@shuoweil shuoweil commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

** This branch is under testing, not ready for review **

This PR resolves a regression introduced when switching to the default sqlglot compiler, where cast columns lost their aliases during type-coercion and were auto-named by BigQuery as f0_, f1_, etc. (fixes #17394).

Before: screen/7FibgBYoY6EN8hR
After: screen/AWsDt8aocqyzjup

Fixes #<521420846> 🦕

@shuoweil shuoweil self-assigned this Jun 12, 2026
@shuoweil shuoweil requested review from a team as code owners June 12, 2026 21:17
@shuoweil shuoweil requested review from tswast and removed request for a team and tswast June 12, 2026 21:17
@shuoweil shuoweil marked this pull request as draft June 12, 2026 21:17

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an issue where CAST columns lose their aliases during SQL compilation by updating the alias preservation logic in sqlglot_ir.py and refining the is_star_selection check in sql_nodes.py. It also updates pre-commit configurations to exclude snapshot files. The feedback highlights a potential issue in sqlglot_ir.py where nested aliases (e.g., (expression AS old_alias) AS new_alias) could be generated if an expression already has a different alias, which is invalid in BigQuery. A code suggestion is provided to unwrap the inner expression when creating a new alias.

Comment on lines +256 to +259
if not (
(isinstance(expr, sge.Column) and expr.name == id)
or (isinstance(expr, sge.Alias) and expr.alias == id)
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If expr is already an Alias but has a different alias (i.e., expr.alias != id), wrapping it directly in another Alias (via this=expr on line 253) will produce nested aliases like (expression AS old_alias) AS new_alias. This is invalid SQL in BigQuery and will cause compilation/execution failures.

To prevent nested aliases, we should unwrap the inner expression (expr.this) when creating the new Alias. For example:

[
    expr
    if (isinstance(expr, sge.Alias) and expr.alias == id)
    or (isinstance(expr, sge.Column) and expr.name == id)
    else sge.Alias(
        this=expr.this if isinstance(expr, sge.Alias) else expr,
        alias=sql.identifier(id),
    )
    for id, expr in selections
]

@shuoweil shuoweil force-pushed the shuowei-fix-cast-alias branch from 8b373db to 3d92d4d Compare June 12, 2026 21:30
@shuoweil shuoweil changed the title fix preserve aliases on cast columns and fix star selection in sqlglot (#17394) fix: preserve aliases on cast columns and fix star selection in sqlglot (#17394) Jun 12, 2026
@shuoweil shuoweil marked this pull request as ready for review June 12, 2026 21:37
@shuoweil shuoweil requested a review from TrevorBergeron June 12, 2026 21:37
@shuoweil shuoweil merged commit 145034a into main Jun 12, 2026
32 checks passed
@shuoweil shuoweil deleted the shuowei-fix-cast-alias branch June 12, 2026 23:06
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.

BigFrames 2.40.0: CAST columns lose aliases (named f0_) under the default sqlglot compiler

2 participants