2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterAlterQuery.h>
|
2017-04-25 15:21:03 +00:00
|
|
|
#include <Interpreters/DDLWorker.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTAlterQuery.h>
|
2018-06-13 13:49:27 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2014-06-03 23:09:57 +00:00
|
|
|
|
2013-08-07 13:07:42 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-08-09 00:12:59 +00:00
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2017-07-14 18:09:28 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2013-09-23 12:01:19 +00:00
|
|
|
|
2017-05-23 18:01:50 +00:00
|
|
|
InterpreterAlterQuery::InterpreterAlterQuery(const ASTPtr & query_ptr_, const Context & context_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: query_ptr(query_ptr_), context(context_)
|
2013-08-07 13:07:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
BlockIO InterpreterAlterQuery::execute()
|
2013-12-18 11:19:37 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto & alter = typeid_cast<ASTAlterQuery &>(*query_ptr);
|
2017-04-25 15:21:03 +00:00
|
|
|
|
|
|
|
if (!alter.cluster.empty())
|
2018-04-17 19:33:58 +00:00
|
|
|
return executeDDLQueryOnCluster(query_ptr, context, {alter.table});
|
2017-04-25 15:21:03 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const String & table_name = alter.table;
|
|
|
|
String database_name = alter.database.empty() ? context.getCurrentDatabase() : alter.database;
|
|
|
|
StoragePtr table = context.getTable(database_name, table_name);
|
|
|
|
|
|
|
|
AlterCommands alter_commands;
|
|
|
|
PartitionCommands partition_commands;
|
2018-02-02 16:02:43 +00:00
|
|
|
MutationCommands mutation_commands;
|
2018-06-13 13:49:27 +00:00
|
|
|
for (ASTAlterCommand * command_ast : alter.command_list->commands)
|
|
|
|
{
|
|
|
|
if (auto alter_command = AlterCommand::parse(command_ast))
|
|
|
|
alter_commands.emplace_back(std::move(*alter_command));
|
|
|
|
else if (auto partition_command = PartitionCommand::parse(command_ast))
|
|
|
|
partition_commands.emplace_back(std::move(*partition_command));
|
|
|
|
else if (auto mut_command = MutationCommand::parse(command_ast))
|
|
|
|
mutation_commands.emplace_back(std::move(*mut_command));
|
|
|
|
else
|
|
|
|
throw Exception("Wrong parameter type in ALTER query", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
2018-02-02 16:02:43 +00:00
|
|
|
|
2018-06-13 13:49:27 +00:00
|
|
|
if (!mutation_commands.empty())
|
2018-02-02 16:02:43 +00:00
|
|
|
{
|
2018-05-15 12:56:14 +00:00
|
|
|
mutation_commands.validate(*table, context);
|
2018-02-02 16:02:43 +00:00
|
|
|
table->mutate(mutation_commands, context);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-05-15 12:56:14 +00:00
|
|
|
partition_commands.validate(*table);
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const PartitionCommand & command : partition_commands)
|
|
|
|
{
|
|
|
|
switch (command.type)
|
|
|
|
{
|
|
|
|
case PartitionCommand::DROP_PARTITION:
|
2018-08-03 09:39:01 +00:00
|
|
|
table->checkPartitionCanBeDropped(command.partition);
|
|
|
|
table->dropPartition(query_ptr, command.partition, command.detach, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PartitionCommand::ATTACH_PARTITION:
|
2018-08-02 08:48:57 +00:00
|
|
|
table->attachPartition(command.partition, command.part, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
2018-05-21 13:49:54 +00:00
|
|
|
case PartitionCommand::REPLACE_PARTITION:
|
|
|
|
{
|
2018-08-03 09:39:01 +00:00
|
|
|
table->checkPartitionCanBeDropped(command.partition);
|
|
|
|
String from_database = command.from_database.empty() ? context.getCurrentDatabase() : command.from_database;
|
|
|
|
auto from_storage = context.getTable(from_database, command.from_table);
|
|
|
|
table->replacePartitionFrom(from_storage, command.partition, command.replace, context);
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
case PartitionCommand::FETCH_PARTITION:
|
2018-05-21 13:49:54 +00:00
|
|
|
table->fetchPartition(command.partition, command.from_zookeeper_path, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PartitionCommand::FREEZE_PARTITION:
|
2017-09-06 20:34:26 +00:00
|
|
|
table->freezePartition(command.partition, command.with_name, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
2017-06-22 11:01:30 +00:00
|
|
|
case PartitionCommand::CLEAR_COLUMN:
|
2017-09-06 20:34:26 +00:00
|
|
|
table->clearColumnInPartition(command.partition, command.column_name, context);
|
2017-04-14 12:40:48 +00:00
|
|
|
break;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 16:02:43 +00:00
|
|
|
if (!alter_commands.empty())
|
|
|
|
{
|
2018-05-15 12:56:14 +00:00
|
|
|
alter_commands.validate(*table, context);
|
2018-02-02 16:02:43 +00:00
|
|
|
table->alter(alter_commands, database_name, table_name, context);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return {};
|
2013-12-18 11:19:37 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|