I have one scala-spark dataframe with three columns say Col1, Col2 and Col3. This dataframe has around 54 million rows.
What I want is max of Col3 per Col1,Col2 combination like below
[![DataSet]](https://i.stack.imgur.com/FJKar.png)
val maxDf = df.groupBy(Col1, Col2).agg(F.max(Col3))
[![Expected Result]](https://i.stack.imgur.com/rXofY.png)
But when I am trying to achieve above using groupBy somehow it is failing with below error
java.lang.OutOfMemoryError: Unable to acquire 8168736160 bytes of memory, got 0
Then I tried the same using Window function
val window = Window.partitionBy("Col1","Col2")
val maxDf = df.withColumn("maxVal", F.max("Col3").over(window))
Somehow above code got executed successfully. I am trying to find out the reason behind this as theoretically both the operation are similar and have same execution plan.