-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-12978][SQL] Skip unnecessary final group-by when input data already clustered with group-by keys #10896
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 1 commit
85a845f
f3659b7
c4242ea
a5addeb
74fa277
b747085
cb51727
5de4871
dc6a7f2
7aff3ad
000c501
2615a6e
91701eb
74a14d7
e37ef6a
8b64305
0375ac6
86068d0
69751bb
d5e0ed3
ac68145
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ package org.apache.spark.sql.execution.aggregate | |
|
|
||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate._ | ||
| import org.apache.spark.sql.execution.aggregate.{Aggregate => AggregateExec} | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
| import org.apache.spark.sql.execution.streaming.{StateStoreRestoreExec, StateStoreSaveExec} | ||
|
|
||
|
|
@@ -27,20 +28,11 @@ import org.apache.spark.sql.execution.streaming.{StateStoreRestoreExec, StateSto | |
| */ | ||
| object AggUtils { | ||
|
|
||
| private[execution] def isAggregate(operator: SparkPlan): Boolean = { | ||
| operator.isInstanceOf[HashAggregateExec] || operator.isInstanceOf[SortAggregateExec] | ||
| } | ||
|
|
||
| private[execution] def supportPartialAggregate(operator: SparkPlan): Boolean = { | ||
| assert(isAggregate(operator)) | ||
| def supportPartial(exprs: Seq[AggregateExpression]) = | ||
| exprs.map(_.aggregateFunction).forall(_.supportsPartial) | ||
| operator match { | ||
| case agg @ HashAggregateExec(_, _, aggregateExpressions, _, _, _, _) => | ||
| supportPartial(aggregateExpressions) | ||
| case agg @ SortAggregateExec(_, _, aggregateExpressions, _, _, _, _) => | ||
| supportPartial(aggregateExpressions) | ||
| } | ||
| private[execution] def supportPartialAggregate(operator: SparkPlan): Boolean = operator match { | ||
| case agg: AggregateExec => | ||
| agg.aggregateExpressions.map(_.aggregateFunction).forall(_.supportsPartial) | ||
| case _ => | ||
| false | ||
| } | ||
|
|
||
| private def createPartialAggregateExec( | ||
|
|
@@ -86,23 +78,18 @@ object AggUtils { | |
|
|
||
| private[execution] def createPartialAggregate(operator: SparkPlan) | ||
| : (SparkPlan, SparkPlan) = operator match { | ||
| case agg @ HashAggregateExec(_, groupingExpressions, aggregateExpressions, _, _, _, child) => | ||
| val mapSideAgg = createPartialAggregateExec( | ||
| groupingExpressions, aggregateExpressions, child) | ||
| val mergeAgg = agg.copy( | ||
| groupingExpressions = groupingExpressions.map(_.toAttribute), | ||
| aggregateExpressions = updateMergeAggregateMode(aggregateExpressions), | ||
| initialInputBufferOffset = groupingExpressions.length) | ||
|
|
||
| (mergeAgg, mapSideAgg) | ||
|
|
||
| case agg @ SortAggregateExec(_, groupingExpressions, aggregateExpressions, _, _, _, child) => | ||
| case agg: Aggregate => | ||
| val mapSideAgg = createPartialAggregateExec( | ||
| groupingExpressions, aggregateExpressions, child) | ||
| val mergeAgg = agg.copy( | ||
| groupingExpressions = groupingExpressions.map(_.toAttribute), | ||
| aggregateExpressions = updateMergeAggregateMode(aggregateExpressions), | ||
| initialInputBufferOffset = groupingExpressions.length) | ||
| agg.groupingExpressions, agg.aggregateExpressions, agg.child) | ||
| val mergeAgg = createAggregateExec( | ||
| requiredChildDistributionExpressions = agg.requiredChildDistributionExpressions, | ||
| groupingExpressions = agg.groupingExpressions.map(_.toAttribute), | ||
| aggregateExpressions = updateMergeAggregateMode(agg.aggregateExpressions), | ||
| aggregateAttributes = agg.aggregateAttributes, | ||
| initialInputBufferOffset = agg.groupingExpressions.length, | ||
| resultExpressions = agg.resultExpressions, | ||
| child = agg.child | ||
|
||
| ) | ||
|
|
||
| (mergeAgg, mapSideAgg) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * 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.execution.aggregate | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression | ||
| import org.apache.spark.sql.catalyst.plans.physical._ | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
|
|
||
| /** | ||
| * A base class for aggregate implementation. | ||
| */ | ||
| trait Aggregate { | ||
|
||
| self: SparkPlan => | ||
|
|
||
| val requiredChildDistributionExpressions: Option[Seq[Expression]] | ||
| val groupingExpressions: Seq[NamedExpression] | ||
| val aggregateExpressions: Seq[AggregateExpression] | ||
| val aggregateAttributes: Seq[Attribute] | ||
| val initialInputBufferOffset: Int | ||
| val resultExpressions: Seq[NamedExpression] | ||
| val child: SparkPlan | ||
|
|
||
| protected[this] val aggregateBufferAttributes = { | ||
| aggregateExpressions.flatMap(_.aggregateFunction.aggBufferAttributes) | ||
| } | ||
|
|
||
| override def producedAttributes: AttributeSet = | ||
| AttributeSet(aggregateAttributes) ++ | ||
| AttributeSet(resultExpressions.diff(groupingExpressions).map(_.toAttribute)) ++ | ||
| AttributeSet(aggregateBufferAttributes) | ||
|
|
||
| override def output: Seq[Attribute] = resultExpressions.map(_.toAttribute) | ||
|
|
||
|
|
||
| override def requiredChildDistribution: List[Distribution] = { | ||
| requiredChildDistributionExpressions match { | ||
| case Some(exprs) if exprs.isEmpty => AllTuples :: Nil | ||
| case Some(exprs) if exprs.nonEmpty => ClusteredDistribution(exprs) :: Nil | ||
| case None => UnspecifiedDistribution :: Nil | ||
| } | ||
| } | ||
| } | ||
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.
A lot of duplication here. It would be nice if we have an parent for the
*AggregateExecnodes.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.
How about this change?
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.
Much better
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 make this public instead of private[execution]? We just opened up a lot of similar APIs.
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.
Small: The name of the function is also quite misleading. It returns a map side and merge aggregate pair, so
createMapMergeAggregatePair? Please also add a little bit of documentation.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.
fixed