-
Notifications
You must be signed in to change notification settings - Fork 28k
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-31620][SQL] Fix reference binding failure in case of an final agg contains subquery #28496
Changes from 6 commits
ba75e36
4dcc1b1
bc1b7e5
cbd891d
882467b
9a0b788
3fd39c5
64d13fc
89ae4bf
8aecd57
493157a
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 |
---|---|---|
|
@@ -129,7 +129,7 @@ case class HashAggregateExec( | |
resultExpressions, | ||
(expressions, inputSchema) => | ||
MutableProjection.create(expressions, inputSchema), | ||
child.output, | ||
inputAttributes, | ||
iter, | ||
testFallbackStartsAt, | ||
numOutputRows, | ||
|
@@ -331,10 +331,32 @@ case class HashAggregateExec( | |
} | ||
} | ||
|
||
private def inputAttributes: Seq[Attribute] = { | ||
if (modes.contains(Final) || modes.contains(PartialMerge)) { | ||
// SPARK-31620: when planning aggregates, the partial aggregate uses aggregate function's | ||
// `inputAggBufferAttributes` as its output. And Final and PartialMerge aggregate rely on the | ||
// output to bind references for `DeclarativeAggregate.mergeExpressions`. But if we copy the | ||
// aggregate function somehow after aggregate planning, like `PlanSubqueries`, the | ||
// `DeclarativeAggregate` will be replaced by a new instance with new | ||
// `inputAggBufferAttributes` and `mergeExpressions`. Then Final and PartialMerge aggregate | ||
// can't bind the `mergeExpressions` with the output of the partial aggregate, as they use | ||
// the `inputAggBufferAttributes` of the original `DeclarativeAggregate` before copy. Instead, | ||
// we shall use `inputAggBufferAttributes` after copy to match the new `mergeExpressions`. | ||
val aggAttrs = aggregateExpressions.map(_.aggregateFunction) | ||
.flatMap(_.inputAggBufferAttributes) | ||
val distinctAttrs = child.output.filterNot( | ||
a => (groupingAttributes ++ aggAttrs).exists(_.name == a.name)) | ||
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. name matching is fragile, how about
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. oh yea, this looks better! |
||
// the order is consistent with `AggUtils.planAggregateWithOneDistinct` | ||
groupingAttributes ++ distinctAttrs ++ aggAttrs | ||
} else { | ||
child.output | ||
} | ||
} | ||
|
||
private def doConsumeWithoutKeys(ctx: CodegenContext, input: Seq[ExprCode]): String = { | ||
// only have DeclarativeAggregate | ||
val functions = aggregateExpressions.map(_.aggregateFunction.asInstanceOf[DeclarativeAggregate]) | ||
val inputAttrs = functions.flatMap(_.aggBufferAttributes) ++ child.output | ||
val inputAttrs = functions.flatMap(_.aggBufferAttributes) ++ inputAttributes | ||
// To individually generate code for each aggregate function, an element in `updateExprs` holds | ||
// all the expressions for the buffer of an aggregation function. | ||
val updateExprs = aggregateExpressions.map { e => | ||
|
@@ -848,9 +870,9 @@ case class HashAggregateExec( | |
private def doConsumeWithKeys(ctx: CodegenContext, input: Seq[ExprCode]): String = { | ||
// create grouping key | ||
val unsafeRowKeyCode = GenerateUnsafeProjection.createCode( | ||
ctx, bindReferences[Expression](groupingExpressions, child.output)) | ||
ctx, bindReferences[Expression](groupingExpressions, inputAttributes)) | ||
val fastRowKeys = ctx.generateExpressions( | ||
bindReferences[Expression](groupingExpressions, child.output)) | ||
bindReferences[Expression](groupingExpressions, inputAttributes)) | ||
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. it's for resolving grouping keys, |
||
val unsafeRowKeys = unsafeRowKeyCode.value | ||
val unsafeRowKeyHash = ctx.freshName("unsafeRowKeyHash") | ||
val unsafeRowBuffer = ctx.freshName("unsafeRowAggBuffer") | ||
|
@@ -931,7 +953,7 @@ case class HashAggregateExec( | |
} | ||
} | ||
|
||
val inputAttr = aggregateBufferAttributes ++ child.output | ||
val inputAttr = aggregateBufferAttributes ++ inputAttributes | ||
// Here we set `currentVars(0)` to `currentVars(numBufferSlots)` to null, so that when | ||
// generating code for buffer columns, we use `INPUT_ROW`(will be the buffer row), while | ||
// generating input columns, we use `currentVars`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -973,4 +973,21 @@ class DataFrameAggregateSuite extends QueryTest | |
assert(error.message.contains("function count_if requires boolean type")) | ||
} | ||
} | ||
|
||
Seq(true, false).foreach { value => | ||
test(s"SPARK-31620: agg with subquery (codegen = $value)") { | ||
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. codegen -> "whole-stage-codegen" |
||
withSQLConf(SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key -> value.toString) { | ||
withTempView("t1", "t2") { | ||
sql("create temporary view t1 as select * from values (1, 2) as t1(a, b)") | ||
sql("create temporary view t2 as select * from values (3, 4) as t2(c, d)") | ||
// test without grouping keys | ||
checkAnswer(sql("select sum(if(c > (select a from t1), d, 0)) as csum from t2"), | ||
Row(4) :: Nil) | ||
// test with grouping keys | ||
checkAnswer(sql("select c, sum(if(c > (select a from t1), d, 0)) as csum from " + | ||
"t2 group by c"), Row(3, 4) :: Nil) | ||
} | ||
} | ||
} | ||
} | ||
} |
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 try to fix like this before, but I forgot to change child's output here then output column can't work well.
LGTM
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.
Thanks for your confirm.