From 9637dad46230e234d1e98981b9f790f0245594ee Mon Sep 17 00:00:00 2001 From: Artem Zuikov Date: Thu, 25 Jun 2020 23:59:10 +0300 Subject: [PATCH] keep ArrayJoin optimisation --- src/Interpreters/ExpressionAnalyzer.cpp | 18 ++++++++---------- src/Interpreters/ExpressionAnalyzer.h | 1 + src/Interpreters/SyntaxAnalyzer.cpp | 1 + src/Interpreters/SyntaxAnalyzer.h | 2 ++ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Interpreters/ExpressionAnalyzer.cpp b/src/Interpreters/ExpressionAnalyzer.cpp index 4fbcf252b36..ea8c084579a 100644 --- a/src/Interpreters/ExpressionAnalyzer.cpp +++ b/src/Interpreters/ExpressionAnalyzer.cpp @@ -504,11 +504,7 @@ void ExpressionAnalyzer::addJoinAction(ExpressionActionsPtr & actions, JoinPtr j bool SelectQueryExpressionAnalyzer::appendJoin(ExpressionActionsChain & chain, bool only_types) { - const ASTTablesInSelectQueryElement * ast_join = getSelectQuery()->join(); - if (!ast_join) - return false; - - JoinPtr table_join = makeTableJoin(*ast_join); + JoinPtr table_join = makeTableJoin(*syntax->ast_join); initChain(chain, sourceColumns()); ExpressionActionsChain::Step & step = chain.steps.back(); @@ -1101,12 +1097,14 @@ ExpressionAnalysisResult::ExpressionAnalysisResult( query_analyzer.appendArrayJoin(chain, only_types || !first_stage); - before_join = chain.getLastActions(true); - if (before_join) - chain.addStep(); - - if (query_analyzer.appendJoin(chain, only_types || !first_stage)) + if (query_analyzer.hasTableJoin()) { + before_join = chain.getLastActions(true); + if (before_join) + chain.addStep(); + + query_analyzer.appendJoin(chain, only_types || !first_stage); + join = chain.getLastActions(); if (!join) throw Exception("No expected JOIN", ErrorCodes::LOGICAL_ERROR); diff --git a/src/Interpreters/ExpressionAnalyzer.h b/src/Interpreters/ExpressionAnalyzer.h index 402a8f09599..ed4f9be2450 100644 --- a/src/Interpreters/ExpressionAnalyzer.h +++ b/src/Interpreters/ExpressionAnalyzer.h @@ -250,6 +250,7 @@ public: /// Does the expression have aggregate functions or a GROUP BY or HAVING section. bool hasAggregation() const { return has_aggregation; } bool hasGlobalSubqueries() { return has_global_subqueries; } + bool hasTableJoin() const { return syntax->ast_join; } const NamesAndTypesList & aggregationKeys() const { return aggregation_keys; } const AggregateDescriptions & aggregates() const { return aggregate_descriptions; } diff --git a/src/Interpreters/SyntaxAnalyzer.cpp b/src/Interpreters/SyntaxAnalyzer.cpp index b8d6d171401..e5483e4fdcc 100644 --- a/src/Interpreters/SyntaxAnalyzer.cpp +++ b/src/Interpreters/SyntaxAnalyzer.cpp @@ -993,6 +993,7 @@ SyntaxAnalyzerResultPtr SyntaxAnalyzer::analyzeSelect( result.aggregates = getAggregates(query, *select_query); result.collectUsedColumns(query, true); + result.ast_join = select_query->join(); if (result.optimize_trivial_count) result.optimize_trivial_count = settings.optimize_trivial_count_query && diff --git a/src/Interpreters/SyntaxAnalyzer.h b/src/Interpreters/SyntaxAnalyzer.h index dd3c49a0f1a..2c86ce11c42 100644 --- a/src/Interpreters/SyntaxAnalyzer.h +++ b/src/Interpreters/SyntaxAnalyzer.h @@ -11,6 +11,7 @@ namespace DB { class ASTFunction; +struct ASTTablesInSelectQueryElement; class TableJoin; class Context; struct Settings; @@ -24,6 +25,7 @@ struct SyntaxAnalyzerResult ConstStoragePtr storage; StorageMetadataPtr metadata_snapshot; std::shared_ptr analyzed_join; + const ASTTablesInSelectQueryElement * ast_join = nullptr; NamesAndTypesList source_columns; NameSet source_columns_set; /// Set of names of source_columns.