Commit b3d2cb6
Fix ORDER BY positional reference regression with aliased aggregates (#19412)
## Which issue does this PR close?
Closes #19410
## Rationale for this change
This PR fixes a regression introduced in #18831 where queries using
GROUP BY with ORDER BY positional reference to an aliased aggregate fail
with:
```
Error during planning: Column in ORDER BY must be in GROUP BY or an aggregate function
```
**Failing query (now fixed):**
```sql
with t as (select 'foo' as x)
select x, count(*) as "Count"
from t
group by x
order by 2 desc;
```
## What changes are included in this PR?
**Root cause:** When building the list of valid columns for ORDER BY
validation in `select.rs`, alias names were converted to `Column` using
`.into()`, which calls `from_qualified_name()` and normalizes
identifiers to lowercase. However, ORDER BY positional references
resolve to columns using schema field names, which preserve case. This
caused a mismatch (e.g., `Column("Count")` vs `Column("count")`).
**Fix:** Use `Column::new_unqualified()` instead of `.into()` to
preserve the exact case of alias names, matching how the schema stores
field names.
## Are these changes tested?
Yes, added a regression test to `order.slt`.
## Are there any user-facing changes?
No, this is a bug fix that restores expected behavior.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.5 <[email protected]>1 parent 2e3707e commit b3d2cb6
2 files changed
+16
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | | - | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
1054 | 1054 | | |
1055 | 1055 | | |
1056 | 1056 | | |
1057 | | - | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
1058 | 1060 | | |
1059 | 1061 | | |
1060 | 1062 | | |
| |||
1069 | 1071 | | |
1070 | 1072 | | |
1071 | 1073 | | |
1072 | | - | |
| 1074 | + | |
1073 | 1075 | | |
1074 | 1076 | | |
1075 | 1077 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
444 | 444 | | |
445 | 445 | | |
446 | 446 | | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
447 | 458 | | |
448 | 459 | | |
449 | 460 | | |
| |||
0 commit comments