Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
alter table <tablename> exchange partition (<(partial)?partition spec>) with table <tablename> partition (<(partial)?partition spec>)

The semantics of the above statement is that the data between the above partitions are switchedis moved from the target table to the source table. Both the tables should have the same schema. The operation fails in the presence of an index. The source table should not have that partition. Consider the following examples:

Code Block

create table T1(a string, b string) partitioned by (ds string);
create table T2(a string, b string);

The operation

Code Block
 alter table T1 exchange partition (ds='1') with table T2 

moves the data from T2 to T1@ds=1. The operation fails if T1@ds=1 already exists or T1 and T2 have different schemas. Note that the schema for T2 is being used for the newly created partition T1@ds=1.

Code Block

create table T1(a string, b string) partitioned by (ds string, hr string);
create table T2(a string, b string) partitioned by (hr string);

The operation

Code Block
 alter table T1 exchange partition (ds='1') with table T2 

moves the data from T2 to T1@ds=1. The operation fails if T1@ds=1 already exists or T1 and T2 have different schemas. Note that the schema for T2 is being used for the newly created partition T1@ds=1. Either all the partitions of T1 will get created or the whole operation will fail. All partitions of T2 are dropped., hr string);
create table T2(a string, b string) partitioned by (ds string, hr string);