Skip to Content
OpenETL
Reference

SQL functions

Look up conditional, string, numeric, time, hash, list, struct and JSON functions.

Edit on GitHub

Use these function tables with the SQL expression and type reference.

Functions

These are the functions a statement can call, with their other names. Functions that are not deterministic give a new value each time they run, so a row read again gets another value. A function not listed here is refused:

my_udf isn't a function transforms can call

crates/sql/functions.txt is the list the engine has, and go test ./test/docs fails when this page differs from it.

Conditionals

FunctionOther namesMeaning
coalesceIts first argument that is not null
greatestThe largest of its arguments, ignoring nulls
leastThe smallest of its arguments, ignoring nulls
nullifnullif(a, b): null if a equals b, else a
nvlifnullnvl(a, b): b if a is null, else a
nvl2nvl2(a, b, c): b if a is not null, else c

Strings

FunctionOther namesMeaning
asciiThe code of the string's first character
bit_lengthThe string's length in bits
btrimtrimThe string without the given characters, spaces by default, at both ends
character_lengthchar_length, lengthThe string's length in characters
chrThe character with the given code
concatIts arguments joined, ignoring nulls
concat_wsconcat_ws(separator, …): its other arguments joined by the separator, ignoring nulls
containsWhether the string contains another
ends_withWhether the string ends with another
find_in_setfind_in_set(s, list): the position of s in a comma-separated list, from 1, or 0
initcapThe string with the first letter of each word in upper case
leftThe first n characters
levenshteinThe edit distance between two strings
lowerThe string in lower case
lpadThe string padded on the left to n characters, with spaces or the given string
ltrimThe string without the given characters, spaces by default, at its start
octet_lengthThe string's length in bytes
overlayoverlay(s PLACING r FROM start FOR n): s with n characters from start replaced by r
repeatThe string repeated n times
replacereplace(s, from, to): s with every from replaced by to
reverseThe string's characters in reverse order
rightThe last n characters
rpadThe string padded on the right to n characters, with spaces or the given string
rtrimThe string without the given characters, spaces by default, at its end
split_partsplit_part(s, delimiter, n): the nth part of s, from 1
starts_withWhether the string starts with another
strposinstr, positionstrpos(s, sub): the position of sub in s, from 1, or 0
substrsubstringsubstr(s, start, length): part of s, from start, counting from 1
substr_indexsubstring_indexsubstr_index(s, delimiter, n): s before the nth delimiter, or with a negative n, after it, counting from the end
to_hexAn integer in hexadecimal
translatetranslate(s, from, to): s with each character of from replaced by the one at the same place in to
upperThe string in upper case
uuidNot deterministic. A new random UUID, as a string.

Regular expressions

FunctionOther namesMeaning
regexp_countThe number of matches in the string
regexp_instrThe position of a match in the string, from 1, or 0
regexp_likeWhether the string matches
regexp_matchThe capture groups of the first match, as a list
regexp_replaceThe string with matches replaced; the flag g replaces every match

Numbers

FunctionOther namesMeaning
absThe absolute value
acosThe arc cosine
acoshThe inverse hyperbolic cosine
asinThe arc sine
asinhThe inverse hyperbolic sine
atanThe arc tangent
atan2atan2(y, x): the arc tangent of y / x
atanhThe inverse hyperbolic tangent
cbrtThe cube root
ceilThe smallest integer not less than the number
cosThe cosine
coshThe hyperbolic cosine
cotThe cotangent
degreesRadians as degrees
expe to the power of the number
factorialThe factorial
floorThe largest integer not greater than the number
gcdThe greatest common divisor of two integers
isnanWhether the number is NaN
iszeroWhether the number is zero
lcmThe least common multiple of two integers
lnThe natural logarithm
loglog(base, x), or log(x) in base 10
log10The base-10 logarithm
log2The base-2 logarithm
nanvlnanvl(x, y): y if x is NaN, else x
piπ
powerpowpower(x, y): x to the power of y
radiansDegrees as radians
randomrandNot deterministic. A random number from 0, inclusive, to 1.
roundThe number rounded to the given number of decimal places, 0 by default
signum-1, 0 or 1, by the number's sign
sinThe sine
sinhThe hyperbolic sine
sqrtThe square root
tanThe tangent
tanhThe hyperbolic tangent
truncThe number truncated to the given number of decimal places, 0 by default

Dates and times

FunctionOther namesMeaning
current_dateNot deterministic. The date in UTC when the engine evaluates the batch.
date_bindate_bin(interval, ts): the start of the interval-long bin that holds ts
date_partdatepartdate_part(part, ts): a part of the time, such as 'year', 'hour' or 'dow'
date_truncdatetruncdate_trunc(part, ts): the time truncated to the part, such as 'day'
from_unixtimeSeconds since 1970-01-01 as a timestamp
make_datemake_date(year, month, day): a date
nowcurrent_timestampNot deterministic. The time in UTC when the engine evaluates the batch.
timezonetimezone(zone, ts): ts AT TIME ZONE zone. See Time zones.
to_chardate_formatto_char(value, format): a date or time as a string, in a strftime-style format such as '%Y-%m-%d'
to_dateA string or number of days as a date, in the given formats or ISO 8601
to_timestampA string or number of seconds as a timestamp in nanoseconds, in the given formats or RFC 3339
to_timestamp_microsAs to_timestamp, in microseconds, and a number counts microseconds
to_timestamp_millisAs to_timestamp, in milliseconds, written as microseconds, and a number counts milliseconds
to_timestamp_nanosAs to_timestamp, and a number counts nanoseconds
to_timestamp_secondsAs to_timestamp, in seconds, written as microseconds
to_unixtimeA timestamp, or a string in the given formats, as seconds since 1970-01-01

Hashes and encodings

FunctionOther namesMeaning
decodedecode(s, format): base64 or hex text as binary
digestdigest(value, algorithm): a hash as binary, by an algorithm such as 'sha256'
encodeencode(value, format): binary as base64 or hex text
md5The MD5 hash, in hexadecimal
sha224The SHA-224 hash, as binary
sha256The SHA-256 hash, as binary
sha384The SHA-384 hash, as binary
sha512The SHA-512 hash, as binary

Lists

FunctionOther namesMeaning
array_appendarray_push_back, list_append, list_push_backThe list with a value added at its end
array_concatarray_cat, list_cat, list_concatThe lists joined end to end
array_distinctlist_distinctThe list without repeated values
array_elementarray_extract, list_element, list_extractarray_element(list, i): the ith element, from 1, as list[i]
array_hasarray_contains, list_contains, list_hasWhether the list has the value
array_has_alllist_has_allWhether the list has every value of another list
array_has_anyarrays_overlap, list_has_anyWhether the list has any value of another list
array_lengthlist_lengthThe list's length
array_positionarray_indexof, list_indexof, list_positionThe position of the value in the list, from 1, or null
array_prependarray_push_front, list_prepend, list_push_frontarray_prepend(value, list): the list with the value added at its start
array_removelist_removeThe list without the first element equal to the value
array_reverselist_reverseThe list in reverse order
array_slicelist_slicearray_slice(list, from, to): the elements from from to to, both included, from 1
array_sortlist_sortThe list sorted, ascending by default
array_to_stringarray_join, list_join, list_to_stringarray_to_string(list, separator): the elements as one string
cardinalityThe number of elements in the list
flattenA list of lists as one list
make_arraymake_listIts arguments as a list
string_to_arraystring_to_liststring_to_array(s, delimiter): the string's parts as a list

Structs

FunctionOther namesMeaning
get_fieldget_field(struct, 'name'): a field's value, as s['name']
named_structnamed_struct('a', x, 'b', y): a struct with those fields
structrowA struct of its arguments, with fields c0, c1 and so on

An output cannot hold a struct, so a struct is useful only inside an expression, such as named_struct('a', id)['a'].

JSON

The JSON functions read a document from a JSON column or from any string.

FunctionOther namesMeaning
json_as_textThe value as text: a string's own value, another value's JSON text, and null for a JSON null. As ->> and #>>.
json_containsWhether the path reaches a value, a JSON null included. As ?. Never null: false for a null document.
json_from_scalarscalar_to_jsonA boolean, number or string as a JSON value
json_getThe value, of any JSON type. As -> and #>.
json_get_arrayAn array's elements as a list, each as its JSON text: a string element keeps its quotes
json_get_boolA boolean, or a string that spells one
json_get_floatA number, or a string that spells one, as float64
json_get_intAn integer, a number with no fraction that int64 holds, or a string that spells an integer
json_get_jsonThe value's JSON text, as the document writes it; a JSON null is the text null
json_get_strA string's value
json_lengthjson_lenThe number of an array's elements or an object's keys
json_object_keysjson_keysAn object's keys, as a list
json_union_to_textA value of json_get as JSON text, and null for a JSON null

Each function but json_from_scalar takes the document, then a path: keys and array indexes, as further arguments or as one list. json_from_scalar takes one boolean, number or string, and no path. A value of another type than the function gives is null: json_get_str of a number is null.

  • A key is a string, and matches exactly. An index is an integer, from 0. A negative index counts from the end: -1 is the last element.
  • A key that reaches an array is an index if it spells one: '1' is the second element.
  • A path can come from columns, so it can differ by row: json_get_str(doc, field_name).
  • A path that the document does not have gives null. So does a null step and a null document.
  • Text that is not valid JSON throughout gives null, and json_contains gives false. A truncated document is not valid.
  • If an object has a key twice, the last value counts.

The operators are the functions:

OperatorFunctionExample
doc -> 'a'json_get(doc, 'a')doc->'user'->'emails'->0, a chain, is one call with the whole path
doc ->> 'a'json_as_text(doc, 'a')doc->'user'->>'email'
doc #> ARRAY['a', 'b']json_get(doc, ARRAY['a', 'b'])The path as one list
doc #>> ARRAY['a', 'b']json_as_text(doc, ARRAY['a', 'b'])
doc ? 'a'json_contains(doc, 'a')Whether the path exists. It takes a path, not only a top-level key: '[1, 2]' ? 1 is true.

A value that ->, #> or json_get gives can be of any JSON type:

  • Selected into an output column, it is JSON, written as the document writes it: {"a": 1}, "text", 3.50.
  • Compared with a value, it is read as that value's type: doc->'status' = 'active' compares the string, and doc->'n' > 3 the number. A JSON null and a missing path are null.
  • Cast, it is read with the function of that type: CAST(doc->'n' AS BIGINT) is json_get_int(doc, 'n'), and a cast to VARCHAR or TEXT is ->>'s text.

On this page