[FIXED] SPARK SQL: does groupBy after orderBy maintain that order?

Issue

I have spark 2.3.0. In this example, does groupBy after orderBy maintain that order?

id date       name
1  2020-10-01 name1
1  2020-09-01 name2
1  2020-11-01 name3

This is the code:

df
  .orderBy(col("date"))
  .groupBy(col("id"))
  .agg(
    first(col("name"))
  ).show()

The result i expect:

id name
1  name2

I read in old posts that the order is not maintained. But I did some tests and the sorting is maintained.


Solution

No it does not, there is no guarantee.

Shuffling and hashing due to groupBy means ordering is different than the preceding orderBy. It should be done after. That's the given from both old and new posts.



Answered By - thebluephantom
Answer Checked By - Gilberto Lyons (sql.easybugfix Admin)

Related Posts

Post a Comment