Skip to content

Commit

Permalink
Merge pull request #411 from Altinity/backports/24.3.3/62524_fix_move…
Browse files Browse the repository at this point in the history
…_partition_to_itself

24.3 Backport of ClickHouse#62524 - fix move partition to itself
  • Loading branch information
Enmk authored Jul 6, 2024
2 parents 8b04b13 + 1611717 commit 3d942a4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/Storages/MergeTree/MergeTreeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5095,6 +5095,25 @@ void MergeTreeData::movePartitionToVolume(const ASTPtr & partition, const String
}
}

void MergeTreeData::movePartitionToTable(const PartitionCommand & command, ContextPtr query_context)
{
String dest_database = query_context->resolveDatabase(command.to_database);
auto dest_storage = DatabaseCatalog::instance().getTable({dest_database, command.to_table}, query_context);

/// The target table and the source table are the same.
if (dest_storage->getStorageID() == this->getStorageID())
return;

auto * dest_storage_merge_tree = dynamic_cast<MergeTreeData *>(dest_storage.get());
if (!dest_storage_merge_tree)
throw Exception(ErrorCodes::NOT_IMPLEMENTED,
"Cannot move partition from table {} to table {} with storage {}",
getStorageID().getNameForLogs(), dest_storage->getStorageID().getNameForLogs(), dest_storage->getName());

dest_storage_merge_tree->waitForOutdatedPartsToBeLoaded();
movePartitionToTable(dest_storage, command.partition, query_context);
}

void MergeTreeData::movePartitionToShard(const ASTPtr & /*partition*/, bool /*move_part*/, const String & /*to*/, ContextPtr /*query_context*/)
{
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "MOVE PARTITION TO SHARD is not supported by storage {}", getName());
Expand Down Expand Up @@ -5171,20 +5190,8 @@ Pipe MergeTreeData::alterPartition(
break;

case PartitionCommand::MoveDestinationType::TABLE:
{
String dest_database = query_context->resolveDatabase(command.to_database);
auto dest_storage = DatabaseCatalog::instance().getTable({dest_database, command.to_table}, query_context);

auto * dest_storage_merge_tree = dynamic_cast<MergeTreeData *>(dest_storage.get());
if (!dest_storage_merge_tree)
throw Exception(ErrorCodes::NOT_IMPLEMENTED,
"Cannot move partition from table {} to table {} with storage {}",
getStorageID().getNameForLogs(), dest_storage->getStorageID().getNameForLogs(), dest_storage->getName());

dest_storage_merge_tree->waitForOutdatedPartsToBeLoaded();
movePartitionToTable(dest_storage, command.partition, query_context);
}
break;
movePartitionToTable(command, query_context);
break;

case PartitionCommand::MoveDestinationType::SHARD:
{
Expand Down
3 changes: 3 additions & 0 deletions src/Storages/MergeTree/MergeTreeData.h
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,9 @@ class MergeTreeData : public IStorage, public WithMutableContext
/// Moves partition to specified Volume
void movePartitionToVolume(const ASTPtr & partition, const String & name, bool moving_part, ContextPtr context);

/// Moves partition to specified Table
void movePartitionToTable(const PartitionCommand & command, ContextPtr query_context);

/// Checks that Partition could be dropped right now
/// Otherwise - throws an exception with detailed information.
/// We do not use mutex because it is not very important that the size could change during the operation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tuple() 1000000
tuple() 1000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS move_partition_to_oneself;
CREATE TABLE move_partition_to_oneself (key UInt64 CODEC(NONE)) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO move_partition_to_oneself SELECT number FROM numbers(1e6);
SELECT partition, rows FROM system.parts WHERE database = currentDatabase() AND table = 'move_partition_to_oneself' and active;
ALTER TABLE move_partition_to_oneself MOVE PARTITION tuple() TO TABLE move_partition_to_oneself;
SELECT partition, rows FROM system.parts WHERE database = currentDatabase() AND table = 'move_partition_to_oneself' and active;

0 comments on commit 3d942a4

Please sign in to comment.