diff --git a/src/Processors/QueryPlan/Optimizations/distinctReadInOrder.cpp b/src/Processors/QueryPlan/Optimizations/distinctReadInOrder.cpp index 7887a54ea9c..6ec7ee98d08 100644 --- a/src/Processors/QueryPlan/Optimizations/distinctReadInOrder.cpp +++ b/src/Processors/QueryPlan/Optimizations/distinctReadInOrder.cpp @@ -79,11 +79,8 @@ size_t tryDistinctReadInOrder(QueryPlan::Node * parent_node, QueryPlan::Nodes &) return 0; /// update input order info in read_from_merge_tree step - const int direction = output_sort_desc.front().direction; /// for DISTINCT direction doesn't matter, so use current direction - /// example: SELECT DISTINCT a from t ORDER BY a DESC; - InputOrderInfoPtr order_info - = std::make_shared(SortDescription{}, number_of_sorted_distinct_columns, direction, pre_distinct->getLimitHint()); - read_from_merge_tree->setQueryInfoInputOrderInfo(order_info); + const int direction = 0; /// for DISTINCT direction doesn't matter, ReadFromMergeTree will choose proper one + read_from_merge_tree->requestReadingInOrder(number_of_sorted_distinct_columns, direction, pre_distinct->getLimitHint()); /// update data stream's sorting properties for found transforms const DataStream * input_stream = &read_from_merge_tree->getOutputStream(); diff --git a/src/Processors/QueryPlan/Optimizations/reuseStorageOrderingForWindowFunctions.cpp b/src/Processors/QueryPlan/Optimizations/reuseStorageOrderingForWindowFunctions.cpp index 03b7d476f17..a8431d38a78 100644 --- a/src/Processors/QueryPlan/Optimizations/reuseStorageOrderingForWindowFunctions.cpp +++ b/src/Processors/QueryPlan/Optimizations/reuseStorageOrderingForWindowFunctions.cpp @@ -101,7 +101,7 @@ size_t tryReuseStorageOrderingForWindowFunctions(QueryPlan::Node * parent_node, if (order_info) { - read_from_merge_tree->setQueryInfoInputOrderInfo(order_info); + read_from_merge_tree->requestReadingInOrder(order_info->used_prefix_of_sorting_key_size, order_info->direction, order_info->limit); sorting->convertToFinishSorting(order_info->sort_description_for_merging); } diff --git a/src/Processors/QueryPlan/ReadFromMergeTree.cpp b/src/Processors/QueryPlan/ReadFromMergeTree.cpp index 6e1fab2c14f..57eeb5dba2d 100644 --- a/src/Processors/QueryPlan/ReadFromMergeTree.cpp +++ b/src/Processors/QueryPlan/ReadFromMergeTree.cpp @@ -1018,16 +1018,17 @@ MergeTreeDataSelectAnalysisResultPtr ReadFromMergeTree::selectRangesToRead( return std::make_shared(MergeTreeDataSelectAnalysisResult{.result = std::move(result)}); } -void ReadFromMergeTree::setQueryInfoInputOrderInfo(InputOrderInfoPtr order_info) +void ReadFromMergeTree::requestReadingInOrder(size_t prefix_size, int direction, size_t limit) { + /// if dirction is not set, use current one + if (!direction) + direction = getSortDirection(); + + auto order_info = std::make_shared(SortDescription{}, prefix_size, direction, limit); if (query_info.projection) - { query_info.projection->input_order_info = order_info; - } else - { query_info.input_order_info = order_info; - } /// update sort info for output stream SortDescription sort_description; diff --git a/src/Processors/QueryPlan/ReadFromMergeTree.h b/src/Processors/QueryPlan/ReadFromMergeTree.h index f90eaef2c63..0a013748e91 100644 --- a/src/Processors/QueryPlan/ReadFromMergeTree.h +++ b/src/Processors/QueryPlan/ReadFromMergeTree.h @@ -151,7 +151,7 @@ public: const SelectQueryInfo & getQueryInfo() const { return query_info; } StorageMetadataPtr getStorageMetadata() const { return metadata_for_reading; } - void setQueryInfoInputOrderInfo(InputOrderInfoPtr order_info); + void requestReadingInOrder(size_t prefix_size, int direction, size_t limit); private: int getSortDirection() const