Skip to content

Commit f1708c9

Browse files
author
Erik van Oosten
committed
Fix for sum on empty RDD fails with exception (SPARK-6878)
1 parent 9d117ce commit f1708c9

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818
package org.apache.spark.rdd
1919

2020
import org.scalatest.FunSuite
21+
import org.scalatest.Matchers._
2122

2223
import org.apache.spark._
2324

2425
class DoubleRDDSuite extends FunSuite with SharedSparkContext {
26+
test("sum") {
27+
sc.parallelize(Seq.empty[Double]).sum() should be(0.0 +- 0.0001)
28+
sc.parallelize(Seq(1.0)).sum() should be(1.0 +- 0.0001)
29+
sc.parallelize(Seq(1.0, 2.0)).sum() should be(3.0 +- 0.0001)
30+
}
31+
2532
// Verify tests on the histogram functionality. We test with both evenly
2633
// and non-evenly spaced buckets as the bucket lookup function changes.
2734
test("WorksOnEmpty") {

0 commit comments

Comments
 (0)