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

    https://github.com/apache/spark/pull/13116#discussion_r63422967
  
    --- 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) {
    --- End diff --
    
    Infinite loop if the user requests more rows than in the dataset.


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

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

Reply via email to