Skip to content

Commit

Permalink
SPARK-2407: Added Parser of SQL SUBSTRING() #1442
Browse files Browse the repository at this point in the history
  • Loading branch information
chutium committed Jul 18, 2014
1 parent 9a60ccf commit b49cc8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
protected val INTERSECT = Keyword("INTERSECT")
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.
protected val reservedWords =
Expand Down Expand Up @@ -316,10 +317,10 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
IF ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ {
case c ~ "," ~ t ~ "," ~ f => If(c,t,f)
} |
SUBSTR ~> "(" ~> expression ~ "," ~ expression <~ ")" ^^ {
(SUBSTR | SUBSTRING) ~> "(" ~> expression ~ "," ~ expression <~ ")" ^^ {
case s ~ "," ~ p => Substring(s,p,Literal(Integer.MAX_VALUE))
} |
SUBSTR ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ {
(SUBSTR | SUBSTRING) ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ {
case s ~ "," ~ p ~ "," ~ l => Substring(s,p,l)
} |
ident ~ "(" ~ repsep(expression, ",") <~ ")" ^^ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class SQLQuerySuite extends QueryTest {
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") {
Expand Down

0 comments on commit b49cc8a

Please sign in to comment.