Skip to content

Commit 0d05b96

Browse files
committed
Still testing treeAggregate implementations
1 parent 93e3cb3 commit 0d05b96

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

core/src/main/scala/org/apache/spark/rdd/RDD.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ abstract class RDD[T: ClassTag](
10751075
val cleanCombOp = context.clean(combOp)
10761076
val aggregatePartition =
10771077
(it: Iterator[T]) => it.aggregate(zeroValue)(cleanSeqOp, cleanCombOp)
1078-
var partiallyAggregated = mapPartitions(it => Iterator(aggregatePartition(it)))
1078+
var partiallyAggregated: RDD[U] = mapPartitions(it => Iterator(aggregatePartition(it)))
10791079
var numPartitions = partiallyAggregated.partitions.length
10801080
val scale = math.max(math.ceil(math.pow(numPartitions, 1.0 / depth)).toInt, 2)
10811081
// If creating an extra level doesn't help reduce
@@ -1087,9 +1087,13 @@ abstract class RDD[T: ClassTag](
10871087
val curNumPartitions = numPartitions
10881088
partiallyAggregated = partiallyAggregated.mapPartitionsWithIndex {
10891089
(i, iter) => iter.map((i % curNumPartitions, _))
1090-
}.foldByKey(zeroValue, new HashPartitioner(curNumPartitions))(cleanCombOp).values
1090+
}.reduceByKey(new HashPartitioner(curNumPartitions), cleanCombOp).values
1091+
// This fails:
1092+
// .foldByKey(zeroValue, new HashPartitioner(curNumPartitions))(cleanCombOp).values
10911093
}
1092-
partiallyAggregated.fold(zeroValue)(cleanCombOp)
1094+
partiallyAggregated.reduce(cleanCombOp)
1095+
// This fails:
1096+
// partiallyAggregated.fold(zeroValue)(cleanCombOp)
10931097
}
10941098
}
10951099

core/src/test/scala/org/apache/spark/rdd/RDDSuite.scala

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ class RDDSuite extends SparkFunSuite with SharedSparkContext {
159159
assert(ser.serialize(union.partitions.head).limit() < 2000)
160160
}
161161

162+
test("fold") {
163+
val rdd = sc.makeRDD(-1000 until 1000, 10)
164+
def op: (Int, Int) => Int = (c: Int, x: Int) => c + x
165+
val sum = rdd.fold(0)(op)
166+
assert(sum === -1000)
167+
}
168+
169+
test("fold with op modifying first arg") {
170+
val rdd = sc.makeRDD(-1000 until 1000, 10).map(x => Array(x))
171+
def op: (Array[Int], Array[Int]) => Array[Int] = { (c: Array[Int], x: Array[Int]) =>
172+
c(0) += x(0)
173+
c
174+
}
175+
val sum = rdd.fold(Array(0))(op)
176+
}
177+
162178
test("aggregate") {
163179
val pairs = sc.makeRDD(Array(("a", 1), ("b", 2), ("a", 2), ("c", 5), ("a", 3)))
164180
type StringMap = HashMap[String, Int]
@@ -185,7 +201,19 @@ class RDDSuite extends SparkFunSuite with SharedSparkContext {
185201
def combOp: (Long, Long) => Long = (c1: Long, c2: Long) => c1 + c2
186202
for (depth <- 1 until 10) {
187203
val sum = rdd.treeAggregate(0L)(seqOp, combOp, depth)
188-
assert(sum === -1000L)
204+
assert(sum === -1000)
205+
}
206+
}
207+
208+
test("treeAggregate with ops modifying first args") {
209+
val rdd = sc.makeRDD(-1000 until 1000, 10).map(x => Array(x))
210+
def op: (Array[Int], Array[Int]) => Array[Int] = { (c: Array[Int], x: Array[Int]) =>
211+
c(0) += x(0)
212+
c
213+
}
214+
for (depth <- 1 until 10) {
215+
val sum = rdd.treeAggregate(Array(0))(op, op, depth)
216+
assert(sum === -1000)
189217
}
190218
}
191219

0 commit comments

Comments
 (0)