Skip to content

COUNT and AVG count NULL rows when the argument is -x, +x or ABS(x) #2767

Open

Description

@mathiasrw

Inside an aggregate, -x, +x and ABS(x) give a value for a row where x is NULL, so COUNT and AVG count that row. In the select list the same expressions give no value. Seen in the sqllogictest run (test/random/expr/, for example COUNT ( - CASE 1 WHEN 2 THEN 3 END )), reproduced on develop (06211a0) and npm 4.20.1, compared with SQLite 3.43.2:

alasql('CREATE TABLE u (x)');
alasql('INSERT INTO u VALUES (1), (NULL), (3)');
Query AlaSQL SQLite
SELECT COUNT(x) FROM u 2 2
SELECT COUNT(-x) FROM u 3 2
SELECT COUNT(+x) FROM u 3 2
SELECT COUNT(ABS(x)) FROM u 3 2
SELECT COUNT(DISTINCT -x) FROM u 3 2
SELECT AVG(-x) FROM u -1.333 -2
SELECT AVG(ABS(x)) FROM u 1.333 2
SELECT COUNT(-NULL) 1 0
SELECT COUNT(x * 2) FROM u, COUNT(x + 1) 2 2
SELECT -x AS v FROM u WHERE x IS NULL [{v: undefined}] NULL

SUM, MIN and MAX give the right value, because the counted row adds nothing to them. breaking.md lists ABS(null) is 0 under #2588; this is where that shows in aggregates.

Breaking?

Yes in the sense of the NULL rule: the counts and averages change, and NULL changes wait for 5.0 (#2588). Few would rely on COUNT(-x) counting NULL rows, but AVG(ABS(x)) over a column with NULLs is plausible in real queries. Related: #2481.

Activity

  1. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I'd like to work on this one.

  2. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I've opened a PR for this: #2993

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