rekbun commented on code in PR #60256:
URL: https://github.com/apache/spark/pull/60256#discussion_r4566323080


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/GroupPartitionsExec.scala:
##########
@@ -866,27 +868,29 @@ private[sql] object GroupPartitionsExec {
  * required for storage-partitioned operations.
  *
  * @param groupedPartitions Sequence where each element is a sequence of input 
partition
- *                         indices that should be grouped together
+ *                         indices that should be grouped together. Only the 
driver reads it,
+ *                         so it is not serialized into tasks.
  */
 class GroupedPartitionCoalescer(
-    val groupedPartitions: Seq[Seq[Int]]
+    @transient val groupedPartitions: Seq[Seq[Int]]
   ) extends PartitionCoalescer with Serializable {
 
   override def coalesce(maxPartitions: Int, parent: RDD[_]): 
Array[PartitionGroup] = {
+    require(groupedPartitions.size == maxPartitions, 
s"GroupedPartitionCoalescer has " +
+      s"${groupedPartitions.size} groups but was asked for $maxPartitions 
partitions")
+    val assigned = mutable.Map.empty[String, Int].withDefaultValue(0)
     groupedPartitions.map { partitionIndices =>
-      val partitions = new ArrayBuffer[Partition](partitionIndices.size)
-      val preferredLocations = new ArrayBuffer[String](partitionIndices.size)
-      partitionIndices.foreach { partitionIndex =>
-        val partition = parent.partitions(partitionIndex)
-        partitions += partition
-        preferredLocations ++= parent.preferredLocations(partition)
+      val partitions = partitionIndices.map(parent.partitions(_))
+      // A group of one partition names no location, so the scheduler uses all 
of that
+      // partition's locations. Otherwise prefer the location that the largest 
number of the
+      // group's partitions report, and spread ties across groups rather than 
sending them all to
+      // one location.
+      val preferredLocation = if (partitions.size == 1) None else {
+        
partitions.flatMap(parent.preferredLocations).groupBy(identity).view.mapValues(_.size)

Review Comment:
   Fixed with finding 1: `getPreferredLocs` drops null hosts before the vote, 
so a group of two partitions on (null, h1) gets h1. That's in "the grouped 
coalescer prefers majority hosts and spreads ties".
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/GroupPartitionsExec.scala:
##########
@@ -866,27 +868,29 @@ private[sql] object GroupPartitionsExec {
  * required for storage-partitioned operations.
  *
  * @param groupedPartitions Sequence where each element is a sequence of input 
partition
- *                         indices that should be grouped together
+ *                         indices that should be grouped together. Only the 
driver reads it,
+ *                         so it is not serialized into tasks.
  */
 class GroupedPartitionCoalescer(
-    val groupedPartitions: Seq[Seq[Int]]
+    @transient val groupedPartitions: Seq[Seq[Int]]
   ) extends PartitionCoalescer with Serializable {
 
   override def coalesce(maxPartitions: Int, parent: RDD[_]): 
Array[PartitionGroup] = {
+    require(groupedPartitions.size == maxPartitions, 
s"GroupedPartitionCoalescer has " +
+      s"${groupedPartitions.size} groups but was asked for $maxPartitions 
partitions")
+    val assigned = mutable.Map.empty[String, Int].withDefaultValue(0)
     groupedPartitions.map { partitionIndices =>
-      val partitions = new ArrayBuffer[Partition](partitionIndices.size)
-      val preferredLocations = new ArrayBuffer[String](partitionIndices.size)
-      partitionIndices.foreach { partitionIndex =>
-        val partition = parent.partitions(partitionIndex)
-        partitions += partition
-        preferredLocations ++= parent.preferredLocations(partition)
+      val partitions = partitionIndices.map(parent.partitions(_))
+      // A group of one partition names no location, so the scheduler uses all 
of that
+      // partition's locations. Otherwise prefer the location that the largest 
number of the

Review Comment:
   Done: each partition's hosts are deduplicated before counting, so a 
partition counts once per host. With (h1, h1, h1), (h2) and (h2), the group now 
picks h2, also in "the grouped coalescer prefers majority hosts and spreads 
ties".
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to