Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1866#discussion_r16032172
  
    --- Diff: 
core/src/test/scala/org/apache/spark/rdd/PairRDDFunctionsSuite.scala ---
    @@ -556,6 +519,97 @@ class PairRDDFunctionsSuite extends FunSuite with 
SharedSparkContext {
         intercept[IllegalArgumentException] {shuffled.lookup(-1)}
       }
     
    +  private object StratifiedAuxiliary {
    +    def stratifier (fractionPositive: Double) = {
    +      (x: Int) => if (x % 10 < (10 * fractionPositive).toInt) "1" else "0"
    +    }
    +
    +    def checkSize(exact: Boolean,
    +        withReplacement: Boolean,
    +        expected: Long,
    +        actual: Long,
    +        p: Double): Boolean = {
    +      if (exact) {
    +        return expected == actual
    +      }
    +      val stdev = if (withReplacement) math.sqrt(expected) else 
math.sqrt(expected * p * (1 - p))
    +      // Very forgiving margin since we're dealing with very small sample 
sizes most of the time
    +      math.abs(actual - expected) <= 6 * stdev
    +    }
    +
    +    def testSampleExact(stratifiedData: RDD[(String, Int)],
    +        samplingRate: Double,
    +        seed: Long,
    +        n: Long) = {
    +      testBernoulli(stratifiedData, true, samplingRate, seed, n)
    +      testPoisson(stratifiedData, true, samplingRate, seed, n)
    +    }
    +
    +    def testSample(stratifiedData: RDD[(String, Int)],
    +        samplingRate: Double,
    +        seed: Long,
    +        n: Long) = {
    +      testBernoulli(stratifiedData, false, samplingRate, seed, n)
    +      testPoisson(stratifiedData, false, samplingRate, seed, n)
    +    }
    +
    +    // Without replacement validation
    +    def testBernoulli(stratifiedData: RDD[(String, Int)],
    +        exact: Boolean,
    +        samplingRate: Double,
    +        seed: Long,
    +        n: Long) = {
    +      val expectedSampleSize = stratifiedData.countByKey()
    +        .mapValues(count => math.ceil(count * samplingRate).toInt)
    +      val fractions = Map("1" -> samplingRate, "0" -> samplingRate)
    +      val sample = if (exact) {
    +        stratifiedData.sampleByKeyExact(false, fractions, seed)
    +      } else {
    +        stratifiedData.sampleByKey(false, fractions, seed)
    +      }
    +      val sampleCounts = sample.countByKey()
    +      val takeSample = sample.collect()
    +      sampleCounts.foreach { case(k, v) =>
    +        assert(checkSize(exact, false, expectedSampleSize(k), v, 
samplingRate)) }
    +      assert(takeSample.size === takeSample.toSet.size)
    +      takeSample.foreach { x => assert(1 <= x._2 && x._2 <= n, s"elements 
not in [1, $n]") }
    +    }
    +
    +    // With replacement validation
    +    def testPoisson(stratifiedData: RDD[(String, Int)],
    +        exact: Boolean,
    +        samplingRate: Double,
    +        seed: Long,
    +        n: Long) = {
    +      val expectedSampleSize = stratifiedData.countByKey().mapValues(count 
=>
    +        math.ceil(count * samplingRate).toInt)
    +      val fractions = Map("1" -> samplingRate, "0" -> samplingRate)
    +      val sample = if (exact) {
    +        stratifiedData.sampleByKeyExact(true, fractions, seed)
    +      } else {
    +        stratifiedData.sampleByKey(true, fractions, seed)
    +      }
    +      val sampleCounts = sample.countByKey()
    +      val takeSample = sample.collect()
    +      sampleCounts.foreach { case (k, v) =>
    +        assert(checkSize(exact, true, expectedSampleSize(k), v, 
samplingRate)) }
    +      val groupedByKey = takeSample.groupBy(_._1)
    +      for ((key, v) <- groupedByKey) {
    +        if (expectedSampleSize(key) >= 100 && samplingRate >= 0.1) {
    +          // sample large enough for there to be repeats with high 
likelihood
    +          assert(v.toSet.size < expectedSampleSize(key))
    +        } else {
    +          if (exact) {
    +            assert(v.toSet.size <= expectedSampleSize(key))
    +          } else {
    +            assert(checkSize(false, true, expectedSampleSize(key), 
v.toSet.size, samplingRate))
    +          }
    +        }
    +      }
    +      takeSample.foreach { x => assert(1 <= x._2 && x._2 <= n, s"elements 
not in [1, $n]") }
    --- End diff --
    
    minor: `takeSample.foreach(x => assert(1 <= x._2 && x._2 <= n, s"elements 
not in [1, $n]"))` We usually use `{ ... }` for pattern matching or multi-line 
statement.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to