You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by ma...@apache.org on 2014/07/19 18:04:50 UTC
git commit: SPARK-2407: Added Parser of SQL SUBSTR()
Repository: spark
Updated Branches:
refs/heads/master 805f329bb -> 2a732110d
SPARK-2407: Added Parser of SQL SUBSTR()
follow-up of #1359
Author: chutium <te...@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()
Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/2a732110
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/2a732110
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/2a732110
Branch: refs/heads/master
Commit: 2a732110d46712c535b75dd4f5a73761b6463aa8
Parents: 805f329
Author: chutium <te...@gmail.com>
Authored: Sat Jul 19 11:04:41 2014 -0500
Committer: Michael Armbrust <mi...@databricks.com>
Committed: Sat Jul 19 11:04:41 2014 -0500
----------------------------------------------------------------------
.../org/apache/spark/sql/catalyst/SqlParser.scala | 9 ++++++++-
.../scala/org/apache/spark/sql/SQLQuerySuite.scala | 15 +++++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/spark/blob/2a732110/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala
index e5653c5..a34b236 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/SqlParser.scala
@@ -120,7 +120,8 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
protected val WHERE = Keyword("WHERE")
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 =
@@ -316,6 +317,12 @@ class SqlParser extends StandardTokenParsers with PackratParsers {
IF ~> "(" ~> expression ~ "," ~ expression ~ "," ~ expression <~ ")" ^^ {
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, ",") <~ ")" ^^ {
case udfName ~ _ ~ exprs => UnresolvedFunction(udfName, exprs)
}
http://git-wip-us.apache.org/repos/asf/spark/blob/2a732110/sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
index 0743cfe..6736189 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
@@ -36,6 +36,21 @@ class SQLQuerySuite extends QueryTest {
"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") {
checkAnswer(
sql("SELECT data, data[0], data[0] + data[1], data[0 + 1] FROM arrayData"),