Github user lamuguo commented on a diff in the pull request: https://github.com/apache/spark/pull/867#discussion_r13217803 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/Aggregate.scala --- @@ -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) + // 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) { + 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) { + 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) { --- End diff -- Done
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---