From d9b312f95599ac0ed99dbb9ddfbdcee4ab270672 Mon Sep 17 00:00:00 2001 From: Igor Nikonov Date: Fri, 15 Jul 2022 17:00:25 +0000 Subject: [PATCH] Self-review + revert test --- src/Processors/QueryPlan/ReadFromMergeTree.cpp | 13 ++++--------- src/Processors/QueryPlan/ReadFromMergeTree.h | 12 ++++++------ src/Processors/QueryPlan/SortingStep.cpp | 12 ++++++------ ...ons_optimize_read_in_window_order_long.reference | 3 +++ ...imizations_optimize_read_in_window_order_long.sh | 6 +++--- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/Processors/QueryPlan/ReadFromMergeTree.cpp b/src/Processors/QueryPlan/ReadFromMergeTree.cpp index e90e55264c2..4d238f18127 100644 --- a/src/Processors/QueryPlan/ReadFromMergeTree.cpp +++ b/src/Processors/QueryPlan/ReadFromMergeTree.cpp @@ -141,7 +141,7 @@ ReadFromMergeTree::ReadFromMergeTree( output_stream->sort_mode = DataStream::SortMode::Chunk; auto const& settings = context->getSettingsRef(); - if ((settings.optimize_read_in_order || settings.optimize_aggregation_in_order || settings.optimize_read_in_window_order) && getInputOrderInfo()) + if ((settings.optimize_read_in_order || settings.optimize_aggregation_in_order) && getInputOrderInfo(query_info)) output_stream->sort_mode = DataStream::SortMode::Port; } } @@ -966,10 +966,7 @@ MergeTreeDataSelectAnalysisResultPtr ReadFromMergeTree::selectRangesToRead( result.total_marks_pk = total_marks_pk; result.selected_rows = sum_rows; - const auto & input_order_info = query_info.input_order_info - ? query_info.input_order_info - : (query_info.projection ? query_info.projection->input_order_info : nullptr); - + const auto & input_order_info = getInputOrderInfo(query_info); if ((settings.optimize_read_in_order || settings.optimize_aggregation_in_order) && input_order_info) result.read_type = (input_order_info->direction > 0) ? ReadType::InOrder : ReadType::InReverseOrder; @@ -989,10 +986,8 @@ void ReadFromMergeTree::setQueryInfoOrderOptimizer(std::shared_ptrinput_order_info = order_info; @@ -1062,7 +1057,7 @@ void ReadFromMergeTree::initializePipeline(QueryPipelineBuilder & pipeline, cons Pipe pipe; const auto & settings = context->getSettingsRef(); - const auto & input_order_info = getInputOrderInfo(); + const auto & input_order_info = getInputOrderInfo(query_info); if (select.final()) { diff --git a/src/Processors/QueryPlan/ReadFromMergeTree.h b/src/Processors/QueryPlan/ReadFromMergeTree.h index ab11cc20ecd..baf4c189b10 100644 --- a/src/Processors/QueryPlan/ReadFromMergeTree.h +++ b/src/Processors/QueryPlan/ReadFromMergeTree.h @@ -1,7 +1,7 @@ #pragma once #include #include -#include "Storages/SelectQueryInfo.h" +#include namespace DB { @@ -135,17 +135,17 @@ public: StorageMetadataPtr getStorageMetadata() const { return metadata_for_reading; } void setQueryInfoOrderOptimizer(std::shared_ptr read_in_order_optimizer); - void setQueryInfoInputOrderInfo(const InputOrderInfoPtr & order_info); + void setQueryInfoInputOrderInfo(InputOrderInfoPtr order_info); private: - InputOrderInfoPtr getInputOrderInfo() const + static InputOrderInfoPtr getInputOrderInfo(const SelectQueryInfo & query_info_) { - return query_info.input_order_info ? query_info.input_order_info - : (query_info.projection ? query_info.projection->input_order_info : nullptr); + return query_info_.input_order_info ? query_info_.input_order_info + : (query_info_.projection ? query_info_.projection->input_order_info : nullptr); } int getSortDirection() const { - const InputOrderInfoPtr & order_info = getInputOrderInfo(); + const InputOrderInfoPtr & order_info = getInputOrderInfo(query_info); if (order_info) return order_info->direction; diff --git a/src/Processors/QueryPlan/SortingStep.cpp b/src/Processors/QueryPlan/SortingStep.cpp index f430b73f1a9..04a852d8ed9 100644 --- a/src/Processors/QueryPlan/SortingStep.cpp +++ b/src/Processors/QueryPlan/SortingStep.cpp @@ -106,6 +106,12 @@ void SortingStep::updateLimit(size_t limit_) } } +void SortingStep::convertToFinishSorting(SortDescription prefix_description_) +{ + type = Type::FinishSorting; + prefix_description = std::move(prefix_description_); +} + void SortingStep::finishSorting(QueryPipelineBuilder & pipeline, const SortDescription & input_sort_desc) { pipeline.addSimpleTransform( @@ -220,12 +226,6 @@ void SortingStep::fullSort(QueryPipelineBuilder & pipeline, const bool skip_part } } -void SortingStep::convertToFinishSorting(SortDescription prefix_description_) -{ - type = Type::FinishSorting; - prefix_description = std::move(prefix_description_); -} - static Poco::Logger * getLogger() { static Poco::Logger & logger = Poco::Logger::get("SortingStep"); diff --git a/tests/queries/0_stateless/01655_plan_optimizations_optimize_read_in_window_order_long.reference b/tests/queries/0_stateless/01655_plan_optimizations_optimize_read_in_window_order_long.reference index d86bac9de59..b462a5a7baa 100644 --- a/tests/queries/0_stateless/01655_plan_optimizations_optimize_read_in_window_order_long.reference +++ b/tests/queries/0_stateless/01655_plan_optimizations_optimize_read_in_window_order_long.reference @@ -1 +1,4 @@ OK +OK +OK +OK diff --git a/tests/queries/0_stateless/01655_plan_optimizations_optimize_read_in_window_order_long.sh b/tests/queries/0_stateless/01655_plan_optimizations_optimize_read_in_window_order_long.sh index cef8ccef624..297688a29c3 100755 --- a/tests/queries/0_stateless/01655_plan_optimizations_optimize_read_in_window_order_long.sh +++ b/tests/queries/0_stateless/01655_plan_optimizations_optimize_read_in_window_order_long.sh @@ -19,13 +19,13 @@ $CLICKHOUSE_CLIENT -q "create table ${name}_n_x engine=MergeTree order by (n, x) $CLICKHOUSE_CLIENT -q "optimize table ${name}_n final" $CLICKHOUSE_CLIENT -q "optimize table ${name}_n_x final" -$CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (ORDER BY n, x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n SETTINGS optimize_read_in_window_order=0, max_memory_usage=$max_memory_usage, max_threads=1 format Null" +$CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (ORDER BY n, x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n SETTINGS optimize_read_in_window_order=0, max_memory_usage=$max_memory_usage, max_threads=1 format Null" 2>&1 | grep -F -q "MEMORY_LIMIT_EXCEEDED" && echo 'OK' || echo 'FAIL' $CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (ORDER BY n, x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n SETTINGS optimize_read_in_window_order=1, max_memory_usage=$max_memory_usage, max_threads=1 format Null" -$CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (ORDER BY n, x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n_x SETTINGS optimize_read_in_window_order=0, max_memory_usage=$max_memory_usage, max_threads=1 format Null" +$CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (ORDER BY n, x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n_x SETTINGS optimize_read_in_window_order=0, max_memory_usage=$max_memory_usage, max_threads=1 format Null" 2>&1 | grep -F -q "MEMORY_LIMIT_EXCEEDED" && echo 'OK' || echo 'FAIL' $CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (ORDER BY n, x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n_x SETTINGS optimize_read_in_window_order=1, max_memory_usage=$max_memory_usage, max_threads=1 format Null" -$CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (PARTITION BY n ORDER BY x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n_x SETTINGS optimize_read_in_window_order=0, max_memory_usage=$max_memory_usage, max_threads=1 format Null" +$CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (PARTITION BY n ORDER BY x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n_x SETTINGS optimize_read_in_window_order=0, max_memory_usage=$max_memory_usage, max_threads=1 format Null" 2>&1 | grep -F -q "MEMORY_LIMIT_EXCEEDED" && echo 'OK' || echo 'FAIL' $CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (PARTITION BY n ORDER BY x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n_x SETTINGS optimize_read_in_window_order=1, max_memory_usage=$max_memory_usage, max_threads=1 format Null" $CLICKHOUSE_CLIENT -q "select n, sum(x) OVER (PARTITION BY n+x%2 ORDER BY n, x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n_x SETTINGS optimize_read_in_window_order=1, max_memory_usage=$max_memory_usage, max_threads=1 format Null" 2>&1 | grep -F -q "MEMORY_LIMIT_EXCEEDED" && echo 'OK' || echo 'FAIL'