-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-9240] [SQL] Hybrid aggregate operator using unsafe row #7813
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 all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3915bac
Create a base iterator class for aggregation iterators and add the in…
yhuai 299008c
First round cleanup.
yhuai af32210
Check iter.hasNext before we create an iterator because the construct…
yhuai f60cc83
Also check input schema.
yhuai d2c45a0
wip
yhuai 3171f44
wip
yhuai f52ee53
wip
yhuai bd9282b
UDAFs now supports UnsafeRow.
yhuai 33b7022
wip
yhuai 533d5b2
Prepare for fallback!
yhuai 7fcbd87
Add a flag to control what iterator to use.
yhuai b1ea5cf
wip
yhuai 964f88b
Implement fallback strategy.
yhuai 21fd15f
Remove unnecessary change.
yhuai 0f1b06f
Remove unnecessary code.
yhuai c9cf3b6
update
yhuai ba6afbc
Add a little bit more comments.
yhuai 74d93c5
Merge remote-tracking branch 'upstream/master' into AggregateOperator
yhuai e317e2b
Remove unnecessary change.
yhuai 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
182 changes: 182 additions & 0 deletions
182
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/Aggregate.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,182 @@ | ||
| /* | ||
| * 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.rdd.RDD | ||
| import org.apache.spark.sql.catalyst.errors._ | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate._ | ||
| import org.apache.spark.sql.catalyst.plans.physical.{UnspecifiedDistribution, ClusteredDistribution, AllTuples, Distribution} | ||
| import org.apache.spark.sql.execution.{UnsafeFixedWidthAggregationMap, SparkPlan, UnaryNode} | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| /** | ||
| * An Aggregate Operator used to evaluate [[AggregateFunction2]]. Based on the data types | ||
| * of the grouping expressions and aggregate functions, it determines if it uses | ||
| * sort-based aggregation and hybrid (hash-based with sort-based as the fallback) to | ||
| * process input rows. | ||
| */ | ||
| case class Aggregate( | ||
| requiredChildDistributionExpressions: Option[Seq[Expression]], | ||
| groupingExpressions: Seq[NamedExpression], | ||
| nonCompleteAggregateExpressions: Seq[AggregateExpression2], | ||
| nonCompleteAggregateAttributes: Seq[Attribute], | ||
| completeAggregateExpressions: Seq[AggregateExpression2], | ||
| completeAggregateAttributes: Seq[Attribute], | ||
| initialInputBufferOffset: Int, | ||
| resultExpressions: Seq[NamedExpression], | ||
| child: SparkPlan) | ||
| extends UnaryNode { | ||
|
|
||
| private[this] val allAggregateExpressions = | ||
| nonCompleteAggregateExpressions ++ completeAggregateExpressions | ||
|
|
||
| private[this] val hasNonAlgebricAggregateFunctions = | ||
| !allAggregateExpressions.forall(_.aggregateFunction.isInstanceOf[AlgebraicAggregate]) | ||
|
|
||
| // Use the hybrid iterator if (1) unsafe is enabled, (2) the schemata of | ||
| // grouping key and aggregation buffer is supported; and (3) all | ||
| // aggregate functions are algebraic. | ||
| private[this] val supportsHybridIterator: Boolean = { | ||
| val aggregationBufferSchema: StructType = | ||
| StructType.fromAttributes( | ||
| allAggregateExpressions.flatMap(_.aggregateFunction.bufferAttributes)) | ||
| val groupKeySchema: StructType = | ||
| StructType.fromAttributes(groupingExpressions.map(_.toAttribute)) | ||
|
|
||
| val schemaSupportsUnsafe: Boolean = | ||
| UnsafeFixedWidthAggregationMap.supportsAggregationBufferSchema(aggregationBufferSchema) && | ||
| UnsafeProjection.canSupport(groupKeySchema) | ||
|
|
||
| // TODO: Use the hybrid iterator for non-algebric aggregate functions. | ||
| sqlContext.conf.unsafeEnabled && schemaSupportsUnsafe && !hasNonAlgebricAggregateFunctions | ||
| } | ||
|
|
||
| // We need to use sorted input if we have grouping expressions, and | ||
| // we cannot use the hybrid iterator or the hybrid is disabled. | ||
| private[this] val requiresSortedInput: Boolean = { | ||
| groupingExpressions.nonEmpty && !supportsHybridIterator | ||
| } | ||
|
|
||
| override def canProcessUnsafeRows: Boolean = !hasNonAlgebricAggregateFunctions | ||
|
|
||
| // If result expressions' data types are all fixed length, we generate unsafe rows | ||
| // (We have this requirement instead of check the result of UnsafeProjection.canSupport | ||
| // is because we use a mutable projection to generate the result). | ||
| override def outputsUnsafeRows: Boolean = { | ||
| // resultExpressions.map(_.dataType).forall(UnsafeRow.isFixedLength) | ||
| // TODO: Supports generating UnsafeRows. We can just re-enable the line above and fix | ||
| // any issue we get. | ||
| false | ||
| } | ||
|
|
||
| override def output: Seq[Attribute] = resultExpressions.map(_.toAttribute) | ||
|
|
||
| override def requiredChildDistribution: List[Distribution] = { | ||
| requiredChildDistributionExpressions match { | ||
| case Some(exprs) if exprs.length == 0 => AllTuples :: Nil | ||
| case Some(exprs) if exprs.length > 0 => ClusteredDistribution(exprs) :: Nil | ||
| case None => UnspecifiedDistribution :: Nil | ||
| } | ||
| } | ||
|
|
||
| override def requiredChildOrdering: Seq[Seq[SortOrder]] = { | ||
| if (requiresSortedInput) { | ||
| // TODO: We should not sort the input rows if they are just in reversed order. | ||
| groupingExpressions.map(SortOrder(_, Ascending)) :: Nil | ||
| } else { | ||
| Seq.fill(children.size)(Nil) | ||
| } | ||
| } | ||
|
|
||
| override def outputOrdering: Seq[SortOrder] = { | ||
| if (requiresSortedInput) { | ||
| // It is possible that the child.outputOrdering starts with the required | ||
| // ordering expressions (e.g. we require [a] as the sort expression and the | ||
| // child's outputOrdering is [a, b]). We can only guarantee the output rows | ||
| // are sorted by values of groupingExpressions. | ||
| groupingExpressions.map(SortOrder(_, Ascending)) | ||
| } else { | ||
| Nil | ||
| } | ||
| } | ||
|
|
||
| protected override def doExecute(): RDD[InternalRow] = attachTree(this, "execute") { | ||
| child.execute().mapPartitions { iter => | ||
| // Because the constructor of an aggregation iterator will read at least the first row, | ||
| // we need to get the value of iter.hasNext first. | ||
| val hasInput = iter.hasNext | ||
| val useHybridIterator = | ||
| hasInput && | ||
| supportsHybridIterator && | ||
| groupingExpressions.nonEmpty | ||
| if (useHybridIterator) { | ||
| UnsafeHybridAggregationIterator.createFromInputIterator( | ||
| groupingExpressions, | ||
| nonCompleteAggregateExpressions, | ||
| nonCompleteAggregateAttributes, | ||
| completeAggregateExpressions, | ||
| completeAggregateAttributes, | ||
| initialInputBufferOffset, | ||
| resultExpressions, | ||
| newMutableProjection _, | ||
| child.output, | ||
| iter, | ||
| outputsUnsafeRows) | ||
| } else { | ||
| if (!hasInput && groupingExpressions.nonEmpty) { | ||
| // This is a grouped aggregate and the input iterator is empty, | ||
| // so return an empty iterator. | ||
| Iterator[InternalRow]() | ||
| } else { | ||
| val outputIter = SortBasedAggregationIterator.createFromInputIterator( | ||
| groupingExpressions, | ||
| nonCompleteAggregateExpressions, | ||
| nonCompleteAggregateAttributes, | ||
| completeAggregateExpressions, | ||
| completeAggregateAttributes, | ||
| initialInputBufferOffset, | ||
| resultExpressions, | ||
| newMutableProjection _ , | ||
| newProjection _, | ||
| child.output, | ||
| iter, | ||
| outputsUnsafeRows) | ||
| if (!hasInput && groupingExpressions.isEmpty) { | ||
| // There is no input and there is no grouping expressions. | ||
| // We need to output a single row as the output. | ||
| Iterator[InternalRow](outputIter.outputForEmptyGroupingKeyWithoutInput()) | ||
| } else { | ||
| outputIter | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override def simpleString: String = { | ||
| val iterator = if (supportsHybridIterator && groupingExpressions.nonEmpty) { | ||
| classOf[UnsafeHybridAggregationIterator].getSimpleName | ||
| } else { | ||
| classOf[SortBasedAggregationIterator].getSimpleName | ||
| } | ||
|
|
||
| s"""NewAggregate with $iterator ${groupingExpressions} ${allAggregateExpressions}""" | ||
| } | ||
| } | ||
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.
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.
i think it'd be a lot more clear if you just separate this into
This operator is clearly doing the job of two different operators, just with a lot of if branches. It'd be more clear if those go into the planner.