Skip to content
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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ case class HashAggregateExec(
resultExpressions,
(expressions, inputSchema) =>
MutableProjection.create(expressions, inputSchema),
child.output,
inputAttributes,
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your confirm.

iter,
testFallbackStartsAt,
numOutputRows,
Expand Down Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name matching is fragile, how about

child.output.dropRight(aggAttrs.length) ++ aggAttrs

Copy link
Member Author

Choose a reason for hiding this comment

The 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 =>
Expand Down Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's for resolving grouping keys, child.output is fine here.

val unsafeRowKeys = unsafeRowKeyCode.value
val unsafeRowKeyHash = ctx.freshName("unsafeRowKeyHash")
val unsafeRowBuffer = ctx.freshName("unsafeRowAggBuffer")
Expand Down Expand Up @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)") {
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
}
}
}
}
}