Skip to content

Commit 6be9189

Browse files
viiryamarmbrus
authored andcommitted
[SPARK-6871][SQL] WITH clause in CTE can not following another WITH clause
JIRA https://issues.apache.org/jira/browse/SPARK-6871 Author: Liang-Chi Hsieh <[email protected]> Closes apache#5480 from viirya/no_cte_after_cte and squashes the following commits: 4da3712 [Liang-Chi Hsieh] Create new test. 40b38ed [Liang-Chi Hsieh] Merge remote-tracking branch 'upstream/master' into no_cte_after_cte 0edf568 [Liang-Chi Hsieh] for comments. 6591b79 [Liang-Chi Hsieh] WITH clause in CTE can not following another WITH clause.
1 parent 30a6e0d commit 6be9189

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ class SqlParser extends AbstractSparkSQLParser with DataTypeParser {
121121
}
122122

123123
protected lazy val start: Parser[LogicalPlan] =
124-
( (select | ("(" ~> select <~ ")")) *
125-
( UNION ~ ALL ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Union(q1, q2) }
126-
| INTERSECT ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Intersect(q1, q2) }
127-
| EXCEPT ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Except(q1, q2)}
128-
| UNION ~ DISTINCT.? ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Distinct(Union(q1, q2)) }
129-
)
130-
| insert
131-
| cte
124+
start1 | insert | cte
125+
126+
protected lazy val start1: Parser[LogicalPlan] =
127+
(select | ("(" ~> select <~ ")")) *
128+
( UNION ~ ALL ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Union(q1, q2) }
129+
| INTERSECT ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Intersect(q1, q2) }
130+
| EXCEPT ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Except(q1, q2)}
131+
| UNION ~ DISTINCT.? ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Distinct(Union(q1, q2)) }
132132
)
133133

134134
protected lazy val select: Parser[LogicalPlan] =
@@ -159,7 +159,7 @@ class SqlParser extends AbstractSparkSQLParser with DataTypeParser {
159159
}
160160

161161
protected lazy val cte: Parser[LogicalPlan] =
162-
WITH ~> rep1sep(ident ~ ( AS ~ "(" ~> start <~ ")"), ",") ~ start ^^ {
162+
WITH ~> rep1sep(ident ~ ( AS ~ "(" ~> start1 <~ ")"), ",") ~ (start1 | insert) ^^ {
163163
case r ~ s => With(s, r.map({case n ~ s => (n, Subquery(n, s))}).toMap)
164164
}
165165

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
431431

432432
}
433433

434+
test("Allow only a single WITH clause per query") {
435+
intercept[RuntimeException] {
436+
sql("with q1 as (select * from testData) with q2 as (select * from q1) select * from q2")
437+
}
438+
}
439+
434440
test("date row") {
435441
checkAnswer(sql(
436442
"""select cast("2015-01-28" as date) from testData limit 1"""),

0 commit comments

Comments
 (0)