diff --git a/be/src/exprs/bitmap_function.cpp b/be/src/exprs/bitmap_function.cpp index 8ca25aa684..5566109b61 100644 --- a/be/src/exprs/bitmap_function.cpp +++ b/be/src/exprs/bitmap_function.cpp @@ -597,6 +597,19 @@ BooleanVal BitmapFunctions::bitmap_has_any(FunctionContext* ctx, const StringVal return {bitmap.cardinality() != 0}; } +BigIntVal BitmapFunctions::bitmap_max(FunctionContext* ctx, const StringVal& src) { + if (src.is_null) { + return BigIntVal::null(); + } + + if (src.len == 0) { + return reinterpret_cast(src.ptr)->maximum(); + } else { + auto bitmap = BitmapValue((char*)src.ptr); + return bitmap.maximum(); + } +} + template void BitmapFunctions::bitmap_update_int(FunctionContext* ctx, const TinyIntVal& src, StringVal* dst); template void BitmapFunctions::bitmap_update_int(FunctionContext* ctx, diff --git a/be/src/exprs/bitmap_function.h b/be/src/exprs/bitmap_function.h index 9739b3559e..5aca41f020 100644 --- a/be/src/exprs/bitmap_function.h +++ b/be/src/exprs/bitmap_function.h @@ -91,6 +91,7 @@ public: static StringVal bitmap_intersect_serialize(FunctionContext* ctx, const StringVal& src); template static BigIntVal bitmap_intersect_finalize(FunctionContext* ctx, const StringVal& src); + static BigIntVal bitmap_max(FunctionContext* ctx, const StringVal& str); }; } // namespace doris #endif //DORIS_BE_SRC_QUERY_EXPRS_BITMAP_FUNCTION_H diff --git a/be/src/util/bitmap_value.h b/be/src/util/bitmap_value.h index d974eb3130..6f9a2fadfa 100644 --- a/be/src/util/bitmap_value.h +++ b/be/src/util/bitmap_value.h @@ -1439,6 +1439,18 @@ public: return ss.str(); } + + doris_udf::BigIntVal maximum() { + switch (_type) { + case SINGLE: + return doris_udf::BigIntVal(_sv); + case BITMAP: + return doris_udf::BigIntVal(_bitmap.maximum()); + default: + return doris_udf::BigIntVal::null(); + } + } + private: void _convert_to_smaller_type() { if (_type == BITMAP) { diff --git a/be/test/exprs/bitmap_function_test.cpp b/be/test/exprs/bitmap_function_test.cpp index 130398ccea..98bae2a559 100644 --- a/be/test/exprs/bitmap_function_test.cpp +++ b/be/test/exprs/bitmap_function_test.cpp @@ -446,6 +446,32 @@ TEST_F(BitmapFunctionsTest, bitmap_from_string) { } } +TEST_F(BitmapFunctionsTest, bitmap_max) { + BigIntVal result = BitmapFunctions::bitmap_max(ctx, StringVal::null()); + ASSERT_TRUE(result.is_null); + + BitmapValue bitmap1; + StringVal empty_str = convert_bitmap_to_string(ctx, bitmap1); + result = BitmapFunctions::bitmap_max(ctx, empty_str); + ASSERT_TRUE(result.is_null); + + BitmapValue bitmap2 = BitmapValue(1024); + StringVal bitmap_str = convert_bitmap_to_string(ctx, bitmap2); + result = BitmapFunctions::bitmap_max(ctx, bitmap_str); + ASSERT_EQ(BigIntVal(1024), result); + + BitmapValue bitmap3 = BitmapValue({1024, 1}); + bitmap_str = convert_bitmap_to_string(ctx, bitmap3); + result = BitmapFunctions::bitmap_max(ctx, bitmap_str); + ASSERT_EQ(BigIntVal(1024), result); + + BitmapValue bitmap4 = BitmapValue({1024, 3, 2}); + bitmap_str = convert_bitmap_to_string(ctx, bitmap4); + result = BitmapFunctions::bitmap_max(ctx, bitmap_str); + ASSERT_EQ(BigIntVal(1024), result); +} + + } // namespace doris int main(int argc, char** argv) { diff --git a/docs/.vuepress/sidebar/en.js b/docs/.vuepress/sidebar/en.js index cbab2d236f..3d82689471 100644 --- a/docs/.vuepress/sidebar/en.js +++ b/docs/.vuepress/sidebar/en.js @@ -420,6 +420,7 @@ module.exports = [ "bitmap_union", "bitmap_xor", "to_bitmap", + "bitmap_max", ], }, { diff --git a/docs/.vuepress/sidebar/zh-CN.js b/docs/.vuepress/sidebar/zh-CN.js index 4b75292f49..d482ea928d 100644 --- a/docs/.vuepress/sidebar/zh-CN.js +++ b/docs/.vuepress/sidebar/zh-CN.js @@ -424,6 +424,7 @@ module.exports = [ "bitmap_union", "bitmap_xor", "to_bitmap", + "bitmap_max", ], }, { diff --git a/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_max.md b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_max.md new file mode 100644 index 0000000000..9edff1aa65 --- /dev/null +++ b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_max.md @@ -0,0 +1,55 @@ +--- +{ + "title": "bitmap_max", + "language": "en" +} +--- + + + +# bitmap_max +## description +### Syntax + +`BIGINT BITMAP_MAX(BITMAP input)` + +Calculate and return the max values of a bitmap. + +## example + +``` +mysql> select bitmap_max(bitmap_from_string('')) value; ++-------+ +| value | ++-------+ +| NULL | ++-------+ + +mysql> select bitmap_max(bitmap_from_string('1,9999999999')) value; ++------------+ +| value | ++------------+ +| 9999999999 | ++------------+ +``` + +## keyword + + BITMAP_MAX,BITMAP diff --git a/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_max.md b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_max.md new file mode 100644 index 0000000000..e9b6e3772f --- /dev/null +++ b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_max.md @@ -0,0 +1,55 @@ +--- +{ + "title": "bitmap_max", + "language": "zh-CN" +} +--- + + + +# bitmap_max +## description +### Syntax + +`BIGINT BITMAP_MAX(BITMAP input)` + +计算并返回 bitmap 中的最大值. + +## example + +``` +mysql> select bitmap_max(bitmap_from_string('')) value; ++-------+ +| value | ++-------+ +| NULL | ++-------+ + +mysql> select bitmap_max(bitmap_from_string('1,9999999999')) value; ++------------+ +| value | ++------------+ +| 9999999999 | ++------------+ +``` + +## keyword + + BITMAP_MAX,BITMAP diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index 0a7a742079..89ef4317a7 100755 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -1202,6 +1202,9 @@ visible_functions = [ [['bitmap_min'], 'BIGINT', ['BITMAP'], '_ZN5doris15BitmapFunctions10bitmap_minEPN9doris_udf15FunctionContextERKNS1_9StringValE', '', '', 'vec', ''], + [['bitmap_max'], 'BIGINT', ['BITMAP'], + '_ZN5doris15BitmapFunctions10bitmap_maxEPN9doris_udf15FunctionContextERKNS1_9StringValE', + '', '', '', ''], # hash functions [['murmur_hash3_32'], 'INT', ['VARCHAR', '...'],