From 1ff3d708cab1b9e839be002e0b870badf5e99ec5 Mon Sep 17 00:00:00 2001 From: zhangstar333 <87313068+zhangstar333@users.noreply.github.com> Date: Mon, 1 Nov 2021 14:00:07 +0800 Subject: [PATCH] [Function] add functions of bitmap_and/or_count (#6912) issue #6875 add bitmap_and_count/ bitmap_or_count --- be/src/exprs/bitmap_function.cpp | 9 +++ be/src/exprs/bitmap_function.h | 3 + be/test/exprs/bitmap_function_test.cpp | 52 +++++++++++++++ docs/.vuepress/sidebar/en.js | 2 + docs/.vuepress/sidebar/zh-CN.js | 2 + .../bitmap-functions/bitmap_and_count.md | 63 +++++++++++++++++++ .../bitmap-functions/bitmap_or_count.md | 63 +++++++++++++++++++ .../bitmap-functions/bitmap_and_count.md | 63 +++++++++++++++++++ .../bitmap-functions/bitmap_or_count.md | 63 +++++++++++++++++++ gensrc/script/doris_builtins_functions.py | 8 ++- 10 files changed, 327 insertions(+), 1 deletion(-) create mode 100644 docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_and_count.md create mode 100644 docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_or_count.md create mode 100644 docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_and_count.md create mode 100644 docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_or_count.md diff --git a/be/src/exprs/bitmap_function.cpp b/be/src/exprs/bitmap_function.cpp index f2576641e5..fe70a4871a 100644 --- a/be/src/exprs/bitmap_function.cpp +++ b/be/src/exprs/bitmap_function.cpp @@ -489,6 +489,15 @@ StringVal BitmapFunctions::bitmap_and(FunctionContext* ctx, const StringVal& lhs } return serialize(ctx, &bitmap); } +BigIntVal BitmapFunctions::bitmap_and_count(FunctionContext* ctx, const StringVal& lhs, + const StringVal& rhs) { + return bitmap_count(ctx, bitmap_and(ctx, lhs, rhs)); +} + +BigIntVal BitmapFunctions::bitmap_or_count(FunctionContext* ctx, const StringVal& lhs, + const StringVal& rhs) { + return bitmap_count(ctx, bitmap_or(ctx, lhs, rhs)); +} StringVal BitmapFunctions::bitmap_xor(FunctionContext* ctx, const StringVal& lhs, const StringVal& rhs) { diff --git a/be/src/exprs/bitmap_function.h b/be/src/exprs/bitmap_function.h index 9c4f155b08..9143a51bcd 100644 --- a/be/src/exprs/bitmap_function.h +++ b/be/src/exprs/bitmap_function.h @@ -59,6 +59,9 @@ public: const StringVal& dst); static BigIntVal bitmap_min(FunctionContext* ctx, const StringVal& str); + static BigIntVal bitmap_and_count(FunctionContext* ctx, const StringVal& lhs, const StringVal& rhs); + static BigIntVal bitmap_or_count(FunctionContext* ctx, const StringVal& lhs, const StringVal& rhs); + static StringVal bitmap_serialize(FunctionContext* ctx, const StringVal& src); static StringVal to_bitmap(FunctionContext* ctx, const StringVal& src); static StringVal bitmap_hash(FunctionContext* ctx, const StringVal& src); diff --git a/be/test/exprs/bitmap_function_test.cpp b/be/test/exprs/bitmap_function_test.cpp index f734e9f690..686d2df5bc 100644 --- a/be/test/exprs/bitmap_function_test.cpp +++ b/be/test/exprs/bitmap_function_test.cpp @@ -347,6 +347,58 @@ TEST_F(BitmapFunctionsTest, bitmap_and) { ASSERT_EQ(expected, result); } +TEST_F(BitmapFunctionsTest, bitmap_and_count) { + BitmapValue bitmap1({0, 1, 2}); + BitmapValue bitmap2; + StringVal bitmap_src1 = convert_bitmap_to_string(ctx, bitmap1); + StringVal bitmap_src2 = convert_bitmap_to_string(ctx, bitmap2); + BigIntVal result = BitmapFunctions::bitmap_and_count(ctx, bitmap_src1, bitmap_src2); + ASSERT_EQ(BigIntVal(0), result); + + result = BitmapFunctions::bitmap_and_count(ctx, bitmap_src1, StringVal::null()); + ASSERT_EQ(BigIntVal(0), result); + + bitmap1 = BitmapValue({0, 1, 2,std::numeric_limits::min()}); + bitmap2 = BitmapValue({0, 1, 2,std::numeric_limits::max()}); + bitmap_src1 = convert_bitmap_to_string(ctx, bitmap1); + bitmap_src2 = convert_bitmap_to_string(ctx, bitmap2); + result = BitmapFunctions::bitmap_and_count(ctx, bitmap_src1, bitmap_src2); + ASSERT_EQ(BigIntVal(3), result); + + bitmap1 = BitmapValue({1, 2, 3}); + bitmap2 = BitmapValue({3, 4, 5}); + bitmap_src1 = convert_bitmap_to_string(ctx, bitmap1); + bitmap_src2 = convert_bitmap_to_string(ctx, bitmap2); + result = BitmapFunctions::bitmap_and_count(ctx, bitmap_src1, bitmap_src2); + ASSERT_EQ(BigIntVal(1), result); +} + +TEST_F(BitmapFunctionsTest, bitmap_or_count) { + BitmapValue bitmap1({0, 1, 2}); + BitmapValue bitmap2; + StringVal bitmap_src1 = convert_bitmap_to_string(ctx, bitmap1); + StringVal bitmap_src2 = convert_bitmap_to_string(ctx, bitmap2); + BigIntVal result = BitmapFunctions::bitmap_or_count(ctx, bitmap_src1, bitmap_src2); + ASSERT_EQ(BigIntVal(3), result); + + result = BitmapFunctions::bitmap_or_count(ctx, bitmap_src1, StringVal::null()); + ASSERT_EQ(BigIntVal(0), result); + + bitmap1 = BitmapValue({0, 1, 2, std::numeric_limits::min()}); + bitmap2 = BitmapValue({0, 1, 2, std::numeric_limits::max()}); + bitmap_src1 = convert_bitmap_to_string(ctx, bitmap1); + bitmap_src2 = convert_bitmap_to_string(ctx, bitmap2); + result = BitmapFunctions::bitmap_or_count(ctx, bitmap_src1, bitmap_src2); + ASSERT_EQ(BigIntVal(4), result); + + bitmap1 = BitmapValue({1, 2, 3}); + bitmap2 = BitmapValue({3, 4, 5}); + bitmap_src1 = convert_bitmap_to_string(ctx, bitmap1); + bitmap_src2 = convert_bitmap_to_string(ctx, bitmap2); + result = BitmapFunctions::bitmap_or_count(ctx, bitmap_src1, bitmap_src2); + ASSERT_EQ(BigIntVal(5), result); +} + TEST_F(BitmapFunctionsTest, bitmap_not) { // result is bitmap BitmapValue bitmap1({1024, 1, 2019}); diff --git a/docs/.vuepress/sidebar/en.js b/docs/.vuepress/sidebar/en.js index 61120b1c4e..09013ee5a5 100644 --- a/docs/.vuepress/sidebar/en.js +++ b/docs/.vuepress/sidebar/en.js @@ -415,6 +415,8 @@ module.exports = [ "bitmap_hash", "bitmap_intersect", "bitmap_or", + "bitmap_and_count", + "bitmap_or_count", "bitmap_xor", "bitmap_not", "bitmap_and_not", diff --git a/docs/.vuepress/sidebar/zh-CN.js b/docs/.vuepress/sidebar/zh-CN.js index 99f858c9d0..e0e495df92 100644 --- a/docs/.vuepress/sidebar/zh-CN.js +++ b/docs/.vuepress/sidebar/zh-CN.js @@ -419,6 +419,8 @@ module.exports = [ "bitmap_hash", "bitmap_intersect", "bitmap_or", + "bitmap_and_count", + "bitmap_or_count", "bitmap_xor", "bitmap_not", "bitmap_and_not", diff --git a/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_and_count.md b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_and_count.md new file mode 100644 index 0000000000..3736bf3524 --- /dev/null +++ b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_and_count.md @@ -0,0 +1,63 @@ +--- +{ + "title": "bitmap_and_count", + "language": "en" +} +--- + + + +# bitmap_and_count +## description +### Syntax + +`BigIntVal bitmap_and_count(BITMAP lhs, BITMAP rhs)` + +Calculate the intersection of two input bitmaps and return the number of intersections. + +## example + +``` +MySQL> select bitmap_and_count(bitmap_from_string('1,2,3'),bitmap_empty()); ++---------------------------------------------------------------+ +| bitmap_and_count(bitmap_from_string('1,2,3'), bitmap_empty()) | ++---------------------------------------------------------------+ +| 0 | ++---------------------------------------------------------------+ + + +MySQL> select bitmap_and_count(bitmap_from_string('1,2,3'),bitmap_from_string('1,2,3')); ++----------------------------------------------------------------------------+ +| bitmap_and_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3')) | ++----------------------------------------------------------------------------+ +| 3 | ++----------------------------------------------------------------------------+ + +MySQL> select bitmap_and_count(bitmap_from_string('1,2,3'),bitmap_from_string('3,4,5')); ++----------------------------------------------------------------------------+ +| bitmap_and_count(bitmap_from_string('1,2,3'), bitmap_from_string('3,4,5')) | ++----------------------------------------------------------------------------+ +| 1 | ++----------------------------------------------------------------------------+ +``` + +## keyword + + BITMAP_AND_COUNT,BITMAP diff --git a/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_or_count.md b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_or_count.md new file mode 100644 index 0000000000..f296f48ad5 --- /dev/null +++ b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_or_count.md @@ -0,0 +1,63 @@ +--- +{ + "title": "bitmap_or_count", + "language": "en" +} +--- + + + +# bitmap_or_count +## description +### Syntax + +`BigIntVal bitmap_or_count(BITMAP lhs, BITMAP rhs)` + +Calculates the union of two input bitmaps and returns the number of union sets. + +## example + +``` +MySQL> select bitmap_or_count(bitmap_from_string('1,2,3'),bitmap_empty()); ++--------------------------------------------------------------+ +| bitmap_or_count(bitmap_from_string('1,2,3'), bitmap_empty()) | ++--------------------------------------------------------------+ +| 3 | ++--------------------------------------------------------------+ + + +MySQL> select bitmap_or_count(bitmap_from_string('1,2,3'),bitmap_from_string('1,2,3')); ++---------------------------------------------------------------------------+ +| bitmap_or_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3')) | ++---------------------------------------------------------------------------+ +| 3 | ++---------------------------------------------------------------------------+ + +MySQL> select bitmap_or_count(bitmap_from_string('1,2,3'),bitmap_from_string('3,4,5')); ++---------------------------------------------------------------------------+ +| bitmap_or_count(bitmap_from_string('1,2,3'), bitmap_from_string('3,4,5')) | ++---------------------------------------------------------------------------+ +| 5 | ++---------------------------------------------------------------------------+ +``` + +## keyword + + BITMAP_OR_COUNT,BITMAP diff --git a/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_and_count.md b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_and_count.md new file mode 100644 index 0000000000..3d3fc28caa --- /dev/null +++ b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_and_count.md @@ -0,0 +1,63 @@ +--- +{ + "title": "bitmap_and_count", + "language": "zh-CN" +} +--- + + + +# bitmap_and_count +## description +### Syntax + +`BigIntVal bitmap_and_count(BITMAP lhs, BITMAP rhs)` + +计算两个输入bitmap的交集,返回交集的个数. + +## example + +``` +MySQL> select bitmap_and_count(bitmap_from_string('1,2,3'),bitmap_empty()); ++---------------------------------------------------------------+ +| bitmap_and_count(bitmap_from_string('1,2,3'), bitmap_empty()) | ++---------------------------------------------------------------+ +| 0 | ++---------------------------------------------------------------+ + + +MySQL> select bitmap_and_count(bitmap_from_string('1,2,3'),bitmap_from_string('1,2,3')); ++----------------------------------------------------------------------------+ +| bitmap_and_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3')) | ++----------------------------------------------------------------------------+ +| 3 | ++----------------------------------------------------------------------------+ + +MySQL> select bitmap_and_count(bitmap_from_string('1,2,3'),bitmap_from_string('3,4,5')); ++----------------------------------------------------------------------------+ +| bitmap_and_count(bitmap_from_string('1,2,3'), bitmap_from_string('3,4,5')) | ++----------------------------------------------------------------------------+ +| 1 | ++----------------------------------------------------------------------------+ +``` + +## keyword + + BITMAP_AND_COUNT,BITMAP diff --git a/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_or_count.md b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_or_count.md new file mode 100644 index 0000000000..8766d2ba4e --- /dev/null +++ b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_or_count.md @@ -0,0 +1,63 @@ +--- +{ + "title": "bitmap_or_count", + "language": "zh-CN" +} +--- + + + +# bitmap_or_count +## description +### Syntax + +`BigIntVal bitmap_or_count(BITMAP lhs, BITMAP rhs)` + +计算两个输入bitmap的并集,返回并集的个数. + +## example + +``` +MySQL> select bitmap_or_count(bitmap_from_string('1,2,3'),bitmap_empty()); ++--------------------------------------------------------------+ +| bitmap_or_count(bitmap_from_string('1,2,3'), bitmap_empty()) | ++--------------------------------------------------------------+ +| 3 | ++--------------------------------------------------------------+ + + +MySQL> select bitmap_or_count(bitmap_from_string('1,2,3'),bitmap_from_string('1,2,3')); ++---------------------------------------------------------------------------+ +| bitmap_or_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3')) | ++---------------------------------------------------------------------------+ +| 3 | ++---------------------------------------------------------------------------+ + +MySQL> select bitmap_or_count(bitmap_from_string('1,2,3'),bitmap_from_string('3,4,5')); ++---------------------------------------------------------------------------+ +| bitmap_or_count(bitmap_from_string('1,2,3'), bitmap_from_string('3,4,5')) | ++---------------------------------------------------------------------------+ +| 5 | ++---------------------------------------------------------------------------+ +``` + +## keyword + + BITMAP_OR_COUNT,BITMAP diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index eb3270c6ab..fab01d6f6c 100755 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -1217,7 +1217,13 @@ visible_functions = [ [['bitmap_subset_in_range'], 'BITMAP', ['BITMAP', 'BIGINT', 'BIGINT'], '_ZN5doris15BitmapFunctions22bitmap_subset_in_rangeEPN9doris_udf15FunctionContextERKNS1_9StringValERKNS1_9BigIntValES9_', '', '', 'vec', ''], - + [['bitmap_and_count'], 'BIGINT', ['BITMAP','BITMAP'], + '_ZN5doris15BitmapFunctions16bitmap_and_countEPN9doris_udf15FunctionContextERKNS1_9StringValES6_', + '', '', '', ''], + [['bitmap_or_count'], 'BIGINT', ['BITMAP','BITMAP'], + '_ZN5doris15BitmapFunctions15bitmap_or_countEPN9doris_udf15FunctionContextERKNS1_9StringValES6_', + '', '', '', ''], + # hash functions [['murmur_hash3_32'], 'INT', ['VARCHAR', '...'], '_ZN5doris13HashFunctions15murmur_hash3_32EPN9doris_udf15FunctionContextEiPKNS1_9StringValE',