ALTER TABLE ... DROP|DETACH PART for MergeTree

This commit is contained in:
Nicolae Vartolomei 2020-09-07 16:05:19 +01:00
parent 0d28fdd116
commit 1876374770
4 changed files with 69 additions and 8 deletions

View File

@ -1066,8 +1066,13 @@ Pipe StorageMergeTree::alterPartition(
switch (command.type)
{
case PartitionCommand::DROP_PARTITION:
checkPartitionCanBeDropped(command.partition);
dropPartition(command.partition, command.detach, query_context);
if (command.part)
{
/// TODO(nv) what would be a good check here?
}
else
checkPartitionCanBeDropped(command.partition);
dropPartition(command.partition, command.detach, command.part, query_context);
break;
case PartitionCommand::DROP_DETACHED_PARTITION:
@ -1165,7 +1170,7 @@ ActionLock StorageMergeTree::stopMergesAndWait()
}
void StorageMergeTree::dropPartition(const ASTPtr & partition, bool detach, const Context & context)
void StorageMergeTree::dropPartition(const ASTPtr & partition, bool detach, bool drop_part, const Context & context)
{
{
/// Asks to complete merges and does not allow them to start.
@ -1173,10 +1178,23 @@ void StorageMergeTree::dropPartition(const ASTPtr & partition, bool detach, cons
auto merge_blocker = stopMergesAndWait();
auto metadata_snapshot = getInMemoryMetadataPtr();
String partition_id = getPartitionIDFromQuery(partition, context);
MergeTreeData::DataPartsVector parts_to_remove;
/// TODO: should we include PreComitted parts like in Replicated case?
auto parts_to_remove = getDataPartsVectorInPartition(MergeTreeDataPartState::Committed, partition_id);
if (drop_part)
{
String part_name = partition->as<ASTLiteral &>().value.safeGet<String>();
auto part = getPartIfExists(part_name, {MergeTreeDataPartState::Committed});
if (part)
parts_to_remove.push_back(part);
} else
{
String partition_id = getPartitionIDFromQuery(partition, context);
parts_to_remove = getDataPartsVectorInPartition(MergeTreeDataPartState::Committed, partition_id);
}
// TODO should we throw an exception if parts_to_remove is empty?
removePartsFromWorkingSet(parts_to_remove, true);
@ -1191,9 +1209,9 @@ void StorageMergeTree::dropPartition(const ASTPtr & partition, bool detach, cons
}
if (detach)
LOG_INFO(log, "Detached {} parts inside partition ID {}.", parts_to_remove.size(), partition_id);
LOG_INFO(log, "Detached {} parts.", parts_to_remove.size());
else
LOG_INFO(log, "Removed {} parts inside partition ID {}.", parts_to_remove.size(), partition_id);
LOG_INFO(log, "Removed {} parts.", parts_to_remove.size());
}
clearOldPartsFromFilesystem();

View File

@ -150,7 +150,7 @@ private:
void clearOldMutations(bool truncate = false);
// Partition helpers
void dropPartition(const ASTPtr & partition, bool detach, const Context & context);
void dropPartition(const ASTPtr & partition, bool detach, bool drop_part, const Context & context);
PartitionCommandsResultInfo attachPartition(const ASTPtr & partition, bool part, const Context & context);
void replacePartitionFrom(const StoragePtr & source_table, const ASTPtr & partition, bool replace, const Context & context);

View File

@ -0,0 +1,12 @@
0
1
2
0
2
all_2_2_0
0
1
2
-- drop part --
0
2

View File

@ -0,0 +1,31 @@
DROP TABLE IF EXISTS attach_01451_mt;
CREATE TABLE attach_01451_mt (v UInt8) ENGINE = MergeTree() order by tuple();
INSERT INTO attach_01451_mt VALUES (0);
INSERT INTO attach_01451_mt VALUES (1);
INSERT INTO attach_01451_mt VALUES (2);
SELECT v FROM attach_01451_mt ORDER BY v;
ALTER TABLE attach_01451_mt DETACH PART 'all_2_2_0';
SELECT v FROM attach_01451_mt ORDER BY v;
SELECT name FROM system.detached_parts WHERE table = 'attach_01451_mt';
ALTER TABLE attach_01451_mt ATTACH PART 'all_2_2_0';
SELECT v FROM attach_01451_mt ORDER BY v;
SELECT name FROM system.detached_parts WHERE table = 'attach_01451_mt';
SELECT '-- drop part --';
ALTER TABLE attach_01451_mt DROP PART 'all_4_4_0';
ALTER TABLE attach_01451_mt ATTACH PART 'all_4_4_0'; -- { serverError 233 }
SELECT v FROM attach_01451_mt ORDER BY v;
DROP TABLE attach_01451_mt;