Skip to content

INSERT without a column list stores NULL as NaN or 0 in INT, FLOAT, NUMBER and MONEY columns #2590

Open

Description

@mathiasrw

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;                          // NaN

SELECT * 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.
  • CAST of 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

  1. added this to the SQL logic test milestone on Oct 14, 2026
  2. mathiasrw commented on Oct 14, 2026

    @mathiasrw
    MemberAuthor

    Note: PR #2576 (opened the same day) implements the fix for this, for issue #1208. It returns undefined; from v5 that becomes null (#2588). The sqllogictest numbers are in #2594.

  3. changed the title [-]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[/+] on Oct 19, 2026
  4. mathiasrw commented on Oct 21, 2026

    @mathiasrw
    MemberAuthor

    Another effect, seen in the NIST SQL Test Suite run: without a column list, a NULL in a numeric NOT NULL column passes the NOT NULL check, because the value is no longer NULL when it is checked. With a column list, or in a CHAR column, the INSERT fails as it should. Reproduced on develop (38bf3c3):

    alasql('CREATE TABLE nn (a INT NOT NULL, b CHAR(2) NOT NULL, c FLOAT NOT NULL)');
    alasql("INSERT INTO nn VALUES (NULL, 'x', 1)");       // 1
    alasql("INSERT INTO nn VALUES (1, 'x', NULL)");       // 1
    alasql("INSERT INTO nn VALUES (1, NULL, 1)");         // Wrong NULL value in NOT NULL column b
    alasql("INSERT INTO nn (a, b, c) VALUES (NULL, 'x', 1)");   // Wrong NULL value in NOT NULL column a

    Expected: the first two INSERTs fail like the last two.

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