From e8e26463bf42327f4ce67041846ad9d98a1ea05a Mon Sep 17 00:00:00 2001 From: vdimir Date: Mon, 28 Jun 2021 12:53:54 +0300 Subject: [PATCH] Change signature of ASTSelectQuery::arrayJoinExpressionList --- src/Interpreters/ArrayJoinedColumnsVisitor.h | 2 +- src/Interpreters/ExpressionAnalyzer.cpp | 3 +-- src/Interpreters/InterpreterSelectQuery.cpp | 8 +++----- src/Interpreters/PredicateExpressionsOptimizer.cpp | 2 +- src/Interpreters/TreeRewriter.cpp | 6 +++--- src/Parsers/ASTSelectQuery.cpp | 14 +++----------- src/Parsers/ASTSelectQuery.h | 4 ++-- src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp | 2 +- 8 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/Interpreters/ArrayJoinedColumnsVisitor.h b/src/Interpreters/ArrayJoinedColumnsVisitor.h index 94f6bdaf138..b64a170cb00 100644 --- a/src/Interpreters/ArrayJoinedColumnsVisitor.h +++ b/src/Interpreters/ArrayJoinedColumnsVisitor.h @@ -60,7 +60,7 @@ public: private: static void visit(const ASTSelectQuery & node, ASTPtr &, Data & data) { - ASTPtr array_join_expression_list = node.arrayJoinExpressionList(); + auto [array_join_expression_list, _] = node.arrayJoinExpressionList(); if (!array_join_expression_list) throw Exception("Logical error: no ARRAY JOIN", ErrorCodes::LOGICAL_ERROR); diff --git a/src/Interpreters/ExpressionAnalyzer.cpp b/src/Interpreters/ExpressionAnalyzer.cpp index ce0eaacb339..028362e392b 100644 --- a/src/Interpreters/ExpressionAnalyzer.cpp +++ b/src/Interpreters/ExpressionAnalyzer.cpp @@ -803,8 +803,7 @@ ArrayJoinActionPtr SelectQueryExpressionAnalyzer::appendArrayJoin(ExpressionActi { const auto * select_query = getSelectQuery(); - bool is_array_join_left; - ASTPtr array_join_expression_list = select_query->arrayJoinExpressionList(is_array_join_left); + auto [array_join_expression_list, is_array_join_left] = select_query->arrayJoinExpressionList(); if (!array_join_expression_list) return nullptr; diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index d4d5affc798..5fe9948f857 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -854,7 +854,7 @@ static std::pair getLimitLengthAndOffset(const ASTSelectQuery & static UInt64 getLimitForSorting(const ASTSelectQuery & query, ContextPtr context) { /// Partial sort can be done if there is LIMIT but no DISTINCT or LIMIT BY, neither ARRAY JOIN. - if (!query.distinct && !query.limitBy() && !query.limit_with_ties && !query.arrayJoinExpressionList() && query.limitLength()) + if (!query.distinct && !query.limitBy() && !query.limit_with_ties && !query.arrayJoinExpressionList().first && query.limitLength()) { auto [limit_length, limit_offset] = getLimitLengthAndOffset(query, context); if (limit_length > std::numeric_limits::max() - limit_offset) @@ -1352,17 +1352,15 @@ void InterpreterSelectQuery::executeImpl(QueryPlan & query_plan, const BlockInpu bool apply_prelimit = apply_limit && query.limitLength() && !query.limit_with_ties && !hasWithTotalsInAnySubqueryInFromClause(query) && - !query.arrayJoinExpressionList() && + !query.arrayJoinExpressionList().first && !query.distinct && !expressions.hasLimitBy() && !settings.extremes && !has_withfill; bool apply_offset = options.to_stage != QueryProcessingStage::WithMergeableStateAfterAggregationAndLimit; - bool limit_applied = false; if (apply_prelimit) { executePreLimit(query_plan, /* do_not_skip_offset= */!apply_offset); - limit_applied = true; } /** If there was more than one stream, @@ -1384,7 +1382,6 @@ void InterpreterSelectQuery::executeImpl(QueryPlan & query_plan, const BlockInpu if (query.limit_with_ties && apply_offset) { executeLimit(query_plan); - limit_applied = true; } /// Projection not be done on the shards, since then initiator will not find column in blocks. @@ -1398,6 +1395,7 @@ void InterpreterSelectQuery::executeImpl(QueryPlan & query_plan, const BlockInpu /// Extremes are calculated before LIMIT, but after LIMIT BY. This is Ok. executeExtremes(query_plan); + bool limit_applied = apply_prelimit || (query.limit_with_ties && apply_offset); /// Limit is no longer needed if there is prelimit. /// /// NOTE: that LIMIT cannot be applied if OFFSET should not be applied, diff --git a/src/Interpreters/PredicateExpressionsOptimizer.cpp b/src/Interpreters/PredicateExpressionsOptimizer.cpp index f2e55441fb6..adc164e0596 100644 --- a/src/Interpreters/PredicateExpressionsOptimizer.cpp +++ b/src/Interpreters/PredicateExpressionsOptimizer.cpp @@ -39,7 +39,7 @@ bool PredicateExpressionsOptimizer::optimize(ASTSelectQuery & select_query) if (!select_query.tables() || select_query.tables()->children.empty()) return false; - if ((!select_query.where() && !select_query.prewhere()) || select_query.arrayJoinExpressionList()) + if ((!select_query.where() && !select_query.prewhere()) || select_query.arrayJoinExpressionList().first) return false; const auto & tables_predicates = extractTablesPredicates(select_query.where(), select_query.prewhere()); diff --git a/src/Interpreters/TreeRewriter.cpp b/src/Interpreters/TreeRewriter.cpp index 0b3d8e82706..073b38eae57 100644 --- a/src/Interpreters/TreeRewriter.cpp +++ b/src/Interpreters/TreeRewriter.cpp @@ -422,7 +422,7 @@ void executeScalarSubqueries(ASTPtr & query, ContextPtr context, size_t subquery void getArrayJoinedColumns(ASTPtr & query, TreeRewriterResult & result, const ASTSelectQuery * select_query, const NamesAndTypesList & source_columns, const NameSet & source_columns_set) { - if (!select_query->arrayJoinExpressionList()) + if (!select_query->arrayJoinExpressionList().first) return; ArrayJoinedColumnsVisitor::Data visitor_data{ @@ -433,10 +433,10 @@ void getArrayJoinedColumns(ASTPtr & query, TreeRewriterResult & result, const AS /// to get the correct number of rows. if (result.array_join_result_to_source.empty()) { - if (select_query->arrayJoinExpressionList()->children.empty()) + if (select_query->arrayJoinExpressionList().first->children.empty()) throw DB::Exception("ARRAY JOIN requires an argument", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); - ASTPtr expr = select_query->arrayJoinExpressionList()->children.at(0); + ASTPtr expr = select_query->arrayJoinExpressionList().first->children.at(0); String source_name = expr->getColumnName(); String result_name = expr->getAliasOrColumnName(); diff --git a/src/Parsers/ASTSelectQuery.cpp b/src/Parsers/ASTSelectQuery.cpp index 7699d380623..1c5a4310f1b 100644 --- a/src/Parsers/ASTSelectQuery.cpp +++ b/src/Parsers/ASTSelectQuery.cpp @@ -319,24 +319,16 @@ bool ASTSelectQuery::withFill() const } -ASTPtr ASTSelectQuery::arrayJoinExpressionList(bool & is_left) const +std::pair ASTSelectQuery::arrayJoinExpressionList() const { const ASTArrayJoin * array_join = getFirstArrayJoin(*this); if (!array_join) return {}; - is_left = (array_join->kind == ASTArrayJoin::Kind::Left); - return array_join->expression_list; + bool is_left = (array_join->kind == ASTArrayJoin::Kind::Left); + return {array_join->expression_list, is_left}; } - -ASTPtr ASTSelectQuery::arrayJoinExpressionList() const -{ - bool is_left; - return arrayJoinExpressionList(is_left); -} - - const ASTTablesInSelectQueryElement * ASTSelectQuery::join() const { return getFirstTableJoin(*this); diff --git a/src/Parsers/ASTSelectQuery.h b/src/Parsers/ASTSelectQuery.h index 2babc2f75c8..2b004e9e040 100644 --- a/src/Parsers/ASTSelectQuery.h +++ b/src/Parsers/ASTSelectQuery.h @@ -123,8 +123,8 @@ public: /// Compatibility with old parser of tables list. TODO remove ASTPtr sampleSize() const; ASTPtr sampleOffset() const; - ASTPtr arrayJoinExpressionList(bool & is_left) const; - ASTPtr arrayJoinExpressionList() const; + std::pair arrayJoinExpressionList() const; + const ASTTablesInSelectQueryElement * join() const; bool final() const; bool withFill() const; diff --git a/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp b/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp index 806c861cf00..1f33f3eb092 100644 --- a/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp +++ b/src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp @@ -373,7 +373,7 @@ bool MergeTreeWhereOptimizer::cannotBeMoved(const ASTPtr & ptr, bool is_final) c void MergeTreeWhereOptimizer::determineArrayJoinedNames(ASTSelectQuery & select) { - auto array_join_expression_list = select.arrayJoinExpressionList(); + auto [array_join_expression_list, _] = select.arrayJoinExpressionList(); /// much simplified code from ExpressionAnalyzer::getArrayJoinedColumns() if (!array_join_expression_list)