Skip to content

Unary + and - on text follow JavaScript: +'abc' gives NaN, SQLite keeps the text #2776

Open

Description

@mathiasrw

Unary + and - on text follow JavaScript: +c turns the text into a number, so 'abc' gives NaN (no value in the result) and '07' gives 7. In SQLite unary + does nothing and keeps the text, and unary - reads the number at the start of the text, so -'12x' is -12 and -'abc' is 0. Seen about 800 times in the sqllogictest run (test/index/random/, for example SELECT + + col5 FROM tab0 with col5 TEXT), reproduced on develop (06211a0) and npm 4.20.1, compared with SQLite 3.43.2:

alasql('CREATE TABLE t (c TEXT, n INT)');
alasql("INSERT INTO t VALUES ('abc', 1), ('12x', 2), ('07', 3)");
Query AlaSQL SQLite
SELECT c FROM t 'abc', '12x', '07' 'abc', '12x', '07'
SELECT +c FROM t (also + + c) NaN, NaN, 7 'abc', '12x', '07'
SELECT -c FROM t NaN, NaN, -7 0, -12, -7
SELECT +n FROM t 1, 2, 3 1, 2, 3

Breaking?

Yes: +col is the JavaScript way to turn text into a number, and code that reads CSV or JSON text with SELECT +price FROM ? relies on it. It is the same kind of choice as integer division, which stays JavaScript by default with an opt-in integer-division behaviour module; SQLite's unary operators could be such a module too. Related: #2594 (lists unary plus on text among the non-NULL causes).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions