Github user gatorsmile commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11283#discussion_r54769010
  
    --- 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 =>
    +        // grouping_id() is converted to VirtualColumn.groupingIdName by 
Analyzer. Revert it back.
    +        Alias(GroupingID(Nil), name)()
    +      case a @ Alias(_ @ Cast(BitwiseAnd(
    +        ShiftRight(ar: AttributeReference, _ @ Literal(value: Any, 
IntegerType)),
    +        Literal(1, IntegerType)), ByteType), name)
    +          if ar.name == VirtualColumn.groupingIdName =>
    +        // for converting an expression to its original SQL format 
grouping(col)
    +        val idx = groupingExprs.length - 1 - value.asInstanceOf[Int]
    +        val groupingCol = groupingExprs.lift(idx)
    +        if (groupingCol.isDefined) {
    +          Grouping(groupingCol.get)
    +        } else {
    +          throw new UnsupportedOperationException(s"unsupported operator 
$a")
    +        }
    +      case a @ Alias(child: AttributeReference, name) if 
aliasMap.contains(child) =>
    +        aliasMap(child).child
    +      case o => o
    +    }
    +
    +    build(
    +      "SELECT",
    +      aggExprs.map(_.sql).mkString(", "),
    +      if (plan.child == OneRowRelation) "" else "FROM",
    +      toSQL(project.child),
    +      if (groupingSQL.isEmpty) "" else "GROUP BY",
    --- End diff --
    
    I did a few tries. The parser enforces it. It has to be non-empty. 


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