From 447fef702d46c29f1922bce2e5576634dd7db726 Mon Sep 17 00:00:00 2001 From: Anton Popov Date: Tue, 22 Jun 2021 16:08:12 +0300 Subject: [PATCH] fix fuzzer in query with 'WITH FILL' and 'WITH TOTALS' --- src/Processors/QueryPlan/FillingStep.cpp | 6 ++--- .../Transforms/FillingTransform.cpp | 11 ++++++++-- src/Processors/Transforms/FillingTransform.h | 4 +++- .../01921_with_fill_with_totals.reference | 22 ++++++++++++++----- .../01921_with_fill_with_totals.sql | 11 +++++++++- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/Processors/QueryPlan/FillingStep.cpp b/src/Processors/QueryPlan/FillingStep.cpp index 5393f1f5133..ba3588efa72 100644 --- a/src/Processors/QueryPlan/FillingStep.cpp +++ b/src/Processors/QueryPlan/FillingStep.cpp @@ -40,10 +40,8 @@ void FillingStep::transformPipeline(QueryPipeline & pipeline, const BuildQueryPi { pipeline.addSimpleTransform([&](const Block & header, QueryPipeline::StreamType stream_type) -> ProcessorPtr { - if (stream_type == QueryPipeline::StreamType::Totals) - return nullptr; - - return std::make_shared(header, sort_description); + bool on_totals = stream_type == QueryPipeline::StreamType::Totals; + return std::make_shared(header, sort_description, on_totals); }); } diff --git a/src/Processors/Transforms/FillingTransform.cpp b/src/Processors/Transforms/FillingTransform.cpp index 8419daf9186..45e46649b3a 100644 --- a/src/Processors/Transforms/FillingTransform.cpp +++ b/src/Processors/Transforms/FillingTransform.cpp @@ -30,12 +30,16 @@ Block FillingTransform::transformHeader(Block header, const SortDescription & so } FillingTransform::FillingTransform( - const Block & header_, const SortDescription & sort_description_) + const Block & header_, const SortDescription & sort_description_, bool on_totals_) : ISimpleTransform(header_, transformHeader(header_, sort_description_), true) , sort_description(sort_description_) + , on_totals(on_totals_) , filling_row(sort_description_) , next_row(sort_description_) { + if (on_totals) + return; + auto try_convert_fields = [](auto & descr, const auto & type) { auto max_type = Field::Types::Null; @@ -106,7 +110,7 @@ FillingTransform::FillingTransform( IProcessor::Status FillingTransform::prepare() { - if (input.isFinished() && !output.isFinished() && !has_input && !generate_suffix) + if (!on_totals && input.isFinished() && !output.isFinished() && !has_input && !generate_suffix) { should_insert_first = next_row < filling_row; @@ -126,6 +130,9 @@ IProcessor::Status FillingTransform::prepare() void FillingTransform::transform(Chunk & chunk) { + if (on_totals) + return; + Columns old_fill_columns; Columns old_other_columns; MutableColumns res_fill_columns; diff --git a/src/Processors/Transforms/FillingTransform.h b/src/Processors/Transforms/FillingTransform.h index 33717b079a0..7ccebadfb6d 100644 --- a/src/Processors/Transforms/FillingTransform.h +++ b/src/Processors/Transforms/FillingTransform.h @@ -13,7 +13,7 @@ namespace DB class FillingTransform : public ISimpleTransform { public: - FillingTransform(const Block & header_, const SortDescription & sort_description_); + FillingTransform(const Block & header_, const SortDescription & sort_description_, bool on_totals_); String getName() const override { return "FillingTransform"; } @@ -28,6 +28,8 @@ private: void setResultColumns(Chunk & chunk, MutableColumns & fill_columns, MutableColumns & other_columns) const; const SortDescription sort_description; /// Contains only rows with WITH FILL. + const bool on_totals; /// FillingTransform does nothing on totals. + FillingRow filling_row; /// Current row, which is used to fill gaps. FillingRow next_row; /// Row to which we need to generate filling rows. diff --git a/tests/queries/0_stateless/01921_with_fill_with_totals.reference b/tests/queries/0_stateless/01921_with_fill_with_totals.reference index 47c8c60e3c3..1f209c7db3d 100644 --- a/tests/queries/0_stateless/01921_with_fill_with_totals.reference +++ b/tests/queries/0_stateless/01921_with_fill_with_totals.reference @@ -1,8 +1,20 @@ -20 0 -19 0 -18 0 -17 0 -16 0 +15 0 +14 0 +13 0 +12 0 +11 0 +10 0 +9 0 +8 0 +7 7 +6 0 +5 0 +4 4 +3 0 +2 0 +1 1 + +0 12 15 0 14 0 13 0 diff --git a/tests/queries/0_stateless/01921_with_fill_with_totals.sql b/tests/queries/0_stateless/01921_with_fill_with_totals.sql index 9d201848141..1821e5b2413 100644 --- a/tests/queries/0_stateless/01921_with_fill_with_totals.sql +++ b/tests/queries/0_stateless/01921_with_fill_with_totals.sql @@ -5,4 +5,13 @@ FROM numbers(10) WHERE number % 3 = 1 GROUP BY number WITH TOTALS -ORDER BY number DESC WITH FILL FROM 20; +ORDER BY number DESC WITH FILL FROM 15; + +SELECT + number, + sum(number) +FROM numbers(10) +WHERE number % 3 = 1 +GROUP BY number + WITH TOTALS +ORDER BY 10, number DESC WITH FILL FROM 15;