Index: metastore/src/java/org/apache/hadoop/hive/metastore/HiveAlterHandler.java =================================================================== --- metastore/src/java/org/apache/hadoop/hive/metastore/HiveAlterHandler.java (revision 1408425) +++ metastore/src/java/org/apache/hadoop/hive/metastore/HiveAlterHandler.java (working copy) @@ -30,7 +30,6 @@ import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.metastore.api.AlreadyExistsException; -import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; import org.apache.hadoop.hive.metastore.api.FieldSchema; import org.apache.hadoop.hive.metastore.api.InvalidObjectException; import org.apache.hadoop.hive.metastore.api.InvalidOperationException; @@ -38,6 +37,7 @@ import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; import org.apache.hadoop.hive.metastore.api.Partition; import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; /** * Hive specific implementation of alter @@ -127,7 +127,11 @@ // that means user is asking metastore to move data to new location // corresponding to the new name // get new location - newTblLoc = wh.getTablePath(msdb.getDatabase(newt.getDbName()), newt.getTableName()).toString(); + newTblLoc = wh.getTablePath(msdb.getDatabase(newt.getDbName()), + newt.getTableName()).toString(); + Path newTblPath = constructRenamedPath(new Path(newTblLoc), + new Path(newt.getSd().getLocation())); + newTblLoc = newTblPath.toString(); newt.getSd().setLocation(newTblLoc); oldTblLoc = oldt.getSd().getLocation(); moveData = true; @@ -296,7 +300,7 @@ try { destPath = new Path(wh.getTablePath(msdb.getDatabase(dbname), name), Warehouse.makePartName(tbl.getPartitionKeys(), new_part.getValues())); - destPath = constructRenamedPartitionPath(destPath, new_part); + destPath = constructRenamedPath(destPath, new Path(new_part.getSd().getLocation())); } catch (NoSuchObjectException e) { LOG.debug(e); throw new InvalidOperationException( @@ -431,13 +435,13 @@ } /** - * Uses the scheme and authority of the partition's current location, and the path constructed - * using the partition's new name to construct a path for the partition's new location. + * Uses the scheme and authority of the object's current location and the path constructed + * using the object's new name to construct a path for the object's new location. */ - private Path constructRenamedPartitionPath(Path defaultPath, Partition part) { - Path oldPath = new Path(part.getSd().getLocation()); - URI oldUri = oldPath.toUri(); + private Path constructRenamedPath(Path defaultNewPath, Path currentPath) { + URI currentUri = currentPath.toUri(); - return new Path(oldUri.getScheme(), oldUri.getAuthority(), defaultPath.toUri().getPath()); + return new Path(currentUri.getScheme(), currentUri.getAuthority(), + defaultNewPath.toUri().getPath()); } } Index: ql/src/test/results/clientpositive/rename_table_location.q.out =================================================================== --- ql/src/test/results/clientpositive/rename_table_location.q.out (revision 0) +++ ql/src/test/results/clientpositive/rename_table_location.q.out (revision 0) @@ -0,0 +1,50 @@ +PREHOOK: query: -- This test verifies that if the tables location changes, renaming a table will not change +-- the table location scheme + +CREATE TABLE rename_partition_table (key STRING, value STRING) PARTITIONED BY (part STRING) +STORED AS RCFILE +#### A masked pattern was here #### +PREHOOK: type: CREATETABLE +POSTHOOK: query: -- This test verifies that if the tables location changes, renaming a table will not change +-- the table location scheme + +CREATE TABLE rename_partition_table (key STRING, value STRING) PARTITIONED BY (part STRING) +STORED AS RCFILE +#### A masked pattern was here #### +POSTHOOK: type: CREATETABLE +POSTHOOK: Output: default@rename_partition_table +PREHOOK: query: INSERT OVERWRITE TABLE rename_partition_table PARTITION (part = '1') SELECT * FROM src +PREHOOK: type: QUERY +PREHOOK: Input: default@src +PREHOOK: Output: default@rename_partition_table@part=1 +POSTHOOK: query: INSERT OVERWRITE TABLE rename_partition_table PARTITION (part = '1') SELECT * FROM src +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +POSTHOOK: Output: default@rename_partition_table@part=1 +POSTHOOK: Lineage: rename_partition_table PARTITION(part=1).key SIMPLE [(src)src.FieldSchema(name:key, type:string, comment:default), ] +POSTHOOK: Lineage: rename_partition_table PARTITION(part=1).value SIMPLE [(src)src.FieldSchema(name:value, type:string, comment:default), ] +#### A masked pattern was here #### +PREHOOK: type: ALTERTABLE_LOCATION +PREHOOK: Input: default@rename_partition_table +PREHOOK: Output: default@rename_partition_table +#### A masked pattern was here #### +POSTHOOK: type: ALTERTABLE_LOCATION +POSTHOOK: Input: default@rename_partition_table +POSTHOOK: Output: default@rename_partition_table +POSTHOOK: Lineage: rename_partition_table PARTITION(part=1).key SIMPLE [(src)src.FieldSchema(name:key, type:string, comment:default), ] +POSTHOOK: Lineage: rename_partition_table PARTITION(part=1).value SIMPLE [(src)src.FieldSchema(name:value, type:string, comment:default), ] +PREHOOK: query: -- If the metastore attempts to change the scheme of the table back to the default pfile, it will get +-- an exception related to the source and destination file systems not matching +ALTER TABLE rename_partition_table RENAME TO rename_partition_table_renamed +PREHOOK: type: ALTERTABLE_RENAME +PREHOOK: Input: default@rename_partition_table +PREHOOK: Output: default@rename_partition_table +POSTHOOK: query: -- If the metastore attempts to change the scheme of the table back to the default pfile, it will get +-- an exception related to the source and destination file systems not matching +ALTER TABLE rename_partition_table RENAME TO rename_partition_table_renamed +POSTHOOK: type: ALTERTABLE_RENAME +POSTHOOK: Input: default@rename_partition_table +POSTHOOK: Output: default@rename_partition_table +POSTHOOK: Output: default@rename_partition_table_renamed +POSTHOOK: Lineage: rename_partition_table PARTITION(part=1).key SIMPLE [(src)src.FieldSchema(name:key, type:string, comment:default), ] +POSTHOOK: Lineage: rename_partition_table PARTITION(part=1).value SIMPLE [(src)src.FieldSchema(name:value, type:string, comment:default), ] Index: ql/src/test/queries/clientpositive/rename_table_location.q =================================================================== --- ql/src/test/queries/clientpositive/rename_table_location.q (revision 0) +++ ql/src/test/queries/clientpositive/rename_table_location.q (revision 0) @@ -0,0 +1,14 @@ +-- This test verifies that if the tables location changes, renaming a table will not change +-- the table location scheme + +CREATE TABLE rename_partition_table (key STRING, value STRING) PARTITIONED BY (part STRING) +STORED AS RCFILE +LOCATION 'pfile:${system:test.tmp.dir}/rename_partition_table'; + +INSERT OVERWRITE TABLE rename_partition_table PARTITION (part = '1') SELECT * FROM src; + +ALTER TABLE rename_partition_table SET LOCATION 'file:${system:test.tmp.dir}/rename_partition_table'; + +-- If the metastore attempts to change the scheme of the table back to the default pfile, it will get +-- an exception related to the source and destination file systems not matching +ALTER TABLE rename_partition_table RENAME TO rename_partition_table_renamed;