The ClickHouse community provides bitmap data Processing capabilities. For details, see Bitmap Functions | ClickHouse Docs. Around the same time, ByteHouse independently developed a set of types and related functions for Processing bitmap Data. There are certain similarities and differences between the two in Data storage and use methods. More Contrast and Intro about BitMap Type Data can be found in BitMap64 Type>#00956554 Document.
ByteHouse offers a rich set of function interfaces to facilitate Search of BitMap64 Type Data. These can be roughly categorized as Normal functions, Aggregate functions, and high-level Aggregate functions. The following provides the interface list and Instructions for ByteHouse's BitMap functions.
ByteHouse's BitMap Normal functions are Delivered a row Record or a BitMap64 object and Back a row As a result.
For this Part of the function interfaces, except for the first function arrayToBitmap, all others are consistent with the community function interfaces.
| | | | | | \
| Order No. | Function interface | parameter description | Return value | Function Description | |
|---|---|---|---|---|---|
| \ | |||||
| 1 | arrayToBitmap([x1, …]) | \ | |||
| -> bitmap | Array of UIntN Type | \ | |||
| N={8, 16, 32, 64} | BitMap64 object | \ | |||
| Construct a bitmap object from an array | |||||
| \ | |||||
| 2 | bitmapToArray(bitmap) | \ | |||
| -> [x1,...] | A BitMap64 object | UInt64 array | Convert a bitmap object to array Format | ||
| \ | |||||
| \ | |||||
| 3 | bitmapAnd(bitmap1, bitmap2) | \ | |||
| -> bitmap | Two BitMap64 objects | \ | |||
| BitMap64 object | bitmap intersection operation | ||||
| \ | |||||
| \ | |||||
| 4 | bitmapOr(bitmap1, bitmap2) | \ | |||
| -> bitmap | Two BitMap64 objects | BitMap64 object | bitmap union operation | ||
| \ | |||||
| 5 | bitmapXor(bitmap1, bitmap2) | \ | |||
| -> bitmap | Two BitMap64 objects | BitMap64 object | bitmap xor operation | ||
| \ | |||||
| 6 | bitmapAndnot(bitmap1, bitmap2) | \ | |||
| -> bitmap | Two BitMap64 objects | BitMap64 object | bitmap difference operation | \ | |
| \ | |||||
| 7 | bitmapCardinality(bitmap) | \ | |||
| -> integer | A BitMap64 object | UInt64 Numerical value | Number of items in the bitmap | \ | |
| \ | |||||
| 8 | bitmapMin(bitmap) | \ | |||
| -> integer | A BitMap64 object | UInt64 Numerical value | Min Element in the bitmap | \ | |
| \ | |||||
| 9 | bitmapMax(bitmap) | \ | |||
| -> integer | A BitMap64 object | UInt64 Numerical value | Max Element in the bitmap | \ | |
| \ | |||||
| 10 | bitmapAndCardinality | \ | |||
| (bitmap1, bitmap2) | \ | ||||
| -> integer | Two BitMap64 objects | UInt64 Numerical value | Number of items After bitmap intersection operation | ||
| \ | |||||
| 11 | bitmapOrCardinality | \ | |||
| (bitmap1, bitmap2) | \ | ||||
| -> integer | Two BitMap64 objects | UInt64 Numerical value | Number of items After bitmap union operation | ||
| \ | |||||
| 12 | bitmapXorCardinality | \ | |||
| (bitmap1, bitmap2) | \ | ||||
| -> integer | Two BitMap64 objects | UInt64 Numerical value | Number of items After bitmap xor operation | ||
| \ | |||||
| 13 | bitmapAndnotCardinality | \ | |||
| (bitmap1, bitmap2) | \ | ||||
| -> integer | Two BitMap64 objects | \ | |||
| UInt64 Numerical value | Number of items After bitmap difference operation | ||||
| \ | |||||
| 14 | bitmapContains(bitmap, integer) | \ | |||
| -> bool | A BitMap64 object | \ | |||
| UIntN Type Number, | \ | ||||
| N={8, 16, 32, 64} | UInt8 enumeration | \ | |||
| 1->true | \ | ||||
| 0->false | Check whether the bitmap contains the specified Element | ||||
| \ | |||||
| 15 | bitmapHasAll | \ | |||
| (bitmap, sub_bitmap) | \ | ||||
| -> bool | Two BitMap64 objects | UInt8 enumeration | \ | ||
| 1->true | \ | ||||
| 0->false | Check whether sub_bitmap is a subset of bitmap | \ | |||
| (returns 1 if equal) | |||||
| \ | |||||
| 16 | bitmapHasAny | \ | |||
| (bitmap1, bitmap2) | \ | ||||
| -> bool | Two BitMap64 objects | UInt8 enumeration | \ | ||
| 1->true | \ | ||||
| 0->false | Check whether the two bitmaps have an intersection | ||||
| \ | |||||
| 17 | bitmapSubsetInRange | \ | |||
| (bitmap, range_start, range_end) | \ | ||||
| -> bitmap | A BitMap64 object, two UIntN Numbers, indicating the Value range | \ | |||
| N={8, 16, 32, 64} | BitMap64 object | Check and return a bitmap composed of values in the bitmap that Meet expectation for the given Element Size range. | \ | ||
| The function filters Data and returns a bitmap in which all Elements are Bigger than or equals to range_start, and Smaller than range_end. | |||||
| \ | |||||
| 18 | bitmapSubsetLimit | \ | |||
| (bitmap, range_start, cardinality_limit) | \ | ||||
| -> bitmap | A BitMap64 object and two UIntN Numbers, indicating the index Scope and subset Size. | BitMap64 object | \ | ||
| Checks and returns a bitmap composed of no more than the specified Quantity of Elements, starting from the specified Element in the bitmap. | \ | ||||
| The function filters Data and returns a bitmap in which all Elements are Bigger than or equals to range_start, and the total Number of Elements does not exceed cardinality_limit. | \ | ||||
| \ | |||||
| 19 | \ | ||||
| subBitmap(bitmap, start_offset, length) | A BitMap64 object, | \ | |||
| Two UIntN type Numbers, | \ | ||||
| N={8, 16, 32, 64} | BitMap64 object | \ | |||
| Extracts Data from the bitmap within the specified index Scope, similar to subString. | \ | ||||
| Caution: start_offset starts From 0, representing the first valid Value stored, similar to an array index. Both start_offset and length are meaningless if they exceed the Number of Elements in the bitmap. |
Adds two bitmaps together, As a result is a new bitmap.
Syntax
bitmapAnd(bitmap,bitmap)
Param
bitmap– Bitmap object.Return value
Example
SELECT bitmapToArray(bitmapAnd(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res;
As a result:
┌─res─┐ │ [3] │ └─────┘
Calculates the Sum of two bitmaps and returns the cardinality of Type UInt64.
Syntax
bitmapAndCardinality(bitmap,bitmap)
Param
bitmap– Bitmap object.Return value
UInt64.Type: Uint64
Example
SELECT bitmapAndCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
As a result:
┌─res─┐ │ 1 │ └─────┘
The Sum of two bitmaps is not calculated, As a result is a new bitmap.
Syntax
bitmapAndnot(bitmap,bitmap)
Param
bitmap– Bitmap object.Return value
Type: Bitmap object.
Example
SELECT bitmapToArray(bitmapAndnot(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res;
As a result:
┌─res────┐ │ [1, 2] │ └────────┘ text
The and of two bitmaps is not calculated, returns the cardinality of Type UInt64.
Syntax
bitmapAndnotCardinality(bitmap,bitmap)
Param
bitmap– Bitmap object.Return value
UInt64.Type: UInt64
Example
SELECT bitmapAndnotCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
As a result:
┌─res─┐ │ 2 │ └─────┘
Builds a bitmap from an unsigned Integer set.
Syntax
bitmapBuild(array)
Param
array– unsigned Integer set.Return value
Type: Bitmap object.
Example
SELECT toTypeName(bitmapBuild([1, 2, 3, 4, 5]));
As a result:
┌─toTypeName(bitmapBuild([1, 2, 3, 4, 5]))─┐ │ AggregateFunction(groupBitmap, UInt8) │ └──────────────────────────────────────────┘
Returns the bitmap cardinality of Type UInt64.
Syntax
bitmapCardinality(bitmap)
Param
bitmap– Bitmap object.Return value
UInt64.Type: UInt64.
Example
SELECT bitmapCardinality(bitmapBuild([1, 2, 3, 4, 5])) AS res;
As a result:
┌─res─┐ │ 5 │ └─────┘
Checks whether the bitmap contains an Element.
Syntax
bitmapContains(haystack, needle)
Param
haystack– [Bitmap object], the function Search Of which.needle– Value the function searches for. Type: [UInt32].Return value
haystack does Not included needle.haystack contains needle.Type: UInt8.
Example
SELECT bitmapContains(bitmapBuild([1,5,7,9]), toUInt32(9)) AS res;
As a result:
┌─res─┐ │ 1 │ └─────┘
hasAll(array, array)If the first bitmap contains all Elements of the second bitmap, then returns 1; Otherwise returns 0.
If the second param is an empty bitmap, then returns 1.
Syntax
bitmapHasAll(bitmap,bitmap)
Param
bitmap– Bitmap object.Return value
1, Back 1 if the first bitmap contains all elements of the second bitmap or the second parameter is an empty bitmap.0, Otherwise.Type: UInt8
Example
SELECT bitmapHasAll(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
As a result:
┌─res─┐ │ 0 │ └─────┘
Check whether two bitmaps have an intersection of certain elements.
Syntax
bitmapHasAny(bitmap1, bitmap2)
If you Confirm bitmap2 only contains one Element, consider use the [bitmapContains] function. It works more efficiently.
Param
bitmap*– Bitmap object.Return value
1, if bitmap1 and bitmap2 have at least one similar Element.0, Otherwise.Example
SELECT bitmapHasAny(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
As a result:
┌─res─┐ │ 1 │ └─────┘
Back the Max Value of Type UInt64 in the set. If the set Is empty, Back 0.
Syntax
bitmapMax(bitmap)
Param
bitmap– Bitmap object.Return value
Type: UInt64
Example
SELECT bitmapMax(bitmapBuild([1, 2, 3, 4, 5])) AS res;
As a result:
┌─res─┐ │ 5 │ └─────┘
Back the Low Value of Type UInt64 in the set. If the set Is empty, Back UINT32_MAX.
Syntax
bitmapMin(bitmap)
Param
bitmap– Bitmap object.Return value
Type: UInt64
Example
SELECT bitmapMin(bitmapBuild([1, 2, 3, 4, 5])) AS res;
As a result:
┌─res─┐ │ 1 │ └─────┘
Carry on or Calculate for two bitmaps. The Result is a new bitmap.
bitmapOr(bitmap,bitmap)
Param
bitmap– Bitmap object.Return value
Type: Bitmap object.
Example
SELECT bitmapToArray(bitmapOr(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res;
As a result:
┌─res─────────────┐ │ [1, 2, 3, 4, 5] │ └─────────────────┘
Carry on or Calculate for two bitmaps, Back the cardinality of Type UInt64.
Syntax
bitmapOrCardinality(bitmap,bitmap)
Param
bitmap– Bitmap object.Return value
Type: UInt64
Example
SELECT bitmapOrCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
As a result:
┌─res─┐ │ 5 │ └─────┘
Back the subset within the specified Scope (excluding range_end).
Syntax
bitmapSubsetInRange(bitmap, range_start, range_end)
Param
bitmap– [Bitmap object].range_start– Scope start. Type: [UInt32].range_end– Scope end (excluded). Type: [UInt32].Return value
Type: array
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;
As a result:
┌─res───────────────────┐ │ [30, 31, 32, 33, 100] │ └───────────────────────┘
create a subset of the bitmap, Of which contains n items between range_start and cardinality_limit.
Syntax
bitmapSubsetLimit(bitmap, range_start, cardinality_limit)
Param
bitmap– [Bitmap object].range_start– Start of the subset. Type: [UInt32].cardinality_limit– Cap of subset cardinality. Type: [UInt32].Return value
Type: Bitmap object.
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;
As a result:
┌─res─────────────────────────────┐ │ [30, 31, 32, 33, 100, 200, 500] │ └─────────────────────────────────┘
Convert the bitmap to an Integer set.
Syntax
bitmapToArray(bitmap)
Param
bitmap– Bitmap object.Return value
Type: array.
Example
SELECT bitmapToArray(bitmapBuild([1, 2, 3, 4, 5])) AS res;
As a result:
┌─res─────────────┐ │ [1, 2, 3, 4, 5] │ └─────────────────┘
Carry on XOR Calculate for two bitmaps. The Result is a new bitmap.
bitmapXor(bitmap,bitmap)
Param
bitmap– Bitmap object.Return value
Type: Bitmap object.
Example
SELECT bitmapToArray(bitmapXor(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res;
As a result:
┌─res──────────┐ │ [1, 2, 4, 5] │ └──────────────┘
Carry on XOR Calculate for two bitmaps, Back the cardinality of Type UInt64.
Syntax
bitmapXorCardinality(bitmap,bitmap)
Param
bitmap– Bitmap object.Return value
Type: Bitmap object.
Example
SELECT bitmapXorCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
As a result:
┌─res─┐ │ 4 │ └─────┘
Extract subset data from the specified index range within the bitmap, starting from the offset position. The maximum cardinality of the returned bitmap is cardinality_limit.
Syntax
subBitmap(bitmap, offset, cardinality_limit)
Parameters
bitmap - [bitmap object]offset - Position of the first element in the subset, Type: UInt32cardinality_limit - Maximum cardinality of the subset, Type: UInt32Caution: offset starts from 0, representing the first valid value actually stored, similar to an array index. Both offset and cardinality_limit are meaningless if they exceed the number of elements in the bitmap.
Return value
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; --- Equivalent to the following ByteHouse BitMap64 type syntax SELECT bitmapToArray(subBitmap(arrayToBitmap([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
┌─res─────────────────────────────┐ │ [10,11,12,13,14,15,16,17,18,19] │ └─────────────────────────────────┘
The following aggregate functions receive multiple rows, perform aggregation, and return a single result.
| | | | | | \
| Order No. | Function interface | Parameter description | Return value | Function description | |
|---|---|---|---|---|---|
| \ | |||||
| 1 | \ | ||||
| bitmapColumnAnd | \ | ||||
| (bitmap_column) | \ | ||||
| -> bitmap | A column of BitMap64 type data | BitMap64 object | Receives a bitmap column and performs intersection operation on all bitmaps in the column. | ||
| \ | |||||
| 2 | bitmapColumnOr | \ | |||
| (bitmap_column) | \ | ||||
| -> bitmap | A column of BitMap64 type data | BitMap64 object | Receives a bitmap column and performs union operation on all bitmaps in the column. | \ | |
| \ | |||||
| 3 | bitmapColumnXor | \ | |||
| (bitmap_column) | \ | ||||
| -> bitmap | A column of BitMap64 type data | BitMap64 object | Receives a bitmap column and performs xor operation on all bitmaps in the column. | \ | |
| \ | |||||
| 4 | bitmapColumnCardinality | \ | |||
| (bitmap_column) | \ | ||||
| -> UInt64 | A column of BitMap64 type data | UInt64 numerical value | Receives a bitmap column, performs union operation on all bitmaps in the column, and returns the number of elements in the resulting bitmap. | ||
| \ | |||||
| 5 | bitmapColumnHas | \ | |||
| (bitmap_column, integer) | \ | ||||
| -> bool | A column of BitMap64 type data | \ | |||
| UIntN type number, | \ | ||||
| N={8, 16, 32, 64} | UInt8 enumeration | \ | |||
| 1->true | \ | ||||
| 0->false | Receives a bitmap column and checks whether the column contains the specified element. | \ | |||
| \ | |||||
| 6 | bitmapFromColumn(id) -> bitmap | Integer column | \ | ||
| UIntN type number, | \ | ||||
| N={8, 16, 32, 64} | BitMap64 object | Receives a column of integers and aggregates them into a bitmap. |
The following are specific function examples:
Calculates the intersection of a bitmap column.
Syntax
bitmapColumnAnd(bitmap_column)
Parametersbitmap_column - Data column of BitMap64 type.
Return value
Example
select bitmapColumnAnd(rbm) as result from ( select arrayToBitmap([1,2,3]) as rbm union all select arrayToBitmap([2,3,4]) as rbm )
Result:
┌─result─┐ │ {2,3} │ └────────┘
Calculates the union of a bitmap column.
Syntax
bitmapColumnOr(bitmap_column)
Parametersbitmap_column - Data column of BitMap64 type.
Return value
Example
select bitmapColumnOr(rbm) as result from ( select arrayToBitmap([1,2,3]) as rbm union all select arrayToBitmap([2,3,4]) as rbm )
Result:
┌─result────┐ │ {1,2,3,4} │ └───────────┘
Calculates the xor set of a bitmap column.
Syntax
bitmapColumnXor(bitmap_column)
Parametersbitmap_column - Data column of BitMap64 type.
Return value
Example
select bitmapColumnXor(rbm) as result from ( select arrayToBitmap([1,2,3]) as rbm union all select arrayToBitmap([2,3,4]) as rbm )
Result:
┌─result─┐ │ {1,4} │ └────────┘
Calculates the number of elements contained in the union of a bitmap column.
Syntax
bitmapColumnCardinality(bitmap_column)
Parametersbitmap_column - Data column of BitMap64 type.
Return value
Example
select bitmapColumnCardinality(rbm) as result from ( select arrayToBitmap([1,2,3]) as rbm union all select arrayToBitmap([2,3,4]) as rbm )
Result:
┌─result─┐ │ 4 │ └────────┘
Determines whether a bitmap column contains the specified numerical value.
Syntax
bitmapColumnHas(bitmap_column, integer_number)
Parametersbitmap_column - Data column of BitMap64 type.integer_number - A constant numerical value. Type: positive integer within 64 bits.
Return value
bitmapContains function is that this function returns only one row value for a bitmap column, while bitmapContains returns multiple rows.Example
select bitmapColumnHas(rbm, 3) as result from ( select arrayToBitmap([1,2,3]) as rbm union all select arrayToBitmap([2,3,4]) as rbm )
As a result:
┌─result─┐ │ 1 │ └────────┘
Construct a BitMap64 object from a column of numerical values.
Syntax
bitmapFromColumn(integer_column)
Param
integer_column - A column of positive integers within 64 bits.Return value
Example
select bitmapFromColumn(number) as res from numbers(10);
As a result:
┌─res───────────────────┐ │ {0,1,2,3,4,5,6,7,8,9} │ └───────────────────────┘
These functions use two sets of parentheses to pass parameters:
ByteHouse provides a series of aggregate functions for batch processing complex calculations between large numbers of bitmaps. By assigning an order number to each bitmap to be calculated and using the order number to express set intersection, union, and difference operations, ByteHouse can perform the corresponding bitmap operations according to the specified operation expression, directly visualizing and simplifying the calculation, greatly improving the SQL writing experience!
A simple illustration is as follows:
| | | | | | \
| Order No. | Function interface | parameter description | Return value | Function description | |
|---|---|---|---|---|---|
| \ | |||||
| 1 | bitmapCount | \ | |||
| (expression) | \ | ||||
| (idx, bitmap) | \ | ||||
| -> integer | \ | ||||
The expression is an intersection, union, or difference expression; & indicates intersection, | indicates union, ~ indicates difference | \ | ||||
| idx is the mark corresponding to each row's bitmap, use positive integer Int16/32 Type | \ | ||||
| UInt64 numerical value | For each row in the bitmap column, take its mark idx and store it in the map. Finally, perform bitmap operations according to the calculation method specified by the expression, and return the number of elements in the final bitmap. | \ | |||
| \ | |||||
| 2 | bitmapExtract | \ | |||
| (idx, bitmap) | \ | ||||
| -> bitmap | The expression is an intersection, union, or difference expression; & indicates intersection, | indicates union, ~ indicates difference | \ | |||
| idx is the mark corresponding to each row's bitmap, use positive integer Int16/32 Type | \ | ||||
| BitMap64 object | \ | ||||
| For each row in the bitmap column, take its idx, and perform bitmap operations according to the calculation method specified by the expression, returning the final result bitmap. | \ | ||||
| \ | |||||
| 3 | bitmapMultiCount | \ | |||
| (expr1, expr2, ...) | \ | ||||
| (idx, bitmap) | \ | ||||
| -> [int1, int2, ...] | \ | ||||
The expression is an intersection, union, or difference expression; & indicates intersection, | indicates union, ~ indicates difference | \ | ||||
| idx is the mark corresponding to each row's bitmap, use positive integer Int16/32 Type | \ | ||||
| UInt64 array | For each row in the bitmap column, take its order number idx, and perform multiple bitmap operations according to the calculation method specified by each expr, returning the number of elements in the bitmap resulting from each expr calculation. | \ | |||
| \ | |||||
| 4 | bitmapMultiExtract | \ | |||
| (expr1, expr2, ...) | \ | ||||
| (idx, bitmap) | \ | ||||
| -> [bitmap1, bitmap2, ...] | \ | ||||
The expression is an intersection, union, or difference expression; & indicates intersection, |represents union, ~ represents difference | \ | ||||
| idx is the Mark corresponding to each row of the bitmap, use positive integer Int16/32 Type | BitMap64 array | For each row in the bitmap Column, take its identifier idx, and perform multiple bitmap operations according to the calculation Details specified by each expr, returning the bitmap Data of the calculation results for each expr | |||
| \ | |||||
| 5 | bitmapMultiCountWithDate | \ | |||
| (expr1, expr2, ...) | \ | ||||
| (date, idx, bitmap) | \ | ||||
| -> [int1, int2, ...] | The Expression is an and, or, difference Expression, & represents intersection, |represents union, ~ represents difference | \ | |||
| date is a Date Format of Int64 Type | \ | ||||
| idx is the Mark corresponding to each row of the bitmap, use positive integer Int16/32 Type | UInt64 array | Based on bitmapMultiCount, the Date Dimension is added, allowing distinction of identifiers across different dates. That is, bitmapMultiCount only supports one identifier Dimension, while this function additionally adds a Date Dimension. | |||
| \ | |||||
| 6 | bitmapMultiExtractWithDate | \ | |||
| (expr1, expr2, ...) | \ | ||||
| (date, idx, bitmap) | \ | ||||
| -> [bitmap1, bitmap2, ...] | \ | ||||
The Expression is an and, or, difference Expression, & represents intersection, |represents union, ~ represents difference | \ | ||||
| date is a Date Format of Int64 Type | \ | ||||
| idx is the Mark corresponding to each row of the bitmap, use positive integer Int16/32 Type | BitMap64 array | \ | |||
| Based on bitmapMultiExtract, the Date Dimension is added, allowing distinction of Tags across different dates. That is, bitmapMultiExtract only supports one identifier Dimension, while this function additionally adds a Date Dimension. |
tip
If there are multiple rows with the same identifier idx, the Aggregate function will automatically Merge multiple bitmaps into one via an OR operation for calculation. In the final calculation, one idx corresponds to one bitmap.
The following are specific function Descriptions.
According to the calculation Expression specified by the User, Run the corresponding bitmap operation and Back the number of Elements in the final bitmap.
Syntax
BitMapCount(expr)(idx_column, bitmap_column)
Param
expr - The and (&), or (|), difference (~) operations expressed by Number or String, & represents bitmap intersection, | represents bitmap union, ~ represents bitmap difference; this function only supports one Expression.idx_column - The Data Column used to Mark the bitmap, which can be an Integer Column (usually Int16/32/64 Type), or a String Column.bitmap_column - BitMap64 Data Column.Note 1: It is not required for idx_column Data and bitmap_column Data to correspond one-to-one. If idx_column contains Repeated Data, multiple rows of bitmap with the same Value will first be unioned, Merged into a unique bitmap for calculation.
Note 2: When the idx_column Column Type is String, must not contain the following reserved characters: &, |, ~, ,, #, (, ), _, and English space.
Return value
Example
SELECT bitmapCount('a')(tag_value, uids) AS res FROM ( SELECT 'a' AS tag_value, arrayToBitmap([1, 2, 3]) AS uids )
As a result:
┌─res─┐ │ 3 │ └─────┘
SELECT BitmapCount('((a&b)|c) ~ d')(tag_value, uids) AS res1, BitmapCount('((1&2)|3)~4')(multiIf(tag_value = 'a', toInt16(1), tag_value = 'b', toInt16(2), tag_value = 'c', toInt16(3), tag_value = 'd', toInt16(4), toInt16(0)) AS idx, uids) AS res2 FROM ( SELECT 'a' AS tag_value, arrayToBitmap([1, 2, 3, 4, 5]) AS uids UNION ALL SELECT 'b' AS tag_value, arrayToBitmap([3, 4, 5, 6]) AS uids UNION ALL SELECT 'c' AS tag_value, arrayToBitmap([5, 7]) AS uids UNION ALL SELECT 'd' AS tag_value, arrayToBitmap([7]) AS uids )
As a result
┌─res1─┬─res2─┐ │ 3 │ 3 │ └──────┴──────┘
According to the calculation expression specified by the user, perform the corresponding bitmap operation and return the final bitmap.
Syntax
BitMapExtract(expr)(idx_column, bitmap_column)
Parameters
expr - Use numbers or strings to represent and (&), or (|), difference (~) operations. & indicates bitmap intersection, | indicates bitmap union, ~ indicates bitmap difference. This function only supports a single expression.idx_column - The data column used to mark the bitmap. It can be an integer column (usually of Int16/32/64 type) or a string column.bitmap_column - BitMap64 data column.warning
idx_column data and bitmap_column data to have a one-to-one correspondence. If idx_column contains duplicate data, multiple rows of bitmap with the same value will first be merged by union operation into a single bitmap for calculation.idx_column is of string type, it cannot contain the following reserved characters: &, |, ~, ,, #, (, ), _, and space character.Return value
Example
SELECT bitmapExtract('a')(tag_value, uids) AS res FROM ( SELECT 'a' AS tag_value, arrayToBitmap([1, 2, 3]) AS uids )
Result
┌─res─────┐ │ {1,2,3} │ └─────────┘
SELECT BitmapExtract('((a&b)|c) ~ d')(tag_value, uids) AS res1, BitmapExtract('((1&2)|3)~4')(multiIf(tag_value = 'a', toInt16(1), tag_value = 'b', toInt16(2), tag_value = 'c', toInt16(3), tag_value = 'd', toInt16(4), toInt16(0)) AS idx, uids) AS res2 FROM ( SELECT 'a' AS tag_value, arrayToBitmap([1, 2, 3, 4, 5]) AS uids UNION ALL SELECT 'b' AS tag_value, arrayToBitmap([3, 4, 5, 6]) AS uids UNION ALL SELECT 'c' AS tag_value, arrayToBitmap([5, 7]) AS uids UNION ALL SELECT 'd' AS tag_value, arrayToBitmap([7]) AS uids )
Result:
┌─res1────┬─res2────┐ │ {3,4,5} │ {3,4,5} │ └─────────┴─────────┘
According to a set of calculation expressions specified by the user, perform the corresponding bitmap operation for each group and return the number of elements in the final bitmap of each group.
Syntax
BitmapMultiCount(expr1, expr2, expr3,...)(idx_column, bitmap_column)
Parameters
expr - Use numbers or strings to represent and (&), or (|), difference (~) operations. & indicates bitmap intersection, | indicates bitmap union, ~ indicates bitmap difference. This function supports multiple expressions.idx_column - The data column used to mark the bitmap. It can be an integer column (usually of Int16/32/64 type) or a string column.bitmap_column - BitMap64 data column.warning
idx_column data and bitmap_column data to have a one-to-one correspondence. If idx_column contains duplicate data, multiple rows of bitmap with the same value will first be merged by union operation into a single bitmap for calculation.idx_column is of string type, it cannot contain the following reserved characters: &, |, ~, ,, #, (, ), _, and space character.expr1='a&b', expr2='(a&b)|c. Then expr2 can be simplified as expr2='_1|c'. Use underscore plus number to specify an already used expression, which can simplify the writing of complex expressions.Return value
Example
SELECT BitmapMultiCount('a')(tag_value, uids) AS res FROM ( SELECT 'a' AS tag_value, arrayToBitmap([1, 2, 3]) AS uids )
┌─res─┐ │ [3] │ └─────┘
SELECT BitmapMultiCount('1&2', '(1&2)|3', '_1|3', '((1&2)|3) ~ 4', '_2~4')(tag_value, uids) AS res FROM ( SELECT toInt32(1) AS tag_value, arrayToBitmap([1, 2, 3, 4, 5]) AS uids UNION ALL SELECT toInt32(2) AS tag_value, arrayToBitmap([3, 4, 5, 6]) AS uids UNION ALL SELECT toInt32(3) AS tag_value, arrayToBitmap([5, 7]) AS uids UNION ALL SELECT toInt32(4) AS tag_value, arrayToBitmap([7]) AS uids )
Result:
┌─res─────────┐ │ [3,4,4,3,3] │ └─────────────┘
According to a set of calculation expressions specified by the user, perform the corresponding bitmap operation for each group and return the final bitmap of each group.
Syntax
BitmapMultiExtract(expr1, expr2, expr3,...)(idx_column, bitmap_column)
Parameters
expr - Use numbers or strings to represent and (&), or (|), and difference (~) operations. & indicates bitmap intersection, | indicates bitmap union, and ~ indicates bitmap difference. This function supports multiple expressions.idx_column - Data column used to mark the bitmap. It can be an integer column (typically of int16/32/64 type), or a string column.bitmap_column - BitMap64 data column.warning
idx_column data and bitmap_column data to correspond one-to-one. If idx_column contains repeated data, multiple bitmap rows with the same value will first be merged using a union operation, resulting in a unique bitmap for calculation.idx_column column type is string, it cannot contain the following reserved characters: &, |, ~, ,, #, (, ), _, and space character.expr1='a&b', expr2='(a&b)|c. Expr2 can then be simplified as expr2='_1|c'. Using underscore plus number allows referencing an already used expression, which can simplify the writing of complex expressions.Return value
Example
SELECT BitmapMultiExtract('a')(tag_value, uids) AS res FROM ( SELECT 'a' AS tag_value, arrayToBitmap([1, 2, 3]) AS uids )
┌─res───────┐ │ [{1,2,3}] │ └───────────┘
SELECT BitmapMultiExtract('1&2', '(1&2)|3', '_1|3', '((1&2)|3) ~ 4', '_2~4')(tag_value, uids) AS res FROM ( SELECT toInt32(1) AS tag_value, arrayToBitmap([1, 2, 3, 4, 5]) AS uids UNION ALL SELECT toInt32(2) AS tag_value, arrayToBitmap([3, 4, 5, 6]) AS uids UNION ALL SELECT toInt32(3) AS tag_value, arrayToBitmap([5, 7]) AS uids UNION ALL SELECT toInt32(4) AS tag_value, arrayToBitmap([7]) AS uids )
As a result:
┌─res───────────────────────────────────────────┐ │ [{3,4,5},{3,4,5,7},{3,4,5,7},{3,4,5},{3,4,5}] │ └───────────────────────────────────────────────┘
According to a set of calculation expressions specified by the user, each group performs the corresponding bitmap operation and returns the number of elements in the final bitmap of each group. Compared to the BitmapMultiCount function, it adds a date dimension.
Syntax
BitmapMultiCountWithDate(expr1, expr2, expr3,...)(date_column, idx_column, bitmap_column)
Param
expr - Use numbers or strings to represent and (&), or (|), and difference (~) operations. & indicates bitmap intersection, | indicates bitmap union, and ~ indicates bitmap difference. This function supports multiple expressions. Note that each expression consists of two parts: one part is the date, and one part is the mark, connected by an underscore. For example: 20240101_aaa&20240102_aaa.date_colulmn - Used to specify the data date. Requires an int64 type date column in the format YYYYMMDD.idx_column - Data column used to mark the bitmap. It can be an integer column (typically of int16/32/64 type), or a string column.bitmap_column - BitMap64 data column.warning
date_column+idx_column data and bitmap_column data to correspond one-to-one. If date_column+idx_column contains repeated data, multiple bitmap rows with the same value will first be merged using a union operation, resulting in a unique bitmap for calculation.idx_column column type is string, it cannot contain the following reserved characters: &, |, ~, ,, #, (, ), _, and space character.expr1='a&b', expr2='(a&b)|c. Expr2 can then be simplified as expr2='_1|c'. Using an underline plus a number allows you to reference a previously defined expression, which can simplify the writing of complex expressions.Return value
Example
SELECT BitmapMultiCountWithDate('20240102_a~20240101_a', '20240101_a&20240102_a')(toInt64(toYYYYMMDD(date)), tag_value, uids) AS res FROM ( SELECT 'a' AS tag_value, '2024-01-01' AS date, arrayToBitmap([1, 2, 3]) AS uids UNION ALL SELECT 'a' AS tag_value, '2024-01-02' AS date, arrayToBitmap([2, 3, 4, 5, 6]) AS uids )
┌─res───┐ │ [3,2] │ └───────┘
SELECT BitmapMultiCountWithDate('20240101_1 & 20240102_2', '_1 | 20240101_3', '_2 ~ 20240101_4')(toInt64(toYYYYMMDD(date)), tag_value, uids) AS res FROM ( SELECT toInt32(1) AS tag_value, '2024-01-01' AS date, arrayToBitmap([1, 2, 3, 4, 5]) AS uids UNION ALL SELECT toInt32(2) AS tag_value, '2024-01-02' AS date, arrayToBitmap([3, 4, 5, 6]) AS uids UNION ALL SELECT toInt32(3) AS tag_value, '2024-01-01' AS date, arrayToBitmap([5, 7]) AS uids UNION ALL SELECT toInt32(4) AS tag_value, '2024-01-01' AS date, arrayToBitmap([7]) AS uids )
As a result:
┌─res─────┐ │ [3,4,3] │ └─────────┘
Based on a set of calculation expressions specified by the user, each group performs the corresponding bitmap operation and returns the final bitmap for each group. Compared to the BitmapMultiExtract function, it adds a date dimension.
Syntax
BitmapMultiExtractWithDate(expr1, expr2, expr3,...)(date_column, idx_column, bitmap_column)
Parameters
expr - and (&), or (|), and difference (~) operations expressed by numbers or strings; & represents bitmap intersection, | represents bitmap union, ~ represents bitmap difference. This function supports multiple expressions. Note that each expression consists of two parts: one part is the date, and the other part is the mark, connected by an underline. For example, 20240101_aaa&20240102_aaa.date_colulmn - Used to specify the data date. Requires an Int64 type date column in the format YYYYMMDD.idx_column - Used to mark the bitmap data column. Can be an integer column (usually Int16/32/64 type) or a string column.bitmap_column - BitMap64 data column.warning
date_column+idx_column data and bitmap_column data to correspond one-to-one. If date_column+idx_column contains repeated data, multiple rows with the same value will first be merged by union operation into a unique bitmap for calculation.idx_column column type is string, cannot contain the following reserved characters: &, |, ~, ,, #, (, ), _, and English space.expr1='a&b', expr2='(a&b)|c. Then expr2 can be simplified as expr2='_1|c'. Using an underline plus a number allows you to reference a previously defined expression, which can simplify the writing of complex expressions.Return value
Example
SELECT BitmapMultiExtractWithDate('20240102_a~20240101_a', '20240101_a&20240102_a')(toInt64(toYYYYMMDD(date)), tag_value, uids) AS res FROM ( SELECT 'a' AS tag_value, '2024-01-01' AS date, arrayToBitmap([1, 2, 3]) AS uids UNION ALL SELECT 'a' AS tag_value, '2024-01-02' AS date, arrayToBitmap([2, 3, 4, 5, 6]) AS uids )
┌─res─────────────┐ │ [{4,5,6},{2,3}] │ └─────────────────┘
SELECT BitmapMultiExtractWithDate('20240101_1 & 20240102_2', '_1 | 20240101_3', '_2 ~ 20240101_4')(toInt64(toYYYYMMDD(date)), tag_value, uids) AS res FROM ( SELECT toInt32(1) AS tag_value, '2024-01-01' AS date, arrayToBitmap([1, 2, 3, 4, 5]) AS uids UNION ALL SELECT toInt32(2) AS tag_value, '2024-01-02' AS date, arrayToBitmap([3, 4, 5, 6]) AS uids UNION ALL SELECT toInt32(3) AS tag_value, '2024-01-01' AS date, arrayToBitmap([5, 7]) AS uids UNION ALL SELECT toInt32(4) AS tag_value, '2024-01-01' AS date, arrayToBitmap([7]) AS uids )
As a result:
┌─res─────────────────────────┐ │ [{3,4,5},{3,4,5,7},{3,4,5}] │ └─────────────────────────────┘
| | | | | |
|No. |\
| Function interface | Parameter description | Return value | Brief explanation | ||
|---|---|---|---|---|---|
| \ | |||||
| 1 | \ | ||||
| EncodeNonBitEngineColumn(UInt64_column, database, table, dictionary,add_new_key_to_bitengine_dict) | \ | ||||
| -> UInt64 | \ | ||||
| The last parameter is an optional parameter for adding new entries. If omitted, the default value is 0. If needed, it must be included and set to 1. | UInt64_column: | \ | |||
| source input; | \ | ||||
| Database, table, dictionary: Specifies the dictionary source to use, as a string | \ | ||||
| add_new_key_to_bitengine_dict, how to handle IDs not found in the dictionary? Join the dictionary or discard? | \ | ||||
| UInt64 value | Use a normal UInt64 as the Key in the BitEngine K-V dictionary to obtain the value after dictionary encoding | \ | |||
| Add: | \ | ||||
| add_new_key_to_bitengine_dict controls how to process newly appeared IDs: | \ | ||||
| 0: Default Behaviour, can be omitted, does not Join the Dictionary; | \ | ||||
| 1: Add Behaviour, must be included, when encoding the Dictionary, if a new ID is encountered, it will be Joined into the Dictionary. | |||||
| \ | |||||
| 2 | DecodeNonBitEngineColumn(UInt64_column, database, table, dictionary) | \ | |||
| -> UInt64 | UInt64_column: | \ | |||
| source input; | \ | ||||
| Database, table, dictionary: specify the Dictionary source, String | \ | ||||
| UInt64 value | use a Normal UInt64 as the V in the BitEngine K-V Dictionary to obtain the Value before Dictionary encoding | \ | |||
| \ | |||||
| 3 | EncodeBitmap(bitmap, database, table, dictionary, add_new_key_to_bitengine_dict) | \ | |||
| -> BitMap64 | \ | ||||
| The last Param is a newly added optional Param. If omitted, the Default is 0. If needed, it must be included and set to 1 | bitmap: source input; | \ | |||
| Database, table, dictionary: specify the Dictionary source, String | \ | ||||
| BitMap64 object | use the BitEngine Dictionary to encode a bitmap | \ | |||
| Add: | \ | ||||
| add_new_key_to_bitengine_dict controls how to process newly appeared IDs: | \ | ||||
| 0: Default Behaviour, can be omitted, does not Join the Dictionary; | \ | ||||
| 1: Add Behaviour, must be included, when encoding the Dictionary, if a new ID is encountered, it will be Joined into the Dictionary. | |||||
| \ | |||||
| 4 | DecodeBitmap(bitmap, database, table, dictionary) | \ | |||
| ->BitMap64 | \ | ||||
| bitmap: source input; | \ | ||||
| Database, table, dictionary: strings identify the dictionary source | BitMap64 object | use the BitEngine Dictionary to decode a bitmap | |||
| \ | |||||
| 5 | arrayToBitmapWithEncode | \ | |||
| ([x1,...], 'db', 'tbl', 'dict_name', | \ | ||||
| add_new_key_to_bitengine_dict) | \ | ||||
| -> bitmap | \ | ||||
| \ | |||||
| The last Param is a newly added optional Param. If omitted, the Default is 0. If needed, it must be included and set to 1 | Array of UIntN Type, | \ | |||
| N={8, 16, 32, 64}, | \ | ||||
| db, tbl, dict_name correspond to database, table, and Column respectively | \ | ||||
| add_new_key_to_bitengine_dict controls how the Dictionary processes newly appeared IDs Join the Dictionary or discard | BitMap64 object | \ | |||
| use an Already existed BitEngine Dictionary to encode a new bitmap, Back the encoded bitmap | \ | ||||
| Add: | \ | ||||
| add_new_key_to_bitengine_dict controls how to process newly appeared IDs: | \ | ||||
| 0: Default Behaviour, can be omitted, does not Join the Dictionary; | \ | ||||
| 1: Add Behaviour, must be included, when encoding the Dictionary, if a new ID is encountered, it will be Joined into the Dictionary. | \ | ||||
| \ | |||||
| 6 | bitmapToArrayWithDecode(bitmap, 'db', 'tbl', 'dict_name') | \ | |||
| -> array | bitmap: source input; | \ | |||
| Database, table, dictionary: strings identify the dictionary source | Array(UInt64) | decode the bitmap, meanwhile convert the bitmap to an array |
If you previously used community BitMap functions and now wish to migrate to ByteHouse, you can refer to the following Rules to replace the community BitMap functions.
warning
ClickHouse/ByteHouse Support project Aggregate function Combine Operators, For details, see Aggregate Function Combinators. The usage Format is [Aggregate function name]+[Combinator]. Common Combinators include -If, -State. For example, groupBitmapOrState and groupBitmapOrStateIf, when replacing functions, only the groupBitmapOr function name needs to be considered, and the Combinator Part remains unchanged.
Replacement Rules for common Combinators:
-State: remove directly-If : keep-Merge: remove directlyExample:groupBitmapStateIf -> bitmapFromColumnIfgroupBitmapOrState -> bitmapColumnOr
ByteHouse adapts to and supports all ClickHouse community bitmap functions. The mapping relationships are as follows:
| | | | \
| ClickHouse community function name | ByteHouse equivalent implementation | Example | |
|---|---|---|---|
| \ | |||
| bitmapBuild | arrayToBitmap | Example: select arrayToBitmap([1,2,3]) | |
| \ | |||
| bitmapToArray | bitmapToArray | Example: select bitmapToArray(arrayToBitmap([1,2,3])) | |
| \ | |||
| bitmapSubsetInRange | bitmapSubsetInRange | Example: select bitmapSubsetInRange(arrayToBitmap([1,2,3]), 1, 2) | |
| \ | |||
| bitmapSubsetLimit | \ | ||
| bitmapSubsetLimit | Example: select bitmapSubsetLimit(arrayToBitmap([1,2,3]), 1, 1) | ||
| \ | |||
| subBitmap | subBitmap | Example: select subBitmap(arrayToBitmap([1,2,3]), 2, 2) | |
| \ | |||
| bitmapContains | bitmapContains | Example: select bitmapContains(arrayToBitmap([1,2,3]), 2) | |
| \ | |||
| bitmapHasAny | bitmapHasAny | Example: select bitmapHasAny(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | |
| \ | |||
| bitmapHasAll | bitmapHasAll | Example: select bitmapHasAll(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | |
| \ | |||
| bitmapCardinality | bitmapCardinality | Example: select bitmapCardinality(arrayToBitmap([1,2,3])) | |
| \ | |||
| bitmapMin | bitmapMin | Example: select bitmapMin(arrayToBitmap([1,2,3])) | |
| \ | |||
| bitmapMax | bitmapMax | Example: select bitmapMax(arrayToBitmap([1,2,3])) | |
| \ | |||
| bitmapTransform | bitmapTransform | Example: select bitmapTransform(arrayToBitmap([1,2,3]), [2,3], [3,4]) | |
| \ | |||
| bitmapAnd | bitmapAnd | Example: select bitmapAnd(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | |
| \ | |||
| bitmapOr | bitmapOr | Example: select bitmapOr(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | |
| \ | |||
| bitmapXor | bitmapXor | Example: select bitmapXor(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | |
| \ | |||
| bitmapAndnot | bitmapAndnot | Example: select bitmapAndnot(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | |
| \ | |||
| bitmapAndCardinality | \ | ||
| bitmapAndCardinality | Example: select bitmapAndCardinality(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | ||
| \ | |||
| bitmapOrCardinality | bitmapOrCardinality | Example: select bitmapOrCardinality(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | |
| \ | |||
| bitmapXorCardinality | bitmapXorCardinality | Example: select bitmapXorCardinality(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | |
| \ | |||
| bitmapAndnotCardinality | bitmapAndnotCardinality | Example: select bitmapAndnotCardinality(arrayToBitmap([1,2,3]), arrayToBitmap([2,3])) | |
| \ | |||
| groupBitmap | bitmapCardinality(bitmapFromColumn(number)) | \ | |
Example: select bitmapCardinality(bitmapFromColumn(number)) from numbers(10); | |||
| \ | |||
| groupBitmapState | bitmapFromColumn | Example: select bitmapFromColumn(number) from numbers(10); | |
| \ | |||
| groupBitmapOr | bitmapColumnCardinality | Example: select bitmapColumnCardinality(rbm) from (select bitmapFromColumn(number) as rbm from numbers(10)); | |
| \ | |||
| groupBitmapOrState | bitmapColumnOr | Example: select bitmapColumnOr(rbm) from (select bitmapFromColumn(number) as rbm from numbers(10)); | |
| \ | |||
| groupBitmapAnd | bitmapCardinality(bitmapColumnAnd(bitmap_column)) | Example: select bitmapCardinality(bitmapColumnAnd(rbm)) from (select bitmapFromColumn(number) as rbm from numbers(10)); | |
| \ | |||
| groupBitmapAndState | bitmapColumnAnd | Example: select bitmapColumnAnd(rbm) from (select bitmapFromColumn(number) as rbm from numbers(10)); | |
| \ | |||
| groupBitmapXor | bitmapCardinality(bitmapColumnXor(bitmap_column)) | \ | |
Example: select bitmapCardinality(bitmapColumnXor(rbm)) from (select bitmapFromColumn(number) as rbm from numbers(10)); | |||
| \ | |||
| groupBitmapXorState | bitmapColumnXor | Example: select bitmapColumnXor(rbm) from (select bitmapFromColumn(number) as rbm from numbers(10)); |
Example uses table test.bitmap_test.
CREATE TABLE test.bitmap_test ( `p_date` Date, `slice_id` UInt64, `tag_id` Int32, `tag_value` String, `uids` BitMap64 ) ENGINE = CnchMergeTree PARTITION BY p_date ORDER BY (tag_id, tag_value) SETTINGS index_granularity = 128;
Data involves two tags, tag_id=11, with two tag values 'a' and 'b'; tag_id=12, with one tag value 'c'.
Each tag value has two rows, with different slice_id values. slice_id is used to split large bitmaps, reducing the bitmap scale per row, which can optimize construction efficiency and memory usage, and meanwhile provide shard credentials for distributed storage.
insert into test.bitmap_test values ('2024-01-01', 0, 11, 'a', [1,2,3,4,5]), ('2024-01-02', 1, 11,'a', [11,12,13,14,15]); insert into test.bitmap_test values ('2024-01-01', 0, 11, 'b', [3,4,5,6,7,8]), ('2024-01-02', 1, 11,'b', [13, 14,15,16]); insert into test.bitmap_test values ('2024-01-01', 0, 12, 'c', [3,4,6,7,9]), ('2024-01-02', 1, 12,'c', [13,15,16,18]);
The use of normal functions, community demo, and other functions is not different. Here are a few examples.
SELECT arrayToBitmap([1, 2, 3, 4, 5]) FORMAT TSV -- As a result: {1,2,3,4,5}
SELECT bitmapToArray(uids) FROM test.bitmap_test WHERE tag_value = 'a' FORMAT TSV -- As a result: [11,12,13,14,15] [1,2,3,4,5] SELECT bitmapToArray(arrayToBitmap([1, 2, 3, 4, 5])) FORMAT TSV -- As a result: [1,2,3,4,5]
SELECT bitmapCardinality(arrayToBitmap([1, 2, 3, 4, 5])) FORMAT TSV -- As a result: 5
-- Calculate the intersection of two bitmaps SELECT bitmapColumnAnd(uids) FROM test.bitmap_test WHERE (slice_id = 0) AND (tag_value IN ('a', 'b')) FORMAT TSV -- As a result: {3,4,5} -- Calculate the union of two bitmaps (two functions are synonymous) SELECT bitmapCardinality(bitmapColumnOr(uids)), bitmapColumnCardinality(uids) FROM test.bitmap_test WHERE (slice_id = 0) AND (tag_value IN ('a', 'b')) FORMAT TSV -- As a result: 8 8 -- Judge whether a given element exists in a bitmap SELECT bitmapColumnHas(uids, 12) FROM test.bitmap_test WHERE tag_value = 'a' FORMAT TSV -- As a result: 1
SELECT bitmapFromColumn(number) FROM ( SELECT number FROM system.numbers LIMIT 100, 10 ) FORMAT TSV -- As a result {100,101,102,103,104,105,106,107,108,109}
purpose:Carry on complex intersection, union, and difference operations between multiple bitmaps. User Pass input expression to represent bitmap operations, and the function parses and calculates accordingly. Example refers to the legend in the section [Aggregate functions] - [Advanced aggregate functions] above.
Input:Index column, bitmap column. The index column is used to identify the bitmap, which can be a field in the table (such as tag_id), or a manually defined variable (such as idx).
Expression:One index anchors one bitmap. The index expression is and, or, and difference operations, corresponding to intersection, union, and difference operations of bitmaps.
Supported index operators:
Output:The number of elements in the resulting bitmap after intersection, union, and difference operations.
Case 1: Calculate the total number of elements for a certain tag 'a':
SELECT bitmapColumnCardinality(uids) FROM test.bitmap_test WHERE tag_value = 'a' FORMAT TSV -- As a result: 10 -- Calculate the total number of elements for tag 'a'. Equivalent to the calculation of bitmapColumnCardinality SELECT bitmapCount('1')(If(tag_value = 'a', toInt32(1), toInt32(999)) AS idx, uids) AS card FROM test.bitmap_test WHERE tag_value = 'a' FORMAT TSV -- As a result: 10 -- Calculate the total number of elements for tag 'a'. If the tag is a string, you can directly use a string expression SELECT bitmapCount('a')(tag_value, uids) FROM test.bitmap_test WHERE tag_value = 'a' FORMAT TSV -- As a result: 10
Case 2: Calculate the intersection of two tags 'a' and 'b'
SELECT bitmapCardinality(bitmapColumnAnd(uids)) AS card FROM test.bitmap_test WHERE (tag_value IN ('a', 'b')) AND (slice_id = 0) FORMAT CSV -- As a result: 10 -- Equivalent to the calculation of bitmapCardinality(bitmapColumnAnd) SELECT bitmapCount('1&2')(multiIf(tag_value = 'a', toInt32(1), tag_value = 'b', 2, toInt32(999)) AS idx, uids) AS card FROM test.bitmap_test WHERE (tag_value IN ('a', 'b')) AND (slice_id = 0) FORMAT TSV -- As a result: 3 -- Another way to write the above SQL, suitable for complex SQL nesting SELECT bitmapCount('1&2')(idx, uids) FROM ( SELECT toInt32(1) AS idx, uids FROM test.bitmap_test WHERE (slice_id = 0) AND (tag_value IN ('a')) UNION ALL SELECT toInt32(2) AS idx, uids FROM test.bitmap_test WHERE (slice_id = 0) AND (tag_value IN ('b')) ) FORMAT TSV -- As a result: 3
Case 3: More Tag intersection, union, and difference operations
-- Method one SELECT bitmapCount('1&2|3')(multiIf(tag_value = 'a', toInt32(1), tag_value = 'b', toInt32(2), tag_value = 'c', toInt32(3), toInt32(999)) AS idx, uids) AS card FROM test.bitmap_test FORMAT TSV -- As a result: 11 -- Method two: Since tag_value is of String Type, you can use a String expression SELECT bitmapCount('a&b|c')(tag_value, uids) FROM test.bitmap_test FORMAT TSV -- As a result: 11
This function calculates in the same way as bitmapCount, except it Back the Result bitmap, not the count.
As for how to Combine Tags, it is equivalent to the bitmapCount function. bitmapCount uses tag_value as an Example, here is a case using tag_id as an Example: finding the intersection between two Tags:
SELECT bitmapExtract('1&2')(multiIf(tag_id = 11, toInt32(1), tag_id = 12, 2, toInt32(999)) AS idx, uids) AS card FROM test.bitmap_test WHERE (tag_id IN (1,2)) AND (slice_id = 0) FORMAT TSV -- As a result (no intersection): {} Another way to write: SELECT bitmapExtract('11&12')(tag_id, uids) FROM test.bitmap_test WHERE (tag_id IN (1, 2)) AND (slice_id = 0) FORMAT TSV -- As a result (no intersection): {}
This Aggregate function is based on bitmapCount, Support project for multiple groups of Expressions.
purpose:To realize complex intersection, union, and difference operations between multiple bitmaps, meanwhile supporting multiple groups of operations and operation reuse. The User expresses bitmap operations by Inputting Expressions, and the function performs the corresponding calculation after Analysis. For Examples, refer to the Legend in the section 【Aggregate function】- 【Advanced Aggregate function】above.
Input:Number Column, bitmap Column. The Number Column is used to identify the bitmap, which can be a Field in the table (such as tag_id) or a manually defined variable (such as idx).
Expression:One Number anchors one bitmap. The Number Expression involves and, or, and Poor Actions, corresponding to intersection, union, and difference operations of the bitmap. Meanwhile, you can provide multiple groups of Expressions, and the function will Run the corresponding calculation in sequence and return all results together.
Supported Number Operators:
Output:The number of Elements in the Result bitmap after intersection, union, and difference operations. Each Expression corresponds to one Result, stored in an array.
-- '_3|3' is equivalent to '(1&2)|3' SELECT bitmapMultiCount('1', '2', '1&2', '_3|3')(multiIf(tag_value = 'a', toInt32(1), tag_value = 'b', toInt32(2), tag_value = 'c', toInt32(3), toInt32(999)) AS idx, uids) AS card FROM test.bitmap_test WHERE slice_id = 0 FORMAT TSV -- As a result: [5,6,3,6] -- tag_value happens to be of String Type, so you can also use a String expression SELECT bitmapMultiCount('a', 'b', 'a&b', '_3|c')(tag_value, uids) AS card FROM test.bitmap_test WHERE slice_id = 0 FORMAT TSV -- As a result: [5,6,3,6]
_3 means reusing the Expression at Location 3, that is, '1&2'.
There is a set of set relationships that can be used to verify correctness. This relationship is
A + B = AUB + A Poor B
SELECT bitmapMultiCount('1', '2', '1&2', '1|2')(multiIf(tag_value = 'a', toInt32(1), tag_value = 'b', toInt32(2), tag_value = 'c', toInt32(3), toInt32(999)) AS idx, uids) AS card FROM test.bitmap_test WHERE slice_id = 0 FORMAT TSV -- As a result: [5,6,3,8] -- tag_value happens to be of String Type, so a string expression can also be used SELECT bitmapMultiCount('a', 'b', 'a&b', 'a|b')(tag_value, uids) FROM test.bitmap_test WHERE slice_id = 0 FORMAT TSV -- As a result: [5,6,3,8]
The meaning for the User is the same as bitmapMultiCount. The difference is in the Return value, which is Back as bitmap Data.
SELECT bitmapMultiExtract('1', '2', '1&2', '1|2')(multiIf(tag_value = 'a', toInt32(1), tag_value = 'b', toInt32(2), tag_value = 'c', toInt32(3), toInt32(999)) AS idx, uids) AS card FROM test.bitmap_test WHERE slice_id = 0 FORMAT TSV [{1,2,3,4,5},{3,4,5,6,7,8},{3,4,5},{1,2,3,4,5,6,7,8}] -- tag_value happens to be of String Type, so a string expression can also be used SELECT bitmapMultiExtract('a', 'b', 'a&b', 'a|b')(tag_value, uids) FROM test.bitmap_test WHERE slice_id = 0 FORMAT TSV -- As a result: [{1,2,3,4,5},{3,4,5,6,7,8},{3,4,5},{1,2,3,4,5,6,7,8}]
purpose:This function adds a Date Dimension on the basis of bitmapMultiCount, allowing Tag differentiation by Date in the Expression.
Caution: The Date Column needs to be of Int64 Type. Typically, Date uses the Date Type and needs to be converted using the toYYYYMMDD function.
Input:Date Column, identifier Column, bitmap Column.
Expression:A combination of a Date and an identifier anchors a bitmap and cannot be used alone. Therefore, Date_identifier must be used as a Whole, as in the Example '20240101_1', which identifies a bitmap. The above function bitmapMultiCount only requires '1', which is the difference between the two.
Output:The Number of Elements in the result bitmap After bitmap intersection, union, and Poor operations. Each Expression corresponds to a result, stored in an array.
SELECT bitmapMultiCountWithDate('20240101_1', '20240102_2', '20240101_1|20240102_2')(CAST(toYYYYMMDD(p_date), 'Int64') AS date1, multiIf((tag_value = 'a') AND (p_date = '2024-01-01'), toInt64(1), (tag_value = 'b') AND (p_date = '2024-01-02'), toInt64(2), (tag_value = 'c') and (p_date = '2024-01-01'), toInt64(3),toInt32(999)) AS idx, uids) AS card FROM test.bitmap_test FORMAT TSV -- As a result: [5,4,9] -- tag_value happens to be of String Type, so a string expression can be used SELECT bitmapMultiCountWithDate('20240101_a', '20240102_b', '20240101_a|20240102_b')(CAST(toYYYYMMDD(p_date), 'Int64') AS date1, tag_value, uids) AS card FROM test.bitmap_test FORMAT TSV -- As a result: [5,4,9]
Function and usage are the same as bitmapMultiCountWithDate. The difference is in the Return value, which is Back as bitmap Data.
SELECT bitmapMultiExtractWithDate('20240101_1', '20240102_2', '20240101_1|20240102_2')(CAST(toYYYYMMDD(p_date), 'Int64') AS date1, multiIf((tag_value = 'a') AND (p_date = '2024-01-01'), toInt64(1), (tag_value = 'b') AND (p_date = '2024-01-02'), toInt64(2), (tag_value = 'c') and (p_date = '2024-01-01'), toInt64(3),toInt32(999)) AS idx, uids) AS card FROM test.bitmap_test FORMAT TSV -- As a result: [{1,2,3,4,5},{13,14,15,16},{1,2,3,4,5,13,14,15,16}] -- tag_value happens to be of String Type, so a string expression can be used SELECT bitmapMultiExtractWithDate('20240101_a', '20240102_b', '20240101_a|20240102_b')(CAST(toYYYYMMDD(p_date), 'Int64') AS date1, tag_value, uids) AS card FROM test.bitmap_test FORMAT TSV -- As a result: [{1,2,3,4,5},{13,14,15,16},{1,2,3,4,5,13,14,15,16}]