Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/10737#discussion_r49674518
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala
---
@@ -137,45 +137,50 @@ case class CaseWhen(branches: Seq[(Expression,
Expression)], elseValue: Option[E
}
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
- val got = ctx.freshName("got")
+ val len = branches.length
- val cases = branches.map { case (condition, value) =>
- val cond = condition.gen(ctx)
- val res = value.gen(ctx)
- s"""
- if (!$got) {
+ def genBranch(branchIndex: Int): String =
+ if (branchIndex >= len) {
+ if (elseValue.isDefined) {
+ val res = elseValue.get.gen(ctx)
+ s"""
+ ${res.code}
+ ${ev.isNull} = ${res.isNull};
+ ${ev.value} = ${res.value};
+ """
+ } else {
+ ""
+ }
+ } else {
+ val cond = branches(branchIndex)._1.gen(ctx)
+ val res = branches(branchIndex)._2.gen(ctx)
+
+ val currentBranch = s"""
${cond.code}
if (!${cond.isNull} && ${cond.value}) {
- $got = true;
${res.code}
${ev.isNull} = ${res.isNull};
${ev.value} = ${res.value};
}
- }
- """
- }.mkString("\n")
-
- val elseCase = {
- if (elseValue.isDefined) {
- val res = elseValue.get.gen(ctx)
- s"""
- if (!$got) {
- ${res.code}
- ${ev.isNull} = ${res.isNull};
- ${ev.value} = ${res.value};
- }
"""
- } else {
- ""
+
+ val moreBranch = genBranch(branchIndex + 1)
+ if (moreBranch != "") {
+ s"""
+ $currentBranch
+ else {
+ $moreBranch
--- End diff --
is this more efficient? We will generate deep `if/else` branch then.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]