bitmap_count
function
Applies to: Databricks SQL 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.
Arguments
expr
: ABINARY
expression, typically produced by bitmap_construct_agg().
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