Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/12121#discussion_r58297825
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala ---
@@ -312,11 +378,38 @@ case class AlterTableSetFileFormat(
genericFormat: Option[String])(sql: String)
extends NativeDDLCommand(sql) with Logging
+/**
+ * A command that sets the location of a table or a partition.
+ *
+ * The syntax of this command is:
+ * {{{
+ * ALTER TABLE table_name [PARTITION partition_spec] SET LOCATION "loc";
+ * }}}
+ */
case class AlterTableSetLocation(
tableName: TableIdentifier,
partitionSpec: Option[TablePartitionSpec],
- location: String)(sql: String)
- extends NativeDDLCommand(sql) with Logging
+ location: String)
+ extends RunnableCommand {
+
+ override def run(sqlContext: SQLContext): Seq[Row] = {
+ val catalog = sqlContext.sessionState.catalog
+ if (partitionSpec.isEmpty) {
+ // No partition spec is specified, so we set the location for the
table itself
+ val table = catalog.getTable(tableName)
+ val newTable = table.withNewStorage(locationUri = Some(location))
+ catalog.alterTable(newTable)
+ } else {
+ // Partition spec is specified, so we set the location only for this
partition
+ val spec = partitionSpec.get
+ val part = catalog.getPartition(tableName, spec)
+ val newPart = part.copy(storage = part.storage.copy(locationUri =
Some(location)))
+ catalog.alterPartitions(tableName, Seq(newPart))
+ }
+ Seq.empty[Row]
+ }
+
+}
--- End diff --
I think for data source tables, we cannot do `ALTER TABLE table_name
PARTITION partition_spec SET LOCATION` because we rely on our partitioning
discovery mechanism. Also, for data source tables, the real path is stored in
table properties (there is a path field). We need to change that as well as the
locationUri.
Also, I think it's good to document clearly on the semantic of this
command. For a user, he/she needs to know if this command will move the data to
the new location. For Hive, it does not move data. For now, I think it is fine
to match that behavior, but we need to document it.
---
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]