This example has been tested with Spark 2.4.x. Let's consider 2 simple dataframes:
case class Event(user_id: String, v: String)
case class User(user_id: String, name: String)
val events = spark.createDataFrame(Seq(Event("u1", "u1.1"),Event("u1", "u1.2"),Event("u2", "u2.1")))
val users = spark.createDataFrame(Seq(User("u1", "name1"),User("u2", "name2"),User("u3", "name3")))
When there is a groupBy followed by a joinWith on the same column, Spark performs an unecessary shuffle:
events
.groupBy("user_id").agg(collect_list(struct("v")).alias("values"))
.joinWith(users, events("user_id") === users("user_id"), "full_outer")
.show
When doing the same thing with a join, the extra shuffle disappears:
events
.groupBy("user_id").agg(collect_list(struct("v")).alias("values"))
.join(users, events("user_id") === users("user_id"), "full_outer")
.show
Is it a known issue?
Is there a workaround to use joinWith without the extra shuffle?
Appendix : here are the SQL plans
- with
joinWith
- with
join

