You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2019/11/07 05:14:36 UTC

[GitHub] [spark] davidvrba commented on a change in pull request #26294: [SPARK-28477] [SQL] Rewrite CaseWhen with single branch to If

davidvrba commented on a change in pull request #26294: [SPARK-28477] [SQL] Rewrite CaseWhen with single branch to If
URL: https://github.com/apache/spark/pull/26294#discussion_r343479177
 
 

 ##########
 File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala
 ##########
 @@ -186,98 +186,106 @@ case class CaseWhen(
   }
 
   override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    // This variable holds the state of the result:
-    // -1 means the condition is not met yet and the result is unknown.
-    val NOT_MATCHED = -1
-    // 0 means the condition is met and result is not null.
-    val HAS_NONNULL = 0
-    // 1 means the condition is met and result is null.
-    val HAS_NULL = 1
-    // It is initialized to `NOT_MATCHED`, and if it's set to `HAS_NULL` or `HAS_NONNULL`,
-    // We won't go on anymore on the computation.
-    val resultState = ctx.freshName("caseWhenResultState")
-    ev.value = JavaCode.global(
-      ctx.addMutableState(CodeGenerator.javaType(dataType), ev.value),
-      dataType)
+    if (branches.length == 1) {
+      // If we have only single branch we can use If expression and its codeGen
+      If(
+        branches(0)._1,
+        branches(0)._2,
+        elseValue.getOrElse(Literal.create(null, branches(0)._2.dataType))).doGenCode(ctx, ev)
+    } else {
+      // This variable holds the state of the result:
 
 Review comment:
   yes, it makes sense

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

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