Skip to content

Keep ORDER BY sort keys beside the rows instead of inside them #3165

Backlog
0 of 1 issue completed
Backlog

Description

@mathiasrw

What happens today

An ORDER BY key is stored on each result row as $$$0, $$$1, ... and deleted again before the rows are returned.

  • compileSelect2 (src/select/columns.js) appends r['$$$0']=... to the row function of a plain query and pushes the key name to query.removeKeys. compileSelectGroup2 does the same for a grouped query.
  • queryfn3 (src/select/queryfn.js) writes the keys again for set operations, in two copies of the same block. An item that is not a column gets undefined.
  • The comparator from compileOrder (src/select/orderby.js) reads a['$$$0']. After the sort the keys are deleted with the other removeKeys.

DISTINCT sees the keys, so SELECT DISTINCT a FROM ? ORDER BY b returns duplicates (#756). This also fixes #3146.

What to change

Compute the keys into an array beside the rows, sort that, and take the rows back out. Nothing is added to a row.

  1. compileSelect2 and compileSelectGroup2 build a second function from exactly the expressions they append today, query.orderkeysfn = new Function('p,r,params,alasql', 'return [k0,k1,...]') (for grouped queries g,r,params,alasql). Each r['$$$i']=X becomes element i. The per-row Object.keys(r) for SELECT * ... ORDER BY 1 stays, Resolve ORDER BY items in one step for plain, grouped and set-operation queries #2693 removes it.
  2. Where a row is pushed, push its keys too with query.orderkeys.push(query.orderkeysfn(scope, r, query.params, alasql)). That is doJoin (src/select/dojoin.js), the sorted join path in queryfn3 and the group loop. Create query.orderkeys = [] next to query.data = [], on the run, not on the compiled query.
  3. For set operations, replace both blocks in queryfn3 with one helper that fills query.orderkeys from the output rows with today's rule (a column reads r[columnid], anything else is undefined).
  4. Keep keys and rows aligned wherever rows are dropped or reordered before the sort. doDistinct keeps the keys of the row it keeps (the last of each key), and the early doLimit for the first SELECT slices both.
  5. Sort with the comparator from Add one comparison module with today's rules for ORDER BY, DISTINCT and set operations #3163, now with key = (side, i) => side + '[' + i + ']'. Sort entries {k, r} in the original order and map back to r.
  6. Stop pushing $$$i to removeKeys. Leave the other entries (aggregate names, _SUM_, $$distinctOn, REMOVE COLUMNS) alone.
  7. compileToJS in src/precompile/index.js calls the key function and sorts the same way.

What stays the same

Done when

  • A new test file named after this issue covers DISTINCT with ORDER BY on a column that is not selected, a column named $$$0, ORDER BY with UNION, EXCEPT and INTERSECT where both SELECTs have their own ORDER BY, a grouped query ordered by an aggregate alias, LIMIT and OFFSET, and a statement run twice with the same compiled query.
  • bun test/performance/bench.js --group=sort shows no slowdown beyond the noise described in test/performance/README.md.
  • 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 #756, #3146.

Depends on

#3163, and #3162 if it is merged first (the keys then sit beside its tuples the same way).

Sub-issues

Sub-issues

0 of 1 Issues completed
Loading 1 sub-issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions