-
Aggregation function:
Use the
groupBitmapfunction with the-Statesuffix to aggregate values into a bitmap. - Array conversion: Convert an existing Array object into a bitmap data structure.
ClickHouse function reference
bitmapBuild
Builds a bitmap from an unsigned integer array. Syntax:array- Unsigned integer array.
bitmapBuild constructs a bitmap from the array of IDs [1, 2, 3, 4, 5]. The resulting bitmap is of type AggregateFunction(groupBitmap, UInt8).
bitmapToArray
Converts a bitmap object to an array of unsigned integers. Syntax:bitmap- A bitmap object.
bitmapToArray converts a bitmap containing the taco IDs 1, 3, 5, 7, and 9 back into an array of integers.
The returned array will be sorted in ascending order of the set bits, regardless of the order they were originally added to the bitmap.
bitmapSubsetInRange
Returns a subset of a bitmap containing only the bits within a specified range. Syntax:bitmap(Bitmap object): The input bitmap.range_start(UInt32): The start of the range (inclusive).range_end(UInt32): The end of the range (exclusive).
bitmapSubsetInRange extracts a subset of the bitmap containing only the values between 3 (inclusive) and 8 (exclusive), which represents taco IDs 3 through 7.
This function is useful for efficiently extracting a range of values from a bitmap without needing to materialize the entire bitmap as an array.
bitmapSubsetLimit
Returns a subset of a bitmap with smallest bit valuerange_start and at most cardinality_limit elements.
Syntax:
bitmap(Bitmap) - The input bitmap.range_start(UInt32) - The start of the range (inclusive).cardinality_limit(UInt32) - The maximum cardinality of the subset.
cardinality_limit elements, starting from range_start.
Example:
bitmapSubsetLimit returns a subset of the bitmap starting from value 5, with a maximum of 3 elements. The result is then converted to an array for display.
subBitmap
Returns a subset of the bitmap, starting from positionoffset. The maximum cardinality of the returned bitmap is cardinality_limit.
Syntax:
bitmap(Bitmap) - The input bitmap.offset(UInt32) - The position of the first element of the subset.cardinality_limit(UInt32) - The maximum number of elements in the subset.
subBitmap returns a subset of the bitmap starting from the 4th element (offset 3) and including up to 4 elements. The result is then converted to an array for display.
bitmapContains
Checks whether a bitmap contains a specific element. Syntax:bitmap(Bitmap): The input bitmap.needle(UInt32): The element to search for in the bitmap.
1if the bitmap contains the needle element. [UInt8]0if the bitmap does not contain the needle element. [UInt8]
bitmapContains checks if each taco’s toppings bitmap contains the topping with ID 3 (assuming 3 represents salsa). The result is 1 if salsa is present and 0 if it’s not.
bitmapHasAny
Checks whether two bitmaps intersect (have any elements in common). Syntax:bitmap1(Bitmap): The first bitmap objectbitmap2(Bitmap): The second bitmap object
1if the bitmaps intersect (have at least one element in common)0if the bitmaps do not intersect
If
bitmap2 contains exactly one element, consider using bitmapContains instead as it is more efficient for that case.bitmapHasAll
Returns1 if the first bitmap contains all elements of the second bitmap, otherwise 0. If the second bitmap is empty, returns 1.
Syntax:
bitmap1(Bitmap): The first bitmap object to check.bitmap2(Bitmap): The second bitmap object to check against.
- 1 if
bitmap1contains all elements inbitmap2 - 0 otherwise
- 1 if
bitmap2is empty
- The first
bitmapHasAllreturns1because [1,2,3,4,5] contains all elements of [3,4,5]. - The second
bitmapHasAllreturns0because [1,2,3] does not contain all elements of [3,4,5].
bitmapCardinality
Returns the cardinality (number of set bits) of a bitmap. Syntax:bitmap(Bitmap): A bitmap object.
UInt64]
Example:
bitmapCardinality counts the number of elements in the bitmap created from the array [1, 2, 3, 4, 5].
bitmapMin
Computes the smallest bit set in a bitmap, orUINT32_MAX if the bitmap is empty.
Syntax:
bitmap(Bitmap): Bitmap object.
UINT32_MAX (4294967295) if the bitmap is empty. [UInt32]
Example:
If the bitmap is empty, the function returns
UINT32_MAX (4294967295).bitmapMax
Computes the greatest bit set in a bitmap, or 0 if the bitmap is empty. Syntax:bitmap(Bitmap): Bitmap object.
0 if empty. [UInt32]
Example:
bitmapMax finds the largest taco ID (5) from a bitmap of taco IDs.
bitmapTransform
Replaces bits in a bitmap based on provided mapping arrays. Syntax:bitmap(Bitmap): The input bitmap to transform.from_array(Array(UInt32)): Array of bit values to replace.to_array(Array(UInt32)): Array of new bit values to set.
Bitmap]
- The
from_arrayandto_arraymust have the same length. - For each index i, if the bitmap contains
from_array[i], it is replaced withto_array[i]. - The result depends on the order of elements in
from_arrayandto_array.
bitmapAnd
Computes the logicalAND (intersection) of two bitmaps.
Syntax:
bitmap1(Bitmap): The first bitmap objectbitmap2(Bitmap): The second bitmap object
Bitmap]
Example:
bitmapOr
Computes the logicalOR (disjunction) of two bitmaps.
Syntax:
bitmap1(Bitmap): The first bitmap object.bitmap2(Bitmap): The second bitmap object.
bitmapOr combines two bitmaps representing odd and even taco IDs, resulting in a bitmap containing all IDs from 1 to 10. The bitmapToArray function is used to convert the result back to an array for display.
bitmapXor
Performs a bitwiseXOR operation on two bitmaps.
Syntax:
bitmap1(Bitmap): The first bitmap object.bitmap2(Bitmap): The second bitmap object.
Bitmap]
Example:
bitmapAndnot
Computes the logicalAND of the first bitmap with the negation of the second bitmap.
Syntax:
bitmap1(Bitmap): The first bitmap operand.bitmap2(Bitmap): The second bitmap operand to be negated.
bitmap1 AND (NOT bitmap2). [Bitmap]
Example:
bitmapAndnot performs the following operation:
- Takes the first bitmap [1,2,3,4,5]
- Negates the second bitmap [3,4,5,6,7] to get [1,2]
- Performs a logical AND between [1,2,3,4,5] and [1,2]
- Returns the result [1,2]
bitmapAndCardinality
Returns the cardinality of the logicalAND (conjunction) of two bitmaps.
Syntax:
bitmap1(Bitmap): The first bitmap objectbitmap2(Bitmap): The second bitmap object
UInt64]
Example:
bitmapAndCardinality calculates how many taco toppings are common between two sets of toppings represented as bitmaps. The result 3 indicates that there are 3 toppings (3, 4, and 5) present in both bitmaps.
bitmapOrCardinality
Returns the cardinality of the logicalOR (disjunction) of two bitmaps.
Syntax:
bitmap1(Bitmap): First bitmap objectbitmap2(Bitmap): Second bitmap object
UInt64]
Example:
bitmapOrCardinality calculates how many unique elements are available when combining two sets. The first bitmap represents [1, 2, 3] and the second represents [3, 4, 5]. The OR operation results in [1, 2, 3, 4, 5], and the cardinality (count of unique elements) is 5.
bitmapXorCardinality
Returns the cardinality of theXOR (exclusive or) of two bitmaps.
Syntax:
bitmap1(Bitmap): The first bitmap objectbitmap2(Bitmap): The second bitmap object
XOR of the two input bitmaps. [UInt64]
Example:
XOR of the two bitmaps is [1,2,6,7], which has a cardinality of 4.
This function efficiently computes the cardinality of the XOR without materializing the full result bitmap, which can be useful for large bitmaps.
bitmapAndnotCardinality
Returns the cardinality of theAND-NOT operation of two bitmaps.
Syntax:
bitmap1(Bitmap): The first bitmap object.bitmap2(Bitmap): The second bitmap object.
bitmap1 that are not in bitmap2. [UInt64]
Example:
bitmapAndnotCardinality calculates the number of elements in bitmap1 that are not in bitmap2. The result is 2, corresponding to the elements [1, 2] which are in bitmap1 but not in bitmap2.