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

Minor: return "not supported" for COUNT DISTINCT with multiple arguments #11391

Merged
merged 3 commits into from
Jul 11, 2024

Conversation

jonahgao
Copy link
Member

Which issue does this PR close?

Closes #11303.

Rationale for this change

COUNT with multiple arguments is a feature of Spark, and DataFusion handles COUNT(a, b, c) well.

But we do not have a DistinctCountAccumulator for multiple arguments.
COUNT(DISTINCT a, b, c) was incorrectly executed as COUNT(DISTINCT a).
Therefore, rather than returning an error result, it is better to make it unsupported.

What changes are included in this PR?

Are these changes tested?

Yes

Are there any user-facing changes?

Yes, although I don't think any users are using this syntax.

@github-actions github-actions bot added the sqllogictest SQL Logic Tests (.slt) label Jul 10, 2024
@@ -138,6 +138,10 @@ impl AggregateUDFImpl for Count {
return Ok(Box::new(CountAccumulator::new()));
}

if acc_args.input_exprs.len() > 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking should it parser issue? Is there any SQL conventions supporting the multiple expressions for COUNT(DISTINCT)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just checked https://chicagobusinesspress.com/download_ancillary?book=22&ancillary=157 for SQL 2016 standard, I can only 1 col supported in syntax. I think we can go with a patch but followup has to be in sqlparser-rs

Copy link
Member Author

@jonahgao jonahgao Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, Spark and MySQL support this syntax, although MySQL states that this feature is not standard SQL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If any of the dialects allow the syntax I think the sqlparser-rs AST will need to support multiple args.

This is kind of explained https://github.com/sqlparser-rs/sqlparser-rs?tab=readme-ov-file#syntax-vs-semantics (where what is a syntax error in some systems and a semantic one in others needs to be enforced as a semantic error)

Thus I think the right thing to do here is check and error in DataFusion, as @jonahgao 's PR does

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct, Spark supports it, I double checked it. Maybe it is SQL 2023 standard, but if any of dialects supports this syntax then parser is not expected to restrict it and this PR is correct

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say "SQL Standard" in quotes -- because even though there is such a thing I don't think any database follows it exactly. It is more like "SQL Standard Suggestions-ish" or something 🤣

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jonahgao and @comphead -- i think this PR makes Datafusion more correct (or less incorrect 🤔 )

@@ -138,6 +138,10 @@ impl AggregateUDFImpl for Count {
return Ok(Box::new(CountAccumulator::new()));
}

if acc_args.input_exprs.len() > 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If any of the dialects allow the syntax I think the sqlparser-rs AST will need to support multiple args.

This is kind of explained https://github.com/sqlparser-rs/sqlparser-rs?tab=readme-ov-file#syntax-vs-semantics (where what is a syntax error in some systems and a semantic one in others needs to be enforced as a semantic error)

Thus I think the right thing to do here is check and error in DataFusion, as @jonahgao 's PR does

Copy link
Contributor

@comphead comphead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm thanks @jonahgao

@jonahgao
Copy link
Member Author

Thanks @comphead @alamb for the review.

@jonahgao jonahgao merged commit 7a23ea9 into apache:main Jul 11, 2024
23 checks passed
@jonahgao jonahgao deleted the count_multiple_args branch July 11, 2024 01:38
Lordworms pushed a commit to Lordworms/arrow-datafusion that referenced this pull request Jul 12, 2024
…ments (apache#11391)

* Minor: return "not supported" for COUNT DISTINCT with multiple arguments

* update condition
findepi pushed a commit to findepi/datafusion that referenced this pull request Jul 16, 2024
…ments (apache#11391)

* Minor: return "not supported" for COUNT DISTINCT with multiple arguments

* update condition
xinlifoobar pushed a commit to xinlifoobar/datafusion that referenced this pull request Jul 17, 2024
…ments (apache#11391)

* Minor: return "not supported" for COUNT DISTINCT with multiple arguments

* update condition
xinlifoobar pushed a commit to xinlifoobar/datafusion that referenced this pull request Jul 18, 2024
…ments (apache#11391)

* Minor: return "not supported" for COUNT DISTINCT with multiple arguments

* update condition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Review the behavior of count with multiple arguments
3 participants