gengliangwang commented on code in PR #56971:
URL: https://github.com/apache/spark/pull/56971#discussion_r3530515207


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/NullExpressionsSuite.scala:
##########
@@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.expressions
 import java.sql.Timestamp
 
 import org.apache.spark.{SparkFunSuite, SparkRuntimeException}
+import org.apache.spark.sql.catalyst.InternalRow

Review Comment:
   Fixed — reordered so `FunctionIdentifier` precedes `InternalRow`.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/nullExpressions.scala:
##########
@@ -93,11 +93,37 @@ case class Coalesce(children: Seq[Expression])
   }
 
   override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    // Fast path for the common `coalesce(a, b)` where `b` is non-nullable and 
its evaluation
+    // generates no code (a literal or an already-evaluated value), e.g. the 
`coalesce(sum, 0)`
+    // emitted for every SUM/AVG update. The result can never be null and `b` 
carries a pure
+    // Java expression, so a single ternary replaces the general do-while 
block below and no
+    // global mutable isNull state is needed. The empty-code requirement also 
preserves lazy
+    // evaluation: there are no statements of `b` to hoist before the null 
check.
+    val probedEvals: Option[Seq[ExprCode]] =
+      if (children.length == 2 && !children(1).nullable) {
+        val first = children(0).genCode(ctx)
+        val second = children(1).genCode(ctx)
+        if (second.code.isEmpty) {
+          val resultType = CodeGenerator.javaType(dataType)

Review Comment:
   Good catch — hoisted `val resultType = CodeGenerator.javaType(dataType)` to 
the top of `doGenCode` so it's computed once and shared by both the fast path 
and the general path.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/nullExpressions.scala:
##########
@@ -93,11 +93,37 @@ case class Coalesce(children: Seq[Expression])
   }
 
   override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    // Fast path for the common `coalesce(a, b)` where `b` is non-nullable and 
its evaluation
+    // generates no code (a literal or an already-evaluated value), e.g. the 
`coalesce(sum, 0)`
+    // emitted for every SUM/AVG update. The result can never be null and `b` 
carries a pure
+    // Java expression, so a single ternary replaces the general do-while 
block below and no
+    // global mutable isNull state is needed. The empty-code requirement also 
preserves lazy
+    // evaluation: there are no statements of `b` to hoist before the null 
check.
+    val probedEvals: Option[Seq[ExprCode]] =
+      if (children.length == 2 && !children(1).nullable) {
+        val first = children(0).genCode(ctx)

Review Comment:
   Done — switched to `children.head`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to