Bitmap functions
Work with bitmap data structures for efficient set operations.
Bitmaps can be constructed using two different methods:
-
Aggregation function: Use the
groupBitmap
function with the-State
suffix to aggregate values into a bitmap. -
Array conversion: Convert an existing Array object into a bitmap data structure.
These methods allow for efficient creation and manipulation of bitmap data structures.
ClickHouse function reference
bitmapBuild
Builds a bitmap from an unsigned integer array.
Syntax:
Arguments:
array
- Unsigned integer array.
Returns:
A bitmap object constructed from the input array.
Example:
Result:
In this example, 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:
Arguments:
bitmap
- A bitmap object.
Returns:
An array of unsigned integers containing the set bits from the bitmap.
Example:
Result:
In this example, 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:
Arguments:
bitmap
(Bitmap object): The input bitmap.range_start
(UInt32
): The start of the range (inclusive).range_end
(UInt32
): The end of the range (exclusive).
Returns:
A new bitmap containing only the bits from the input bitmap that fall within the specified range.
Example:
Result:
In this example, 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 value range_start
and at most cardinality_limit
elements.
Syntax:
Arguments:
bitmap
(Bitmap
) - The input bitmap.range_start
(UInt32
) - The start of the range (inclusive).cardinality_limit
(UInt32
) - The maximum cardinality of the subset.
Returns:
A bitmap subset with at most cardinality_limit
elements, starting from range_start
.
Example:
Result:
In this 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 position offset
. The maximum cardinality of the returned bitmap is cardinality_limit
.
Syntax:
Arguments:
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.
Returns:
A subset of the input bitmap.
Example:
Result:
In this example, 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:
Arguments:
bitmap
(Bitmap
): The input bitmap.needle
(UInt32
): The element to search for in the bitmap.
Returns:
1
if the bitmap contains the needle element. [UInt8
]0
if the bitmap does not contain the needle element. [UInt8
]
Example:
Result:
In this example, 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:
Arguments:
bitmap1
(Bitmap
): The first bitmap objectbitmap2
(Bitmap
): The second bitmap object
Returns:
1
if the bitmaps intersect (have at least one element in common)0
if the bitmaps do not intersect
Example:
Result:
If bitmap2
contains exactly one element, consider using bitmapContains
instead as it is more efficient for that case.
bitmapHasAll
Returns 1
if the first bitmap contains all elements of the second bitmap, otherwise 0
. If the second bitmap is empty, returns 1
.
Syntax:
Arguments:
bitmap1
(Bitmap): The first bitmap object to check.bitmap2
(Bitmap): The second bitmap object to check against.
Returns:
- 1 if
bitmap1
contains all elements inbitmap2
- 0 otherwise
- 1 if
bitmap2
is empty
Example:
Result:
In this example:
- The first
bitmapHasAll
returns1
because [1,2,3,4,5] contains all elements of [3,4,5]. - The second
bitmapHasAll
returns0
because [1,2,3] does not contain all elements of [3,4,5].
bitmapCardinality
Returns the cardinality (number of set bits) of a bitmap.
Syntax:
Arguments:
bitmap
(Bitmap
): A bitmap object.
Returns:
The number of set bits in the bitmap. [UInt64
]
Example:
Result:
In this 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, or UINT32_MAX
if the bitmap is empty.
Syntax:
Arguments:
bitmap
(Bitmap
): Bitmap object.
Returns:
The smallest bit set in the bitmap, or UINT32_MAX
(4294967295) if the bitmap is empty. [UInt32
]
Example:
Result:
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:
Arguments:
bitmap
(Bitmap
): Bitmap object.
Returns:
The greatest bit set in the bitmap, or 0
if empty. [UInt32
]
Example:
Result:
In this 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:
Arguments:
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.
Returns:
A new bitmap with the specified bits replaced. [Bitmap
]
- The
from_array
andto_array
must 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_array
andto_array
.
Example:
Result:
In this example, the bitmap [1,2,3,4,5] is transformed by replacing 2 with 10 and 4 with 20, resulting in [1,3,5,10,20].
bitmapAnd
Computes the logical AND
(intersection) of two bitmaps.
Syntax:
Arguments:
bitmap1
(Bitmap
): The first bitmap objectbitmap2
(Bitmap
): The second bitmap object
Returns:
A new bitmap object containing the intersection of the input bitmaps. [Bitmap
]
Example:
Result:
bitmapOr
Computes the logical OR
(disjunction) of two bitmaps.
Syntax:
Arguments:
bitmap1
(Bitmap): The first bitmap object.bitmap2
(Bitmap): The second bitmap object.
Returns:
A new bitmap object representing the union of the input bitmaps.
Example:
Result:
In this example, 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 bitwise XOR
operation on two bitmaps.
Syntax:
Arguments:
bitmap1
(Bitmap
): The first bitmap object.bitmap2
(Bitmap
): The second bitmap object.
Returns:
A new bitmap object containing the result of the XOR operation. [Bitmap
]
Example:
Result:
bitmapAndnot
Computes the logical AND
of the first bitmap with the negation of the second bitmap.
Syntax:
Arguments:
bitmap1
(Bitmap
): The first bitmap operand.bitmap2
(Bitmap
): The second bitmap operand to be negated.
Returns:
A new bitmap containing the result of bitmap1 AND (NOT bitmap2)
. [Bitmap
]
Example:
Result:
In this 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]
This function is useful for finding elements that are in the first set but not in the second set.
bitmapAndCardinality
Returns the cardinality of the logical AND
(conjunction) of two bitmaps.
Syntax:
Arguments:
bitmap1
(Bitmap
): The first bitmap objectbitmap2
(Bitmap
): The second bitmap object
Returns:
The number of set bits in the result of ANDing the two input bitmaps. [UInt64
]
Example:
Result:
In this 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 logical OR
(disjunction) of two bitmaps.
Syntax:
Arguments:
bitmap1
(Bitmap
): First bitmap objectbitmap2
(Bitmap
): Second bitmap object
Returns:
The number of set bits in the result of ORing the two input bitmaps. [UInt64
]
Example:
Result:
In this 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 the XOR
(exclusive or) of two bitmaps.
Syntax:
Arguments:
bitmap1
(Bitmap
): The first bitmap objectbitmap2
(Bitmap
): The second bitmap object
Returns:
The cardinality (number of set bits) of the XOR
of the two input bitmaps. [UInt64
]
Example:
Result:
In this example, the 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 the AND-NOT
operation of two bitmaps.
Syntax:
Arguments:
bitmap1
(Bitmap
): The first bitmap object.bitmap2
(Bitmap
): The second bitmap object.
Returns:
The number of elements in bitmap1
that are not in bitmap2
. [UInt64
]
Example:
Result:
In this 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
.
Was this page helpful?