This commit is contained in:
Nikita Vasilev 2019-01-13 22:31:48 +03:00
parent d0db87b412
commit e197697b22
5 changed files with 49 additions and 12 deletions

View File

@ -156,7 +156,8 @@ static bool namesEqual(const String & name_without_dot, const DB::NameAndTypePai
return (name_with_dot == name_type.name.substr(0, name_without_dot.length() + 1) || name_without_dot == name_type.name);
}
void AlterCommand::apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast, ASTPtr & primary_key_ast) const
void AlterCommand::apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast,
ASTPtr & primary_key_ast, ASTPtr & indexes_decl_ast) const
{
if (type == ADD_COLUMN)
{
@ -315,6 +316,29 @@ void AlterCommand::apply(ColumnsDescription & columns_description, ASTPtr & orde
{
columns_description.comments[column_name] = comment;
}
else if (type == ADD_INDEX)
{
if (std::any_of(
indexes_decl_ast->children.cbegin(),
indexes_decl_ast->children.cend(),
[this](const ASTPtr & index_ast){
return typeid_cast<const ASTIndexDeclaration &>(*index_ast).name == index_name;
}))
{
if (if_not_exists)
return;
else
throw Exception{"Cannot add index " + index_name + ": index with this name already exists",
ErrorCodes::ILLEGAL_COLUMN};
}
//auto insert_it = indexes_decl_ast->children.end();
// TODO: implementation
}
else if (type == DROP_INDEX)
{
// TODO: implementation
}
else
throw Exception("Wrong parameter type in ALTER query", ErrorCodes::LOGICAL_ERROR);
}
@ -329,19 +353,22 @@ bool AlterCommand::is_mutable() const
return true;
}
void AlterCommands::apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast, ASTPtr & primary_key_ast) const
void AlterCommands::apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast,
ASTPtr & primary_key_ast, ASTPtr & indexes_decl_ast) const
{
auto new_columns_description = columns_description;
auto new_order_by_ast = order_by_ast;
auto new_primary_key_ast = primary_key_ast;
auto new_indexes_decl_ast = indexes_decl_ast;
for (const AlterCommand & command : *this)
if (!command.ignore)
command.apply(new_columns_description, new_order_by_ast, new_primary_key_ast);
command.apply(new_columns_description, new_order_by_ast, new_primary_key_ast, new_indexes_decl_ast);
columns_description = std::move(new_columns_description);
order_by_ast = std::move(new_order_by_ast);
primary_key_ast = std::move(new_primary_key_ast);
indexes_decl_ast = std::move(new_indexes_decl_ast);
}
void AlterCommands::validate(const IStorage & table, const Context & context)
@ -558,12 +585,15 @@ void AlterCommands::apply(ColumnsDescription & columns_description) const
auto out_columns_description = columns_description;
ASTPtr out_order_by;
ASTPtr out_primary_key;
apply(out_columns_description, out_order_by, out_primary_key);
ASTPtr out_indexes_decl;
apply(out_columns_description, out_order_by, out_primary_key, out_indexes_decl);
if (out_order_by)
throw Exception("Storage doesn't support modifying ORDER BY expression", ErrorCodes::NOT_IMPLEMENTED);
if (out_primary_key)
throw Exception("Storage doesn't support modifying PRIMARY KEY expression", ErrorCodes::NOT_IMPLEMENTED);
if (out_indexes_decl)
throw Exception("Storage doesn't support modifying INDEXES", ErrorCodes::NOT_IMPLEMENTED);
columns_description = std::move(out_columns_description);
}

View File

@ -76,7 +76,8 @@ struct AlterCommand
static std::optional<AlterCommand> parse(const ASTAlterCommand * command);
void apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast, ASTPtr & primary_key_ast) const;
void apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast,
ASTPtr & primary_key_ast, ASTPtr & indexes_decl_ast) const;
/// Checks that not only metadata touched by that command
bool is_mutable() const;
};
@ -87,7 +88,8 @@ class Context;
class AlterCommands : public std::vector<AlterCommand>
{
public:
void apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast, ASTPtr & primary_key_ast) const;
void apply(ColumnsDescription & columns_description, ASTPtr & order_by_ast,
ASTPtr & primary_key_ast, ASTPtr & index_decl_ast) const;
/// For storages that don't support MODIFY_ORDER_BY.
void apply(ColumnsDescription & columns_description) const;

View File

@ -1042,7 +1042,8 @@ void MergeTreeData::checkAlter(const AlterCommands & commands)
auto new_columns = getColumns();
ASTPtr new_order_by_ast = order_by_ast;
ASTPtr new_primary_key_ast = primary_key_ast;
commands.apply(new_columns, new_order_by_ast, new_primary_key_ast);
ASTPtr new_indexes_ast = skip_indexes_ast;
commands.apply(new_columns, new_order_by_ast, new_primary_key_ast, new_indexes_ast);
/// Set of columns that shouldn't be altered.
NameSet columns_alter_forbidden;
@ -1122,7 +1123,7 @@ void MergeTreeData::checkAlter(const AlterCommands & commands)
}
setPrimaryKeyAndColumns(new_order_by_ast, new_primary_key_ast, new_columns, /* only_check = */ true);
setSkipIndexes(skip_indexes_ast, /* only_check = */ true);
setSkipIndexes(new_indexes_ast, /* only_check = */ true);
/// Check that type conversions are possible.
ExpressionActionsPtr unused_expression;

View File

@ -216,7 +216,8 @@ void StorageMergeTree::alter(
auto new_columns = data.getColumns();
ASTPtr new_order_by_ast = data.order_by_ast;
ASTPtr new_primary_key_ast = data.primary_key_ast;
params.apply(new_columns, new_order_by_ast, new_primary_key_ast);
ASTPtr new_indexes_ast = data.skip_indexes_ast;
params.apply(new_columns, new_order_by_ast, new_primary_key_ast, new_indexes_ast);
auto parts = data.getDataParts({MergeTreeDataPartState::PreCommitted, MergeTreeDataPartState::Committed, MergeTreeDataPartState::Outdated});
auto columns_for_parts = new_columns.getAllPhysical();
@ -702,7 +703,8 @@ void StorageMergeTree::clearColumnInPartition(const ASTPtr & partition, const Fi
auto new_columns = getColumns();
ASTPtr ignored_order_by_ast;
ASTPtr ignored_primary_key_ast;
alter_command.apply(new_columns, ignored_order_by_ast, ignored_primary_key_ast);
ASTPtr ignored_indexes_ast;
alter_command.apply(new_columns, ignored_order_by_ast, ignored_primary_key_ast, ignored_indexes_ast);
auto columns_for_parts = new_columns.getAllPhysical();
for (const auto & part : parts)

View File

@ -1489,7 +1489,8 @@ void StorageReplicatedMergeTree::executeClearColumnInPartition(const LogEntry &
auto new_columns = getColumns();
ASTPtr ignored_order_by_ast;
ASTPtr ignored_primary_key_ast;
alter_command.apply(new_columns, ignored_order_by_ast, ignored_primary_key_ast);
ASTPtr ignored_indexes_ast;
alter_command.apply(new_columns, ignored_order_by_ast, ignored_primary_key_ast, ignored_indexes_ast);
size_t modified_parts = 0;
auto parts = data.getDataParts();
@ -3067,7 +3068,8 @@ void StorageReplicatedMergeTree::alter(const AlterCommands & params,
ColumnsDescription new_columns = data.getColumns();
ASTPtr new_order_by_ast = data.order_by_ast;
ASTPtr new_primary_key_ast = data.primary_key_ast;
params.apply(new_columns, new_order_by_ast, new_primary_key_ast);
ASTPtr new_indexes_ast = data.skip_indexes_ast;
params.apply(new_columns, new_order_by_ast, new_primary_key_ast, new_indexes_ast);
String new_columns_str = new_columns.toString();
if (new_columns_str != data.getColumns().toString())