Bitmap functionsΒΆ

The following functions are used to work with bitmaps.

You can construct bitmaps in two ways: by using the groupBitmap aggregation function with -State, or by constructing a bitmap from an Array object.

bitmapBuildΒΆ

Creates a bitmap from an array of unsigned integers.

SyntaxΒΆ

bitmapBuild(array)

ArgumentsΒΆ

  • array: Array. An array of unsigned integers to build the bitmap from.

ReturnsΒΆ

A bitmap object representing the input array. AggregateFunction(groupBitmap, UInt8).

ExampleΒΆ

SELECT bitmapBuild([1, 2, 3, 4, 5]) AS res, toTypeName(res)

Result:

β”Œβ”€res─┬─toTypeName(bitmapBuild([1, 2, 3, 4, 5]))─────┐
β”‚     β”‚ AggregateFunction(groupBitmap, UInt8)        β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

bitmapToArrayΒΆ

Converts a bitmap object back into an array of unsigned integers.

SyntaxΒΆ

bitmapToArray(bitmap)

ArgumentsΒΆ

  • bitmap: Bitmap object. The bitmap to convert.

ReturnsΒΆ

An array of unsigned integers contained in the bitmap. Array.

ExampleΒΆ

SELECT bitmapToArray(bitmapBuild([1, 2, 3, 4, 5])) AS res

Result:

β”Œβ”€res─────────┐
β”‚ [1,2,3,4,5] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

bitmapSubsetInRangeΒΆ

Extracts a new bitmap containing only the elements from the original bitmap that fall within a specified numerical range.

SyntaxΒΆ

bitmapSubsetInRange(bitmap, range_start, range_end)

ArgumentsΒΆ

  • bitmap: Bitmap object. The source bitmap.
  • range_start: UInt32. The inclusive lower bound of the range.
  • range_end: UInt32. The exclusive upper bound of the range.

ReturnsΒΆ

A new bitmap containing elements within the specified range. Bitmap object.

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

Result:

β”Œβ”€res───────────────┐
β”‚ [30,31,32,33,100] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

bitmapSubsetLimitΒΆ

Creates a new bitmap containing a limited number of elements from the original bitmap, starting from a specified value.

SyntaxΒΆ

bitmapSubsetLimit(bitmap, range_start, cardinality_limit)

ArgumentsΒΆ

  • bitmap: Bitmap object. The source bitmap.
  • range_start: UInt32. The inclusive starting value for the subset.
  • cardinality_limit: UInt32. The maximum number of elements to include in the subset.

ReturnsΒΆ

A new bitmap containing the limited subset of elements. 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

Result:

β”Œβ”€res───────────────────────┐
β”‚ [30,31,32,33,100,200,500] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

subBitmapΒΆ

Extracts a subset of a bitmap based on an offset and a maximum cardinality limit.

SyntaxΒΆ

subBitmap(bitmap, offset, cardinality_limit)

ArgumentsΒΆ

  • bitmap: Bitmap object. The source bitmap.
  • offset: UInt32. The zero-based starting position for the subset.
  • cardinality_limit: UInt32. The maximum number of elements to include in the subset.

ReturnsΒΆ

A new bitmap containing the specified subset of elements. Bitmap object.

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

Result:

β”Œβ”€res─────────────────────────────┐
β”‚ [10,11,12,13,14,15,16,17,18,19] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

bitmapContainsΒΆ

Checks if a specific unsigned integer value is present within a bitmap.

SyntaxΒΆ

bitmapContains(bitmap, needle)

ArgumentsΒΆ

  • bitmap: Bitmap object. The bitmap to search within.
  • needle: UInt32. The unsigned integer value to search for.

ReturnsΒΆ

1 if the needle is found in the bitmap, 0 otherwise. UInt8.

ExampleΒΆ

SELECT bitmapContains(bitmapBuild([1,5,7,9]), toUInt32(9)) AS res

Result:

β”Œβ”€res─┐
β”‚  1  β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapHasAnyΒΆ

Determines if two bitmaps share any common elements.

If bitmap2 contains exactly one element, consider using bitmapContains instead as it works more efficiently.

SyntaxΒΆ

bitmapHasAny(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The first bitmap.
  • bitmap2: Bitmap object. The second bitmap.

ReturnsΒΆ

1 if there is at least one shared element between bitmap1 and bitmap2, 0 otherwise. UInt8.

ExampleΒΆ

SELECT bitmapHasAny(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

β”Œβ”€res─┐
β”‚  1  β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapHasAllΒΆ

Checks if the first bitmap contains all elements present in the second bitmap.

If the second bitmap is empty, the function returns 1. Also see hasAll(array, array).

SyntaxΒΆ

bitmapHasAll(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The bitmap to check for containment.
  • bitmap2: Bitmap object. The bitmap whose elements must be present in bitmap1.

ReturnsΒΆ

1 if bitmap1 contains all elements of bitmap2, 0 otherwise. UInt8.

ExampleΒΆ

SELECT bitmapHasAll(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

β”Œβ”€res─┐
β”‚  0  β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapCardinalityΒΆ

Calculates the number of unique elements (cardinality) within a bitmap.

SyntaxΒΆ

bitmapCardinality(bitmap)

ArgumentsΒΆ

  • bitmap: Bitmap object. The bitmap to measure.

ReturnsΒΆ

The count of unique elements in the bitmap. UInt64.

ExampleΒΆ

SELECT bitmapCardinality(bitmapBuild([1, 2, 3, 4, 5])) AS res

Result:

β”Œβ”€res─┐
β”‚   5 β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapMinΒΆ

Finds the smallest unsigned integer value stored in a bitmap.

SyntaxΒΆ

bitmapMin(bitmap)

ArgumentsΒΆ

  • bitmap: Bitmap object. The bitmap to inspect.

ReturnsΒΆ

The minimum value in the bitmap, or UINT32_MAX if the bitmap is empty. UInt32.

ExampleΒΆ

SELECT bitmapMin(bitmapBuild([1, 2, 3, 4, 5])) AS res

Result:

β”Œβ”€res─┐
β”‚   1 β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapMaxΒΆ

Finds the largest unsigned integer value stored in a bitmap.

SyntaxΒΆ

bitmapMax(bitmap)

ArgumentsΒΆ

  • bitmap: Bitmap object. The bitmap to inspect.

ReturnsΒΆ

The maximum value in the bitmap, or 0 if the bitmap is empty. UInt32.

ExampleΒΆ

SELECT bitmapMax(bitmapBuild([1, 2, 3, 4, 5])) AS res

Result:

β”Œβ”€res─┐
β”‚   5 β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapTransformΒΆ

Modifies a bitmap by replacing specific elements with new values based on two corresponding arrays.

SyntaxΒΆ

bitmapTransform(bitmap, from_array, to_array)

ArgumentsΒΆ

  • bitmap: Bitmap object. The bitmap to transform.
  • from_array: Array(UInt32). An array of values to be replaced.
  • to_array: Array(UInt32). An array of new values to replace the corresponding from_array elements. Must have the same size as from_array.

ReturnsΒΆ

A new bitmap with the specified transformations applied. Bitmap object.

ExampleΒΆ

SELECT bitmapToArray(bitmapTransform(bitmapBuild([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), cast([5,999,2] as Array(UInt32)), cast([2,888,20] as Array(UInt32)))) AS res

Result:

β”Œβ”€res───────────────────┐
β”‚ [1,3,4,6,7,8,9,10,20] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

bitmapAndΒΆ

Performs a bitwise AND operation between two bitmaps, returning a new bitmap containing only the common elements.

SyntaxΒΆ

bitmapAnd(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The first bitmap.
  • bitmap2: Bitmap object. The second bitmap.

ReturnsΒΆ

A new bitmap containing elements present in both input bitmaps. Bitmap object.

ExampleΒΆ

SELECT bitmapToArray(bitmapAnd(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res

Result:

β”Œβ”€res─┐
β”‚ [3] β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapOrΒΆ

Performs a bitwise OR operation between two bitmaps, returning a new bitmap containing all unique elements from both.

SyntaxΒΆ

bitmapOr(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The first bitmap.
  • bitmap2: Bitmap object. The second bitmap.

ReturnsΒΆ

A new bitmap containing all unique elements from either input bitmap. Bitmap object.

ExampleΒΆ

SELECT bitmapToArray(bitmapOr(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res

Result:

β”Œβ”€res─────────┐
β”‚ [1,2,3,4,5] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

bitmapXorΒΆ

Performs a bitwise XOR operation between two bitmaps, returning a new bitmap with elements present in one but not both.

SyntaxΒΆ

bitmapXor(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The first bitmap.
  • bitmap2: Bitmap object. The second bitmap.

ReturnsΒΆ

A new bitmap containing elements that are in bitmap1 or bitmap2, but not in both. Bitmap object.

ExampleΒΆ

SELECT bitmapToArray(bitmapXor(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res

Result:

β”Œβ”€res───────┐
β”‚ [1,2,4,5] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

bitmapAndnotΒΆ

Performs a bitwise AND NOT operation, returning a new bitmap with elements present in the first bitmap but not in the second.

SyntaxΒΆ

bitmapAndnot(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The first bitmap.
  • bitmap2: Bitmap object. The second bitmap.

ReturnsΒΆ

A new bitmap containing elements from bitmap1 that are not present in bitmap2. Bitmap object.

ExampleΒΆ

SELECT bitmapToArray(bitmapAndnot(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res

Result:

β”Œβ”€res───┐
β”‚ [1,2] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”˜

bitmapAndCardinalityΒΆ

Calculates the cardinality (number of elements) of the intersection between two bitmaps.

SyntaxΒΆ

bitmapAndCardinality(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The first bitmap.
  • bitmap2: Bitmap object. The second bitmap.

ReturnsΒΆ

The count of common elements between the two bitmaps. UInt64.

ExampleΒΆ

SELECT bitmapAndCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

β”Œβ”€res─┐
β”‚   1 β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapOrCardinalityΒΆ

Calculates the cardinality (number of elements) of the union of two bitmaps.

SyntaxΒΆ

bitmapOrCardinality(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The first bitmap.
  • bitmap2: Bitmap object. The second bitmap.

ReturnsΒΆ

The count of all unique elements across both bitmaps. UInt64.

ExampleΒΆ

SELECT bitmapOrCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

β”Œβ”€res─┐
β”‚   5 β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapXorCardinalityΒΆ

Calculates the cardinality (number of elements) of the symmetric difference (XOR) between two bitmaps.

SyntaxΒΆ

bitmapXorCardinality(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The first bitmap.
  • bitmap2: Bitmap object. The second bitmap.

ReturnsΒΆ

The count of elements that are in one bitmap but not in both. UInt64.

ExampleΒΆ

SELECT bitmapXorCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

β”Œβ”€res─┐
β”‚   4 β”‚
β””β”€β”€β”€β”€β”€β”˜

bitmapAndnotCardinalityΒΆ

Calculates the cardinality (number of elements) of the difference between two bitmaps (elements in the first but not the second).

SyntaxΒΆ

bitmapAndnotCardinality(bitmap1, bitmap2)

ArgumentsΒΆ

  • bitmap1: Bitmap object. The first bitmap.
  • bitmap2: Bitmap object. The second bitmap.

ReturnsΒΆ

The count of elements present in bitmap1 but not in bitmap2. UInt64.

ExampleΒΆ

SELECT bitmapAndnotCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res

Result:

β”Œβ”€res─┐
β”‚   2 β”‚
β””β”€β”€β”€β”€β”€β”˜
Updated