Repository navigation
Description
What happens today
What an ORDER BY item refers to (an output column, a group value, or an expression over the source row) is decided in five places, each with part of the rules.
compileOrder(src/select/orderby.js) turns a number intoself.columns[n-1], or{_useColumnIndex}forSELECT *, and a quoted string into a column. It throwsInvalid column number ...andYou are trying to order by column number ....compileSelect2(src/select/columns.js) decides per item for a plain query. WithSELECT *, a position readsObject.keys(r)[n]for each row.compileSelectGroup2has three more maps for a grouped query.queryfn3(src/select/queryfn.js) readsr[columnid]for set operations andundefinedfor anything else.- For a set operation the ORDER BY is compiled on the last branch (
src/select/select.js), so a position takes the last SELECT's column while the rows carry the first SELECT's names.
alasql('SELECT * FROM ? ORDER BY 1', [[{a: 2, b: 1}, {b: 5, a: 1}, {a: 3, b: 0}]]);
// [{a: 2, b: 1}, {a: 3, b: 0}, {b: 5, a: 1}], the last row was sorted by bThis also fixes #3147.
What to change
-
In
src/engine/bind.js, add the output columns of a SELECT and the resolution of an ORDER BY item against them.// [{name, node}] in select-list order; a * entry is {star: true, tableid} bind.outputs(select, query) // {output: name} | {star: position} | {expression: node} bind.order(item, outputs, place) // place: 'plain' | 'grouped' | 'set'
orderapplies today's rules in today's order.- A number below 1 or past an explicit list throws today's error texts. A number on a
*entry gives{star}. - A quoted string is a name. A parameter whose value names an output column is that column.
- A bare or qualified column that names an output column is that column, as
query.xcolumnsdecides today. - In a grouped query, a projected grouped column resolves to the name it is projected under, and a grouped column that is not projected to its group value.
- Anything else is
{expression}.
- A number below 1 or past an explicit list throws today's error texts. A number on a
-
Expand
*once per run. When every source declares its columns (the scope frombind.scope), the names are known at compile time. Otherwise take them from the first result row of the run and use them for every row. -
Build the key function of Keep ORDER BY sort keys beside the rows instead of inside them #3165 from the resolved items in one helper, used by
compileSelect2,compileSelectGroup2and the set-operation path.{output}readsr[name],{star}readsr[names[n]], and{expression}compiles over the source row (plain), the group (grouped) or the output row with its names as columns (set operations). -
For a set operation, resolve against the output columns of the first SELECT.
-
Remove
_useColumnIndex, the three maps incompileSelectGroup2, the item handling incompileSelect2, the column check in the set-operation helper,self.orderColumnsandquery.orderColumns.
What stays the same
- Every result that works today, for names, aliases, positions, quoted names, parameters and expressions in plain and grouped queries, and the two error texts.
ORDER BY LOWER(c)afterGROUP BY LOWER(c)keeps today's result. That is Match GROUP BY keys by structure and keep each group's state in slots #2647.- A name that is not an output column of a set operation still has no effect.
- Results change only for
SELECT *with a position over rows with different key orders, and for ORDER BY of a set operation is ignored for some positions and for expressions over output columns #3147.
Done when
- A new test file named after this issue covers each item kind (name, alias, position, quoted name, parameter, expression) in each query kind (plain, grouped, set operation,
SELECT *,SELECT *over rows with different key orders), with ASC and DESC. - The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Fixes
Fixes #3147.
Activity
mathiasrw commentedon Oct 20, 2026
Part of the ORDER BY design in #2720.
[-]Review #2189 for code improvements: ORDER BY position with SELECT * is resolved per row[/-][+]Resolve ORDER BY items in one step for plain, grouped and set-operation queries[/+]