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 | 使用Regex分割字串。 | 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 |