Skip to content

Commit 2a73211

Browse files
chutiummarmbrus
authored andcommitted
SPARK-2407: Added Parser of SQL SUBSTR()
follow-up of #1359 Author: chutium <[email protected]> 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()
1 parent 805f329 commit 2a73211

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
120120
protected val WHERE = Keyword("WHERE")
121121
protected val INTERSECT = Keyword("INTERSECT")
122122
protected val EXCEPT = Keyword("EXCEPT")
123-
123+
protected val SUBSTR = Keyword("SUBSTR")
124+
protected val SUBSTRING = Keyword("SUBSTRING")
124125

125126
// Use reflection to find the reserved words defined in this class.
126127
protected val reservedWords =
@@ -316,6 +317,12 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
316317
IF ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ {
317318
case c ~ "," ~ t ~ "," ~ f => If(c,t,f)
318319
} |
320+
(SUBSTR | SUBSTRING) ~> "(" ~> expression ~ "," ~ expression <~ ")" ^^ {
321+
case s ~ "," ~ p => Substring(s,p,Literal(Integer.MAX_VALUE))
322+
} |
323+
(SUBSTR | SUBSTRING) ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ {
324+
case s ~ "," ~ p ~ "," ~ l => Substring(s,p,l)
325+
} |
319326
ident ~ "(" ~ repsep(expression, ",") <~ ")" ^^ {
320327
case udfName ~ _ ~ exprs => UnresolvedFunction(udfName, exprs)
321328
}

sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ class SQLQuerySuite extends QueryTest {
3636
"test")
3737
}
3838

39+
test("SPARK-2407 Added Parser of SQL SUBSTR()") {
40+
checkAnswer(
41+
sql("SELECT substr(tableName, 1, 2) FROM tableName"),
42+
"te")
43+
checkAnswer(
44+
sql("SELECT substr(tableName, 3) FROM tableName"),
45+
"st")
46+
checkAnswer(
47+
sql("SELECT substring(tableName, 1, 2) FROM tableName"),
48+
"te")
49+
checkAnswer(
50+
sql("SELECT substring(tableName, 3) FROM tableName"),
51+
"st")
52+
}
53+
3954
test("index into array") {
4055
checkAnswer(
4156
sql("SELECT data, data[0], data[0] + data[1], data[0 + 1] FROM arrayData"),

0 commit comments

Comments
 (0)