From 279970337a9d7aecaa7a2f5c6db256587ddc9560 Mon Sep 17 00:00:00 2001 From: Li Shuai Date: Thu, 27 Apr 2023 22:58:49 +0800 Subject: [PATCH] Fix all key value is null and group use rollup return wrong answer --- src/Interpreters/Aggregator.cpp | 4 ++-- .../02725_null_group_key_with_rollup.reference | 10 ++++++++++ .../02725_null_group_key_with_rollup.sql | 13 +++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/02725_null_group_key_with_rollup.reference create mode 100644 tests/queries/0_stateless/02725_null_group_key_with_rollup.sql diff --git a/src/Interpreters/Aggregator.cpp b/src/Interpreters/Aggregator.cpp index f11792afcc7..7df36e8600a 100644 --- a/src/Interpreters/Aggregator.cpp +++ b/src/Interpreters/Aggregator.cpp @@ -2102,6 +2102,7 @@ Aggregator::convertToBlockImplNotFinal(Method & method, Table & data, Arenas & a std::optional out_cols; std::optional shuffled_key_sizes; + size_t rows_in_current_block = 0; auto init_out_cols = [&]() { @@ -2116,6 +2117,7 @@ Aggregator::convertToBlockImplNotFinal(Method & method, Table & data, Arenas & a for (size_t i = 0; i < params.aggregates_size; ++i) out_cols->aggregate_columns_data[i]->push_back(data.getNullKeyData() + offsets_of_aggregate_states[i]); + ++rows_in_current_block; data.getNullKeyData() = nullptr; data.hasNullKeyData() = false; } @@ -2127,8 +2129,6 @@ Aggregator::convertToBlockImplNotFinal(Method & method, Table & data, Arenas & a // should be invoked at least once, because null data might be the only content of the `data` init_out_cols(); - size_t rows_in_current_block = 0; - data.forEachValue( [&](const auto & key, auto & mapped) { diff --git a/tests/queries/0_stateless/02725_null_group_key_with_rollup.reference b/tests/queries/0_stateless/02725_null_group_key_with_rollup.reference new file mode 100644 index 00000000000..e296f838e48 --- /dev/null +++ b/tests/queries/0_stateless/02725_null_group_key_with_rollup.reference @@ -0,0 +1,10 @@ +\N 2 + +\N 2 +\N 2 + +\N 2 +\N 2 +\N 2 +\N 2 +\N 2 diff --git a/tests/queries/0_stateless/02725_null_group_key_with_rollup.sql b/tests/queries/0_stateless/02725_null_group_key_with_rollup.sql new file mode 100644 index 00000000000..98f354e2911 --- /dev/null +++ b/tests/queries/0_stateless/02725_null_group_key_with_rollup.sql @@ -0,0 +1,13 @@ +set allow_suspicious_low_cardinality_types=1; +DROP TABLE IF EXISTS group_by_null_key; +CREATE TABLE group_by_null_key (c1 Nullable(Int32), c2 LowCardinality(Nullable(Int32))) ENGINE = Memory(); +INSERT INTO group_by_null_key VALUES (null, null), (null, null); + +select c1, count(*) from group_by_null_key group by c1 WITH TOTALS; +select c2, count(*) from group_by_null_key group by c2 WITH TOTALS; + +select c1, count(*) from group_by_null_key group by ROLLUP(c1); +select c2, count(*) from group_by_null_key group by ROLLUP(c2); + + +DROP TABLE group_by_null_key;