-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-12706] [SQL] grouping() and grouping_id() #10677
Changes from all commits
bcb8d9e
736e8d2
68d6a3c
90c1655
9511c2c
b1ddff1
c008569
ba8e06f
aa34559
3469e45
9c7a06f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -238,14 +238,39 @@ class Analyzer( | |
} | ||
}.toMap | ||
|
||
val aggregations: Seq[NamedExpression] = x.aggregations.map { | ||
// If an expression is an aggregate (contains a AggregateExpression) then we dont change | ||
// it so that the aggregation is computed on the unmodified value of its argument | ||
// expressions. | ||
case expr if expr.find(_.isInstanceOf[AggregateExpression]).nonEmpty => expr | ||
// If not then its a grouping expression and we need to use the modified (with nulls from | ||
// Expand) value of the expression. | ||
case expr => expr.transformDown { | ||
val aggregations: Seq[NamedExpression] = x.aggregations.map { case expr => | ||
// collect all the found AggregateExpression, so we can check an expression is part of | ||
// any AggregateExpression or not. | ||
val aggsBuffer = ArrayBuffer[Expression]() | ||
// Returns whether the expression belongs to any expressions in `aggsBuffer` or not. | ||
def isPartOfAggregation(e: Expression): Boolean = { | ||
aggsBuffer.exists(a => a.find(_ eq e).isDefined) | ||
} | ||
expr.transformDown { | ||
// AggregateExpression should be computed on the unmodified value of its argument | ||
// expressions, so we should not replace any references to grouping expression | ||
// inside it. | ||
case e: AggregateExpression => | ||
aggsBuffer += e | ||
e | ||
case e if isPartOfAggregation(e) => e | ||
case e: GroupingID => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably a dumb question. What happens if we use these functions without grouping sets? Do we get a nice analysis exception? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now, it will fail to resolve, agreed that should be have a better error message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had capture this in CheckAnasys. |
||
if (e.groupByExprs.isEmpty || e.groupByExprs == x.groupByExprs) { | ||
gid | ||
} else { | ||
throw new AnalysisException( | ||
s"Columns of grouping_id (${e.groupByExprs.mkString(",")}) does not match " + | ||
s"grouping columns (${x.groupByExprs.mkString(",")})") | ||
} | ||
case Grouping(col: Expression) => | ||
val idx = x.groupByExprs.indexOf(col) | ||
if (idx >= 0) { | ||
Cast(BitwiseAnd(ShiftRight(gid, Literal(x.groupByExprs.length - 1 - idx)), | ||
Literal(1)), ByteType) | ||
} else { | ||
throw new AnalysisException(s"Column of grouping ($col) can't be found " + | ||
s"in grouping columns ${x.groupByExprs.mkString(",")}") | ||
} | ||
case e => | ||
groupByAliases.find(_.child.semanticEquals(e)).map(attributeMap(_)).getOrElse(e) | ||
}.asInstanceOf[NamedExpression] | ||
|
@@ -814,8 +839,11 @@ class Analyzer( | |
} | ||
} | ||
|
||
private def isAggregateExpression(e: Expression): Boolean = { | ||
e.isInstanceOf[AggregateExpression] || e.isInstanceOf[Grouping] || e.isInstanceOf[GroupingID] | ||
} | ||
def containsAggregate(condition: Expression): Boolean = { | ||
condition.find(_.isInstanceOf[AggregateExpression]).isDefined | ||
condition.find(isAggregateExpression).isDefined | ||
} | ||
} | ||
|
||
|
@@ -997,7 +1025,7 @@ class Analyzer( | |
_.transform { | ||
// Extracts children expressions of a WindowFunction (input parameters of | ||
// a WindowFunction). | ||
case wf : WindowFunction => | ||
case wf: WindowFunction => | ||
val newChildren = wf.children.map(extractExpr) | ||
wf.withNewChildren(newChildren) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to take a long hard look to understand this. You are comparing references not structure. Maybe a small piece of documentation?