上云无忧 > 文档中心 > 百度数据仓库 Palo SQL语法手册 - BITMAP函数
百度数据仓库 Palo Doris版
百度数据仓库 Palo SQL语法手册 - 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
*此产品及展示信息均由百度智能云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

本文档主要介绍和 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 类型。多用于导入时,将源数据中的整型列映射到 PALO 表中的 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, bitmap

BITMAP_HASH

Description

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

Example

mysql> select bitmap_to_string(bitmap_hash("abc")); +--------------------------
------------+ | bitmap_to_string(bitmap_hash('abc')) | +------------------------------
-------+ | 3409700625 | +--------------------------------------+

Keywords

bitmap_hash, bitmap

BITMAP_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, bitmap

BITMAP_EMPTY

Description

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

Example

mysql> select bitmap_to_string(bitmap_empty()); +--------------------------------
--+ | bitmap_to_string(bitmap_empty()) | +--------------------------------
--+ | | +----------------------------------+

Keywords

bitmap_empty, bitmap

BITMAP_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, bitmap

BITMAP_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, bitmap

BITMAP_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, bitmap

BITMAP_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, bitmap

BITMAP_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, bitmap

BITMAP_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, bitmap

BITMAP_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,BITMAP

BITMAP_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,BITMAP

BITMAP_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, bitmap

BITMAP_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, bitmap

BITMAP_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, bitmap

BITMAP_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, bitmap

BITMAP_INTERSECT

Description

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

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

因为目前 PALO 在建表时,对于 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, bitmap

INTERSECT_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 BITMAP

mysql> 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 ) t2

Keywords

intersect_count, bitmap

相似文档
  • 本文档主要介绍 HLL(HyperLogLog) 类型相关的内置函数。 标量函数: 1.hll_cardinality 2.hll_hash 3.hll_empty 聚合函数: 1.hll_union,hll_raw_agg 2.hll_union_agg
  • PALO 目前支持3个 JSON 解析函数: 1.get_json_int 2.get_json_string 3.get_json_double GET_JSON_INT Description: get_json_int(VARCHAR json_str, VARCHAR json_path)
  • PALO 支持以下位操作函数: 1.bitand 2.bitnot 3.bitor 4.bitxor BITAND Desciption: bitand(integer_type a, same_type b) 功能:按位与运算 返回类型: 和输入类型相同
  • PALO支持的哈希函数如下: 1.murmur_hash3_32 MURMUR_HASH3_32 Description: INT MURMUR_HASH3_32(VARCHAR input, ...) 功能:返回输入字符串的32位murmur3 hash值 返回类型:string类型
  • PALO支持的字符串函数如下: 1.append_trailing_char_if_absent 2.ascii 3.concat 4.concat_ws 5.ends_with 6.find_in_set 7.group_concat 8.instr 9.length,char_length,character_length 10.locate 11.lower,lcase 12.lpad 13.ltrim 14.money_format 15.null_or_empty 16.parse_url 17.regexp_extract 18.regexp_replace 19.repeat 20.replace 21.reverse 22.rpad 23.rtrim 24.space 25.split_part 26.starts_with 27.strleft,left 28.strright,right 29.substr,substring 30.trim 31.upper,ucase
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部