-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-49000][SQL][3.4] Fix "select count(distinct 1) from t" where t is empty table by expanding RewriteDistinctAggregates #47567
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
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uros-db
commented
Aug 1, 2024
Contributor
Author
uros-db
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
backport to 3.4 ready, waiting for CI checks
nikolamand-db
approved these changes
Aug 1, 2024
dbatomic
approved these changes
Aug 1, 2024
Member
dongjoon-hyun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you re-trigger the failed CI, @uros-db ?
yaooqinn
approved these changes
Aug 2, 2024
Member
|
Merged to 3.4, thank you all |
yaooqinn
pushed a commit
that referenced
this pull request
Aug 2, 2024
… is empty table by expanding RewriteDistinctAggregates
### What changes were proposed in this pull request?
Fix `RewriteDistinctAggregates` rule to deal properly with aggregation on DISTINCT literals. Physical plan for `select count(distinct 1) from t`:
```
-- count(distinct 1)
== Physical Plan ==
AdaptiveSparkPlan isFinalPlan=false
+- HashAggregate(keys=[], functions=[count(distinct 1)], output=[count(DISTINCT 1)#2L])
+- HashAggregate(keys=[], functions=[partial_count(distinct 1)], output=[count#6L])
+- HashAggregate(keys=[], functions=[], output=[])
+- Exchange SinglePartition, ENSURE_REQUIREMENTS, [plan_id=20]
+- HashAggregate(keys=[], functions=[], output=[])
+- FileScan parquet spark_catalog.default.t[] Batched: false, DataFilters: [], Format: Parquet, Location: InMemoryFileIndex(1 paths)[file:/Users/nikola.mandic/oss-spark/spark-warehouse/org.apache.spark.s..., PartitionFilters: [], PushedFilters: [], ReadSchema: struct<>
```
Problem is happening when `HashAggregate(keys=[], functions=[], output=[])` node yields one row to `partial_count` node, which then captures one row. This four-node structure is constructed by `AggUtils.planAggregateWithOneDistinct`.
To fix the problem, we're adding `Expand` node which will force non-empty grouping expressions in `HashAggregateExec` nodes. This will in turn enable streaming zero rows to parent `partial_count` node, yielding correct final result.
### Why are the changes needed?
Aggregation with DISTINCT literal gives wrong results. For example, when running on empty table `t`:
`select count(distinct 1) from t` returns 1, while the correct result should be 0.
For reference:
`select count(1) from t` returns 0, which is the correct and expected result.
### Does this PR introduce _any_ user-facing change?
Yes, this fixes a critical bug in Spark.
### How was this patch tested?
New e2e SQL tests for aggregates with DISTINCT literals.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #47567 from uros-db/SPARK-49000-3.4.
Authored-by: Uros Bojanic <157381213+uros-db@users.noreply.github.com>
Signed-off-by: Kent Yao <yao@apache.org>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Fix
RewriteDistinctAggregatesrule to deal properly with aggregation on DISTINCT literals. Physical plan forselect count(distinct 1) from t:Problem is happening when
HashAggregate(keys=[], functions=[], output=[])node yields one row topartial_countnode, which then captures one row. This four-node structure is constructed byAggUtils.planAggregateWithOneDistinct.To fix the problem, we're adding
Expandnode which will force non-empty grouping expressions inHashAggregateExecnodes. This will in turn enable streaming zero rows to parentpartial_countnode, yielding correct final result.Why are the changes needed?
Aggregation with DISTINCT literal gives wrong results. For example, when running on empty table
t:select count(distinct 1) from treturns 1, while the correct result should be 0.For reference:
select count(1) from treturns 0, which is the correct and expected result.Does this PR introduce any user-facing change?
Yes, this fixes a critical bug in Spark.
How was this patch tested?
New e2e SQL tests for aggregates with DISTINCT literals.
Was this patch authored or co-authored using generative AI tooling?
No.