fix(unparser): inline computed projection aliases in ORDER BY expressions - #177
Merged
Conversation
…ions unproject_sort_expr only re-inlined ScalarFunction projection expressions when unprojecting sort keys. Any other computed expression (e.g. a BinaryExpr like grouping(a) + grouping(b)) fell through, leaving a bare SELECT-list alias inside the ORDER BY. When that alias appears inside a larger expression (e.g. CASE WHEN), standard-conforming engines resolve identifiers against the FROM relations only and reject it (PostgreSQL: column "..." does not exist, SQLSTATE 42703). Generalize the re-inlining to any non-trivial projection expression (excluding plain Column renames, AggregateFunction and WindowFunction), with best-effort aggregate-column resolution for the inlined expression. Adds a regression test covering a computed alias referenced inside an ORDER BY CASE WHEN.
peasee
approved these changes
Jul 1, 2026
There was a problem hiding this comment.
Pull request overview
This PR fixes SQL unparsing for ORDER BY expressions that reference SELECT-list aliases inside larger expressions (e.g. CASE WHEN ...), by re-inlining the underlying projection expression instead of emitting a bare alias identifier that standard-conforming SQL engines resolve only against FROM.
Changes:
- Generalize
unproject_sort_exprto re-inline non-trivial projection expressions (not justScalarFunction) when referenced viaExpr::ColumninORDER BY. - Add a regression test covering a computed (
BinaryExpr) projection alias used inside anORDER BY CASE WHENexpression. - Minor comment whitespace cleanup in
unproject_unnest_expr.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| datafusion/sql/src/unparser/utils.rs | Generalizes ORDER BY alias re-inlining to handle any non-trivial projection expression (with best-effort aggregate expression resolution). |
| datafusion/sql/tests/cases/plan_to_sql.rs | Adds a regression test ensuring computed projection aliases are inlined when referenced inside ORDER BY expressions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Jeadie
added a commit
to spiceai/spiceai
that referenced
this pull request
Jul 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
During Datafusion unparsing, only
ScalarFunctionprojections are reinlined forORDER BYexpression (prior work by @phillipleblanc; upstreamed in apache#16127) . Any other computed expression — e.g. aBinaryExprsuch asgrouping(a) + grouping(b)— fell through, leaving a bare SELECT-list alias inside theORDER BY.A bare alias is a valid top-level sort key, but when it appears inside a larger expression (e.g.
CASE WHEN), standard-conforming engines resolve identifiers against theFROMrelations only and reject it:ERROR: column "computed" does not exist(SQLSTATE 42703)Binder Error: Referenced column "computed" not foundERROR 1054 (42S22): Unknown column 'computed' in 'order clause'Before (broken)
After (valid)
Fix
Generalize the re-inlining in
unproject_sort_exprto any non-trivial projection expression (excluding plainColumnrenames,AggregateFunction, andWindowFunction).Why this surfaced in the DataFusion 54 upgrade
Changes to
OptimizeProjectionsin DF54 changed the producedLogicalPlanand caused this issue.DF53: Projection → Sort → Projection → WindowAggr
DF54: Sort → Projection → WindowAggr
If a projection is already open above the
Sort(already_projected() == true), the unparser wraps the whole thing in aderived_sortsubquery:SELECT ... FROM (SELECT ... AS computed ... ORDER BY ...) AS derived_sort. Within this CTE,computedis a real derived-table column.Tests
test_order_by_with_computed_alias_inside_expr. Verified to fail without the fix.