Skip to content

Fix ORDER BY with named aggregates that expand to subqueries#34

Merged
nicosuave merged 8 commits into
mainfrom
nicosuave/fix-orderby-subquery
May 14, 2026
Merged

Fix ORDER BY with named aggregates that expand to subqueries#34
nicosuave merged 8 commits into
mainfrom
nicosuave/fix-orderby-subquery

Conversation

@nicosuave
Copy link
Copy Markdown
Member

@nicosuave nicosuave commented Apr 10, 2026

Fixes #28.

When an expanded AGGREGATE ... AT SELECT alias contains a scalar subquery, DuckDB can order by the alias directly but fails when that alias is referenced inside a complex ORDER BY expression such as revenue / year_total.

This rewrites those complex ORDER BY expressions through the DuckDB parser by inlining SELECT aliases only when the expression references a subquery-backed alias. Simple ORDER BY year_total and qualified tie-breakers stay in the original query scope.

Tests added for the original ratio sort, qualified tie-breakers, and simple alias ordering.

Loading
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.

order by named aggregates results in an error

1 participant