Github user liancheng commented on a diff in the pull request:
https://github.com/apache/spark/pull/2678#discussion_r18468334
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala ---
@@ -333,6 +338,24 @@ class SqlParser extends StandardTokenParsers with
PackratParsers {
IF ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")"
^^ {
case c ~ "," ~ t ~ "," ~ f => If(c,t,f)
} |
+ CASE ~> opt(expression) ~ (WHEN ~ expression ~ THEN ~ expression).* ~
+ opt(ELSE ~> expression) <~ END ^^ {
+ case c ~ l ~ el =>
+ var caseWhenExpr = l.map{x =>
+ x match {
+ case w ~ we ~ t ~ te =>
+ c match {
+ case Some(e) => Seq(EqualTo(e, we), te)
+ case None => Seq(we, te)
+ }
+ }
+ }.toSeq.reduce(_ ++ _)
+ caseWhenExpr = el match {
+ case Some(e) => caseWhenExpr ++ Seq(e)
+ case None => caseWhenExpr
+ }
+ CaseWhen(caseWhenExpr)
+ } |
--- End diff --
Haven't tested yet, but I think this parser can be simplified to:
```scala
CASE ~> expression.? ~ (WHEN ~> expression ~ (THEN ~> expression)).* ~
(ELSE ~> expression).? <~ END ^^ {
case casePart ~ alternatives ~ elsePart =>
val alternativesPart= alternatives.flatMap { case whenExpr ~
thenExpr =>
casePart.fold(whenExpr)(EqualTo(_, whenExpr)) :: thenExpr :: Nil
}
CaseWhen(alternativesPart ++ elsePart.toList)
}
```
---
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]