From 135cac6d7753b715d15929582bf8e3e562890a21 Mon Sep 17 00:00:00 2001 From: Alexey Arno Date: Sat, 11 Jul 2015 18:16:59 +0300 Subject: [PATCH] dbms: Server: Performance improvement. [#METR-16778] --- dbms/include/DB/Parsers/ASTSelectQuery.h | 5 ++++- .../Interpreters/InterpreterSelectQuery.cpp | 5 +---- dbms/src/Parsers/ASTSelectQuery.cpp | 22 ++++++++++++++----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/dbms/include/DB/Parsers/ASTSelectQuery.h b/dbms/include/DB/Parsers/ASTSelectQuery.h index 244a53b45e2..f6edf7ebfb9 100644 --- a/dbms/include/DB/Parsers/ASTSelectQuery.h +++ b/dbms/include/DB/Parsers/ASTSelectQuery.h @@ -36,11 +36,14 @@ public: ASTPtr clone() const override; + /// Получить глубокую копию дерева первого запроса SELECT. + ASTPtr cloneFirstSelect() const; + /// Возвращает указатель на формат из последнего SELECT'а цепочки UNION ALL. const IAST * getFormat() const override; private: - ASTPtr cloneImpl() const; + ASTPtr cloneImpl(bool traverse_union_all) const; public: bool distinct = false; diff --git a/dbms/src/Interpreters/InterpreterSelectQuery.cpp b/dbms/src/Interpreters/InterpreterSelectQuery.cpp index f781bc70510..cf5180f9e08 100644 --- a/dbms/src/Interpreters/InterpreterSelectQuery.cpp +++ b/dbms/src/Interpreters/InterpreterSelectQuery.cpp @@ -720,10 +720,7 @@ QueryProcessingStage::Enum InterpreterSelectQuery::executeFetchColumns(BlockInpu if (storage->isRemote()) { /// В случае удаленного запроса отправляем только SELECT, который выполнится. - actual_query_ptr = query_ptr->clone(); - auto actual_select_query = static_cast(&*actual_query_ptr); - actual_select_query->prev_union_all = nullptr; - actual_select_query->next_union_all = nullptr; + actual_query_ptr = query.cloneFirstSelect(); } else actual_query_ptr = query_ptr; diff --git a/dbms/src/Parsers/ASTSelectQuery.cpp b/dbms/src/Parsers/ASTSelectQuery.cpp index a63202eeb72..36fa67a2cec 100644 --- a/dbms/src/Parsers/ASTSelectQuery.cpp +++ b/dbms/src/Parsers/ASTSelectQuery.cpp @@ -134,7 +134,7 @@ void ASTSelectQuery::rewriteSelectExpressionList(const Names & column_names) ASTPtr ASTSelectQuery::clone() const { - ASTPtr ptr = cloneImpl(); + ASTPtr ptr = cloneImpl(true); /// Установить указатели на предыдущие запросы SELECT. ASTPtr current = ptr; @@ -150,7 +150,14 @@ ASTPtr ASTSelectQuery::clone() const return ptr; } -ASTPtr ASTSelectQuery::cloneImpl() const +ASTPtr ASTSelectQuery::cloneFirstSelect() const +{ + ASTPtr res = cloneImpl(false); + static_cast(&*res)->prev_union_all = nullptr; + return res; +} + +ASTPtr ASTSelectQuery::cloneImpl(bool traverse_union_all) const { ASTSelectQuery * res = new ASTSelectQuery(*this); ASTPtr ptr{res}; @@ -187,11 +194,16 @@ ASTPtr ASTSelectQuery::cloneImpl() const #undef CLONE - if (next_union_all) + if (traverse_union_all) { - res->next_union_all = static_cast(&*next_union_all)->cloneImpl(); - res->children.push_back(res->next_union_all); + if (next_union_all) + { + res->next_union_all = static_cast(&*next_union_all)->cloneImpl(true); + res->children.push_back(res->next_union_all); + } } + else + res->next_union_all = nullptr; return ptr; }