Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ trait PredicateHelper extends AliasHelper with Logging {
None
}
}

// If one expression and its children are null intolerant, it is null intolerant.
protected def isNullIntolerant(expr: Expression): Boolean = expr match {
case e: NullIntolerant => e.children.forall(isNullIntolerant)
case _ => false
}

protected def outputWithNullability(
output: Seq[Attribute],
nonNullAttrExprIds: Seq[ExprId]): Seq[Attribute] = {
output.map { a =>
if (a.nullable && nonNullAttrExprIds.contains(a.exprId)) {
a.withNullability(false)
} else {
a
}
}
}
}

@ExpressionDescription(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ case class HashAggregateExec(
resultExpressions: Seq[NamedExpression],
child: SparkPlan)
extends BaseAggregateExec
with BlockingOperatorWithCodegen {
with BlockingOperatorWithCodegen
with GeneratePredicateHelper {

require(HashAggregateExec.supportsAggregate(aggregateBufferAttributes))

Expand Down Expand Up @@ -131,10 +132,8 @@ case class HashAggregateExec(
override def usedInputs: AttributeSet = inputSet

override def supportCodegen: Boolean = {
// ImperativeAggregate and filter predicate are not supported right now
// TODO: SPARK-30027 Support codegen for filter exprs in HashAggregateExec
!(aggregateExpressions.exists(_.aggregateFunction.isInstanceOf[ImperativeAggregate]) ||
aggregateExpressions.exists(_.filter.isDefined))
// ImperativeAggregate are not supported right now
!aggregateExpressions.exists(_.aggregateFunction.isInstanceOf[ImperativeAggregate])
}

override def inputRDDs(): Seq[RDD[InternalRow]] = {
Expand Down Expand Up @@ -254,7 +253,7 @@ case class HashAggregateExec(
aggNames: Seq[String],
aggBufferUpdatingExprs: Seq[Seq[Expression]],
aggCodeBlocks: Seq[Block],
subExprs: Map[Expression, SubExprEliminationState]): Option[String] = {
subExprs: Map[Expression, SubExprEliminationState]): Option[Seq[String]] = {
val exprValsInSubExprs = subExprs.flatMap { case (_, s) => s.value :: s.isNull :: Nil }
if (exprValsInSubExprs.exists(_.isInstanceOf[SimpleExprValue])) {
// `SimpleExprValue`s cannot be used as an input variable for split functions, so
Expand Down Expand Up @@ -293,7 +292,7 @@ case class HashAggregateExec(
val inputVariables = args.map(_.variableName).mkString(", ")
s"$doAggFuncName($inputVariables);"
}
Some(splitCodes.mkString("\n").trim)
Some(splitCodes)
} else {
val errMsg = "Failed to split aggregate code into small functions because the parameter " +
"length of at least one split function went over the JVM limit: " +
Expand All @@ -308,6 +307,39 @@ case class HashAggregateExec(
}
}

private def generateEvalCodeForAggFuncs(
ctx: CodegenContext,
input: Seq[ExprCode],
inputAttrs: Seq[Attribute],
boundUpdateExprs: Seq[Seq[Expression]],
aggNames: Seq[String],
aggCodeBlocks: Seq[Block],
subExprs: SubExprCodes): String = {
val aggCodes = if (conf.codegenSplitAggregateFunc &&
aggCodeBlocks.map(_.length).sum > conf.methodSplitThreshold) {
val maybeSplitCodes = splitAggregateExpressions(
ctx, aggNames, boundUpdateExprs, aggCodeBlocks, subExprs.states)

maybeSplitCodes.getOrElse(aggCodeBlocks.map(_.code))
} else {
aggCodeBlocks.map(_.code)
}

aggCodes.zip(aggregateExpressions.map(ae => (ae.mode, ae.filter))).map {
case (aggCode, (Partial | Complete, Some(condition))) =>
// Note: wrap in "do { } while(false);", so the generated checks can jump out
// with "continue;"
s"""
|do {
| ${generatePredicateCode(ctx, condition, inputAttrs, input)}
| $aggCode
|} while(false);

Choose a reason for hiding this comment

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

I don't understand why "while(false)" can take an effect here. Could you explain why it is needed here?

Copy link
Contributor

@HeartSaVioR HeartSaVioR Jan 27, 2020

Choose a reason for hiding this comment

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

NOTE in above code comment would be enough to explain why, right? It still executes only once, but be able to exit the specific code block via continue instead of exiting the function/method in the middle of the code.

Choose a reason for hiding this comment

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

Got it. Thanks for the explanation.

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 the reply, @HeartSaVioR. Since I forgot to add the generated code, I've added it in the PR description.

""".stripMargin
case (aggCode, _) =>
aggCode
}.mkString("\n")
}

private def doConsumeWithoutKeys(ctx: CodegenContext, input: Seq[ExprCode]): String = {
// only have DeclarativeAggregate
val functions = aggregateExpressions.map(_.aggregateFunction.asInstanceOf[DeclarativeAggregate])
Expand Down Expand Up @@ -354,24 +386,14 @@ case class HashAggregateExec(
""".stripMargin
}

val codeToEvalAggFunc = if (conf.codegenSplitAggregateFunc &&
aggCodeBlocks.map(_.length).sum > conf.methodSplitThreshold) {
val maybeSplitCode = splitAggregateExpressions(
ctx, aggNames, boundUpdateExprs, aggCodeBlocks, subExprs.states)

maybeSplitCode.getOrElse {
aggCodeBlocks.fold(EmptyBlock)(_ + _).code
}
} else {
aggCodeBlocks.fold(EmptyBlock)(_ + _).code
}

val codeToEvalAggFuncs = generateEvalCodeForAggFuncs(
ctx, input, inputAttrs, boundUpdateExprs, aggNames, aggCodeBlocks, subExprs)
s"""
|// do aggregate
|// common sub-expressions
|$effectiveCodes
|// evaluate aggregate functions and update aggregation buffers
|$codeToEvalAggFunc
|$codeToEvalAggFuncs
""".stripMargin
}

Expand Down Expand Up @@ -908,7 +930,7 @@ case class HashAggregateExec(
}
}

val inputAttr = aggregateBufferAttributes ++ inputAttributes
val inputAttrs = 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 All @@ -930,7 +952,7 @@ case class HashAggregateExec(
val updateRowInRegularHashMap: String = {
ctx.INPUT_ROW = unsafeRowBuffer
val boundUpdateExprs = updateExprs.map { updateExprsForOneFunc =>
bindReferences(updateExprsForOneFunc, inputAttr)
bindReferences(updateExprsForOneFunc, inputAttrs)
}
val subExprs = ctx.subexpressionEliminationForWholeStageCodegen(boundUpdateExprs.flatten)
val effectiveCodes = subExprs.codes.mkString("\n")
Expand Down Expand Up @@ -961,23 +983,13 @@ case class HashAggregateExec(
""".stripMargin
}

val codeToEvalAggFunc = if (conf.codegenSplitAggregateFunc &&
aggCodeBlocks.map(_.length).sum > conf.methodSplitThreshold) {
val maybeSplitCode = splitAggregateExpressions(
ctx, aggNames, boundUpdateExprs, aggCodeBlocks, subExprs.states)

maybeSplitCode.getOrElse {
aggCodeBlocks.fold(EmptyBlock)(_ + _).code
}
} else {
aggCodeBlocks.fold(EmptyBlock)(_ + _).code
}

val codeToEvalAggFuncs = generateEvalCodeForAggFuncs(
ctx, input, inputAttrs, boundUpdateExprs, aggNames, aggCodeBlocks, subExprs)
s"""
|// common sub-expressions
|$effectiveCodes
|// evaluate aggregate functions and update aggregation buffers
|$codeToEvalAggFunc
|$codeToEvalAggFuncs
""".stripMargin
}

Expand All @@ -986,7 +998,7 @@ case class HashAggregateExec(
if (isVectorizedHashMapEnabled) {
ctx.INPUT_ROW = fastRowBuffer
val boundUpdateExprs = updateExprs.map { updateExprsForOneFunc =>
bindReferences(updateExprsForOneFunc, inputAttr)
bindReferences(updateExprsForOneFunc, inputAttrs)
}
val subExprs = ctx.subexpressionEliminationForWholeStageCodegen(boundUpdateExprs.flatten)
val effectiveCodes = subExprs.codes.mkString("\n")
Expand Down Expand Up @@ -1016,18 +1028,8 @@ case class HashAggregateExec(
""".stripMargin
}


val codeToEvalAggFunc = if (conf.codegenSplitAggregateFunc &&
aggCodeBlocks.map(_.length).sum > conf.methodSplitThreshold) {
val maybeSplitCode = splitAggregateExpressions(
ctx, aggNames, boundUpdateExprs, aggCodeBlocks, subExprs.states)

maybeSplitCode.getOrElse {
aggCodeBlocks.fold(EmptyBlock)(_ + _).code
}
} else {
aggCodeBlocks.fold(EmptyBlock)(_ + _).code
}
val codeToEvalAggFuncs = generateEvalCodeForAggFuncs(
ctx, input, inputAttrs, boundUpdateExprs, aggNames, aggCodeBlocks, subExprs)

// If vectorized fast hash map is on, we first generate code to update row
// in vectorized fast hash map, if the previous loop up hit vectorized fast hash map.
Expand All @@ -1037,7 +1039,7 @@ case class HashAggregateExec(
| // common sub-expressions
| $effectiveCodes
| // evaluate aggregate functions and update aggregation buffers
| $codeToEvalAggFunc
| $codeToEvalAggFuncs
|} else {
| $updateRowInRegularHashMap
|}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,59 +109,39 @@ case class ProjectExec(projectList: Seq[NamedExpression], child: SparkPlan)
}
}

/** Physical plan for Filter. */
case class FilterExec(condition: Expression, child: SparkPlan)
Copy link
Contributor

Choose a reason for hiding this comment

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

@maropu I don't understand why we need change FilterExec ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just for sharing code to process predicates between aggregates and filters.

extends UnaryExecNode with CodegenSupport with PredicateHelper {

// Split out all the IsNotNulls from condition.
private val (notNullPreds, otherPreds) = splitConjunctivePredicates(condition).partition {
case IsNotNull(a) => isNullIntolerant(a) && a.references.subsetOf(child.outputSet)
case _ => false
}

// If one expression and its children are null intolerant, it is null intolerant.
private def isNullIntolerant(expr: Expression): Boolean = expr match {
case e: NullIntolerant => e.children.forall(isNullIntolerant)
case _ => false
}

// The columns that will filtered out by `IsNotNull` could be considered as not nullable.
private val notNullAttributes = notNullPreds.flatMap(_.references).distinct.map(_.exprId)

// Mark this as empty. We'll evaluate the input during doConsume(). We don't want to evaluate
// all the variables at the beginning to take advantage of short circuiting.
override def usedInputs: AttributeSet = AttributeSet.empty

override def output: Seq[Attribute] = {
child.output.map { a =>
if (a.nullable && notNullAttributes.contains(a.exprId)) {
a.withNullability(false)
} else {
a
}
trait GeneratePredicateHelper extends PredicateHelper {
self: CodegenSupport =>

protected def generatePredicateCode(
ctx: CodegenContext,
condition: Expression,
inputAttrs: Seq[Attribute],
inputExprCode: Seq[ExprCode]): String = {
val (notNullPreds, otherPreds) = splitConjunctivePredicates(condition).partition {
case IsNotNull(a) => isNullIntolerant(a) && a.references.subsetOf(AttributeSet(inputAttrs))
case _ => false
}
}

override lazy val metrics = Map(
"numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of output rows"))

override def inputRDDs(): Seq[RDD[InternalRow]] = {
child.asInstanceOf[CodegenSupport].inputRDDs()
}

protected override def doProduce(ctx: CodegenContext): String = {
child.asInstanceOf[CodegenSupport].produce(ctx, this)
}

override def doConsume(ctx: CodegenContext, input: Seq[ExprCode], row: ExprCode): String = {
val numOutput = metricTerm(ctx, "numOutputRows")

val nonNullAttrExprIds = notNullPreds.flatMap(_.references).distinct.map(_.exprId)
val outputAttrs = outputWithNullability(inputAttrs, nonNullAttrExprIds)
generatePredicateCode(
ctx, inputAttrs, inputExprCode, outputAttrs, notNullPreds, otherPreds,
nonNullAttrExprIds)
}

protected def generatePredicateCode(
ctx: CodegenContext,
inputAttrs: Seq[Attribute],
inputExprCode: Seq[ExprCode],
outputAttrs: Seq[Attribute],
notNullPreds: Seq[Expression],
otherPreds: Seq[Expression],
nonNullAttrExprIds: Seq[ExprId]): String = {
/**
* Generates code for `c`, using `in` for input attributes and `attrs` for nullability.
*/
def genPredicate(c: Expression, in: Seq[ExprCode], attrs: Seq[Attribute]): String = {
val bound = BindReferences.bindReference(c, attrs)
val evaluated = evaluateRequiredVariables(child.output, in, c.references)
val evaluated = evaluateRequiredVariables(inputAttrs, in, c.references)

// Generate the code for the predicate.
val ev = ExpressionCanonicalizer.execute(bound).genCode(ctx)
Expand Down Expand Up @@ -195,10 +175,10 @@ case class FilterExec(condition: Expression, child: SparkPlan)
if (idx != -1 && !generatedIsNotNullChecks(idx)) {
generatedIsNotNullChecks(idx) = true
// Use the child's output. The nullability is what the child produced.
genPredicate(notNullPreds(idx), input, child.output)
} else if (notNullAttributes.contains(r.exprId) && !extraIsNotNullAttrs.contains(r)) {
genPredicate(notNullPreds(idx), inputExprCode, inputAttrs)
} else if (nonNullAttrExprIds.contains(r.exprId) && !extraIsNotNullAttrs.contains(r)) {
extraIsNotNullAttrs += r
genPredicate(IsNotNull(r), input, child.output)
genPredicate(IsNotNull(r), inputExprCode, inputAttrs)
} else {
""
}
Expand All @@ -208,18 +188,61 @@ case class FilterExec(condition: Expression, child: SparkPlan)
// enforced them with the IsNotNull checks above.
s"""
|$nullChecks
|${genPredicate(c, input, output)}
|${genPredicate(c, inputExprCode, outputAttrs)}
""".stripMargin.trim
}.mkString("\n")

val nullChecks = notNullPreds.zipWithIndex.map { case (c, idx) =>
if (!generatedIsNotNullChecks(idx)) {
genPredicate(c, input, child.output)
genPredicate(c, inputExprCode, inputAttrs)
} else {
""
}
}.mkString("\n")

s"""
|$generated
|$nullChecks
""".stripMargin
}
}

/** Physical plan for Filter. */
case class FilterExec(condition: Expression, child: SparkPlan)
extends UnaryExecNode with CodegenSupport with GeneratePredicateHelper {

// Split out all the IsNotNulls from condition.
private val (notNullPreds, otherPreds) = splitConjunctivePredicates(condition).partition {
case IsNotNull(a) => isNullIntolerant(a) && a.references.subsetOf(child.outputSet)
case _ => false
}

// The columns that will filtered out by `IsNotNull` could be considered as not nullable.
private val notNullAttributes = notNullPreds.flatMap(_.references).distinct.map(_.exprId)

// Mark this as empty. We'll evaluate the input during doConsume(). We don't want to evaluate
// all the variables at the beginning to take advantage of short circuiting.
override def usedInputs: AttributeSet = AttributeSet.empty

override def output: Seq[Attribute] = outputWithNullability(child.output, notNullAttributes)

override lazy val metrics = Map(
"numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of output rows"))

override def inputRDDs(): Seq[RDD[InternalRow]] = {
child.asInstanceOf[CodegenSupport].inputRDDs()
}

protected override def doProduce(ctx: CodegenContext): String = {
child.asInstanceOf[CodegenSupport].produce(ctx, this)
}

override def doConsume(ctx: CodegenContext, input: Seq[ExprCode], row: ExprCode): String = {
val numOutput = metricTerm(ctx, "numOutputRows")

val predicateCode = generatePredicateCode(
ctx, child.output, input, output, notNullPreds, otherPreds, notNullAttributes)

// Reset the isNull to false for the not-null columns, then the followed operators could
// generate better code (remove dead branches).
val resultVars = input.zipWithIndex.map { case (ev, i) =>
Expand All @@ -232,8 +255,7 @@ case class FilterExec(condition: Expression, child: SparkPlan)
// Note: wrap in "do { } while(false);", so the generated checks can jump out with "continue;"
s"""
|do {
| $generated
| $nullChecks
| $predicateCode
| $numOutput.add(1);
| ${consume(ctx, resultVars)}
|} while(false);
Expand Down
Loading