Skip to content

AVG and SUM read text as a number in the sqlite dialect: '12' is 12, 'abc' is 0 #2774

Open

Description

@mathiasrw

In the sqlite dialect, AVG and SUM should read text as a number, as SQLite does: text that starts with a number counts as that number ('12abc' is 12), other text counts as 0. The alasql dialect keeps ignoring text.

Split from #2763, which fixes the wrong result of AVG over text in 4.x.

alasql('CREATE TABLE t1 (x INTEGER, y VARCHAR(8))');
alasql("INSERT INTO t1 VALUES (1, 'true'), (0, 'false'), (NULL, 'NULL')");
alasql('SELECT AVG(y) AS a, SUM(y) AS s FROM t1');
// alasql dialect: no value (null from 5.0, #2588)
// sqlite dialect: a = 0, s = 0, as SQLite

alasql('SELECT SUM(y) AS s, AVG(y) AS a FROM ?', [[{y: '12'}, {y: 3}]]);
// alasql dialect: s = 3, a = 3 (after the 4.x fix in #2763)
// sqlite dialect: s = 15, a = 7.5, as SQLite

Why not in the alasql dialect

Engines disagree: SQLite and MySQL convert text to a number, PostgreSQL rejects SUM over text. Where engines disagree and v4 is consistent, the alasql dialect keeps v4 (context/strategic-direction.md). v4 consistently ignores values that are not numbers in SUM.

The sqlite dialect may move closer to SQLite in a minor version, so this is not a breaking change.

Related: #2763, #2588, #1605.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions