Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions be/src/exprs/bitmap_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions be/src/exprs/bitmap_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class BitmapFunctions {
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
Expand Down
57 changes: 57 additions & 0 deletions be/src/util/bitmap_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,63 @@ class BitmapValue {
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) {
Expand Down
55 changes: 55 additions & 0 deletions docs/en/sql-reference/sql-functions/bitmap-functions/bitmap_xor.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,8 @@
'_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'],
Expand Down