2019-06-09 17:39:45 +00:00
# Функции для битмапов
2019-07-30 06:47:38 +00:00
## bitmapBuild {#bitmap_functions-bitmapbuild}
2019-06-09 17:39:45 +00:00
2019-07-30 06:47:38 +00:00
Создаёт битовый массив из массива целочисленных значений.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapBuild(array)
```
**Параметры**
- `array` – массив типа `UInt*` .
**Пример**
```sql
2019-06-14 10:29:16 +00:00
SELECT bitmapBuild([1, 2, 3, 4, 5]) AS res, toTypeName(res)
2019-06-09 17:39:45 +00:00
```
2019-06-14 10:29:16 +00:00
```text
┌─res─┬─toTypeName(bitmapBuild([1, 2, 3, 4, 5]))─────┐
│ │ AggregateFunction(groupBitmap, UInt8) │
└─────┴──────────────────────────────────────────────┘
```
2019-06-09 17:39:45 +00:00
## bitmapToArray
2019-07-30 06:47:38 +00:00
Преобразует битовый массив в массив целочисленных значений.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapToArray(bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapToArray(bitmapBuild([1, 2, 3, 4, 5])) AS res
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─────────┐
│ [1,2,3,4,5] │
└─────────────┘
```
2019-10-28 19:03:25 +00:00
## bitmapSubsetLimit {#bitmapsubsetlimit}
Возвращает подмножество битового массива. Значения подмножества сортируются в порядке возрастания, а е г о верхний предел и начальная точка не превышают соответствующие входные значения.
**Синтаксис**
```sql
bitmapSubsetLimit(bitmap, range_start, cardinality_limit)
```
**Параметры**
- `bitmap` – Битовый массив. [Bitmap object ](#bitmap_functions-bitmapbuild ).
- `range_start` – Начальная точка подмножества. [UInt32 ](../../data_types/int_uint.md ).
- `cardinality_limit` – Верхний предел подмножества. [UInt32 ](../../data_types/int_uint.md ).
**Возвращаемое значение**
Подмножество битового массива.
Тип: `Bitmap object` .
**Пример**
Запрос:
```sql
SELECT bitmapToArray(bitmapSubsetLimit(bitmapBuild([30, 20, 10, 1, 2, 3]), toUInt32(2), toUInt32(3))) AS res;
```
Ответ:
```text
┌─res──────┐
│ [2,3,10] │
└──────────┘
```
2019-07-30 06:47:38 +00:00
## bitmapContains {#bitmap_functions-bitmapcontains}
Проверяет вхождение элемента в битовый массив.
2019-09-23 15:31:46 +00:00
```sql
2019-07-30 06:47:38 +00:00
bitmapContains(haystack, needle)
```
**Параметры**
- `haystack` – [объект Bitmap ](#bitmap_functions-bitmapbuild ), в котором функция ищет значение.
- `needle` – значение, которое функция ищет. Тип — [UInt32 ](../../data_types/int_uint.md ).
**Возвращаемые значения**
- 0 — если в `haystack` нет `needle` .
- 1 — если в `haystack` есть `needle` .
Тип — `UInt8` .
**Пример**
2019-09-23 15:31:46 +00:00
```sql
2019-07-30 06:47:38 +00:00
SELECT bitmapContains(bitmapBuild([1,5,7,9]), toUInt32(9)) AS res
```
```text
┌─res─┐
│ 1 │
└─────┘
```
2019-06-09 17:39:45 +00:00
## bitmapHasAny
2019-07-30 06:47:38 +00:00
Проверяет, имеют ли два битовых массива хотя бы один общий элемент.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-14 10:29:16 +00:00
bitmapHasAny(bitmap1, bitmap2)
2019-06-09 17:39:45 +00:00
```
2019-07-30 06:47:38 +00:00
Если вы уверены, что `bitmap2` содержит строго один элемент, используйте функцию [bitmapContains ](#bitmap_functions-bitmapcontains ). Она работает эффективнее.
2019-06-09 17:39:45 +00:00
**Параметры**
2019-06-14 10:29:16 +00:00
- `bitmap*` – массив любого типа с набором элементов.
**Возвращаемые значения**
- `1` , если `bitmap1` и `bitmap2` имеют хотя бы один одинаковый элемент.
- `0` , в противном случае.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapHasAny(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─┐
│ 1 │
└─────┘
```
## bitmapHasAll
2019-07-30 06:47:38 +00:00
Аналогично функции `hasAll(array, array)` возвращает 1 если первый битовый массив содержит все элементы второго, 0 в противном случае.
Если второй аргумент является пустым битовым массивом, то возвращает 1.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapHasAll(bitmap,bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapHasAll(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─┐
│ 0 │
└─────┘
```
## bitmapAnd
2019-07-30 06:47:38 +00:00
Логическое И для двух битовых массивов. Результат — новый битовый массив.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapAnd(bitmap,bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapToArray(bitmapAnd(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─┐
│ [3] │
└─────┘
```
## bitmapOr
2019-07-30 06:47:38 +00:00
Логическое ИЛИ для двух битовых массивов. Результат — новый битовый массив.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapOr(bitmap,bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapToArray(bitmapOr(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─────────┐
│ [1,2,3,4,5] │
└─────────────┘
```
## bitmapXor
2019-07-30 06:47:38 +00:00
Логическое исключающее ИЛИ для двух битовых массивов. Результат — новый битовый массив.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapXor(bitmap,bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapToArray(bitmapXor(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res───────┐
│ [1,2,4,5] │
└───────────┘
```
## bitmapAndnot
2019-07-30 06:47:38 +00:00
Логическое отрицание И для двух битовых массивов. Результат — новый битовый массив.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapAndnot(bitmap,bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapToArray(bitmapAndnot(bitmapBuild([1,2,3]),bitmapBuild([3,4,5]))) AS res
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res───┐
│ [1,2] │
└───────┘
```
## bitmapCardinality
2019-07-30 06:47:38 +00:00
Возвращает кардинальность битового массива в виде значения типа `UInt64` .
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapCardinality(bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapCardinality(bitmapBuild([1, 2, 3, 4, 5])) AS res
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─┐
│ 5 │
└─────┘
```
## bitmapAndCardinality
2019-07-30 06:47:38 +00:00
Выполняет логическое И и возвращает кардинальность (`UInt64`) результирующего битового массива.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapAndCardinality(bitmap,bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapAndCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─┐
│ 1 │
└─────┘
```
## bitmapOrCardinality
2019-07-30 06:47:38 +00:00
Выполняет логическое ИЛИ и возвращает кардинальность (`UInt64`) результирующего битового массива.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapOrCardinality(bitmap,bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapOrCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─┐
│ 5 │
└─────┘
```
## bitmapXorCardinality
2019-07-30 06:47:38 +00:00
Выполняет логическое исключающее ИЛИ и возвращает кардинальность (`UInt64`) результирующего битового массива.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapXorCardinality(bitmap,bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapXorCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─┐
│ 4 │
└─────┘
```
## bitmapAndnotCardinality
2019-07-30 06:47:38 +00:00
Выполняет логическое отрицание И и возвращает кардинальность (`UInt64`) результирующего битового массива.
2019-06-09 17:39:45 +00:00
2019-09-23 15:31:46 +00:00
```sql
2019-06-09 17:39:45 +00:00
bitmapAndnotCardinality(bitmap,bitmap)
```
**Параметры**
2019-07-30 06:47:38 +00:00
- `bitmap` – битовый массив.
2019-06-09 17:39:45 +00:00
**Пример**
```sql
SELECT bitmapAndnotCardinality(bitmapBuild([1,2,3]),bitmapBuild([3,4,5])) AS res;
```
2019-09-23 15:31:46 +00:00
```text
2019-06-09 17:39:45 +00:00
┌─res─┐
│ 2 │
└─────┘
```
[Оригинальная статья ](https://clickhouse.yandex/docs/ru/query_language/functions/bitmap_functions/ ) <!--hide-->