Support bitmap_intersect (#3571)
* Support bitmap_intersect
Support aggregate function Bitmap Intersect, it is mainly used to take intersection of grouped data.
The function 'bitmap_intersect(expr)' calculates the intersection of bitmap columns and returns a bitmap object.
The defination is following:
FunctionName: bitmap_intersect,
InputType: bitmap,
OutputType: bitmap
The scenario is as follows:
Query which users satisfy the three tags a, b, and c at the same time.
```
select bitmap_to_string(bitmap_intersect(user_id)) from
(
select bitmap_union(user_id) user_id from bitmap_intersect_test
where tag in ('a', 'b', 'c')
group by tag
) a
```
Closed #3552.
* Add docs of bitmap_union and bitmap_intersect
* Support null of bitmap_intersect
This commit is contained in:
@ -302,6 +302,31 @@ void BitmapFunctions::bitmap_union(FunctionContext* ctx, const StringVal& src, S
|
||||
}
|
||||
}
|
||||
|
||||
// the dst value could be null
|
||||
void BitmapFunctions::nullable_bitmap_init(FunctionContext* ctx, StringVal* dst) {
|
||||
dst->is_null = true;
|
||||
}
|
||||
|
||||
void BitmapFunctions::bitmap_intersect(FunctionContext* ctx, const StringVal& src, StringVal* dst) {
|
||||
if (src.is_null) {
|
||||
return;
|
||||
}
|
||||
// if dst is null, the src input is the first value
|
||||
if (dst->is_null) {
|
||||
dst->is_null = false;
|
||||
dst->len = sizeof(BitmapValue);
|
||||
dst->ptr = (uint8_t*)new BitmapValue((char*) src.ptr);
|
||||
return;
|
||||
}
|
||||
auto dst_bitmap = reinterpret_cast<BitmapValue*>(dst->ptr);
|
||||
// zero size means the src input is a agg object
|
||||
if (src.len == 0) {
|
||||
(*dst_bitmap) &= *reinterpret_cast<BitmapValue*>(src.ptr);
|
||||
} else {
|
||||
(*dst_bitmap) &= BitmapValue((char*) src.ptr);
|
||||
}
|
||||
}
|
||||
|
||||
BigIntVal BitmapFunctions::bitmap_count(FunctionContext* ctx, const StringVal& src) {
|
||||
if (src.is_null) {
|
||||
return 0;
|
||||
@ -343,12 +368,17 @@ StringVal BitmapFunctions::bitmap_hash(doris_udf::FunctionContext* ctx, const do
|
||||
}
|
||||
|
||||
StringVal BitmapFunctions::bitmap_serialize(FunctionContext* ctx, const StringVal& src) {
|
||||
if (src.is_null) {
|
||||
return src;
|
||||
}
|
||||
|
||||
auto src_bitmap = reinterpret_cast<BitmapValue*>(src.ptr);
|
||||
StringVal result = serialize(ctx, src_bitmap);
|
||||
delete src_bitmap;
|
||||
return result;
|
||||
}
|
||||
|
||||
// This is a init function for intersect_count not for bitmap_intersect.
|
||||
template<typename T, typename ValType>
|
||||
void BitmapFunctions::bitmap_intersect_init(FunctionContext* ctx, StringVal* dst) {
|
||||
dst->is_null = false;
|
||||
@ -510,6 +540,7 @@ template void BitmapFunctions::bitmap_update_int<IntVal>(
|
||||
template void BitmapFunctions::bitmap_update_int<BigIntVal>(
|
||||
FunctionContext* ctx, const BigIntVal& src, StringVal* dst);
|
||||
|
||||
// this is init function for intersect_count not for bitmap_intersect
|
||||
template void BitmapFunctions::bitmap_intersect_init<int8_t, TinyIntVal>(
|
||||
FunctionContext* ctx, StringVal* dst);
|
||||
template void BitmapFunctions::bitmap_intersect_init<int16_t, SmallIntVal>(
|
||||
|
||||
Reference in New Issue
Block a user