Skip to content

Text stays text in INTEGER, BIGINT, REAL and DOUBLE PRECISION columns and in casts to int4 or float8: '456' + 1 is '4561' #2816

Open

Description

@mathiasrw

Text is turned into a number only for some type names. An INT column stores '456' as the number 456, but INTEGER, BIGINT, INT8, REAL, DOUBLE PRECISION and DECIMAL columns keep the text, so '456' + 1 is '4561' and WHERE y = 456 finds nothing. CAST and :: have their own list: INTEGER, INT, BIGINT and NUMERIC convert, while int2, int4, int8, float4, float8, REAL, DOUBLE PRECISION and bool keep the text.

Seen in the PostgreSQL regression tests run (alasql-pgsqltest): about 50 statements in 9 files (int8.sql, float4.sql, float8.sql, money.sql, ...), plus the rows of INT8_TBL in test_setup.sql (INSERT INTO INT8_TBL VALUES (+4567890123456789, '4567890123456789')). Reproduced on develop (38bf3c3):

alasql('CREATE TABLE a (x INT, y INTEGER, z BIGINT, w DOUBLE PRECISION, v DECIMAL(5,1), r REAL)');
alasql("INSERT INTO a VALUES ('456', '456', '456', '1.5', '2.5', '3.5')");
alasql('SELECT x + 1 AS x, y + 1 AS y, z + 1 AS z, w + 1 AS w, v + 1 AS v, r + 1 AS r FROM a');
// [{x: 457, y: '4561', z: '4561', w: '1.51', v: '2.51', r: '3.51'}]
alasql('SELECT COUNT(*) AS n FROM a WHERE y = 456');   // [{n: 0}]

alasql("SELECT '12'::INTEGER + 1 AS a, '12'::int4 + 1 AS b, '1.5'::float8 + 1 AS c, CAST('1.5' AS REAL) + 1 AS d, 't'::bool AS e");
// [{a: 13, b: '121', c: '1.51', d: '1.51', e: 't'}]

Expected (PostgreSQL): every column stores a number (457, 457, 457, 2.5, 3.5, 4.5), the count is 1, and the casts give 13, 13, 2.5, 2.5 and true.

Breaking?

Yes. Stored values change from text to numbers, and code can compare them with text today (WHERE y = '456').

Related: #2590 (the NULL side of the same type list: only INT, FLOAT, NUMBER and MONEY are converted).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions