diff --git a/dbms/src/Functions/FunctionsBitmap.h b/dbms/src/Functions/FunctionsBitmap.h index a3fe968a157..c264b2fb0ba 100644 --- a/dbms/src/Functions/FunctionsBitmap.h +++ b/dbms/src/Functions/FunctionsBitmap.h @@ -991,24 +991,37 @@ private: void executeBitmapData(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) { const ColumnAggregateFunction * columns[2]; + bool is_column_const[2]; for (size_t i = 0; i < 2; ++i) { if (auto argument_column_const = typeid_cast(block.getByPosition(arguments[i]).column.get())) + { columns[i] = typeid_cast(argument_column_const->getDataColumnPtr().get()); + is_column_const[i] = true; + } else + { columns[i] = typeid_cast(block.getByPosition(arguments[i]).column.get()); + is_column_const[i] = false; + } } auto col_to = ColumnAggregateFunction::create(columns[0]->getAggregateFunction()); col_to->reserve(input_rows_count); + const PaddedPODArray & container0 = columns[0]->getData(); + const PaddedPODArray & container1 = columns[1]->getData(); + for (size_t i = 0; i < input_rows_count; ++i) { - col_to->insertFrom(columns[0]->getData()[i]); + const AggregateDataPtr data_ptr_0 = is_column_const[0] ? container0[0] : container0[i]; + const AggregateDataPtr data_ptr_1 = is_column_const[1] ? container1[0] : container1[i]; + + col_to->insertFrom(data_ptr_0); AggregateFunctionGroupBitmapData & bitmap_data_1 = *reinterpret_cast *>(col_to->getData()[i]); const AggregateFunctionGroupBitmapData & bitmap_data_2 - = *reinterpret_cast *>(columns[1]->getData()[i]); + = *reinterpret_cast *>(data_ptr_1); Impl::apply(bitmap_data_1, bitmap_data_2); } block.getByPosition(result).column = std::move(col_to); diff --git a/dbms/tests/queries/0_stateless/00829_bitmap_function.reference b/dbms/tests/queries/0_stateless/00829_bitmap_function.reference index bc1fda84ed9..e03a7300db0 100644 --- a/dbms/tests/queries/0_stateless/00829_bitmap_function.reference +++ b/dbms/tests/queries/0_stateless/00829_bitmap_function.reference @@ -13,15 +13,19 @@ 70 2019-01-01 50 [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50] 2019-01-02 60 [11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70] +2019-01-03 10 [1,2,3,4,5,6,7,8,9,10] 60 50 70 40 20 30 60 50 70 40 20 30 -90 -90 +100 +100 20 -90 +100 20 +[1,2,3] +[1,2,3] 2019-01-01 50 2019-01-02 60 +2019-01-03 10 1 1 1 diff --git a/dbms/tests/queries/0_stateless/00829_bitmap_function.sql b/dbms/tests/queries/0_stateless/00829_bitmap_function.sql index 241a827fb9b..1217fbefc71 100644 --- a/dbms/tests/queries/0_stateless/00829_bitmap_function.sql +++ b/dbms/tests/queries/0_stateless/00829_bitmap_function.sql @@ -15,6 +15,7 @@ DROP TABLE IF EXISTS bitmap_test; CREATE TABLE bitmap_test(pickup_date Date, city_id UInt32, uid UInt32)ENGINE = Memory; INSERT INTO bitmap_test SELECT '2019-01-01', 1, number FROM numbers(1,50); INSERT INTO bitmap_test SELECT '2019-01-02', 1, number FROM numbers(11,60); +INSERT INTO bitmap_test SELECT '2019-01-03', 2, number FROM numbers(1,10); SELECT groupBitmap( uid ) AS user_num FROM bitmap_test; @@ -65,6 +66,9 @@ SELECT count(*) FROM bitmap_test WHERE bitmapContains((SELECT groupBitmapState(u SELECT count(*) FROM bitmap_test WHERE 0 = bitmapContains((SELECT groupBitmapState(uid) FROM bitmap_test WHERE pickup_date = '2019-01-01'), uid); +-- PR#8082 +SELECT bitmapToArray(bitmapAnd(groupBitmapState(uid), bitmapBuild(CAST([1, 2, 3], 'Array(UInt32)')))) FROM bitmap_test GROUP BY city_id; + -- bitmap state test DROP TABLE IF EXISTS bitmap_state_test; CREATE TABLE bitmap_state_test