[Function] add bitmap function of bitmap_has_all (#6918)

The 'bitmap_has_all' function returns true if the first bitmap contains all the elements of the second bitmap.
This commit is contained in:
luozenglin
2021-11-01 12:50:47 +08:00
committed by GitHub
parent 210625b358
commit c7a3116f98
8 changed files with 172 additions and 0 deletions

View File

@ -607,6 +607,24 @@ BooleanVal BitmapFunctions::bitmap_has_any(FunctionContext* ctx, const StringVal
return {bitmap.cardinality() != 0};
}
BooleanVal BitmapFunctions::bitmap_has_all(FunctionContext *ctx, const StringVal &lhs,
const StringVal &rhs) {
if (lhs.is_null || rhs.is_null) {
return BooleanVal::null();
}
if (lhs.len != 0 && rhs.len != 0) {
BitmapValue bitmap = BitmapValue(reinterpret_cast<char *>(lhs.ptr));
int64_t lhs_cardinality = bitmap.cardinality();
bitmap |= BitmapValue(reinterpret_cast<char *>(rhs.ptr));
return {bitmap.cardinality() == lhs_cardinality};
} else if (rhs.len != 0) {
return {false};
} else {
return {true};
}
}
BigIntVal BitmapFunctions::bitmap_max(FunctionContext* ctx, const StringVal& src) {
if (src.is_null) {
return BigIntVal::null();

View File

@ -80,6 +80,8 @@ public:
const BigIntVal& input);
static BooleanVal bitmap_has_any(FunctionContext* ctx, const StringVal& lhs,
const StringVal& rhs);
static BooleanVal bitmap_has_all(FunctionContext* ctx, const StringVal& lhs,
const StringVal& rhs);
// intersect count
template <typename T, typename ValType>

View File

@ -504,6 +504,41 @@ TEST_F(BitmapFunctionsTest, bitmap_has_any) {
ASSERT_EQ(expected2, result1);
}
TEST_F(BitmapFunctionsTest, bitmap_has_all) {
BitmapValue bitmap1({1, 4, 5, std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::min()});
BitmapValue bitmap2({4, std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::min()});
StringVal string_val1 = convert_bitmap_to_string(ctx, bitmap1);
StringVal string_val2 = convert_bitmap_to_string(ctx, bitmap2);
BooleanVal result = BitmapFunctions::bitmap_has_all(ctx, string_val1, string_val2);
ASSERT_EQ(BooleanVal{true}, result);
bitmap1 = BitmapValue({0, 1, 2});
bitmap2 = BitmapValue({0, 1, 2, std::numeric_limits<uint64_t>::max()});
string_val1 = convert_bitmap_to_string(ctx, bitmap1);
string_val2 = convert_bitmap_to_string(ctx, bitmap2);
result = BitmapFunctions::bitmap_has_all(ctx, string_val1, string_val2);
ASSERT_EQ(BooleanVal{false}, result);
bitmap1 = BitmapValue();
bitmap2 = BitmapValue({0, 1, 2});
string_val1 = convert_bitmap_to_string(ctx, bitmap1);
string_val2 = convert_bitmap_to_string(ctx, bitmap2);
result = BitmapFunctions::bitmap_has_all(ctx, string_val1, string_val2);
ASSERT_EQ(BooleanVal{false}, result);
bitmap1 = BitmapValue();
bitmap2 = BitmapValue();
string_val1 = convert_bitmap_to_string(ctx, bitmap1);
string_val2 = convert_bitmap_to_string(ctx, bitmap2);
result = BitmapFunctions::bitmap_has_all(ctx, string_val1, string_val2);
ASSERT_EQ(BooleanVal{true}, result);
bitmap1 = BitmapValue();
string_val1 = convert_bitmap_to_string(ctx, bitmap1);
result = BitmapFunctions::bitmap_has_all(ctx, string_val1, StringVal::null());
ASSERT_TRUE(result.is_null);
}
TEST_F(BitmapFunctionsTest, bitmap_from_string) {
FunctionUtils utils;
{

View File

@ -411,6 +411,7 @@ module.exports = [
"bitmap_empty",
"bitmap_from_string",
"bitmap_has_any",
"bitmap_has_all",
"bitmap_hash",
"bitmap_intersect",
"bitmap_or",

View File

@ -415,6 +415,7 @@ module.exports = [
"bitmap_empty",
"bitmap_from_string",
"bitmap_has_any",
"bitmap_has_all",
"bitmap_hash",
"bitmap_intersect",
"bitmap_or",

View File

@ -0,0 +1,56 @@
---
{
"title": "bitmap_has_all",
"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_has_all
## description
### Syntax
`B00LEAN BITMAP_HAS_ALL(BITMAP lhs, BITMAP rhs)`
Returns true if the first bitmap contains all the elements of the second bitmap.
Returns true if the second bitmap contains an empty element.
## example
```
mysql> select bitmap_has_all(bitmap_from_string("0, 1, 2"), bitmap_from_string("1, 2")) cnt;
+------+
| cnt |
+------+
| 1 |
+------+
mysql> select bitmap_has_all(bitmap_empty(), bitmap_from_string("1, 2")) cnt;
+------+
| cnt |
+------+
| 0 |
+------+
```
## keyword
BITMAP_HAS_ALL,BITMAP

View File

@ -0,0 +1,56 @@
---
{
"title": "bitmap_has_all",
"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_has_all
## description
### Syntax
`B00LEAN BITMAP_HAS_ALL(BITMAP lhs, BITMAP rhs)`
如果第一个bitmap包含第二个bitmap的全部元素,则返回true。
如果第二个bitmap包含的元素为空,返回true。
## example
```
mysql> select bitmap_has_all(bitmap_from_string("0, 1, 2"), bitmap_from_string("1, 2")) cnt;
+------+
| cnt |
+------+
| 1 |
+------+
mysql> select bitmap_has_all(bitmap_empty(), bitmap_from_string("1, 2")) cnt;
+------+
| cnt |
+------+
| 0 |
+------+
```
## keyword
BITMAP_HAS_ALL,BITMAP

View File

@ -1205,6 +1205,9 @@ visible_functions = [
[['bitmap_has_any'], 'BOOLEAN', ['BITMAP','BITMAP'],
'_ZN5doris15BitmapFunctions14bitmap_has_anyEPN9doris_udf15FunctionContextERKNS1_9StringValES6_',
'', '', 'vec', ''],
[['bitmap_has_all'], 'BOOLEAN', ['BITMAP','BITMAP'],
'_ZN5doris15BitmapFunctions14bitmap_has_allEPN9doris_udf15FunctionContextERKNS1_9StringValES6_',
'', '', 'vec', ''],
[['bitmap_min'], 'BIGINT', ['BITMAP'],
'_ZN5doris15BitmapFunctions10bitmap_minEPN9doris_udf15FunctionContextERKNS1_9StringValE',
'', '', 'vec', ''],