From efc93ad1b7279491b797f0ebd07eee6abd1b1874 Mon Sep 17 00:00:00 2001 From: yanweiqi <592838129@qq.com> Date: Thu, 12 May 2022 15:44:35 +0800 Subject: [PATCH] Interpreter: Remove delete query (#4594) ref pingcap/tiflash#4860 --- .../Interpreters/InterpreterDeleteQuery.cpp | 128 ---------------- .../src/Interpreters/InterpreterDeleteQuery.h | 42 ------ dbms/src/Interpreters/InterpreterFactory.cpp | 7 - dbms/src/Parsers/ASTDeleteQuery.cpp | 48 ------ dbms/src/Parsers/ASTDeleteQuery.h | 58 -------- dbms/src/Parsers/ParserDeleteQuery.cpp | 140 ------------------ dbms/src/Parsers/ParserDeleteQuery.h | 35 ----- dbms/src/Parsers/ParserQuery.cpp | 3 - 8 files changed, 461 deletions(-) delete mode 100644 dbms/src/Interpreters/InterpreterDeleteQuery.cpp delete mode 100644 dbms/src/Interpreters/InterpreterDeleteQuery.h delete mode 100644 dbms/src/Parsers/ASTDeleteQuery.cpp delete mode 100644 dbms/src/Parsers/ASTDeleteQuery.h delete mode 100644 dbms/src/Parsers/ParserDeleteQuery.cpp delete mode 100644 dbms/src/Parsers/ParserDeleteQuery.h diff --git a/dbms/src/Interpreters/InterpreterDeleteQuery.cpp b/dbms/src/Interpreters/InterpreterDeleteQuery.cpp deleted file mode 100644 index 5cd909de050..00000000000 --- a/dbms/src/Interpreters/InterpreterDeleteQuery.cpp +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright 2022 PingCAP, Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace ProfileEvents -{ -extern const Event DeleteQuery; -} - -namespace DB -{ -namespace ErrorCodes -{ -extern const int NO_SUCH_COLUMN_IN_TABLE; -extern const int READONLY; -extern const int ILLEGAL_COLUMN; -extern const int LOGICAL_ERROR; -} // namespace ErrorCodes - -InterpreterDeleteQuery::InterpreterDeleteQuery(const ASTPtr & query_ptr_, const Context & context_, bool allow_materialized_) - : query_ptr(query_ptr_) - , context(context_) - , allow_materialized(allow_materialized_) -{ - ProfileEvents::increment(ProfileEvents::DeleteQuery); -} - -BlockIO InterpreterDeleteQuery::execute() -{ - ASTDeleteQuery & query = typeid_cast(*query_ptr); - checkAccess(query); - - StoragePtr table = context.getTable(query.database, query.table); - if (!table->supportsModification()) - throw Exception("Table engine " + table->getName() + " does not support Delete."); - - auto table_lock = table->lockStructureForShare(context.getCurrentQueryId()); - - NamesAndTypesList required_columns = table->getColumns().getAllPhysical(); - - BlockOutputStreamPtr out; - - out = std::make_shared(query.database, query.table, table, context, query_ptr, false); - - out = std::make_shared( - out, - table->getSampleBlockNoHidden(), - required_columns, - table->getColumns().defaults, - context); - - out = std::make_shared( - out, - context.getSettingsRef().min_insert_block_size_rows, - context.getSettingsRef().min_insert_block_size_bytes); - - auto out_wrapper = std::make_shared(out); - out_wrapper->setProcessListElement(context.getProcessListElement()); - out = std::move(out_wrapper); - - BlockIO res; - res.out = std::move(out); - - if (!query.where) - throw Exception("Delete query must have WHERE.", ErrorCodes::LOGICAL_ERROR); - - InterpreterSelectQuery interpreter_select(query.select, context); - - res.in = interpreter_select.execute().in; - - res.in = std::make_shared(context, res.in, res.out->getHeader(), ConvertingBlockInputStream::MatchColumnsMode::Position); - res.in = std::make_shared(res.in, res.out); - - res.out = nullptr; - - if (!allow_materialized) - { - Block in_header = res.in->getHeader(); - for (const auto & name_type : table->getColumns().materialized) - if (in_header.has(name_type.name)) - throw Exception("Cannot insert column " + name_type.name + ", because it is MATERIALIZED column.", ErrorCodes::ILLEGAL_COLUMN); - } - - return res; -} - -void InterpreterDeleteQuery::checkAccess(const ASTDeleteQuery & query) -{ - const Settings & settings = context.getSettingsRef(); - auto readonly = settings.readonly; - - if (!readonly || (query.database.empty() && context.tryGetExternalTable(query.table) && readonly >= 2)) - { - return; - } - - throw Exception("Cannot insert into table in readonly mode", ErrorCodes::READONLY); -} - -} // namespace DB diff --git a/dbms/src/Interpreters/InterpreterDeleteQuery.h b/dbms/src/Interpreters/InterpreterDeleteQuery.h deleted file mode 100644 index e912f2d6afa..00000000000 --- a/dbms/src/Interpreters/InterpreterDeleteQuery.h +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2022 PingCAP, Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include -#include - -namespace DB -{ -/** Interprets the DELETE query. - */ -class InterpreterDeleteQuery : public IInterpreter -{ -public: - InterpreterDeleteQuery(const ASTPtr & query_ptr_, const Context & context_, bool allow_materialized_ = false); - - BlockIO execute() override; - -private: - void checkAccess(const ASTDeleteQuery & query); - - ASTPtr query_ptr; - const Context & context; - bool allow_materialized; -}; - -} // namespace DB diff --git a/dbms/src/Interpreters/InterpreterFactory.cpp b/dbms/src/Interpreters/InterpreterFactory.cpp index bb0d6e31467..0e6e6fbdae3 100644 --- a/dbms/src/Interpreters/InterpreterFactory.cpp +++ b/dbms/src/Interpreters/InterpreterFactory.cpp @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -34,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -134,11 +132,6 @@ std::unique_ptr InterpreterFactory::get(ASTPtr & query, Context & throwIfReadOnly(context); return std::make_unique(query, context); } - else if (typeid_cast(query.get())) - { - bool allow_materialized = static_cast(context.getSettingsRef().insert_allow_materialized_columns); - return std::make_unique(query, context, allow_materialized); - } else if (typeid_cast(query.get())) { return std::make_unique(query, context); diff --git a/dbms/src/Parsers/ASTDeleteQuery.cpp b/dbms/src/Parsers/ASTDeleteQuery.cpp deleted file mode 100644 index 9b3e2c0ff1b..00000000000 --- a/dbms/src/Parsers/ASTDeleteQuery.cpp +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2022 PingCAP, Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - - -namespace DB -{ - -void ASTDeleteQuery::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const -{ - frame.need_parens = false; - std::string indent_str = settings.one_line ? "" : std::string(4 * frame.indent, ' '); - - settings.ostr - << (settings.hilite ? hilite_keyword : "") - << "DELETE FROM " - << (settings.hilite ? hilite_none : "") - << (!database.empty() ? backQuoteIfNeed(database) + "." : "") - << backQuoteIfNeed(table); - - if (partition_expression_list) - { - settings.ostr << (settings.hilite ? hilite_keyword : "") << settings.nl_or_ws << - indent_str << "PARTITION " << (settings.hilite ? hilite_none : ""); - partition_expression_list->formatImpl(settings, state, frame); - } - - if (where) - { - settings.ostr << " WHERE "; - where->formatImpl(settings, state, frame); - } -} - -} diff --git a/dbms/src/Parsers/ASTDeleteQuery.h b/dbms/src/Parsers/ASTDeleteQuery.h deleted file mode 100644 index cfd1cf227be..00000000000 --- a/dbms/src/Parsers/ASTDeleteQuery.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2022 PingCAP, Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - - -namespace DB -{ - - -/** DELETE query - */ -class ASTDeleteQuery : public IAST -{ -public: - String database; - String table; - ASTPtr partition_expression_list; - ASTPtr where; - - // Just for execute. - ASTPtr select; - - /** Get the text that identifies this element. */ - String getID() const override { return "DeleteQuery_" + database + "_" + table; }; - - ASTPtr clone() const override - { - auto res = std::make_shared(*this); - res->children.clear(); - - if (where) - { - res->where = where->clone(); - res->children.push_back(res->where); - } - - return res; - } - -protected: - void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override; -}; - -} diff --git a/dbms/src/Parsers/ParserDeleteQuery.cpp b/dbms/src/Parsers/ParserDeleteQuery.cpp deleted file mode 100644 index 03232536d73..00000000000 --- a/dbms/src/Parsers/ParserDeleteQuery.cpp +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright 2022 PingCAP, Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - - -namespace DB -{ - -namespace ErrorCodes -{ - extern const int SYNTAX_ERROR; -} - - -bool ParserDeleteQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) -{ - ParserKeyword s_delete_from("DELETE FROM"); - ParserToken s_dot(TokenType::Dot); - ParserKeyword s_partition("PARTITION"); - ParserKeyword s_where("WHERE"); - ParserIdentifier name_p; - - ASTPtr database; - ASTPtr table; - ASTPtr where; - - ParserExpressionWithOptionalAlias exp_elem(false); - - if (!s_delete_from.ignore(pos, expected)) - return false; - - if (!name_p.parse(pos, table, expected)) - return false; - - if (s_dot.ignore(pos, expected)) - { - database = table; - if (!name_p.parse(pos, table, expected)) - return false; - } - - std::shared_ptr query = std::make_shared(); - node = query; - - /// PARTITION p or PARTITION (p1, p2, ...) - if (s_partition.ignore(pos, expected)) - { - if (!ParserPartition().parse(pos, query->partition_expression_list, expected)) - return false; - } - - if (!s_where.ignore(pos, expected)) - return false; - - if (!exp_elem.parse(pos, where, expected)) - return false; - - if (database) - query->database = typeid_cast(*database).name; - - query->table = typeid_cast(*table).name; - - // TODO: Support syntax without 'where' - - if (where) - { - query->where = where; - query->children.push_back(where); - - std::shared_ptr select_query = std::make_shared(); - - std::shared_ptr asterisk = std::make_shared(); - std::shared_ptr table_columns = std::make_shared(); - table_columns->children.push_back(asterisk); - - select_query->select_expression_list = table_columns; - select_query->children.push_back(table_columns); - - auto table_expr = std::make_shared(); - table_expr->database_and_table_name = - std::make_shared( - query->database.size() ? query->database + '.' + query->table : query->table, - ASTIdentifier::Table); - if(!query->database.empty()) - { - table_expr->database_and_table_name->children.emplace_back(std::make_shared(query->database, ASTIdentifier::Database)); - table_expr->database_and_table_name->children.emplace_back(std::make_shared(query->table, ASTIdentifier::Table)); - } - - auto table_element = std::make_shared(); - table_element->table_expression = table_expr; - table_element->children.emplace_back(table_expr); - - auto table_list = std::make_shared(); - table_list->children.emplace_back(table_element); - - select_query->tables = table_list; - select_query->children.push_back(table_list); - - select_query->where_expression = query->where; - select_query->children.push_back(query->where); - select_query->partition_expression_list = query->partition_expression_list; - - query->select = select_query; - query->children.push_back(select_query); - } - - return true; -} - - -} diff --git a/dbms/src/Parsers/ParserDeleteQuery.h b/dbms/src/Parsers/ParserDeleteQuery.h deleted file mode 100644 index d930b3b652d..00000000000 --- a/dbms/src/Parsers/ParserDeleteQuery.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2022 PingCAP, Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - - -namespace DB -{ - - -/** Cases: - * - * DELETE FROM [db.]table WHERE ... - */ -class ParserDeleteQuery : public IParserBase -{ -protected: - const char * getName() const { return "DELETE query"; } - bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); -}; - -} diff --git a/dbms/src/Parsers/ParserQuery.cpp b/dbms/src/Parsers/ParserQuery.cpp index 7325a3850b3..8394cf0c722 100644 --- a/dbms/src/Parsers/ParserQuery.cpp +++ b/dbms/src/Parsers/ParserQuery.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -34,7 +33,6 @@ bool ParserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) ParserInsertQuery insert_p(end); ParserUseQuery use_p; ParserSetQuery set_p; - ParserDeleteQuery delete_p; ParserDBGInvokeQuery dbginvoke_p; ParserSystemQuery system_p; ParserManageQuery manage_p; @@ -43,7 +41,6 @@ bool ParserQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) || insert_p.parse(pos, node, expected) || use_p.parse(pos, node, expected) || set_p.parse(pos, node, expected) - || delete_p.parse(pos, node, expected) || dbginvoke_p.parse(pos, node, expected) || system_p.parse(pos, node, expected) || manage_p.parse(pos, node, expected);