You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by yhuai <gi...@git.apache.org> on 2016/05/04 23:25:06 UTC

[GitHub] spark pull request: [SPARK-6339][SQL] Supports CREATE TEMPORARY VI...

Github user yhuai commented on a diff in the pull request:

    https://github.com/apache/spark/pull/12872#discussion_r62131182
  
    --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/command/views.scala ---
    @@ -71,29 +87,59 @@ case class CreateViewCommand(
         require(tableDesc.schema == Nil || tableDesc.schema.length == analyzedPlan.output.length)
         val sessionState = sparkSession.sessionState
     
    -    if (sessionState.catalog.tableExists(tableIdentifier)) {
    -      if (allowExisting) {
    -        // Handles `CREATE VIEW IF NOT EXISTS v0 AS SELECT ...`. Does nothing when the target view
    -        // already exists.
    -      } else if (replace) {
    -        // Handles `CREATE OR REPLACE VIEW v0 AS SELECT ...`
    -        sessionState.catalog.alterTable(prepareTable(sparkSession, analyzedPlan))
    +    if (isTemporary) {
    +      createTemporaryView(tableDesc.identifier, sparkSession, analyzedPlan)
    +    } else {
    +      // Adds default database for permanent table if it doesn't exist, so that tableExists()
    +      // only check permanent tables.
    +      val database = tableDesc.identifier.database.getOrElse(
    +        sessionState.catalog.getCurrentDatabase)
    +      val tableIdentifier = tableDesc.identifier.copy(database = Option(database))
    +
    +      if (sessionState.catalog.tableExists(tableIdentifier)) {
    +        if (allowExisting) {
    +          // Handles `CREATE VIEW IF NOT EXISTS v0 AS SELECT ...`. Does nothing when the target view
    +          // already exists.
    +        } else if (replace) {
    +          // Handles `CREATE OR REPLACE VIEW v0 AS SELECT ...`
    +          sessionState.catalog.alterTable(prepareTable(sparkSession, analyzedPlan))
    +        } else {
    +          // Handles `CREATE VIEW v0 AS SELECT ...`. Throws exception when the target view already
    +          // exists.
    +          throw new AnalysisException(
    +            s"View $tableIdentifier already exists. If you want to update the view definition, " +
    +              "please use ALTER VIEW AS or CREATE OR REPLACE VIEW AS")
    +        }
           } else {
    -        // Handles `CREATE VIEW v0 AS SELECT ...`. Throws exception when the target view already
    -        // exists.
    -        throw new AnalysisException(s"View $tableIdentifier already exists. " +
    -          "If you want to update the view definition, please use ALTER VIEW AS or " +
    -          "CREATE OR REPLACE VIEW AS")
    +        // Create the view if it doesn't exist.
    +        sessionState.catalog.createTable(
    +          prepareTable(sparkSession, analyzedPlan), ignoreIfExists = false)
           }
    -    } else {
    -      // Create the view if it doesn't exist.
    -      sessionState.catalog.createTable(
    -        prepareTable(sparkSession, analyzedPlan), ignoreIfExists = false)
         }
    -
         Seq.empty[Row]
       }
     
    +  private def createTemporaryView(
    +      table: TableIdentifier, sparkSession: SparkSession, analyzedPlan: LogicalPlan): Unit = {
    +
    +    val sessionState = sparkSession.sessionState
    +    val catalog = sessionState.catalog
    +
    +    // Projects column names to alias names
    +    val logicalPlan = {
    +      if (tableDesc.schema.isEmpty) {
    +        analyzedPlan
    +      } else {
    +        val projectList = analyzedPlan.output.zip(tableDesc.schema).map {
    --- End diff --
    
    Seems you want to check if `analyzedPlan.output` and `tableDesc.schema` have the same number of columns?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org