-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-21335][SQL] support un-aliased subquery #18559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -751,15 +751,17 @@ class AstBuilder 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) | ||
| val alias = if (ctx.strictIdentifier == null) { | ||
| // For un-aliased subqueries, ues a default alias name that is not likely to conflict with | ||
| // normal subquery names, so that parent operators can only access the columns in subquery by | ||
| // unqualified names. Users can still use this special qualifier to access columns if they | ||
| // know it, but that's not recommended. | ||
| "_auto_generated_subquery_name" | ||
|
||
| } else { | ||
| ctx.strictIdentifier.getText | ||
| } | ||
|
|
||
| aliasPlan(ctx.strictIdentifier, | ||
| plan(ctx.queryNoWith).optionalMap(ctx.sample)(withSample)) | ||
| SubqueryAlias(alias, plan(ctx.queryNoWith).optionalMap(ctx.sample)(withSample)) | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ AND t2b = (SELECT max(avg) | |
| FROM (SELECT t2b, avg(t2b) avg | ||
| FROM t2 | ||
| WHERE t2a = t1.t1b | ||
| ) T | ||
| ) | ||
| ) | ||
| ; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,20 +30,20 @@ abc | |
|
|
||
| -- !query 3 | ||
| EXPLAIN EXTENDED SELECT (col1 || col2 || col3 || col4) col | ||
| FROM (SELECT id col1, id col2, id col3, id col4 FROM range(10)) t | ||
| FROM (SELECT id col1, id col2, id col3, id col4 FROM range(10)) | ||
| -- !query 3 schema | ||
| struct<plan:string> | ||
| -- !query 3 output | ||
| == Parsed Logical Plan == | ||
| 'Project [concat(concat(concat('col1, 'col2), 'col3), 'col4) AS col#x] | ||
| +- 'SubqueryAlias t | ||
| +- 'SubqueryAlias _auto_generated_subquery_name | ||
|
||
| +- 'Project ['id AS col1#x, 'id AS col2#x, 'id AS col3#x, 'id AS col4#x] | ||
| +- 'UnresolvedTableValuedFunction range, [10] | ||
|
|
||
| == Analyzed Logical Plan == | ||
| col: string | ||
| Project [concat(concat(concat(cast(col1#xL as string), cast(col2#xL as string)), cast(col3#xL as string)), cast(col4#xL as string)) AS col#x] | ||
| +- SubqueryAlias t | ||
| +- SubqueryAlias _auto_generated_subquery_name | ||
| +- Project [id#xL AS col1#xL, id#xL AS col2#xL, id#xL AS col3#xL, id#xL AS col4#xL] | ||
| +- Range (0, 10, step=1, splits=None) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2638,4 +2638,17 @@ class SQLQuerySuite extends QueryTest with SharedSQLContext { | |
| } | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-21335: support un-aliased subquery") { | ||
| withTempView("v") { | ||
| Seq(1 -> "a").toDF("i", "j").createOrReplaceTempView("v") | ||
| checkAnswer(sql("SELECT i from (SELECT i FROM v)"), Row(1)) | ||
|
|
||
| val e = intercept[AnalysisException](sql("SELECT v.i from (SELECT i FROM v)")) | ||
| assert(e.message == | ||
| "cannot resolve '`v.i`' given input columns: [_auto_generated_subquery_name.i]") | ||
|
||
|
|
||
| checkAnswer(sql("SELECT _auto_generated_subquery_name.i from (SELECT i FROM v)"), Row(1)) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: typo
ues.