From 6ada8a6337a6c165ceaf7b90b265244e6d08ff0a Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Mon, 9 May 2022 17:27:09 +0300 Subject: [PATCH] Fix optimize_aggregation_in_order with *Array aggregate functions row_begin was wrong, and before this patch aggregator processing {row_end, row_end} range, in other words, zero range. Fixes: #9113 (cc @dimarub2000) v2: add static_cast to fix UBSan Signed-off-by: Azat Khuzhin --- src/Interpreters/Aggregator.cpp | 13 +++++++++++-- ...e_aggregation_in_order_Array_functions.reference | 1 + ...ptimize_aggregation_in_order_Array_functions.sql | 5 +++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/02293_optimize_aggregation_in_order_Array_functions.reference create mode 100644 tests/queries/0_stateless/02293_optimize_aggregation_in_order_Array_functions.sql diff --git a/src/Interpreters/Aggregator.cpp b/src/Interpreters/Aggregator.cpp index a76558cbc9d..0bd0ea9f444 100644 --- a/src/Interpreters/Aggregator.cpp +++ b/src/Interpreters/Aggregator.cpp @@ -1194,9 +1194,18 @@ void NO_INLINE Aggregator::executeOnIntervalWithoutKeyImpl( for (AggregateFunctionInstruction * inst = aggregate_instructions; inst->that; ++inst) { if (inst->offsets) - inst->batch_that->addBatchSinglePlaceFromInterval(inst->offsets[row_begin], inst->offsets[row_end - 1], res + inst->state_offset, inst->batch_arguments, arena); + inst->batch_that->addBatchSinglePlaceFromInterval( + inst->offsets[static_cast(row_begin) - 1], + inst->offsets[row_end - 1], + res + inst->state_offset, + inst->batch_arguments, arena); else - inst->batch_that->addBatchSinglePlaceFromInterval(row_begin, row_end, res + inst->state_offset, inst->batch_arguments, arena); + inst->batch_that->addBatchSinglePlaceFromInterval( + row_begin, + row_end, + res + inst->state_offset, + inst->batch_arguments, + arena); } } diff --git a/tests/queries/0_stateless/02293_optimize_aggregation_in_order_Array_functions.reference b/tests/queries/0_stateless/02293_optimize_aggregation_in_order_Array_functions.reference new file mode 100644 index 00000000000..c800527c196 --- /dev/null +++ b/tests/queries/0_stateless/02293_optimize_aggregation_in_order_Array_functions.reference @@ -0,0 +1 @@ +[0,1] [0,1] diff --git a/tests/queries/0_stateless/02293_optimize_aggregation_in_order_Array_functions.sql b/tests/queries/0_stateless/02293_optimize_aggregation_in_order_Array_functions.sql new file mode 100644 index 00000000000..2df7c9f2b8f --- /dev/null +++ b/tests/queries/0_stateless/02293_optimize_aggregation_in_order_Array_functions.sql @@ -0,0 +1,5 @@ +drop table if exists data_02293; +create table data_02293 (a Int64, grp_aggreg AggregateFunction(groupArrayArray, Array(UInt64)), grp_simple SimpleAggregateFunction(groupArrayArray, Array(UInt64))) engine = MergeTree() order by a; +insert into data_02293 select 1 a, groupArrayArrayState([toUInt64(number)]), groupArrayArray([toUInt64(number)]) from numbers(2) group by a; +SELECT arraySort(groupArrayArrayMerge(grp_aggreg)) gra , arraySort(groupArrayArray(grp_simple)) grs FROM data_02293 group by a SETTINGS optimize_aggregation_in_order=1; +drop table data_02293;