Starting from V3.1, Hologres supports Presto compatible functions through the hg_presto_funcs extension. All functions in this topic are backed by the Velox execution engine.
Prerequisites
Before you begin, ensure that you have:
A Hologres instance running V3.1 or later
Superuser access to the target database
Install the extension
Run the following statement as a Superuser. Run it once per database — if you create a new database, run it again.
CREATE EXTENSION hg_presto_funcs;The extension installs into the hg_presto_funcs schema only. You cannot specify a different schema.
Call Presto functions
All Presto functions live in the hg_presto_funcs schema. To call them, either prefix each function name or set the search path once per session:
-- Option 1: prefix each call
SELECT hg_presto_funcs.array_cum_sum(b) FROM public.presto_test;
-- Option 2: set search path for the session
SET search_path = hg_presto_funcs;
SELECT array_cum_sum(b) FROM public.presto_test;These functions do not support constant input parameters. All examples in this topic use column references from the sample table below.
Sample table
The examples in this topic use the following table:
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]'
);Supported functions
The tables below list all supported functions, grouped by category. Each entry shows the function signature with parameter and return types.
Array functions
| Function signature | Description | Example | Result |
|---|---|---|---|
array_cum_sum(array(bigint)) → array(bigint) | Returns an array where position n holds the sum of the first n elements. Returns null at position n if any of the first n elements is null. | SELECT array_cum_sum(b) FROM public.presto_test; | {1,3,6,8} |
array_has_duplicates(array(T)) → boolean | Returns true if the array contains duplicate elements. | SELECT array_has_duplicates(b) FROM public.presto_test; | t |
array_join(array(T), varchar) → varchar | Concatenates array elements into a string using the given separator. | SELECT array_join(b, ';') FROM public.presto_test; | 1;2;3;2 |
remove_nulls(array(T)) → array(T) | Removes NULL values from an array. | SELECT remove_nulls(b) FROM public.presto_test; | {1,2,3,2} |
trim_array(array(T), bigint) → array(T) | Removes n elements from the end of an array. | SELECT trim_array(b, 2) FROM public.presto_test; | {1,2} |
Bitwise functions
| Function signature | Description | Example | Result |
|---|---|---|---|
bit_count(bigint, bigint) → bigint | Counts the number of set bits (Hamming weight) in the value within the specified bit width. | SELECT bit_count(a, 32) FROM public.presto_test; | 2 |
bitwise_and(bigint, bigint) → bigint | Returns the bitwise AND of two integers. | SELECT bitwise_and(a, 2) FROM public.presto_test; | 2 |
bitwise_or(bigint, bigint) → bigint | Returns the bitwise OR of two integers. | SELECT bitwise_or(a, 2) FROM public.presto_test; | 3 |
bitwise_xor(bigint, bigint) → bigint | Returns the bitwise XOR of two integers. | SELECT bitwise_xor(a, 2) FROM public.presto_test; | 1 |
bitwise_not(bigint) → bigint | Returns the bitwise NOT of an integer. | SELECT bitwise_not(a) FROM public.presto_test; | -4 |
bitwise_left_shift(bigint, bigint) → bigint | Shifts the value left by n bits. | SELECT bitwise_left_shift(a, 1) FROM public.presto_test; | 6 |
bitwise_shift_left(bigint, bigint, bigint) → bigint | Shifts the value left by n bits within the specified bit width. | SELECT bitwise_shift_left(a, 1, 32) FROM public.presto_test; | 6 |
bitwise_right_shift(bigint, bigint) → bigint | Shifts the value right by n bits (logical, zero-fill). | SELECT bitwise_right_shift(a, 1) FROM public.presto_test; | 1 |
bitwise_logical_shift_right(bigint, bigint, bigint) → bigint | Shifts the value right by n bits (logical, zero-fill) within the specified bit width. | SELECT bitwise_logical_shift_right(a, 1, 32) FROM public.presto_test; | 1 |
bitwise_arithmetic_shift_right(bigint, bigint) → bigint | Shifts the value right by n bits, preserving the sign bit (arithmetic right shift). | SELECT bitwise_arithmetic_shift_right(a, 1) FROM public.presto_test; | 1 |
bitwise_right_shift_arithmetic(bigint, bigint) → bigint | Alias for bitwise_arithmetic_shift_right. Shifts the value right by n bits, preserving the sign bit. | SELECT bitwise_right_shift_arithmetic(a, 1) FROM public.presto_test; | 1 |
bitwise_right_shiftandbitwise_logical_shift_rightfill vacated bits with zeros.bitwise_arithmetic_shift_rightandbitwise_right_shift_arithmeticfill with the sign bit, making them equivalent for negative integers.
Binary and hash functions
For general-purpose hashing, usexxhash64— it is significantly faster thancrc32andspooky_hash_v2_*and produces better hash quality. Usehmac_*functions when you need keyed authentication codes. Usecrc32only for compatibility with systems that already depend on it.
| Function signature | Description | Example | Result |
|---|---|---|---|
crc32(varbinary) → bigint | Computes the CRC32 checksum of binary data. | SELECT crc32(bin(a)::bytea) FROM public.presto_test; | 3596227959 |
xxhash64(varbinary) → varbinary | Computes the xxHash64 hash of binary data and returns a 64-bit binary result. | SELECT xxhash64(r::BYTEA) FROM public.presto_test; | \x616f621d4581935f |
spooky_hash_v2_32(varbinary) → varbinary | Computes the SpookyHashV2 32-bit hash of binary data. | SELECT spooky_hash_v2_32(r::BYTEA) FROM public.presto_test; | \x4a6ed4f7 |
spooky_hash_v2_64(varbinary) → varbinary | Computes the SpookyHashV2 64-bit hash of binary data. | SELECT spooky_hash_v2_64(r::BYTEA) FROM public.presto_test; | \x2d89595e4a6ed4f7 |
hmac_md5(varbinary, varchar) → varbinary | Computes an HMAC using MD5 and the given key. | SELECT hmac_md5(k::BYTEA, 'secret_key') FROM public.presto_test; | \x52460d22ec7e402dc8c62aeda51ec920 |
hmac_sha1(varbinary, varchar) → varbinary | Computes an HMAC using SHA-1 and the given key. | SELECT hmac_sha1(k::BYTEA, 'secret_key') FROM public.presto_test; | \xf8b6b3ee753fe1d8052cf317b0b4606089c85b19 |
hmac_sha256(varbinary, varchar) → varbinary | Computes an HMAC using SHA-256 and the given key. | SELECT hmac_sha256(k::BYTEA, 'secret_key') FROM public.presto_test; | \x0f0d2e10ec2bdf21bbdf490fd103820089879277261e9aa53ce3f8ecfd46b687 |
hmac_sha512(varbinary, varchar) → varbinary | Computes an HMAC using SHA-512 and the given key. | SELECT hmac_sha512(k::BYTEA, 'secret_key') FROM public.presto_test; | \x99376d305f3c2e729e60eb1e096fc364b8564452c4089b509c5d0d6e63608a43f14643a82880f156bf7df5ce32ba6bc35e36980772f9199b1cf43793e9bbd545 |
sha1(varbinary) → varchar | Computes the SHA-1 hash of binary data and returns it as a hexadecimal string. | SELECT sha1(a::text::bytea) FROM public.presto_test; | 77de68daecd823babbb58edb1c8e14d7106e83bb |
from_base64(varchar) → varbinary | Decodes a standard Base64-encoded string to binary. | SELECT from_base64(e) FROM public.presto_test; | \x48656c6c6f20576f726c6421 |
to_base64(varbinary) → varchar | Encodes binary data as a standard Base64 string. | SELECT to_base64(r::BYTEA) FROM public.presto_test; | QUJDREE= |
from_base64url(varchar) → varbinary | Decodes a URL-safe Base64-encoded string to binary. | SELECT from_base64url(e) FROM public.presto_test; | \x48656c6c6f20576f726c6421 |
to_base64url(varbinary) → varchar | Encodes binary data as a URL-safe Base64 string. | SELECT to_base64url(r::BYTEA) FROM public.presto_test; | QUJDREE= |
from_hex(varchar) → varbinary | Decodes a hexadecimal string to binary. | SELECT from_hex(h) FROM public.presto_test; | \x48656c6c6f |
from_big_endian_32(varbinary) → bigint | Decodes a 32-bit big-endian two's complement binary value to a bigint. | SELECT from_big_endian_32(f::BYTEA) FROM public.presto_test; | 1 |
to_big_endian_32(bigint) → varbinary | Encodes an integer as a 32-bit big-endian two's complement binary value. | SELECT to_big_endian_32(a) FROM public.presto_test; | \x00000003 |
from_big_endian_64(varbinary) → bigint | Decodes a 64-bit big-endian two's complement binary value to a bigint. | SELECT from_big_endian_64(g::BYTEA) FROM public.presto_test; | 1 |
to_big_endian_64(bigint) → varbinary | Encodes an integer as a 64-bit big-endian two's complement binary value. | SELECT to_big_endian_64(a) FROM public.presto_test; | \x0000000000000003 |
from_ieee754_32(varbinary) → real | Decodes a 32-bit big-endian IEEE 754 binary value to a single-precision floating-point number. | SELECT from_ieee754_32(i::BYTEA) FROM public.presto_test; | 3.14159012 |
to_ieee754_32(real) → varbinary | Encodes a single-precision floating-point number as a 32-bit big-endian IEEE 754 binary value. | SELECT to_ieee754_32(a::REAL) FROM public.presto_test; | \x40400000 |
from_ieee754_64(varbinary) → double | Decodes a 64-bit big-endian IEEE 754 binary value to a double-precision floating-point number. | SELECT from_ieee754_64(j::BYTEA) FROM public.presto_test; | 3.141592653589793 |
to_ieee754_64(double) → varbinary | Encodes a double-precision floating-point number as a 64-bit big-endian IEEE 754 binary value. | SELECT to_ieee754_64(a::FLOAT) FROM public.presto_test; | \x4008000000000000 |
from_base(varchar, bigint) → bigint | Interprets a string as a number in the given base and returns its decimal value. | SELECT from_base(d, 2) FROM public.presto_test; | 18 |
to_base(bigint, bigint) → varchar | Converts an integer to its string representation in the given base. | SELECT to_base(a, 2) FROM public.presto_test; | 11 |
Math and statistical functions
General math
| Function signature | Description | Example | Result |
|---|---|---|---|
clamp(double, double, double) → double | Clamps a value to the range [min, max]. Returns the value if within range, or the nearest bound otherwise. | SELECT clamp(a, 2, 9) FROM public.presto_test; | 3 |
cosh(double) → double | Returns the hyperbolic cosine of the value. | SELECT cosh(a) FROM public.presto_test; | 10.067661995777765 |
tanh(double) → double | Returns the hyperbolic tangent of the value. | SELECT tanh(a) FROM public.presto_test; | 0.9950547536867305 |
log2(double) → double | Returns the base-2 logarithm of the value. | SELECT log2(a) FROM public.presto_test; | 1.584962500721156 |
log10(double) → double | Returns the base-10 logarithm of the value. | SELECT log10(a) FROM public.presto_test; | 0.47712125471966244 |
minus(double, double) → double | Returns the difference of two numbers (x − y). | SELECT minus(a, 1) FROM public.presto_test; | 2 |
negate(double) → double | Returns the negation of the value. | SELECT negate(a) FROM public.presto_test; | -3 |
truncate(double, integer) → double | Truncates the value to n decimal places. | SELECT truncate(t, 2) FROM public.presto_test; | 3.14 |
is_finite(double) → boolean | Returns true if the value is finite. | SELECT is_finite(a) FROM public.presto_test; | t |
is_infinite(double) → boolean | Returns true if the value is infinite. | SELECT is_infinite(a) FROM public.presto_test; | f |
is_nan(double) → boolean | Returns true if the value is not-a-number (NaN). | SELECT is_nan(a) FROM public.presto_test; | f |
secure_rand() → double | Returns a cryptographically secure random double in [0, 1). | SELECT secure_rand() FROM public.presto_test; | 0.22077085443234523 |
secure_random() → double | Alias for secure_rand. Returns a cryptographically secure random double in [0, 1). | SELECT secure_random() FROM public.presto_test; | 0.3414298654539425 |
Cumulative distribution functions (CDF)
| Function signature | Description | Example | Result |
|---|---|---|---|
beta_cdf(double, double, double) → double | Returns the cumulative distribution function (CDF) of the beta distribution with the given shape parameters. | SELECT beta_cdf(2, a, 0.5) FROM public.presto_test; | 0.6875 |
binomial_cdf(bigint, double, bigint) → double | Returns the CDF of the binomial distribution. | SELECT binomial_cdf(6, 0.5, a) FROM public.presto_test; | 0.65625 |
cauchy_cdf(double, double, double) → double | Returns the CDF of the Cauchy distribution. | SELECT cauchy_cdf(2, a, 0.5) FROM public.presto_test; | 0.6024163823495667 |
chi_squared_cdf(double, double) → double | Returns the CDF of the chi-squared distribution. | SELECT chi_squared_cdf(a, 4) FROM public.presto_test; | 0.7385358700508894 |
f_cdf(double, double, double) → double | Returns the CDF of the F distribution. | SELECT f_cdf(2, a, 0.5) FROM public.presto_test; | 0.350480947161671 |
gamma_cdf(double, double, double) → double | Returns the CDF of the gamma distribution. | SELECT gamma_cdf(2, a, 0.5) FROM public.presto_test; | 0.012437987627616913 |
laplace_cdf(double, double, double) → double | Returns the CDF of the Laplace distribution. | SELECT laplace_cdf(0, 1, a) FROM public.presto_test; | 0.9751064658160681 |
normal_cdf(double, double, double) → double | Returns the CDF of the normal distribution with the given mean and standard deviation. | SELECT normal_cdf(0, 1, a) FROM public.presto_test; | 0.9986501019683699 |
poisson_cdf(double, bigint) → double | Returns the CDF of the Poisson distribution. | SELECT poisson_cdf(1, a) FROM public.presto_test; | 0.9810118431238462 |
weibull_cdf(double, double, double) → double | Returns the CDF of the Weibull distribution. | SELECT weibull_cdf(2, a, 1.5) FROM public.presto_test; | 0.22119921692859512 |
inverse_beta_cdf(double, double, double) → double | Returns the inverse CDF of the beta distribution. | SELECT inverse_beta_cdf(2, a, 0.6875) FROM public.presto_test; | 0.5 |
inverse_cauchy_cdf(double, double, double) → double | Returns the inverse CDF of the Cauchy distribution. | SELECT inverse_cauchy_cdf(2, a, 0.5) FROM public.presto_test; | 2 |
inverse_laplace_cdf(double, double, double) → double | Returns the inverse CDF of the Laplace distribution. | SELECT inverse_laplace_cdf(a, 1, 0.5) FROM public.presto_test; | 3 |
inverse_normal_cdf(double, double, double) → double | Returns the inverse CDF of the normal distribution with the given mean and standard deviation. | SELECT inverse_normal_cdf(a, 1, 0.5) FROM public.presto_test; | 3 |
inverse_weibull_cdf(double, double, double) → double | Returns the inverse CDF of the Weibull distribution. | SELECT inverse_weibull_cdf(2, a, 0.5) FROM public.presto_test; | 2.497663833473093 |
wilson_interval_lower(bigint, bigint, double) → double | Returns the lower bound of the Wilson confidence interval for a Bernoulli trial at the given confidence level z. | SELECT wilson_interval_lower(a, 20, 1.96) FROM public.presto_test; | 0.05236779195949585 |
wilson_interval_upper(bigint, bigint, double) → double | Returns the upper bound of the Wilson confidence interval for a Bernoulli trial at the given confidence level z. | SELECT wilson_interval_upper(a, 20, 1.96) FROM public.presto_test; | 0.3604232958869574 |
String and Unicode functions
These functions operate on Unicode code points, not user-visible characters (grapheme clusters). Input strings must be valid UTF-8.
| Function signature | Description | Example | Result |
|---|---|---|---|
codepoint(varchar) → integer | Returns the Unicode code point of a single character. | SELECT codepoint(c) FROM public.presto_test; | 65 |
ends_with(varchar, varchar) → boolean | Returns true if the string ends with the given suffix. | SELECT ends_with(concat(a, 'hologres'), 'gres') FROM public.presto_test; | t |
hamming_distance(varchar, varchar) → bigint | Returns the Hamming distance between two strings — the number of positions where characters differ. | SELECT hamming_distance(e, 'SGVsBG7gV39ybGQh') FROM public.presto_test; | 3 |
levenshtein_distance(varchar, varchar) → bigint | Returns the Levenshtein distance (edit distance) between two strings. | SELECT levenshtein(a::text, 'hologres') FROM public.presto_test; | 8 |
normalize(varchar) → varchar | Transforms a string to NFC normalized form. | SELECT normalize(o) FROM public.presto_test; | café |
regexp_split(varchar, varchar) → array(varchar) | Splits a string using a regular expression and returns the parts as an array. | SELECT regexp_split(p, ',') FROM public.presto_test; | {"apple","banana","cherry"} |
strrpos(varchar, varchar) → bigint | Returns the position of the last occurrence of a substring within a string. | SELECT STRRPOS(r, 'A') FROM public.presto_test; | 5 |
trim(varchar, varchar) → varchar | Removes occurrences of the specified characters from both ends of the string. | SELECT trim(r, 'A') FROM public.presto_test; | BCD |
word_stem(varchar, varchar) → varchar | Returns the stem of a word in the specified language. Pass 'en' for English or 'zn' for Chinese. | SELECT word_stem(s, 'en') FROM public.presto_test; | run |
Comparison functions
| Function signature | Description | Example | Result |
|---|---|---|---|
eq(T, T) → boolean | Returns true if the two values are equal. | SELECT eq(c, 'A') FROM public.presto_test; | t |
neq(T, T) → boolean | Returns true if the two values are not equal. | SELECT neq('A', c) FROM public.presto_test; | f |
gt(varchar, varchar) → boolean | Returns true if x > y. | SELECT gt('B', c) FROM public.presto_test; | t |
gte(varchar, varchar) → boolean | Returns true if x ≥ y. | SELECT gte('A', c) FROM public.presto_test; | t |
lt(varchar, varchar) → boolean | Returns true if x < y. | SELECT lt('ABCDA', c) FROM public.presto_test; | f |
lte(varchar, varchar) → boolean | Returns true if x ≤ y. | SELECT lte('A', c) FROM public.presto_test; | t |
JSON functions
| Function signature | Description | Example | Result |
|---|---|---|---|
is_json_scalar(varchar) → boolean | Returns true if the value is a JSON scalar (not an object or array). | SELECT is_json_scalar(l) FROM public.presto_test; | t |
json_array_contains(varchar, T) → boolean | Returns true if the JSON array contains the given value. | SELECT json_array_contains(u, 2) FROM public.presto_test; | t |
json_extract_scalar(varchar, varchar) → varchar | Extracts a scalar value from a JSON string at the given JSONPath. | SELECT json_extract_scalar(m, '$[1]') FROM public.presto_test; | 2 |
json_size(varchar, varchar) → bigint | Returns the number of elements in the JSON object or array at the given JSONPath. | SELECT json_size(n, '$.x') FROM public.presto_test; | 2 |
URL functions
URL extraction follows the URI syntax defined in RFC 2396:
[protocol:][//host[:port]][path][?query][#fragment]Extracted components do not include URI syntax separators such as : or ?.
| Function signature | Description | Example | Result |
|---|---|---|---|
url_decode(varchar) → varchar | Decodes a URL-encoded string. | SELECT url_decode(url_encode(concat('www.', '中文', a, '.com'))) FROM public.presto_test; | www.中文3.com |
url_encode(varchar) → varchar | URL-encodes a string by escaping special characters. | SELECT url_encode(concat('www.', '中文', a, '.com')) FROM public.presto_test; | www.%E4%B8%AD%E6%96%873.com |
url_extract_fragment(varchar) → varchar | Extracts the fragment identifier from a 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(varchar) → varchar | Extracts the host from a 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(varchar, varchar) → varchar | Extracts the value of the first query parameter with the given 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(varchar) → varchar | Extracts the path from a 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(varchar) → bigint | Extracts the port number from a 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(varchar) → varchar | Extracts the protocol from a 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(varchar) → varchar | Extracts the query string from a 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 |