This is an automated email from the ASF dual-hosted git repository. morningman pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-doris.git
The following commit(s) were added to refs/heads/master by this push: new 05ac7fc [Function] Add BE udf bitmap_xor (#5098) 05ac7fc is described below commit 05ac7fcd4a4d97642013407224d7165e4badffd6 Author: lihuigang <35155212+lihuig...@users.noreply.github.com> AuthorDate: Mon Jan 4 09:27:46 2021 +0800 [Function] Add BE udf bitmap_xor (#5098) this function will return the xor result of inputs two bitmap . --- be/src/exprs/bitmap_function.cpp | 21 ++++++++ be/src/exprs/bitmap_function.h | 1 + be/src/util/bitmap_value.h | 57 ++++++++++++++++++++++ .../sql-functions/bitmap-functions/bitmap_xor.md | 55 +++++++++++++++++++++ .../sql-functions/bitmap-functions/bitmap_xor.md | 55 +++++++++++++++++++++ gensrc/script/doris_builtins_functions.py | 2 + 6 files changed, 191 insertions(+) diff --git a/be/src/exprs/bitmap_function.cpp b/be/src/exprs/bitmap_function.cpp index 76267bb..f0c0e6f 100644 --- a/be/src/exprs/bitmap_function.cpp +++ b/be/src/exprs/bitmap_function.cpp @@ -477,6 +477,27 @@ StringVal BitmapFunctions::bitmap_and(FunctionContext* ctx, const StringVal& lhs return serialize(ctx, &bitmap); } +StringVal BitmapFunctions::bitmap_xor(FunctionContext* ctx, const StringVal& lhs, + const StringVal& rhs) { + if (lhs.is_null || rhs.is_null) { + return StringVal::null(); + } + BitmapValue bitmap; + if (lhs.len == 0) { + bitmap |= *reinterpret_cast<BitmapValue*>(lhs.ptr); + } else { + bitmap |= BitmapValue((char*)lhs.ptr); + } + + if (rhs.len == 0) { + bitmap ^= *reinterpret_cast<BitmapValue*>(rhs.ptr); + } else { + bitmap ^= BitmapValue((char*)rhs.ptr); + } + return serialize(ctx, &bitmap); +} + + StringVal BitmapFunctions::bitmap_to_string(FunctionContext* ctx, const StringVal& input) { if (input.is_null) { return StringVal::null(); diff --git a/be/src/exprs/bitmap_function.h b/be/src/exprs/bitmap_function.h index fa110cb..21fbd89 100644 --- a/be/src/exprs/bitmap_function.h +++ b/be/src/exprs/bitmap_function.h @@ -60,6 +60,7 @@ public: static StringVal to_bitmap(FunctionContext* ctx, const StringVal& src); static StringVal bitmap_hash(FunctionContext* ctx, const StringVal& src); static StringVal bitmap_or(FunctionContext* ctx, const StringVal& src, const StringVal& dst); + static StringVal bitmap_xor(FunctionContext* ctx, const StringVal& src, const StringVal& dst); static StringVal bitmap_and(FunctionContext* ctx, const StringVal& src, const StringVal& dst); static StringVal bitmap_to_string(FunctionContext* ctx, const StringVal& input); // Convert a comma separated string to a Bitmap diff --git a/be/src/util/bitmap_value.h b/be/src/util/bitmap_value.h index 9ccace7..f1ff0d1 100644 --- a/be/src/util/bitmap_value.h +++ b/be/src/util/bitmap_value.h @@ -1087,6 +1087,63 @@ public: return *this; } + // Compute the symmetric union between the current bitmap and the provided bitmap. + // Possible type transitions are: + // SINGLE -> EMPTY + // BITMAP -> EMPTY + // BITMAP -> SINGLE + BitmapValue& operator^=(const BitmapValue& rhs) { + switch (rhs._type) { + case EMPTY: + break; + case SINGLE: + switch (_type) { + case EMPTY: + add(rhs._sv); + break; + case SINGLE: + if (_sv == rhs._sv) { + _type = EMPTY; + _bitmap.clear(); + } else { + add(rhs._sv); + } + break; + case BITMAP: + if (!_bitmap.contains(rhs._sv)) { + add(rhs._sv); + } else { + _bitmap.remove(rhs._sv); + } + break; + } + break; + case BITMAP: + switch (_type) { + case EMPTY: + _bitmap = rhs._bitmap; + _type = BITMAP; + break; + case SINGLE: + _bitmap = rhs._bitmap; + _type = BITMAP; + if (!rhs._bitmap.contains(_sv)) { + _bitmap.add(_sv); + } else { + _bitmap.remove(_sv); + } + break; + case BITMAP: + _bitmap ^= rhs._bitmap; + _convert_to_smaller_type(); + break; + } + break; + } + return *this; + } + + // check if value x is present bool contains(uint64_t x) { switch (_type) { diff --git a/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_xor.md b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_xor.md new file mode 100644 index 0000000..2734f36 --- /dev/null +++ b/docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_xor.md @@ -0,0 +1,55 @@ +--- +{ + "title": "bitmap_xor", + "language": "en" +} +--- + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +# bitmap_xor +## description +### Syntax + +`BITMAP BITMAP_XOR(BITMAP lhs, BITMAP rhs)` + +Compute the symmetric union of two input bitmaps, return the new bitmap. + +## example + +``` +mysql> select bitmap_count(bitmap_xor(bitmap_from_string('2,3'),bitmap_from_string('1,2,3,4'))) cnt; ++------+ +| cnt | ++------+ +| 2 | ++------+ + +mysql> select bitmap_to_string(bitmap_xor(bitmap_from_string('2,3'),bitmap_from_string('1,2,3,4'))); ++----------------------------------------------------------------------------------------+ +| bitmap_to_string(bitmap_xor(bitmap_from_string('2,3'), bitmap_from_string('1,2,3,4'))) | ++----------------------------------------------------------------------------------------+ +| 1,4 | ++----------------------------------------------------------------------------------------+ +``` + +## keyword + + BITMAP_XOR,BITMAP diff --git a/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_xor.md b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_xor.md new file mode 100644 index 0000000..3e67e4f --- /dev/null +++ b/docs/zh-CN/sql-reference/sql-functions/bitmap-functions/bitmap_xor.md @@ -0,0 +1,55 @@ +--- +{ + "title": "bitmap_xor", + "language": "zh-CN" +} +--- + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +# bitmap_xor +## description +### Syntax + +`BITMAP BITMAP_XOR(BITMAP lhs, BITMAP rhs)` + +计算两个输入bitmap的差集,返回新的bitmap. + +## example + +``` +mysql> select bitmap_count(bitmap_xor(bitmap_from_string('2,3'),bitmap_from_string('1,2,3,4'))) cnt; ++------+ +| cnt | ++------+ +| 2 | ++------+ + +mysql> select bitmap_to_string(bitmap_xor(bitmap_from_string('2,3'),bitmap_from_string('1,2,3,4'))); ++----------------------------------------------------------------------------------------+ +| bitmap_to_string(bitmap_xor(bitmap_from_string('2,3'), bitmap_from_string('1,2,3,4'))) | ++----------------------------------------------------------------------------------------+ +| 1,4 | ++----------------------------------------------------------------------------------------+ +``` + +## keyword + + BITMAP_XOR,BITMAP diff --git a/gensrc/script/doris_builtins_functions.py b/gensrc/script/doris_builtins_functions.py index cb31d40..67742ba 100755 --- a/gensrc/script/doris_builtins_functions.py +++ b/gensrc/script/doris_builtins_functions.py @@ -800,6 +800,8 @@ visible_functions = [ '_ZN5doris15BitmapFunctions12bitmap_emptyEPN9doris_udf15FunctionContextE'], [['bitmap_or'], 'BITMAP', ['BITMAP','BITMAP'], '_ZN5doris15BitmapFunctions9bitmap_orEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'], + [['bitmap_xor'], 'BITMAP', ['BITMAP','BITMAP'], + '_ZN5doris15BitmapFunctions10bitmap_xorEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'], [['bitmap_and'], 'BITMAP', ['BITMAP','BITMAP'], '_ZN5doris15BitmapFunctions10bitmap_andEPN9doris_udf15FunctionContextERKNS1_9StringValES6_'], [['bitmap_to_string'], 'VARCHAR', ['BITMAP'], --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org