Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review the behavior of count with multiple arguments #11303

Closed
jonahgao opened this issue Jul 6, 2024 · 3 comments · Fixed by #11391
Closed

Review the behavior of count with multiple arguments #11303

jonahgao opened this issue Jul 6, 2024 · 3 comments · Fixed by #11391
Assignees
Labels
enhancement New feature or request

Comments

@jonahgao
Copy link
Member

jonahgao commented Jul 6, 2024

Is your feature request related to a problem or challenge?

DataFusion supports this type of syntax, such as count(a, b) and count(distinct a, b).
However, its behavior may not be well-defined. A counterintuitive behavior is that count(distinct a, b) returns more rows than count(a, b).

DataFusion CLI v39.0.0
> create table t(a int, b int) as values(1, NULL), (NULL, 10), (NULL, NULL);

> select count(distinct a,b) from t;
+-------------------------+
| count(DISTINCT t.a,t.b) |
+-------------------------+
| 1                       |
+-------------------------+

> select count(a,b) from t;
+----------------+
| count(t.a,t.b) |
+----------------+
| 0              |
+----------------+

Additionally, PostgreSQL does not support this type of syntax. MySQL only supports count(distinct a, b), but its result is different from DataFusion.

Server version: 8.3.0 Homebrew

mysql> create table t(a int, b int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t  values(1, NULL), (NULL, 10), (NULL, NULL);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select count(distinct a,b) from t;
+---------------------+
| count(distinct a,b) |
+---------------------+
|                   0 |
+---------------------+
1 row in set (0.00 sec)

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

No response

@jonahgao jonahgao added the enhancement New feature or request label Jul 6, 2024
@findepi
Copy link
Member

findepi commented Jul 6, 2024

Related to this, we could maybe support zero-arg count().

For 2+ args for count, I as a user would prefer to used filtered aggregation. It's then obvious whether I am counting rows when all args are not null, or when at least one is not null:

count() FILTER (WHERE a IS NOT NULL ( AND | OR ) b IS NOT NULL)

(definitely more verbose than count(a, b), but is count(a, b) established & consistent across query engines?)

@jayzhan211
Copy link
Contributor

Related to this, we could maybe support zero-arg count().

For 2+ args for count, I as a user would prefer to used filtered aggregation. It's then obvious whether I am counting rows when all args are not null, or when at least one is not null:

count() FILTER (WHERE a IS NOT NULL ( AND | OR ) b IS NOT NULL)

(definitely more verbose than count(a, b), but is count(a, b) established & consistent across query engines?)

count() is WIP #11229 , it is equivalent to count(*) and count(1)

count(a, b) established & consistent across query engines

I don't think so, therefore I prefer not to support it and returns error instead.

@jonahgao
Copy link
Member Author

jonahgao commented Jul 7, 2024

This feature was introduced by #5908. Spark also supports it, and its behavior seems to be consistent with MySQL. So I think we can follow Spark.

count(expr[, expr...]) - Returns the number of rows for which the supplied expression(s) are all non-null.
count(DISTINCT expr[, expr...]) - Returns the number of rows for which the supplied expression(s) are unique and non-null.

We need to correct count(distinct a, b).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants