add bitmapSubsetOffsetLimit function

This commit is contained in:
董海镔 2021-08-06 00:44:07 +08:00
parent d852207b0e
commit e8b6c0a227
7 changed files with 157 additions and 1 deletions

View File

@ -125,6 +125,44 @@ Result:
└───────────────────────────┘
```
## bitmapSubsetOffsetLimit {#bitmapsubsetoffsetlimit}
Creates a subset of bitmap limit the results to `cardinality_limit` with offset of `offset`.
**Syntax**
``` sql
bitmapSubsetOffsetLimit(bitmap, offset, cardinality_limit)
```
**Arguments**
- `bitmap` [Bitmap object](#bitmap_functions-bitmapbuild).
- `offset` the number of offsets. Type: [UInt32](../../sql-reference/data-types/int-uint.md).
- `cardinality_limit` The subset cardinality upper limit. Type: [UInt32](../../sql-reference/data-types/int-uint.md).
**Returned value**
The subset.
Type: `Bitmap object`.
**Example**
Query:
``` sql
SELECT bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,100,200,500]), toUInt32(10), toUInt32(10))) AS res;
```
Result:
``` text
┌─res─────────────────────────────┐
│ [10,11,12,13,14,15,16,17,18,19] │
└─────────────────────────────────┘
```
## bitmapContains {#bitmap_functions-bitmapcontains}
Checks whether the bitmap contains an element.

View File

@ -88,6 +88,30 @@ SELECT bitmapToArray(bitmapSubsetLimit(bitmapBuild([0,1,2,3,4,5,6,7,8,9,10,11,12
│ [30,31,32,33,100,200,500] │
└───────────────────────────┘
## bitmapSubsetOffsetLimit {#bitmapsubsetoffsetlimit}
将位图跳过`offset`个元素,限制大小为`limit`个的结果转换为另一个位图。
bitmapSubsetOffsetLimit(bitmap, offset, limit)
**参数**
- `bitmap` 位图对象.
- `offset` 跳过多少个元素.
- `limit` 子位图基数上限.
**示例**
``` sql
SELECT bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,100,200,500]), toUInt32(10), toUInt32(10))) AS res
```
```text
┌─res─────────────────────────────┐
│ [10,11,12,13,14,15,16,17,18,19] │
└─────────────────────────────────┘
```
## bitmapContains {#bitmapcontains}
检查位图是否包含指定元素。

View File

@ -579,6 +579,57 @@ public:
}
}
UInt64 rb_offset_limit(UInt64 offset, UInt64 limit, RoaringBitmapWithSmallSet & r1) const
{
if (limit == 0 || offset >= size())
return 0;
if (isSmall())
{
UInt64 offset_count = 0;
std::vector<T> answer;
for (const auto & x : small)
{
T val = x.getValue();
if (offset_count >= offset)
{
answer.push_back(val);
} else {
offset_count++;
}
}
if (limit < answer.size())
{
std::nth_element(answer.begin(), answer.begin() + limit, answer.end());
answer.resize(limit);
}
for (const auto & elem : answer)
r1.add(elem);
return answer.size();
}
else
{
UInt64 count = 0;
UInt64 offset_count = 0;
for (auto it = rb->begin(); it != rb->end(); ++it)
{
offset_count++;
if (offset_count <= offset)
continue;
if (count < limit)
{
r1.add(*it);
++count;
}
else
break;
}
return count;
}
}
UInt64 rb_min() const
{
if (isSmall())

View File

@ -13,6 +13,7 @@ void registerFunctionsBitmap(FunctionFactory & factory)
factory.registerFunction<FunctionBitmapToArray>();
factory.registerFunction<FunctionBitmapSubsetInRange>();
factory.registerFunction<FunctionBitmapSubsetLimit>();
factory.registerFunction<FunctionBitmapSubsetOffsetLimit>();
factory.registerFunction<FunctionBitmapTransform>();
factory.registerFunction<FunctionBitmapSelfCardinality>();

View File

@ -460,9 +460,24 @@ public:
}
};
struct BitmapSubsetOffsetLimitImpl
{
public:
static constexpr auto name = "bitmapSubsetOffsetLimit";
template <typename T>
static void apply(
const AggregateFunctionGroupBitmapData<T> & bitmap_data_0,
UInt64 range_start,
UInt64 range_end,
AggregateFunctionGroupBitmapData<T> & bitmap_data_2)
{
bitmap_data_0.rbs.rb_offset_limit(range_start, range_end, bitmap_data_2.rbs);
}
};
using FunctionBitmapSubsetInRange = FunctionBitmapSubset<BitmapSubsetInRangeImpl>;
using FunctionBitmapSubsetLimit = FunctionBitmapSubset<BitmapSubsetLimitImpl>;
using FunctionBitmapSubsetOffsetLimit = FunctionBitmapSubset<BitmapSubsetOffsetLimitImpl>;
class FunctionBitmapTransform : public IFunction
{

View File

@ -91,6 +91,14 @@ tag4 [0,1,2,3,4,5,6,7,8,9] [5,999,2] [2,888,20] [0,1,3,4,6,7,8,9,20]
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,100,200,500]
[30,31,32,33,100,200,500]
[100,200,500]
[]
[]
[1,5,7,9]
[5,7,9]
[5,7]
[0,1,2,3,4,5,6,7,8,9]
[30,31,32,33,100,200,500]
[100,200,500]
0
0
0

View File

@ -286,6 +286,25 @@ select bitmapToArray(bitmapSubsetLimit(bitmapBuild([
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,
100,200,500]), toUInt32(100), toUInt16(200)));
-- bitmapSubsetOffsetLimit:
---- Empty
SELECT bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild(emptyArrayUInt32()), toUInt8(0), toUInt32(10)));
SELECT bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild(emptyArrayUInt16()), toUInt32(0), toUInt64(10)));
---- Small
select bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild([1,5,7,9]), toUInt8(0), toUInt32(4)));
select bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild([1,5,7,9]), toUInt32(1), toUInt64(4)));
select bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild([1,5,7,9]), toUInt16(1), toUInt32(2)));
---- Large
select bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild([
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,
100,200,500]), toUInt32(0), toUInt32(10)));
select bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild([
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,
100,200,500]), toUInt32(30), toUInt32(200)));
select bitmapToArray(bitmapSubsetOffsetLimit(bitmapBuild([
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,
100,200,500]), toUInt32(34), toUInt16(3)));
-- bitmapMin:
---- Empty
SELECT bitmapMin(bitmapBuild(emptyArrayUInt8()));