From 03c785931adbc60e647463673ee837183d0c9517 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Fri, 16 Jul 2021 17:27:38 +0300 Subject: [PATCH] fix pushdown of limit to reading stage --- src/Interpreters/InterpreterSelectQuery.cpp | 2 +- src/Processors/QueryPlan/ReadFromMergeTree.cpp | 4 ++-- src/Storages/MergeTree/MergeTreeSelectProcessor.cpp | 2 ++ src/Storages/MergeTree/MergeTreeSelectProcessor.h | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 14b977f6864..0750dd6e27c 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -1925,7 +1925,7 @@ void InterpreterSelectQuery::executeFetchColumns(QueryProcessingStage::Enum proc } /// If we don't have filtration, we can pushdown limit to reading stage for optimizations. - UInt64 limit = (query.where() && query.prewhere()) ? getLimitForSorting(query, context) : 0; + UInt64 limit = (query.where() || query.prewhere()) ? 0 : getLimitForSorting(query, context); if (query_info.projection) query_info.projection->input_order_info = query_info.projection->order_optimizer->getInputOrder(query_info.projection->desc->metadata, context, limit); diff --git a/src/Processors/QueryPlan/ReadFromMergeTree.cpp b/src/Processors/QueryPlan/ReadFromMergeTree.cpp index 612b3d5da3e..6abf37eb008 100644 --- a/src/Processors/QueryPlan/ReadFromMergeTree.cpp +++ b/src/Processors/QueryPlan/ReadFromMergeTree.cpp @@ -181,8 +181,8 @@ ProcessorPtr ReadFromMergeTree::createSource( { return std::make_shared( data, metadata_snapshot, part.data_part, max_block_size, preferred_block_size_bytes, - preferred_max_column_in_block_size_bytes, required_columns, part.ranges, use_uncompressed_cache, - prewhere_info, actions_settings, true, reader_settings, virt_column_names, has_limit_below_one_block); + preferred_max_column_in_block_size_bytes, required_columns, part.ranges, use_uncompressed_cache, prewhere_info, + actions_settings, true, reader_settings, virt_column_names, part.part_index_in_query, has_limit_below_one_block); } Pipe ReadFromMergeTree::readInOrder( diff --git a/src/Storages/MergeTree/MergeTreeSelectProcessor.cpp b/src/Storages/MergeTree/MergeTreeSelectProcessor.cpp index 638e04820f3..440b47b12cf 100644 --- a/src/Storages/MergeTree/MergeTreeSelectProcessor.cpp +++ b/src/Storages/MergeTree/MergeTreeSelectProcessor.cpp @@ -22,6 +22,7 @@ MergeTreeSelectProcessor::MergeTreeSelectProcessor( bool check_columns, const MergeTreeReaderSettings & reader_settings_, const Names & virt_column_names_, + size_t part_index_in_query_, bool has_limit_below_one_block_) : MergeTreeBaseSelectProcessor{ metadata_snapshot_->getSampleBlockForColumns(required_columns_, storage_.getVirtuals(), storage_.getStorageID()), @@ -31,6 +32,7 @@ MergeTreeSelectProcessor::MergeTreeSelectProcessor( required_columns{std::move(required_columns_)}, data_part{owned_data_part_}, all_mark_ranges(std::move(mark_ranges_)), + part_index_in_query(part_index_in_query_), has_limit_below_one_block(has_limit_below_one_block_), total_rows(data_part->index_granularity.getRowsCountInRanges(all_mark_ranges)) { diff --git a/src/Storages/MergeTree/MergeTreeSelectProcessor.h b/src/Storages/MergeTree/MergeTreeSelectProcessor.h index 83f1a0dab94..621a3bd740c 100644 --- a/src/Storages/MergeTree/MergeTreeSelectProcessor.h +++ b/src/Storages/MergeTree/MergeTreeSelectProcessor.h @@ -31,6 +31,7 @@ public: bool check_columns, const MergeTreeReaderSettings & reader_settings, const Names & virt_column_names = {}, + size_t part_index_in_query_ = 0, bool has_limit_below_one_block_ = false); ~MergeTreeSelectProcessor() override;