-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-18801][SQL][FOLLOWUP] Alias the view with its child #16561
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 all commits
0e82340
179a265
7e19803
d6537a5
16ec310
21e63f8
c86ab48
06e8855
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 |
|---|---|---|
|
|
@@ -28,22 +28,60 @@ import org.apache.spark.sql.catalyst.rules.Rule | |
| */ | ||
|
|
||
| /** | ||
| * Make sure that a view's child plan produces the view's output attributes. We wrap the child | ||
| * with a Project and add an alias for each output attribute. The attributes are resolved by | ||
| * name. This should be only done after the batch of Resolution, because the view attributes are | ||
| * not completely resolved during the batch of Resolution. | ||
| * Make sure that a view's child plan produces the view's output attributes. We try to wrap the | ||
| * child by: | ||
| * 1. Generate the `queryOutput` by: | ||
| * 1.1. If the query column names are defined, map the column names to attributes in the child | ||
| * output by name(This is mostly for handling view queries like SELECT * FROM ..., the | ||
| * schema of the referenced table/view may change after the view has been created, so we | ||
| * have to save the output of the query to `viewQueryColumnNames`, and restore them during | ||
| * view resolution, in this way, we are able to get the correct view column ordering and | ||
| * omit the extra columns that we don't require); | ||
| * 1.2. Else set the child output attributes to `queryOutput`. | ||
| * 2. Map the `queryQutput` to view output by index, if the corresponding attributes don't match, | ||
| * try to up cast and alias the attribute in `queryOutput` to the attribute in the view output. | ||
| * 3. Add a Project over the child, with the new output generated by the previous steps. | ||
| * If the view output doesn't have the same number of columns neither with the child output, nor | ||
| * with the query column names, throw an AnalysisException. | ||
| * | ||
| * This should be only done after the batch of Resolution, because the view attributes are not | ||
| * completely resolved during the batch of Resolution. | ||
| */ | ||
| case class AliasViewChild(conf: CatalystConf) extends Rule[LogicalPlan] { | ||
| override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { | ||
| case v @ View(_, output, child) if child.resolved => | ||
| case v @ View(desc, output, child) if child.resolved && output != child.output => | ||
| val resolver = conf.resolver | ||
| val newOutput = output.map { attr => | ||
| val originAttr = findAttributeByName(attr.name, child.output, resolver) | ||
| // The dataType of the output attributes may be not the same with that of the view output, | ||
| // so we should cast the attribute to the dataType of the view output attribute. If the | ||
| // cast can't perform, will throw an AnalysisException. | ||
| Alias(Cast(originAttr, attr.dataType), attr.name)(exprId = attr.exprId, | ||
| qualifier = attr.qualifier, explicitMetadata = Some(attr.metadata)) | ||
| val queryColumnNames = desc.viewQueryColumnNames | ||
| val queryOutput = if (queryColumnNames.nonEmpty) { | ||
| // If the view output doesn't have the same number of columns with the query column names, | ||
| // throw an AnalysisException. | ||
| if (output.length != queryColumnNames.length) { | ||
| throw new AnalysisException( | ||
| s"The view output ${output.mkString("[", ",", "]")} doesn't have the same number of " + | ||
| s"columns with the query column names ${queryColumnNames.mkString("[", ",", "]")}") | ||
| } | ||
| desc.viewQueryColumnNames.map { colName => | ||
| findAttributeByName(colName, child.output, resolver) | ||
| } | ||
| } else { | ||
| // For view created before Spark 2.2.0, the view text is already fully qualified, the plan | ||
| // output is the same with the view output. | ||
| child.output | ||
| } | ||
| // Map the attributes in the query output to the attributes in the view output by index. | ||
| val newOutput = output.zip(queryOutput).map { | ||
|
Contributor
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. Seems we need to check the size of
Contributor
Author
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. For views created by older versions of Spark, the view text is fully qualified, so the output is the same with the view output. Or else we have checked that the output have the same length with |
||
| case (attr, originAttr) if attr != originAttr => | ||
| // The dataType of the output attributes may be not the same with that of the view | ||
| // output, so we should cast the attribute to the dataType of the view output attribute. | ||
| // Will throw an AnalysisException if the cast can't perform or might truncate. | ||
| if (Cast.mayTruncate(originAttr.dataType, attr.dataType)) { | ||
| throw new AnalysisException(s"Cannot up cast ${originAttr.sql} from " + | ||
| s"${originAttr.dataType.simpleString} to ${attr.simpleString} as it may truncate\n") | ||
| } else { | ||
| Alias(Cast(originAttr, attr.dataType), attr.name)(exprId = attr.exprId, | ||
| qualifier = attr.qualifier, explicitMetadata = Some(attr.metadata)) | ||
| } | ||
| case (_, originAttr) => originAttr | ||
| } | ||
| v.copy(child = Project(newOutput, child)) | ||
| } | ||
|
|
@@ -74,7 +112,9 @@ object EliminateView extends Rule[LogicalPlan] { | |
| // The child should have the same output attributes with the View operator, so we simply | ||
| // remove the View operator. | ||
| case View(_, output, child) => | ||
| assert(output == child.output, "The output of the child is different from the view output") | ||
| assert(output == child.output, | ||
| s"The output of the child ${child.output.mkString("[", ",", "]")} is different from the " + | ||
| s"view output ${output.mkString("[", ",", "]")}") | ||
| child | ||
| } | ||
| } | ||
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.
how about