2018-06-13 13:49:27 +00:00
|
|
|
#include <Storages/PartitionCommands.h>
|
|
|
|
#include <Storages/IStorage.h>
|
2020-05-22 13:29:33 +00:00
|
|
|
#include <Storages/DataDestinationType.h>
|
2018-06-13 13:49:27 +00:00
|
|
|
#include <Parsers/ASTAlterQuery.h>
|
2020-07-28 15:10:36 +00:00
|
|
|
#include <Core/ColumnWithTypeAndName.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
2020-07-29 08:32:52 +00:00
|
|
|
#include <Processors/Chunk.h>
|
2021-10-16 14:03:50 +00:00
|
|
|
#include <QueryPipeline/Pipe.h>
|
2020-07-29 08:32:52 +00:00
|
|
|
#include <Processors/Sources/SourceFromSingleChunk.h>
|
2020-07-28 15:10:36 +00:00
|
|
|
|
2018-06-13 13:49:27 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-11-24 14:24:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2018-06-13 13:49:27 +00:00
|
|
|
std::optional<PartitionCommand> PartitionCommand::parse(const ASTAlterCommand * command_ast)
|
|
|
|
{
|
|
|
|
if (command_ast->type == ASTAlterCommand::DROP_PARTITION)
|
|
|
|
{
|
|
|
|
PartitionCommand res;
|
|
|
|
res.type = DROP_PARTITION;
|
|
|
|
res.partition = command_ast->partition;
|
|
|
|
res.detach = command_ast->detach;
|
2020-09-04 15:48:51 +00:00
|
|
|
res.part = command_ast->part;
|
2018-06-13 13:49:27 +00:00
|
|
|
return res;
|
|
|
|
}
|
2019-07-22 11:23:11 +00:00
|
|
|
else if (command_ast->type == ASTAlterCommand::DROP_DETACHED_PARTITION)
|
|
|
|
{
|
|
|
|
PartitionCommand res;
|
|
|
|
res.type = DROP_DETACHED_PARTITION;
|
|
|
|
res.partition = command_ast->partition;
|
|
|
|
res.part = command_ast->part;
|
|
|
|
return res;
|
|
|
|
}
|
2018-06-13 13:49:27 +00:00
|
|
|
else if (command_ast->type == ASTAlterCommand::ATTACH_PARTITION)
|
|
|
|
{
|
|
|
|
PartitionCommand res;
|
|
|
|
res.type = ATTACH_PARTITION;
|
|
|
|
res.partition = command_ast->partition;
|
|
|
|
res.part = command_ast->part;
|
|
|
|
return res;
|
|
|
|
}
|
2019-07-18 15:19:03 +00:00
|
|
|
else if (command_ast->type == ASTAlterCommand::MOVE_PARTITION)
|
2018-06-13 13:49:27 +00:00
|
|
|
{
|
|
|
|
PartitionCommand res;
|
2019-07-18 15:19:03 +00:00
|
|
|
res.type = MOVE_PARTITION;
|
2018-06-13 13:49:27 +00:00
|
|
|
res.partition = command_ast->partition;
|
2019-07-18 15:19:03 +00:00
|
|
|
res.part = command_ast->part;
|
2019-07-23 09:38:26 +00:00
|
|
|
switch (command_ast->move_destination_type)
|
2019-07-18 15:19:03 +00:00
|
|
|
{
|
2020-05-22 13:29:33 +00:00
|
|
|
case DataDestinationType::DISK:
|
2019-07-23 13:34:17 +00:00
|
|
|
res.move_destination_type = PartitionCommand::MoveDestinationType::DISK;
|
2019-07-18 15:19:03 +00:00
|
|
|
break;
|
2020-05-22 13:29:33 +00:00
|
|
|
case DataDestinationType::VOLUME:
|
2019-07-23 13:34:17 +00:00
|
|
|
res.move_destination_type = PartitionCommand::MoveDestinationType::VOLUME;
|
2019-07-18 15:19:03 +00:00
|
|
|
break;
|
2020-05-22 13:29:33 +00:00
|
|
|
case DataDestinationType::TABLE:
|
2019-09-17 11:59:09 +00:00
|
|
|
res.move_destination_type = PartitionCommand::MoveDestinationType::TABLE;
|
2019-09-16 07:27:38 +00:00
|
|
|
res.to_database = command_ast->to_database;
|
|
|
|
res.to_table = command_ast->to_table;
|
2019-12-17 07:06:41 +00:00
|
|
|
break;
|
2020-11-24 14:24:48 +00:00
|
|
|
case DataDestinationType::SHARD:
|
|
|
|
res.move_destination_type = PartitionCommand::MoveDestinationType::SHARD;
|
2019-09-16 07:27:38 +00:00
|
|
|
break;
|
2020-11-24 14:24:48 +00:00
|
|
|
case DataDestinationType::DELETE:
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "ALTER with this destination type is not handled. This is a bug.");
|
2019-07-18 15:19:03 +00:00
|
|
|
}
|
2019-09-17 11:59:09 +00:00
|
|
|
if (res.move_destination_type != PartitionCommand::MoveDestinationType::TABLE)
|
2019-09-16 07:27:38 +00:00
|
|
|
res.move_destination_name = command_ast->move_destination_name;
|
2018-06-13 13:49:27 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else if (command_ast->type == ASTAlterCommand::REPLACE_PARTITION)
|
2019-07-26 08:42:17 +00:00
|
|
|
{
|
|
|
|
PartitionCommand res;
|
2018-06-13 13:49:27 +00:00
|
|
|
res.type = REPLACE_PARTITION;
|
2019-07-26 08:42:17 +00:00
|
|
|
res.partition = command_ast->partition;
|
2018-06-13 13:49:27 +00:00
|
|
|
res.replace = command_ast->replace;
|
|
|
|
res.from_database = command_ast->from_database;
|
|
|
|
res.from_table = command_ast->from_table;
|
2019-07-26 08:42:17 +00:00
|
|
|
return res;
|
|
|
|
}
|
2018-06-13 13:49:27 +00:00
|
|
|
else if (command_ast->type == ASTAlterCommand::FETCH_PARTITION)
|
|
|
|
{
|
|
|
|
PartitionCommand res;
|
|
|
|
res.type = FETCH_PARTITION;
|
|
|
|
res.partition = command_ast->partition;
|
|
|
|
res.from_zookeeper_path = command_ast->from;
|
2021-04-13 04:40:33 +00:00
|
|
|
res.part = command_ast->part;
|
2018-06-13 13:49:27 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else if (command_ast->type == ASTAlterCommand::FREEZE_PARTITION)
|
|
|
|
{
|
|
|
|
PartitionCommand res;
|
|
|
|
res.type = FREEZE_PARTITION;
|
|
|
|
res.partition = command_ast->partition;
|
|
|
|
res.with_name = command_ast->with_name;
|
|
|
|
return res;
|
|
|
|
}
|
2018-11-20 17:05:22 +00:00
|
|
|
else if (command_ast->type == ASTAlterCommand::FREEZE_ALL)
|
|
|
|
{
|
2021-03-02 20:28:42 +00:00
|
|
|
PartitionCommand res;
|
|
|
|
res.type = PartitionCommand::FREEZE_ALL_PARTITIONS;
|
|
|
|
res.with_name = command_ast->with_name;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else if (command_ast->type == ASTAlterCommand::UNFREEZE_PARTITION)
|
|
|
|
{
|
|
|
|
PartitionCommand res;
|
|
|
|
res.type = PartitionCommand::UNFREEZE_PARTITION;
|
|
|
|
res.partition = command_ast->partition;
|
|
|
|
res.with_name = command_ast->with_name;
|
|
|
|
return res;
|
2018-11-20 17:05:22 +00:00
|
|
|
}
|
2021-03-02 20:28:42 +00:00
|
|
|
else if (command_ast->type == ASTAlterCommand::UNFREEZE_ALL)
|
2021-02-24 14:26:46 +00:00
|
|
|
{
|
2021-03-02 20:28:42 +00:00
|
|
|
PartitionCommand res;
|
|
|
|
res.type = PartitionCommand::UNFREEZE_ALL_PARTITIONS;
|
|
|
|
res.with_name = command_ast->with_name;
|
|
|
|
return res;
|
2021-02-24 14:26:46 +00:00
|
|
|
}
|
2018-06-13 13:49:27 +00:00
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-17 17:22:41 +00:00
|
|
|
|
2020-07-28 15:10:36 +00:00
|
|
|
std::string PartitionCommand::typeToString() const
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case PartitionCommand::Type::ATTACH_PARTITION:
|
|
|
|
if (part)
|
|
|
|
return "ATTACH PART";
|
|
|
|
else
|
|
|
|
return "ATTACH PARTITION";
|
|
|
|
case PartitionCommand::Type::MOVE_PARTITION:
|
|
|
|
return "MOVE PARTITION";
|
|
|
|
case PartitionCommand::Type::DROP_PARTITION:
|
|
|
|
if (detach)
|
|
|
|
return "DETACH PARTITION";
|
|
|
|
else
|
|
|
|
return "DROP PARTITION";
|
|
|
|
case PartitionCommand::Type::DROP_DETACHED_PARTITION:
|
|
|
|
if (part)
|
|
|
|
return "DROP DETACHED PART";
|
|
|
|
else
|
|
|
|
return "DROP DETACHED PARTITION";
|
|
|
|
case PartitionCommand::Type::FETCH_PARTITION:
|
2021-04-13 04:40:33 +00:00
|
|
|
if (part)
|
|
|
|
return "FETCH PART";
|
|
|
|
else
|
|
|
|
return "FETCH PARTITION";
|
2020-07-28 15:10:36 +00:00
|
|
|
case PartitionCommand::Type::FREEZE_ALL_PARTITIONS:
|
|
|
|
return "FREEZE ALL";
|
|
|
|
case PartitionCommand::Type::FREEZE_PARTITION:
|
|
|
|
return "FREEZE PARTITION";
|
2021-03-02 20:28:42 +00:00
|
|
|
case PartitionCommand::Type::UNFREEZE_PARTITION:
|
|
|
|
return "UNFREEZE PARTITION";
|
|
|
|
case PartitionCommand::Type::UNFREEZE_ALL_PARTITIONS:
|
|
|
|
return "UNFREEZE ALL";
|
2020-07-28 15:10:36 +00:00
|
|
|
case PartitionCommand::Type::REPLACE_PARTITION:
|
|
|
|
return "REPLACE PARTITION";
|
2021-05-08 15:20:40 +00:00
|
|
|
default:
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Uninitialized partition command");
|
2020-07-28 15:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:54:14 +00:00
|
|
|
Pipe convertCommandsResultToSource(const PartitionCommandsResultInfo & commands_result)
|
2020-07-28 15:10:36 +00:00
|
|
|
{
|
|
|
|
Block header {
|
|
|
|
ColumnWithTypeAndName(std::make_shared<DataTypeString>(), "command_type"),
|
|
|
|
ColumnWithTypeAndName(std::make_shared<DataTypeString>(), "partition_id"),
|
|
|
|
ColumnWithTypeAndName(std::make_shared<DataTypeString>(), "part_name"),
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto & command_result : commands_result)
|
|
|
|
{
|
|
|
|
if (!command_result.old_part_name.empty() && !header.has("old_part_name"))
|
|
|
|
header.insert(ColumnWithTypeAndName(std::make_shared<DataTypeString>(), "old_part_name"));
|
|
|
|
|
|
|
|
if (!command_result.backup_name.empty() && !header.has("backup_name"))
|
|
|
|
header.insert(ColumnWithTypeAndName(std::make_shared<DataTypeString>(), "backup_name"));
|
|
|
|
|
|
|
|
if (!command_result.backup_path.empty() && !header.has("backup_path"))
|
|
|
|
header.insert(ColumnWithTypeAndName(std::make_shared<DataTypeString>(), "backup_path"));
|
2020-07-30 17:03:26 +00:00
|
|
|
if (!command_result.backup_path.empty() && !header.has("part_backup_path"))
|
|
|
|
header.insert(ColumnWithTypeAndName(std::make_shared<DataTypeString>(), "part_backup_path"));
|
2020-07-28 15:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MutableColumns res_columns = header.cloneEmptyColumns();
|
|
|
|
|
|
|
|
for (const auto & command_result : commands_result)
|
|
|
|
{
|
|
|
|
res_columns[0]->insert(command_result.command_type);
|
|
|
|
res_columns[1]->insert(command_result.partition_id);
|
|
|
|
res_columns[2]->insert(command_result.part_name);
|
|
|
|
if (header.has("old_part_name"))
|
|
|
|
{
|
|
|
|
size_t pos = header.getPositionByName("old_part_name");
|
|
|
|
res_columns[pos]->insert(command_result.old_part_name);
|
|
|
|
}
|
|
|
|
if (header.has("backup_name"))
|
|
|
|
{
|
|
|
|
size_t pos = header.getPositionByName("backup_name");
|
|
|
|
res_columns[pos]->insert(command_result.backup_name);
|
|
|
|
}
|
|
|
|
if (header.has("backup_path"))
|
|
|
|
{
|
|
|
|
size_t pos = header.getPositionByName("backup_path");
|
|
|
|
res_columns[pos]->insert(command_result.backup_path);
|
|
|
|
}
|
2020-07-30 17:03:26 +00:00
|
|
|
if (header.has("part_backup_path"))
|
|
|
|
{
|
|
|
|
size_t pos = header.getPositionByName("part_backup_path");
|
|
|
|
res_columns[pos]->insert(command_result.part_backup_path);
|
|
|
|
}
|
2020-07-28 15:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Chunk chunk(std::move(res_columns), commands_result.size());
|
2020-08-03 13:54:14 +00:00
|
|
|
return Pipe(std::make_shared<SourceFromSingleChunk>(std::move(header), std::move(chunk)));
|
2020-07-28 15:10:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 13:49:27 +00:00
|
|
|
}
|