ClickHouse/dbms/src/Interpreters/InterpreterAlterQuery.cpp

115 lines
3.8 KiB
C++
Raw Normal View History

2013-08-07 13:07:42 +00:00
#include <DB/Interpreters/InterpreterAlterQuery.h>
#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>
#include <DB/Parsers/ParserCreateQuery.h>
#include <DB/IO/copyData.h>
#include <DB/Common/escapeForFileName.h>
#include <DB/Parsers/formatAST.h>
2013-09-23 12:01:19 +00:00
#include <DB/Storages/StorageMerge.h>
#include <DB/Storages/StorageMergeTree.h>
2014-05-21 10:34:27 +00:00
#include <DB/Storages/StorageReplicatedMergeTree.h>
2013-08-07 13:07:42 +00:00
#include <Poco/FileStream.h>
2013-08-07 13:07:42 +00:00
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/bind/placeholders.hpp>
2013-08-09 00:12:59 +00:00
2013-09-23 12:01:19 +00:00
using namespace DB;
2013-08-07 13:07:42 +00:00
InterpreterAlterQuery::InterpreterAlterQuery(ASTPtr query_ptr_, Context & context_)
2013-08-09 00:12:59 +00:00
: query_ptr(query_ptr_), context(context_)
2013-08-07 13:07:42 +00:00
{
}
void InterpreterAlterQuery::execute()
{
ASTAlterQuery & alter = typeid_cast<ASTAlterQuery &>(*query_ptr);
String & table_name = alter.table;
String database_name = alter.database.empty() ? context.getCurrentDatabase() : alter.database;
AlterCommands commands = parseAlter(alter.parameters, context.getDataTypeFactory());
StoragePtr table = context.getTable(database_name, table_name);
table->alter(commands, database_name, table_name, context);
}
AlterCommands InterpreterAlterQuery::parseAlter(
const ASTAlterQuery::ParameterContainer & params_container, const DataTypeFactory & data_type_factory)
{
AlterCommands res;
for (const auto & params : params_container)
{
res.push_back(AlterCommand());
AlterCommand & command = res.back();
if (params.type == ASTAlterQuery::ADD)
{
command.type = AlterCommand::ADD;
const ASTNameTypePair & ast_name_type = typeid_cast<const ASTNameTypePair &>(*params.name_type);
StringRange type_range = ast_name_type.type->range;
String type_string = String(type_range.first, type_range.second - type_range.first);
command.column_name = ast_name_type.name;
command.data_type = data_type_factory.get(type_string);
if (params.column)
command.after_column = typeid_cast<const ASTIdentifier &>(*params.column).name;
}
else if (params.type == ASTAlterQuery::DROP)
{
command.type = AlterCommand::DROP;
command.column_name = typeid_cast<const ASTIdentifier &>(*(params.column)).name;
}
else if (params.type == ASTAlterQuery::MODIFY)
{
command.type = AlterCommand::MODIFY;
const ASTNameTypePair & ast_name_type = typeid_cast<const ASTNameTypePair &>(*params.name_type);
StringRange type_range = ast_name_type.type->range;
String type_string = String(type_range.first, type_range.second - type_range.first);
command.column_name = ast_name_type.name;
command.data_type = data_type_factory.get(type_string);
}
else
throw Exception("Wrong parameter type in ALTER query", ErrorCodes::LOGICAL_ERROR);
}
return res;
}
void InterpreterAlterQuery::updateMetadata(
const String& database_name, const String& table_name, const NamesAndTypesList& columns, Context& context)
2013-08-07 13:07:42 +00:00
{
2013-09-23 12:01:19 +00:00
String path = context.getPath();
2013-08-07 13:07:42 +00:00
String database_name_escaped = escapeForFileName(database_name);
String table_name_escaped = escapeForFileName(table_name);
2013-08-09 00:12:59 +00:00
String metadata_path = path + "metadata/" + database_name_escaped + "/" + table_name_escaped + ".sql";
String metadata_temp_path = metadata_path + ".tmp";
2013-08-07 13:07:42 +00:00
2013-08-09 00:12:59 +00:00
ASTPtr attach_ptr = context.getCreateQuery(database_name, table_name);
ASTCreateQuery & attach = typeid_cast<ASTCreateQuery &>(*attach_ptr);
2013-08-07 13:07:42 +00:00
attach.attach = true;
ASTPtr new_columns = InterpreterCreateQuery::formatColumns(columns);
*std::find(attach.children.begin(), attach.children.end(), attach.columns) = new_columns;
attach.columns = new_columns;
2013-08-07 13:07:42 +00:00
{
Poco::FileOutputStream ostr(metadata_temp_path + ".tmp");
2013-08-07 13:07:42 +00:00
formatAST(attach, ostr, 0, false);
}
Poco::File(metadata_temp_path).renameTo(metadata_path);
2013-08-07 13:07:42 +00:00
}