Github user mateiz commented on a diff in the pull request:

    https://github.com/apache/spark/pull/931#discussion_r15038532
  
    --- Diff: 
core/src/main/scala/org/apache/spark/rdd/OrderedRDDFunctions.scala ---
    @@ -56,15 +62,56 @@ class OrderedRDDFunctions[K : Ordering : ClassTag,
        * order of the keys).
        */
       def sortByKey(ascending: Boolean = true, numPartitions: Int = 
self.partitions.size): RDD[P] = {
    +    val externalSorting = 
SparkEnv.get.conf.getBoolean("spark.shuffle.spill", true)
         val part = new RangePartitioner(numPartitions, self, ascending)
         val shuffled = new ShuffledRDD[K, V, P](self, part)
    -    shuffled.mapPartitions(iter => {
    -      val buf = iter.toArray
    +    if (!externalSorting) {
    +      shuffled.mapPartitions(iter => {
    +        val buf = iter.toArray
    +        if (ascending) {
    +          buf.sortWith((x, y) => ordering.lt(x._1, y._1)).iterator
    +        } else {
    +          buf.sortWith((x, y) => ordering.gt(x._1, y._1)).iterator
    +        }
    +      }, preservesPartitioning = true)
    +    } else {
    +      shuffled.mapPartitions(iter => {
    +        val map = createExternalMap(ascending)
    +        while (iter.hasNext) { 
    +          val kv = iter.next()
    +          map.insert(kv._1, kv.asInstanceOf[P])
    +        }
    +        map.sortIterator
    +      }).flatMap(elem => elem._2)
    +    }
    +  }
    +
    +  private def createExternalMap(ascending: Boolean): 
ExternalAppendOnlyMap[K, P, SortCombiner] = {
    --- End diff --
    
    It seems unnecessary to have a combiner here: if there are multiple 
key-value pairs with the same key, this requires them to all fit in memory. 
Instead we should have an option for the ExternalAppendOnlyMap to not attempt 
to combine them. I'll work on this in my PR.


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

Reply via email to