Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/11283#discussion_r54978777
--- Diff:
sql/hive/src/main/scala/org/apache/spark/sql/hive/SQLBuilder.scala ---
@@ -233,6 +242,67 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext:
SQLContext) extends Loggi
)
}
+ private def groupingSetToSQL(
+ plan: Aggregate,
+ expand: Expand,
+ project: Project): String = {
+ require(plan.groupingExpressions.length > 1)
+
+ // The last column of Expand is always grouping ID
+ val gid = expand.output.last
+
+ val groupByAttributes =
plan.groupingExpressions.dropRight(1).map(_.asInstanceOf[Attribute])
+ val groupByExprs =
+
project.projectList.drop(project.child.output.length).map(_.asInstanceOf[Alias].child)
+ val groupByAttrMap = AttributeMap(groupByAttributes.zip(groupByExprs))
+ val groupingSQL = groupByExprs.map(_.sql).mkString(", ")
+
+ val groupingSet = expand.projections.map { project =>
+ // Assumption: expand.projections are composed of
+ // 1) the original output (Project's child.output),
+ // 2) group by attributes(or null literal)
+ // 3) gid, which is always the last one in each project in Expand
+ project.dropRight(1).collect {
+ case attr: Attribute if groupByAttrMap.contains(attr) =>
groupByAttrMap(attr)
+ }
+ }
+ val groupingSetSQL =
+ "GROUPING SETS(" +
+ groupingSet.map(e => s"(${e.map(_.sql).mkString(",
")})").mkString(", ") + ")"
+
+ val aggExprs = plan.aggregateExpressions.map { case expr =>
+ expr.transformDown {
+ // grouping_id() is converted to VirtualColumn.groupingIdName by
Analyzer. Revert it back.
+ case ar: AttributeReference if ar eq gid => GroupingID(Nil)
+ case a @ Alias(_ @ Cast(BitwiseAnd(
--- End diff --
can we also remove this `Alias` and the `Alias` in next case?
---
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]