-
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 2 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 |
---|---|---|
|
@@ -233,14 +233,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]() | ||
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 == x.groupByExprs) { | ||
// the bitmask is following Hive, which is wrong, we need to reverse it here | ||
// TODO: don't not follow Hive | ||
BitwiseReverse(BitwiseNot(gid), e.groupByExprs.length) | ||
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. Why not break compatibility here? We are doing that in quite a lot of places anyway. 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. Try to re-use the Hive tests, will pull them out. |
||
} 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(BitwiseNot(gid), Literal(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] | ||
|
@@ -707,8 +732,11 @@ class Analyzer( | |
} | ||
} | ||
|
||
private def isAggregateExpression(e: Expression): Boolean = { | ||
e.isInstanceOf[AggregateExpression] || e.isInstanceOf[Grouping] || e.isInstanceOf[GroupingID] | ||
} | ||
protected def containsAggregate(condition: Expression): Boolean = { | ||
condition.find(_.isInstanceOf[AggregateExpression]).isDefined | ||
condition.find(isAggregateExpression).isDefined | ||
} | ||
} | ||
|
||
|
@@ -889,7 +917,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) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,47 @@ case class BitwiseNot(child: Expression) extends UnaryExpression with ExpectsInp | |
|
||
protected override def nullSafeEval(input: Any): Any = not(input) | ||
} | ||
|
||
/** | ||
* A function that reverse the lowest N bits of a integer. | ||
* | ||
* Note: this is only used for grouping_id() | ||
*/ | ||
case class BitwiseReverse(child: Expression, width: Int) | ||
extends UnaryExpression with ExpectsInputTypes { | ||
|
||
override def inputTypes: Seq[AbstractDataType] = Seq(IntegerType) | ||
|
||
override def dataType: DataType = IntegerType | ||
|
||
override def toString: String = s"^$child" | ||
|
||
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = { | ||
nullSafeCodeGen(ctx, ev, c => { | ||
val v = ctx.freshName("v") | ||
val i = ctx.freshName("i") | ||
s""" | ||
| int $v = $c; | ||
| ${ev.value} = 0; | ||
| for (int $i = 0; $i < $width; $i ++) { | ||
| ${ev.value} <<= 1; | ||
| ${ev.value} |= $v & 1; | ||
| $v >>>= 1; | ||
| } | ||
""".stripMargin | ||
}) | ||
} | ||
|
||
protected override def nullSafeEval(input: Any): Any = { | ||
var v = input.asInstanceOf[Int] | ||
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. Nit: We could also use 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. Great, will do. |
||
var r = 0 | ||
var i = 0 | ||
while (i < width) { | ||
r <<= 1 | ||
r |= v & 1 | ||
v >>>= 1 | ||
i += 1 | ||
} | ||
r | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,6 +324,51 @@ object functions extends LegacyFunctions { | |
*/ | ||
def first(columnName: String): Column = first(Column(columnName)) | ||
|
||
|
||
/** | ||
* Aggregate function: indicates whether a specified column in a GROUP BY list is aggregated | ||
* or not, returns 1 for aggregated or 0 for not aggregated in the result set. | ||
* | ||
* @group agg_funcs | ||
* @since 2.0.0 | ||
*/ | ||
def grouping(e: Column): Column = Column(Grouping(e.expr)) | ||
|
||
/** | ||
* Aggregate function: indicates whether a specified column in a GROUP BY list is aggregated | ||
* or not, returns 1 for aggregated or 0 for not aggregated in the result set. | ||
* | ||
* @group agg_funcs | ||
* @since 2.0.0 | ||
*/ | ||
def grouping(columnName: String): Column = grouping(Column(columnName)) | ||
|
||
/** | ||
* Aggregate function: returns the level of grouping, equals to | ||
* | ||
* (grouping(c1) << (n-1)) + (grouping(c1) << (n-2)) + ... + grouping(cn) | ||
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. Second term should be grouping(c2) |
||
* | ||
* Note: the list of columns should match with grouping columns exactly. | ||
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. So why have people pass a list of columns at all? Do you want to support subsets of the group by clause in the future? We could at least remove this for the DataFrame API. 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. Agreed that this is useless, this is to match the API in other databases (porgresql, sql server, oracle) |
||
* | ||
* @group agg_funcs | ||
* @since 2.0.0 | ||
*/ | ||
def grouping_id(cols: Column*): Column = Column(GroupingID(cols.map(_.expr))) | ||
|
||
/** | ||
* Aggregate function: returns the level of grouping, equals to | ||
* | ||
* (grouping(c1) << (n-1)) + (grouping(c1) << (n-2)) + ... + grouping(cn) | ||
* | ||
* Note: the list of columns should match with grouping columns exactly. | ||
* | ||
* @group agg_funcs | ||
* @since 2.0.0 | ||
*/ | ||
def grouping_id(colName: String, colNames: String*): Column = { | ||
grouping_id((Seq(colName) ++ colNames).map(n => Column(n)): _*) | ||
} | ||
|
||
/** | ||
* Aggregate function: returns the kurtosis of the values in a group. | ||
* | ||
|
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?