Repository navigation
Description
INSERT INTO t VALUES (NULL) without a column list stores NaN in an INT, FLOAT, NUMBER or MONEY column. A null parameter is stored as 0. In SQL the stored value must be NULL.
The stored value is then not NULL. IS NULL is false, COUNT counts it, and CAST(b AS INTEGER) returns 0.
The CAST/CONVERT part of this issue is fixed by #2576. What is left is below.
1. INSERT stores NULL as NaN or 0
alasql('CREATE TABLE t (a INT, b INT)');
alasql('INSERT INTO t VALUES (1, NULL), (2, 3)');
alasql('SELECT a, CAST(b AS INTEGER) AS c FROM t'); // [{a:1, c:0}, {a:2, c:3}] expected c: undefined
alasql('SELECT COUNT(b) AS n FROM t'); // [{n:2}] expected 1
alasql('SELECT a FROM t WHERE b IS NULL'); // [] expected [{a:1}]
alasql.tables.t.data[0].b; // NaNSELECT * hides the fault, because JSON.stringify(NaN) gives null.
| Column type | VALUES (NULL) |
VALUES (?) with [null] |
With column list (b) |
|---|---|---|---|
INT, FLOAT, NUMBER, MONEY |
NaN |
0 |
NULL (correct) |
All other types, for example INTEGER, DECIMAL, REAL, STRING, DATE |
NULL (correct) | NULL (correct) | NULL (correct) |
UPDATE t SET b = NULL stores NULL correctly.
Cause: src/70insert.js has two code paths. The path with a column list guards NULL: (x=value, x==undefined?undefined:+x) (line 141). The path without a column list does not: '+' + value (line 171).
Direction: use the same guard in the path without a column list.
Tests: the table above, for a literal and for a parameter.
sqllogictest impact: small. The corpus has 10 INSERT lines without a column list that contain NULL, and none of them is in a file with FLOAT columns.
2. A null parameter comes back as undefined
CAST(? AS INT) with [null] returns undefined, not null. The v4 direction was to return the input unchanged. From v5 every NULL is null (#2588), so this only matters if 4.x must keep null.
3. Missing tests
test/test1208.js (#2576) tests CAST(NULL AS <type>) as a literal, and NULLs from JS data (FROM ?). These are not tested:
CAST(? AS <type>)with[null], for each type.CASTof a NULL stored in a table column. This test would have found item 1.
Related: #2588 (null instead of undefined), #2597 (three-valued logic), #2594 (sqllogictest numbers), #1208.
Activity
mathiasrw commentedon Oct 14, 2026
Note: PR #2576 (opened the same day) implements the fix for this, for issue #1208. It returns
undefined; from v5 that becomesnull(#2588). The sqllogictest numbers are in #2594.[-]CAST(NULL AS INTEGER) returns 0, not NULL (and other types return false, 'undefined' or a 1970 date)[/-][+]INSERT without a column list stores NULL as NaN or 0 in INT, FLOAT, NUMBER and MONEY columns[/+]mathiasrw commentedon Oct 21, 2026
Another effect, seen in the NIST SQL Test Suite run: without a column list, a NULL in a numeric
NOT NULLcolumn passes theNOT NULLcheck, because the value is no longer NULL when it is checked. With a column list, or in aCHARcolumn, theINSERTfails as it should. Reproduced ondevelop(38bf3c3):Expected: the first two
INSERTs fail like the last two.