Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
SPARK-2407: Added Parser of SQL SUBSTRING() #1442
  • Loading branch information
chutium committed Jul 18, 2014
commit b49cc8a5bb73ff25c289a38cbaedfaf7edfefc5b
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