搜索本产品文档关键词
BITMAP函数
所有文档
menu

数据仓库 PALO

BITMAP函数

本文档主要介绍和 BITMAP 类型相关的内置函数。

TO_BITMAP

Description

SQL
1BITMAP to_bitmap(int i)
  • 功能:将一个整型转换为一个 BITMAP 类型。最大支持 BIGINT 类型。多用于导入时,将源数据中的整型列映射到 PALO 表中的 BITMAP 列。
  • 返回值:BITMAP 类型

Example

SQL
1mysql> select bitmap_to_string(to_bitmap("123"));
2+------------------------------------+
3| bitmap_to_string(to_bitmap('123')) |
4+------------------------------------+
5| 123                                |
6+------------------------------------+
7
8mysql> select bitmap_to_string(to_bitmap(1));
9+--------------------------------+
10| bitmap_to_string(to_bitmap(1)) |
11+--------------------------------+
12| 1                              |
13+--------------------------------+

因为 BITMAP 类型本身为二进制类型,无法在 MySQL 客户端展示。所以这里使用 bitmap_to_string 对结果进行可视化展示。

Keywords

Plain Text
1to_bitmap, bitmap

BITMAP_HASH

Description

SQL
1BITMAP bitmap_hash(string s)
  • 功能:将一个字符串通过 Hash 算法映射为32位整型,然后再转换为 BITMAP 类型。多用于导入时,将源数据中的非整型列映射到 PALO 表中的 BITMAP 列。因为采用 Hash 算法,所以可能会产生 Hash 冲突。即不同的字符串可能产生相同的 BITMAP 值。所以只能用于近似计算。
  • 返回值:BITMAP 类型

Example

SQL
1mysql> select bitmap_to_string(bitmap_hash("abc"));
2+--------------------------------------+
3| bitmap_to_string(bitmap_hash('abc')) |
4+--------------------------------------+
5| 3409700625                           |
6+--------------------------------------+

Keywords

Plain Text
1bitmap_hash, bitmap

BITMAP_COUNT

Description

SQL
1bigint bitmap_count(bitmap s)
  • 功能:计算一个 BITMAP 中 1 的个数。
  • 返回值:bigint 类型。

Example

SQL
1mysql> select bitmap_count(bitmap_from_string("1,2,3"));
2+-------------------------------------------+
3| bitmap_count(bitmap_from_string('1,2,3')) |
4+-------------------------------------------+
5|                                         3 |
6+-------------------------------------------+
7
8mysql> select bitmap_count(bitmap_from_string("1,2,3,4"));
9+---------------------------------------------+
10| bitmap_count(bitmap_from_string('1,2,3,4')) |
11+---------------------------------------------+
12|                                           4 |
13+---------------------------------------------+

Keywords

Plain Text
1bitmap_count, bitmap

BITMAP_EMPTY

Description

SQL
1bitmap bitmap_empty()
  • 功能:返回一个空的 BITMAP。通常用于导入时,产生一个空 bitmap。
  • 返回值:bitmap 类型。

Example

SQL
1mysql> select bitmap_to_string(bitmap_empty());
2+----------------------------------+
3| bitmap_to_string(bitmap_empty()) |
4+----------------------------------+
5|                                  |
6+----------------------------------+

Keywords

Plain Text
1bitmap_empty, bitmap

BITMAP_OR

Description

SQL
1bitmap bitmap_or(bitmap a, bitmap b)
  • 功能:返回两个 bitmap 的并集。
  • 返回值:bitmap 类型。

Example

SQL
1mysql> select bitmap_to_string(bitmap_or(bitmap_from_string("1,2,3,4"), bitmap_from_string("4,5,6")));
2+-----------------------------------------------------------------------------------------+
3| bitmap_to_string(bitmap_or(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))) |
4+-----------------------------------------------------------------------------------------+
5| 1,2,3,4,5,6                                                                             |
6+-----------------------------------------------------------------------------------------+

Keywords

Plain Text
1bitmap_or, bitmap

BITMAP_AND

Description

SQL
1bitmap bitmap_and(bitmap a, bitmap b)
  • 功能:返回两个 bitmap 的交集。
  • 返回值:bitmap 类型。

Example

SQL
1mysql> select bitmap_to_string(bitmap_and(bitmap_from_string("1,2,3,4"), bitmap_from_string("4,5,6")));
2+------------------------------------------------------------------------------------------+
3| bitmap_to_string(bitmap_and(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))) |
4+------------------------------------------------------------------------------------------+
5| 4                                                                                        |
6+------------------------------------------------------------------------------------------+

Keywords

Plain Text
1bitmap_and, bitmap

BITMAP_XOR

Description

SQL
1bitmap bitmap_xor(bitmap a, bitmap b)
  • 功能:返回两个 bitmap 异或的结果。
  • 返回值:bitmap 类型。

Example

SQL
1mysql> select bitmap_to_string(bitmap_xor(bitmap_from_string("1,2,3,4"), bitmap_from_string("4,5,6")));
2+------------------------------------------------------------------------------------------+
3| bitmap_to_string(bitmap_and(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6'))) |
4+------------------------------------------------------------------------------------------+
5| 1,2,3,5,6                                                                                |
6+------------------------------------------------------------------------------------------+

Keywords

Plain Text
1bitmap_xor, bitmap

BITMAP_TO_STRING

Description

SQL
1string bitmap_to_string(bitmap a)
  • 功能:以字符串的形式返回一个 bitmap 的内容。如一个 bitmap的第1位,第3位为1,则返回 1,3
  • 返回值:字符串。

Example

SQL
1mysql> select bitmap_to_string(bitmap_from_string("4,5,6"));
2+-----------------------------------------------+
3| bitmap_to_string(bitmap_from_string('4,5,6')) |
4+-----------------------------------------------+
5| 4,5,6                                         |
6+-----------------------------------------------+

Keywords

Plain Text
1bitmap_to_string, bitmap

BITMAP_FROM_STRING

Description

SQL
1bitmap bitmap_from_string(string a)
  • 功能:解析一个字符串,并返回一个 bitmap。字符串是以逗号分隔的数值列表,如:1,200,301
  • 返回值:bitmap 类型。

Example

SQL
1mysql> select bitmap_to_string(bitmap_from_string("4, 5, 6"));
2+-----------------------------------------------+
3| bitmap_to_string(bitmap_from_string('4,5,6')) |
4+-----------------------------------------------+
5| 4,5,6                                         |
6+-----------------------------------------------+

Keywords

Plain Text
1bitmap_from_string, bitmap

BITMAP_CONTAINS

Description

SQL
1boolean bitmap_contains(bitmap a, bigint b)
  • 功能:判断一个bitmap中是否包含指定的数值。
  • 返回值:bool 类型。

Example

SQL
1mysql> select bitmap_contains(bitmap_from_string("4, 5 ,6"), 4);
2+---------------------------------------------------+
3| bitmap_contains(bitmap_from_string('4, 5 ,6'), 4) |
4+---------------------------------------------------+
5|                                                 1 |
6+---------------------------------------------------+
7
8mysql> select bitmap_contains(bitmap_from_string("4, 5 ,6"), 7);
9+---------------------------------------------------+
10| bitmap_contains(bitmap_from_string('4, 5 ,6'), 7) |
11+---------------------------------------------------+
12|                                                 0 |
13+---------------------------------------------------+

Keywords

Plain Text
1bitmap_contains, bitmap

BITMAP_MIN

Description

SQL
1BIGINT BITMAP_MIN(BITMAP input)
  • 功能:计算并返回 bitmap 中的最小值.
  • 返回值:int类型。

Example

SQL
1mysql> select bitmap_min(bitmap_from_string('')) value;
2+-------+
3| value |
4+-------+
5|  NULL |
6+-------+
7
8mysql> select bitmap_min(bitmap_from_string('1,9999999999')) value;
9+-------+
10| value |
11+-------+
12|     1 |
13+-------+

Keywords

Plain Text
1BITMAP_MIN,BITMAP

BITMAP_NOT

Description

SQL
1BITMAP BITMAP_NOT(BITMAP lhs, BITMAP rhs)
  • 功能:计算lhs减去rhs之后的集合,返回新的bitmap.
  • 返回值:int 类型。

Example

SQL
1mysql> select bitmap_count(bitmap_not(bitmap_from_string('2,3'),bitmap_from_string('1,2,3,4'))) cnt;
2+------+
3| cnt  |
4+------+
5|    0 |
6+------+
7
8mysql> select bitmap_to_string(bitmap_not(bitmap_from_string('2,3,5'),bitmap_from_string('1,2,3,4')));
9+----------------------------------------------------------------------------------------+
10| bitmap_to_string(bitmap_xor(bitmap_from_string('2,3,5'), bitmap_from_string('1,2,3,4'))) |
11+----------------------------------------------------------------------------------------+
12| 5                                                                                      |
13+----------------------------------------------------------------------------------------+

Keywords

Plain Text
1BITMAP_NOT,BITMAP

BITMAP_HAS_ANY

Description

SQL
1boolean bitmap_has_any(bitmap a, bitmap b)
  • 功能:判断两个bitmap是否有交集。
  • 返回值:bool 类型。

Example

SQL
1mysql> select bitmap_has_any(bitmap_from_string("1,2,3,4"), bitmap_from_string("4,5,6"));
2+----------------------------------------------------------------------------+
3| bitmap_has_any(bitmap_from_string('1,2,3,4'), bitmap_from_string('4,5,6')) |
4+----------------------------------------------------------------------------+
5|                                                                          1 |
6+----------------------------------------------------------------------------+
7
8mysql> select bitmap_has_any(bitmap_from_string("1,2,3"), bitmap_from_string("4,5,6"));
9+--------------------------------------------------------------------------+
10| bitmap_has_any(bitmap_from_string('1,2,3'), bitmap_from_string('4,5,6')) |
11+--------------------------------------------------------------------------+
12|                                                                        0 |
13+--------------------------------------------------------------------------+

Keywords

Plain Text
1bitmap_has_any, bitmap

BITMAP_UNION

Description

SQL
1bitmap bitmap_union(bitmap a)
  • 功能:聚合函数,返回一组bitmap的并集。
  • 返回值:bitmap 类型。

Example

SQL
1mysql> select k1, bitmap_to_string(bitmap_union(v1)) from tbl1 group by k1;
2+------+--------------------------------------+
3| k1   | bitmap_to_string(bitmap_union(`v1`)) |
4+------+--------------------------------------+
5|    2 | 2,3,4                                |
6|    1 | 1,2,3                                |
7+------+--------------------------------------+

Keywords

Plain Text
1bitmap_union, bitmap

BITMAP_UNION_INT

Description

SQL
1bigint bitmap_union_int(int a)
  • 功能:聚合函数,利用 bitmap 数据结构计算整型列的去重值。等价于 count(distinct a)。其中参数类型支持 TINYINT、SMALLINT、INT、BIGINT。该函数可以利用 bitmap 数据结构,使用更少的系统资源得到去重值。
  • 返回值:bigint 类型。

Example

SQL
1mysql> select bitmap_union_int(k1) from tbl1;
2+------------------------+
3| bitmap_union_int(`k1`) |
4+------------------------+
5|                      2 |
6+------------------------+

Keywords

Plain Text
1bitmap_union_int, bitmap

BITMAP_UNION_COUNT

Description

SQL
1bigint bitmap_union_count(bitmap a)
  • 功能:聚合函数,返回一组bitmap的并集结果中,1的个数。等价于:bitmap_count(bitmap_union(a))。推荐直接使用 bitmap_union_count`,效率更高。
  • 返回值:bigint 类型。

Example

SQL
1mysql> select k1, bitmap_union_count(v1) from tbl1 group by k1;
2+------+--------------------------+
3| k1   | bitmap_union_count(`v1`) |
4+------+--------------------------+
5|    2 |                        3 |
6|    1 |                        3 |
7+------+--------------------------+

Keywords

Plain Text
1bitmap_union, bitmap

BITMAP_INTERSECT

Description

SQL
1bitmap bitmap_intersect(bitmap a)
  • 功能:聚合函数,返回一组bitmap的交集。
  • 返回值:bitmap 类型。

该聚合函数的功能对应于 bitmap_union。但是用场景上和 bitmap_union 稍有不同。

因为目前 PALO 在建表时,对于 bitmap 类型的列,必须指定 bitmap_union 聚合方式(注意这里指的是聚合方式,而非聚合函数)。而聚合方式必须是用等价的聚合函数查询才有实际意义。因此对于 bitmap 类型的列,如下查询是有意义的:

SQL
1select k1, bitmap_union(v1) from tbl group by k1;

而如下查询是无意义的:

SQL
1select k1, bitmap_intersect(v1) from tbl group by k1;

bitmap_intersect 的具体使用方式见示例。

Example

表结构如下:

Plain Text
1k1  INT
2v1  BITMAP  BITMAP_UNION

查询时,需要先通过 bitmap_union 使用子查询将数据进行聚合后,在再外层通过 bitmap_intersect 求交集:

SQL
1mysql> select bitmap_to_string(bitmap_intersect(x)) from (select k1, bitmap_union(v1) x from tbl1 group by k1) a;
2+-----------------------------------------+
3| bitmap_to_string(bitmap_intersect(`x`)) |
4+-----------------------------------------+
5| 2,3                                     |
6+-----------------------------------------+

Keywords

Plain Text
1bitmap_intersect, bitmap

INTERSECT_COUNT

Description

SQL
1bigint 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 两天的用户留存。表结构如下:

Plain Text
1dt      DATETIME
2user_id BITMAP
SQL
1mysql>  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');
2+--------------------------------------------------------------+------------------------------------------------+
3| intersect_count(`user_id`, `dt`, '2020-10-01', '2020-10-02') | intersect_count(`user_id`, `dt`, '2020-10-01') |
4+--------------------------------------------------------------+------------------------------------------------+
5|                                                            3 |                                              7 |
6+--------------------------------------------------------------+------------------------------------------------+

以上结果表示,2020-10-01 的访客数量为 7,而这些访客在 2020-10-02 再次访问的数量为 3。

其中 intersect_count(user_id, dt, '2020-10-01', '2020-10-02') 等价于如下语句:

SQL
1select bitmap_count(bitmap_intersect(b))
2from 
3(
4    select dt, bitmap_union(user_id) b from tbl2 
5    where dt in ('2020-10-01', '2020-10-02')
6    group by dt
7) t2

Keywords

Plain Text
1intersect_count, bitmap
上一篇
语法帮助
下一篇
HLL函数