UInt32 values) and are an advanced, ClickHouse-only feature: a bitmap is created with bitmapBuild and exported back to a normal array with bitmapToArray, so the aggregate/transform functions below are typically wrapped in bitmapToArray(...) to return a usable result.
bitAnd, bitOr, bitXor
Bitwise AND, OR, and XOR of two integers, applied bit by bit. Syntaxa— first integer operand.b— second integer operand.
- The bitwise combination of
aandb, as an integer.
Combine flag bits
bitNot, bitShiftLeft, bitShiftRight
Bitwise NOT (one’s complement) of an integer, and logical shifts of an integer’s bits left or right byn positions.
Syntax
a— the integer operand.n— number of bit positions to shift by (shift functions only).
- The complemented or shifted integer.
Invert and shift bits
bitCount, bitTest
bitCount returns how many bits are set to 1 in the binary representation of a number (its population count). bitTest returns the value (0 or 1) of the bit at a given position, counting from the least significant bit at position 0.
Syntax
x— the integer whose set bits are counted.a— the integer to inspect.i— zero-based bit position to test, from the least significant bit.
bitCountreturns the number of set bits, as an integer.bitTestreturns1if the bit at positioniis set, otherwise0.
Count and test bits
bitRotateLeft, bitRotateRight
Rotate the bits of an integer left or right byn positions, wrapping bits shifted off one end back around to the other. Unlike shifts, no bits are lost (ClickHouse only).
Syntax
a— the unsigned integer to rotate (UInt8/UInt16/UInt32/UInt64).n— number of bit positions to rotate by.
- The rotated integer, of the same width as
a.
Rotate an 8-bit value
bitmapBuild, bitmapToArray
bitmapBuild constructs a roaring bitmap from an array of unsigned integers. bitmapToArray converts a bitmap back into a sorted Array(UInt32), and is used to export the result of any bitmap operation (ClickHouse only).
Syntax
array— an array ofUInt*values to load into the bitmap (bitmapBuild).bitmap— a roaring bitmap to export (bitmapToArray).
bitmapBuildreturns a bitmap object.bitmapToArrayreturns anArray(UInt32)of the bitmap’s elements in ascending order.
Round-trip an array through a bitmap
bitmapCardinality
Returns the number of elements stored in a bitmap (ClickHouse only). Syntaxbitmap— the roaring bitmap to measure.
- The element count, as a
UInt64.
Count elements in a bitmap
bitmapMin, bitmapMax
Return the smallest and largest element in a bitmap (ClickHouse only). Syntaxbitmap— the roaring bitmap to inspect.
- The smallest (
bitmapMin) or largest (bitmapMax) element, as aUInt64. For an empty bitmap,bitmapMinreturns the maximum unsigned value andbitmapMaxreturns0.
Smallest and largest elements
bitmapContains
Tests whether a bitmap contains a specific value (ClickHouse only). Syntaxbitmap— the roaring bitmap to search.value— theUInt32element to look for.
1ifbitmapcontainsvalue, otherwise0.
Check membership
bitmapHasAll, bitmapHasAny
Compare two bitmaps for containment.bitmapHasAll checks whether the first bitmap contains every element of the second; bitmapHasAny checks whether they share at least one element (ClickHouse only).
Syntax
bitmap1— the first roaring bitmap.bitmap2— the second roaring bitmap.
bitmapHasAllreturns1if every element ofbitmap2is present inbitmap1, otherwise0.bitmapHasAnyreturns1if the two bitmaps have any element in common, otherwise0.
Containment and overlap checks
bitmapAnd, bitmapOr, bitmapXor, bitmapAndnot
Set operations on two bitmaps: intersection (bitmapAnd), union (bitmapOr), symmetric difference (bitmapXor), and difference (bitmapAndnot, elements in the first bitmap but not the second). Each returns a bitmap, so wrap it in bitmapToArray to export the result (ClickHouse only).
Syntax
bitmap1— the first roaring bitmap.bitmap2— the second roaring bitmap.
- A new bitmap holding the combined elements.
Intersection, union, and difference of two bitmaps
bitmapAndCardinality, bitmapOrCardinality, bitmapXorCardinality, bitmapAndnotCardinality
Return the element count of a set operation on two bitmaps without materializing the resulting bitmap: the size of the intersection, union, symmetric difference, and difference respectively (ClickHouse only). Syntaxbitmap1— the first roaring bitmap.bitmap2— the second roaring bitmap.
- The number of elements in the corresponding set operation’s result, as a
UInt64.
Sizes of set operations
bitmapSubsetInRange, bitmapSubsetLimit, subBitmap
Extract a subset of a bitmap.bitmapSubsetInRange keeps elements whose value falls in the half-open range [range_start, range_end). bitmapSubsetLimit keeps up to cardinality_limit elements whose value is at least range_start. subBitmap skips offset elements (by position) and then keeps up to cardinality_limit of the remaining elements. Each returns a bitmap, so wrap it in bitmapToArray to export (ClickHouse only).
Syntax
bitmap— the source roaring bitmap.range_start— inclusive lower bound on element value.range_end— exclusive upper bound on element value (bitmapSubsetInRange).cardinality_limit— maximum number of elements to keep.offset— number of leading elements to skip, by position, zero-based (subBitmap).
- A new bitmap containing the selected subset.
Range, value-limit, and positional subsets
bitmapTransform
Replaces specified elements of a bitmap with new values, according to matchingfrom_array and to_array mappings. Returns a bitmap, so wrap it in bitmapToArray to export (ClickHouse only).
Syntax
bitmap— the source roaring bitmap.from_array— array of existing element values to replace.to_array— array of replacement values, positionally matched tofrom_array.
- A new bitmap with the mapped elements substituted.
Remap two elements