Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ identifierComment

relationPrimary
: tableIdentifier sample? tableAlias #tableName
| '(' queryNoWith ')' sample? (AS? strictIdentifier) #aliasedQuery
| '(' queryNoWith ')' sample? (AS? strictIdentifier)? #aliasedQuery
| '(' relation ')' sample? (AS? strictIdentifier)? #aliasedRelation
| inlineTable #inlineTableDefault2
| functionTable #tableValuedFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,13 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
* hooks.
*/
override def visitAliasedQuery(ctx: AliasedQueryContext): LogicalPlan = withOrigin(ctx) {
// The unaliased subqueries in the FROM clause are disallowed. Instead of rejecting it in
// parser rules, we handle it here in order to provide better error message.
if (ctx.strictIdentifier == null) {
throw new ParseException("The unaliased subqueries in the FROM clause are not supported.",
ctx)
}

aliasPlan(ctx.strictIdentifier,
plan(ctx.queryNoWith).optionalMap(ctx.sample)(withSample))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,15 @@ class PlanParserSuite extends PlanTest {
}

test("aliased subquery") {
val errMsg = "The unaliased subqueries in the FROM clause are not supported"

assertEqual("select a from (select id as a from t0) tt",
table("t0").select('id.as("a")).as("tt").select('a))
intercept("select a from (select id as a from t0)", "mismatched input")
intercept("select a from (select id as a from t0)", errMsg)

assertEqual("from (select id as a from t0) tt select a",
table("t0").select('id.as("a")).as("tt").select('a))
intercept("from (select id as a from t0) select a", "extraneous input 'a'")
intercept("from (select id as a from t0) select a", errMsg)
}

test("scalar sub-query") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Aliased subqueries in FROM clause
SELECT * FROM (SELECT * FROM testData) AS t WHERE key = 1;

FROM (SELECT * FROM testData WHERE key = 1) AS t SELECT *;

-- Optional `AS` keyword
SELECT * FROM (SELECT * FROM testData) t WHERE key = 1;

FROM (SELECT * FROM testData WHERE key = 1) t SELECT *;

-- Disallow unaliased subqueries in FROM clause
SELECT * FROM (SELECT * FROM testData) WHERE key = 1;

FROM (SELECT * FROM testData WHERE key = 1) SELECT *;
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 6


-- !query 0
SELECT * FROM (SELECT * FROM testData) AS t WHERE key = 1
-- !query 0 schema
struct<key:int,value:string>
-- !query 0 output
1 1


-- !query 1
FROM (SELECT * FROM testData WHERE key = 1) AS t SELECT *
-- !query 1 schema
struct<key:int,value:string>
-- !query 1 output
1 1


-- !query 2
SELECT * FROM (SELECT * FROM testData) t WHERE key = 1
-- !query 2 schema
struct<key:int,value:string>
-- !query 2 output
1 1


-- !query 3
FROM (SELECT * FROM testData WHERE key = 1) t SELECT *
-- !query 3 schema
struct<key:int,value:string>
-- !query 3 output
1 1


-- !query 4
SELECT * FROM (SELECT * FROM testData) WHERE key = 1
-- !query 4 schema
struct<>
-- !query 4 output
org.apache.spark.sql.catalyst.parser.ParseException

The unaliased subqueries in the FROM clause are not supported.(line 1, pos 14)

== SQL ==
SELECT * FROM (SELECT * FROM testData) WHERE key = 1
--------------^^^


-- !query 5
FROM (SELECT * FROM testData WHERE key = 1) SELECT *
-- !query 5 schema
struct<>
-- !query 5 output
org.apache.spark.sql.catalyst.parser.ParseException

The unaliased subqueries in the FROM clause are not supported.(line 1, pos 5)

== SQL ==
FROM (SELECT * FROM testData WHERE key = 1) SELECT *
-----^^^