Skip to content

Commit fe15ea9

Browse files
committed
SPARK-6480 [CORE] histogram() bucket function is wrong in some simple edge cases
Fix fastBucketFunction for histogram() to handle edge conditions more correctly. Add a test, and fix existing one accordingly Author: Sean Owen <sowen@cloudera.com> Closes apache#5148 from srowen/SPARK-6480 and squashes the following commits: 974a0a0 [Sean Owen] Additional test of huge ranges, and a few more comments (and comment fixes) 23ec01e [Sean Owen] Fix fastBucketFunction for histogram() to handle edge conditions more correctly. Add a test, and fix existing one accordingly
1 parent 3ddb975 commit fe15ea9

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,23 @@ class DoubleRDDFunctions(self: RDD[Double]) extends Logging with Serializable {
191191
}
192192
}
193193
// Determine the bucket function in constant time. Requires that buckets are evenly spaced
194-
def fastBucketFunction(min: Double, increment: Double, count: Int)(e: Double): Option[Int] = {
194+
def fastBucketFunction(min: Double, max: Double, count: Int)(e: Double): Option[Int] = {
195195
// If our input is not a number unless the increment is also NaN then we fail fast
196-
if (e.isNaN()) {
197-
return None
198-
}
199-
val bucketNumber = (e - min)/(increment)
200-
// We do this rather than buckets.lengthCompare(bucketNumber)
201-
// because Array[Double] fails to override it (for now).
202-
if (bucketNumber > count || bucketNumber < 0) {
196+
if (e.isNaN || e < min || e > max) {
203197
None
204198
} else {
205-
Some(bucketNumber.toInt.min(count - 1))
199+
// Compute ratio of e's distance along range to total range first, for better precision
200+
val bucketNumber = (((e - min) / (max - min)) * count).toInt
201+
// should be less than count, but will equal count if e == max, in which case
202+
// it's part of the last end-range-inclusive bucket, so return count-1
203+
Some(math.min(bucketNumber, count - 1))
206204
}
207205
}
208206
// Decide which bucket function to pass to histogramPartition. We decide here
209-
// rather than having a general function so that the decission need only be made
207+
// rather than having a general function so that the decision need only be made
210208
// once rather than once per shard
211209
val bucketFunction = if (evenBuckets) {
212-
fastBucketFunction(buckets(0), buckets(1)-buckets(0), buckets.length-1) _
210+
fastBucketFunction(buckets.head, buckets.last, buckets.length - 1) _
213211
} else {
214212
basicBucketFunction _
215213
}

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ class DoubleRDDSuite extends FunSuite with SharedSparkContext {
235235
assert(histogramBuckets === expectedHistogramBuckets)
236236
}
237237

238+
test("WorksWithDoubleValuesAtMinMax") {
239+
val rdd = sc.parallelize(Seq(1, 1, 1, 2, 3, 3))
240+
assert(Array(3, 0, 1, 2) === rdd.map(_.toDouble).histogram(4)._2)
241+
assert(Array(3, 1, 2) === rdd.map(_.toDouble).histogram(3)._2)
242+
}
243+
238244
test("WorksWithoutBucketsWithMoreRequestedThanElements") {
239245
// Verify the basic case of one bucket and all elements in that bucket works
240246
val rdd = sc.parallelize(Seq(1, 2))
@@ -248,7 +254,7 @@ class DoubleRDDSuite extends FunSuite with SharedSparkContext {
248254
}
249255

250256
test("WorksWithoutBucketsForLargerDatasets") {
251-
// Verify the case of slighly larger datasets
257+
// Verify the case of slightly larger datasets
252258
val rdd = sc.parallelize(6 to 99)
253259
val (histogramBuckets, histogramResults) = rdd.histogram(8)
254260
val expectedHistogramResults =
@@ -259,17 +265,27 @@ class DoubleRDDSuite extends FunSuite with SharedSparkContext {
259265
assert(histogramBuckets === expectedHistogramBuckets)
260266
}
261267

262-
test("WorksWithoutBucketsWithIrrationalBucketEdges") {
263-
// Verify the case of buckets with irrational edges. See #SPARK-2862.
268+
test("WorksWithoutBucketsWithNonIntegralBucketEdges") {
269+
// Verify the case of buckets with nonintegral edges. See #SPARK-2862.
264270
val rdd = sc.parallelize(6 to 99)
265271
val (histogramBuckets, histogramResults) = rdd.histogram(9)
272+
// Buckets are 6.0, 16.333333333333336, 26.666666666666668, 37.0, 47.333333333333336 ...
266273
val expectedHistogramResults =
267-
Array(11, 10, 11, 10, 10, 11, 10, 10, 11)
274+
Array(11, 10, 10, 11, 10, 10, 11, 10, 11)
268275
assert(histogramResults === expectedHistogramResults)
269276
assert(histogramBuckets(0) === 6.0)
270277
assert(histogramBuckets(9) === 99.0)
271278
}
272279

280+
test("WorksWithHugeRange") {
281+
val rdd = sc.parallelize(Array(0, 1.0e24, 1.0e30))
282+
val histogramResults = rdd.histogram(1000000)._2
283+
assert(histogramResults(0) === 1)
284+
assert(histogramResults(1) === 1)
285+
assert(histogramResults.last === 1)
286+
assert((2 to histogramResults.length - 2).forall(i => histogramResults(i) == 0))
287+
}
288+
273289
// Test the failure mode with an invalid RDD
274290
test("ThrowsExceptionOnInvalidRDDs") {
275291
// infinity

0 commit comments

Comments
 (0)