spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hvanhovell <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-15324] [SQL] Add the takeSample functio...
Date Mon, 16 May 2016 20:52:35 GMT
Github user hvanhovell commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13116#discussion_r63423262
  
    --- Diff: sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala ---
    @@ -1494,6 +1493,56 @@ class Dataset[T] private[sql](
       }
     
       /**
    +   * Returns a new [[Dataset]] by sampling a fixed num of rows, using a random seed.
    +   *
    +   * @param withReplacement Sample with replacement or not.
    +   * @param num num of rows to generate.
    +   * @param seed Seed for sampling
    +   * @return the sampling [[Dataset]]
    +   */
    +  def takeSample(withReplacement: Boolean, num: Int, seed: Long): Dataset[T] = {
    +    val numStDev = 10.0
    +    require(num >= 0, "Negative number of elements requested")
    +    require(num <= (Int.MaxValue - (numStDev * math.sqrt(Int.MaxValue)).toInt),
    +      "Cannot support a sample size > Int.MaxValue - " +
    +        s"$numStDev * math.sqrt(Int.MaxValue)")
    +    if (num == 0) {
    +      sqlContext.createDataset[T](Seq())
    +    } else {
    +      val initialCount = this.count()
    +      if (initialCount == 0) {
    +        sqlContext.createDataset[T](Seq())
    +      } else {
    +        val rand = new Random(seed)
    +        if (!withReplacement && num >= initialCount) {
    +          sqlContext.createDataset[T](Utils.randomizeInPlace(this.collect(), rand))
    +        } else {
    +          val fraction = SamplingUtils.computeFractionForSampleSize(num, initialCount,
    +            withReplacement)
    +          var samples = this.sample(withReplacement, fraction, rand.nextInt()).collect()
    +          var numIters = 0
    +          while (samples.length < num) {
    +            samples = this.sample(withReplacement, fraction, rand.nextInt()).collect()
    +            numIters += 1
    +          }
    +          sqlContext.createDataset[T](Utils.randomizeInPlace(samples, rand).take(num))
    --- End diff --
    
    potential OOME


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message