cloud-fan commented on a change in pull request #26509: [SPARK-29427][SQL] Add 
API to convert RelationalGroupedDataset to KeyValueGroupedDataset
URL: https://github.com/apache/spark/pull/26509#discussion_r347716019
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/RelationalGroupedDataset.scala
 ##########
 @@ -129,6 +132,32 @@ class RelationalGroupedDataset protected[sql](
     (inputExpr: Expression) => exprToFunc(inputExpr)
   }
 
+  /**
+   * Returns a `KeyValueGroupedDataset` where the data is grouped by the 
grouping expressions
+   * of current `RelationalGroupedDataset`.
+   *
+   * @since 3.0.0
+   */
+  def keyAs[U : Encoder]: KeyValueGroupedDataset[U, T] = {
+    val keyEncoder = encoderFor[U]
+    val aliasedGrps = groupingExprs.map { g =>
+     g.transformDown {
+       case u: UnresolvedAttribute => df.resolve(u.name)
+     }
+    }.map(alias)
+    val additionalCols = aliasedGrps.filter(g => 
!df.logicalPlan.outputSet.contains(g.toAttribute))
+    val qe = Dataset.ofRows(
+      df.sparkSession,
+      Project(df.logicalPlan.output ++ additionalCols, 
df.logicalPlan)).queryExecution
 
 Review comment:
   We can make `MapGroupsExec` follow aggregate, to calculate grouping columns 
ahead and put it in the buffer row, then run key/value deserializer on buffer 
row.
   
   But I admit that it's a big change, we can do it in the future.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

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

Reply via email to