Skip to content
Snippets Groups Projects
Commit 35b644bd authored by Liang-Chi Hsieh's avatar Liang-Chi Hsieh Committed by Wenchen Fan
Browse files

[SPARK-20916][SQL] Improve error message for unaliased subqueries in FROM clause

## What changes were proposed in this pull request?

We changed the parser to reject unaliased subqueries in the FROM clause in SPARK-20690. However, the error message that we now give isn't very helpful:

    scala> sql("""SELECT x FROM (SELECT 1 AS x)""")
    org.apache.spark.sql.catalyst.parser.ParseException:
    mismatched input 'FROM' expecting {<EOF>, 'WHERE', 'GROUP', 'ORDER', 'HAVING', 'LIMIT', 'LATERAL', 'WINDOW', 'UNION', 'EXCEPT', 'MINUS', 'INTERSECT', 'SORT', 'CLUSTER', 'DISTRIBUTE'}(line 1, pos 9)

We should modify the parser to throw a more clear error for such queries:

    scala> sql("""SELECT x FROM (SELECT 1 AS x)""")
    org.apache.spark.sql.catalyst.parser.ParseException:
    The unaliased subqueries in the FROM clause are not supported.(line 1, pos 14)

## How was this patch tested?

Modified existing tests to reflect this change.

Author: Liang-Chi Hsieh <viirya@gmail.com>

Closes #18141 from viirya/SPARK-20916.
parent 80fb24b8
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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))
}
......
......@@ -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") {
......
-- 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 *;
-- 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 *
-----^^^
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment