Skip to content

Add one comparison module with today's rules for ORDER BY, DISTINCT and set operations #3163

Backlog

Description

@mathiasrw

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 compileOrder in src/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 as toUpperCase() on strings.
  • DISTINCT (doDistinct in src/select/queryfn.js) builds a text key from the values of the first row's keys, objects through JSON.stringify, joined with a backtick, and stores it in a plain object.
  • UNION, EXCEPT and INTERSECT use arrayUnionDeep, arrayExceptDeep, arrayIntersectDeep and deepEqual in src/utils/helpers.js, called from queryfn3.

What to change

  1. Add src/engine/compare.js with install(alasql, internal) that sets internal.compare. Install it in src/index.js before queryfn and orderby.

    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) {},
      },
    };
  2. compileOrder keeps deciding what each item is and whether .valueOf() applies (the alasql.options.valueof and query.xcolumns checks). It passes plain item descriptions to orderJS with key = (side, i) => side + "['$$$" + i + "']" and wraps the result in new Function('a,b', ...). The comparator stays generated text, because src/precompile/index.js serialises query.orderfn with toString().

  3. doDistinct builds each key with compare.v4.distinctKey(row, keys). It keeps reading the column names from the first row and keeps the plain object.

  4. queryfn3 calls compare.v4.union, except and intersect. alasql.utils.arrayUnionDeep, arrayExceptDeep, arrayIntersectDeep and deepEqual stay 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 -1 and 1 on numbers, NULL first ascending on text, 0, '', false and 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 1 and '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=sort and --group=set-ops show 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.

Depends on

Nothing, can start now.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions