BITMAP函数
本文档主要介绍和 BITMAP 类型相关的内置函数。
- 
标量函数 1.to_bitmap 
 2.bitmap_hash
 3.bitmap_count
 4.bitmap_empty
 5.bitmap_or
 6.bitmap_and
 7.bitmap_xor
 8.bitmap_to_string
 9.bitmap_from_string
 10.bitmap_contains
 11.bitmap_has_any
 12.bitmap_min
 13.bitmap_not
- 
聚合函数 1.bitmap_union 
 2.bitmap_union_int
 3.bitmap_union_count
 4.bitmap_intersect
 5.intersect_count
TO_BITMAP
Description
BITMAP to_bitmap(int i)- 功能:将一个整型转换为一个 BITMAP 类型。最大支持 BIGINT 类型。多用于导入时,将源数据中的整型列映射到 Doris 表中的 BITMAP 列。
- 返回值:BITMAP 类型
Example
mysql> select bitmap_to_string(to_bitmap("123"));
+------------------------------------+
| bitmap_to_string(to_bitmap('123')) |
+------------------------------------+
| 123                                |
+------------------------------------+
mysql> select bitmap_to_string(to_bitmap(1));
+--------------------------------+
| bitmap_to_string(to_bitmap(1)) |
+--------------------------------+
| 1                              |
+--------------------------------+因为 BITMAP 类型本身为二进制类型,无法在 MySQL 客户端展示。所以这里使用
bitmap_to_string对结果进行可视化展示。
Keywords
to_bitmap, bitmapBITMAP_HASH
Description
BITMAP bitmap_hash(string s)- 功能:将一个字符串通过 Hash 算法映射为32位整型,然后再转换为 BITMAP 类型。多用于导入时,将源数据中的非整型列映射到 Doris 表中的 BITMAP 列。因为采用 Hash 算法,所以可能会产生 Hash 冲突。即不同的字符串可能产生相同的 BITMAP 值。所以只能用于近似计算。
- 返回值:BITMAP 类型
Example
mysql> select bitmap_to_string(bitmap_hash("abc"));
+--------------------------------------+
| bitmap_to_string(bitmap_hash('abc')) |
+--------------------------------------+
| 3409700625                           |
+--------------------------------------+Keywords
bitmap_hash, bitmapBITMAP_COUNT
Description
bigint bitmap_count(bitmap s)- 功能:计算一个 BITMAP 中 1 的个数。
- 返回值:bigint 类型。
Example
mysql> select bitmap_count(bitmap_from_string("1,2,3"));
+-------------------------------------------+
| bitmap_count(bitmap_from_string('1,2,3')) |
+-------------------------------------------+
|                                         3 |
+-------------------------------------------+
mysql> select bitmap_count(bitmap_from_string("1,2,3,4"));
+---------------------------------------------+
| bitmap_count(bitmap_from_string('1,2,3,4')) |
+---------------------------------------------+
|                                           4 |
+---------------------------------------------+Keywords
bitmap_count, bitmapBITMAP_EMPTY
Description
bitmap bitmap_empty()- 功能:返回一个空的 BITMAP。通常用于导入时,产生一个空 bitmap。
- 返回值:bitmap 类型。
Example
mysql> select bitmap_to_string(bitmap_empty());
+----------------------------------+
| bitmap_to_string(bitmap_empty()) |
+----------------------------------+
|                                  |
+----------------------------------+Keywords
bitmap_empty, bitmapBITMAP_OR
Description
bitmap bitmap_or(bitmap a, bitmap b)- 功能:返回两个 bitmap 的并集。
- 返回值:bitmap 类型。
Example
mysql> select bitmap_to_string(bitmap_or(bitmap_from_string("1,2,3,4"), bitmap_from_string("4,5,6")));
+-----------------------------------------------------------------------------------------+
| bitmap_to_string(bitmap_or(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))) |
+-----------------------------------------------------------------------------------------+
| 1,2,3,4,5,6                                                                             |
+-----------------------------------------------------------------------------------------+Keywords
bitmap_or, bitmapBITMAP_AND
Description
bitmap bitmap_and(bitmap a, bitmap b)- 功能:返回两个 bitmap 的交集。
- 返回值:bitmap 类型。
Example
mysql> select bitmap_to_string(bitmap_and(bitmap_from_string("1,2,3,4"), bitmap_from_string("4,5,6")));
+------------------------------------------------------------------------------------------+
| bitmap_to_string(bitmap_and(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))) |
+------------------------------------------------------------------------------------------+
| 4                                                                                        |
+------------------------------------------------------------------------------------------+Keywords
bitmap_and, bitmapBITMAP_XOR
Description
bitmap bitmap_xor(bitmap a, bitmap b)- 功能:返回两个 bitmap 异或的结果。
- 返回值:bitmap 类型。
Example
mysql> select bitmap_to_string(bitmap_xor(bitmap_from_string("1,2,3,4"), bitmap_from_string("4,5,6")));
+------------------------------------------------------------------------------------------+
| bitmap_to_string(bitmap_and(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))) |
+------------------------------------------------------------------------------------------+
| 1,2,3,5,6                                                                                |
+------------------------------------------------------------------------------------------+Keywords
bitmap_xor, bitmapBITMAP_TO_STRING
Description
string bitmap_to_string(bitmap a)- 功能:以字符串的形式返回一个 bitmap 的内容。如一个 bitmap的第1位,第3位为1,则返回 1,3。
- 返回值:字符串。
Example
mysql> select bitmap_to_string(bitmap_from_string("4,5,6"));
+-----------------------------------------------+
| bitmap_to_string(bitmap_from_string('4,5,6')) |
+-----------------------------------------------+
| 4,5,6                                         |
+-----------------------------------------------+Keywords
bitmap_to_string, bitmapBITMAP_FROM_STRING
Description
bitmap bitmap_from_string(string a)- 功能:解析一个字符串,并返回一个 bitmap。字符串是以逗号分隔的数值列表,如:1,200,301
- 返回值:bitmap 类型。
Example
mysql> select bitmap_to_string(bitmap_from_string("4, 5, 6"));
+-----------------------------------------------+
| bitmap_to_string(bitmap_from_string('4,5,6')) |
+-----------------------------------------------+
| 4,5,6                                         |
+-----------------------------------------------+Keywords
bitmap_from_string, bitmapBITMAP_CONTAINS
Description
boolean bitmap_contains(bitmap a, bigint b)- 功能:判断一个bitmap中是否包含指定的数值。
- 返回值:bool 类型。
Example
mysql> select bitmap_contains(bitmap_from_string("4, 5 ,6"), 4);
+---------------------------------------------------+
| bitmap_contains(bitmap_from_string('4, 5 ,6'), 4) |
+---------------------------------------------------+
|                                                 1 |
+---------------------------------------------------+
mysql> select bitmap_contains(bitmap_from_string("4, 5 ,6"), 7);
+---------------------------------------------------+
| bitmap_contains(bitmap_from_string('4, 5 ,6'), 7) |
+---------------------------------------------------+
|                                                 0 |
+---------------------------------------------------+Keywords
bitmap_contains, bitmapBITMAP_MIN
Description
BIGINT BITMAP_MIN(BITMAP input)- 功能:计算并返回 bitmap 中的最小值.
- 返回值:int类型。
Example
mysql> select bitmap_min(bitmap_from_string('')) value;
+-------+
| value |
+-------+
|  NULL |
+-------+
mysql> select bitmap_min(bitmap_from_string('1,9999999999')) value;
+-------+
| value |
+-------+
|     1 |
+-------+Keywords
BITMAP_MIN,BITMAPBITMAP_NOT
Description
BITMAP BITMAP_NOT(BITMAP lhs, BITMAP rhs)- 功能:计算lhs减去rhs之后的集合,返回新的bitmap.
- 返回值:int 类型。
Example
mysql> select bitmap_count(bitmap_not(bitmap_from_string('2,3'),bitmap_from_string('1,2,3,4'))) cnt;
+------+
| cnt  |
+------+
|    0 |
+------+
mysql> select bitmap_to_string(bitmap_not(bitmap_from_string('2,3,5'),bitmap_from_string('1,2,3,4')));
+----------------------------------------------------------------------------------------+
| bitmap_to_string(bitmap_xor(bitmap_from_string('2,3,5'), bitmap_from_string('1,2,3,4'))) |
+----------------------------------------------------------------------------------------+
| 5                                                                                      |
+----------------------------------------------------------------------------------------+Keywords
BITMAP_NOT,BITMAPBITMAP_HAS_ANY
Description
boolean bitmap_has_any(bitmap a, bitmap b)- 功能:判断两个bitmap是否有交集。
- 返回值:bool 类型。
Example
mysql> select bitmap_has_any(bitmap_from_string("1,2,3,4"), bitmap_from_string("4,5,6"));
+----------------------------------------------------------------------------+
| bitmap_has_any(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6')) |
+----------------------------------------------------------------------------+
|                                                                          1 |
+----------------------------------------------------------------------------+
mysql> select bitmap_has_any(bitmap_from_string("1,2,3"), bitmap_from_string("4,5,6"));
+--------------------------------------------------------------------------+
| bitmap_has_any(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6')) |
+--------------------------------------------------------------------------+
|                                                                        0 |
+--------------------------------------------------------------------------+Keywords
bitmap_has_any, bitmapBITMAP_UNION
Description
bitmap bitmap_union(bitmap a)- 功能:聚合函数,返回一组bitmap的并集。
- 返回值:bitmap 类型。
Example
mysql> select k1, bitmap_to_string(bitmap_union(v1)) from tbl1 group by k1;
+------+--------------------------------------+
| k1   | bitmap_to_string(bitmap_union(`v1`)) |
+------+--------------------------------------+
|    2 | 2,3,4                                |
|    1 | 1,2,3                                |
+------+--------------------------------------+Keywords
bitmap_union, bitmapBITMAP_UNION_INT
Description
bigint bitmap_union_int(int a)- 功能:聚合函数,利用 bitmap 数据结构计算整型列的去重值。等价于 count(distinct a)。其中参数类型支持 TINYINT、SMALLINT、INT、BIGINT。该函数可以利用 bitmap 数据结构,使用更少的系统资源得到去重值。
- 返回值:bigint 类型。
Example
mysql> select bitmap_union_int(k1) from tbl1;
+------------------------+
| bitmap_union_int(`k1`) |
+------------------------+
|                      2 |
+------------------------+Keywords
bitmap_union_int, bitmapBITMAP_UNION_COUNT
Description
bigint bitmap_union_count(bitmap a)- 功能:聚合函数,返回一组bitmap的并集结果中,1的个数。等价于:bitmap_count(bitmap_union(a))。推荐直接使用 bitmap_union_count`,效率更高。
- 返回值:bigint 类型。
Example
mysql> select k1, bitmap_union_count(v1) from tbl1 group by k1;
+------+--------------------------+
| k1   | bitmap_union_count(`v1`) |
+------+--------------------------+
|    2 |                        3 |
|    1 |                        3 |
+------+--------------------------+Keywords
bitmap_union, bitmapBITMAP_INTERSECT
Description
bitmap bitmap_intersect(bitmap a)- 功能:聚合函数,返回一组bitmap的交集。
- 返回值:bitmap 类型。
该聚合函数的功能对应于 bitmap_union。但是用场景上和 bitmap_union 稍有不同。
因为目前 Doris 在建表时,对于 bitmap 类型的列,必须指定 bitmap_union 聚合方式(注意这里指的是聚合方式,而非聚合函数)。而聚合方式必须是用等价的聚合函数查询才有实际意义。因此对于 bitmap 类型的列,如下查询是有意义的:
select k1, bitmap_union(v1) from tbl group by k1;而如下查询是无意义的:
select k1, bitmap_intersect(v1) from tbl group by k1;bitmap_intersect 的具体使用方式见示例。
Example
表结构如下:
k1  INT
v1  BITMAP  BITMAP_UNION查询时,需要先通过 bitmap_union 使用子查询将数据进行聚合后,在再外层通过 bitmap_intersect 求交集:
mysql> select bitmap_to_string(bitmap_intersect(x)) from (select k1, bitmap_union(v1) x from tbl1 group by k1) a;
+-----------------------------------------+
| bitmap_to_string(bitmap_intersect(`x`)) |
+-----------------------------------------+
| 2,3                                     |
+-----------------------------------------+Keywords
bitmap_intersect, bitmapINTERSECT_COUNT
Description
bigint intersect_count(bitmap a, column c, type cond1[, type cond2, ...])- 
功能: 该函数通常用于计算留存等业务场景。第一个参数为要进行留存计算的 bitmap 列。第二个参数为需要进行交集计算的列名。之后的变长参数为第二个参数对应列的一组取值。 该函数类似一个语法糖,相当于组合了以下函数: bitmap_count+bitmap_intersect+bitmap_union+where
- 返回值:bigint 类型。
Example
计算 2020-10-01 和 2020-10-02 两天的用户留存。表结构如下:
dt      DATETIME
user_id BITMAPmysql>  select intersect_count(user_id, dt, '2020-10-01', '2020-10-02'), intersect_count(user_id, dt, '2020-10-01') from tbl where dt in ('2020-10-01', '2020-10-02');
+--------------------------------------------------------------+------------------------------------------------+
| intersect_count(`user_id`, `dt`, '2020-10-01', '2020-10-02') | intersect_count(`user_id`, `dt`, '2020-10-01') |
+--------------------------------------------------------------+------------------------------------------------+
|                                                            3 |                                              7 |
+--------------------------------------------------------------+------------------------------------------------+以上结果表示,2020-10-01 的访客数量为 7,而这些访客在 2020-10-02 再次访问的数量为 3。
其中 intersect_count(user_id, dt, '2020-10-01', '2020-10-02') 等价于如下语句:
select bitmap_count(bitmap_intersect(b))
from 
(
    select dt, bitmap_union(user_id) b from tbl2 
    where dt in ('2020-10-01', '2020-10-02')
    group by dt
) t2Keywords
intersect_count, bitmap