From e197697b22c84916173cd241bb4e11dd5576c7b4 Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Sun, 13 Jan 2019 22:31:48 +0300 Subject: [PATCH] fix --- dbms/src/Storages/AlterCommands.cpp | 38 +++++++++++++++++-- dbms/src/Storages/AlterCommands.h | 6 ++- dbms/src/Storages/MergeTree/MergeTreeData.cpp | 5 ++- dbms/src/Storages/StorageMergeTree.cpp | 6 ++- .../Storages/StorageReplicatedMergeTree.cpp | 6 ++- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/dbms/src/Storages/AlterCommands.cpp b/dbms/src/Storages/AlterCommands.cpp index 2cde9562c82..a32d37dcea3 100644 --- a/dbms/src/Storages/AlterCommands.cpp +++ b/dbms/src/Storages/AlterCommands.cpp @@ -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(*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); } diff --git a/dbms/src/Storages/AlterCommands.h b/dbms/src/Storages/AlterCommands.h index a465a8412ed..cd4bb622a95 100644 --- a/dbms/src/Storages/AlterCommands.h +++ b/dbms/src/Storages/AlterCommands.h @@ -76,7 +76,8 @@ struct AlterCommand static std::optional 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 { 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; diff --git a/dbms/src/Storages/MergeTree/MergeTreeData.cpp b/dbms/src/Storages/MergeTree/MergeTreeData.cpp index c2396e02988..3e51efa808d 100644 --- a/dbms/src/Storages/MergeTree/MergeTreeData.cpp +++ b/dbms/src/Storages/MergeTree/MergeTreeData.cpp @@ -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; diff --git a/dbms/src/Storages/StorageMergeTree.cpp b/dbms/src/Storages/StorageMergeTree.cpp index f71a64662a4..1fd0f422975 100644 --- a/dbms/src/Storages/StorageMergeTree.cpp +++ b/dbms/src/Storages/StorageMergeTree.cpp @@ -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) diff --git a/dbms/src/Storages/StorageReplicatedMergeTree.cpp b/dbms/src/Storages/StorageReplicatedMergeTree.cpp index 50b133482b2..732ad0405cc 100644 --- a/dbms/src/Storages/StorageReplicatedMergeTree.cpp +++ b/dbms/src/Storages/StorageReplicatedMergeTree.cpp @@ -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())