-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-38832][SQL] Remove unnecessary distinct in aggregate expression by distinctKeys #36117
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -29,6 +29,12 @@ import org.apache.spark.sql.internal.SQLConf.PROPAGATE_DISTINCT_KEYS_ENABLED | |||
| */ | ||||
| trait LogicalPlanDistinctKeys { self: LogicalPlan => | ||||
| lazy val distinctKeys: Set[ExpressionSet] = { | ||||
| if (conf.getConf(PROPAGATE_DISTINCT_KEYS_ENABLED)) DistinctKeyVisitor.visit(self) else Set.empty | ||||
| if (conf.getConf(PROPAGATE_DISTINCT_KEYS_ENABLED)) { | ||||
| val keys = DistinctKeyVisitor.visit(self) | ||||
| require(keys.forall(_.nonEmpty)) | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's more about avoid some unexpected things. It will be a correctness issue if other opterators return empty distinct key. And as you mentioned, the global aggregate has already optimzied by
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My point is that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have already forbidden it inside Line 50 in a67acba
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only done at the
I prefer option 2 as I think an empty expression set does mean something as a distinct key, we should not ignore this information. It also works the same as other distinct keys:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's a good point. I will do a followup soon. |
||||
| keys | ||||
| } else { | ||||
| Set.empty | ||||
| } | ||||
| } | ||||
| } | ||||
Uh oh!
There was an error while loading. Please reload this page.
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.
Is it correct?
If input plan to this rule is:
Will the added case branch rewrite the plan to
agg.child.distinctKeys is
{a, b, c}ExpressionSet(ae.aggregateFunction.children.filterNot(_.foldable)) is
{c}.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.
the distinctKeys of
distinct a, b, cis ExpressionSet(a, b, c) not ExpressionSet(a), ExpressionSet(b), ExpressionSet(c)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.
You're right. I forgot
distinctKeysis a set of sets.How about:
Alternatively, we can do a require here to make sure that we never return an empty key:
https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/LogicalPlanDistinctKeys.scala#L32
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.
make sense, I add a require at
LogicalPlanDistinctKeys