title: How does Round Robin partitioning in Spark work?
body: <p>I've trouble to understand Round Robin Partitioning in Spark. Consider the following exampl. I split a Seq of size 3 into 3 partitions:</p>
  
  <pre><code>val df = Seq(0,1,2).toDF().repartition(3)
  
  df.explain
  
  == Physical Plan ==
  Exchange RoundRobinPartitioning(3)
  +- LocalTableScan [value#42]
  </code></pre>
  
  <p>Now if I inspect the partitions, I get:</p>
  
  <pre><code>df
    .rdd
    .mapPartitionsWithIndex{case (i,rows) =&gt; Iterator((i,rows.size))}
    .toDF("partition_index","number_of_records")
    .show
  
  +---------------+-----------------+
  |partition_index|number_of_records|
  +---------------+-----------------+
  |              0|                0|
  |              1|                2|
  |              2|                1|
  +---------------+-----------------+
  </code></pre>
  
  <p>If I do the same with Seq of size 8 and split it into 8 partitions, I get even worse skew:</p>
  
  <pre><code>(0 to 7).toDF().repartition(8)
    .rdd
    .mapPartitionsWithIndex{case (i,rows) =&gt; Iterator((i,rows.size))}
    .toDF("partition_index","number_of_records")
    .show
  
  +---------------+-----------------+
  |partition_index|number_of_records|
  +---------------+-----------------+
  |              0|                0|
  |              1|                0|
  |              2|                0|
  |              3|                0|
  |              4|                0|
  |              5|                0|
  |              6|                4|
  |              7|                4|
  +---------------+-----------------+
  </code></pre>
  
  <p>Can somebody explain this behavior. As far as I understand round robin partitioning, all partitions show be ~same size.</p>