-
Notifications
You must be signed in to change notification settings - Fork 29k
SPARK-1627: Support external aggregation by using Aggregator in Spark SQL #867
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 4 commits
6507d15
358874e
f941a50
e5bc329
213fd47
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 |
|---|---|---|
|
|
@@ -17,13 +17,13 @@ | |
|
|
||
| package org.apache.spark.sql.execution | ||
|
|
||
| import java.util.HashMap | ||
|
|
||
| import org.apache.spark.annotation.DeveloperApi | ||
| import org.apache.spark.SparkContext | ||
| import org.apache.spark.{Logging, SparkConf, Aggregator, SparkContext} | ||
| import org.apache.spark.sql.catalyst.errors._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.physical._ | ||
| import org.apache.spark.sql.execution.SparkSqlSerializer | ||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| /** | ||
| * :: DeveloperApi :: | ||
|
|
@@ -42,7 +42,7 @@ case class Aggregate( | |
| groupingExpressions: Seq[Expression], | ||
| aggregateExpressions: Seq[NamedExpression], | ||
| child: SparkPlan)(@transient sc: SparkContext) | ||
| extends UnaryNode with NoBind { | ||
| extends UnaryNode with NoBind with Logging { | ||
|
|
||
| override def requiredChildDistribution = | ||
| if (partial) { | ||
|
|
@@ -155,48 +155,60 @@ case class Aggregate( | |
| } | ||
| } else { | ||
| child.execute().mapPartitions { iter => | ||
| val hashTable = new HashMap[Row, Array[AggregateFunction]] | ||
| val groupingProjection = new MutableProjection(groupingExpressions, childOutput) | ||
|
|
||
| var currentRow: Row = null | ||
| while (iter.hasNext) { | ||
| currentRow = iter.next() | ||
| val currentGroup = groupingProjection(currentRow) | ||
| var currentBuffer = hashTable.get(currentGroup) | ||
| if (currentBuffer == null) { | ||
| currentBuffer = newAggregateBuffer() | ||
| hashTable.put(currentGroup.copy(), currentBuffer) | ||
| val groupingProjection = new | ||
| MutableProjection(groupingExpressions, childOutput) | ||
|
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. no need to wrap this line
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. Done |
||
| // TODO: Can't use "Array[AggregateFunction]" directly, due to lack of | ||
| // "concat(AggregateFunction, AggregateFunction)". Should add | ||
| // AggregateFunction.update(agg: AggregateFunction) in the future. | ||
| def createCombiner(row: Row) = mergeValue(newAggregateBuffer(), row) | ||
| def mergeValue(buffer: Array[AggregateFunction], row: Row) = { | ||
| for (i <- 0 to buffer.length - 1) { | ||
|
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. It'd be better to rewrite this using a while loop, since while loops perform much better than for loop in Scala.
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. Done |
||
| buffer(i).update(row) | ||
| } | ||
|
|
||
| var i = 0 | ||
| while (i < currentBuffer.length) { | ||
| currentBuffer(i).update(currentRow) | ||
| i += 1 | ||
| buffer | ||
| } | ||
| def mergeCombiners(buf1: Array[AggregateFunction], buf2: Array[AggregateFunction]) = { | ||
| if (buf1.length != buf2.length) { | ||
| throw new TreeNodeException(this, s"Unequal aggregate buffer length ${buf1.length} != ${buf2.length}") | ||
| } | ||
| for (i <- 0 to buf1.length - 1) { | ||
|
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. while loop here too
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. Done |
||
| buf1(i).merge(buf2(i)) | ||
| } | ||
| buf1 | ||
| } | ||
|
|
||
| val aggregator = new Aggregator[Row, Row, Array[AggregateFunction]]( | ||
| createCombiner, mergeValue, mergeCombiners, new SparkSqlSerializer(new SparkConf(false))) | ||
|
|
||
| val aggIter = aggregator.combineValuesByKey( | ||
| new Iterator[(Row, Row)] { // (groupKey, row) | ||
| override final def hasNext: Boolean = iter.hasNext | ||
|
|
||
| override final def next(): (Row, Row) = { | ||
| val row = iter.next() | ||
| // TODO: copy() here for suppressing reference problems. Please clearly address | ||
| // the root-cause and remove copy() here. | ||
| (groupingProjection(row).copy(), row) | ||
| } | ||
| }, | ||
| null | ||
| ) | ||
| new Iterator[Row] { | ||
| private[this] val hashTableIter = hashTable.entrySet().iterator() | ||
| private[this] val aggregateResults = new GenericMutableRow(computedAggregates.length) | ||
| private[this] val resultProjection = | ||
| new MutableProjection(resultExpressions, computedSchema ++ namedGroups.map(_._2)) | ||
| private[this] val resultProjection = new MutableProjection( | ||
| resultExpressions, computedSchema ++ namedGroups.map(_._2)) | ||
| private[this] val joinedRow = new JoinedRow | ||
|
|
||
| override final def hasNext: Boolean = hashTableIter.hasNext | ||
| override final def hasNext: Boolean = aggIter.hasNext | ||
|
|
||
| override final def next(): Row = { | ||
| val currentEntry = hashTableIter.next() | ||
| val currentGroup = currentEntry.getKey | ||
| val currentBuffer = currentEntry.getValue | ||
|
|
||
| var i = 0 | ||
| while (i < currentBuffer.length) { | ||
| // Evaluating an aggregate buffer returns the result. No row is required since we | ||
| // already added all rows in the group using update. | ||
| aggregateResults(i) = currentBuffer(i).eval(EmptyRow) | ||
| i += 1 | ||
| val entry = aggIter.next() | ||
| val group = entry._1 | ||
| val data = entry._2 | ||
|
|
||
| for (i <- 0 to data.length - 1) { | ||
|
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. again, while loop here.
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. Done
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. Hi there, I made some changes per comments couple of days ago here: Best Regards, On Sat, May 24, 2014 at 8:52 PM, Reynold Xin [email protected]
|
||
| aggregateResults(i) = data(i).eval(EmptyRow) | ||
| } | ||
| resultProjection(joinedRow(aggregateResults, currentGroup)) | ||
| resultProjection(joinedRow(aggregateResults, group)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
also update the documentation above to add the new parameter.
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.
Done