Repository navigation
Description
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
Dateobjects cost 100 ns at every level, becausevalueOf()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
INTorSTRING, the generator can emit the plain operator at any level. - Where a total order is needed.
MIN,MAX,ORDER BYand=need the helper to be correct at all.<and>in aWHEREare 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, hascompare-standard.enginehascompare-fast: v4's speed and v4's operators. Either can add the other level in one line of setup. - The tiers between
engineandfullare cut after the ES6 conversion, based on file size and developer experience. - The same query can answer differently in
engineand infullwhen the data has dates, NULLs or mixed types in a comparison. The docs must say this where the tiers are described.
Work
- 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). - The code generator asks the comparison module for source text at
=,<,>,IN, join keys,MIN/MAX(scalar, aggregate and window),ORDER BYandGROUP BY. - The three levels as behaviour modules.
- 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.
Activity
[-]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)[/+]