Skip to content

Commit

Permalink
WITH clause in CTE can not following another WITH clause.
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Apr 12, 2015
1 parent 5d8f7b9 commit 6591b79
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ class SqlParser extends AbstractSparkSQLParser with DataTypeParser {
}

protected lazy val start: Parser[LogicalPlan] =
( (select | ("(" ~> select <~ ")")) *
( UNION ~ ALL ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Union(q1, q2) }
| INTERSECT ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Intersect(q1, q2) }
| EXCEPT ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Except(q1, q2)}
| UNION ~ DISTINCT.? ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Distinct(Union(q1, q2)) }
)
| insert
| cte
( start1 | insert | cte )

protected lazy val start1: Parser[LogicalPlan] =
(select | ("(" ~> select <~ ")")) *
( UNION ~ ALL ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Union(q1, q2) }
| INTERSECT ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Intersect(q1, q2) }
| EXCEPT ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Except(q1, q2)}
| UNION ~ DISTINCT.? ^^^ { (q1: LogicalPlan, q2: LogicalPlan) => Distinct(Union(q1, q2)) }
)

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
sql("with q1 as (select * from testData limit 10) select * from q1"),
testData.take(10).toSeq)

intercept[RuntimeException] {
sql("with q1 as (select * from testData) with q2 as (select * from q1) select * from q2")
}

checkAnswer(
sql("""
|with q1 as (select * from testData where key= '5'),
Expand Down

0 comments on commit 6591b79

Please sign in to comment.