HLL函数和操作符

哈希函数

  • hll_hash_boolean(bool)

    描述:对bool类型数据计算哈希值。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_boolean(FALSE);
      hll_hash_boolean   
    ---------------------
     -5451962507482445012
    (1 row)
    
  • hll_hash_boolean(bool, int32)

    描述:设置hash seed(即改变哈希策略)并对bool类型数据计算哈希值。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_boolean(FALSE, 10);
      hll_hash_boolean  
    --------------------
     -1169037589280886076
    (1 row)
    
  • hll_hash_smallint(smallint)

    描述:对smallint类型数据计算哈希值。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_smallint(100::smallint);
      hll_hash_smallint  
    ---------------------
     962727970174027904
    (1 row)
    

说明:

数值大小相同的参数使用不同数据类型的哈希函数计算,最后结果会不一样,因为不同类型哈希函数会选取不同的哈希计算策略。

  • hll_hash_smallint(smallint, int32)

    描述:设置hash seed(即改变哈希策略)同时对smallint类型数据计算哈希值。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_smallint(100::smallint, 10);
      hll_hash_smallint  
    ---------------------
     -9056177146160443041
    (1 row)
    
  • hll_hash_integer(integer)

    描述:对integer类型数据计算哈希值。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_integer(0);
       hll_hash_integer   
    ----------------------
     5156626420896634997
    (1 row)
    
  • hll_hash_integer(integer, int32)

    描述:对integer类型数据计算哈希值,并设置hashseed(即改变哈希策略)。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_integer(0, 10);
      hll_hash_integer  
    --------------------
     -5035020264353794276
    (1 row)
    
  • hll_hash_bigint(bigint)

    描述:对bigint类型数据计算哈希值。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_bigint(100::bigint);
       hll_hash_bigint   
    ---------------------
     -2401963681423227794
    (1 row)
    
  • hll_hash_bigint(bigint, int32)

    描述:对bigint类型数据计算哈希值,并设置hashseed(即改变哈希策略)。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_bigint(100::bigint, 10);
       hll_hash_bigint   
    ---------------------
     -2305749404374433531
    (1 row)
    
  • hll_hash_bytea(bytea)

    描述:对bytea类型数据计算哈希值。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_bytea(E'\\x');
     hll_hash_bytea 
    ----------------
     0
    (1 row)
    
  • hll_hash_bytea(bytea, int32)

    描述:对bytea类型数据计算哈希值,并设置hashseed(即改变哈希策略)。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_bytea(E'\\x', 10);
       hll_hash_bytea    
    ---------------------
     7233188113542599437
    (1 row)
    
  • hll_hash_text(text)

    描述:对text类型数据计算哈希值。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_text('AB');
        hll_hash_text    
    ---------------------
     -5666002586880275174
    (1 row)
    
  • hll_hash_text(text, int32)

    描述:对text类型数据计算哈希值,并设置hashseed(即改变哈希策略)。

    返回值类型:hll_hashval

    示例:

    openGauss=# SELECT hll_hash_text('AB', 10);
    hll_hash_text
    ---------------------
    -2215507121143724132
    (1 row)
    
  • hll_hash_any(anytype)

    描述:对任意类型数据计算哈希值。

    返回值类型:hll_hashval

    示例:

    openGauss=# select hll_hash_any(1);
         hll_hash_any     
    ----------------------
     -1316670585935156930
    (1 row)
    
    openGauss=# select hll_hash_any('08:00:2b:01:02:03'::macaddr);
         hll_hash_any     
    ----------------------
     -3719950434455589360
    (1 row)
    
  • hll_hash_any(anytype, int32)

    描述:对任意类型数据计算哈希值,并设置hashseed(即改变哈希策略)。

    返回值类型:hll_hashval

    示例:

    openGauss=# select hll_hash_any(1, 10);
         hll_hash_any     
    ----------------------
     7048553517657992351
    (1 row)
    
  • hll_hashval_eq(hll_hashval, hll_hashval)

    描述:比较两个hll_hashval类型数据是否相等。

    返回值类型:bool

    示例:

    openGauss=# select hll_hashval_eq(hll_hash_integer(1), hll_hash_integer(1));
     hll_hashval_eq 
    ----------------
     t
    (1 row)
    
  • hll_hashval_ne(hll_hashval, hll_hashval)

    描述:比较两个hll_hashval类型数据是否不相等。

    返回值类型:bool

    示例:

    openGauss=# select hll_hashval_ne(hll_hash_integer(1), hll_hash_integer(1));
     hll_hashval_ne 
    ----------------
     f
    (1 row)
    

日志函数

hll主要存在三种模式Explicit,Sparse,Full。当数据规模比较小的时候会使用Explicit模式,这种模式下distinct值的计算是没有误差的;随着distinct值越来越多,hll会先后转换为Sparse模式和Full模式,这两种模式在计算结果上没有任何区别,只影响hll函数的计算效率和hll对象的存储空间。下面的函数可以用于查看hll的一些参数。

  • hll_print(hll)

    描述:打印hll的一些debug参数信息。

    示例:

    openGauss=# select hll_print(hll_empty());
                                       hll_print
    -------------------------------------------------------------------------------
     type=1(HLL_EMPTY), log2m=14, log2explicit=10, log2sparse=12, duplicatecheck=0
    (1 row)
    
  • hll_type(hll)

    描述:查看当前hll的类型。返回值具体含义如下:返回值0,表示HLL_UNINIT,未初始化的hll对象;返回值1,表示HLL_EMPTY,hll空对象;返回值2,表示HLL_EXPLICIT,Explicit模式的hll对象;返回值3,表示HLL_SPARSE,Sparse模式的hll对象;返回值4,表示HLL_FULL,Full模式的hll对象;返回值5,表示HLL_UNDEFINED,不合法的hll对象。

    示例:

    openGauss=# select hll_type(hll_empty());
     hll_type 
    ----------
            1
    (1 row)
    
  • hll_log2m(hll)

    描述:查看当前hll数据结构中的log2m数值,log2m是分桶数的对数值,此值会影响最后hll计算distinct误差率,误差率计算公式为±1.04/√(2 ^ log2m)。当显式指定log2m的取值为10-16之间时,hll会设置分桶数为2log2m。当显示指定log2explicit为-1时,会采用内置默认值。

    示例:

    openGauss=# select hll_log2m(hll_empty());
     hll_log2m
    -----------
            14
    (1 row)
    
    openGauss=# select hll_log2m(hll_empty(10));
     hll_log2m
    -----------
            10
    (1 row)
    
    openGauss=# select hll_log2m(hll_empty(-1));
     hll_log2m
    -----------
            14
    (1 row)
    
  • hll_log2explicit(hll)

    描述:查看当前hll数据结构中的log2explicit数值。hll通常会由Explicit模式到Sparse模式再到Full模式,这个过程称为promotion hierarchy策略。可以通过调整log2explicit值的大小改变策略,比如log2explicit为0的时候就会跳过Explicit模式而直接进入Sparse模式。当显式指定log2explicit的取值为1-12之间时,hll会在数据段长度超过2log2explicit时转为Sparse模式。当显示指定log2explicit为-1时,会采用内置默认值。

    示例:

    openGauss=# select hll_log2explicit(hll_empty());
     hll_log2explicit
    ------------------
                   10
    (1 row)
    
    openGauss=# select hll_log2explicit(hll_empty(12, 8));
     hll_log2explicit
    ------------------
                    8
    (1 row)
    
    openGauss=# select hll_log2explicit(hll_empty(12, -1));
     hll_log2explicit
    ------------------
                   10
    (1 row)
    
  • hll_log2sparse(hll)

    描述:查看当前hll数据结构中的log2sparse数值。hll通常会由Explicit模式到Sparse模式再到Full模式,这个过程称为promotion hierarchy策略。可以通过调整log2sparse值的大小改变策略,比如log2sparse为0的时候就会跳过Sparse模式而直接进入Full模式。当显式指定Sparse的取值为1-14之间时,hll会在数据段长度超过2log2sparse时转为Full模式。当显示指定log2sparse为-1时,会采用内置默认值。

    示例:

    openGauss=# select hll_log2sparse(hll_empty());
     hll_log2sparse
    ----------------
                 12
    (1 row)
    
    openGauss=# select hll_log2sparse(hll_empty(12, 8, 10));
     hll_log2sparse
    ----------------
                 10
    (1 row)
    
    openGauss=# select hll_log2sparse(hll_empty(12, 8, -1));
     hll_log2sparse
    ----------------
                 12
    (1 row)
    
  • hll_duplicatecheck(hll)

    描述:是否启用duplicatecheck,0是关闭,1是开启。默认关闭,对于有较多重复值出现的情况,可以开启以提高效率。当显示指定duplicatecheck为-1时,会采用内置默认值。

    示例:

    openGauss=# select hll_duplicatecheck(hll_empty());
     hll_duplicatecheck
    --------------------
                      0
    (1 row)
    
    openGauss=# select hll_duplicatecheck(hll_empty(12, 8, 10, 1));
     hll_duplicatecheck
    --------------------
                      1
    (1 row)
    
    openGauss=# select hll_duplicatecheck(hll_empty(12, 8, 10, -1));
     hll_duplicatecheck
    --------------------
                      0
    (1 row)
    

功能函数

  • hll_empty()

    描述:创建一个空的hll。

    返回值类型:hll

    示例:

    openGauss=# select hll_empty();
                             hll_empty
    ------------------------------------------------------------
     \x484c4c00000000002b05000000000000000000000000000000000000
    (1 row)
    
  • hll_empty(int32 log2m)

    描述:创建空的hll并指定参数log2m,取值范围是10到16。若输入-1,则采用内置默认值。

    返回值类型:hll

    示例:

    openGauss=# select hll_empty(10);
                             hll_empty
    ------------------------------------------------------------
     \x484c4c00000000002b04000000000000000000000000000000000000
    (1 row)
    
    openGauss=# select hll_empty(-1);
                             hll_empty
    ------------------------------------------------------------
     \x484c4c00000000002b05000000000000000000000000000000000000
    (1 row)
    
  • hll_empty(int32 log2m, int32 log2explicit)

    描述:创建空的hll并依次指定参数log2m、log2explicit。log2explicit取值范围是0到12,0表示直接跳过Explicit模式。该参数可以用来设置Explicit模式的阈值大小,在数据段长度达到2log2explicit后切换为Sparse模式或者Full模式。若输入-1,则log2explicit采用内置默认值。

    返回值类型: hll

    示例:

    openGauss=# select hll_empty(10, 4);
                             hll_empty
    ------------------------------------------------------------
     \x484c4c00000000001304000000000000000000000000000000000000
    (1 row)
    
    openGauss=# select hll_empty(10, -1);
                             hll_empty
    ------------------------------------------------------------
     \x484c4c00000000002b04000000000000000000000000000000000000
    (1 row)
    
  • hll_empty(int32 log2m, int32 log2explicit, int64 log2sparse)

    描述:创建空的hll并依次指定参数log2m、log2explicit、log2sparse。log2sparse取值范围是0到14,0表示直接跳过Sparse模式。该参数可以用来设置Sparse模式的阈值大小,在数据段长度达到2log2sparse后切换为Full模式。若输入-1,则log2sparse采用内置默认值。

    返回值类型:hll

    示例:

    openGauss=# select hll_empty(10, 4, 8);
                             hll_empty
    ------------------------------------------------------------
     \x484c4c00000000001204000000000000000000000000000000000000
    (1 row)
    
    openGauss=# select hll_empty(10, 4, -1);
                             hll_empty
    ------------------------------------------------------------
     \x484c4c00000000001304000000000000000000000000000000000000
    (1 row)
    
  • hll_empty(int32 log2m, int32 log2explicit, int64 log2sparse, int32 duplicatecheck)

    描述:创建空的hll并依次指定参数log2m、log2explicit、log2sparse、duplicatecheck。duplicatecheck取0或者1,表示是否开启该模式,默认情况下该模式会关闭。若输入-1,则duplicatecheck采用内置默认值。

    返回值类型:hll

    示例:

    openGauss=# select hll_empty(10, 4, 8, 0);
                             hll_empty
    ------------------------------------------------------------
     \x484c4c00000000001204000000000000000000000000000000000000
    (1 row)
    
    openGauss=# select hll_empty(10, 4, 8, -1);
                             hll_empty
    ------------------------------------------------------------
     \x484c4c00000000001204000000000000000000000000000000000000
    (1 row)
    
  • hll_add(hll, hll_hashval)

    描述:把hll_hashval加入到hll中。

    返回值类型:hll

    示例:

    openGauss=# select hll_add(hll_empty(), hll_hash_integer(1));
                                      hll_add
    ----------------------------------------------------------------------------
     \x484c4c08000002002b0900000000000000f03f3e2921ff133fbaed3e2921ff133fbaed00
    (1 row)
    
  • hll_add_rev(hll_hashval, hll)

    描述:把hll_hashval加入到hll中,和hll_add功能一样,只是参数位置进行了交换。

    返回值类型:hll

    示例:

    openGauss=# select hll_add_rev(hll_hash_integer(1), hll_empty());
                                    hll_add_rev
    ----------------------------------------------------------------------------
     \x484c4c08000002002b0900000000000000f03f3e2921ff133fbaed3e2921ff133fbaed00
    (1 row)
    
  • hll_eq(hll, hll)

    描述:比较两个hll是否相等。

    返回值类型:bool

    示例:

    openGauss=# select hll_eq(hll_add(hll_empty(), hll_hash_integer(1)), hll_add(hll_empty(), hll_hash_integer(2)));
     hll_eq 
    --------
     f
    (1 row)
    
  • hll_ne(hll, hll)

    描述:比较两个hll是否不相等。

    返回值类型:bool

    示例:

    openGauss=# select hll_ne(hll_add(hll_empty(), hll_hash_integer(1)), hll_add(hll_empty(), hll_hash_integer(2)));
     hll_ne 
    --------
     t
    (1 row)
    
  • hll_cardinality(hll)

    描述:计算hll的distinct值。

    返回值类型:int

    示例:

    openGauss=# select hll_cardinality(hll_empty() || hll_hash_integer(1));
     hll_cardinality 
    -----------------
                   1
    (1 row)
    
  • hll_union(hll, hll)

    描述:把两个hll数据结构union成一个。

    返回值类型:hll

    示例:

    openGauss=# select hll_union(hll_add(hll_empty(), hll_hash_integer(1)), hll_add(hll_empty(), hll_hash_integer(2)));
                                             hll_union
    --------------------------------------------------------------------------------------------
     \x484c4c10002000002b090000000000000000400000000000000000b3ccc49320cca1ae3e2921ff133fbaed00
    (1 row)
    

聚合函数

  • hll_add_agg(hll_hashval)

    描述:把哈希后的数据按照分组放到hll中。

    返回值类型:hll

    示例:

    --准备数据
    openGauss=# create table t_id(id int);
    openGauss=# insert into t_id values(generate_series(1,500));
    openGauss=# create table t_data(a int, c text);
    openGauss=# insert into t_data select mod(id,2), id from t_id;
    
    --创建表并指定列为hll
    openGauss=# create table t_a_c_hll(a int, c hll);
    
    --根据a列group by对数据分组,把各组数据加到hll中
    openGauss=# insert into t_a_c_hll select a, hll_add_agg(hll_hash_text(c)) from t_data group by a;
    
    --得到每组数据中hll的Distinct值
    openGauss=# select a, #c as cardinality from t_a_c_hll order by a;
     a |   cardinality    
    ---+------------------
     0 | 247.862354346299
     1 | 250.908710610377
    (2 rows)
    
  • hll_add_agg(hll_hashval, int32 log2m)

    描述:把哈希后的数据按照分组放到hll中,并指定参数log2m,取值范围是10到16。若输入-1或者NULL,则采用内置默认值。

    返回值类型:hll

    示例:

    openGauss=# select hll_cardinality(hll_add_agg(hll_hash_text(c), 12)) from t_data;
     hll_cardinality  
    ------------------
     497.965240179228
    (1 row)
    
  • hll_add_agg(hll_hashval, int32 log2m, int32 log2explicit)

    描述:把哈希后的数据按照分组放到hll中,依次指定参数log2m、log2explicit。log2explicit取值范围是0到12,0表示直接跳过Explicit模式。该参数可以用来设置Explicit模式的阈值大小,在数据段长度达到2log2explicit后切换为Sparse模式或者Full模式。若输入-1或者NULL,则log2explicit采用内置默认值。

    返回值类型:hll

    示例:

    openGauss=# select hll_cardinality(hll_add_agg(hll_hash_text(c), NULL, 1)) from t_data;
     hll_cardinality  
    ------------------
     498.496062953313
    (1 row)
    
  • hll_add_agg(hll_hashval, int32 log2m, int32 log2explicit, int64 log2sparse)

    描述:把哈希后的数据按照分组放到hll中,依次指定参数log2m、log2explicit、log2sparse。log2sparse取值范围是0到14,0表示直接跳过Sparse模式。该参数可以用来设置Sparse模式的阈值大小,在数据段长度达到2log2sparse后切换为Full模式。若输入-1或者NULL,则log2sparse采用内置默认值。

    返回值类型:hll

    示例:

    openGauss=# select hll_cardinality(hll_add_agg(hll_hash_text(c), NULL, 6, 10)) from t_data;
     hll_cardinality  
    ------------------
     498.496062953313
    (1 row)
    
  • hll_add_agg(hll_hashval, int32 log2m, int32 log2explicit, int64 log2sparse, int32 duplicatecheck)

    描述:把哈希后的数据按照分组放到hll中, 依次制定参数log2m、log2explicit、log2sparse、duplicatecheck,duplicatecheck取值范围是0或者1,表示是否开启该模式,默认情况下该模式会关闭。若输入-1或者NULL,则duplicatecheck采用内置默认值。

    返回值类型:hll

    示例:

    openGauss=# select hll_cardinality(hll_add_agg(hll_hash_text(c), NULL, 6, 10, -1)) from t_data;
     hll_cardinality  
    ------------------
     498.496062953313
    (1 row)
    
  • hll_union_agg(hll)

    描述:将多个hll类型数据union成一个hll。

    返回值类型:hll

    示例:

    --将各组中的hll数据union成一个hll,并计算distinct值。
    openGauss=# select #hll_union_agg(c) as cardinality from t_a_c_hll;
       cardinality    
    ------------------
     498.496062953313
    (1 row)
    

    说明:

    注意:当两个或者多个hll数据结构做union的时候,必须要保证其中每一个hll里面的精度参数一样,否则将不可以进行union。同样的约束也适用于函数hll_union(hll,hll)。

废弃函数

由于版本升级,HLL(HyperLogLog)有一些旧的函数废弃,用户可以用类似的函数进行替代。

  • hll_schema_version(hll)

    描述:查看当前hll中的schema version。旧版本schema version是常值1,用来进行hll字段的头部校验,重构后的hll在头部增加字段“HLL”进行校验,schema version不再使用。

  • hll_regwidth(hll)

    描述:查看hll数据结构中桶的位数大小。旧版本桶的位数regwidth取值1~5,会存在较大的误差,也限制了基数估计上限。 重构后regwidth为固定值6,不再使用regwidth变量。

  • hll_expthresh(hll)

    描述:得到当前hll中expthresh大小。采用hll_log2explicit(hll)替代类似功能。

  • hll_sparseon(hll)

    描述:是否启用Sparse模式。采用hll_log2sparse(hll)替代类似功能,0表示关闭Sparse模式。

内置函数

HLL(HyperLogLog)有一系列内置函数用于内部对数据进行处理,一般情况下用户不需要熟知这些函数的使用。详情见表1

表 1 内置函数

函数名称

功能描述

hll_in

以string格式接收hll数据。

hll_out

以string格式发送hll数据。

hll_recv

以bytea格式接收hll数据。

hll_send

以bytea格式发送hll数据。

hll_trans_in

以string格式接收hll_trans_type数据。

hll_trans_out

以string格式发送hll_trans_type数据。

hll_trans_recv

以bytea形式接收hll_trans_type数据。

hll_trans_send

以bytea形式发送hll_trans_type数据。

hll_typmod_in

接收typmod类型数据。

hll_typmod_out

发送typmod类型数据。

hll_hashval_in

接收hll_hashval类型数据。

hll_hashval_out

发送hll_hashval类型数据。

hll_add_trans0

类似于hll_add所提供的功能,初始化时无指定入参,通常在聚合运算的第一阶段DN上使用。

hll_add_trans1

类似于hll_add所提供的功能,初始化时指定一个入参,通常在聚合运算的第一阶段DN上使用。

hll_add_trans2

类似于hll_add所提供的功能,初始化时指定两个入参,通常在聚合运算的第一阶段DN上使用。

hll_add_trans3

类似于hll_add所提供的功能,初始化时指定三个入参,通常在聚合运算的第一阶段DN上使用。

hll_add_trans4

类似于hll_add所提供的功能,初始化时指定四个入参,通常在聚合运算的第一阶段DN上使用。

hll_union_trans

类似hll_union所提供的功能,在聚合运算的第一阶段DN上使用。

hll_union_collect

类似于hll_union所提供的功能,在聚合运算第二阶段DN上使用,汇总各个DN上的结果。

hll_pack

在聚合运算第三阶段DN上使用,把自定义hll_trans_type类型最后转换成hll类型。

hll

用于hll类型转换成hll类型,根据输入参数会设定指定参数。

hll_hashval

用于bigint类型转换成hll_hashval类型。

hll_hashval_int4

用于int4类型转换成hll_hashval类型。

操作符

  • =

    描述:比较hll或hll_hashval的值是否相等。

    返回值类型:bool

    示例:

    --hll
    openGauss=# select (hll_empty() || hll_hash_integer(1)) = (hll_empty() || hll_hash_integer(1));
    column 
    ----------
     t
    (1 row)
    
    --hll_hashval
    openGauss=# select hll_hash_integer(1) = hll_hash_integer(1);
     ?column? 
    ----------
     t
    (1 row)
    
  • <> or !=

    描述:比较hll或hll_hashval是否不相等。

    返回值类型:bool

    示例:

    --hll
    openGauss=# select (hll_empty() || hll_hash_integer(1)) <> (hll_empty() || hll_hash_integer(2));
     ?column? 
    ----------
     t
    (1 row)
    
    --hll_hashval
    openGauss=# select hll_hash_integer(1) <> hll_hash_integer(2);
     ?column? 
    ----------
     t
    (1 row)
    
  • ||

    描述:可代表hll_add,hll_union,hll_add_rev三个函数的功能。

    返回值类型:hll

    示例:

    --hll_add
    openGauss=# select hll_empty() || hll_hash_integer(1);
                                      ?column?
    ----------------------------------------------------------------------------
     \x484c4c08000002002b0900000000000000f03f3e2921ff133fbaed3e2921ff133fbaed00
    (1 row)
    
    --hll_add_rev
    openGauss=# select hll_hash_integer(1) || hll_empty();
                                      ?column?
    ----------------------------------------------------------------------------
     \x484c4c08000002002b0900000000000000f03f3e2921ff133fbaed3e2921ff133fbaed00
    (1 row)
    
    --hll_union
    openGauss=# select (hll_empty() || hll_hash_integer(1)) || (hll_empty() || hll_hash_integer(2));
                                              ?column?
    --------------------------------------------------------------------------------------------
     \x484c4c10002000002b090000000000000000400000000000000000b3ccc49320cca1ae3e2921ff133fbaed00
    (1 row)
    
  • #

    描述:计算出hll的Dintinct值, 同hll_cardinality函数。

    返回值类型:int

    示例:

    openGauss=# select #(hll_empty() || hll_hash_integer(1));
     ?column? 
    ----------
            1
    (1 row)
    
意见反馈
编组 3备份
openGauss 2024-03-19 00:47:31
取消