@@ -310,6 +310,9 @@ abstract class RDD[T: ClassTag](
310310 * Return a sampled subset of this RDD.
311311 */
312312 def sample (withReplacement : Boolean , fraction : Double , seed : Int ): RDD [T ] = {
313+ if (fraction < Double .MinValue || fraction > Double .MaxValue ) {
314+ throw new Exception (" Invalid fraction value:" + fraction)
315+ }
313316 if (withReplacement) {
314317 new PartitionwiseSampledRDD [T , T ](this , new PoissonSampler [T ](fraction), seed)
315318 } else {
@@ -344,6 +347,10 @@ abstract class RDD[T: ClassTag](
344347 throw new IllegalArgumentException (" Negative number of elements requested" )
345348 }
346349
350+ if (initialCount == 0 ) {
351+ return new Array [T ](0 )
352+ }
353+
347354 if (initialCount > Integer .MAX_VALUE - 1 ) {
348355 maxSelected = Integer .MAX_VALUE - 1
349356 } else {
@@ -362,7 +369,7 @@ abstract class RDD[T: ClassTag](
362369 var samples = this .sample(withReplacement, fraction, rand.nextInt()).collect()
363370
364371 // If the first sample didn't turn out large enough, keep trying to take samples;
365- // this shouldn't happen often because we use a big multiplier for thei initial size
372+ // this shouldn't happen often because we use a big multiplier for the initial size
366373 while (samples.length < total) {
367374 samples = this .sample(withReplacement, fraction, rand.nextInt()).collect()
368375 }
0 commit comments