GitHub user junegunn opened a pull request: https://github.com/apache/spark/pull/16347
[SPARK-18934][SQL] Writing to dynamic partitions does not preserve sort order if spills occur ## What changes were proposed in this pull request? Make dynamic partition writer perform stable sort by the partition key, so that the sort order within the partition specified via `sortWithinPartitions` or `SORT BY` is preserved even when spill occurs. ## How was this patch tested? Manually tested with the following code snippet and orcdump. ```scala // FileFormatWriter sc.parallelize(1 to 10000000).toDS.withColumn("part", 'value.mod(2)) .repartition(1, 'part).sortWithinPartitions("value") .write.mode("overwrite").format("orc").partitionBy("part") .saveAsTable("test_sort_within") spark.read.table("test_sort_within").filter('part === 0).show spark.read.table("test_sort_within").filter('part === 1).show // SparkHiveDynamicPartitionWriterContainer // Insert into an existing Hive table with dynamic partitions // CREATE TABLE TEST_SORT_WITHIN (VALUE INT) PARTITIONED BY (PART INT) STORED AS ORC spark.conf.set("hive.exec.dynamic.partition.mode", "nonstrict") sc.parallelize(1 to 10000000).toDS.withColumn("part", 'value.mod(2)) .repartition(1, 'part).sortWithinPartitions("value") .write.mode("overwrite").insertInto("test_sort_within_hive") spark.read.table("test_sort_within_hive").filter('part === 0).show spark.read.table("test_sort_within_hive").filter('part === 1).show ``` It was not straightforward to come up with a unit test as the problem is only reproducible if spill occurs due to memory constraint. I'd appreciate any suggestions or pointers. You can merge this pull request into a Git repository by running: $ git pull https://github.com/junegunn/spark dynamic-partition-writer-stable-sort Alternatively you can review and apply these changes as the patch at: https://github.com/apache/spark/pull/16347.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #16347 ---- commit bfeccd80ef032cab3525037be3d3e42519619493 Author: Junegunn Choi <junegun...@gmail.com> Date: 2016-12-19T05:54:42Z [SPARK-18934][SQL] Writing to dynamic partitions does not preserve sort order if spills occur ---- --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org