2013-08-07 13:07:42 +00:00
|
|
|
#include <DB/Interpreters/InterpreterAlterQuery.h>
|
2014-07-11 08:12:03 +00:00
|
|
|
#include <DB/Interpreters/InterpreterCreateQuery.h>
|
2013-08-07 13:07:42 +00:00
|
|
|
#include <DB/Parsers/ASTAlterQuery.h>
|
|
|
|
#include <DB/Parsers/ASTCreateQuery.h>
|
|
|
|
#include <DB/Parsers/ASTExpressionList.h>
|
|
|
|
#include <DB/Parsers/ASTNameTypePair.h>
|
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
2015-03-02 01:10:58 +00:00
|
|
|
#include <DB/Parsers/ASTLiteral.h>
|
2016-01-28 01:00:27 +00:00
|
|
|
#include <DB/Parsers/ASTWeightedZooKeeperPath.h>
|
2013-08-07 13:07:42 +00:00
|
|
|
|
|
|
|
#include <DB/Parsers/ParserCreateQuery.h>
|
|
|
|
#include <DB/IO/copyData.h>
|
2015-04-16 06:12:35 +00:00
|
|
|
#include <DB/IO/ReadBufferFromFile.h>
|
2013-08-07 13:07:42 +00:00
|
|
|
#include <DB/Common/escapeForFileName.h>
|
2015-05-28 03:49:28 +00:00
|
|
|
#include <DB/DataTypes/DataTypeFactory.h>
|
2013-08-07 13:07:42 +00:00
|
|
|
#include <DB/Parsers/formatAST.h>
|
2015-04-11 03:10:23 +00:00
|
|
|
#include <DB/Parsers/parseQuery.h>
|
2013-08-07 13:07:42 +00:00
|
|
|
|
2014-06-03 23:09:57 +00:00
|
|
|
#include <Poco/FileStream.h>
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
2016-11-20 12:43:20 +00:00
|
|
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2013-09-23 12:01:19 +00:00
|
|
|
|
2016-01-28 01:00:27 +00:00
|
|
|
InterpreterAlterQuery::InterpreterAlterQuery(ASTPtr query_ptr_, const Context & context_)
|
2013-08-09 00:12:59 +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
|
|
|
{
|
2014-10-07 09:09:59 +00:00
|
|
|
auto & alter = typeid_cast<ASTAlterQuery &>(*query_ptr);
|
|
|
|
const String & table_name = alter.table;
|
2014-07-11 08:12:03 +00:00
|
|
|
String database_name = alter.database.empty() ? context.getCurrentDatabase() : alter.database;
|
2014-10-07 09:09:59 +00:00
|
|
|
StoragePtr table = context.getTable(database_name, table_name);
|
|
|
|
|
2014-08-07 09:23:55 +00:00
|
|
|
AlterCommands alter_commands;
|
|
|
|
PartitionCommands partition_commands;
|
2015-05-28 03:49:28 +00:00
|
|
|
parseAlter(alter.parameters, alter_commands, partition_commands);
|
2013-12-18 11:19:37 +00:00
|
|
|
|
2014-08-07 09:23:55 +00:00
|
|
|
for (const PartitionCommand & command : partition_commands)
|
|
|
|
{
|
2014-10-09 20:28:33 +00:00
|
|
|
switch (command.type)
|
|
|
|
{
|
|
|
|
case PartitionCommand::DROP_PARTITION:
|
2015-10-02 21:28:19 +00:00
|
|
|
table->dropPartition(query_ptr, command.partition, command.detach, command.unreplicated, context.getSettingsRef());
|
2014-10-09 20:28:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PartitionCommand::ATTACH_PARTITION:
|
2015-10-02 21:28:19 +00:00
|
|
|
table->attachPartition(query_ptr, command.partition, command.unreplicated, command.part, context.getSettingsRef());
|
2014-10-09 20:28:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PartitionCommand::FETCH_PARTITION:
|
2014-10-18 19:14:09 +00:00
|
|
|
table->fetchPartition(command.partition, command.from, context.getSettingsRef());
|
2014-10-09 20:28:33 +00:00
|
|
|
break;
|
|
|
|
|
2014-11-11 04:11:07 +00:00
|
|
|
case PartitionCommand::FREEZE_PARTITION:
|
2016-06-28 20:50:37 +00:00
|
|
|
table->freezePartition(command.partition, command.with_name, context.getSettingsRef());
|
2014-11-11 04:11:07 +00:00
|
|
|
break;
|
|
|
|
|
2016-01-28 01:00:27 +00:00
|
|
|
case PartitionCommand::RESHARD_PARTITION:
|
2016-03-01 17:47:53 +00:00
|
|
|
table->reshardPartitions(query_ptr, database_name, command.partition, command.last_partition,
|
2016-03-25 11:48:45 +00:00
|
|
|
command.weighted_zookeeper_paths, command.sharding_key_expr, command.do_copy,
|
2016-03-01 17:47:53 +00:00
|
|
|
command.coordinator, context.getSettingsRef());
|
2016-01-28 01:00:27 +00:00
|
|
|
break;
|
|
|
|
|
2014-10-09 20:28:33 +00:00
|
|
|
default:
|
2017-03-14 00:14:19 +00:00
|
|
|
throw Exception("Bad PartitionCommand::Type: " + toString<int>(command.type), ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
2014-10-09 20:28:33 +00:00
|
|
|
}
|
2014-08-07 09:23:55 +00:00
|
|
|
}
|
2014-08-06 10:26:35 +00:00
|
|
|
|
2014-10-21 12:11:20 +00:00
|
|
|
if (alter_commands.empty())
|
2015-06-18 02:11:05 +00:00
|
|
|
return {};
|
2014-10-21 12:11:20 +00:00
|
|
|
|
|
|
|
alter_commands.validate(table.get(), context);
|
|
|
|
|
|
|
|
table->alter(alter_commands, database_name, table_name, context);
|
2015-06-18 02:11:05 +00:00
|
|
|
|
|
|
|
return {};
|
2013-12-18 11:19:37 +00:00
|
|
|
}
|
|
|
|
|
2014-08-06 10:26:35 +00:00
|
|
|
void InterpreterAlterQuery::parseAlter(
|
2015-05-28 03:49:28 +00:00
|
|
|
const ASTAlterQuery::ParameterContainer & params_container,
|
2014-08-07 09:23:55 +00:00
|
|
|
AlterCommands & out_alter_commands, PartitionCommands & out_partition_commands)
|
2014-04-02 18:53:30 +00:00
|
|
|
{
|
2015-05-28 03:49:28 +00:00
|
|
|
const DataTypeFactory & data_type_factory = DataTypeFactory::instance();
|
|
|
|
|
2014-07-11 08:12:03 +00:00
|
|
|
for (const auto & params : params_container)
|
2014-04-02 18:53:30 +00:00
|
|
|
{
|
2014-08-06 09:24:30 +00:00
|
|
|
if (params.type == ASTAlterQuery::ADD_COLUMN)
|
2014-04-02 18:53:30 +00:00
|
|
|
{
|
2014-08-06 10:26:35 +00:00
|
|
|
AlterCommand command;
|
2016-05-05 18:28:46 +00:00
|
|
|
command.type = AlterCommand::ADD_COLUMN;
|
2014-04-02 18:53:30 +00:00
|
|
|
|
2014-10-07 09:09:59 +00:00
|
|
|
const auto & ast_col_decl = typeid_cast<const ASTColumnDeclaration &>(*params.col_decl);
|
2014-05-20 15:00:13 +00:00
|
|
|
|
2014-10-07 09:09:59 +00:00
|
|
|
command.column_name = ast_col_decl.name;
|
2014-10-16 13:37:01 +00:00
|
|
|
if (ast_col_decl.type)
|
|
|
|
{
|
|
|
|
StringRange type_range = ast_col_decl.type->range;
|
|
|
|
String type_string(type_range.first, type_range.second - type_range.first);
|
|
|
|
command.data_type = data_type_factory.get(type_string);
|
|
|
|
}
|
|
|
|
if (ast_col_decl.default_expression)
|
|
|
|
{
|
|
|
|
command.default_type = columnDefaultTypeFromString(ast_col_decl.default_specifier);
|
2014-10-21 12:11:20 +00:00
|
|
|
command.default_expression = ast_col_decl.default_expression;
|
2014-10-16 13:37:01 +00:00
|
|
|
}
|
2014-05-20 15:00:13 +00:00
|
|
|
|
2014-07-11 08:12:03 +00:00
|
|
|
if (params.column)
|
|
|
|
command.after_column = typeid_cast<const ASTIdentifier &>(*params.column).name;
|
2014-08-06 10:26:35 +00:00
|
|
|
|
2016-05-05 18:28:46 +00:00
|
|
|
out_alter_commands.emplace_back(std::move(command));
|
2014-04-02 18:53:30 +00:00
|
|
|
}
|
2014-08-06 09:24:30 +00:00
|
|
|
else if (params.type == ASTAlterQuery::DROP_COLUMN)
|
2014-05-20 15:44:00 +00:00
|
|
|
{
|
2014-08-06 10:26:35 +00:00
|
|
|
AlterCommand command;
|
2016-05-05 18:28:46 +00:00
|
|
|
command.type = AlterCommand::DROP_COLUMN;
|
2014-07-11 08:12:03 +00:00
|
|
|
command.column_name = typeid_cast<const ASTIdentifier &>(*(params.column)).name;
|
2014-08-06 10:26:35 +00:00
|
|
|
|
2016-05-05 18:28:46 +00:00
|
|
|
out_alter_commands.emplace_back(std::move(command));
|
2014-05-20 15:44:00 +00:00
|
|
|
}
|
2014-08-06 09:24:30 +00:00
|
|
|
else if (params.type == ASTAlterQuery::MODIFY_COLUMN)
|
2014-05-20 15:00:13 +00:00
|
|
|
{
|
2014-08-06 10:26:35 +00:00
|
|
|
AlterCommand command;
|
2016-05-05 18:28:46 +00:00
|
|
|
command.type = AlterCommand::MODIFY_COLUMN;
|
2014-05-20 15:44:00 +00:00
|
|
|
|
2014-10-07 09:09:59 +00:00
|
|
|
const auto & ast_col_decl = typeid_cast<const ASTColumnDeclaration &>(*params.col_decl);
|
2014-05-20 15:00:13 +00:00
|
|
|
|
2014-10-07 09:09:59 +00:00
|
|
|
command.column_name = ast_col_decl.name;
|
2014-10-16 13:37:01 +00:00
|
|
|
if (ast_col_decl.type)
|
|
|
|
{
|
|
|
|
StringRange type_range = ast_col_decl.type->range;
|
|
|
|
String type_string(type_range.first, type_range.second - type_range.first);
|
|
|
|
command.data_type = data_type_factory.get(type_string);
|
|
|
|
}
|
2014-05-20 15:00:13 +00:00
|
|
|
|
2014-10-16 13:37:01 +00:00
|
|
|
if (ast_col_decl.default_expression)
|
|
|
|
{
|
|
|
|
command.default_type = columnDefaultTypeFromString(ast_col_decl.default_specifier);
|
2014-10-21 12:11:20 +00:00
|
|
|
command.default_expression = ast_col_decl.default_expression;
|
2014-10-16 13:37:01 +00:00
|
|
|
}
|
2014-08-06 10:26:35 +00:00
|
|
|
|
2016-05-05 18:28:46 +00:00
|
|
|
out_alter_commands.emplace_back(std::move(command));
|
|
|
|
}
|
|
|
|
else if (params.type == ASTAlterQuery::MODIFY_PRIMARY_KEY)
|
|
|
|
{
|
|
|
|
AlterCommand command;
|
|
|
|
command.type = AlterCommand::MODIFY_PRIMARY_KEY;
|
|
|
|
command.primary_key = params.primary_key;
|
|
|
|
out_alter_commands.emplace_back(std::move(command));
|
2014-08-06 10:26:35 +00:00
|
|
|
}
|
|
|
|
else if (params.type == ASTAlterQuery::DROP_PARTITION)
|
|
|
|
{
|
2014-08-06 10:28:36 +00:00
|
|
|
const Field & partition = dynamic_cast<const ASTLiteral &>(*params.partition).value;
|
2016-05-05 18:28:46 +00:00
|
|
|
out_partition_commands.emplace_back(PartitionCommand::dropPartition(partition, params.detach, params.unreplicated));
|
2014-05-20 15:00:13 +00:00
|
|
|
}
|
2014-08-07 11:46:01 +00:00
|
|
|
else if (params.type == ASTAlterQuery::ATTACH_PARTITION)
|
|
|
|
{
|
|
|
|
const Field & partition = dynamic_cast<const ASTLiteral &>(*params.partition).value;
|
2016-05-05 18:28:46 +00:00
|
|
|
out_partition_commands.emplace_back(PartitionCommand::attachPartition(partition, params.unreplicated, params.part));
|
2014-08-07 11:46:01 +00:00
|
|
|
}
|
2014-10-09 20:28:33 +00:00
|
|
|
else if (params.type == ASTAlterQuery::FETCH_PARTITION)
|
|
|
|
{
|
|
|
|
const Field & partition = dynamic_cast<const ASTLiteral &>(*params.partition).value;
|
2016-05-05 18:28:46 +00:00
|
|
|
out_partition_commands.emplace_back(PartitionCommand::fetchPartition(partition, params.from));
|
2014-10-09 20:28:33 +00:00
|
|
|
}
|
2014-11-11 04:11:07 +00:00
|
|
|
else if (params.type == ASTAlterQuery::FREEZE_PARTITION)
|
|
|
|
{
|
|
|
|
const Field & partition = dynamic_cast<const ASTLiteral &>(*params.partition).value;
|
2016-06-28 20:50:37 +00:00
|
|
|
out_partition_commands.emplace_back(PartitionCommand::freezePartition(partition, params.with_name));
|
2014-11-11 04:11:07 +00:00
|
|
|
}
|
2016-01-28 01:00:27 +00:00
|
|
|
else if (params.type == ASTAlterQuery::RESHARD_PARTITION)
|
|
|
|
{
|
|
|
|
Field first_partition;
|
|
|
|
if (params.partition)
|
|
|
|
first_partition = dynamic_cast<const ASTLiteral &>(*params.partition).value;
|
|
|
|
|
|
|
|
Field last_partition;
|
|
|
|
if (params.last_partition)
|
|
|
|
last_partition = dynamic_cast<const ASTLiteral &>(*params.last_partition).value;
|
|
|
|
else
|
|
|
|
last_partition = first_partition;
|
|
|
|
|
|
|
|
WeightedZooKeeperPaths weighted_zookeeper_paths;
|
|
|
|
|
|
|
|
const ASTs & ast_weighted_zookeeper_paths = typeid_cast<const ASTExpressionList &>(*params.weighted_zookeeper_paths).children;
|
|
|
|
for (size_t i = 0; i < ast_weighted_zookeeper_paths.size(); ++i)
|
|
|
|
{
|
|
|
|
const auto & weighted_zookeeper_path = typeid_cast<const ASTWeightedZooKeeperPath &>(*ast_weighted_zookeeper_paths[i]);
|
|
|
|
weighted_zookeeper_paths.emplace_back(weighted_zookeeper_path.path, weighted_zookeeper_path.weight);
|
|
|
|
}
|
|
|
|
|
2016-03-01 17:47:53 +00:00
|
|
|
Field coordinator;
|
|
|
|
if (params.coordinator)
|
|
|
|
coordinator = dynamic_cast<const ASTLiteral &>(*params.coordinator).value;
|
|
|
|
|
2016-05-05 18:28:46 +00:00
|
|
|
out_partition_commands.emplace_back(PartitionCommand::reshardPartitions(
|
2016-03-25 11:48:45 +00:00
|
|
|
first_partition, last_partition, weighted_zookeeper_paths, params.sharding_key_expr,
|
|
|
|
params.do_copy, coordinator));
|
2016-01-28 01:00:27 +00:00
|
|
|
}
|
2014-05-20 15:00:13 +00:00
|
|
|
else
|
2014-07-11 08:12:03 +00:00
|
|
|
throw Exception("Wrong parameter type in ALTER query", ErrorCodes::LOGICAL_ERROR);
|
2014-05-20 15:00:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|