fix: preserve aliases on cast columns and fix star selection in sqlglot (#17394)#17455
Conversation
There was a problem hiding this comment.
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.
| if not ( | ||
| (isinstance(expr, sge.Column) and expr.name == id) | ||
| or (isinstance(expr, sge.Alias) and expr.alias == id) | ||
| ) |
There was a problem hiding this comment.
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
]8b373db to
3d92d4d
Compare
** This branch is under testing, not ready for review **
This PR resolves a regression introduced when switching to the default
sqlglotcompiler, where cast columns lost their aliases during type-coercion and were auto-named by BigQuery asf0_,f1_, etc. (fixes #17394).Before: screen/7FibgBYoY6EN8hR
After: screen/AWsDt8aocqyzjup
Fixes #<521420846> 🦕