Bitmap Functions

Bitmap functions work for two bitmaps Object value calculation, it is to return new bitmap or cardinality while using formula calculation, such as and, or, xor, and not, etc.

There are 2 kinds of construction methods for Bitmap Object. One is to be constructed by aggregation function groupBitmap with -State, the other is to be constructed by Array Object. It is also to convert Bitmap Object to Array Object.

RoaringBitmap is wrapped into a data structure while actual storage of Bitmap objects. When the cardinality is less than or equal to 32, it uses Set objet. When the cardinality is greater than 32, it uses RoaringBitmap object. That is why storage of low cardinality set is faster.

For more information on RoaringBitmap, see: CRoaring.

bitmapBuild

Build a bitmap from unsigned integer array.

  1. bitmapBuild(array)

Parameters

  • array – unsigned integer array.

Example

  1. SELECT bitmapBuild([1, 2, 3, 4, 5]) AS res, toTypeName(res)
  1. ┌─res─┬─toTypeName(bitmapBuild([1, 2, 3, 4, 5]))─────┐
  2.  AggregateFunction(groupBitmap, UInt8)
  3. └─────┴──────────────────────────────────────────────┘

bitmapToArray

Convert bitmap to integer array.

  1. bitmapToArray(bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapToArray(bitmapBuild([1, 2, 3, 4, 5])) AS res
  1. ┌─res─────────┐
  2. [1,2,3,4,5]
  3. └─────────────┘

bitmapSubsetInRange

Return subset in specified range (not include the range_end).

  1. bitmapSubsetInRange(bitmap, range_start, range_end)

Parameters

  • bitmapBitmap object.
  • range_start – range start point. Type: UInt32.
  • range_end – range end point(excluded). Type: UInt32.

Example

  1. 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
  1. ┌─res───────────────┐
  2. [30,31,32,33,100]
  3. └───────────────────┘

bitmapSubsetLimit

Creates a subset of bitmap with n elements taken between range_start and cardinality_limit.

Syntax

  1. bitmapSubsetLimit(bitmap, range_start, cardinality_limit)

Parameters

  • bitmapBitmap object.
  • range_start – The subset starting point. Type: UInt32.
  • cardinality_limit – The subset cardinality upper limit. Type: UInt32.

Returned value

The subset.

Type: Bitmap object.

Example

Query:

  1. 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:

  1. ┌─res───────────────────────┐
  2. [30,31,32,33,100,200,500]
  3. └───────────────────────────┘

bitmapContains

Checks whether the bitmap contains an element.

  1. bitmapContains(haystack, needle)

Parameters

  • haystackBitmap object, where the function searches.
  • needle – Value that the function searches. Type: UInt32.

Returned values

  • 0 — If haystack doesn’t contain needle.
  • 1 — If haystack contains needle.

Type: UInt8.

Example

  1. SELECT bitmapContains(bitmapBuild([1,5,7,9]), toUInt32(9)) AS res
  1. ┌─res─┐
  2. 1
  3. └─────┘

bitmapHasAny

Checks whether two bitmaps have intersection by some elements.

  1. bitmapHasAny(bitmap1, bitmap2)

If you are sure that bitmap2 contains strictly one element, consider using the bitmapContains function. It works more efficiently.

Parameters

  • bitmap* – bitmap object.

Return values

  • 1, if bitmap1 and bitmap2 have one similar element at least.
  • 0, otherwise.

Example

  1. SELECT bitmapHasAny(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res
  1. ┌─res─┐
  2. 1
  3. └─────┘

bitmapHasAll

Analogous to hasAll(array, array) returns 1 if the first bitmap contains all the elements of the second one, 0 otherwise.
If the second argument is an empty bitmap then returns 1.

  1. bitmapHasAll(bitmap,bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapHasAll(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res
  1. ┌─res─┐
  2. 0
  3. └─────┘

bitmapCardinality

Retrun bitmap cardinality of type UInt64.

  1. bitmapCardinality(bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapCardinality(bitmapBuild([1, 2, 3, 4, 5])) AS res
  1. ┌─res─┐
  2. 5
  3. └─────┘

bitmapMin

Retrun the smallest value of type UInt64 in the set, UINT32_MAX if the set is empty.

  1. bitmapMin(bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapMin(bitmapBuild([1, 2, 3, 4, 5])) AS res
  1. ┌─res─┐
  2. 1
  3. └─────┘

bitmapMax

Retrun the greatest value of type UInt64 in the set, 0 if the set is empty.

  1. bitmapMax(bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapMax(bitmapBuild([1, 2, 3, 4, 5])) AS res
  1. ┌─res─┐
  2. 5
  3. └─────┘

bitmapTransform

Transform an array of values in a bitmap to another array of values, the result is a new bitmap.

  1. bitmapTransform(bitmap, from_array, to_array)

Parameters

  • bitmap – bitmap object.
  • from_array – UInt32 array. For idx in range [0, from_array.size()), if bitmap contains from_array[idx], then replace it with to_array[idx]. Note that the result depends on array ordering if there are common elements between from_array and to_array.
  • to_array – UInt32 array, its size shall be the same to from_array.

Example

  1. 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
  1. ┌─res───────────────────┐
  2. [1,3,4,6,7,8,9,10,20]
  3. └───────────────────────┘

bitmapAnd

Two bitmap and calculation, the result is a new bitmap.

  1. bitmapAnd(bitmap,bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapToArray(bitmapAnd(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res
  1. ┌─res─┐
  2. [3]
  3. └─────┘

bitmapOr

Two bitmap or calculation, the result is a new bitmap.

  1. bitmapOr(bitmap,bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapToArray(bitmapOr(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res
  1. ┌─res─────────┐
  2. [1,2,3,4,5]
  3. └─────────────┘

bitmapXor

Two bitmap xor calculation, the result is a new bitmap.

  1. bitmapXor(bitmap,bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapToArray(bitmapXor(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res
  1. ┌─res───────┐
  2. [1,2,4,5]
  3. └───────────┘

bitmapAndnot

Two bitmap andnot calculation, the result is a new bitmap.

  1. bitmapAndnot(bitmap,bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapToArray(bitmapAndnot(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res
  1. ┌─res───┐
  2. [1,2]
  3. └───────┘

bitmapAndCardinality

Two bitmap and calculation, return cardinality of type UInt64.

  1. bitmapAndCardinality(bitmap,bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapAndCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
  1. ┌─res─┐
  2. 1
  3. └─────┘

bitmapOrCardinality

Two bitmap or calculation, return cardinality of type UInt64.

  1. bitmapOrCardinality(bitmap,bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapOrCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
  1. ┌─res─┐
  2. 5
  3. └─────┘

bitmapXorCardinality

Two bitmap xor calculation, return cardinality of type UInt64.

  1. bitmapXorCardinality(bitmap,bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapXorCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
  1. ┌─res─┐
  2. 4
  3. └─────┘

bitmapAndnotCardinality

Two bitmap andnot calculation, return cardinality of type UInt64.

  1. bitmapAndnotCardinality(bitmap,bitmap)

Parameters

  • bitmap – bitmap object.

Example

  1. SELECT bitmapAndnotCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
  1. ┌─res─┐
  2. 2
  3. └─────┘

Original article