From 026a30092ebe13c1c3a9f8e2b1a90680a28d10c7 Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Tue, 15 Jan 2019 17:49:22 +0300 Subject: [PATCH] added alter --- dbms/src/Storages/AlterCommands.cpp | 33 ++++++++++++++++++++++---- dbms/src/Storages/StorageMergeTree.cpp | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/dbms/src/Storages/AlterCommands.cpp b/dbms/src/Storages/AlterCommands.cpp index a32d37dcea3..ae36da3cb0e 100644 --- a/dbms/src/Storages/AlterCommands.cpp +++ b/dbms/src/Storages/AlterCommands.cpp @@ -321,7 +321,7 @@ void AlterCommand::apply(ColumnsDescription & columns_description, ASTPtr & orde if (std::any_of( indexes_decl_ast->children.cbegin(), indexes_decl_ast->children.cend(), - [this](const ASTPtr & index_ast){ + [this](const ASTPtr & index_ast) { return typeid_cast(*index_ast).name == index_name; })) { @@ -332,12 +332,37 @@ void AlterCommand::apply(ColumnsDescription & columns_description, ASTPtr & orde ErrorCodes::ILLEGAL_COLUMN}; } - //auto insert_it = indexes_decl_ast->children.end(); - // TODO: implementation + auto insert_it = indexes_decl_ast->children.end(); + + if (!after_index_name.empty()) + { + insert_it = std::find_if( + indexes_decl_ast->children.begin(), + indexes_decl_ast->children.end(), + [this](const ASTPtr & index_ast) { + return typeid_cast(*index_ast).name == after_index_name; + }); + if (insert_it == indexes_decl_ast->children.end()) { + throw Exception("Wrong index name. Cannot find index `" + after_index_name + "` to insert after.", + ErrorCodes::LOGICAL_ERROR); + } + } + indexes_decl_ast->children.emplace(insert_it, index_decl); } else if (type == DROP_INDEX) { - // TODO: implementation + auto erase_it = std::find_if( + indexes_decl_ast->children.begin(), + indexes_decl_ast->children.end(), + [this](const ASTPtr & index_ast) { + return typeid_cast(*index_ast).name == index_name; + }); + if (erase_it == indexes_decl_ast->children.end()) + { + throw Exception("Wrong index name. Cannot find index `" + index_name + "` to drop.", + ErrorCodes::LOGICAL_ERROR); + } + indexes_decl_ast->children.erase(erase_it); } else throw Exception("Wrong parameter type in ALTER query", ErrorCodes::LOGICAL_ERROR); diff --git a/dbms/src/Storages/StorageMergeTree.cpp b/dbms/src/Storages/StorageMergeTree.cpp index 1fd0f422975..39dba4ed109 100644 --- a/dbms/src/Storages/StorageMergeTree.cpp +++ b/dbms/src/Storages/StorageMergeTree.cpp @@ -245,7 +245,7 @@ void StorageMergeTree::alter( /// Reinitialize primary key because primary key column types might have changed. data.setPrimaryKeyAndColumns(new_order_by_ast, new_primary_key_ast, new_columns); - data.setSkipIndexes(data.skip_indexes_ast); + data.setSkipIndexes(new_indexes_ast); for (auto & transaction : transactions) transaction->commit();