Use ATTACH PARTITION ALL to copy data from the source table

This commit is contained in:
Tuan Pham Anh 2024-09-03 08:03:13 +00:00
parent 4b3da04f6c
commit 782353d831
2 changed files with 13 additions and 22 deletions

View File

@ -1957,27 +1957,20 @@ BlockIO InterpreterCreateQuery::fillTableIfNeeded(const ASTCreateQuery & create)
return {};
}
auto partition = std::make_shared<ASTPartition>();
partition->all = true;
auto command = std::make_shared<ASTAlterCommand>();
command->replace = false;
command->type = ASTAlterCommand::REPLACE_PARTITION;
command->partition = command->children.emplace_back(std::move(partition)).get();
command->from_database = as_database_name;
command->from_table = as_table_saved;
command->to_database = create.getDatabase();
command->to_table = create.getTable();
auto command_list = std::make_shared<ASTExpressionList>();
for (const auto & partition_id : merge_tree_table->getAllPartitionIds())
{
auto partition = std::make_shared<ASTPartition>();
partition->all = false;
partition->setPartitionID(std::make_shared<ASTLiteral>(partition_id));
auto command = std::make_shared<ASTAlterCommand>();
command->replace = false;
command->type = ASTAlterCommand::REPLACE_PARTITION;
command->partition = command->children.emplace_back(std::move(partition)).get();
command->from_database = as_database_name;
command->from_table = as_table_saved;
command->to_database = create.getDatabase();
command->to_table = create.getTable();
command_list->children.push_back(command);
}
command_list->children.push_back(command);
auto query = std::make_shared<ASTAlterQuery>();
query->database = create.database;

View File

@ -54,9 +54,7 @@ ${CLICKHOUSE_CLIENT} --optimize_throw_if_noop 1 -q "SHOW CREATE TABLE clone_as_f
echo "from foo_replacing_merge_tree"
${CLICKHOUSE_CLIENT} --optimize_throw_if_noop 1 -q "SELECT * FROM foo_replacing_merge_tree"
echo "from clone_as_foo_replacing_merge_tree"
${CLICKHOUSE_CLIENT} -q "SELECT mutation_id, command, block_numbers.partition_id, block_numbers.number, parts_to_do, is_done FROM system.mutations WHERE database = '$CLICKHOUSE_DATABASE' and table = 'clone_as_foo_replacing_merge_tree' ORDER BY mutation_id"
${CLICKHOUSE_CLIENT} --optimize_throw_if_noop 1 -q "SELECT * FROM clone_as_foo_replacing_merge_tree FINAL"
${CLICKHOUSE_CLIENT} -q "SELECT mutation_id, command, block_numbers.partition_id, block_numbers.number, parts_to_do, is_done FROM system.mutations WHERE database = '$CLICKHOUSE_DATABASE' and table = 'clone_as_foo_replacing_merge_tree' ORDER BY mutation_id"
${CLICKHOUSE_CLIENT} -q "DROP TABLE IF EXISTS foo_memory"
${CLICKHOUSE_CLIENT} -q "DROP TABLE IF EXISTS clone_as_foo_memory"