Hologres自V3.1版本起,通过增加hg_presto_funcs扩展包,支持若干Presto兼容函数。本文介绍Presto函数在Hologres中的使用方法。
安装扩展
使用Presto函数前,需要Superuser在DB内执行以下语句安装扩展包。一个DB只需执行一次,如果创建新的DB,还需要再次执行如下语句。
CREATE EXTENSION hg_presto_funcs;
该扩展仅支持创建在hg_presto_funcs
Schema下,手动指定其他Schema无效。
示例数据
CREATE TABLE public.presto_test(a INT, b INT[],c TEXT,d TEXT,e TEXT,f TEXT,g TEXT, h TEXT,i TEXT,j TEXT,k TEXT,l TEXT,m TEXT,n TEXT,o TEXT,p TEXT ,r TEXT ,s TEXT ,t FLOAT, u TEXT);
INSERT INTO public.presto_test
VALUES (3, ARRAY[1, 2, 3, 2],'A','10010','SGVsbG8gV29ybGQh','\x00000001','\x0000000000000001','48656C6C6F','\x40490FD0','\x400921FB54442D18','\x48656C6C6F','"dwdw"','[1,2,3,4,5,6]','{"x": {"a": 1, "b": 2}}','café','apple,banana,cherry','ABCDA','running',3.1415926,'[1.0,2.0]');
Presto函数
Hologres已支持的Presto函数列表如下,函数详细说明请参见Presto Functions。
本文函数暂不支持常量入参。下述函数的示例数据均来源于示例表
public.presto_test
。使用下述函数时,需在函数名前增加
hg_presto_funcs.
前缀,或指定路径set search_path = hg_presto_funcs;
与示例一起执行。例如:set search_path = hg_presto_funcs; select array_cum_sum(b) from public.presto_test;
函数 | 说明 | 示例 | 结果 |
array_cum_sum | 输出一个数组,第n位为输入数组的前n位之和。前n位有null时结果也为null。 | select array_cum_sum(b) from public.presto_test; | {1,3,6,8} |
array_has_duplicates | 数组中是否有重复元素。 | select array_has_duplicates(b) from public.presto_test; | t |
array_join | 数组使用给定分隔符连接数组元素的字符串。 | select array_join(b,';') from public.presto_test; | 1;2;3;2 |
beta_cdf | 计算Beta分布的累积分布函数(CDF)。 | select beta_cdf(2,a,0.5) from public.presto_test; | 0.6875 |
binomial_cdf | 计算二项分布的累积分布函数(CDF)。 | select binomial_cdf(6,0.5,a) from public.presto_test; | 0.65625 |
bit_count | 二进制计算后在指定位宽内的Hamming权重(1的个数)。 | select bit_count(a, 32) from public.presto_test; | 2 |
bitwise_and | Bitwise按位AND计算。 | select bitwise_and(a, 2) from public.presto_test; | 2 |
bitwise_arithmetic_shift_right | Bitwise按位算术右移n位。 | select bitwise_arithmetic_shift_right(a,1) from public.presto_test; | 1 |
bitwise_left_shift | Bitwise按位左移n位。 | select bitwise_left_shift(a,1) from public.presto_test; | 6 |
bitwise_logical_shift_right | Bitwise按位逻辑右移n位。 | select bitwise_logical_shift_right(a,1,32) from public.presto_test; | 1 |
bitwise_not | Bitwise按位NOT计算。 | select bitwise_not(a) from public.presto_test; | -4 |
bitwise_or | Bitwise按位OR计算。 | select bitwise_or(a, 2) from public.presto_test; | 3 |
bitwise_right_shift | Bitwise按位右移n位。 | select bitwise_right_shift(a,1) from public.presto_test; | 1 |
bitwise_right_shift_arithmetic | Bitwise按位算术右移n位。 | select bitwise_right_shift_arithmetic(a,1) from public.presto_test; | 1 |
bitwise_shift_left | Bitwise按位左移n位。 | select bitwise_shift_left(a,1,32) from public.presto_test; | 6 |
bitwise_xor | Bitwise按位XOR计算。 | select bitwise_xor(a, 2) from public.presto_test; | 1 |
cauchy_cdf | 计算柯西分布的累积分布函数(CDF)。 | select beta_cdf(2,a,0.5) from public.presto_test; | 0.6875 |
chi_squared_cdf | 计算卡方分布的累积分布函数(CDF)。 | select chi_squared_cdf(a, 4) from public.presto_test; | 0.7385358700508894 |
clamp | 判断输入值是否限制在指定范围内,若在则返回该值,否则返回最近的范围值。 | select clamp(a,2,9) from public.presto_test; | 3 |
codepoint | 返回单字符中的Unicode码。 | select codepoint(c) from public.presto_test; | 65 |
cosh | 双曲余弦函数。 | select cosh(a) from public.presto_test; | 10.067661995777765 |
crc32 | CRC32哈希值。 | select crc32(bin(a)::bytea) from public.presto_test; | 3596227959 |
ends_with | 第一个入参是否以第二个入参结尾。 | select ends_with(concat(a, 'hologres'), 'gres') from public.presto_test; | t |
eq | 判断两个值是否相等。 | select eq(c,'A') from public.presto_test; | t |
f_cdf | 计算F分布的累积分布函数(CDF)。 | select f_cdf(2,a,0.5) from public.presto_test; | 0.350480947161671 |
from_base | 将字符串解释为给定基数的形式,并返回十进制数值。 | select from_base(d,2) from public.presto_test; | 18 |
from_base64 | 将Base64编码的字符串解码为原始二进制形式。 | select from_base64(e) from public.presto_test; | \x48656c6c6f20576f726c6421 |
from_base64url | 使用URL安全的Base64字符集,将Base64编码的字符串解码为二进制数据。 | select from_base64url(e) from public.presto_test; | \x48656c6c6f20576f726c6421 |
from_big_endian_32 | 将大端序的32位二进制补码解码为bigint值。 | select from_big_endian_32(f::BYTEA) from public.presto_test; | 1 |
from_big_endian_64 | 将大端序的64位二进制补码解码为bigint值。 | select from_big_endian_64(g::BYTEA) from public.presto_test; | 1 |
from_hex | 将十六进制编码的字符串解码为原始二进制数据。 | select from_hex(h) from public.presto_test; | \x48656c6c6f |
from_ieee754_32 | 将32位大端序二进制数据解码为IEEE 754单精度浮点数。 | select from_ieee754_32(i::BYTEA) from public.presto_test; | 3.14159012 |
from_ieee754_64 | 将64位大端序二进制数据解码为IEEE 754单精度浮点数。 | select from_ieee754_64(j::BYTEA) from public.presto_test; | 3.141592653589793 |
gamma_cdf | 计算gamma分布的累积分布函数(CDF)。 | select gamma_cdf(2,a,0.5) from public.presto_test; | 0.012437987627616913 |
gt | 若x>y,返回true,否则返货false,字符串类型。 | select gt('B' ,c) from public.presto_test; | t |
gte | 若x>=y,返回true,否则返货false,字符串类型。 | select gte('A' ,c) from public.presto_test; | t |
hamming_distance | 返回对应位置字符不同的数量。 | select hamming_distance(e,'SGVsBG7gV39ybGQh') from public.presto_test; | 3 |
hmac_md5 | 使用MD5算法和给定的密钥,对输入的二进制计算哈希信息认证码。 | select hmac_md5(k::BYTEA,'secret_key') from public.presto_test; | \x52460d22ec7e402dc8c62aeda51ec920 |
hmac_sha1 | 使用sha1算法和给定的密钥,对输入的二进制计算哈希信息认证码。 | select hmac_sha1(k::BYTEA,'secret_key') from public.presto_test; | \xf8b6b3ee753fe1d8052cf317b0b4606089c85b19 |
hmac_sha256 | 使用sha256算法和给定的密钥,对输入的二进制计算哈希信息认证码。 | select hmac_sha256(k::BYTEA,'secret_key') from public.presto_test; | \x0f0d2e10ec2bdf21bbdf490fd103820089879277261e9aa53ce3f8ecfd46b687 |
hmac_sha512 | 使用sha512算法和给定的密钥,对输入的二进制计算哈希信息认证码。 | select hmac_sha512(k::BYTEA,'secret_key') from public.presto_test; | \x99376d305f3c2e729e60eb1e096fc364b8564452c4089b509c5d0d6e63608a43f14643a82880f156bf7df5ce32ba6bc35e36980772f9199b1cf43793e9bbd545 |
inverse_beta_cdf | 计算Beta分布的逆累积分布函数(CDF)。 | select inverse_beta_cdf(2,a,0.6875) from public.presto_test; | 0.5 |
inverse_cauchy_cdf | 计算柯西分布的逆累积分布函数(CDF)。 | select inverse_cauchy_cdf(2,a,0.5) from public.presto_test; | 2 |
inverse_laplace_cdf | 计算拉普拉斯分布的逆累积分布函数(CDF)。 | select inverse_laplace_cdf(a,1,0.5) from public.presto_test; | 3 |
inverse_normal_cdf | 计算给定均值和标准差正态分布的逆累积分布函数(CDF)。 | select inverse_normal_cdf(a,1,0.5) from public.presto_test; | 3 |
inverse_weibull_cdf | 计算Weibull分布的逆累积分布函数(CDF)。 | select inverse_weibull_cdf(2,a,0.5) from public.presto_test; | 2.497663833473093 |
is_finite | 判断值是否为有限数。 | select is_finite(a) from public.presto_test; | t |
is_infinite | 判断值是否为无限数。 | select is_infinite(a) from public.presto_test; | f |
is_json_scalar | 判断值是否是JSON标量值,输入值需要为JSON标量值。 | select is_json_scalar(l) from public.presto_test; | t |
is_nan | 检测数值是否NAN(Not a Number)。 | select is_nan(a) from public.presto_test; | f |
json_array_contains | 判断值是否在JSON数组中。 | select json_array_contains(u,2) from public.presto_test; | t |
json_extract_scalar | 从JSON字符串中返回指定路径的值,输入值需要为JSON标量值。 | select json_extract_scalar(m,'$[1]') from public.presto_test; | 2 |
json_size | 计算JSON值的大小,输入值需要为JSON标量值。 | select json_size(n,'$.x') from public.presto_test; | 2 |
laplace_cdf | 计算拉普拉斯分布的累积分布函数(CDF)。 | select laplace_cdf(0,1,a) from public.presto_test; | 0.9751064658160681 |
levenshtein_distance | 返回两个字符串之间的Levenshtein距离(编辑距离,指将一个字符串转换为另一个字符串所需的最少单字符编辑操作次数)。 | select levenshtein(a::text, 'hologres') from public.presto_test; | 8 |
log10 | 给定值以10为底的对数。 | select log10(a) from public.presto_test; | 0.47712125471966244 |
log2 | 给定值以2为底的对数。 | select log2(a) from public.presto_test; | 1.584962500721156 |
lt | 判断两个数据中,前者是否小于后者。 | select lt('ABCDA',c) from public.presto_test; | f |
lte | 判断两个数据中,前者是否小于或等于后者。 | select lte('A',c) from public.presto_test; | t |
minus | 两数之差。 | select minus(a,1) from public.presto_test; | 2 |
negate | 返回相反数。 | select negate(a) from public.presto_test; | -3 |
neq | 判断两个值是否相等。 | select neq('A',c) from public.presto_test; | f |
normal_cdf | 计算给定均值和标准差正态分布的累积分布函数(CDF)。 | select normal_cdf(0,1,a) from public.presto_test; | 0.9986501019683699 |
normalize | 将字符串转换为NFC标准化形式。 | select normalize(o) from public.presto_test; | café |
poisson_cdf | 计算泊松分布的累积分布函数(CDF)。 | select poisson_cdf(1,a) from public.presto_test; | 0.9810118431238462 |
regexp_split | 使用正则表达式分割字符串。 | select regexp_split(p,',') from public.presto_test; | {"apple","banana","cherry"} |
remove_nulls | 移除数组中的NULL值。 | select remove_nulls(b) from public.presto_test; | {1,2,3,2} |
secure_rand | 返回一个加密安全的随机双精度浮点数。 | select secure_rand() from public.presto_test; | 0.22077085443234523 |
secure_random | 返回一个加密安全的随机双精度浮点数。 | select secure_random() from public.presto_test; | 0.3414298654539425 |
sha1 | 计算字符串的SHA-1(Secure Hash Algorithm 1)哈希值,并将结果转换为十六进制字符串。 | select sha1(a::text::bytea) from public.spark_test; | 77de68daecd823babbb58edb1c8e14d7106e83bb |
spooky_hash_v2_32 | 计算输入二进制数据的SpookyHashV2 32位哈希值。 | select spooky_hash_v2_32(r::BYTEA) from public.presto_test; | \x4a6ed4f7 |
spooky_hash_v2_64 | 计算输入二进制数据的SpookyHashV2 64位哈希值。 | select spooky_hash_v2_64(r::BYTEA) from public.presto_test; | \x2d89595e4a6ed4f7 |
strrpos | 返回字符串中最后一次出现子字符串的位置。 | select STRRPOS(r,'A') from public.presto_test; | 5 |
tanh | 计算双曲正切值。 | select tanh(a) from public.presto_test; | 0.9950547536867305 |
to_base | 将整数转换为指定基数的字符串形式。 | select to_base(a, 2) from public.presto_test; | 11 |
to_base64 | 将二进制数据编码为Base64格式的字符串。 | select to_base64(r::BYTEA) from public.presto_test; | QUJDREE= |
to_base64url | 使用URL安全字符集,将二进制数据编码为Base64格式的字符串。 | select to_base64url(r::BYTEA) from public.presto_test; | QUJDREE= |
to_big_endian_32 | 将整数编码为32位二进制补码,并以大端序存储为二进制数据。 | select to_big_endian_32(a) from public.presto_test; | \x00000003 |
to_big_endian_64 | 将整数编码为64位二进制补码,并以大端序存储为二进制数据。 | select to_big_endian_64(a) from public.presto_test; | \x0000000000000003 |
to_ieee754_32 | 将单精度浮点数类型编码为32位IEEE 754标准格式的二进制数据,并以大端序(Big Endian) 存储为4字节的二进制类型。 | select to_ieee754_32(a::REAL) from public.presto_test; | \x40400000 |
to_ieee754_64 | 将双精度浮点数类型编码为64位IEEE 754标准格式的二进制数据,并以大端序(Big Endian) 存储为8字节的二进制类型。 | select to_ieee754_64(a::FLOAT ) from public.presto_test; | \x4008000000000000 |
trim | 去除字符集合。 | select trim(r,'A') from public.presto_test; | BCD |
trim_array | 从数组末尾移除n个元素。 | select trim_array(b,2) from public.presto_test; | {1,2} |
truncate | 将x截断到n位小数。 | select truncate(t,2) from public.presto_test; | 3.14 |
url_decode | 取消转义URL的编码值。 | select url_decode(url_encode(concat('www.','中文',a,'.com'))) from public.presto_test; | www.中文3.com |
url_encode | 通过编码对值进行转义。 | select url_encode(concat('www.','中文',a,'.com')) from public.presto_test; | www.%E4%B8%AD%E6%96%873.com |
url_extract_fragment | 从URL中提取片段标识符。 | select url_extract_fragment(concat('https://www.example.com:',a,'080/products/shirt?color=red&size=XL#discount-section')) from public.presto_test; | discount-section |
url_extract_host | 从URL中提取域名。 | select url_extract_host(concat('https://www.example.com:',a,'080/products/shirt?color=red&size=XL#discount-section')) from public.presto_test; | www.example.com |
url_extract_parameter | 从URL中提取查询字符串参数, 中第一个名为name的参数值。 | select url_extract_parameter(concat('https://www.example.com:',a,'080/products/shirt?color=red&size=XL#discount-section'),'color') from public.presto_test; | red |
url_extract_path | 从URL中提取路径。 | select url_extract_path(concat('https://www.example.com:',a,'080/products/shirt?color=red&size=XL#discount-section')) from public.presto_test; | /products/shirt |
url_extract_port | 从URL中提取端口号。 | select url_extract_port(concat('https://www.example.com:',a,'080/products/shirt?color=red&size=XL#discount-section')) from public.presto_test; | 3088 |
url_extract_protocol | 从URL中提取端协议。 | select url_extract_protocol(concat('https://www.example.com:',a,'080/products/shirt?color=red&size=XL#discount-section')) from public.presto_test; | https |
url_extract_query | 从URL中提取查询参数。 | select url_extract_query(concat('https://www.example.com:',a,'080/products/shirt?color=red&size=XL#discount-section')) from public.presto_test; | color=red&size=XL |
weibull_cdf | 计算Weibull分布的累积分布函数(CDF)。 | select weibull_cdf(2,a,1.5) from public.presto_test; | 0.22119921692859512 |
wilson_interval_lower | 返回伯努利试验过程在置信水平z下的威尔逊置信区间的下限。 | select wilson_interval_lower(a,20,1.96) from public.presto_test; | 0.05236779195949585 |
wilson_interval_upper | 返回伯努利试验过程在置信水平z下的威尔逊置信区间的上限。 | select wilson_interval_upper(a,20,1.96) from public.presto_test; | 0.3604232958869574 |
word_stem | 返回word在lang语言中的词干形式, | select word_stem(s, 'en') from public.presto_test; | run |
xxhash64 | 计算输入二进制数据的XXHash64哈希值,并返回64位二进制结果。 | select xxhash64(r::BYTEA) from public.presto_test; | \x616f621d4581935f |