Github user wzhfy commented on a diff in the pull request:
https://github.com/apache/spark/pull/16677#discussion_r115680727
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/limit.scala ---
@@ -90,25 +95,102 @@ trait BaseLimitExec extends UnaryExecNode with
CodegenSupport {
}
/**
- * Take the first `limit` elements of each child partition, but do not
collect or shuffle them.
+ * Take the `limit` elements of the child output.
*/
-case class LocalLimitExec(limit: Int, child: SparkPlan) extends
BaseLimitExec {
+case class GlobalLimitExec(limit: Int, child: SparkPlan) extends
UnaryExecNode {
- override def outputOrdering: Seq[SortOrder] = child.outputOrdering
+ override def output: Seq[Attribute] = child.output
override def outputPartitioning: Partitioning = child.outputPartitioning
-}
-/**
- * Take the first `limit` elements of the child's single output partition.
- */
-case class GlobalLimitExec(limit: Int, child: SparkPlan) extends
BaseLimitExec {
+ override def outputOrdering: Seq[SortOrder] = child.outputOrdering
+
+ private val serializer: Serializer = new
UnsafeRowSerializer(child.output.size)
+
+ protected override def doExecute(): RDD[InternalRow] = {
+ val childRDD = child.execute()
+ val partitioner = LocalPartitioning(child.outputPartitioning,
+ childRDD.getNumPartitions)
+ val shuffleDependency = ShuffleExchange.prepareShuffleDependency(
+ childRDD, child.output, partitioner, serializer)
+ val numberOfOutput: Seq[Long] = if
(shuffleDependency.rdd.getNumPartitions != 0) {
+ // submitMapStage does not accept RDD with 0 partition.
+ // So, we will not submit this dependency.
+ val submittedStageFuture =
sparkContext.submitMapStage(shuffleDependency)
+ submittedStageFuture.get().numberOfOutput.toSeq
+ } else {
+ Nil
+ }
- override def requiredChildDistribution: List[Distribution] = AllTuples
:: Nil
+ // Try to keep child plan's original data parallelism or not. It is
enabled by default.
+ val respectChildParallelism = sqlContext.conf.enableParallelGlobalLimit
- override def outputPartitioning: Partitioning = child.outputPartitioning
+ val shuffled = new ShuffledRowRDD(shuffleDependency)
- override def outputOrdering: Seq[SortOrder] = child.outputOrdering
+ val sumOfOutput = numberOfOutput.sum
+ if (sumOfOutput <= limit) {
+ shuffled
+ } else if (!respectChildParallelism) {
+ // This is mainly for tests.
+ // We take the rows of each partition until we reach the required
limit number.
+ var numTakenRow = 0
+ val takeAmounts = new mutable.HashMap[Int, Int]()
+ numberOfOutput.zipWithIndex.foreach { case (num, index) =>
+ if (numTakenRow + num < limit) {
+ numTakenRow += num.toInt
+ takeAmounts += ((index, num.toInt))
+ } else {
+ val toTake = limit - numTakenRow
+ numTakenRow += toTake
+ takeAmounts += ((index, toTake))
+ }
+ }
+ val broadMap = sparkContext.broadcast(takeAmounts)
+ shuffled.mapPartitionsWithIndexInternal { case (index, iter) =>
+ broadMap.value.get(index).map { size =>
+ iter.take(size)
+ }.get
+ }
+ } else {
+ // We try to distribute the required limit number of rows across all
child rdd's partitions.
--- End diff --
The logic is a little complex, is the following the same logic as you
wanted?
```
// We try to distribute the required limit number of rows across all
child rdd's partitions.
var totalRemainingRows: Long = limit
val takeAmountPerPartition: Array[Long] =
Array.fill[Long](numberOfOutput.length)(0L)
val remainingRowsPerPartition: Array[Long] = Array(numberOfOutput: _*)
while (totalRemainingRows > 0) {
val nonEmptyParts = remainingRowsPerPartition.count(_ > 0)
// If limit < nonEmptyParts, take one row from each partition,
until we get `limit` rows.
// Otherwise, take `limit / nonEmptyParts` rows from each partition.
val takePerPart = math.max(1, limit / nonEmptyParts)
remainingRowsPerPartition.zipWithIndex.foreach { case (num, index)
=>
// In case limit < nonEmptyParts, we may run out of
totalRemainingRows during the
// traversal, so we need to add this check.
if (totalRemainingRows > 0) {
if (num >= takePerPart) {
totalRemainingRows -= takePerPart
takeAmountPerPartition(index) += takePerPart
} else {
totalRemainingRows -= num
takeAmountPerPartition(index) += num
}
}
}
for (i <- remainingRowsPerPartition.indices) {
remainingRowsPerPartition(i) -= takeAmountPerPartition(i)
}
}
val broadMap = sparkContext.broadcast(takeAmountPerPartition)
shuffled.mapPartitionsWithIndexInternal { case (index, iter) =>
iter.take(broadMap.value(index).toInt)
}
```
---
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]