Repository: spark
Updated Branches:
  refs/heads/branch-2.0 b41294bb8 -> 8bf642226


[SPARK-19472][SQL] Parser should not mistake CASE WHEN(...) for a function call

## What changes were proposed in this pull request?
The SQL parser can mistake a `WHEN (...)` used in `CASE` for a function call. 
This happens in cases like the following:
```sql
select case when (1) + case when 1 > 0 then 1 else 0 end = 2 then 1 else 0 end
from tb
```
This PR fixes this by re-organizing the case related parsing rules.

## How was this patch tested?
Added a regression test to the `ExpressionParserSuite`.

Author: Herman van Hovell <hvanhov...@databricks.com>

Closes #16821 from hvanhovell/SPARK-19472.

(cherry picked from commit cb2677b86039a75fcd8a4e567ab06055f054a19a)
Signed-off-by: gatorsmile <gatorsm...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/8bf64222
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/8bf64222
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/8bf64222

Branch: refs/heads/branch-2.0
Commit: 8bf6422268d594b4313511dbae07c829160d57f0
Parents: b41294b
Author: Herman van Hovell <hvanhov...@databricks.com>
Authored: Mon Feb 6 15:28:13 2017 -0500
Committer: gatorsmile <gatorsm...@gmail.com>
Committed: Mon Feb 6 15:28:52 2017 -0500

----------------------------------------------------------------------
 .../main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4    | 2 +-
 .../apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala   | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/8bf64222/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4 
b/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4
index e405d47..ed0d749 100644
--- 
a/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4
+++ 
b/sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4
@@ -520,8 +520,8 @@ valueExpression
 
 primaryExpression
     : name=(CURRENT_DATE | CURRENT_TIMESTAMP)                                  
                #timeFunctionCall
-    | CASE value=expression whenClause+ (ELSE elseExpression=expression)? END  
                #simpleCase
     | CASE whenClause+ (ELSE elseExpression=expression)? END                   
                #searchedCase
+    | CASE value=expression whenClause+ (ELSE elseExpression=expression)? END  
                #simpleCase
     | CAST '(' expression AS dataType ')'                                      
                #cast
     | constant                                                                 
                #constantDefault
     | ASTERISK                                                                 
                #star

http://git-wip-us.apache.org/repos/asf/spark/blob/8bf64222/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala
----------------------------------------------------------------------
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala
index 15ebc19..d689a07 100644
--- 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ExpressionParserSuite.scala
@@ -298,6 +298,8 @@ class ExpressionParserSuite extends PlanTest {
       CaseKeyWhen("a" ===  "a", Seq(true, 1)))
     assertEqual("case when a = 1 then b when a = 2 then c else d end",
       CaseWhen(Seq(('a === 1, 'b.expr), ('a === 2, 'c.expr)), 'd))
+    assertEqual("case when (1) + case when a > b then c else d end then f else 
g end",
+      CaseWhen(Seq((Literal(1) + CaseWhen(Seq(('a > 'b, 'c.expr)), 'd.expr), 
'f.expr)), 'g))
   }
 
   test("dereference") {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to