diff --git a/src/Processors/QueryPipeline.cpp b/src/Processors/QueryPipeline.cpp index ff086fd5b115..97d753bf5fb1 100644 --- a/src/Processors/QueryPipeline.cpp +++ b/src/Processors/QueryPipeline.cpp @@ -182,8 +182,11 @@ void QueryPipeline::addExtremesTransform() { checkInitializedAndNotCompleted(); + /// It is possible that pipeline already have extremes. + /// For example, it may be added from VIEW subquery. + /// In this case, recalculate extremes again - they should be calculated for different rows. if (pipe.getExtremesPort()) - throw Exception("Extremes transform was already added to pipeline.", ErrorCodes::LOGICAL_ERROR); + pipe.dropExtremes(); resize(1); auto transform = std::make_shared(getHeader()); diff --git a/tests/queries/0_stateless/01660_second_extremes_bug.reference b/tests/queries/0_stateless/01660_second_extremes_bug.reference new file mode 100644 index 000000000000..9b6a60d5b25a --- /dev/null +++ b/tests/queries/0_stateless/01660_second_extremes_bug.reference @@ -0,0 +1,16 @@ +{ + "meta": + [ + { + "name": "a", + "type": "String" + } + ], + + "data": + [ + + ], + + "rows": 0 +} diff --git a/tests/queries/0_stateless/01660_second_extremes_bug.sql b/tests/queries/0_stateless/01660_second_extremes_bug.sql new file mode 100644 index 000000000000..f5515470208e --- /dev/null +++ b/tests/queries/0_stateless/01660_second_extremes_bug.sql @@ -0,0 +1,10 @@ +DROP TABLE IF EXISTS t; +DROP TABLE IF EXISTS t_v; + +CREATE TABLE t ( a String ) ENGINE = Memory(); +CREATE VIEW t_v AS SELECT * FROM t; +SET output_format_write_statistics = 0; +SELECT * FROM t_v FORMAT JSON SETTINGS extremes = 1; + +DROP TABLE IF EXISTS t; +DROP TABLE IF EXISTS t_v;