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

    https://github.com/apache/spark/pull/16626#discussion_r97191587
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala ---
    @@ -168,6 +168,43 @@ case class AlterTableRenameCommand(
     }
     
     /**
    + * A command that add columns to a table
    + * The syntax of using this command in SQL is:
    + * {{{
    + *   ALTER TABLE table_identifier
    + *   ADD COLUMNS (col_name data_type [COMMENT col_comment], ...);
    + * }}}
    +*/
    +case class AlterTableAddColumnsCommand(
    +    table: TableIdentifier,
    +    columns: Seq[StructField]) extends RunnableCommand {
    +  override def run(sparkSession: SparkSession): Seq[Row] = {
    +    val catalog = sparkSession.sessionState.catalog
    +    val catalogTable = DDLUtils.verifyAlterTableAddColumn(catalog, table)
    +
    +    // If an exception is thrown here we can just assume the table is 
uncached;
    +    // this can happen with Hive tables when the underlying catalog is 
in-memory.
    +    val wasCached = 
Try(sparkSession.catalog.isCached(table.unquotedString)).getOrElse(false)
    +    if (wasCached) {
    +      try {
    +        sparkSession.catalog.uncacheTable(table.unquotedString)
    +      } catch {
    +        case NonFatal(e) => log.warn(e.toString, e)
    +      }
    +    }
    +    // Invalidate the table last, otherwise uncaching the table would load 
the logical plan
    +    // back into the hive metastore cache
    +    catalog.refreshTable(table)
    +
    +    val newSchema = catalogTable.schema.copy(fields = 
catalogTable.schema.fields ++ columns)
    --- End diff --
    
    We support partitioned tables. The test cases add include this case. 
    However, we don't support ALTER ADD COLUMNS to a particular partition, as 
what Hive can do today. EX: `ALTER TABLE T1 PARTITION(c3=1) ADD COLUMNS .... `. 
This is another potential feature to add if we maintain schema for a partition. 


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to