Commit 95157ef
Unparser drops ORDER BY alias when flattening Projection through SubqueryAlias (#21491)
## Which issue does this PR close?
- Closes #21490.
## Rationale for this change
When `plan_to_sql` encounters a `Projection → Sort → Projection →
SubqueryAlias` plan where the outer Projection excludes a Sort
column defined as an alias in the inner Projection (e.g. `Z AS c`), the
function `rewrite_plan_for_sort_on_non_projected_fields`
flattens the two Projections into one but only keeps the outer
Projection's columns. This drops the alias definition, leaving
`ORDER BY c` referencing a column that no longer exists in the generated
SQL.
## What changes are included in this PR?
In `rewrite_plan_for_sort_on_non_projected_fields`
(`datafusion/sql/src/unparser/rewrite.rs`), when the inner Projection is
trimmed to only the outer Projection's expressions, sort expressions
that reference dropped aliases are now inlined to the
underlying physical expression. For example, `ORDER BY c` becomes `ORDER
BY t."Z"` when `c` was defined as `Z AS c` in the
dropped inner Projection.
## Are these changes tested?
Yes. A new regression test
`test_sort_on_aliased_column_dropped_by_outer_projection` in
`datafusion/sql/tests/cases/plan_to_sql.rs` constructs the exact plan
shape that triggers the bug and asserts the correct SQL
output:
```sql
SELECT t."X" AS a, t."Y" AS b FROM phys_table AS t ORDER BY t."Z" DESC
NULLS FIRST LIMIT 1
```
All existing `plan_to_sql` tests (118 tests) continue to pass.
## Are there any user-facing changes?
No API changes. Previously invalid SQL is now generated correctly.
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 643db7a commit 95157ef
2 files changed
Lines changed: 91 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
254 | 254 | | |
255 | 255 | | |
256 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
257 | 299 | | |
258 | 300 | | |
259 | 301 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3008 | 3008 | | |
3009 | 3009 | | |
3010 | 3010 | | |
| 3011 | + | |
| 3012 | + | |
| 3013 | + | |
| 3014 | + | |
| 3015 | + | |
| 3016 | + | |
| 3017 | + | |
| 3018 | + | |
| 3019 | + | |
| 3020 | + | |
| 3021 | + | |
| 3022 | + | |
| 3023 | + | |
| 3024 | + | |
| 3025 | + | |
| 3026 | + | |
| 3027 | + | |
| 3028 | + | |
| 3029 | + | |
| 3030 | + | |
| 3031 | + | |
| 3032 | + | |
| 3033 | + | |
| 3034 | + | |
| 3035 | + | |
| 3036 | + | |
| 3037 | + | |
| 3038 | + | |
| 3039 | + | |
| 3040 | + | |
| 3041 | + | |
| 3042 | + | |
| 3043 | + | |
| 3044 | + | |
| 3045 | + | |
| 3046 | + | |
| 3047 | + | |
| 3048 | + | |
| 3049 | + | |
| 3050 | + | |
| 3051 | + | |
| 3052 | + | |
| 3053 | + | |
| 3054 | + | |
| 3055 | + | |
| 3056 | + | |
| 3057 | + | |
| 3058 | + | |
| 3059 | + | |
3011 | 3060 | | |
3012 | 3061 | | |
3013 | 3062 | | |
| |||
0 commit comments