diff --git a/be/src/exprs/bitmap_function.cpp b/be/src/exprs/bitmap_function.cpp index fe70a4871a..6abbee447f 100644 --- a/be/src/exprs/bitmap_function.cpp +++ b/be/src/exprs/bitmap_function.cpp @@ -519,6 +519,11 @@ StringVal BitmapFunctions::bitmap_xor(FunctionContext* ctx, const StringVal& lhs return serialize(ctx, &bitmap); } +BigIntVal BitmapFunctions::bitmap_xor_count(FunctionContext* ctx, const StringVal& lhs, + const StringVal& rhs) { + return bitmap_count(ctx, bitmap_xor(ctx, lhs, rhs)); +} + StringVal BitmapFunctions::bitmap_not(FunctionContext* ctx, const StringVal& lhs, const StringVal& rhs) { if (lhs.is_null || rhs.is_null) { diff --git a/be/src/exprs/bitmap_function.h b/be/src/exprs/bitmap_function.h index 9143a51bcd..89cf0d9a0b 100644 --- a/be/src/exprs/bitmap_function.h +++ b/be/src/exprs/bitmap_function.h @@ -57,6 +57,8 @@ public: static BigIntVal bitmap_count(FunctionContext* ctx, const StringVal& src); static BigIntVal bitmap_and_not_count(FunctionContext* ctx, const StringVal& src, const StringVal& dst); + static BigIntVal bitmap_xor_count(FunctionContext* ctx, const StringVal& src, + 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); diff --git a/be/test/exprs/bitmap_function_test.cpp b/be/test/exprs/bitmap_function_test.cpp index 686d2df5bc..437985812a 100644 --- a/be/test/exprs/bitmap_function_test.cpp +++ b/be/test/exprs/bitmap_function_test.cpp @@ -347,6 +347,59 @@ TEST_F(BitmapFunctionsTest, bitmap_and) { ASSERT_EQ(expected, result); } +TEST_F(BitmapFunctionsTest, bitmap_xor) { + BitmapValue bitmap1({1024, 1, 2019}); + BitmapValue bitmap2({33, 44, 2019}); + + StringVal bitmap_src = convert_bitmap_to_string(ctx, bitmap1); + StringVal bitmap_dst = convert_bitmap_to_string(ctx, bitmap2); + + StringVal bitmap_str = BitmapFunctions::bitmap_xor(ctx, bitmap_src, bitmap_dst); + BigIntVal result = BitmapFunctions::bitmap_count(ctx, bitmap_str); + + BigIntVal expected(4); + ASSERT_EQ(expected, result); +} + +TEST_F(BitmapFunctionsTest, bitmap_xor_count) { + { + BitmapValue bitmap1({1, 2, 3}); + BitmapValue bitmap2({3, 4, 5}); + + StringVal bitmap_src = convert_bitmap_to_string(ctx, bitmap1); + StringVal bitmap_dst = convert_bitmap_to_string(ctx, bitmap2); + + BigIntVal result = BitmapFunctions::bitmap_xor_count(ctx, bitmap_src, bitmap_dst); + + BigIntVal expected(4); + ASSERT_EQ(expected, result); + } + { + BitmapValue bitmap1({1, 2, 3}); + BitmapValue bitmap2({1, 2, 3}); + + StringVal bitmap_src = convert_bitmap_to_string(ctx, bitmap1); + StringVal bitmap_dst = convert_bitmap_to_string(ctx, bitmap2); + + BigIntVal result = BitmapFunctions::bitmap_xor_count(ctx, bitmap_src, bitmap_dst); + + BigIntVal expected(0); + ASSERT_EQ(expected, result); + } + { + BitmapValue bitmap1({1, 2, 3}); + BitmapValue bitmap2({4, 5, 6}); + + StringVal bitmap_src = convert_bitmap_to_string(ctx, bitmap1); + StringVal bitmap_dst = convert_bitmap_to_string(ctx, bitmap2); + + BigIntVal result = BitmapFunctions::bitmap_xor_count(ctx, bitmap_src, bitmap_dst); + + BigIntVal expected(6); + ASSERT_EQ(expected, result); + } +} + TEST_F(BitmapFunctionsTest, bitmap_and_count) { BitmapValue bitmap1({0, 1, 2}); BitmapValue bitmap2; diff --git a/docs/.vuepress/sidebar/en.js b/docs/.vuepress/sidebar/en.js index 09013ee5a5..e6c4e3b774 100644 --- a/docs/.vuepress/sidebar/en.js +++ b/docs/.vuepress/sidebar/en.js @@ -418,6 +418,7 @@ module.exports = [ "bitmap_and_count", "bitmap_or_count", "bitmap_xor", + "bitmap_xor_count", "bitmap_not", "bitmap_and_not", "bitmap_and_not_count", diff --git a/docs/.vuepress/sidebar/zh-CN.js b/docs/.vuepress/sidebar/zh-CN.js index e0e495df92..a21315d2f9 100644 --- a/docs/.vuepress/sidebar/zh-CN.js +++ b/docs/.vuepress/sidebar/zh-CN.js @@ -422,6 +422,7 @@ module.exports = [ "bitmap_and_count", "bitmap_or_count", "bitmap_xor", + "bitmap_xor_count", "bitmap_not", "bitmap_and_not", "bitmap_and_not_count", diff --git a/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_xor_count.md b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_xor_count.md new file mode 100644 index 0000000000..e3d0a6ffa3 --- /dev/null +++ b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_xor_count.md @@ -0,0 +1,62 @@ +--- +{ + "title": "bitmap_xor_count", + "language": "en" +} +--- + + + +# bitmap_xor_count + +## description + +### Syntax + +`BIGINT BITMAP_XOR_COUNT(BITMAP lhs, BITMAP rhs)` + +XOR two bitmap sets and return the size of the result set. + +## example + +``` +mysql> select bitmap_xor_count(bitmap_from_string('1,2,3'),bitmap_from_string('3,4,5')); ++----------------------------------------------------------------------------+ +| bitmap_xor_count(bitmap_from_string('1,2,3'), bitmap_from_string('3,4,5')) | ++----------------------------------------------------------------------------+ +| 4 | ++----------------------------------------------------------------------------+ + +mysql> select bitmap_xor_count(bitmap_from_string('1,2,3'),bitmap_from_string('1,2,3')); ++----------------------------------------------------------------------------+ +| bitmap_xor_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3')) | ++----------------------------------------------------------------------------+ +| 0 | ++----------------------------------------------------------------------------+ + +mysql> select bitmap_xor_count(bitmap_from_string('1,2,3'),bitmap_from_string('4,5,6')); ++----------------------------------------------------------------------------+ +| bitmap_xor_count(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6')) | ++----------------------------------------------------------------------------+ +| 6 | ++----------------------------------------------------------------------------+ +``` + +## keyword + + BITMAP_XOR_COUNT,BITMAP diff --git a/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_xor_count.md b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_xor_count.md new file mode 100644 index 0000000000..4f7574665a --- /dev/null +++ b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_xor_count.md @@ -0,0 +1,63 @@ +--- +{ + "title": "bitmap_xor_count", + "language": "zh-CN" +} +--- + + + +# bitmap_xor_count + +## description + +### Syntax + +`BIGINT BITMAP_XOR_COUNT(BITMAP lhs, BITMAP rhs)` + +将两个bitmap集合进行异或操作并返回结果集的大小 + +## example + +``` +mysql> select bitmap_xor_count(bitmap_from_string('1,2,3'),bitmap_from_string('3,4,5')); ++----------------------------------------------------------------------------+ +| bitmap_xor_count(bitmap_from_string('1,2,3'), bitmap_from_string('3,4,5')) | ++----------------------------------------------------------------------------+ +| 4 | ++----------------------------------------------------------------------------+ + +mysql> select bitmap_xor_count(bitmap_from_string('1,2,3'),bitmap_from_string('1,2,3')); ++----------------------------------------------------------------------------+ +| bitmap_xor_count(bitmap_from_string('1,2,3'), bitmap_from_string('1,2,3')) | ++----------------------------------------------------------------------------+ +| 0 | ++----------------------------------------------------------------------------+ + +mysql> select bitmap_xor_count(bitmap_from_string('1,2,3'),bitmap_from_string('4,5,6')); ++----------------------------------------------------------------------------+ +| bitmap_xor_count(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6')) | ++----------------------------------------------------------------------------+ +| 6 | ++----------------------------------------------------------------------------+ +``` + +## keyword + + BITMAP_XOR_COUNT,BITMAP + diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index fab01d6f6c..87d0e0c2a5 100755 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -1181,6 +1181,9 @@ visible_functions = [ [['bitmap_xor'], 'BITMAP', ['BITMAP','BITMAP'], '_ZN5doris15BitmapFunctions10bitmap_xorEPN9doris_udf15FunctionContextERKNS1_9StringValES6_', '', '', 'vec', ''], + [['bitmap_xor_count'], 'BIGINT', ['BITMAP','BITMAP'], + '_ZN5doris15BitmapFunctions16bitmap_xor_countEPN9doris_udf15FunctionContextERKNS1_9StringValES6_', + '', '', '', ''], [['bitmap_not'], 'BITMAP', ['BITMAP','BITMAP'], '_ZN5doris15BitmapFunctions10bitmap_notEPN9doris_udf15FunctionContextERKNS1_9StringValES6_', '', '', 'vec', ''],