From 840895509e48616992b2295c5dc0ab0af2169414 Mon Sep 17 00:00:00 2001 From: Amos Bird Date: Mon, 7 Jun 2021 22:26:28 +0800 Subject: [PATCH] Fix using aggregate projection when group by without key --- src/Interpreters/Aggregator.cpp | 4 ++-- src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp | 1 - .../01710_projections_group_by_no_key.reference | 2 ++ .../0_stateless/01710_projections_group_by_no_key.sql | 8 ++++++++ 4 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 tests/queries/0_stateless/01710_projections_group_by_no_key.reference create mode 100644 tests/queries/0_stateless/01710_projections_group_by_no_key.sql diff --git a/src/Interpreters/Aggregator.cpp b/src/Interpreters/Aggregator.cpp index 64b20a54955..81f75530cef 100644 --- a/src/Interpreters/Aggregator.cpp +++ b/src/Interpreters/Aggregator.cpp @@ -1894,11 +1894,11 @@ void NO_INLINE Aggregator::mergeWithoutKeyStreamsImpl( res = place; } - if (block.rows() > 0) + for (size_t row = 0, rows = block.rows(); row < rows; ++row) { /// Adding Values for (size_t i = 0; i < params.aggregates_size; ++i) - aggregate_functions[i]->merge(res + offsets_of_aggregate_states[i], (*aggregate_columns[i])[0], result.aggregates_pool); + aggregate_functions[i]->merge(res + offsets_of_aggregate_states[i], (*aggregate_columns[i])[row], result.aggregates_pool); } /// Early release memory. diff --git a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp index 894e57d41e5..929fb814ab8 100644 --- a/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp +++ b/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp @@ -234,7 +234,6 @@ QueryPlanPtr MergeTreeDataSelectExecutor::read( select.setExpression(ASTSelectQuery::Expression::WHERE, given_select.where()->clone()); if (given_select.prewhere()) select.setExpression(ASTSelectQuery::Expression::WHERE, given_select.prewhere()->clone()); - // TODO will row policy filter work? // After overriding the group by clause, we finish the possible aggregations directly if (processed_stage >= QueryProcessingStage::Enum::WithMergeableState && given_select.groupBy()) diff --git a/tests/queries/0_stateless/01710_projections_group_by_no_key.reference b/tests/queries/0_stateless/01710_projections_group_by_no_key.reference new file mode 100644 index 00000000000..caacaa85780 --- /dev/null +++ b/tests/queries/0_stateless/01710_projections_group_by_no_key.reference @@ -0,0 +1,2 @@ +499500 +499500 diff --git a/tests/queries/0_stateless/01710_projections_group_by_no_key.sql b/tests/queries/0_stateless/01710_projections_group_by_no_key.sql new file mode 100644 index 00000000000..eefc03afb7a --- /dev/null +++ b/tests/queries/0_stateless/01710_projections_group_by_no_key.sql @@ -0,0 +1,8 @@ +drop table if exists projection_without_key; + +create table projection_without_key (key UInt32, PROJECTION x (SELECT sum(key) group by key % 3)) engine MergeTree order by key; +insert into projection_without_key select number from numbers(1000); +select sum(key) from projection_without_key settings allow_experimental_projection_optimization = 1; +select sum(key) from projection_without_key settings allow_experimental_projection_optimization = 0; + +drop table projection_without_key;