-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-14163][CORE] SumEvaluator and countApprox cannot reliably handle RDDs of size 1 #12016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
3b26826
3faecc4
93bcd99
5db9678
5e3c477
4040e0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,8 +29,9 @@ import org.apache.spark.util.StatCounter | |
| private[spark] class SumEvaluator(totalOutputs: Int, confidence: Double) | ||
| extends ApproximateEvaluator[StatCounter, BoundedDouble] { | ||
|
|
||
| // modified in merge | ||
| var outputsMerged = 0 | ||
| var counter = new StatCounter | ||
| val counter = new StatCounter | ||
|
|
||
| override def merge(outputId: Int, taskResult: StatCounter) { | ||
| outputsMerged += 1 | ||
|
|
@@ -40,30 +41,39 @@ private[spark] class SumEvaluator(totalOutputs: Int, confidence: Double) | |
| override def currentResult(): BoundedDouble = { | ||
| if (outputsMerged == totalOutputs) { | ||
| new BoundedDouble(counter.sum, 1.0, counter.sum, counter.sum) | ||
| } else if (outputsMerged == 0) { | ||
| } else if (outputsMerged == 0 || counter.count == 0) { | ||
| new BoundedDouble(0, 0.0, Double.NegativeInfinity, Double.PositiveInfinity) | ||
| } else { | ||
| val p = outputsMerged.toDouble / totalOutputs | ||
| val meanEstimate = counter.mean | ||
| val meanVar = counter.sampleVariance / counter.count | ||
| val countEstimate = (counter.count + 1 - p) / p | ||
| val countVar = (counter.count + 1) * (1 - p) / (p * p) | ||
| val sumEstimate = meanEstimate * countEstimate | ||
| val sumVar = (meanEstimate * meanEstimate * countVar) + | ||
| (countEstimate * countEstimate * meanVar) + | ||
| (meanVar * countVar) | ||
| val sumStdev = math.sqrt(sumVar) | ||
| val confFactor = { | ||
| if (counter.count > 100) { | ||
|
|
||
| val meanVar = counter.sampleVariance / counter.count | ||
|
|
||
| // branch at this point because counter.count == 1 implies counter.sampleVariance == Nan | ||
| // and we don't want to ever return a bound of NaN | ||
| if (meanVar == Double.NaN || counter.count == 1) { | ||
|
||
| new BoundedDouble(sumEstimate, confidence, Double.NegativeInfinity, Double.PositiveInfinity) | ||
| } else { | ||
| val countVar = (counter.count + 1) * (1 - p) / (p * p) | ||
| val sumVar = (meanEstimate * meanEstimate * countVar) + | ||
| (countEstimate * countEstimate * meanVar) + | ||
| (meanVar * countVar) | ||
| val sumStdev = math.sqrt(sumVar) | ||
| val confFactor = if (counter.count > 100) { | ||
| new NormalDistribution().inverseCumulativeProbability(1 - (1 - confidence) / 2) | ||
| } else { | ||
| } else if (counter.count > 1) { | ||
| val degreesOfFreedom = (counter.count - 1).toInt | ||
| new TDistribution(degreesOfFreedom).inverseCumulativeProbability(1 - (1 - confidence) / 2) | ||
| } else { | ||
| throw new Exception("Counter.count <= 1; this should be impossible at this point") | ||
|
||
| } | ||
|
|
||
| val low = sumEstimate - confFactor * sumStdev | ||
| val high = sumEstimate + confFactor * sumStdev | ||
| new BoundedDouble(sumEstimate, confidence, low, high) | ||
| } | ||
| val low = sumEstimate - confFactor * sumStdev | ||
| val high = sumEstimate + confFactor * sumStdev | ||
| new BoundedDouble(sumEstimate, confidence, low, high) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.partial | ||
|
|
||
|
|
||
| import org.apache.spark._ | ||
| import org.apache.spark.util.StatCounter | ||
|
|
||
| class SumEvaluatorSuite extends SparkFunSuite with SharedSparkContext { | ||
|
|
||
| test("correct handling of count 1") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While you're here throw in a basic test for count == 0 too, and ideally some normal-path case for completeness
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
|
||
| //setup | ||
| val counter = new StatCounter(List(2.0)) | ||
| // count of 10 because it's larger than 1, | ||
| // and 0.95 because that's the default | ||
| val evaluator = new SumEvaluator(10, 0.95) | ||
| // arbitrarily assign id 1 | ||
| evaluator.merge(1, counter) | ||
|
|
||
| //execute | ||
| val res = evaluator.currentResult() | ||
| // Build version with known precisions for equality check | ||
| val round_res = new BoundedDouble(res.mean.round.toDouble, res.confidence, res.low, res.high) | ||
|
||
|
|
||
| //Sanity check that equality works on BoundedDouble | ||
| assert(new BoundedDouble(2.0, 0.95, 1.1, 1.2) == new BoundedDouble(2.0, 0.95, 1.1, 1.2)) | ||
| // actual test | ||
|
|
||
| // 38.0 because that's how the maths shakes out | ||
| assert(round_res == new BoundedDouble(38.0, 0.950, Double.NegativeInfinity, Double.PositiveInfinity)) | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to say that this can become a case class to get this all for free, but it's part of the API. In theory that doesn't change anything, so you can try a case class and see if MiMa accepts it. Otherwise you can do this, but you'll need to fix up a few little style things. I'd not change the toString, hashCode might be too long a line, and indent on equals is too deep
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case classes bring extra concerns with binary compatibility (due to pattern matching). I'd minimize its use in public APIs.