The EXCHANGE PARTITION command will move a partition from a source table to target table and alter each table's metadata. The Exchange Partition feature is implemented as part of HIVE-4095. Exchanging multiple partitions is supported in Hive versions 1.2.2, 1.3.0, and 2.0.0+ as part of HIVE-11745.
When the command is executed, the source table's partition folder in HDFS will be renamed to move it to the destination table's partition folder. The Hive metastore will be updated to change the metadata of the source and destination tables accordingly.
The partition specification can be fully or partially specified.
See Language Manual DDL for additional information on the Exchange Partition feature.
- The destination table cannot contain the partition to be exchanged.
The operation fails in the presence of an index.
This command requires both the source and destination table names to have the same table schema.
If the schemas are different, the following exception is thrown:
The tables have different schemas. Their partitions cannot be exchanged
Example Usage – Basic
Example Usage – Partial Partition Spec (Exchanging Multiple Partitions)
Note that the schema for T1 is being used for the newly created partition T2(ds=1). Either all the partitions of T1 will get created or the whole operation will fail. All partitions of T1 are dropped.