Repository navigation
Description
What happens today
Each operation has its own rule for when two values are equal and which comes first. This step moves three of them into one module.
- The ORDER BY comparator is text generated per item by
compileOrderinsrc/select/orderby.js. It reads(a['$$$0']||'')with.valueOf(), compares with>or<by direction and==for a tie, tests NULLS FIRST and LAST with== null, and applies COLLATE NOCASE astoUpperCase()on strings. - DISTINCT (
doDistinctinsrc/select/queryfn.js) builds a text key from the values of the first row's keys, objects throughJSON.stringify, joined with a backtick, and stores it in a plain object. - UNION, EXCEPT and INTERSECT use
arrayUnionDeep,arrayExceptDeep,arrayIntersectDeepanddeepEqualinsrc/utils/helpers.js, called fromqueryfn3.
What to change
-
Add
src/engine/compare.jswithinstall(alasql, internal)that setsinternal.compare. Install it insrc/index.jsbeforequeryfnandorderby.internal.compare = { v4: { // ORDER BY. items: [{direction, nullsOrder, nocase, valueOf}]. // key(side, i) returns the JS text that reads key i of row 'a' or 'b'. // Returns the body of a function (a, b), exactly the text compileOrder builds today. orderJS(items, key) {}, // DISTINCT. The text key of a row from the given column names. distinctKey(row, keys) {}, // UNION, EXCEPT, INTERSECT. Today's deepEqual. sameRow(a, b) {}, union(a, b) {}, except(a, b) {}, intersect(a, b) {}, }, };
-
compileOrderkeeps deciding what each item is and whether.valueOf()applies (thealasql.options.valueofandquery.xcolumnschecks). It passes plain item descriptions toorderJSwithkey = (side, i) => side + "['$$$" + i + "']"and wraps the result innew Function('a,b', ...). The comparator stays generated text, becausesrc/precompile/index.jsserialisesquery.orderfnwithtoString(). -
doDistinctbuilds each key withcompare.v4.distinctKey(row, keys). It keeps reading the column names from the first row and keeps the plain object. -
queryfn3callscompare.v4.union,exceptandintersect.alasql.utils.arrayUnionDeep,arrayExceptDeep,arrayIntersectDeepanddeepEqualstay exported.
All other comparison sites stay as they are, including the function form of ORDER BY (SEARCH ... ORDER BY()), Op.toJS, GROUP BY keys, aggregates, window functions and the recursive CTE.
What stays the same
- Every result. This includes NULL between
-1and1on numbers, NULL first ascending on text,0,'',falseand NULL sorting as the same value, the order of mixed numbers and text, COLLATE NOCASE leaving numbers alone, the DISTINCT row order, the row DISTINCT keeps (the last of each key), and which rows the set operations treat as equal. - The three rules stay different from each other. DISTINCT merges
1and'1'and UNION does not. UNION merges two different dates and DISTINCT does not. - DISTINCT keeps a plain object for its keys, so integer values come out in ascending order.
Done when
- A new test file named after this issue checks each v4 rule on a value matrix of numbers, text,
'',0,false,NaN, NULL,undefined, dates, objects, values with a backtick, and mixed columns. For ORDER BY it covers ASC, DESC, NULLS FIRST, NULLS LAST, COLLATE NOCASE and two items. Record the expected results on main before the change. bun test/performance/bench.js --group=sortand--group=set-opsshow 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.
Depends on
Nothing, can start now.
Activity