Article Directory
1. The concept of bitmap function
The bitmap function is used to calculate two bitmap objects. For any bitmap function, the calculation result will return a bitmap object.
Second, the construction method of bitmap objects
There are two construction methods for bitmap objects. One is constructed by the aggregate function groupBitmapState, and the other is constructed by Array Object. At the same time, bitmap objects can be converted into array objects.
Three, the usage of bitmap function
(1) Construct a bitmap
bitmapBuild
Build a bitmap object from an array of unsigned integers
bitmapBuild(array)
Note: array-unsigned integer array
select bitmapBuild([1, 2, 3, 4, 5]) as result
(2) Bitmap object is converted to array object
bitmapToArray
Convert bitmap to integer array
bitmapToArray(bitmap)
Note: bitmap-bitmap object
select bitmapToArray(bitmapBuild([1, 2, 3, 4, 5])) as result
(3) The attributes of the bitmap object
1.bitmapContains
Check whether the bitmap contains the specified element
bitmapContains(haystack, needle)
Note:
haystack – bitmap object
needle – element, type UInt32
select bitmapContains(bitmapBuild([1,5,7,9]), toUInt32(9)) as result
2.bitmapCardinality
Returns a UInt64 type value, representing the cardinality of the bitmap object
bitmapCardinality(bitmap)
Note: bitmap-bitmap object
select bitmapCardinality(bitmapBuild([1, 2, 3, 4, 5])) as result
3.bitmapMin
Returns a UInt64 type value, representing the minimum value in the bitmap. If the bitmap is empty, return UINT32_MAX
bitmapMin(bitmap)
Note: bitmap-bitmap object
select bitmapMin(bitmapBuild([1, 2, 3, 4, 5])) as result
4.bitmapMax
Returns a UInt64 type value, which represents the maximum value in the bitmap. If the bitmap is empty, return 0
bitmapMax(bitmap)
Note: bitmap-bitmap object
select bitmapMax(bitmapBuild([1, 2, 3, 4, 5])) as result
(4) Convert bitmap to new bitmap
1.bitmapSubsetInRange
Convert the specified range of the bitmap (not including range_end) to another bitmap
bitmapSubsetInRange(bitmap, range_start, range_end)
Note:
bitmap – bitmap object
range_start – range start point (inclusive)
range_end – range end point (not included)
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 result
2.bitmapSubsetLimit
Convert the specified range (start point and upper limit of the number) of the bitmap to another bitmap
bitmapSubsetLimit(bitmap, range_start, limit)
Note:
bitmap – bitmap object
range_start – range starting point (inclusive)
limit – upper limit of the base of the sub-bitmap
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 result
(5) Bitmap operation
1.bitmapHasAny
Similar to hasAny(array, array), it returns 1 if the bitmap has any common elements, otherwise it returns 0.
For empty bitmaps, 0 is returned.
bitmapHasAny(bitmap,bitmap)
Note: bitmap – bitmap object
select bitmapHasAny(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) as result
2.bitmapHasAll
Similar to hasAll(array, array), if the first bitmap contains all the elements of the second bitmap, it returns 1, otherwise it returns 0.
If the second parameter is an empty bitmap, it returns 1.
bitmapHasAll(bitmap,bitmap)
Note: bitmap – bitmap object
select bitmapHasAll(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) as result
3.bitmapAnd
Perform AND operation for two bitmap objects and return a new bitmap object
bitmapAnd(bitmap1,bitmap2)
Note:
bitmap1-bitmap object
bitmap2-bitmap object
select bitmapToArray(bitmapAnd(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) as result
4.bitmapOr
Perform an OR operation for two bitmap objects and return a new bitmap object
bitmapOr(bitmap1,bitmap2)
Note:
bitmap1-bitmap object
bitmap2-bitmap object
select bitmapToArray(bitmapOr(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) as result
5.bitmapXor
Perform exclusive OR operation for two bitmap objects and return a new bitmap object
bitmapXor(bitmap1,bitmap2)
Note:
bitmap1-bitmap object
bitmap2-bitmap object
select bitmapToArray(bitmapXor(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) as result
6.bitmapAndnot
Calculate the difference between two bitmaps and return a new bitmap object
bitmapAndnot(bitmap1,bitmap2)
Note:
bitmap1-bitmap object
bitmap2-bitmap object
select bitmapToArray(bitmapAndnot(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) as result
7.bitmapAndCardinality
Perform AND operation for two bitmap objects and return the cardinality of the result bitmap
bitmapAndCardinality(bitmap1,bitmap2)
Note:
bitmap1-bitmap object
bitmap2-bitmap object
select bitmapAndCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) as result
8.bitmapOrCardinality
Perform an OR operation for two bitmaps and return the base of the result bitmap
bitmapOrCardinality(bitmap1,bitmap2)
Note:
bitmap1-bitmap object
bitmap2-bitmap object
select bitmapOrCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) as result
9.bitmapXorCardinality
Perform XOR operation for two bitmaps and return the base of the result bitmap
bitmapXorCardinality(bitmap1,bitmap2)
Note:
bitmap1-bitmap object
bitmap2-bitmap object
select bitmapXorCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) as result
10.bitmapAndnotCardinality
Calculate the difference between two bitmaps and return the cardinality of the resulting bitmap
bitmapAndnotCardinality(bitmap1,bitmap2)
Note:
bitmap1-bitmap object
bitmap2-bitmap object
select bitmapAndnotCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) as result
Four, summary
There are two construction methods for bitmap objects. One is constructed by the aggregate function groupBitmapState, and the other is constructed by Array Object. At the same time, bitmap objects can be converted into array objects. For any bitmap function, the calculation result will return a bitmap object. The value returned by the judgment function with Has is the logical value 0 or 1; the value returned by the function with Cardinality suffix is the base of the result bitmap after the operation; other bitmap operations return the result bitmap, such as And, Or, Xor, Andnot, etc.