diff --git standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java index 47de215..b79e809 100644 --- standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java +++ standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java @@ -3365,6 +3365,20 @@ public Partition exchange_partition(Map partitionSpecs, List> transactionalListenerResponsesForDropPartition = Lists.newArrayListWithCapacity(partitionsToExchange.size()); + // Check if any of the partitions already exists in destTable. + List destPartitionNames = + ms.listPartitionNames(destDbName, destTableName, (short) -1); + if (destPartitionNames != null && !destPartitionNames.isEmpty()) { + for (Partition partition : partitionsToExchange) { + String partToExchangeName = + Warehouse.makePartName(destinationTable.getPartitionKeys(), partition.getValues()); + if (destPartitionNames.contains(partToExchangeName)) { + throw new MetaException("The partition " + partToExchangeName + + " already exists in the table " + destTableName); + } + } + } + try { for (Partition partition: partitionsToExchange) { Partition destPartition = new Partition(partition); diff --git standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/client/TestExchangePartitions.java standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/client/TestExchangePartitions.java index 3a06aec..a5908d8 100644 --- standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/client/TestExchangePartitions.java +++ standalone-metastore/src/test/java/org/apache/hadoop/hive/metastore/client/TestExchangePartitions.java @@ -504,9 +504,6 @@ public void testExchangePartitionsPartAlreadyExists() throws Exception { Map partitionSpecs = getPartitionSpec(partitions[1]); client.exchange_partitions(partitionSpecs, DB_NAME, sourceTable.getTableName(), DB_NAME, destTable.getTableName()); - // TODO: In this case we get an exception, because the insert statement fails due to duplicated - // primary key. Maybe this use case could be handled better by checking if the partition already - // exists. } @Test @@ -1097,9 +1094,6 @@ public void testExchangePartitionPartAlreadyExists() throws Exception { Map partitionSpecs = getPartitionSpec(partitions[1]); client.exchange_partition(partitionSpecs, DB_NAME, sourceTable.getTableName(), DB_NAME, destTable.getTableName()); - // TODO: In this case we get an exception, because the insert statement fails due to duplicated - // primary key. Maybe this use case could be handled better by checking if the partition already - // exists. } @Test