Github user davies commented on a diff in the pull request: https://github.com/apache/spark/pull/11327#discussion_r61801132 --- Diff: core/src/main/scala/org/apache/spark/rdd/CoalescedRDD.scala --- @@ -334,8 +332,41 @@ private class DefaultPartitionCoalescer(val balanceSlack: Double = 0.10) } } } else { + // It is possible to have unionRDD where one rdd has preferred locations and another rdd + // that doesn't. To make sure we end up with the requested number of partitions, + // make sure to put a partition in every group. + + if (groupArr.size > initialHash.size) { + // we don't have a partition assigned to every group yet so first try to fill them + // with the partitions with preferred locations + val partIter = partitionLocs.partsWithLocs.iterator + while (partIter.hasNext && initialHash.size < groupArr.size) { + var (nxt_replica, nxt_part) = partIter.next() + if (!initialHash.contains(nxt_part)) { + groupArr.find(pg => pg.numPartitions == 0).map(firstEmpty => { + firstEmpty.partitions += nxt_part + initialHash += nxt_part + }) + } + } + } + + // if we didn't get one partitions per group from partitions with preferred locations + // use partitions without preferred locations + val partNoLocIter = partitionLocs.partsWithoutLocs.iterator + while (partNoLocIter.hasNext && initialHash.size < groupArr.size) { + var nxt_part = partNoLocIter.next() + if (!initialHash.contains(nxt_part)) { + groupArr.find(pg => pg.numPartitions == 0).map(firstEmpty => { --- End diff -- This is still O(N*N) (the worst), it could be ``` groupArr.filter(pg => pg.numPartitions == 0).foreach { pg => while (partNoLocIter && pg.numPartitions == 0) { if (!initialHash.contains(nxt_part)) { pg.partitions += nxt_part initialHash += nxt_part } } } ```
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org