-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-17180] [SQL] Fix View Resolution Order in ALTER VIEW AS SELECT #14746
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 4 commits
db45b4c
c5add2c
51dbf72
d84db62
47fdf1f
5606344
5c60b4c
baa8a1f
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 | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,7 +19,7 @@ package org.apache.spark.sql.execution.command | ||||||||||||||||
|
|
|||||||||||||||||
| import scala.util.control.NonFatal | |||||||||||||||||
|
|
|||||||||||||||||
| import org.apache.spark.sql.{AnalysisException, Row, SparkSession} | |||||||||||||||||
| import org.apache.spark.sql.{AnalysisException, Row, SaveMode, 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} | |||||||||||||||||
|
|
@@ -41,14 +41,16 @@ import org.apache.spark.sql.types.StructType | ||||||||||||||||
| * Dataset API. | |||||||||||||||||
| * @param child 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. | |||||||||||||||||
| * @param allowExisting if true, and if the view already exists, noop; if false, and if the view | |||||||||||||||||
| * already exists, throws analysis exception. | |||||||||||||||||
| * @param replace if true, and if the view already exists, updates it; if false, and if the view | |||||||||||||||||
| * already exists, throws analysis exception. | |||||||||||||||||
| * @param mode only three modes are applicable here: Ignore, Overwrite and ErrorIfExists. If the | |||||||||||||||||
| * view already exists, CreateViewCommand behaves based on the mode: | |||||||||||||||||
| * 1) Overwrite: update the view; | |||||||||||||||||
| * 2) Ignore: noop; | |||||||||||||||||
| * 3) ErrorIfExists throws analysis exception. | |||||||||||||||||
| * @param isTemporary if true, the view is created as a temporary view. Temporary views are dropped | |||||||||||||||||
| * at the end of current Spark session. Existing permanent relations with the same | |||||||||||||||||
| * name are not visible to the current session while the temporary view exists, | |||||||||||||||||
| * unless they are specified with full qualified table name with database prefix. | |||||||||||||||||
| * @param isAlterViewAsSelect if true, this original DDL command is ALTER VIEW AS SELECT | |||||||||||||||||
| */ | |||||||||||||||||
| case class CreateViewCommand( | |||||||||||||||||
| name: TableIdentifier, | |||||||||||||||||
|
|
@@ -57,9 +59,9 @@ case class CreateViewCommand( | ||||||||||||||||
| properties: Map[String, String], | |||||||||||||||||
| originalText: Option[String], | |||||||||||||||||
| child: LogicalPlan, | |||||||||||||||||
| allowExisting: Boolean, | |||||||||||||||||
| replace: Boolean, | |||||||||||||||||
| isTemporary: Boolean) | |||||||||||||||||
| mode: SaveMode, | |||||||||||||||||
| isTemporary: Boolean, | |||||||||||||||||
| isAlterViewAsSelect: Boolean = false) | |||||||||||||||||
| extends RunnableCommand { | |||||||||||||||||
|
|
|||||||||||||||||
| override protected def innerChildren: Seq[QueryPlan[_]] = Seq(child) | |||||||||||||||||
|
|
@@ -69,17 +71,16 @@ case class CreateViewCommand( | ||||||||||||||||
|
|
|||||||||||||||||
| override def output: Seq[Attribute] = Seq.empty[Attribute] | |||||||||||||||||
|
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. why remove this check?
Member
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.
This checking looks useless. If you think we should keep it, I can add it back. Thanks! |
|||||||||||||||||
|
|
|||||||||||||||||
| assert(mode != SaveMode.Append, | |||||||||||||||||
| "CREATE or ALTER VIEW can only use ErrorIfExists, Ignore or Overwrite.") | |||||||||||||||||
|
|
|||||||||||||||||
| if (!isTemporary) { | |||||||||||||||||
| require(originalText.isDefined, | |||||||||||||||||
| "The table to created with CREATE VIEW must have 'originalText'.") | |||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| if (allowExisting && replace) { | |||||||||||||||||
| throw new AnalysisException("CREATE VIEW with both IF NOT EXISTS and REPLACE is not allowed.") | |||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| // Disallows 'CREATE TEMPORARY VIEW IF NOT EXISTS' to be consistent with 'CREATE TEMPORARY TABLE' | |||||||||||||||||
| if (allowExisting && isTemporary) { | |||||||||||||||||
| if (mode == SaveMode.Ignore && isTemporary) { | |||||||||||||||||
| throw new AnalysisException( | |||||||||||||||||
| "It is not allowed to define a TEMPORARY view with IF NOT EXISTS.") | |||||||||||||||||
| } | |||||||||||||||||
|
|
@@ -105,7 +106,13 @@ case class CreateViewCommand( | ||||||||||||||||
| } | |||||||||||||||||
| val sessionState = sparkSession.sessionState | |||||||||||||||||
|
|
|||||||||||||||||
| if (isTemporary) { | |||||||||||||||||
| // 1) CREATE VIEW: create a temp view when users explicitly specify the keyword TEMPORARY; | |||||||||||||||||
| // otherwise, create a permanent view no matter whether the temporary view | |||||||||||||||||
| // with the same name exists or not. | |||||||||||||||||
| // 2) ALTER VIEW: alter the temporary view if the temp view exists; otherwise, try to alter | |||||||||||||||||
|
|||||||||||||||||
| allowExisting | replace | SaveMode |
|---|---|---|
| true | false | Ignore |
| false | false | ErrorIfExists |
| false | true | Overwrite |
| true | true | AnalysisException |
Let me try it. Thanks!
Uh oh!
There was an error while loading. Please reload this page.
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 a
viewTypeparameter which is an enum?We are going to add
global temp viewsoon, so this enum will also be useful at that time.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.
Sure, will do it. Thanks!