From c5d9379df0ecffe56f186141c1da3a11ee511277 Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Wed, 17 Jun 2020 19:54:51 +0300 Subject: [PATCH] Add TotalsHavingStep. --- src/Interpreters/InterpreterSelectQuery.cpp | 8 ++-- src/Processors/QueryPlan/TotalsHavingStep.cpp | 38 ++++++++++++++++++ src/Processors/QueryPlan/TotalsHavingStep.h | 39 +++++++++++++++++++ .../Transforms/TotalsHavingTransform.cpp | 4 +- .../Transforms/TotalsHavingTransform.h | 2 + src/Processors/ya.make | 1 + 6 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 src/Processors/QueryPlan/TotalsHavingStep.cpp create mode 100644 src/Processors/QueryPlan/TotalsHavingStep.h diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 11c3a6b1211..9452fbeaa43 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -89,6 +89,7 @@ #include #include #include +#include namespace DB @@ -1508,12 +1509,13 @@ void InterpreterSelectQuery::executeTotalsAndHaving(QueryPipeline & pipeline, bo { const Settings & settings = context->getSettingsRef(); - auto totals_having = std::make_shared( - pipeline.getHeader(), overflow_row, expression, + TotalsHavingStep totals_having_step( + DataStream{.header = pipeline.getHeader()}, + overflow_row, expression, has_having ? getSelectQuery().having()->getColumnName() : "", settings.totals_mode, settings.totals_auto_threshold, final); - pipeline.addTotalsHavingTransform(std::move(totals_having)); + totals_having_step.transformPipeline(pipeline); } diff --git a/src/Processors/QueryPlan/TotalsHavingStep.cpp b/src/Processors/QueryPlan/TotalsHavingStep.cpp new file mode 100644 index 00000000000..0db1acd74f5 --- /dev/null +++ b/src/Processors/QueryPlan/TotalsHavingStep.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +namespace DB +{ + +TotalsHavingStep::TotalsHavingStep( + const DataStream & input_stream_, + bool overflow_row_, + const ExpressionActionsPtr & expression_, + const std::string & filter_column_, + TotalsMode totals_mode_, + double auto_include_threshold_, + bool final_) + : ITransformingStep( + input_stream_, + DataStream{.header = TotalsHavingTransform::transformHeader(input_stream_.header, expression_, final_)}) + , overflow_row(overflow_row_) + , expression(expression_) + , filter_column_name(filter_column_) + , totals_mode(totals_mode_) + , auto_include_threshold(auto_include_threshold_) + , final(final_) +{ +} + +void TotalsHavingStep::transformPipeline(QueryPipeline & pipeline) +{ + auto totals_having = std::make_shared( + pipeline.getHeader(), overflow_row, expression, + filter_column_name, totals_mode, auto_include_threshold, final); + + pipeline.addTotalsHavingTransform(std::move(totals_having)); +} + +} diff --git a/src/Processors/QueryPlan/TotalsHavingStep.h b/src/Processors/QueryPlan/TotalsHavingStep.h new file mode 100644 index 00000000000..ddb0ccd2572 --- /dev/null +++ b/src/Processors/QueryPlan/TotalsHavingStep.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include + +namespace DB +{ + +class ExpressionActions; +using ExpressionActionsPtr = std::shared_ptr; + +enum class TotalsMode; + +class TotalsHavingStep : public ITransformingStep +{ +public: + TotalsHavingStep( + const DataStream & input_stream_, + bool overflow_row_, + const ExpressionActionsPtr & expression_, + const std::string & filter_column_, + TotalsMode totals_mode_, + double auto_include_threshold_, + bool final_); + + String getName() const override { return "TotalsHaving"; } + + void transformPipeline(QueryPipeline & pipeline) override; + +private: + bool overflow_row; + ExpressionActionsPtr expression; + String filter_column_name; + TotalsMode totals_mode; + double auto_include_threshold; + bool final; +}; + +} + diff --git a/src/Processors/Transforms/TotalsHavingTransform.cpp b/src/Processors/Transforms/TotalsHavingTransform.cpp index 083066a72d9..eb1cbf6e9d8 100644 --- a/src/Processors/Transforms/TotalsHavingTransform.cpp +++ b/src/Processors/Transforms/TotalsHavingTransform.cpp @@ -32,7 +32,7 @@ void finalizeChunk(Chunk & chunk) chunk.setColumns(std::move(columns), num_rows); } -static Block createOutputHeader(Block block, const ExpressionActionsPtr & expression, bool final) +Block TotalsHavingTransform::transformHeader(Block block, const ExpressionActionsPtr & expression, bool final) { if (final) finalizeBlock(block); @@ -51,7 +51,7 @@ TotalsHavingTransform::TotalsHavingTransform( TotalsMode totals_mode_, double auto_include_threshold_, bool final_) - : ISimpleTransform(header, createOutputHeader(header, expression_, final_), true) + : ISimpleTransform(header, transformHeader(header, expression_, final_), true) , overflow_row(overflow_row_) , expression(expression_) , filter_column_name(filter_column_) diff --git a/src/Processors/Transforms/TotalsHavingTransform.h b/src/Processors/Transforms/TotalsHavingTransform.h index f16b333ffd4..8191ec763b0 100644 --- a/src/Processors/Transforms/TotalsHavingTransform.h +++ b/src/Processors/Transforms/TotalsHavingTransform.h @@ -37,6 +37,8 @@ public: Status prepare() override; void work() override; + static Block transformHeader(Block block, const ExpressionActionsPtr & expression, bool final); + protected: void transform(Chunk & chunk) override; diff --git a/src/Processors/ya.make b/src/Processors/ya.make index 537d81e9750..b5be0208241 100644 --- a/src/Processors/ya.make +++ b/src/Processors/ya.make @@ -155,6 +155,7 @@ SRCS( QueryPlan/ReadFromPreparedSource.cpp QueryPlan/ReadFromStorageStep.cpp QueryPlan/ReadNothingStep.cpp + QueryPlan/TotalsHavingStep QueryPlan/QueryPlan.cpp )