How does spark determine the number of partitions after using an orderBy
? I always thought that the resulting dataframe has spark.sql.shuffle.partitions
, but this does not seem to be true :
val df = (1 to 10000).map(i => ("a",i)).toDF("n","i").repartition(10).cache
df.orderBy($"i").rdd.getNumPartitions // = 200 (=spark.sql.shuffle.partitions)
df.orderBy($"n").rdd.getNumPartitions // = 2
In both cases, spark does +- Exchange rangepartitioning(i/n ASC NULLS FIRST, 200)
, so how can the resulting number of partitions in the second case be 2?
Finding the number of partitions Simply turn the DataFrame to rdd and call partitions followed by size to get the number of partitions. We would see the number of partitions as 200.
Similarly, in PySpark you can get the current length/size of partitions by running getNumPartitions() of RDD class, so to use with DataFrame first you need to convert to RDD.
The repartition method can be used to either increase or decrease the number of partitions in a DataFrame.
The general recommendation for Spark is to have 4x of partitions to the number of cores in cluster available for application, and for upper bound — the task should take 100ms+ time to execute.
spark.sql.shuffle.partitions
is used as an upper bound. The final number of partitions is 1 <= partitions <= spark.sql.shuffle.partition
.
As you've mentioned, the sorting in Spark goes through RangePartitioner
. What it tries to achieve is to partition your dataset into a specified number (spark.sql.shuffle.partition
) of roughly equal ranges.
There's a guarantee that equal values will be in the same partition after the partitioning. It's worth checking RangePartitioning
(not part of the public API) class documentation:
...
All row where the expressions in
ordering
evaluate to the same values will be in the same partition
And if the number of distinct ordering values is less than the desired number of partitions, i.e. the number of possible ranges is less than spark.sql.shuffle.partition
, you'll end up with a smaller number of partitions. Also, here's a quote from RangePartitioner
Scaladoc:
The actual number of partitions created by the RangePartitioner might not be the same as the partitions parameter, in the case where the number of sampled records is less than the value of partitions.
Going back to your example, n
is a constant ("a"
) and could not be partitioned. On the other hand, i
can have 10,000 possible values and is partitioned into 200 (=spark.sql.shuffle.partition
) ranges or partitions.
Note that this is only true for DataFrame/Dataset API. When using RDD's sortByKey
one can either specify the number of partitions explicitly or Spark will use the current number of partitions.
See also:
I ran various tests so as to look at this more empirically, in addition to looking at Range Partitioning for Sorting - which is the crux of the matter here. See How does range partitioner work in Spark?.
Having experimented with both 1 distinct value for "n" as in the example in the question, and more than 1 such distinct value for the "n", then using various dataframe sizes with df.orderBy($"n"):
The fact that the extra partition allocated is nearly always empty makes me think there is a calculation error in the coding in some way, in other words a small bug imho.
I base this on the following simple test, which does return what RR I suspect would consider to be the proper number of partitions:
val df_a1 = (1 to 1).map(i => ("a",i)).toDF("n","i").cache
val df_a2 = (1 to 1).map(i => ("b",i)).toDF("n","i").cache
val df_a3 = (1 to 1).map(i => ("c",i)).toDF("n","i").cache
val df_b = df_a1.union(df_a2)
val df_c = df_b.union(df_a3)
df_c.orderBy($"n")
.rdd
.mapPartitionsWithIndex{case (i,rows) => Iterator((i,rows.size))}
.toDF("partition_number","number_of_records")
.show(100,false)
returns:
+----------------+-----------------+
|partition_number|number_of_records|
+----------------+-----------------+
|0 |1 |
|1 |1 |
|2 |1 |
+----------------+-----------------+
This boundary example calculation is rather simple. As soon as I use 1 to 2 or 1 .. N for any of the "n", the extra empty partitions results:
+----------------+-----------------+
|partition_number|number_of_records|
+----------------+-----------------+
|0 |2 |
|1 |1 |
|2 |1 |
|3 |0 |
+----------------+-----------------+
The sorting requires all data for a given "n" or set of "n" to be in the same partition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With