-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-1540] Add an optional Ordering parameter to PairRDDFunctions.
In https://issues.apache.org/jira/browse/SPARK-1540 we'd like to look at Spark's API to see if we can take advantage of Comparable keys in more places, which will make external spilling more efficient. This PR is a first step towards that that shows how to pass an Ordering when available and still continue functioning otherwise. It does this using a new implicit parameter with a default value of null. The API is currently only in Scala -- in Java we'd have to add new versions of mapToPair and such that take a Comparator, or a new method to add a "type hint" to an RDD. We can address those later though. Unfortunately requiring all keys to be Comparable would not work without requiring RDDs in general to contain only Comparable types. The reason is that methods such as distinct() and intersection() do a shuffle, but should be usable on RDDs of any type. So ordering will have to remain an optimization for the types that can be ordered. I think this isn't a horrible outcome though because one of the nice things about Spark's API is that it works on objects of *any* type, without requiring you to specify a schema or implement Writable or stuff like that. Author: Matei Zaharia <[email protected]> This patch had conflicts when merged, resolved by Committer: Reynold Xin <[email protected]> Closes #487 from mateiz/ordered-keys and squashes the following commits: bd565f6 [Matei Zaharia] Pass an Ordering to only one version of groupBy because the Scala language spec doesn't allow having an optional parameter on all of them (this was only compiling in Scala 2.10 due to a bug). 4629965 [Matei Zaharia] Add tests for other versions of groupBy 3beae85 [Matei Zaharia] Added a test for implicit orderings 80b7a3b [Matei Zaharia] Add an optional Ordering parameter to PairRDDFunctions.
- Loading branch information
Showing
8 changed files
with
124 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
core/src/test/scala/org/apache/spark/ImplicitOrderingSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark | ||
|
||
import org.scalatest.FunSuite | ||
|
||
import org.apache.spark.SparkContext._ | ||
|
||
class ImplicitOrderingSuite extends FunSuite with LocalSparkContext { | ||
class NonOrderedClass {} | ||
|
||
class ComparableClass extends Comparable[ComparableClass] { | ||
override def compareTo(o: ComparableClass): Int = ??? | ||
} | ||
|
||
class OrderedClass extends Ordered[OrderedClass] { | ||
override def compare(o: OrderedClass): Int = ??? | ||
} | ||
|
||
// Tests that PairRDDFunctions grabs an implicit Ordering in various cases where it should. | ||
test("basic inference of Orderings"){ | ||
sc = new SparkContext("local", "test") | ||
val rdd = sc.parallelize(1 to 10) | ||
|
||
// Infer orderings after basic maps to particular types | ||
assert(rdd.map(x => (x, x)).keyOrdering.isDefined) | ||
assert(rdd.map(x => (1, x)).keyOrdering.isDefined) | ||
assert(rdd.map(x => (x.toString, x)).keyOrdering.isDefined) | ||
assert(rdd.map(x => (null, x)).keyOrdering.isDefined) | ||
assert(rdd.map(x => (new NonOrderedClass, x)).keyOrdering.isEmpty) | ||
assert(rdd.map(x => (new ComparableClass, x)).keyOrdering.isDefined) | ||
assert(rdd.map(x => (new OrderedClass, x)).keyOrdering.isDefined) | ||
|
||
// Infer orderings for other RDD methods | ||
assert(rdd.groupBy(x => x).keyOrdering.isDefined) | ||
assert(rdd.groupBy(x => new NonOrderedClass).keyOrdering.isEmpty) | ||
assert(rdd.groupBy(x => new ComparableClass).keyOrdering.isDefined) | ||
assert(rdd.groupBy(x => new OrderedClass).keyOrdering.isDefined) | ||
assert(rdd.groupBy((x: Int) => x, 5).keyOrdering.isDefined) | ||
assert(rdd.groupBy((x: Int) => x, new HashPartitioner(5)).keyOrdering.isDefined) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.