Bitmap Functions
Bitmaps can be constructed in two ways. The first way is constructed by aggregation function groupBitmap with -State, the other way is to constructed a bitmap from an Array object.
bitmapBuild
Builds a bitmap from an unsigned integer array.
Syntax
bitmapBuild(array)
Arguments
array– Unsigned integer array.
Example
SELECT bitmapBuild([1, 2, 3, 4, 5]) AS res, toTypeName(res);
┌─res─┬─toTypeName(bitmapBuild([1, 2, 3, 4, 5]))─────┐
│ │ AggregateFunction(groupBitmap, UInt8) │
└─────┴──────────────────────────────────────────────┘
bitmapToArray
Converts bitmap to an integer array.
Syntax
bitmapToArray(bitmap)
Arguments
bitmap– Bitmap object.
Example
SELECT bitmapToArray(bitmapBuild([1, 2, 3, 4, 5])) AS res;
Result:
┌─res─────────┐
│ [1,2,3,4,5] │
└─────────────┘
bitmapSubsetInRange
Returns the subset of a bitmap with bits within a value interval.
Syntax
bitmapSubsetInRange(bitmap, range_start, range_end)
Arguments
bitmap– Bitmap object.range_start– Start of the range (inclusive). UInt32.range_end– End of the range (exclusive). UInt32.
Example
SELECT bitmapToArray(bitmapSubsetInRange(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))) AS res;
Result:
┌─res───────────────┐
│ [30,31,32,33,100] │
└─── ────────────────┘
bitmapSubsetLimit
Returns a subset of a bitmap with smallest bit value range_start and at most cardinality_limit elements.
Syntax
bitmapSubsetLimit(bitmap, range_start, cardinality_limit)
Arguments
bitmap– Bitmap object.range_start– Start of the range (inclusive). UInt32.cardinality_limit– Maximum cardinality of the subset. UInt32.
Example
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(30), toUInt32(200))) AS res;
Result:
┌─res───────────────────────┐
│ [30,31,32,33,100,200,500] │
└───────────────────────────┘
subBitmap
Returns a subset of the bitmap, starting from position offset. The maximum cardinality of the returned bitmap is cardinality_limit.
Syntax
subBitmap(bitmap, offset, cardinality_limit)
Arguments
bitmap– The bitmap. Bitmap object.offset– The position of the first element of the subset. UInt32.cardinality_limit– The maximum number of elements in the subset. UInt32.
Example
SELECT bitmapToArray(subBitmap(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:
┌─res─────────────────────────────┐
│ [10,11,12,13,14,15,16,17,18,19] │
└─────────────────────────────────┘
bitmapContains
Checks whether the bitmap contains an element.
bitmapContains(bitmap, needle)
Arguments
bitmap– Bitmap object.needle– Searched bit value. UInt32.
Returned values
Example
SELECT bitmapContains(bitmapBuild([1,5,7,9]), toUInt32(9)) AS res;
Result:
┌─res─┐
│ 1 │
└─────┘
bitmapHasAny
Checks whether two bitmaps intersect.
If bitmap2 contains exactly one element, consider using bitmapContains instead as it works more efficiently.
Syntax
bitmapHasAny(bitmap1, bitmap2)
Arguments
bitmap1– Bitmap object 1.bitmap2– Bitmap object 2.
Return values
1, ifbitmap1andbitmap2have at least one shared element.0, otherwise.
Example
SELECT bitmapHasAny(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
Result:
┌─res─┐
│ 1 │