From 213e2dd700bb2e526fdf4d487fd32dd4776ac5e9 Mon Sep 17 00:00:00 2001 From: tyrionhuang Date: Sun, 8 Dec 2019 22:44:17 +0800 Subject: [PATCH 1/3] fix BitmapAnd error, like issue #5479: bitmapHasAny in scalar subquery causes segment fault --- dbms/src/Functions/FunctionsBitmap.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/dbms/src/Functions/FunctionsBitmap.h b/dbms/src/Functions/FunctionsBitmap.h index 322399e96a6..26135fc250d 100644 --- a/dbms/src/Functions/FunctionsBitmap.h +++ b/dbms/src/Functions/FunctionsBitmap.h @@ -991,24 +991,35 @@ 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())) + if (auto argument_column_const = typeid_cast(block.getByPosition(arguments[i]).column.get())) { columns[i] = typeid_cast(argument_column_const->getDataColumnPtr().get()); - else + 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); From 6cf7ac045b924556c8ff01f93edf32f731936647 Mon Sep 17 00:00:00 2001 From: tyrionhuang Date: Sun, 8 Dec 2019 23:10:37 +0800 Subject: [PATCH 2/3] fix BitmapAnd error, like issue #5479: bitmapHasAny in scalar subquery causes segment fault --- dbms/src/Functions/FunctionsBitmap.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dbms/src/Functions/FunctionsBitmap.h b/dbms/src/Functions/FunctionsBitmap.h index 26135fc250d..c6dce7dfdb6 100644 --- a/dbms/src/Functions/FunctionsBitmap.h +++ b/dbms/src/Functions/FunctionsBitmap.h @@ -994,11 +994,13 @@ private: 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())) { + 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 { + else + { columns[i] = typeid_cast(block.getByPosition(arguments[i]).column.get()); is_column_const[i] = false; } From caedf9d91de6181b466a29f242040b25108a426a Mon Sep 17 00:00:00 2001 From: tyrionhuang Date: Sat, 21 Dec 2019 17:22:39 +0800 Subject: [PATCH 3/3] add bitmapAnd sql & reference --- .../0_stateless/00829_bitmap_function.reference | 10 +++++++--- .../queries/0_stateless/00829_bitmap_function.sql | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) 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