From 815380b8b223fff49edf91ef78aab5fa176f3be3 Mon Sep 17 00:00:00 2001 From: CurtizJ Date: Wed, 10 Oct 2018 20:07:21 +0300 Subject: [PATCH] fix unnecessarily preparations for join at initial server --- dbms/src/Interpreters/ExpressionAnalyzer.cpp | 6 ++++++ dbms/src/Interpreters/ExpressionAnalyzer.h | 3 +++ .../Interpreters/InterpreterSelectQuery.cpp | 20 ++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index e39790e3eb7..2dc4b04fe13 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -574,14 +574,20 @@ void ExpressionAnalyzer::initGlobalSubqueries(ASTPtr & ast) { /// For GLOBAL IN. if (do_global && (func->name == "globalIn" || func->name == "globalNotIn")) + { addExternalStorage(func->arguments->children.at(1)); + has_global_subqueries = true; + } } else if (ASTTablesInSelectQueryElement * table_elem = typeid_cast(ast.get())) { /// For GLOBAL JOIN. if (do_global && table_elem->table_join && static_cast(*table_elem->table_join).locality == ASTTableJoin::Locality::Global) + { addExternalStorage(table_elem->table_expression); + has_global_subqueries = true; + } } } diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.h b/dbms/src/Interpreters/ExpressionAnalyzer.h index a3d1d0f10e9..f49d306e26f 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.h +++ b/dbms/src/Interpreters/ExpressionAnalyzer.h @@ -186,6 +186,8 @@ public: bool isRewriteSubqueriesPredicate() { return rewrite_subqueries; } + bool hasGlobalSubqueries() { return has_global_subqueries; } + private: ASTPtr query; ASTSelectQuery * select_query; @@ -216,6 +218,7 @@ private: /// Do I need to prepare for execution global subqueries when analyzing the query. bool do_global; + bool has_global_subqueries = false; SubqueriesForSets subqueries_for_sets; diff --git a/dbms/src/Interpreters/InterpreterSelectQuery.cpp b/dbms/src/Interpreters/InterpreterSelectQuery.cpp index 50a2d623b21..0925ca13fdf 100644 --- a/dbms/src/Interpreters/InterpreterSelectQuery.cpp +++ b/dbms/src/Interpreters/InterpreterSelectQuery.cpp @@ -549,6 +549,10 @@ void InterpreterSelectQuery::executeImpl(Pipeline & pipeline, const BlockInputSt if (query.limit_length) executePreLimit(pipeline); } + + // If there is no global subqueries, we can run subqueries only when recieve them on server. + if (!query_analyzer->hasGlobalSubqueries() && !expressions.subqueries_for_sets.empty()) + executeSubqueriesInSetsAndJoins(pipeline, expressions.subqueries_for_sets); } if (expressions.second_stage) @@ -654,7 +658,21 @@ void InterpreterSelectQuery::executeImpl(Pipeline & pipeline, const BlockInputSt } } - if (!expressions.subqueries_for_sets.empty()) + LOG_DEBUG(log, "is first_stage: " << expressions.first_stage); + LOG_DEBUG(log, "is second_stage: " << expressions.second_stage); + LOG_DEBUG(log, "has_join: " << expressions.has_join); + LOG_DEBUG(log, "subqueries size: " << expressions.subqueries_for_sets.size()); + LOG_DEBUG(log, "has global subqueries: " << query_analyzer->hasGlobalSubqueries()); + for (auto s : expressions.subqueries_for_sets) + { + LOG_DEBUG(log, "set: " << !!s.second.set); + LOG_DEBUG(log, "join: " << !!s.second.join); + LOG_DEBUG(log, "table: " << !!s.second.table); + for (auto name : s.second.joined_block_aliases) + LOG_DEBUG(log, "name with alias: " << name.first << "; " << name.second); + } + + if (query_analyzer->hasGlobalSubqueries() && !expressions.subqueries_for_sets.empty()) executeSubqueriesInSetsAndJoins(pipeline, expressions.subqueries_for_sets); }