title: How is `sample` different from `TABLESAMPLE` in Spark?
body: <p>I'd like to get create a random sub-sample of my data.</p>
  <ol>
  <li>Spark's <code>sample</code> function (<a href="https://spark.apache.org/docs/3.2.1/api/scala/org/apache/spark/api/java/JavaHadoopRDD.html#sample(withReplacement:Boolean,fraction:Double):org.apache.spark.api.java.JavaPairRDD%5BK,V%5D" rel="nofollow noreferrer">link</a>) is the API I'd like to use. Particularly, because it allows me to toggle if the sampling is done with or without replacement. However, executing this function takes a long time. Based on the answers from this question <a href="https://stackoverflow.com/questions/50004006/spark-sample-is-too-slow">Spark sample is too slow</a>, it seems like <code>sample</code> requires a full table scan.</li>
  <li>TABLESAMPLE seems like a faster alternative, albeit, the ability to toggle with and without replacement is lost.</li>
  </ol>
  <p>I'd like to understand how <code>sample</code> and TABLESAMPLE are different, and why does TABLESAMPLE execute faster than <code>sample</code>. Could it be that TABLESAMPLE does not require a full table scan?</p>