Skip to content
Snippets Groups Projects
Commit 2a732110 authored by chutium's avatar chutium Committed by Michael Armbrust
Browse files

SPARK-2407: Added Parser of SQL SUBSTR()

follow-up of #1359

Author: chutium <teng.qiu@gmail.com>

Closes #1442 from chutium/master and squashes the following commits:

b49cc8a [chutium] SPARK-2407: Added Parser of SQL SUBSTRING() #1442
9a60ccf [chutium] SPARK-2407: Added Parser of SQL SUBSTR() #1442
06e933b [chutium] Merge https://github.com/apache/spark
c870172 [chutium] Merge https://github.com/apache/spark
094f773 [chutium] Merge https://github.com/apache/spark
88cb37d [chutium] Merge https://github.com/apache/spark
1de83a7 [chutium] SPARK-2407: Added Parse of SQL SUBSTR()
parent 805f329b
No related branches found
No related tags found
No related merge requests found
...@@ -120,7 +120,8 @@ class SqlParser extends StandardTokenParsers with PackratParsers { ...@@ -120,7 +120,8 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
protected val WHERE = Keyword("WHERE") protected val WHERE = Keyword("WHERE")
protected val INTERSECT = Keyword("INTERSECT") protected val INTERSECT = Keyword("INTERSECT")
protected val EXCEPT = Keyword("EXCEPT") protected val EXCEPT = Keyword("EXCEPT")
protected val SUBSTR = Keyword("SUBSTR")
protected val SUBSTRING = Keyword("SUBSTRING")
// Use reflection to find the reserved words defined in this class. // Use reflection to find the reserved words defined in this class.
protected val reservedWords = protected val reservedWords =
...@@ -316,6 +317,12 @@ class SqlParser extends StandardTokenParsers with PackratParsers { ...@@ -316,6 +317,12 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
IF ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ { IF ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ {
case c ~ "," ~ t ~ "," ~ f => If(c,t,f) case c ~ "," ~ t ~ "," ~ f => If(c,t,f)
} | } |
(SUBSTR | SUBSTRING) ~> "(" ~> expression ~ "," ~ expression <~ ")" ^^ {
case s ~ "," ~ p => Substring(s,p,Literal(Integer.MAX_VALUE))
} |
(SUBSTR | SUBSTRING) ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ {
case s ~ "," ~ p ~ "," ~ l => Substring(s,p,l)
} |
ident ~ "(" ~ repsep(expression, ",") <~ ")" ^^ { ident ~ "(" ~ repsep(expression, ",") <~ ")" ^^ {
case udfName ~ _ ~ exprs => UnresolvedFunction(udfName, exprs) case udfName ~ _ ~ exprs => UnresolvedFunction(udfName, exprs)
} }
......
...@@ -36,6 +36,21 @@ class SQLQuerySuite extends QueryTest { ...@@ -36,6 +36,21 @@ class SQLQuerySuite extends QueryTest {
"test") "test")
} }
test("SPARK-2407 Added Parser of SQL SUBSTR()") {
checkAnswer(
sql("SELECT substr(tableName, 1, 2) FROM tableName"),
"te")
checkAnswer(
sql("SELECT substr(tableName, 3) FROM tableName"),
"st")
checkAnswer(
sql("SELECT substring(tableName, 1, 2) FROM tableName"),
"te")
checkAnswer(
sql("SELECT substring(tableName, 3) FROM tableName"),
"st")
}
test("index into array") { test("index into array") {
checkAnswer( checkAnswer(
sql("SELECT data, data[0], data[0] + data[1], data[0 + 1] FROM arrayData"), sql("SELECT data, data[0], data[0] + data[1], data[0 + 1] FROM arrayData"),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment