-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-17180][SPARK-17309][SPARK-17323][SQL] create AlterViewAsCommand to handle ALTER VIEW AS #14874
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
Closed
Closed
[SPARK-17180][SPARK-17309][SPARK-17323][SQL] create AlterViewAsCommand to handle ALTER VIEW AS #14874
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,15 +22,16 @@ import scala.util.control.NonFatal | |
| import org.apache.spark.sql.{AnalysisException, Row, SparkSession} | ||
| import org.apache.spark.sql.catalyst.{SQLBuilder, TableIdentifier} | ||
| import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTable, CatalogTableType} | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute} | ||
| import org.apache.spark.sql.catalyst.expressions.Alias | ||
| import org.apache.spark.sql.catalyst.plans.QueryPlan | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Project} | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
|
|
||
| /** | ||
| * Create Hive view on non-hive-compatible tables by specifying schema ourselves instead of | ||
| * depending on Hive meta-store. | ||
| * Create or replace a view with given query plan. This command will convert the query plan to | ||
| * canonicalized SQL string, and store it as view text in metastore, if we need to create a | ||
| * permanent view. | ||
| * | ||
| * @param name the name of this view. | ||
| * @param userSpecifiedColumns the output column names and optional comments specified by users, | ||
|
|
@@ -64,11 +65,6 @@ case class CreateViewCommand( | |
|
|
||
| override protected def innerChildren: Seq[QueryPlan[_]] = Seq(child) | ||
|
|
||
| // TODO: Note that this class can NOT canonicalize the view SQL string entirely, which is | ||
| // different from Hive and may not work for some cases like create view on self join. | ||
|
|
||
| override def output: Seq[Attribute] = Seq.empty[Attribute] | ||
|
|
||
| if (!isTemporary) { | ||
| require(originalText.isDefined, | ||
| "The table to created with CREATE VIEW must have 'originalText'.") | ||
|
|
@@ -119,9 +115,7 @@ case class CreateViewCommand( | |
| // Handles `CREATE VIEW IF NOT EXISTS v0 AS SELECT ...`. Does nothing when the target view | ||
| // already exists. | ||
| } else if (tableMetadata.tableType != CatalogTableType.VIEW) { | ||
| throw new AnalysisException( | ||
| "Existing table is not a view. The following is an existing table, " + | ||
| s"not a view: $qualifiedName") | ||
| throw new AnalysisException(s"$qualifiedName is not a view") | ||
| } else if (replace) { | ||
| // Handles `CREATE OR REPLACE VIEW v0 AS SELECT ...` | ||
| sessionState.catalog.alterTable(prepareTable(sparkSession, analyzedPlan)) | ||
|
|
@@ -179,7 +173,7 @@ case class CreateViewCommand( | |
| sparkSession.sql(viewSQL).queryExecution.assertAnalyzed() | ||
| } catch { | ||
| case NonFatal(e) => | ||
| throw new RuntimeException(s"Failed to analyze the canonicalized SQL: ${viewSQL}", e) | ||
| throw new RuntimeException(s"Failed to analyze the canonicalized SQL: $viewSQL", e) | ||
| } | ||
|
|
||
| val viewSchema = if (userSpecifiedColumns.isEmpty) { | ||
|
|
@@ -202,3 +196,62 @@ case class CreateViewCommand( | |
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Alter a view with given query plan. If the view name contains database prefix, this command will | ||
| * alter a permanent view matching the given name, or throw an exception if view not exist. Else, | ||
| * this command will try to alter a temporary view first, if view not exist, try permanent view | ||
| * next, if still not exist, throw an exception. | ||
| * | ||
| * @param name the name of this view. | ||
| * @param originalText the original SQL text of this view. Note that we can only alter a view by | ||
| * SQL API, which means we always have originalText. | ||
| * @param query the logical plan that represents the view; this is used to generate a canonicalized | ||
| * version of the SQL that can be saved in the catalog. | ||
| */ | ||
| case class AlterViewAsCommand( | ||
| name: TableIdentifier, | ||
| originalText: String, | ||
| query: LogicalPlan) extends RunnableCommand { | ||
|
|
||
| override protected def innerChildren: Seq[QueryPlan[_]] = Seq(query) | ||
|
|
||
| override def run(session: SparkSession): Seq[Row] = { | ||
| // If the plan cannot be analyzed, throw an exception and don't proceed. | ||
| val qe = session.sessionState.executePlan(query) | ||
| qe.assertAnalyzed() | ||
| val analyzedPlan = qe.analyzed | ||
|
|
||
| if (session.sessionState.catalog.isTemporaryTable(name)) { | ||
| session.sessionState.catalog.createTempView(name.table, analyzedPlan, overrideIfExists = true) | ||
| } else { | ||
| alterPermanentView(session, analyzedPlan) | ||
| } | ||
|
|
||
| Seq.empty[Row] | ||
| } | ||
|
|
||
| private def alterPermanentView(session: SparkSession, analyzedPlan: LogicalPlan): Unit = { | ||
| val viewMeta = session.sessionState.catalog.getTableMetadata(name) | ||
| if (viewMeta.tableType != CatalogTableType.VIEW) { | ||
| throw new AnalysisException(s"${viewMeta.identifier} is not a view.") | ||
| } | ||
|
|
||
| val viewSQL: String = new SQLBuilder(analyzedPlan).toSQL | ||
| // Validate the view SQL - make sure we can parse it and analyze it. | ||
| // If we cannot analyze the generated query, there is probably a bug in SQL generation. | ||
| try { | ||
| session.sql(viewSQL).queryExecution.assertAnalyzed() | ||
| } catch { | ||
| case NonFatal(e) => | ||
| throw new RuntimeException(s"Failed to analyze the canonicalized SQL: $viewSQL", e) | ||
| } | ||
|
|
||
| val updatedViewMeta = viewMeta.copy( | ||
| schema = analyzedPlan.schema, | ||
| viewOriginalText = Some(originalText), | ||
| viewText = Some(viewSQL)) | ||
|
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. Since we are doing the alter view, we should not lose the existing fields, e.g., comment and table properties. |
||
|
|
||
| session.sessionState.catalog.alterTable(updatedViewMeta) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Now, we can remove this function
createView. It is only being used byvisitCreateView. We just need to directly create aCreateViewCommandinvisitCreateView