Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11792#discussion_r57605732
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/basicOperators.scala ---
    @@ -110,39 +114,81 @@ case class Filter(condition: Expression, child: 
SparkPlan)
       override def doConsume(ctx: CodegenContext, input: Seq[ExprCode], row: 
String): String = {
         val numOutput = metricTerm(ctx, "numOutputRows")
     
    -    // filter out the nulls
    -    val filterOutNull = notNullAttributes.map { a =>
    -      val idx = child.output.indexOf(a)
    -      s"if (${input(idx).isNull}) continue;"
    -    }.mkString("\n")
    +    /**
    +     * 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)
     
    -    ctx.currentVars = input
    -    val predicates = otherPreds.map { e =>
    -      val bound = ExpressionCanonicalizer.execute(
    -        BindReferences.bindReference(e, output))
    -      val ev = bound.gen(ctx)
    +      // Generate the code for the predicate.
    +      val ev = ExpressionCanonicalizer.execute(bound).gen(ctx)
           val nullCheck = if (bound.nullable) {
             s"${ev.isNull} || "
           } else {
             s""
           }
    +
           s"""
    +         |$evaluated
              |${ev.code}
              |if (${nullCheck}!${ev.value}) continue;
            """.stripMargin
    +    }
    +
    +    ctx.currentVars = input
    +
    +    // To generate the predicates we will follow this algorithm.
    +    // For each predicate that is not IsNotNull, we will generate them one 
by one loading attributes
    +    // as necessary. For each of both attributes, if there is a IsNotNull 
predicate we will generate
    +    // that check *before* the predicate. After all of these predicates, 
we will generate the
    +    // remaining IsNotNull checks that were not part of other predicates.
    +    // This has the property of not doing redundant IsNotNull checks and 
taking better advantage of
    +    // short-circuiting, not loading attributes until they are needed.
    +    // This is very perf sensitive.
    +    // TODO: revisit this. We can consider reodering predicates as well.
    +    val generatedIsNotNullChecks = new Array[Boolean](notNullPreds.length)
    +    val generated = otherPreds.map { c =>
    +      val nullChecks = c.references.map { r =>
    +        val idx = notNullPreds.indexWhere { n => 
n.asInstanceOf[IsNotNull].child.semanticEquals(r)}
    +        if (idx != -1 && !generatedIsNotNullChecks(idx)) {
    +          // Use the child's output. The nullability is what the child 
produced.
    +          val code = genPredicate(notNullPreds(idx), input, child.output)
    +          generatedIsNotNullChecks(idx) = true
    +          code
    +        } 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(c, input, output)}
    +       """.stripMargin.trim
    +    }.mkString("\n")
    +
    +    val nullChecks = notNullPreds.zipWithIndex.map { case (c, idx) =>
    +      if (!generatedIsNotNullChecks(idx)) {
    +        genPredicate(c, input, child.output)
    +      } else {
    +        ""
    +      }
         }.mkString("\n")
     
         // Reset the isNull to false for the not-null columns, then the 
followed operators could
    -    // generate better code (remove dead branches).
    +    // generate better code (remove dead branches).                        
                      O
    --- End diff --
    
    ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to