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

[Only Test][Don't Review] Make FilterExec to support subexpressionElimination in the codegen scenario #49573

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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 @@ -21,6 +21,7 @@ import java.util.concurrent.{Future => JFuture}
import java.util.concurrent.TimeUnit._

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.Duration

Expand Down Expand Up @@ -214,6 +215,87 @@ trait GeneratePredicateHelper extends PredicateHelper {
|$nullChecks
""".stripMargin
}

protected def subexpressionEliminationGeneratePredicateCode(
ctx: CodegenContext,
inputAttrs: Seq[Attribute],
inputExprCode: Seq[ExprCode],
outputAttrs: Seq[Attribute],
notNullPreds: Seq[Expression],
otherPreds: Seq[Expression],
nonNullAttrExprIds: Seq[ExprId]): String = {

def genPredicate(
bound: Expression,
unbound: Expression,
in: Seq[ExprCode],
attrs: Seq[Attribute]): String = {
val evaluated = evaluateRequiredVariables(attrs, in, unbound.references)

// Generate the code for the predicate.
val ev = ExpressionCanonicalizer.execute(bound).genCode(ctx)
val nullCheck = if (bound.nullable) { s"${ev.isNull} || " } else { "" }
s"""
|$evaluated
|${ev.code}
|if ($nullCheck!${ev.value}) continue;
""".stripMargin
}

val boundOtherPreds = bindReferences[Expression](otherPreds, inputAttrs)
val boundNotNullPreds = bindReferences[Expression](notNullPreds, inputAttrs)
val boundExprs = boundOtherPreds ++ boundNotNullPreds
val subExprs = ctx.subexpressionEliminationForWholeStageCodegen(boundExprs)
val commonPredicateCodes = ctx.evaluateSubExprEliminationState(subExprs.states.values)
val predicateCodes = ArrayBuffer[String]()
ctx.withSubExprEliminationExprs(subExprs.states) {
val generatedIsNotNullChecks = new Array[Boolean](notNullPreds.length)
val extraIsNotNullAttrs = mutable.Set[Attribute]()
val generated = otherPreds.zipWithIndex.map { case (c, index) =>
val nullChecks = c.references.map { r =>
val idx = notNullPreds.indexWhere {
n => n.asInstanceOf[IsNotNull].child.semanticEquals(r)}
if (idx != -1 && !generatedIsNotNullChecks(idx)) {
generatedIsNotNullChecks(idx) = true
// Use the child's output. The nullability is what the child produced.
genPredicate(boundNotNullPreds(idx), notNullPreds(idx), inputExprCode, inputAttrs)
} else if (nonNullAttrExprIds.contains(r.exprId) && !extraIsNotNullAttrs.contains(r)) {
val newIsNotNull = IsNotNull(r)
extraIsNotNullAttrs += r
genPredicate(BindReferences.bindReference(newIsNotNull, inputAttrs),
newIsNotNull, inputExprCode, inputAttrs)
} else {
""
}
}.mkString("\n").trim

// Here we use *this* operator's output with this output's nullability since we already
// enforced them with the IsNotNull checks above.
s"""
|$nullChecks
|${genPredicate(boundOtherPreds(index), c, inputExprCode, outputAttrs)}
""".stripMargin.trim
}.mkString("\n")

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

predicateCodes.append(generated)
predicateCodes.append(nullChecks)

Seq.empty // workaround for `withSubExprEliminationExprs`
}
s"""
|${evaluateVariables(subExprs.exprCodesNeedEvaluate)}
|$commonPredicateCodes
|${predicateCodes.mkString("\n")}
""".stripMargin
}
}

/** Physical plan for Filter. */
Expand Down Expand Up @@ -248,9 +330,13 @@ case class FilterExec(condition: Expression, child: SparkPlan)

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)
val predicateCode = if (conf.subexpressionEliminationEnabled) {
subexpressionEliminationGeneratePredicateCode(
ctx, child.output, input, output, notNullPreds, otherPreds, notNullAttributes)
} else {
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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object SubExprEliminationBenchmark extends SqlBasedBenchmark {

withTempPath { path =>
prepareDataInfo(benchmark)
val numCols = 500
val numCols = 330
val schema = writeWideRow(path.getAbsolutePath, rowsNum, numCols)

val jsonValue = from_json($"value", schema)
Expand Down
Loading