Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/8038#discussion_r36682953
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/TungstenAggregate.scala
---
@@ -61,32 +62,54 @@ case class TungstenAggregate(
}
protected override def doExecute(): RDD[InternalRow] = attachTree(this,
"execute") {
- child.execute().mapPartitions { iter =>
- val hasInput = iter.hasNext
- if (!hasInput && groupingExpressions.nonEmpty) {
- // This is a grouped aggregate and the input iterator is empty,
- // so return an empty iterator.
- Iterator.empty.asInstanceOf[Iterator[UnsafeRow]]
- } else {
- val aggregationIterator =
- new TungstenAggregationIterator(
- groupingExpressions,
- nonCompleteAggregateExpressions,
- completeAggregateExpressions,
- initialInputBufferOffset,
- resultExpressions,
- newMutableProjection,
- child.output,
- iter,
- testFallbackStartsAt)
- if (!hasInput && groupingExpressions.isEmpty) {
+ /**
+ * Set up the underlying unsafe data structures used before computing
the parent partition.
+ * This makes sure our iterator is not starved by other operators in
the same task.
+ */
+ def preparePartition(): TungstenAggregationIterator = {
+ new TungstenAggregationIterator(
+ groupingExpressions,
+ nonCompleteAggregateExpressions,
+ completeAggregateExpressions,
+ initialInputBufferOffset,
+ resultExpressions,
+ newMutableProjection,
+ child.output,
+ testFallbackStartsAt)
+ }
+
+ /** Compute a partition using the iterator already set up previously.
*/
+ def executePartition(
+ context: TaskContext,
+ partitionIndex: Int,
+ aggregationIterator: TungstenAggregationIterator,
+ parentIterator: Iterator[InternalRow]): Iterator[UnsafeRow] = {
+ val hasInput = parentIterator.hasNext
+ if (!hasInput) {
+ // We're not using the underlying map, so we just can free it here
+ aggregationIterator.free()
+ if (groupingExpressions.isEmpty) {
+ // This is a grouped aggregate and the input iterator is empty,
+ // so return an empty iterator.
Iterator.single[UnsafeRow](aggregationIterator.outputForEmptyGroupingKeyWithoutInput())
} else {
- aggregationIterator
+ Iterator[UnsafeRow]()
}
+ } else {
+ aggregationIterator.start(parentIterator)
+ aggregationIterator
}
}
+
+ // Note: we need to set up the iterator in each partition before
computing the
+ // parent partition, so we cannot simply use `mapPartitions` here
(SPARK-9747).
+ val parentPartition = child.execute()
+ val resultRdd = {
+ new MapPartitionsWithPreparationRDD[UnsafeRow, InternalRow,
TungstenAggregationIterator](
+ parentPartition, preparePartition, executePartition,
preservesPartitioning = true)
+ }
+ resultRdd.asInstanceOf[RDD[InternalRow]]
--- End diff --
Should we just return `resultRdd`? Seems we do not need to cast?
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]