[Function] Add bitmap-xor-count function for doris (#6982)

Add bitmap-xor-count function for doris

relate to #6875
This commit is contained in:
xy720
2021-11-02 16:37:00 +08:00
committed by GitHub
parent f0a71a067b
commit aeec9c45e6
8 changed files with 190 additions and 0 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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;

View File

@ -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",

View File

@ -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",

View File

@ -0,0 +1,62 @@
---
{
"title": "bitmap_xor_count",
"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_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

View File

@ -0,0 +1,63 @@
---
{
"title": "bitmap_xor_count",
"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_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

View File

@ -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', ''],