SQL functions & operators
This page details Materialize’s supported SQL functions and operators.
Functions
Unmaterializable functions
Several functions in Materialize are unmaterializable because their output
depends upon state besides their input parameters, like the value of a session
parameter or the timestamp of the current transaction. You cannot create an
index or materialized view that depends on an
unmaterializable function, but you can use them in unmaterialized views and
one-off SELECT
statements.
Unmaterializable functions are marked as such in the table below.
Generic
Generic functions can typically take arguments of any type.
Function | Computes |
---|---|
|
Value as type T
(docs)
|
|
First non-NULL arg, or NULL if all are NULL |
|
The maximum argument, or NULL if all are NULL |
|
The minimum argument, or NULL if all are NULL |
|
NULL if x == y , else x
|
Aggregate
Aggregate functions take one or more of the same element type as arguments.
Function | Computes |
---|---|
|
Aggregate values (including nulls) as an array. (docs) |
|
Average of T ’s values. Returns numeric if x is int , double if x is real , else returns same type as x .
|
|
Number of non-NULL inputs. |
|
Aggregate values (including nulls) as a jsonb array. (docs) |
|
Aggregate keys and values (including nulls) as a jsonb object. (docs) |
|
Maximum value among T
|
|
Minimum value among T
|
|
Historical alias for stddev_samp . (imprecise) Returns numeric if x is int , double if x is real , else returns same type as x .
|
|
Population standard deviation of T ’s values. (imprecise) Returns numeric if x is int , double if x is real , else returns same type as x .
|
|
Sample standard deviation of T ’s values. (imprecise) Returns numeric if x is int , double if x is real , else returns same type as x .
|
|
Concatenates the non-null input values into text. Each value after the first is preceded by the corresponding delimiter. (docs) |
|
Sum of T ’s values Returns bigint if x is int , numeric if x is bigint , else returns same type as x .
|
|
Historical alias for variance_samp . (imprecise) Returns numeric if x is int , double if x is real , else returns same type as x .
|
|
Population variance of T ’s values. (imprecise) Returns numeric if x is int , double if x is real , else returns same type as x .
|
|
Sample variance of T ’s values. (imprecise) Returns numeric if x is int , double if x is real , else returns same type as x .
|
List
List functions take list
arguments, and are polymorphic.
Function | Computes |
---|---|
|
Aggregate values (including nulls) as a list. (docs) |
|
Appends e to l .
|
|
Concatenates l1 and l2 .
|
|
Experimental––Returns the number of dimensions on l .
|
|
Return the number of elements in l .
|
|
Experimental––Return the greatest length among all lists on dimension d of l .
|
|
Prepends e to l .
|
|
Experimental––Returns the list l without any elements equal to the given value e . Comparisons are done using IS NOT DISTINCT FROM semantics, so it is possible to remove NULLs.
|
Numbers
Number functions take number-like arguments, e.g. int
, float
, numeric
, unless otherwise specified.
Function | Computes |
---|---|
|
The absolute value of x
|
|
The cube root of x .
|
|
The smallest integer >= x
|
|
Exponential of x (e raised to the given power)
|
|
The largest integer <= x
|
|
Natural logarithm of x
|
|
Natural logarithm of x
|
|
Base 10 logarithm of x
|
|
Base 10 logarithm of x
|
|
Base 10 logarithm of x , same as log
|
|
Base 10 logarithm of x , same as log
|
|
Base b logarithm of x
|
|
x % y
|
|
Alias of power
|
|
Alias of power
|
|
x raised to the power of y
|
|
x raised to the power of y
|
|
x rounded to the nearest whole number. If N is real or double precision , rounds ties to the nearest even number. If N is numeric , rounds ties away from zero.
|
|
x rounded to y decimal places, while retaining the same numeric scale; rounds ties away from zero.
|
|
The square root of x .
|
|
The square root of x .
|
Trigonometric
Trigonometric functions take and return double precision
values.
Function | Computes |
---|---|
|
The cosine of x , with x in radians.
|
|
The inverse cosine of x , result in radians.
|
|
The hyperbolic cosine of x , with x as a hyperbolic angle.
|
|
The inverse hyperbolic cosine of x .
|
|
The cotangent of x , with x in radians.
|
|
The sine of x , with x in radians.
|
|
The inverse sine of x , result in radians.
|
|
The hyperbolic sine of x , with x as a hyperbolic angle.
|
|
The inverse hyperbolic sine of x .
|
|
The tangent of x , with x in radians.
|
|
The inverse tangent of x , result in radians.
|
|
The hyperbolic tangent of x , with x as a hyperbolic angle.
|
|
The inverse hyperbolic tangent of x .
|
|
Converts degrees to radians. |
|
Converts radians to degrees. |
String
Function | Computes |
---|---|
|
The ASCII value of s ’s left-most character
|
|
Trim all spaces from both sides of s .
|
|
Trim any character in c from both sides of s .
|
|
Number of bits in s
|
|
Number of bits in b
|
|
Number of code points in s
|
|
Character with the given Unicode codepoint. Only supports codepoints that can be encoded in UTF-8. The NULL (0) character is not allowed. |
|
Convert data b from original encoding specified by src_encoding into text .
|
|
Decode s using the specified textual representation.
(docs)
|
|
Encode b using the specified textual representation.
(docs)
|
|
The first n characters of s . If n is negative, all but the last |n| characters of s .
|
|
Number of code points in s
(docs)
|
|
Number of bytes in s
(docs)
|
|
Number of code points in s after encoding
(docs)
|
|
Convert s to lowercase.
|
|
Prepend s with spaces up to length len , or right truncate if len is less than the length of s .
|
|
Prepend s with characters pulled from p up to length len , or right truncate if len is less than the length of s .
|
|
Trim all spaces from the left side of s .
|
|
Trim any character in c from the left side of s .
|
|
Number of bytes in s
|
|
Number of bytes in b
|
|
The starting index of sub within s or 0 if sub is not a substring of s .
|
|
Matches the regular expression needle against haystack, returning a string array that contains the value of each capture group specified in needle , in order. If flags is set to the string i matches case-insensitively.
|
|
Replicate the string n times.
|
|
s with all instances of f replaced with r
|
|
The last n characters of s . If n is negative, all but the first |n| characters of s .
|
|
Trim all spaces from the right side of s .
|
|
Trim any character in c from the right side of s .
|
|
Split s on delimiter d . Return the str at index i , counting from 1.
|
|
Substring of s starting at start_pos
(docs)
|
|
Substring starting at start_pos of length l
(docs)
|
|
Substring starting at start_pos of length l
(docs)
|
|
Trims any character in c from s on the specified side.Defaults: • Side: BOTH • 'c' : ' ' (space)
|
|
Convert s to uppercase.
|
Scalar
Scalar functions take a list of scalar expressions
Function | Computes |
---|---|
|
true if applying bool_op to expression and every value of s evaluates to true
|
|
true if applying bool_op to expression and any value of s evaluates to true
|
|
true for each value in expression if it matches at least one element of s
|
|
true for each value in expression if it does not match any elements of s
|
|
true if applying bool_op to expression and any value of s evaluates to true
|
Subquery
Subquery functions take a query, e.g. SELECT
Function | Computes |
---|---|
|
s must return exactly one column; true if applying bool_op to expression and every value of s evaluates to true
|
|
s must return exactly one column; true if applying bool_op to expression and any value of s evaluates to true
|
|
Extracts separated values from a column containing a CSV file formatted as a string. (docs) |
|
true if s returns at least one row
|
|
s must return exactly one column; true for each value in expression if it matches at least one element of s
|
|
true if s returns zero rows
|
|
s must return exactly one column; true for each value in expression if it does not match any elements of s
|
|
s must return exactly one column; true if applying bool_op to expression and any value of s evaluates to true
|
Date and Time
Time functions take or produce a time-like type, e.g. date
, timestamp
, timestamp with time zone
.
Function | Computes |
---|---|
|
The timestamp with time zone representing when the query was executed.
Note: This function is unmaterializable. |
|
Align source with origin along stride .
(docs)
|
|
Experimental: Align source with the UNIX epoch along stride .
(docs)
|
|
Largest time_component <= val
(docs)
|
|
Largest time_component <= val
(docs)
|
|
Specified time component from value (docs) |
|
Specified time component from value (docs) |
|
The logical time at which a query executes. Used for temporal filters and internal debugging.
Note: This function generally behaves like a [non-pure] function, but can be used in limited contexts in materialized views as a temporal filter. (docs) Note: This function is unmaterializable. |
|
The timestamp with time zone representing when the query was executed.
(docs)
Note: This function is unmaterializable. |
|
Converts timestamp to the specified time zone, expressed as an offset from UTC. Known limitation: You must explicitly cast the type for the time zone. (docs) |
|
Converts timestamp with time zone from UTC to the specified time zone, expressed as the local time. Known limitation: You must explicitly cast the type for the time zone. (docs) |
|
Converts timestamp to specified time zone, expressed as an offset from UTC. Known limitation: You must explicitly cast the type for the time zone. (docs) |
|
Converts timestamp with time zone from UTC to specified time zone, expressed as the local time. Known limitation: You must explicitly cast the type for the time zone. (docs) |
|
Converts Unix epoch (seconds since 00:00:00 UTC on January 1, 1970) to timestamp |
|
Converts a timestamp into a string using the specified format. (docs) |
|
Adjust interval so 30-day time periods are represented as months. (docs) |
|
Adjust interval so 24-hour time periods are represented as days. (docs) |
|
Adjust interval using justify_days and justify_hours, with additional sign adjustments. (docs) |
UUID
Function | Computes |
---|---|
|
The uuid uniquely identifying this Materialize cluster.
Note: This function is unmaterializable. |
JSON
Function | Computes |
---|---|
|
j ’s elements if j is an array.
(docs)
|
|
j ’s elements if j is an array.
(docs)
|
|
Number of elements in j ’s outermost array.
(docs)
|
|
The elements of x in a jsonb array. Elements can be of heterogenous types.
(docs)
|
|
The elements of x as a jsonb object. The argument list alternates between keys and values.
(docs)
|
|
j ’s outermost elements if j is an object.
(docs)
|
|
j ’s outermost elements if j is an object.
(docs)
|
|
j ’s outermost keys if j is an object.
(docs)
|
|
Pretty printed (i.e. indented) j .
(docs)
|
|
Type of j ’s outermost value. One of object , array , string , number , boolean , and null .
(docs)
|
|
j with all object fields with a value of null removed. Other null values remain.
(docs)
|
|
v as jsonb
(docs)
|
Table
Table functions evaluate to a set of rows, rather than a single expression.
Function | Computes |
---|---|
|
Generate all integer values between start and stop , inclusive.
|
|
Generate all integer values between start and stop , inclusive, incrementing by step each time.
|
|
Generate all timestamp values between start and stop , inclusive, incrementing by step each time.
|
|
Generates a series comprising the valid subscripts of the dim ‘th dimension of the given array a .
|
|
Values of the capture groups of regex as matched in haystack
|
|
Expands the array a into a set of rows.
|
|
Expands the list l into a set of rows.
|
Array
Function | Computes |
---|---|
|
Concatenates a1 and a2 .
|
|
Concatenates the elements of array together separated by sep . Null elements are omitted unless ifnull is non-null, in which case null elements are replaced with the value of ifnull .
|
|
Returns the array a without any elements equal to the given value e . The array must be one-dimensional. Comparisons are done using IS NOT DISTINCT FROM semantics, so it is possible to remove NULLs.
|
Cryptography
Function | Computes |
---|---|
|
Computes a binary hash of the given text data using the specified type algorithm. Supported hash algorithms are: md5 , sha1 , sha224 , sha256 , sha384 , and sha512 .
|
|
Computes a binary hash of the given bytea data using the specified type algorithm. The supported hash algorithms are the same as for the text variant of this function.
|
|
Computes a hashed MAC of the given text data using the specified key and type algorithm. Supported hash algorithms are the same as for digest .
|
|
Computes a hashed MAC of the given bytea data using the specified key and type algorithm. The supported hash algorithms are the same as for digest .
|
|
Computes the MD5 hash of the given bytea data . For PostgreSQL compatibility, returns a hex-encoded value of type text rather than bytea .
|
|
Computes the SHA-224 hash of the given bytea data .
|
|
Computes the SHA-256 hash of the given bytea data .
|
|
Computes the SHA-384 hash of the given bytea data .
|
|
Computes the SHA-512 hash of the given bytea data .
|
Window
Window functions compute values across sets of rows related to the current query.
Function | Computes |
---|---|
|
Returns the rank of the current row within its partition without gaps, counting from 1. |
|
Returns value evaluated at the first row of the window frame. The default window frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW .
|
|
Returns value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead returns default (which must be of a type compatible with value ). If offset is NULL , NULL is returned instead. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to NULL .
|
|
Returns value evaluated at the last row of the window frame. The default window frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW .
|
|
Returns value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead returns default (which must be of a type compatible with value ). If offset is NULL , NULL is returned instead. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to NULL .
|
|
Returns the number of the current row within its partition, counting from 1. |
System information
Functions that return information about the system
Function | Computes |
---|---|
|
Returns the length of time that the materialized process has been running.
Note: This function is unmaterializable. |
|
Returns the server’s version information as a human-readable string.
Note: This function is unmaterializable. |
|
Returns the name of the current database.
Note: This function is unmaterializable. |
|
Alias for current_user .
Note: This function is unmaterializable. |
|
Returns the name of the user who executed the containing query.
Note: This function is unmaterializable. |
|
Returns the name of the user who executed the containing query.
Note: This function is unmaterializable. |
|
Returns the number of bytes used to store a row. |
PostgreSQL compatibility
Functions whose primary purpose is to facilitate compatibility with PostgreSQL tools. These functions may have suboptimal performance characteristics.
Function | Computes |
---|---|
|
Returns the canonical SQL name for the type specified by oid with typemod applied.
|
|
Returns the name of the first non-implicit schema on the search path, or NULL if the search path is empty.
Note: This function is unmaterializable. |
|
Returns the names of the schemas on the search path. The include_implicit parameter controls whether implicit schemas like mz_catalog and pg_catalog are included in the output.
Note: This function is unmaterializable. |
|
PostgreSQL compatibility shim. Currently always returns NULL .
|
|
Returns the internal connection ID.
Note: This function is unmaterializable. |
|
Returns the number of bytes used to store any individual data value. |
|
Returns the constraint definition for the given oid . Currently always returns NULL since constraints aren’t supported.
|
|
Reconstructs the creating command for an index. (This is a decompiled reconstruction, not the original text of the command.) If column is supplied and is not zero, only the definition of that column is reconstructed. |
|
Returns the underlying SELECT command for the given view. |
|
Returns the underlying SELECT command for the given view. |
|
Returns the underlying SELECT command for the given view. |
|
Reports whether the relation with the specified OID is visible in the search path. |
|
Reports whether the type with the specified OID is visible in the search path. |
|
Returns the type of its input argument as a string. |
|
PostgreSQL compatibility shim. Not intended for direct use. |
|
Returns the time when the server started.
Note: This function is unmaterializable. |
|
Returns a PostgreSQL-compatible version string.
Note: This function is unmaterializable. |
Operators
Generic
Operator | Computes |
---|---|
val::type |
Cast of val as type (docs) |
Boolean
Operator | Computes |
---|---|
AND |
Boolean “and” |
OR |
Boolean “or” |
= |
Equality |
<> |
Inequality |
!= |
Inequality |
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
a BETWEEN x AND y |
a >= x AND a <= y |
a NOT BETWEEN x AND y |
a < x OR a > y |
a IS NULL |
a = NULL |
a ISNULL |
a = NULL |
a IS NOT NULL |
a != NULL |
a IS TRUE |
a is true, requiring a to be a boolean |
a IS NOT TRUE |
a is not true, requiring a to be a boolean |
a IS FALSE |
a is false, requiring a to be a boolean |
a IS NOT FALSE |
a is not false, requiring a to be a boolean |
a IS UNKNOWN |
a = NULL , requiring a to be a boolean |
a IS NOT UNKNOWN |
a != NULL , requiring a to be a boolean |
a LIKE match_expr [ ESCAPE escape_char ] |
a matches match_expr , using SQL LIKE matching |
a ILIKE match_expr [ ESCAPE escape_char ] |
a matches match_expr , using case-insensitive SQL LIKE matching |
Numbers
Operator | Computes |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulo |
& |
Bitwise AND |
` | ` |
# |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Bitwise left shift |
>> |
Bitwise right shift |
String
Operator | Computes |
---|---|
|| |
Concatenation |
~ |
Matches regular expression, case sensitive |
~* |
Matches regular expression, case insensitive |
!~ |
Does not match regular expression, case insensitive |
!~* |
Does not match regular expression, case insensitive |
The regular expression syntax supported by Materialize is documented by the
Rust regex
crate.
Time-like
Operation | Computes |
---|---|
date + interval |
timestamp |
date - interval |
timestamp |
date + time |
timestamp |
date - date |
interval |
timestamp + interval |
timestamp |
timestamp - interval |
timestamp |
timestamp - timestamp |
interval |
time + interval |
time |
time - interval |
time |
time - time |
interval |
JSON
Operator | RHS Type | Description |
---|---|---|
-> |
text , int |
Access field by name or index position, and return jsonb (docs) |
->> |
text , int |
Access field by name or index position, and return text (docs) |
#> |
text[] |
Access field by path, and return jsonb (docs) |
#>> |
text[] |
Access field by path, and return text (docs) |
|| |
jsonb |
Concatenate LHS and RHS (docs) |
- |
text |
Delete all values with key of RHS (docs) |
@> |
jsonb |
Does element contain RHS? (docs) |
<@ |
jsonb |
Does RHS contain element? (docs) |
? |
text |
Is RHS a top-level key? (docs) |
Map
Operator | RHS Type | Description |
---|---|---|
-> |
string |
Access field by name, and return target field (docs) |
@> |
map |
Does element contain RHS? (docs) |
<@ |
map |
Does RHS contain element? (docs) |
? |
string |
Is RHS a top-level key? (docs) |
?& |
string[] |
Does LHS contain all RHS top-level keys? (docs) |
?| |
string[] |
Does LHS contain any RHS top-level keys? (docs) |
List
List operators are polymorphic.
Operator | Description |
---|---|
listany || listany |
Concatenate the two lists. |
listany || listelementany |
Append the element to the list. |
listelementany || listany |
Prepend the element to the list. |