---
title: bitmap_or_agg aggregate function
description: Learn the syntax of the bitmap\_or\_agg function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_or_agg
---

# `bitmap_or_agg` aggregate function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 13.3 LTS and above

Returns the bitwise `OR` of all `BINARY` input values in the group.

The function is commonly used to count the number of distinct integral numbers in combination with the [bitmap\_bucket\_number()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_bucket_number.md), [bitmap\_count()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_count.md), [bitmap\_bit\_position()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_bit_position.md), and [bitmap\_construct\_agg()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_or_agg.md#) functions.

To aggregate bitmaps in form of integral numerics, use the [bit\_or()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bit_or.md) aggregate function.
To aggregate bit positions into a `BINARY` bitmap, use [bitmap\_construct\_agg()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_or_agg.md#).

## Syntax

```
bitmap_or_agg(expr) [FILTER ( WHERE cond ) ]
```

This function can also be invoked as a [window function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) using the `OVER` clause.

## Arguments

* `expr`: A `BINARY` of length `<= 4096` representing a bitmap. Databricks truncates longer values to 4096.
* `cond`: An optional boolean expression filtering the rows used for aggregation.

## Returns

A `BINARY` of length 4096.

## Examples

```sql
> SELECT hex(trim(TRAILING X'00' FROM bitmap_or_agg(val))) FROM VALUES(x'10'), (x'02') AS T(val);
 12

-- Count the number of distinct values across two tables
> SELECT sum(num_distinct) AS num_distinct
    FROM (SELECT bucket, bitmap_count(bitmap_or_agg(num_distinct)) AS num_distinct
            FROM ((SELECT bitmap_bucket_number(val) AS bucket,
                          bitmap_construct_agg(bitmap_bit_position(val)) AS num_distinct
                     FROM VALUES(1), (2), (1), (-1), (5), (0), (5) AS t(val)
                     GROUP BY ALL)
                  UNION ALL
                  (SELECT bitmap_bucket_number(val) AS bucket,
                          bitmap_construct_agg(bitmap_bit_position(val)) AS num_distinct
                     FROM VALUES(3), (1), (-1), (6), (5), (1), (5), (8) AS t(val)
                     GROUP BY ALL))
            GROUP BY ALL);
  8
```

## Related functions

* [`bit_or` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bit_or.md)
* [`bitmap_bit_position` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_bit_position.md)
* [`bitmap_bucket_number` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_bucket_number.md)
* [`bitmap_construct_agg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_construct_agg.md)
* [`bitmap_count` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_count.md)
