-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-10575][Spark Core]Wrapped RDD.takeSample with Scope #8730
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cf2c466
wrapped in scope
vinodkc c82b4f9
Added assert instead of IAE
vinodkc afe9396
fixed condition
vinodkc c06c6e5
Removed space
vinodkc 7fa497d
Update RDD.scala
vinodkc b37778e
Fixed Review Comment
vinodkc 4e6ce62
Update RDD.scala
vinodkc 70de2b4
Handled review comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ) { | ||
| new Array[T](0) | ||
|
Contributor
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. Hm, the only difference here is that if |
||
| } else { | ||
| val rand = new Random(seed) | ||
| if (!withReplacement && num >= initialCount) { | ||
| Utils.randomizeInPlace(this.collect(), rand) | ||
| } else { | ||
| 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 | ||
| } | ||
| Utils.randomizeInPlace(samples, rand).take(num) | ||
| } | ||
| } | ||
|
|
||
| Utils.randomizeInPlace(samples, rand).take(num) | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
no space before )