bitmap_count function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 13.3 LTS and above

Returns the number of bits set in a BINARY string representing a bitmap. This function is typically used to count distinct value in combination with the bitmap_bucket_number() and the bitmap_construct_agg() functions.

To count bits in a BIGINT expression use bit_count function.

Syntax

bitmap_count(expr)

Arguments

Returns

A BIGINT that is >=0.

Examples

> SELECT bitmap_count(X'00');
 0

> SELECT bitmap_count(X'');
 0

> SELECT bitmap_count(X'7700CC');
 10

-- Count the number of distinct values
> SELECT sum(num_distinct) AS num_distinct
    FROM (SELECT bitmap_bucket_number(val),
                 bitmap_count(bitmap_construct_agg(bitmap_bit_position(val)))
            FROM VALUES(1), (2), (1), (-1), (5), (0), (5) AS t(val)
            GROUP BY ALL) AS distinct_vals_by_bucket(bucket, num_distinct)
  5