Skip to content

Count distinct rows in COUNT(DISTINCT *) to close #2758 - #2994

Merged
mathiasrw merged 3 commits into
AlaSQL:developfrom
mateuscmtropical:fix-2758-count-distinct-star
Nov 16, 2026
Merged

mathiasrw merged 3 commits into
AlaSQL:developfrom
mateuscmtropical:fix-2758-count-distinct-star

Conversation

@mateuscmtropical

@mateuscmtropical mateuscmtropical commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2758

COUNT(DISTINCT *) returned 1 for any number of rows. The distinct check used the value of the column *, which is undefined for every row, so after the first row nothing was counted.

  • src/15utility.js adds alasql.utils.distinctRowKey, the key of a row. It sorts the columns and leaves out NULL and undefined values, so the column order and an omitted column do not matter. A number and a BigInt are keyed by their digits and a Date by its time. NULL in an INT column is stored as NaN, so NaN counts as NULL too.
  • src/423groupby.js uses that key for a DISTINCT aggregate over *. COUNT(DISTINCT t1.*) keys the row of t1, and COUNT(DISTINCT *) the row of all tables. Other DISTINCT aggregates keep their key. COUNT(DISTINCT *) stays accepted, as the issue asks.

test/test2758.js covers a table with a repeated row and a row with NULL, GROUP BY, parameter data, COUNT(DISTINCT x) as before, rows inserted with the columns in another order, an explicit NULL next to an omitted column, t4.*, t5.* and * in a JOIN, and BigInt and Date values. The tests fail on develop and pass with the change. yarn test-only gives 2558 pass. The only failures are the 5 Test 418 cases that load data from the internet, and they fail the same way on develop in my environment.

@mathiasrw

Copy link
Copy Markdown
Member

Thank you for this @mateuscmtropical

Going from "always 1" to a real count is the right direction, but comparing rows with JSON.stringify counts some equal rows twice.

CREATE TABLE t (x INT, y INT);
INSERT INTO t (x, y) VALUES (1, 2);
INSERT INTO t (y, x) VALUES (2, 1);
SELECT COUNT(DISTINCT *) AS n FROM t;  -- gives 2, should be 1

The same happens in three other cases.

  • An explicit NULL and an omitted column count as two rows, for example VALUES (1, NULL) and INSERT INTO t (x) VALUES (1).
  • In a JOIN, COUNT(DISTINCT t1.*) compares the whole joined row and ignores t1.
  • A BigInt value now throws "Do not know how to serialize a BigInt". Before, it returned 1.

Please build the key only from the row of the named table, or from all tables when no table is named. Sort the keys, and treat NULL and undefined the same. Then add a test for each case above.

A general note, as you have many PRs open. We value quality over quantity. One complete PR helps the project more than several that need another round of review. Before you open a PR, please test the edge cases around your change, not only the case in the issue. Check NULL against undefined, column order, JOINs, and values such as Date and BigInt. Then your work gets merged faster, and we spend less time on reviews.

@mateuscmtropical

Copy link
Copy Markdown
Contributor Author

Thanks for the cases. I changed the key in two places:

  • src/15utility.js has alasql.utils.distinctRowKey. It sorts the columns, leaves out NULL and undefined values, keys a number and a BigInt by their digits and a Date by its time. NULL in an INT column is stored as NaN, so NaN counts as NULL too. Your VALUES (1, NULL) example only gives 1 with that.
  • src/423groupby.js builds the key from p['t1'] for COUNT(DISTINCT t1.*) and from all tables for COUNT(DISTINCT *).

test/test2758.js now also covers the column order, an explicit NULL next to an omitted column (from a table and from parameter data), t4.*, t5.* and * in a JOIN, and BigInt and Date values.

Noted on quality over quantity. I'll test these edge cases before opening the next ones.

@mathiasrw
mathiasrw merged commit 3b13872 into AlaSQL:develop Nov 16, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

COUNT(DISTINCT *) is accepted and returns 1 for any number of rows

2 participants