> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for Bitmap Functions

# Bitmap Functions

Bitmaps can be constructed in two ways. The first way is constructed by aggregation function groupBitmap with `-State`, the other way is to constructed a bitmap from an Array object.

{/*AUTOGENERATED_START*/}

<h2 id="bitmapAnd">
  bitmapAnd
</h2>

Introduced in: v20.1.0

Computes the logical conjunction (AND) of two bitmaps.

**Syntax**

```sql theme={null}
bitmapAnd(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns a bitmap containing bits present in both input bitmaps [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapToArray(bitmapAnd(bitmapBuild([1, 2, 3]), bitmapBuild([3, 4, 5]))) AS res;
```

```response title=Response theme={null}
┌─res─┐
│ [3] │
└─────┘
```

<h2 id="bitmapAndCardinality">
  bitmapAndCardinality
</h2>

Introduced in: v20.1.0

Returns the cardinality of the logical conjunction (AND) of two bitmaps.

**Syntax**

```sql theme={null}
bitmapAndCardinality(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns the number of set bits in the intersection of the two bitmaps [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapAndCardinality(bitmapBuild([1,2,3]), bitmapBuild([3,4,5])) AS res;
```

```response title=Response theme={null}
┌─res─┐
│   1 │
└─────┘
```

<h2 id="bitmapAndnot">
  bitmapAndnot
</h2>

Introduced in: v20.1.0

Computes the set difference A AND-NOT B of two bitmaps.

**Syntax**

```sql theme={null}
bitmapAndnot(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns a bitmap containing set bits present in the first bitmap but not in the second [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapToArray(bitmapAndnot(bitmapBuild([1, 2, 3]), bitmapBuild([3, 4, 5]))) AS res;
```

```response title=Response theme={null}
┌─res────┐
│ [1, 2] │
└────────┘
```

<h2 id="bitmapAndnotCardinality">
  bitmapAndnotCardinality
</h2>

Introduced in: v20.1.0

Returns the cardinality of the AND-NOT operation of two bitmaps.

**Syntax**

```sql theme={null}
bitmapAndnotCardinality(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns the number of set bits in the result of `bitmap1 AND-NOT bitmap2` [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapAndnotCardinality(bitmapBuild([1,2,3]), bitmapBuild([3,4,5])) AS res;
```

```response title=Response theme={null}
┌─res─┐
│   2 │
└─────┘
```

<h2 id="bitmapBuild">
  bitmapBuild
</h2>

Introduced in: v20.1.0

Builds a bitmap from an unsigned integer array. It is the opposite of function [`bitmapToArray`](/reference/functions/regular-functions/bitmap-functions#bitmapToArray).

**Syntax**

```sql theme={null}
bitmapBuild(array)
```

**Arguments**

* `array` — Unsigned integer array. [`Array(UInt*)`](/reference/data-types/array)

**Returned value**

Returns a bitmap from the provided array [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapBuild([1, 2, 3, 4, 5]) AS res, toTypeName(res);
```

```response title=Response theme={null}
┌─res─┬─toTypeName(bitmapBuild([1, 2, 3, 4, 5]))─────┐
│     │ AggregateFunction(groupBitmap, UInt8)        │
└─────┴──────────────────────────────────────────────┘
```

<h2 id="bitmapCardinality">
  bitmapCardinality
</h2>

Introduced in: v20.1.0

Returns the number of bits set (the cardinality) in the bitmap.

**Syntax**

```sql theme={null}
bitmapCardinality(bitmap)
```

**Arguments**

* `bitmap` — Bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns the number of bits set in the bitmap [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapCardinality(bitmapBuild([1, 3, 3, 5, 7, 7])) AS res
```

```response title=Response theme={null}
┌─res─┐
│   4 │
└─────┘
```

<h2 id="bitmapContains">
  bitmapContains
</h2>

Introduced in: v20.1.0

Checks if the bitmap contains a specific element.

**Syntax**

```sql theme={null}
bitmapContains(bitmap, value)
```

**Arguments**

* `bitmap` — Bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `value` — Element to check for. [(U)Int8/16/32/64](/reference/data-types/int-uint)

**Returned value**

Returns `1` if the bitmap contains the specified value, otherwise `0` [`UInt8`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapContains(bitmapBuild([1, 2, 3]), 2) AS res;
```

```response title=Response theme={null}
┌─res─┐
│  1  │
└─────┘
```

<h2 id="bitmapHasAll">
  bitmapHasAll
</h2>

Introduced in: v20.1.0

Checks if the first bitmap contains all set bits of the second bitmap.

**Syntax**

```sql theme={null}
bitmapHasAll(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns `1` if all set bits of the second bitmap are present in the first bitmap, otherwise `0` [`UInt8`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapHasAll(bitmapBuild([1, 2, 3]), bitmapBuild([2, 3])) AS res;
```

```response title=Response theme={null}
┌─res─┐
│  1  │
└─────┘
```

<h2 id="bitmapHasAny">
  bitmapHasAny
</h2>

Introduced in: v20.1.0

Checks if the first bitmap contains any set bits of the second bitmap.

**Syntax**

```sql theme={null}
bitmapHasAny(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns `1` if any bits of the second bitmap are present in the first bitmap, otherwise `0` [`UInt8`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapHasAny(bitmapBuild([1, 2, 3]), bitmapBuild([3, 4, 5])) AS res;
```

```response title=Response theme={null}
┌─res─┐
│  1  │
└─────┘
```

<h2 id="bitmapMax">
  bitmapMax
</h2>

Introduced in: v20.1.0

Returns the position of the greatest bit set in a bitmap, or `0` if the bitmap is empty.

**Syntax**

```sql theme={null}
bitmapMax(bitmap)
```

**Arguments**

* `bitmap` — Bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns the position of the greatest bit set in the bitmap, otherwise `0` [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapMax(bitmapBuild([1, 2, 3, 4, 5])) AS res;
```

```response title=Response theme={null}
┌─res─┐
│   5 │
└─────┘
```

<h2 id="bitmapMin">
  bitmapMin
</h2>

Introduced in: v20.1.0

Returns the position of the smallest bit set in a bitmap. If all bits are unset, or `UINT32_MAX` (`UINT64_MAX` if the bitmap contains more than `2^64` bits).

**Syntax**

```sql theme={null}
bitmapMin(bitmap)
```

**Arguments**

* `bitmap` — Bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns the position of the smallest bit set in the bitmap, or `UINT32_MAX`/`UINT64_MAX` [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapMin(bitmapBuild([3, 5, 2, 6])) AS res;
```

```response title=Response theme={null}
┌─res─┐
│   2 │
└─────┘
```

<h2 id="bitmapOr">
  bitmapOr
</h2>

Introduced in: v20.1.0

Computes the logical disjunction (OR) of two bitmaps.

**Syntax**

```sql theme={null}
bitmapOr(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns a bitmap containing set bits present in either input bitmap [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapToArray(bitmapOr(bitmapBuild([1, 2, 3]), bitmapBuild([3, 4, 5]))) AS res;
```

```response title=Response theme={null}
┌─res─────────────┐
│ [1, 2, 3, 4, 5] │
└─────────────────┘
```

<h2 id="bitmapOrCardinality">
  bitmapOrCardinality
</h2>

Introduced in: v20.1.0

Returns the cardinality of the logical disjunction (OR) of two bitmaps.

**Syntax**

```sql theme={null}
bitmapOrCardinality(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns the number of set bits in the union of the two bitmaps [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapOrCardinality(bitmapBuild([1,2,3]), bitmapBuild([3,4,5])) AS res;
```

```response title=Response theme={null}
┌─res─┐
│   5 │
└─────┘
```

<h2 id="bitmapSubsetInRange">
  bitmapSubsetInRange
</h2>

Introduced in: v20.1.0

Returns a subset of the bitmap, containing only the set bits in the specified range \[start, end). Uses 1-based indexing.

**Syntax**

```sql theme={null}
bitmapSubsetInRange(bitmap, start, end)
```

**Arguments**

* `bitmap` — Bitmap to extract the subset from. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `start` — Start of the range (inclusive). [`UInt*`](/reference/data-types/int-uint) - `end` — End of the range (exclusive). [`UInt*`](/reference/data-types/int-uint)

**Returned value**

Returns a bitmap containing only the set bits in the specified range [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapToArray(bitmapSubsetInRange(bitmapBuild([1, 2, 3, 4, 5]), 2, 5)) AS res;
```

```response title=Response theme={null}
┌─res───────┐
│ [2, 3, 4] │
└───────────┘
```

<h2 id="bitmapSubsetLimit">
  bitmapSubsetLimit
</h2>

Introduced in: v20.1.0

Returns a subset of a bitmap from position `range_start` with at most `cardinality_limit` set bits. Uses 1-based indexing.

**Syntax**

```sql theme={null}
bitmapSubsetLimit(bitmap, range_start, cardinality_limit)
```

**Arguments**

* `bitmap` — Bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `range_start` — Start of the range (inclusive). [`UInt32`](/reference/data-types/int-uint) - `cardinality_limit` — Maximum cardinality of the subset. [`UInt32`](/reference/data-types/int-uint)

**Returned value**

Returns a bitmap containing at most `cardinality_limit` set bits, starting from `range_start` [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapToArray(bitmapSubsetLimit(bitmapBuild([1, 5, 3, 2, 8]), 3, 2)) AS res;
```

```response title=Response theme={null}
┌─res────┐
│ [5, 3] │
└────────┘
```

<h2 id="bitmapToArray">
  bitmapToArray
</h2>

Introduced in: v20.1.0

Converts a bitmap to an array of unsigned integers. It is the opposite of function [`bitmapBuild`](/reference/functions/regular-functions/bitmap-functions#bitmapBuild).

**Syntax**

```sql theme={null}
bitmapToArray(bitmap)
```

**Arguments**

* `bitmap` — Bitmap to convert. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns an array of unsigned integers contained in the bitmap [`Array(UInt*)`](/reference/data-types/array)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapToArray(bitmapBuild([1, 2, 3, 4, 5])) AS res;
```

```response title=Response theme={null}
┌─res─────────────┐
│ [1, 2, 3, 4, 5] │
└─────────────────┘
```

<h2 id="bitmapTransform">
  bitmapTransform
</h2>

Introduced in: v20.1.0

Changes up to N bits in a bitmap by swapping specific bit values in `from_array` with corresponding ones in `to_array`.

**Syntax**

```sql theme={null}
bitmapTransform(bitmap, from_array, to_array)
```

**Arguments**

* `bitmap` — Bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `from_array` — Array of original set bits to be replaced. [`Array(T)`](/reference/data-types/array). - `to_array` — Array of new set bits to replace with. [`Array(T)`](/reference/data-types/array).

**Returned value**

Returns a bitmap with elements transformed according to the given mapping [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapToArray(bitmapTransform(bitmapBuild([1, 2, 3, 4, 5]), [2, 4], [20, 40])) AS res;
```

```response title=Response theme={null}
┌─res───────────────┐
│ [1, 3, 5, 20, 40] │
└───────────────────┘
```

<h2 id="bitmapXor">
  bitmapXor
</h2>

Introduced in: v20.1.0

Computes the symmetric difference (XOR) of two bitmaps.

**Syntax**

```sql theme={null}
bitmapXor(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns a bitmap containing set bits present in either input bitmap, but not in both [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapToArray(bitmapXor(bitmapBuild([1, 2, 3]), bitmapBuild([3, 4, 5]))) AS res;
```

```response title=Response theme={null}
┌─res──────────┐
│ [1, 2, 4, 5] │
└──────────────┘
```

<h2 id="bitmapXorCardinality">
  bitmapXorCardinality
</h2>

Introduced in: v20.1.0

Returns the cardinality of the XOR (symmetric difference) of two bitmaps.

**Syntax**

```sql theme={null}
bitmapXorCardinality(bitmap1, bitmap2)
```

**Arguments**

* `bitmap1` — First bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `bitmap2` — Second bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction).

**Returned value**

Returns the number of set bits in the symmetric difference of the two bitmaps [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapXorCardinality(bitmapBuild([1,2,3]), bitmapBuild([3,4,5])) AS res;
```

```response title=Response theme={null}
┌─res─┐
│   4 │
└─────┘
```

<h2 id="subBitmap">
  subBitmap
</h2>

Introduced in: v21.9.0

Returns a subset of the bitmap, starting from position `offset`. The maximum cardinality of the returned bitmap is `cardinality_limit`.

**Syntax**

```sql theme={null}
subBitmap(bitmap, offset, cardinality_limit)
```

**Arguments**

* `bitmap` — Bitmap object. [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction). - `offset` — Number of set bits to skip from the beginning (zero-based). [`UInt32`](/reference/data-types/int-uint) - `cardinality_limit` — Maximum number of set bits to include in the subset. [`UInt32`](/reference/data-types/int-uint)

**Returned value**

Returns a bitmap containing at most `limit` set bits, starting after skipping `offset` set bits in ascending order [`AggregateFunction(groupBitmap, T)`](/reference/data-types/aggregatefunction)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT bitmapToArray(subBitmap(bitmapBuild([1, 2, 3, 4, 5]), 2, 2)) AS res;
```

```response title=Response theme={null}
┌─res────┐
│ [3, 4] │
└────────┘
```
