Github user cloud-fan commented on the issue:

    https://github.com/apache/spark/pull/16347
  
    @junegunn Can you check the query plan of hive for `INSERT OVERWRITE TABLE 
... DISTRIBUTE BY ... SORT BY ...`?
    
    In Spark SQL, the query plan looks like
    ```
    'InsertIntoTable  `table1`
    +- 'Sort ['j ASC NULLS FIRST], false
       +- 'RepartitionByExpression ['i], 200
          +- table
    ```
    
    The sort happens before the insertion, this explains why Spark doesn't 
preserve the sort order. If in hive the sort is included in insertion, we 
should follow that.
    
    In the meanwhile, the behavior of DataFrameWriter looks reasonable(need to 
fix the document of `sortWithinPartitions `), but we should add new API to 
allow users specify sort ordering during writing, to be consistent with SQL API.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to