Skip to content

Commit a3dcb19

Browse files
authored
Merge pull request ClickHouse#62524 from helifu/master
Fix a bug moving one partition from one to itself
2 parents bd64bc0 + e28320a commit a3dcb19

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

src/Storages/MergeTree/MergeTreeData.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5065,6 +5065,25 @@ void MergeTreeData::movePartitionToVolume(const ASTPtr & partition, const String
50655065
}
50665066
}
50675067

5068+
void MergeTreeData::movePartitionToTable(const PartitionCommand & command, ContextPtr query_context)
5069+
{
5070+
String dest_database = query_context->resolveDatabase(command.to_database);
5071+
auto dest_storage = DatabaseCatalog::instance().getTable({dest_database, command.to_table}, query_context);
5072+
5073+
/// The target table and the source table are the same.
5074+
if (dest_storage->getStorageID() == this->getStorageID())
5075+
return;
5076+
5077+
auto * dest_storage_merge_tree = dynamic_cast<MergeTreeData *>(dest_storage.get());
5078+
if (!dest_storage_merge_tree)
5079+
throw Exception(ErrorCodes::NOT_IMPLEMENTED,
5080+
"Cannot move partition from table {} to table {} with storage {}",
5081+
getStorageID().getNameForLogs(), dest_storage->getStorageID().getNameForLogs(), dest_storage->getName());
5082+
5083+
dest_storage_merge_tree->waitForOutdatedPartsToBeLoaded();
5084+
movePartitionToTable(dest_storage, command.partition, query_context);
5085+
}
5086+
50685087
void MergeTreeData::movePartitionToShard(const ASTPtr & /*partition*/, bool /*move_part*/, const String & /*to*/, ContextPtr /*query_context*/)
50695088
{
50705089
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "MOVE PARTITION TO SHARD is not supported by storage {}", getName());
@@ -5141,20 +5160,8 @@ Pipe MergeTreeData::alterPartition(
51415160
break;
51425161

51435162
case PartitionCommand::MoveDestinationType::TABLE:
5144-
{
5145-
String dest_database = query_context->resolveDatabase(command.to_database);
5146-
auto dest_storage = DatabaseCatalog::instance().getTable({dest_database, command.to_table}, query_context);
5147-
5148-
auto * dest_storage_merge_tree = dynamic_cast<MergeTreeData *>(dest_storage.get());
5149-
if (!dest_storage_merge_tree)
5150-
throw Exception(ErrorCodes::NOT_IMPLEMENTED,
5151-
"Cannot move partition from table {} to table {} with storage {}",
5152-
getStorageID().getNameForLogs(), dest_storage->getStorageID().getNameForLogs(), dest_storage->getName());
5153-
5154-
dest_storage_merge_tree->waitForOutdatedPartsToBeLoaded();
5155-
movePartitionToTable(dest_storage, command.partition, query_context);
5156-
}
5157-
break;
5163+
movePartitionToTable(command, query_context);
5164+
break;
51585165

51595166
case PartitionCommand::MoveDestinationType::SHARD:
51605167
{

src/Storages/MergeTree/MergeTreeData.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,9 @@ class MergeTreeData : public IStorage, public WithMutableContext
790790
/// Moves partition to specified Volume
791791
void movePartitionToVolume(const ASTPtr & partition, const String & name, bool moving_part, ContextPtr context);
792792

793+
/// Moves partition to specified Table
794+
void movePartitionToTable(const PartitionCommand & command, ContextPtr query_context);
795+
793796
/// Checks that Partition could be dropped right now
794797
/// Otherwise - throws an exception with detailed information.
795798
/// We do not use mutex because it is not very important that the size could change during the operation.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tuple() 1000000
2+
tuple() 1000000
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DROP TABLE IF EXISTS move_partition_to_oneself;
2+
CREATE TABLE move_partition_to_oneself (key UInt64 CODEC(NONE)) ENGINE = MergeTree ORDER BY tuple();
3+
INSERT INTO move_partition_to_oneself SELECT number FROM numbers(1e6);
4+
SELECT partition, rows FROM system.parts WHERE database = currentDatabase() AND table = 'move_partition_to_oneself' and active;
5+
ALTER TABLE move_partition_to_oneself MOVE PARTITION tuple() TO TABLE move_partition_to_oneself;
6+
SELECT partition, rows FROM system.parts WHERE database = currentDatabase() AND table = 'move_partition_to_oneself' and active;

0 commit comments

Comments
 (0)