Github user viirya commented on a diff in the pull request:
https://github.com/apache/spark/pull/10755#discussion_r49700975
--- Diff:
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")
--- End diff --
Yes.
---
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]