@@ -19,9 +19,29 @@ package org.apache.spark
19
19
20
20
import org .scalatest .FunSuite
21
21
22
+ import org .apache .spark .rdd .RDD
22
23
import org .apache .spark .SparkContext ._
23
24
24
25
class ImplicitOrderingSuite extends FunSuite with LocalSparkContext {
26
+ // Tests that PairRDDFunctions grabs an implicit Ordering in various cases where it should.
27
+ test(" basic inference of Orderings" ){
28
+ sc = new SparkContext (" local" , " test" )
29
+ val rdd = sc.parallelize(1 to 10 )
30
+
31
+ // These RDD methods are in the companion object so that the unserializable ScalaTest Engine
32
+ // won't be reachable from the closure object
33
+
34
+ // Infer orderings after basic maps to particular types
35
+ val basicMapExpectations = ImplicitOrderingSuite .basicMapExpectations(rdd)
36
+ basicMapExpectations.map({case (met, explain) => assert(met, explain)})
37
+
38
+ // Infer orderings for other RDD methods
39
+ val otherRDDMethodExpectations = ImplicitOrderingSuite .otherRDDMethodExpectations(rdd)
40
+ otherRDDMethodExpectations.map({case (met, explain) => assert(met, explain)})
41
+ }
42
+ }
43
+
44
+ private object ImplicitOrderingSuite {
25
45
class NonOrderedClass {}
26
46
27
47
class ComparableClass extends Comparable [ComparableClass ] {
@@ -31,27 +51,36 @@ class ImplicitOrderingSuite extends FunSuite with LocalSparkContext {
31
51
class OrderedClass extends Ordered [OrderedClass ] {
32
52
override def compare (o : OrderedClass ): Int = ???
33
53
}
34
-
35
- // Tests that PairRDDFunctions grabs an implicit Ordering in various cases where it should.
36
- test(" basic inference of Orderings" ){
37
- sc = new SparkContext (" local" , " test" )
38
- val rdd = sc.parallelize(1 to 10 )
39
-
40
- // Infer orderings after basic maps to particular types
41
- assert(rdd.map(x => (x, x)).keyOrdering.isDefined)
42
- assert(rdd.map(x => (1 , x)).keyOrdering.isDefined)
43
- assert(rdd.map(x => (x.toString, x)).keyOrdering.isDefined)
44
- assert(rdd.map(x => (null , x)).keyOrdering.isDefined)
45
- assert(rdd.map(x => (new NonOrderedClass , x)).keyOrdering.isEmpty)
46
- assert(rdd.map(x => (new ComparableClass , x)).keyOrdering.isDefined)
47
- assert(rdd.map(x => (new OrderedClass , x)).keyOrdering.isDefined)
48
-
49
- // Infer orderings for other RDD methods
50
- assert(rdd.groupBy(x => x).keyOrdering.isDefined)
51
- assert(rdd.groupBy(x => new NonOrderedClass ).keyOrdering.isEmpty)
52
- assert(rdd.groupBy(x => new ComparableClass ).keyOrdering.isDefined)
53
- assert(rdd.groupBy(x => new OrderedClass ).keyOrdering.isDefined)
54
- assert(rdd.groupBy((x : Int ) => x, 5 ).keyOrdering.isDefined)
55
- assert(rdd.groupBy((x : Int ) => x, new HashPartitioner (5 )).keyOrdering.isDefined)
54
+
55
+ def basicMapExpectations (rdd : RDD [Int ]) = {
56
+ List ((rdd.map(x => (x, x)).keyOrdering.isDefined,
57
+ " rdd.map(x => (x, x)).keyOrdering.isDefined" ),
58
+ (rdd.map(x => (1 , x)).keyOrdering.isDefined,
59
+ " rdd.map(x => (1, x)).keyOrdering.isDefined" ),
60
+ (rdd.map(x => (x.toString, x)).keyOrdering.isDefined,
61
+ " rdd.map(x => (x.toString, x)).keyOrdering.isDefined" ),
62
+ (rdd.map(x => (null , x)).keyOrdering.isDefined,
63
+ " rdd.map(x => (null, x)).keyOrdering.isDefined" ),
64
+ (rdd.map(x => (new NonOrderedClass , x)).keyOrdering.isEmpty,
65
+ " rdd.map(x => (new NonOrderedClass, x)).keyOrdering.isEmpty" ),
66
+ (rdd.map(x => (new ComparableClass , x)).keyOrdering.isDefined,
67
+ " rdd.map(x => (new ComparableClass, x)).keyOrdering.isDefined" ),
68
+ (rdd.map(x => (new OrderedClass , x)).keyOrdering.isDefined,
69
+ " rdd.map(x => (new OrderedClass, x)).keyOrdering.isDefined" ))
56
70
}
57
- }
71
+
72
+ def otherRDDMethodExpectations (rdd : RDD [Int ]) = {
73
+ List ((rdd.groupBy(x => x).keyOrdering.isDefined,
74
+ " rdd.groupBy(x => x).keyOrdering.isDefined" ),
75
+ (rdd.groupBy(x => new NonOrderedClass ).keyOrdering.isEmpty,
76
+ " rdd.groupBy(x => new NonOrderedClass).keyOrdering.isEmpty" ),
77
+ (rdd.groupBy(x => new ComparableClass ).keyOrdering.isDefined,
78
+ " rdd.groupBy(x => new ComparableClass).keyOrdering.isDefined" ),
79
+ (rdd.groupBy(x => new OrderedClass ).keyOrdering.isDefined,
80
+ " rdd.groupBy(x => new OrderedClass).keyOrdering.isDefined" ),
81
+ (rdd.groupBy((x : Int ) => x, 5 ).keyOrdering.isDefined,
82
+ " rdd.groupBy((x: Int) => x, 5).keyOrdering.isDefined" ),
83
+ (rdd.groupBy((x : Int ) => x, new HashPartitioner (5 )).keyOrdering.isDefined,
84
+ " rdd.groupBy((x: Int) => x, new HashPartitioner(5)).keyOrdering.isDefined" ))
85
+ }
86
+ }
0 commit comments