Github user andrewor14 commented on a diff in the pull request:
https://github.com/apache/spark/pull/8730#discussion_r39450667
--- Diff: core/src/main/scala/org/apache/spark/rdd/RDD.scala ---
@@ -469,50 +469,40 @@ abstract class RDD[T: ClassTag](
* @param seed seed for the random number generator
* @return sample of specified size in an array
*/
- // TODO: rewrite this without return statements so we can wrap it in a
scope
def takeSample(
withReplacement: Boolean,
num: Int,
- seed: Long = Utils.random.nextLong): Array[T] = {
+ seed: Long = Utils.random.nextLong): Array[T] = withScope {
val numStDev = 10.0
- if (num < 0) {
- throw new IllegalArgumentException("Negative number of elements
requested")
- } else if (num == 0) {
- return new Array[T](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)")
val initialCount = this.count()
- if (initialCount == 0) {
- return new Array[T](0)
- }
-
- val maxSampleSize = Int.MaxValue - (numStDev *
math.sqrt(Int.MaxValue)).toInt
- if (num > maxSampleSize) {
- throw new IllegalArgumentException("Cannot support a sample size >
Int.MaxValue - " +
- s"$numStDev * math.sqrt(Int.MaxValue)")
- }
-
- val rand = new Random(seed)
- if (!withReplacement && num >= initialCount) {
- return Utils.randomizeInPlace(this.collect(), rand)
- }
-
- val fraction = SamplingUtils.computeFractionForSampleSize(num,
initialCount,
- withReplacement)
-
- var samples = this.sample(withReplacement, fraction,
rand.nextInt()).collect()
-
- // If the first sample didn't turn out large enough, keep trying to
take samples;
- // this shouldn't happen often because we use a big multiplier for the
initial size
- var numIters = 0
- while (samples.length < num) {
- logWarning(s"Needed to re-sample due to insufficient sample size.
Repeat #$numIters")
- samples = this.sample(withReplacement, fraction,
rand.nextInt()).collect()
- numIters += 1
+ if (num == 0 || initialCount == 0 ) {
--- End diff --
no space before )
---
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]