spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yhuai <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-12728][SQL] Integrates SQL generation w...
Date Wed, 27 Jan 2016 02:06:49 GMT
Github user yhuai commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10733#discussion_r50932896
  
    --- Diff: sql/hive/src/main/scala/org/apache/spark/sql/hive/execution/CreateViewAsSelect.scala
---
    @@ -44,55 +48,96 @@ private[hive] case class CreateViewAsSelect(
       override def run(sqlContext: SQLContext): Seq[Row] = {
         val hiveContext = sqlContext.asInstanceOf[HiveContext]
     
    -    if (hiveContext.catalog.tableExists(tableIdentifier)) {
    -      if (allowExisting) {
    -        // view already exists, will do nothing, to keep consistent with Hive
    -      } else if (orReplace) {
    -        hiveContext.catalog.client.alertView(prepareTable())
    -      } else {
    +    hiveContext.catalog.tableExists(tableIdentifier) match {
    +      case true if allowExisting =>
    +        // Handles `CREATE VIEW IF NOT EXISTS v0 AS SELECT ...`. Does nothing when the
target view
    +        // already exists.
    +
    +      case true if orReplace =>
    +        // Handles `CREATE OR REPLACE VIEW v0 AS SELECT ...`
    +        hiveContext.catalog.client.alertView(prepareTable(sqlContext))
    +
    +      case true =>
    +        // 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 {
    -      hiveContext.catalog.client.createView(prepareTable())
    +
    +      case false =>
    +        hiveContext.catalog.client.createView(prepareTable(sqlContext))
         }
     
         Seq.empty[Row]
       }
     
    -  private def prepareTable(): HiveTable = {
    -    // setup column types according to the schema of child.
    -    val schema = if (tableDesc.schema == Nil) {
    -      childSchema.map { attr =>
    -        HiveColumn(attr.name, HiveMetastoreTypes.toMetastoreType(attr.dataType), null)
    -      }
    +  private def prepareTable(sqlContext: SQLContext): HiveTable = {
    +    val expandedText = if (sqlContext.conf.canonicalView) {
    +      rebuildViewQueryString(sqlContext).getOrElse(wrapViewTextWithSelect)
         } else {
    -      childSchema.zip(tableDesc.schema).map { case (attr, col) =>
    -        HiveColumn(col.name, HiveMetastoreTypes.toMetastoreType(attr.dataType), col.comment)
    +      wrapViewTextWithSelect
    +    }
    +
    +    val viewSchema = {
    +      if (tableDesc.schema.isEmpty) {
    +        childSchema.map { attr =>
    +          HiveColumn(attr.name, HiveMetastoreTypes.toMetastoreType(attr.dataType), null)
    +        }
    +      } else {
    +        childSchema.zip(tableDesc.schema).map { case (attr, col) =>
    +          HiveColumn(col.name, HiveMetastoreTypes.toMetastoreType(attr.dataType), col.comment)
    +        }
           }
         }
     
    -    val columnNames = childSchema.map(f => verbose(f.name))
    +    tableDesc.copy(schema = viewSchema, viewText = Some(expandedText))
    +  }
     
    +  private def wrapViewTextWithSelect: String = {
         // When user specified column names for view, we should create a project to do the
renaming.
         // When no column name specified, we still need to create a project to declare the
columns
         // we need, to make us more robust to top level `*`s.
    -    val projectList = if (tableDesc.schema == Nil) {
    -      columnNames.mkString(", ")
    -    } else {
    -      columnNames.zip(tableDesc.schema.map(f => verbose(f.name))).map {
    -        case (name, alias) => s"$name AS $alias"
    -      }.mkString(", ")
    +    val viewOutput = {
    +      val columnNames = childSchema.map(f => quote(f.name))
    +      if (tableDesc.schema.isEmpty) {
    +        columnNames.mkString(", ")
    +      } else {
    +        columnNames.zip(tableDesc.schema.map(f => quote(f.name))).map {
    --- End diff --
    
    and this


---
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


Mime
View raw message