You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ORDER BY on a column of numbers and text gives an order that depends on the runtime and on earlier statements #2812
ORDER BY on a column that holds both numbers and text gives an order that depends on the JavaScript runtime and on the statements that ran before it. The same query on the same data can come back in different orders.
CREATETABLEo (id INTEGER, mix);
INSERT INTO o VALUES (1, 10), (2, '9'), (3, 'a'), (4, NULL), (5, 2.5), (6, 'B');
INSERT INTO o (id) VALUES (7);
CREATETABLEo2 (id INTEGER, mix);
INSERT INTO o2 VALUES (8, 'c'), (9, 1);
SELECT id, mix AS k FROM o UNIONSELECT id, mix FROM o2 ORDER BY k DESC, id;
Measured on develop (38bf3c3), ids in the order returned:
Run
Order of id
Node 22, the query alone, 200 times
3,6,1,2,5,9,8,4,7 every time
Node 22, after the 692 dialect cases before it (test/dialect/), 12 processes
3,6,1,2,5,9,8,4,7 every time
Bun 1.4.3, after the same 692 cases, 12 processes
1,9,8,3,6,2,5,4,7 every time
Bun 1.4.3, the same cases under a recorder that hooks alasql.dexec (a Bun build of the same source), 12 processes
1,9,8,3,6,2,5,4,7 in 6 runs, 3,6,1,2,5,9,8,4,7 in 6 runs
SQLite 3.43
8,3,6,2,1,5,9,4,7
The same happens with ORDER BY 2 DESC, id (4 and 8 of 12 runs under the recorder). In both alasql orders, text values come both before and after numbers.
Cause
The comparator compares two values with JavaScript > and == (src/426orderby.js). For a number and a text that is not a number, both 10 > 'a' and 'a' > 10 are false, so the comparator says "less than" in both directions. A sort with such a comparator has no defined result: it depends on the order of the input rows and on the sort algorithm of the runtime. Read from the source, not traced.
Direction
Give the comparator one consistent order across types, so that the result is the same everywhere. SQLite orders NULL, then numbers, then text, then blobs. The full type order for the default is part of #2595 (one comparison module).
Today's order is not something anyone can rely on, because it changes with the runtime and with earlier statements. So making it consistent is not a breaking change. Which consistent order the default should use in the long run is #2595.
Found while building the parity check for the ES6 conversion: these two statements were the only ones in 76,999 that did not repeat between two runs.
Activity
mateuscmtropical commentedon Nov 11, 2026
Hi, I'd like to work on this one.
mateuscmtropical commentedon Nov 11, 2026
Hi, I've opened a PR for this: #3050