Skip to content

Commit 51b306b

Browse files
Erik van Oostensrowen
authored andcommitted
SPARK-6878 [CORE] Fix for sum on empty RDD fails with exception
Author: Erik van Oosten <[email protected]> Closes #5489 from erikvanoosten/master and squashes the following commits: 1c91954 [Erik van Oosten] Rewrote double range matcher to an exact equality assert (SPARK-6878) f1708c9 [Erik van Oosten] Fix for sum on empty RDD fails with exception (SPARK-6878)
1 parent 628a72f commit 51b306b

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.apache.spark.util.StatCounter
3131
class DoubleRDDFunctions(self: RDD[Double]) extends Logging with Serializable {
3232
/** Add up the elements in this RDD. */
3333
def sum(): Double = {
34-
self.reduce(_ + _)
34+
self.fold(0.0)(_ + _)
3535
}
3636

3737
/**

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ import org.scalatest.FunSuite
2222
import org.apache.spark._
2323

2424
class DoubleRDDSuite extends FunSuite with SharedSparkContext {
25+
test("sum") {
26+
assert(sc.parallelize(Seq.empty[Double]).sum() === 0.0)
27+
assert(sc.parallelize(Seq(1.0)).sum() === 1.0)
28+
assert(sc.parallelize(Seq(1.0, 2.0)).sum() === 3.0)
29+
}
30+
2531
// Verify tests on the histogram functionality. We test with both evenly
2632
// and non-evenly spaced buckets as the bucket lookup function changes.
2733
test("WorksOnEmpty") {

0 commit comments

Comments
 (0)