aboutsummaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorLiang-Chi Hsieh <viirya@gmail.com>2016-02-21 19:10:17 -0800
committerReynold Xin <rxin@databricks.com>2016-02-21 19:10:17 -0800
commit55d6fdf22d1d6379180ac09f364c38982897d9ff (patch)
tree640a95deb129947dedae19fe70eab06e782dc215 /sql
parent3d79f1065cd02133ad9dd4423c09b8c8b52b38e2 (diff)
downloadspark-55d6fdf22d1d6379180ac09f364c38982897d9ff.tar.gz
spark-55d6fdf22d1d6379180ac09f364c38982897d9ff.tar.bz2
spark-55d6fdf22d1d6379180ac09f364c38982897d9ff.zip
[SPARK-13321][SQL] Support nested UNION in parser
JIRA: https://issues.apache.org/jira/browse/SPARK-13321 The following SQL can not be parsed with current parser: SELECT `u_1`.`id` FROM (((SELECT `t0`.`id` FROM `default`.`t0`) UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`)) UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`)) AS u_1 We should fix it. Author: Liang-Chi Hsieh <viirya@gmail.com> Closes #11204 from viirya/nested-union.
Diffstat (limited to 'sql')
-rw-r--r--sql/catalyst/src/main/antlr3/org/apache/spark/sql/catalyst/parser/SparkSqlParser.g20
-rw-r--r--sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/CatalystQlSuite.scala64
2 files changed, 84 insertions, 0 deletions
diff --git a/sql/catalyst/src/main/antlr3/org/apache/spark/sql/catalyst/parser/SparkSqlParser.g b/sql/catalyst/src/main/antlr3/org/apache/spark/sql/catalyst/parser/SparkSqlParser.g
index e1908a8e03..9aeea69cd2 100644
--- a/sql/catalyst/src/main/antlr3/org/apache/spark/sql/catalyst/parser/SparkSqlParser.g
+++ b/sql/catalyst/src/main/antlr3/org/apache/spark/sql/catalyst/parser/SparkSqlParser.g
@@ -2320,6 +2320,26 @@ regularBody[boolean topLevel]
)
|
selectStatement[topLevel]
+ |
+ (LPAREN selectStatement0[true]) => nestedSetOpSelectStatement[topLevel]
+ ;
+
+nestedSetOpSelectStatement[boolean topLevel]
+ :
+ (
+ LPAREN s=selectStatement0[topLevel] RPAREN -> {$s.tree}
+ )
+ (set=setOpSelectStatement[$nestedSetOpSelectStatement.tree, topLevel])
+ -> {set == null}?
+ {$nestedSetOpSelectStatement.tree}
+ -> {$set.tree}
+ ;
+
+selectStatement0[boolean topLevel]
+ :
+ (selectStatement[true]) => selectStatement[topLevel]
+ |
+ (nestedSetOpSelectStatement[true]) => nestedSetOpSelectStatement[topLevel]
;
selectStatement[boolean topLevel]
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/CatalystQlSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/CatalystQlSuite.scala
index 42147f516f..1d1c0cb216 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/CatalystQlSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/CatalystQlSuite.scala
@@ -203,6 +203,70 @@ class CatalystQlSuite extends PlanTest {
"from windowData")
}
+ test("nesting UNION") {
+ val parsed = parser.parsePlan(
+ """
+ |SELECT `u_1`.`id` FROM (((SELECT `t0`.`id` FROM `default`.`t0`)
+ |UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`)) UNION ALL
+ |(SELECT `t0`.`id` FROM `default`.`t0`)) AS u_1
+ """.stripMargin)
+
+ val expected = Project(
+ UnresolvedAlias(UnresolvedAttribute("u_1.id"), None) :: Nil,
+ Subquery("u_1",
+ Union(
+ Union(
+ Project(
+ UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
+ UnresolvedRelation(TableIdentifier("t0", Some("default")), None)),
+ Project(
+ UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
+ UnresolvedRelation(TableIdentifier("t0", Some("default")), None))),
+ Project(
+ UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
+ UnresolvedRelation(TableIdentifier("t0", Some("default")), None)))))
+
+ comparePlans(parsed, expected)
+
+ val parsedSame = parser.parsePlan(
+ """
+ |SELECT `u_1`.`id` FROM ((SELECT `t0`.`id` FROM `default`.`t0`)
+ |UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`) UNION ALL
+ |(SELECT `t0`.`id` FROM `default`.`t0`)) AS u_1
+ """.stripMargin)
+
+ comparePlans(parsedSame, expected)
+
+ val parsed2 = parser.parsePlan(
+ """
+ |SELECT `u_1`.`id` FROM ((((SELECT `t0`.`id` FROM `default`.`t0`)
+ |UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`)) UNION ALL
+ |(SELECT `t0`.`id` FROM `default`.`t0`))
+ |UNION ALL (SELECT `t0`.`id` FROM `default`.`t0`)) AS u_1
+ """.stripMargin)
+
+ val expected2 = Project(
+ UnresolvedAlias(UnresolvedAttribute("u_1.id"), None) :: Nil,
+ Subquery("u_1",
+ Union(
+ Union(
+ Union(
+ Project(
+ UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
+ UnresolvedRelation(TableIdentifier("t0", Some("default")), None)),
+ Project(
+ UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
+ UnresolvedRelation(TableIdentifier("t0", Some("default")), None))),
+ Project(
+ UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
+ UnresolvedRelation(TableIdentifier("t0", Some("default")), None))),
+ Project(
+ UnresolvedAlias(UnresolvedAttribute("t0.id"), None) :: Nil,
+ UnresolvedRelation(TableIdentifier("t0", Some("default")), None)))))
+
+ comparePlans(parsed2, expected2)
+ }
+
test("subquery") {
parser.parsePlan("select (select max(b) from s) ss from t")
parser.parsePlan("select * from t where a = (select b from s)")