Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11283#discussion_r54714755
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/SQLBuilder.scala ---
    @@ -211,6 +215,71 @@ class SQLBuilder(logicalPlan: LogicalPlan, sqlContext: 
SQLContext) extends Loggi
         )
       }
     
    +  private def groupingSetToSQL(
    +      plan: Aggregate,
    +      expand: Expand,
    +      project: Project): String = {
    +    // In cube/rollup/groupingsets, Analyzer creates new aliases for all 
group by expressions.
    +    // Since conversion from attribute back SQL ignore expression IDs, the 
alias of attribute
    +    // references are ignored in aliasMap
    +    val aliasMap = AttributeMap(project.projectList.collect {
    +      case a @ Alias(child, name) if 
!child.isInstanceOf[AttributeReference] => (a.toAttribute, a)
    +    })
    +
    +    val groupingExprs = plan.groupingExpressions.filterNot {
    +      // VirtualColumn.groupingIdName is added by Analyzer, and thus 
remove it.
    +      case a: NamedExpression => a.name == VirtualColumn.groupingIdName
    +      case o => false
    +    }.map {
    +      case a: AttributeReference if aliasMap.contains(a) => 
aliasMap(a).child
    +      case o => o
    +    }
    +
    +    val groupingSQL = groupingExprs.map(_.sql).mkString(", ")
    +
    +    val groupingSet = expand.projections.map(_.filter {
    +      case _: Literal => false
    +      case e: Expression if 
plan.groupingExpressions.exists(_.semanticEquals(e)) => true
    +      case _ => false
    +    }.map {
    +      case a: AttributeReference if aliasMap.contains(a) => 
aliasMap(a).child
    +      case o => o
    +    })
    +
    +    val aggExprs = plan.aggregateExpressions.map {
    +      case a @ Alias(child: AttributeReference, name)
    +          if child.name == VirtualColumn.groupingIdName =>
    --- End diff --
    
    BTW should we follow the way we 
[generate](https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala#L261-L297)
 these aggregate expressions? i.e.
    ```
    plan.aggregateExpressions.map { case expr =>
      expr.transformDown {
        ...
      }
    }
    ```


---
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]

Reply via email to