Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
case logical.Repartition(numPartitions, shuffle, child) =>
if (shuffle) {
ShuffleExchangeExec(RoundRobinPartitioning(numPartitions),
planLater(child), canChangeNumPartitions = false) :: Nil
planLater(child), noUserSpecifiedNumPartition = false) :: Nil
} else {
execution.CoalesceExec(numPartitions, planLater(child)) :: Nil
}
Expand Down Expand Up @@ -698,9 +698,10 @@ abstract class SparkStrategies extends QueryPlanner[SparkPlan] {
case r: logical.Range =>
execution.RangeExec(r) :: Nil
case r: logical.RepartitionByExpression =>
val canChangeNumParts = r.optNumPartitions.isEmpty
exchange.ShuffleExchangeExec(
r.partitioning, planLater(r.child), canChangeNumParts) :: Nil
r.partitioning,
planLater(r.child),
noUserSpecifiedNumPartition = r.optNumPartitions.isEmpty) :: Nil
case ExternalRDD(outputObjAttr, rdd) => ExternalRDDScanExec(outputObjAttr, rdd) :: Nil
case r: LogicalRDD =>
RDDScanExec(r.output, r.rdd, "ExistingRDD", r.outputPartitioning, r.outputOrdering) :: Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ object OptimizeLocalShuffleReader {

def canUseLocalShuffleReader(plan: SparkPlan): Boolean = plan match {
case s: ShuffleQueryStageExec =>
s.shuffle.canChangeNumPartitions
s.shuffle.canChangeNumPartitions && s.mapStats.isDefined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This skips 0 partitions case, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, otherwise we will hit

val splitPoints = if (numMappers == 0) {
  Seq.empty
} else ...

which creates a local reader with 0 partitions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to turn the if into assert, but I'd like to only do it in master to be safe.

case CustomShuffleReaderExec(s: ShuffleQueryStageExec, partitionSpecs) =>
s.shuffle.canChangeNumPartitions && partitionSpecs.nonEmpty
s.shuffle.canChangeNumPartitions && s.mapStats.isDefined && partitionSpecs.nonEmpty
case _ => false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ object ShufflePartitionsUtil extends Logging {
var coalescedSize = 0L
var i = 0

def createPartitionSpec(): Unit = {
def createPartitionSpec(forceCreate: Boolean = false): Unit = {
// Skip empty inputs, as it is a waste to launch an empty task.
if (coalescedSize > 0) {
if (coalescedSize > 0 || forceCreate) {
partitionSpecs += CoalescedPartitionSpec(latestSplitPoint, i)
}
}
Expand All @@ -120,7 +120,8 @@ object ShufflePartitionsUtil extends Logging {
}
i += 1
}
createPartitionSpec()
// Create at least one partition if all partitions are empty.
createPartitionSpec(partitionSpecs.isEmpty)
partitionSpecs.toSeq
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ trait ShuffleExchangeLike extends Exchange {
case class ShuffleExchangeExec(
override val outputPartitioning: Partitioning,
child: SparkPlan,
canChangeNumPartitions: Boolean = true) extends ShuffleExchangeLike {
noUserSpecifiedNumPartition: Boolean = true) extends ShuffleExchangeLike {

// If users specify the num partitions via APIs like `repartition`, we shouldn't change it.
// For `SinglePartition`, it requires exactly one partition and we can't change it either.
override def canChangeNumPartitions: Boolean =
noUserSpecifiedNumPartition && outputPartitioning != SinglePartition

private lazy val writeMetrics =
SQLShuffleWriteMetricsReporter.createShuffleWriteMetrics(sparkContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,11 @@ class ShufflePartitionsUtilSuite extends SparkFunSuite {
// the size of data is 0.
val bytesByPartitionId1 = Array[Long](0, 0, 0, 0, 0)
val bytesByPartitionId2 = Array[Long](0, 0, 0, 0, 0)
// Create at least one partition spec
val expectedPartitionSpecs = Seq(CoalescedPartitionSpec(0, 5))
checkEstimation(
Array(bytesByPartitionId1, bytesByPartitionId2),
Seq.empty, targetSize, minNumPartitions)
expectedPartitionSpecs, targetSize, minNumPartitions)
}


Expand Down Expand Up @@ -248,16 +250,19 @@ class ShufflePartitionsUtilSuite extends SparkFunSuite {
val minNumPartitions = 2

{
// 1 shuffle: All bytes per partition are 0, no partition spec created.
// 1 shuffle: All bytes per partition are 0, 1 empty partition spec created.
val bytesByPartitionId = Array[Long](0, 0, 0, 0, 0)
checkEstimation(Array(bytesByPartitionId), Seq.empty, targetSize)
val expectedPartitionSpecs = Seq(CoalescedPartitionSpec(0, 5))
checkEstimation(Array(bytesByPartitionId), expectedPartitionSpecs, targetSize)
}

{
// 2 shuffles: All bytes per partition are 0, no partition spec created.
// 2 shuffles: All bytes per partition are 0, 1 empty partition spec created.
val bytesByPartitionId1 = Array[Long](0, 0, 0, 0, 0)
val bytesByPartitionId2 = Array[Long](0, 0, 0, 0, 0)
checkEstimation(Array(bytesByPartitionId1, bytesByPartitionId2), Seq.empty, targetSize)
val expectedPartitionSpecs = Seq(CoalescedPartitionSpec(0, 5))
checkEstimation(
Array(bytesByPartitionId1, bytesByPartitionId2), expectedPartitionSpecs, targetSize)
}

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class AdaptiveQueryExecSuite
}
}

test("Empty stage coalesced to 0-partition RDD") {
test("Empty stage coalesced to 1-partition RDD") {
withSQLConf(
SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
SQLConf.COALESCE_PARTITIONS_ENABLED.key -> "true") {
Expand All @@ -227,8 +227,8 @@ class AdaptiveQueryExecSuite
val coalescedReaders = collect(plan) {
case r: CustomShuffleReaderExec => r
}
assert(coalescedReaders.length == 2)
coalescedReaders.foreach(r => assert(r.partitionSpecs.isEmpty))
assert(coalescedReaders.length == 3)
coalescedReaders.foreach(r => assert(r.partitionSpecs.length == 1))
}

withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "1") {
Expand All @@ -239,8 +239,8 @@ class AdaptiveQueryExecSuite
val coalescedReaders = collect(plan) {
case r: CustomShuffleReaderExec => r
}
assert(coalescedReaders.length == 2, s"$plan")
coalescedReaders.foreach(r => assert(r.partitionSpecs.isEmpty))
assert(coalescedReaders.length == 3, s"$plan")
coalescedReaders.foreach(r => assert(r.isLocalReader || r.partitionSpecs.length == 1))
}
}
}
Expand Down