Skip to content

Resolve ORDER BY items in one step for plain, grouped and set-operation queries #2693

Open
0 of 1 issue completed

Description

@mathiasrw

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 into self.columns[n-1], or {_useColumnIndex} for SELECT *, and a quoted string into a column. It throws Invalid column number ... and You are trying to order by column number ....
  • compileSelect2 (src/select/columns.js) decides per item for a plain query. With SELECT *, a position reads Object.keys(r)[n] for each row.
  • compileSelectGroup2 has three more maps for a grouped query.
  • queryfn3 (src/select/queryfn.js) reads r[columnid] for set operations and undefined for 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 b

This also fixes #3147.

What to change

  1. 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'

    order applies 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.xcolumns decides 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}.
  2. Expand * once per run. When every source declares its columns (the scope from bind.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.

  3. 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, compileSelectGroup2 and the set-operation path. {output} reads r[name], {star} reads r[names[n]], and {expression} compiles over the source row (plain), the group (grouped) or the output row with its names as columns (set operations).

  4. For a set operation, resolve against the output columns of the first SELECT.

  5. Remove _useColumnIndex, the three maps in compileSelectGroup2, the item handling in compileSelect2, the column check in the set-operation helper, self.orderColumns and query.orderColumns.

What stays the same

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.

Depends on

#3165

Sub-issues

Sub-issues

0 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. mathiasrw commented on Oct 20, 2026

    @mathiasrw
    MemberAuthor

    Part of the ORDER BY design in #2720.

  2. added this to the v4.x Focus milestone on Nov 26, 2026
  3. changed the title [-]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[/+] on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions