From d573005b044abc43e51abe38217d71b97fddd0ab Mon Sep 17 00:00:00 2001 From: Artem Zuikov Date: Fri, 26 Jun 2020 02:05:47 +0300 Subject: [PATCH] place left join keys in before_join actions --- src/Interpreters/ExpressionActions.h | 7 +++ src/Interpreters/ExpressionAnalyzer.cpp | 62 ++++++++++--------------- src/Interpreters/ExpressionAnalyzer.h | 6 +-- 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/src/Interpreters/ExpressionActions.h b/src/Interpreters/ExpressionActions.h index 9ec0047cc35..06b847a6c93 100644 --- a/src/Interpreters/ExpressionActions.h +++ b/src/Interpreters/ExpressionActions.h @@ -340,6 +340,13 @@ struct ExpressionActionsChain return steps.back(); } + Step & lastStep(const NamesAndTypesList & columns) + { + if (steps.empty()) + steps.emplace_back(std::make_shared(columns, context)); + return steps.back(); + } + std::string dumpChain(); }; diff --git a/src/Interpreters/ExpressionAnalyzer.cpp b/src/Interpreters/ExpressionAnalyzer.cpp index ea8c084579a..44aa70b1697 100644 --- a/src/Interpreters/ExpressionAnalyzer.cpp +++ b/src/Interpreters/ExpressionAnalyzer.cpp @@ -453,14 +453,6 @@ const ASTSelectQuery * SelectQueryExpressionAnalyzer::getAggregatingQuery() cons return getSelectQuery(); } -void ExpressionAnalyzer::initChain(ExpressionActionsChain & chain, const NamesAndTypesList & columns) const -{ - if (chain.steps.empty()) - { - chain.steps.emplace_back(std::make_shared(columns, context)); - } -} - /// "Big" ARRAY JOIN. void ExpressionAnalyzer::addMultipleArrayJoinAction(ExpressionActionsPtr & actions, bool array_join_is_left) const { @@ -487,8 +479,7 @@ bool SelectQueryExpressionAnalyzer::appendArrayJoin(ExpressionActionsChain & cha if (!array_join_expression_list) return false; - initChain(chain, sourceColumns()); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(sourceColumns()); getRootActions(array_join_expression_list, only_types, step.actions); @@ -502,14 +493,20 @@ void ExpressionAnalyzer::addJoinAction(ExpressionActionsPtr & actions, JoinPtr j actions->add(ExpressionAction::ordinaryJoin(syntax->analyzed_join, join)); } -bool SelectQueryExpressionAnalyzer::appendJoin(ExpressionActionsChain & chain, bool only_types) +bool SelectQueryExpressionAnalyzer::appendJoinLeftKeys(ExpressionActionsChain & chain, bool only_types) +{ + ExpressionActionsChain::Step & step = chain.lastStep(sourceColumns()); + + getRootActions(analyzedJoin().leftKeysList(), only_types, step.actions); + return true; +} + +bool SelectQueryExpressionAnalyzer::appendJoin(ExpressionActionsChain & chain) { JoinPtr table_join = makeTableJoin(*syntax->ast_join); - initChain(chain, sourceColumns()); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(sourceColumns()); - getRootActions(analyzedJoin().leftKeysList(), only_types, step.actions); addJoinAction(step.actions, table_join); return true; } @@ -633,8 +630,7 @@ bool SelectQueryExpressionAnalyzer::appendPrewhere( if (!select_query->prewhere()) return false; - initChain(chain, sourceColumns()); - auto & step = chain.getLastStep(); + auto & step = chain.lastStep(sourceColumns()); getRootActions(select_query->prewhere(), only_types, step.actions); String prewhere_column_name = select_query->prewhere()->getColumnName(); step.required_output.push_back(prewhere_column_name); @@ -701,8 +697,7 @@ bool SelectQueryExpressionAnalyzer::appendPrewhere( void SelectQueryExpressionAnalyzer::appendPreliminaryFilter(ExpressionActionsChain & chain, ExpressionActionsPtr actions, String column_name) { - initChain(chain, sourceColumns()); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(sourceColumns()); // FIXME: assert(filter_info); step.actions = std::move(actions); @@ -719,8 +714,7 @@ bool SelectQueryExpressionAnalyzer::appendWhere(ExpressionActionsChain & chain, if (!select_query->where()) return false; - initChain(chain, sourceColumns()); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(sourceColumns()); step.required_output.push_back(select_query->where()->getColumnName()); step.can_remove_required_output = {true}; @@ -738,8 +732,7 @@ bool SelectQueryExpressionAnalyzer::appendGroupBy(ExpressionActionsChain & chain if (!select_query->groupBy()) return false; - initChain(chain, sourceColumns()); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(sourceColumns()); ASTs asts = select_query->groupBy()->children; for (const auto & ast : asts) @@ -765,8 +758,7 @@ void SelectQueryExpressionAnalyzer::appendAggregateFunctionsArguments(Expression { const auto * select_query = getAggregatingQuery(); - initChain(chain, sourceColumns()); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(sourceColumns()); for (const auto & desc : aggregate_descriptions) for (const auto & name : desc.argument_names) @@ -797,8 +789,7 @@ bool SelectQueryExpressionAnalyzer::appendHaving(ExpressionActionsChain & chain, if (!select_query->having()) return false; - initChain(chain, aggregated_columns); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(aggregated_columns); step.required_output.push_back(select_query->having()->getColumnName()); getRootActions(select_query->having(), only_types, step.actions); @@ -810,8 +801,7 @@ void SelectQueryExpressionAnalyzer::appendSelect(ExpressionActionsChain & chain, { const auto * select_query = getSelectQuery(); - initChain(chain, aggregated_columns); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(aggregated_columns); getRootActions(select_query->select(), only_types, step.actions); @@ -827,8 +817,7 @@ bool SelectQueryExpressionAnalyzer::appendOrderBy(ExpressionActionsChain & chain if (!select_query->orderBy()) return false; - initChain(chain, aggregated_columns); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(aggregated_columns); getRootActions(select_query->orderBy(), only_types, step.actions); @@ -860,8 +849,7 @@ bool SelectQueryExpressionAnalyzer::appendLimitBy(ExpressionActionsChain & chain if (!select_query->limitBy()) return false; - initChain(chain, aggregated_columns); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(aggregated_columns); getRootActions(select_query->limitBy(), only_types, step.actions); @@ -886,8 +874,7 @@ void SelectQueryExpressionAnalyzer::appendProjectResult(ExpressionActionsChain & { const auto * select_query = getSelectQuery(); - initChain(chain, aggregated_columns); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(aggregated_columns); NamesWithAliases result_columns; @@ -935,8 +922,7 @@ void SelectQueryExpressionAnalyzer::appendProjectResult(ExpressionActionsChain & void ExpressionAnalyzer::appendExpression(ExpressionActionsChain & chain, const ASTPtr & expr, bool only_types) { - initChain(chain, sourceColumns()); - ExpressionActionsChain::Step & step = chain.steps.back(); + ExpressionActionsChain::Step & step = chain.lastStep(sourceColumns()); getRootActions(expr, only_types, step.actions); step.required_output.push_back(expr->getColumnName()); } @@ -1099,11 +1085,13 @@ ExpressionAnalysisResult::ExpressionAnalysisResult( if (query_analyzer.hasTableJoin()) { + query_analyzer.appendJoinLeftKeys(chain, only_types || !first_stage); + before_join = chain.getLastActions(true); if (before_join) chain.addStep(); - query_analyzer.appendJoin(chain, only_types || !first_stage); + query_analyzer.appendJoin(chain); join = chain.getLastActions(); if (!join) diff --git a/src/Interpreters/ExpressionAnalyzer.h b/src/Interpreters/ExpressionAnalyzer.h index ed4f9be2450..bd099693a91 100644 --- a/src/Interpreters/ExpressionAnalyzer.h +++ b/src/Interpreters/ExpressionAnalyzer.h @@ -153,9 +153,6 @@ protected: void analyzeAggregation(); bool makeAggregateDescriptions(ExpressionActionsPtr & actions); - /// columns - the columns that are present before the transformations begin. - void initChain(ExpressionActionsChain & chain, const NamesAndTypesList & columns) const; - const ASTSelectQuery * getSelectQuery() const; bool isRemoteStorage() const; @@ -309,7 +306,8 @@ private: /// Before aggregation: bool appendArrayJoin(ExpressionActionsChain & chain, bool only_types); - bool appendJoin(ExpressionActionsChain & chain, bool only_types); + bool appendJoinLeftKeys(ExpressionActionsChain & chain, bool only_types); + bool appendJoin(ExpressionActionsChain & chain); /// Add preliminary rows filtration. Actions are created in other expression analyzer to prevent any possible alias injection. void appendPreliminaryFilter(ExpressionActionsChain & chain, ExpressionActionsPtr actions, String column_name); /// remove_filter is set in ExpressionActionsChain::finalize();