You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by rx...@apache.org on 2016/01/14 19:09:06 UTC
spark git commit: [SPARK-12771][SQL] Simplify CaseWhen code generation
Repository: spark
Updated Branches:
refs/heads/master 501e99ef0 -> 902667fd2
[SPARK-12771][SQL] Simplify CaseWhen code generation
The generated code for CaseWhen uses a control variable "got" to make sure we do not evaluate more branches once a branch is true. Changing that to generate just simple "if / else" would be slightly more efficient.
This closes #10737.
Author: Reynold Xin <rx...@databricks.com>
Closes #10755 from rxin/SPARK-12771.
Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/902667fd
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/902667fd
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/902667fd
Branch: refs/heads/master
Commit: 902667fd2766f0472a15851b1ed8fb5859593f97
Parents: 501e99e
Author: Reynold Xin <rx...@databricks.com>
Authored: Thu Jan 14 10:09:03 2016 -0800
Committer: Reynold Xin <rx...@databricks.com>
Committed: Thu Jan 14 10:09:03 2016 -0800
----------------------------------------------------------------------
.../expressions/conditionalExpressions.scala | 60 ++++++++++++--------
1 file changed, 35 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/spark/blob/902667fd/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala
index 8cc7bc1..83abbcd 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala
@@ -137,45 +137,55 @@ case class CaseWhen(branches: Seq[(Expression, Expression)], elseValue: Option[E
}
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
- val got = ctx.freshName("got")
-
- val cases = branches.map { case (condition, value) =>
- val cond = condition.gen(ctx)
- val res = value.gen(ctx)
+ // Generate code that looks like:
+ //
+ // condA = ...
+ // if (condA) {
+ // valueA
+ // } else {
+ // condB = ...
+ // if (condB) {
+ // valueB
+ // } else {
+ // condC = ...
+ // if (condC) {
+ // valueC
+ // } else {
+ // elseValue
+ // }
+ // }
+ // }
+ val cases = branches.map { case (condExpr, valueExpr) =>
+ val cond = condExpr.gen(ctx)
+ val res = valueExpr.gen(ctx)
s"""
- if (!$got) {
- ${cond.code}
- if (!${cond.isNull} && ${cond.value}) {
- $got = true;
- ${res.code}
- ${ev.isNull} = ${res.isNull};
- ${ev.value} = ${res.value};
- }
+ ${cond.code}
+ if (!${cond.isNull} && ${cond.value}) {
+ ${res.code}
+ ${ev.isNull} = ${res.isNull};
+ ${ev.value} = ${res.value};
}
"""
- }.mkString("\n")
+ }
- val elseCase = {
- if (elseValue.isDefined) {
- val res = elseValue.get.gen(ctx)
+ var generatedCode = cases.mkString("", "\nelse {\n", "\nelse {\n")
+
+ elseValue.foreach { elseExpr =>
+ val res = elseExpr.gen(ctx)
+ generatedCode +=
s"""
- if (!$got) {
${res.code}
${ev.isNull} = ${res.isNull};
${ev.value} = ${res.value};
- }
"""
- } else {
- ""
- }
}
+ generatedCode += "}\n" * cases.size
+
s"""
- boolean $got = false;
boolean ${ev.isNull} = true;
${ctx.javaType(dataType)} ${ev.value} = ${ctx.defaultValue(dataType)};
- $cases
- $elseCase
+ $generatedCode
"""
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org