Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/10737#discussion_r49690466
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala
---
@@ -137,45 +137,65 @@ case class CaseWhen(branches: Seq[(Expression,
Expression)], elseValue: Option[E
}
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
- val got = ctx.freshName("got")
+ val firstCase = {
+ val cond = branches.head._1.gen(ctx)
+ val res = branches.head._2.gen(ctx)
+ s"""
+ ${cond.code}
+ if (!${cond.isNull} && ${cond.value}) {
+ ${res.code}
+ ${ev.isNull} = ${res.isNull};
+ ${ev.value} = ${res.value};
+ }
+ """
+ }
- val cases = branches.map { case (condition, value) =>
+ val otherCases = branches.tail.map { case (condition, value) =>
val cond = condition.gen(ctx)
val res = value.gen(ctx)
+
s"""
- if (!$got) {
+ else {
${cond.code}
if (!${cond.isNull} && ${cond.value}) {
- $got = true;
${res.code}
${ev.isNull} = ${res.isNull};
${ev.value} = ${res.value};
}
- }
"""
- }.mkString("\n")
+ }
+
+ val cases = firstCase + otherCases.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 {
+ ${res.code}
+ ${ev.isNull} = ${res.isNull};
+ ${ev.value} = ${res.value};
+ }
"""
} else {
""
}
}
+ val leftBrackets = {
+ if (otherCases.length > 0) {
+ (0 until otherCases.length).map("}").mkString("\n")
--- End diff --
this is just
```
"}\n" * otherCases.length
```
---
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]