-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-20962][SQL] Support subquery column aliases in FROM clause #18185
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 3 commits
2014ac0
d971d35
5b272e4
23ca897
277d6ee
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 |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ import org.apache.spark.sql.catalyst.errors.TreeNodeException | |
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodegenFallback, ExprCode} | ||
| import org.apache.spark.sql.catalyst.parser.ParserUtils | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, LogicalPlan, UnaryNode} | ||
| import org.apache.spark.sql.catalyst.trees.TreeNode | ||
| import org.apache.spark.sql.catalyst.util.quoteIdentifier | ||
| import org.apache.spark.sql.types.{DataType, Metadata, StructType} | ||
|
|
@@ -422,6 +422,26 @@ case class UnresolvedAlias( | |
| override lazy val resolved = false | ||
| } | ||
|
|
||
| /** | ||
| * Aliased column names for subquery. We could add alias names for output columns in the subquery: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resolved by positions. |
||
| * {{{ | ||
| * // Assign alias names for output columns | ||
| * SELECT col1, col2 FROM testData AS t(col1, col2); | ||
| * }}} | ||
| * | ||
| * @param outputColumnNames the column names for this subquery. | ||
| * @param child the logical plan of this subquery. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| */ | ||
| case class UnresolvedSubqueryColumnAlias( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| outputColumnNames: Seq[String], | ||
| child: LogicalPlan) | ||
| extends UnaryNode { | ||
|
|
||
| override def output: Seq[Attribute] = Nil | ||
|
|
||
| override lazy val resolved = false | ||
| } | ||
|
|
||
| /** | ||
| * Holds the deserializer expression and the attributes that are available during the resolution | ||
| * for it. Deserializer expression is a special kind of expression that is not always resolved by | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -750,20 +750,28 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging | |
| /** | ||
| * Create an alias (SubqueryAlias) for a sub-query. This is practically the same as | ||
| * visitAliasedRelation and visitNamedExpression, ANTLR4 however requires us to use 3 different | ||
| * hooks. | ||
| * hooks. We could add alias names for output columns, for example: | ||
| * {{{ | ||
| * SELECT col1, col2 FROM testData AS t(col1, col2) | ||
| * }}} | ||
| */ | ||
| override def visitAliasedQuery(ctx: AliasedQueryContext): LogicalPlan = withOrigin(ctx) { | ||
| val alias = if (ctx.strictIdentifier == null) { | ||
| val alias = if (ctx.tableAlias.strictIdentifier == null) { | ||
| // For un-aliased subqueries, use 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 | ||
| ctx.tableAlias.strictIdentifier.getText | ||
| } | ||
| val subquery = SubqueryAlias(alias, plan(ctx.queryNoWith).optionalMap(ctx.sample)(withSample)) | ||
| if (ctx.tableAlias.identifierList != null) { | ||
| val columnNames = visitIdentifierList(ctx.tableAlias.identifierList) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| UnresolvedSubqueryColumnAlias(columnNames, subquery) | ||
| } else { | ||
| subquery | ||
| } | ||
|
|
||
| SubqueryAlias(alias, plan(ctx.queryNoWith).optionalMap(ctx.sample)(withSample)) | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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: remove the string Interpolator
s.