From 4b666226011e1dd92edc255f94a77681a2eb430f Mon Sep 17 00:00:00 2001 From: Andy Yang Date: Thu, 9 May 2019 17:24:36 +0800 Subject: [PATCH] Fix bitmap functions crash the server bug segfault --- dbms/src/Functions/FunctionsBitmap.h | 14 +++++++++-- .../00829_bitmap_function.reference | 4 ++++ .../0_stateless/00829_bitmap_function.sql | 23 ++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/dbms/src/Functions/FunctionsBitmap.h b/dbms/src/Functions/FunctionsBitmap.h index 88c17feeb5c..c5b0bc78d61 100644 --- a/dbms/src/Functions/FunctionsBitmap.h +++ b/dbms/src/Functions/FunctionsBitmap.h @@ -406,7 +406,12 @@ private: { const ColumnAggregateFunction * columns[2]; for (size_t i = 0; i < 2; ++i) - columns[i] = 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 + columns[i] = typeid_cast(block.getByPosition(arguments[i]).column.get()); + } for (size_t i = 0; i < input_rows_count; ++i) { @@ -511,7 +516,12 @@ private: { const ColumnAggregateFunction * columns[2]; for (size_t i = 0; i < 2; ++i) - columns[i] = 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 + columns[i] = typeid_cast(block.getByPosition(arguments[i]).column.get()); + } auto col_to = ColumnAggregateFunction::create(columns[0]->getAggregateFunction()); diff --git a/dbms/tests/queries/0_stateless/00829_bitmap_function.reference b/dbms/tests/queries/0_stateless/00829_bitmap_function.reference index cc159d2aaf8..be27c9c7edb 100644 --- a/dbms/tests/queries/0_stateless/00829_bitmap_function.reference +++ b/dbms/tests/queries/0_stateless/00829_bitmap_function.reference @@ -15,3 +15,7 @@ 60 50 70 40 20 30 2019-01-01 50 2019-01-02 60 +1 +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 9013b53d99e..2c43bb07e90 100644 --- a/dbms/tests/queries/0_stateless/00829_bitmap_function.sql +++ b/dbms/tests/queries/0_stateless/00829_bitmap_function.sql @@ -1,3 +1,4 @@ +USE test; SELECT bitmapToArray(bitmapBuild([1, 2, 3, 4, 5])); SELECT bitmapToArray(bitmapAnd(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))); SELECT bitmapToArray(bitmapOr(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))); @@ -53,7 +54,7 @@ ALL LEFT JOIN ) USING city_id; - +-- bitmap state test DROP TABLE IF EXISTS bitmap_state_test; CREATE TABLE bitmap_state_test ( @@ -72,6 +73,26 @@ GROUP BY pickup_date, city_id; SELECT pickup_date, groupBitmapMerge(uv) AS users from bitmap_state_test group by pickup_date; +-- between column and expression test +DROP TABLE IF EXISTS bitmap_column_expr_test; +CREATE TABLE bitmap_column_expr_test +( + t DateTime, + z AggregateFunction(groupBitmap, UInt32) +) +ENGINE = MergeTree +PARTITION BY toYYYYMMDD(t) +ORDER BY t; + +INSERT INTO bitmap_column_expr_test VALUES (now(), bitmapBuild(cast([3,19,47] as Array(UInt32)))); + +SELECT bitmapAndCardinality( bitmapBuild(cast([19,7] as Array(UInt32))), z) from bitmap_column_expr_test; +SELECT bitmapAndCardinality( z, bitmapBuild(cast([19,7] as Array(UInt32))) ) from bitmap_column_expr_test; + +select bitmapCardinality(bitmapAnd(bitmapBuild(cast([19,7] as Array(UInt32))), z )) from bitmap_column_expr_test; +select bitmapCardinality(bitmapAnd(z, bitmapBuild(cast([19,7] as Array(UInt32))))) from bitmap_column_expr_test; + DROP TABLE IF EXISTS bitmap_test; DROP TABLE IF EXISTS bitmap_state_test; +DROP TABLE IF EXISTS bitmap_column_expr_test;