-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-33122][SQL] Remove redundant aggregates in the Optimzier #30018
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
Closed
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
84ba723
RemoveRedundantAggregates
tanelk 14f3033
Merge branch 'master' into SPARK-33122
tanelk 29701dc
Clearer naming
tanelk 4ce0644
Clearer naming
tanelk ef64abf
Handle aliases
ca974c7
Merge branch 'master' into SPARK-33122
tanelk 4bf08bb
UTs for non-deterministic cases
tanelk 832ff02
Extract alias helper
tanelk 6cdc43a
cleanup
2d63bb4
Extract AliasHelper
tanelk a82699e
Loosen the determinicity check
tanelk 38d7007
Typo
tanelk fab0427
Keep old behaviour with attribute naming
tanelk 67861f9
Merge branch 'master' into SPARK-33122
tanelk 12d1bf4
Fix merge conflict
tanelk e396ce3
Fix merge conflict
tanelk 6d68718
Fix merge conflict
tanelk 0d86060
Fix failing test
tanelk 33d6072
Remove non-deterministic restriction
tanelk 3e3168a
Address comments
tanelk 57af005
Add UTs for python funcs
tanelk f50048f
Merge branch 'SPARK-33122' of https://github.com/tanelk/spark into SP…
9219bca
Merge branch 'master' into SPARK-33122
c21dd52
Merge branch 'master' into SPARK-33122
b415194
upperRefsOnlyDeterministicNonAgg
tanelk 797d48f
upperRefsOnlyDeterministicNonAgg
tanelk 2b32f4a
Merge branch 'master' into SPARK-33122
tanelk e202987
Cleanup
tanelk 37dc4b1
rerun plan stability
tanelk 07e758d
Trigger build
tanelk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,6 +108,7 @@ abstract class Optimizer(catalogManager: CatalogManager) | |
| RewriteCorrelatedScalarSubquery, | ||
| EliminateSerialization, | ||
| RemoveRedundantAliases, | ||
| RemoveRedundantAggregates, | ||
| UnwrapCastInBinaryComparison, | ||
| RemoveNoopOperators, | ||
| CombineUpdateFields, | ||
|
|
@@ -477,6 +478,25 @@ object RemoveRedundantAliases extends Rule[LogicalPlan] { | |
| def apply(plan: LogicalPlan): LogicalPlan = removeRedundantAliases(plan, AttributeSet.empty) | ||
| } | ||
|
|
||
| /** | ||
| * Remove redundant aggregates from a query plan. A redundant aggregate is an aggregate whose | ||
| * only goal is to keep distinct values, while its parent aggregate would ignore duplicate values. | ||
| */ | ||
| object RemoveRedundantAggregates extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
| case upper @ Aggregate(_, _, lower: Aggregate) if lowerIsRedundant(upper, lower) => | ||
| upper.copy(child = lower.child) | ||
tanelk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private def lowerIsRedundant(upper: Aggregate, lower: Aggregate): Boolean = { | ||
tanelk marked this conversation as resolved.
Show resolved
Hide resolved
Member
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. nit. Usually, |
||
| val upperReferencesOnlyGrouping = upper.references | ||
| .subsetOf(AttributeSet(lower.groupingExpressions)) | ||
tanelk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val upperHasAggregateExpressions = upper.aggregateExpressions | ||
| .exists(_.find(_.isInstanceOf[AggregateExpression]).nonEmpty) | ||
tanelk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| upperReferencesOnlyGrouping && !upperHasAggregateExpressions | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Remove no-op operators from the query plan that do not make any modifications. | ||
| */ | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -563,7 +563,7 @@ case class Range( | |
| * | ||
| * @param groupingExpressions expressions for grouping keys | ||
| * @param aggregateExpressions expressions for a project list, which could contain | ||
| * [[AggregateFunction]]s. | ||
| * [[AggregateExpression]]s. | ||
|
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. This caused some confusion while making this PR |
||
| * | ||
| * Note: Currently, aggregateExpressions is the project list of this Group by operator. Before | ||
| * separating projection from grouping and aggregate, we should avoid expression-level optimization | ||
|
|
||
83 changes: 83 additions & 0 deletions
83
...c/test/scala/org/apache/spark/sql/catalyst/optimizer/RemoveRedundantAggregatesSuite.scala
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.plans.PlanTest | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
|
|
||
| class RemoveRedundantAggregatesSuite extends PlanTest { | ||
|
|
||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = Batch("RemoveRedundantAggregates", FixedPoint(10), | ||
| RemoveRedundantAggregates) :: Nil | ||
| } | ||
|
|
||
| test("Remove redundant aggregate") { | ||
| val relation = LocalRelation('a.int, 'b.int) | ||
| val query = relation | ||
| .groupBy('a)('a, count('b)) | ||
| .groupBy('a)('a) | ||
| .analyze | ||
| val expected = relation | ||
| .groupBy('a)('a) | ||
| .analyze | ||
| val optimized = Optimize.execute(query) | ||
| comparePlans(optimized, expected) | ||
| } | ||
|
|
||
| test("Remove 2 redundant aggregates") { | ||
| val relation = LocalRelation('a.int, 'b.int) | ||
| val query = relation | ||
| .groupBy('a)('a, count('b)) | ||
| .groupBy('a)('a) | ||
| .groupBy('a)('a) | ||
| .analyze | ||
| val expected = relation | ||
| .groupBy('a)('a) | ||
| .analyze | ||
| val optimized = Optimize.execute(query) | ||
| comparePlans(optimized, expected) | ||
| } | ||
|
|
||
| test("Remove redundant aggregate with different grouping") { | ||
| val relation = LocalRelation('a.int, 'b.int) | ||
| val query = relation | ||
| .groupBy('a, 'b)('a) | ||
| .groupBy('a)('a) | ||
| .analyze | ||
| val expected = relation | ||
| .groupBy('a)('a) | ||
| .analyze | ||
| val optimized = Optimize.execute(query) | ||
| comparePlans(optimized, expected) | ||
| } | ||
|
|
||
| test("Keep non-redundant aggregate") { | ||
| val relation = LocalRelation('a.int, 'b.int) | ||
| val query = relation | ||
| .groupBy('a)('a, first('b) as 'b) | ||
| // The count would change if we remove the first aggregate | ||
| .groupBy('a)('a, count('b)) | ||
| .analyze | ||
| val optimized = Optimize.execute(query) | ||
| comparePlans(optimized, query) | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.