peter-toth commented on code in PR #60256:
URL: https://github.com/apache/spark/pull/60256#discussion_r4538351675


##########
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:
   **Finding 1.** `parent.preferredLocations` returns only the parent RDD's own 
locations. Over a scan, `GroupPartitionsExec` coalesces `child.execute()`, 
which is `inputRDD.map {...}` (`DataSourceV2ScanExecBase.scala:219` and 
`:227`). That map reports none. So every group there still gets `None`, and the 
new majority and tie rules never run. The description says as much, so the 
location half of the change only reaches parents that report locations 
themselves.
   
   `DefaultPartitionCoalescer.currPrefLocs` resolves locations through the 
lineage instead (`CoalescedRDD.scala:181`). With the same lookup here, a group 
over the mapped RDD from your second test prefers its majority host:
   
   ```scala
           partitions.flatMap(p => parent.context.getPreferredLocs(parent, 
p.index).map(_.host))
             .groupBy(identity).view.mapValues(_.size)
   ```
   
   | group (0, 1, 2) on h2, h1, h1, over `source.map(identity)` | scheduled on |
   |---|---|
   | this head | h2, the first input |
   | with the change above | h1 |
   
   With the change, `GroupPartitionsExecSuite` passes 39 of 40. The one failure 
is "groups over a parent without locations take their first input's location", 
which pins the old fallback. The change also drops null entries, since 
`getPreferredLocs` filters them (finding 3).
   
   This also matters for the reuse in #59522: a packing node over a scan's 
mapped RDD would see no locations either. If the change is kept to parents with 
their own locations, could the title and the scaladoc say so?
   



##########
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 {

Review Comment:
   **Finding 2.** A group of one partition now names no location. That holds up 
only where something walks the narrow dependency to its parent. 
`ZippedPartitionsBaseRDD` does not: it reads each side's `preferredLocations` 
directly and keeps their intersection, or else their union 
(`ZippedPartitionsRDD.scala:59-63`). Whole-stage codegen zips the two grouped 
sides of a storage-partitioned join this way. So a single-partition side now 
drops out of the zipped task's locations whenever the other side names one.
   
   What I ran: one side is a group of one partition on h1, h2, the other a 
group of two partitions on h3, both through `GroupedPartitionCoalescer`, then 
`zipPartitions`:
   
   | | zipped locations | the single side alone |
   |---|---|---|
   | base | h2, h3 | h2 |
   | this head | h3 | h1, h2 |
   
   Over a scan neither side reports locations, so this needs a parent with 
locations of its own, such as a keyed shuffle stage.
   
   Two ways out:
   - Keep naming a location for a group of one, which the tie-spreading now 
picks.
   - Or narrow the comment and the description to the paths that walk the 
dependency.
   



##########
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:
   **Finding 3.** The tie-break orders by the location string. When a null 
entry ties another location on count and on `assigned`, `Ordering.String` 
throws. The old `maxByOption(_._2)` compared counts only. 
`DAGScheduler.getPreferredLocsInternal` filters `_ != null`, so core does 
expect nulls from `preferredLocations`.
   
   What I ran: a group of two partitions that both report `(null, "h1")`.
   - The base returns `Some(null)`.
   - This head throws `NullPointerException: Cannot read field "value" because 
"anotherString" is null` from `coalesce`.
   
   No in-tree parent of this node reports a null, so this is latent. Filtering 
nulls before counting closes it, and so does the lookup in finding 1. With the 
change below, the group gets `Some(h1)` and `GroupPartitionsExecSuite` passes, 
40 of 40:
   
   ```suggestion
           partitions.flatMap(parent.preferredLocations).filter(_ != 
null).groupBy(identity)
             .view.mapValues(_.size)
   ```
   



##########
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:
   **Finding 4.** The comment says "the location that the largest number of the 
group's partitions report", but the code counts location entries. A partition 
that lists a host more than once outvotes the others. One example is a 
`ShuffledRowRDD` partition that spans several reducers.
   
   With partitions on (h1, h1, h1), (h2) and (h2), this picks h1, although two 
of the three partitions prefer h2. The base counted the same way. Taking 
`.distinct` per partition would match the comment: `partitions.flatMap(p => 
parent.preferredLocations(p).distinct)`.
   



-- 
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