Repository navigation
Backlog
0 / 10 of 1 issue completedBacklog
0 / 10 of 1 issue completed
Copy link
Labels
Milestone
Description
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) appendsr['$$$0']=...to the row function of a plain query and pushes the key name toquery.removeKeys.compileSelectGroup2does 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 getsundefined.- The comparator from
compileOrder(src/select/orderby.js) readsa['$$$0']. After the sort the keys are deleted with the otherremoveKeys.
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.
compileSelect2andcompileSelectGroup2build a second function from exactly the expressions they append today,query.orderkeysfn = new Function('p,r,params,alasql', 'return [k0,k1,...]')(for grouped queriesg,r,params,alasql). Eachr['$$$i']=Xbecomes elementi. The per-rowObject.keys(r)forSELECT * ... ORDER BY 1stays, Resolve ORDER BY items in one step for plain, grouped and set-operation queries #2693 removes it.- Where a row is pushed, push its keys too with
query.orderkeys.push(query.orderkeysfn(scope, r, query.params, alasql)). That isdoJoin(src/select/dojoin.js), the sorted join path inqueryfn3and the group loop. Createquery.orderkeys = []next toquery.data = [], on the run, not on the compiled query. - For set operations, replace both blocks in
queryfn3with one helper that fillsquery.orderkeysfrom the output rows with today's rule (a column readsr[columnid], anything else isundefined). - Keep keys and rows aligned wherever rows are dropped or reordered before the sort.
doDistinctkeeps the keys of the row it keeps (the last of each key), and the earlydoLimitfor the first SELECT slices both. - 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 tor. - Stop pushing
$$$itoremoveKeys. Leave the other entries (aggregate names,_SUM_,$$distinctOn, REMOVE COLUMNS) alone. compileToJSinsrc/precompile/index.jscalls the key function and sorts the same way.
What stays the same
- Every result, row order and column list, including NULL placement, COLLATE NOCASE, the order of mixed values,
SELECT *with a position, and the first SELECT's own ORDER BY and LIMIT in a set operation. - Results change only for SELECT DISTINCT and ORDER BY not working #756 and A source column named $$$0 is dropped from the result when the query has ORDER BY #3146. List in the pull request any statement that stops throwing.
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=sortshows no slowdown beyond the noise described intest/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
Depends on
#3163, and #3162 if it is merged first (the keys then sit beside its tuples the same way).
Sub-issues
Collapse Sub-issuesSub-issues
Loading 1 sub-issues
Activity