Skip to content

One comparison module with levels (dates compared by value, MAX/MIN on strings, one NULL order) #2595

Open
0 of 2 issues completed

Description

@mathiasrw

alasql has no single definition of "a is less than b". The comparison is written out again, each time a little differently, in:

  • scalar functions,
  • the aggregate code generator,
  • the window code,
  • ORDER BY,
  • =.

Dates, strings in MAX/MIN and NULL ordering all fail because of this. The v5 proposal: one comparison module, used by all of them, in levels chosen when a statement is compiled.

Where it fails today (v4.20.0, measured)

Where What happens Cause
d = ?, JOIN … ON a.d = b.d, d IN (?) with an equal but distinct Date no match = compiles to ===, which compares object identity. <, BETWEEN, ORDER BY, GROUP BY and DISTINCT work.
MAX(d) / MIN(d) over a DATE column undefined DATE columns hold strings by default, and the aggregate accepts only numbers, bigints and number-like objects. Same root as #609 (MAX() on strings, open since 2016).
MAX(d) OVER (PARTITION BY g) a number (1717200000000), not a date; NaN for strings The window path uses Math.max.apply.
ORDER BY with NULLs on a number column NULLs come last in both directions; on a string column they come first ascending and last descending separate comparators

Scalar LEAST/GREATEST on dates already work, because < and > call valueOf(). That is why #2479 patches a place that is not broken.

A sort with an inconsistent comparator has no defined result. One comparison makes ORDER BY a total order. It is also the only preparation Temporal needs: Temporal objects throw a TypeError when compared with <, by design.

Levels, because speed matters

The level is chosen when a statement is compiled, not per row. The code generator asks the comparison module for a piece of JavaScript source, so only the chosen level's code is in the loop.

Level Generated code For
compare-fast a > b, as today Data known to be clean numbers or strings. v4 speed exactly.
compare-standard typeof a === 'number' && typeof b === 'number' ? a > b : alasql.cmp(a, b) > 0 Numbers take the short path inline. Everything else goes to one helper that knows NULL, dates, strings and mixed types.
compare-strict alasql.cmp(a, b) > 0 always A fixed order across types, Temporal, a hook for collation.

Measured in a tight loop shaped like alasql's row code: one million rows, median of nine runs. It is a micro-benchmark, so it is an upper bound on what a real query would show.

fast standard strict
WHERE v > ?, numbers 3.0 ms 9.1 ms 10.9 ms
WHERE v > ?, strings 13.8 ms 24.4 ms 21.5 ms
WHERE v > ?, Date objects 100.6 ms 106.9 ms 105.2 ms
ORDER BY v, 300,000 numbers 63.6 ms 66.6 ms 96.0 ms

Per comparison:

  • The standard level adds about 6 ns on numbers and about 10 ns on strings, and 5% on a numeric sort.
  • Two Date objects cost 100 ns at every level, because valueOf() dominates. That is slow today already, and the helper can do better by comparing time values.

Two more things keep it fast without asking the user anything:

  • Declared types. For a column declared INT or STRING, the generator can emit the plain operator at any level.
  • Where a total order is needed. MIN, MAX, ORDER BY and = need the helper to be correct at all. < and > in a WHERE are correct with the plain operator for numbers, strings and dates, and wrong only for mixed types.

Decided (2026-10-14)

  • The level goes with the build. full, the default import, has compare-standard. engine has compare-fast: v4's speed and v4's operators. Either can add the other level in one line of setup.
  • The tiers between engine and full are cut after the ES6 conversion, based on file size and developer experience.
  • The same query can answer differently in engine and in full when the data has dates, NULLs or mixed types in a comparison. The docs must say this where the tiers are described.

Work

  1. One cmp(a, b) with a defined order: NULL, numbers, strings, dates. The order across types follows SQLite (Dialect test: compare alasql with SQLite expression by expression in CI (the reference engine for v5) #2593).
  2. The code generator asks the comparison module for source text at =, <, >, IN, join keys, MIN/MAX (scalar, aggregate and window), ORDER BY and GROUP BY.
  3. The three levels as behaviour modules.
  4. Confirm with test/performance/ on real queries, where the fixed cost per row is much higher than in the loop above.

Breaking for v5: = on dates matches by value in full; MAX/MIN over strings and dates return a value instead of undefined; one rule for NULLs in ORDER BY.

Related: #2479, #2480, #2481, #609, #2583, #2593.

Sub-issues

Sub-issues

0 of 2 Issues completed
Loading 2 sub-issues

Activity

  1. added this to the Breaking change milestone on Oct 14, 2026
  2. changed the title [-]v5: one comparison module with levels (dates compared by value, MAX/MIN on strings, one NULL order)[/-] [+]One comparison module with levels (dates compared by value, MAX/MIN on strings, one NULL order)[/+] on Oct 21, 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