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 non-materialized views and
one-off SELECT
statements.
Unmaterializable functions are marked as such in the table below.
Side-effecting functions
Several functions in Materialize are side-effecting because their evaluation
changes system state. For example, the pg_cancel_backend
function allows
canceling a query running on another connection.
Materialize offers only limited support for these functions. They may be called
only at the top level of a SELECT
statement, like so:
SELECT side_effecting_function(arg, ...);
You cannot manipulate or alias the function call expression, call multiple
side-effecting functions in the same SELECT
statement, nor add any additional
clauses to the SELECT
statement (e.g., FROM
, WHERE
).
Side-effecting functions are marked as such in the table below.
Generic functions
Generic functions can typically take arguments of any type.
Function |
---|
Value as type |
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 |
Aggregate functions
Aggregate functions take one or more of the same element type as arguments.
Function |
---|
Aggregate values (including nulls) as an array |
Average of Returns |
NULL if all values of |
NULL if all values of |
Number of non-NULL inputs. |
Aggregate values (including nulls) as a jsonb array |
Aggregate keys and values (including nulls) as a jsonb object |
Maximum value among |
Minimum value among |
Historical alias for Returns |
Population standard deviation of Returns |
Sample standard deviation of Returns |
Concatenates the non-null input values into text. Each value after the first is preceded by the corresponding delimiter |
Sum of Returns |
Historical alias for Returns |
Population variance of Returns |
Sample variance of Returns |
List functions
List functions take list
arguments, and are polymorphic.
Function |
---|
Aggregate values (including nulls) as a list |
Appends |
Concatenates |
Return the number of elements in |
Prepends |
Map functions
Map functions take map
arguments, and are polymorphic.
Function |
---|
Return the number of elements in |
Builds a map from a list of records whose fields are two elements, the
first of which is |
Aggregate keys and values (including nulls) as a map |
Numbers functions
Number functions take number-like arguments, e.g. int
, float
, numeric
, unless otherwise specified.
Function |
---|
The absolute value of |
The cube root of |
The smallest integer >= |
Alias of |
Exponential of |
The largest integer <= |
Natural logarithm of |
Natural logarithm of |
Base 10 logarithm of |
Base 10 logarithm of |
Base 10 logarithm of |
Base 10 logarithm of |
Base |
|
Alias of |
Alias of |
|
|
|
|
The square root of |
The square root of |
|
Trigonometric functions
Trigonometric functions take and return double precision
values.
Function |
---|
The cosine of |
The inverse cosine of |
The hyperbolic cosine of |
The inverse hyperbolic cosine of |
The cotangent of |
The sine of |
The inverse sine of |
The hyperbolic sine of |
The inverse hyperbolic sine of |
The tangent of |
The inverse tangent of |
The hyperbolic tangent of |
The inverse hyperbolic tangent of |
Converts degrees to radians. |
Converts radians to degrees. |
String functions
Function |
---|
The ASCII value of |
Trim all spaces from both sides of |
Trim any character in |
Number of bits in |
Number of bits in |
Number of code points in |
Character with the given Unicode codepoint. Only supports codepoints that can be encoded in UTF-8. The NULL (0) character is not allowed. |
Concatenates the text representation of non-NULL arguments. |
Concatenates the text representation of non-NULL arguments from |
Convert data |
Decode |
Encode |
Return the |
Returns |
Returns |
Returns |
The first |
Number of code points in |
Number of bytes in |
Number of code points in |
Convert |
Prepend |
Prepend |
Trim all spaces from the left side of |
Trim any character in |
Number of bytes in |
Number of bytes in |
Given a qualified identifier like |
The starting index of |
Matches the regular expression |
Replaces the first occurrence of If
See the rust regex docs for more details about replacement. |
Splits |
Replicate the string |
|
The last |
Trim all spaces from the right side of |
Trim any character in |
Split |
Report whether |
Substring of |
Substring starting at |
Substring starting at |
Any character in |
Trims any character in Defaults: • Side: • |
Parses a specific subset of ISO8601 timestamps, returning |
Convert |
Scalar functions
Scalar functions take a list of scalar expressions
Function |
---|
|
|
|
|
|
Subquery functions
Subquery functions take a query, e.g. SELECT
Function |
---|
|
|
Extracts separated values from a column containing a CSV file formatted as a string |
|
|
|
|
|
Date and time functions
Time functions take or produce a time-like type, e.g. date
, timestamp
, timestamp with time zone
.
Function |
---|
Subtracts one timestamp from another, producing a “symbolic” result that uses years and months, rather than just days. |
The Note: This function is unmaterializable. |
Align |
Largest |
Largest |
Specified time component from value |
Specified time component from value |
The logical time at which a query executes. Used for temporal filters and query timestamp introspection Note: This function is unmaterializable, but can be used in limited contexts in materialized views as a temporal filter. |
The Note: This function is unmaterializable. |
Converts Known limitation: You must explicitly cast the type for the time zone. |
Converts Known limitation: You must explicitly cast the type for the time zone. |
Converts Known limitation: You must explicitly cast the type for the time zone. |
Converts Known limitation: You must explicitly cast the type for the time zone. |
Describes a time zone’s offset from UTC at a specified moment.
|
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 |
Adjust interval so 30-day time periods are represented as months |
Adjust interval so 24-hour time periods are represented as days |
Adjust interval using justify_days and justify_hours, with additional sign adjustments |
UUID functions
Function |
---|
Generates a version 5 UUID (SHA-1) in the given namespace using the specified input name. |
JSON functions
Function |
---|
Aggregate values (including nulls) as a jsonb array |
|
|
Number of elements in |
Output each element of |
The elements of x as a |
|
|
Aggregate keys and values (including nulls) as a |
|
Pretty printed (i.e. indented) |
Type of |
|
|
Table functions
Table functions evaluate to a set of rows, rather than a single expression.
Function |
---|
Generate all integer values between |
Generate all integer values between |
Generate all timestamp values between |
Generates a series comprising the valid subscripts of the |
Values of the capture groups of |
Splits |
Expands the array |
Expands the list |
Expands the map |
Array functions
Function |
---|
Concatenates |
Returns an array initialized with supplied value and dimensions, optionally with lower bounds other than 1. |
Returns the length of the specified dimension of the array. |
Returns the subscript of |
Returns the subscript of |
Concatenates the elements of |
Returns the array |
Hash functions
Function |
---|
Computes the 32-bit cyclic redundancy check of the given bytea |
Computes the 32-bit cyclic redundancy check of the given text |
Computes a binary hash of the given text |
Computes a binary hash of the given bytea |
Computes a hashed MAC of the given text |
Computes a hashed MAC of the given bytea |
Computes the Murmur2 hash of the given bytea |
Computes the Murmur2 hash of the given text |
Computes the MD5 hash of the given bytea |
Computes the SeaHash hash of the given bytea |
Computes the SeaHash hash of the given text |
Computes the SHA-224 hash of the given bytea |
Computes the SHA-256 hash of the given bytea |
Computes the SHA-384 hash of the given bytea |
Computes the SHA-512 hash of the given bytea |
Window functions
Window functions compute values across sets of rows related to the current row. For example, you can use a window aggregation to smooth measurement data by computing the average of the last 5 measurements before every row as follows:
SELECT
avg(measurement) OVER (ORDER BY time ROWS BETWEEN 4 PRECEDING AND CURRENT ROW)
FROM measurements;
Window functions always need an OVER
clause. For the OVER
clause, Materialize supports the same
syntax as
PostgreSQL,
but supports only the following frame modes:
-
the
ROWS
frame mode. -
the default frame, which is
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
.
For window functions, when an input record
in a partition (as determined by the PARTITION BY
clause of your window
function) is added/removed/changed, Materialize recomputes the results for the
entire window partition. This means that when a new batch of input data arrives
(that is, every second), the amount of computation performed is proportional
to the total size of the touched partitions.
For example, assume that in a given second, 20 input records change, and these records belong to 10 different partitions, where the average size of each partition is 100. Then, amount of work to perform is proportional to computing the window function results for 10*100=1000 rows.
As a rule of thumb, if the total size of all touched window partitions is at most 1000000 rows per second, then the system should be able to keep up with the input data as it arrives. However, if your use case has higher performance requirements, consider rewriting your query to not use window functions. If your query cannot be rewritten without the window functions and the performance of window functions is insufficient for your use case, please contact our team.
See Idiomatic Materialize SQL for examples of rewriting window functions.
In addition to the below window functions, you can use the OVER
clause with any aggregation function
(e.g., sum
, avg
) as well. Using an aggregation with an OVER
clause is called a window aggregation. A
window aggregation computes the aggregate not on the groups specified by the GROUP BY
clause, but on the frames
specified inside the OVER
clause. (Note that a window aggregation produces exactly one output value for each input
row. This is different from a standard aggregation, which produces one output value for each group specified by
the GROUP BY
clause.)
Function |
---|
Returns the rank of the current row within its partition without gaps, counting from 1. Rows that compare equal will have the same rank. |
Returns See also Idiomatic Materialize SQL: First value. |
Returns See also Idiomatic Materialize SQL: Lag over. |
Returns See also Idiomatic Materialize SQL: Last value. |
Returns See also Idiomatic Materialize SQL: Lead over. |
Returns the rank of the current row within its partition with gaps (counting from 1): rows that compare equal will have the same rank, and then the rank is incremented by the number of rows that compared equal. |
Returns the number of the current row within its partition, counting from 1. Rows that compare equal will be ordered in an unspecified way. See also Idiomatic Materialize SQL: Top-K. |
System information functions
Functions that return information about the system.
Function |
---|
Returns a string containing a Note: This function is unmaterializable. |
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 server’s version as an integer having the format Note: This function is unmaterializable. |
Returns the name of the current database. Note: This function is unmaterializable. |
Alias for Note: This function is unmaterializable. |
Returns the name of the user who executed the containing query. Note: This function is unmaterializable. |
Alias for Note: This function is unmaterializable. |
Alias for Note: This function is unmaterializable. |
Returns the name of the user who initiated the database connection. Note: This function is unmaterializable. |
Returns the number of bytes used to store a row. |
PostgreSQL compatibility functions
Functions whose primary purpose is to facilitate compatibility with PostgreSQL tools. These functions may have suboptimal performance characteristics.
Function |
---|
Returns the canonical SQL name for the type specified by |
Returns the name of the first non-implicit schema on the search path, or
Note: This function is unmaterializable. |
Returns the names of the schemas on the search path.
The Note: This function is unmaterializable. |
Returns the value of the named setting or error if it does not exist.
If Note: This function is unmaterializable. |
Returns the comment for a database object specified by its |
Returns the comment for a table column, which is specified by the |
Returns the internal connection ID. Note: This function is unmaterializable. |
Cancels an in-progress query on the specified connection ID. Returns whether the connection ID existed (not if it cancelled a query). Note: This function is side-effecting. |
Returns the number of bytes used to store any individual data value. |
Converts a size in bytes into a human-readable format. |
Returns the constraint definition for the given |
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. |
Reconstructs the creating command for a rule. This function always returns NULL because Materialize does not support rules. |
Returns the role (user) name for the given |
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. |
Alias for |
Returns if the a recovery is still in progress. |
Reports whether the relation with the specified OID is visible in the search path. |
Returns the path in the file system that the provided tablespace is on. |
Reports whether the type with the specified OID is visible in the search path. |
Reports whether the function 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. |
Disk space used by the specified fork (‘main’, ‘fsm’, ‘vm’, or ‘init’) of the specified table or index. If no fork is specified, it defaults to ‘main’. This function always returns -1 because Materialize does not store tables and indexes on local disk. |
Number of sequential scans done when argument is a table, or number of index scans done when argument is an index. This function always returns -1 because Materialize does not collect statistics. |
Returns a PostgreSQL-compatible version string. Note: This function is unmaterializable. |
Access privilege inquiry functions
Functions that allow querying object access privileges. None of the following functions consider whether the provided role is a superuser or not.
Function |
---|
Reports whether the role with the specified role name or OID has the privilege on
the cluster with the specified cluster name. If the role is omitted then
the |
Reports whether the role with the specified role name or OID has the privilege on
the connection with the specified connection name or OID. If the role is omitted then
the |
Reports whether the role with the specified role name or OID has the privilege on
the database with the specified database name or OID. If the role is omitted then
the |
Reports whether the role with the specified role name or OID has the privilege on
the schema with the specified schema name or OID. If the role is omitted then
the |
Reports whether the |
Reports whether the role with the specified role name or OID has the privilege on
the secret with the specified secret name or OID. If the role is omitted then
the |
Reports whether the role with the specified role name or OID has the system privilege.
If the role is omitted then the |
Reports whether the role with the specified role name or OID has the privilege on
the relation with the specified relation name or OID. If the role is omitted then
the |
Reports whether the role with the specified role name or OID has the privilege on
the type with the specified type name or OID. If the role is omitted then
the |
Reports whether the |
Operators
Generic operators
Operator | Computes |
---|---|
val::type |
Cast of val as type (docs) |
Boolean operators
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 operators
Operator | Computes |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulo |
& |
Bitwise AND |
| |
Bitwise OR |
# |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Bitwise left shift |
>> |
Bitwise right shift |
String operators
Operator | Computes |
---|---|
|| |
Concatenation |
~~ |
Matches LIKE pattern case sensitively, see SQL LIKE matching |
~~* |
Matches LIKE pattern case insensitively (ILIKE), see SQL LIKE matching |
!~~ |
Matches NOT LIKE pattern (case sensitive), see SQL LIKE matching |
!~~* |
Matches NOT ILIKE pattern (case insensitive), see SQL LIKE matching |
~ |
Matches regular expression, case sensitive |
~* |
Matches regular expression, case insensitive |
!~ |
Matches regular expression case sensitively, and inverts the match |
!~* |
Match regular expression case insensitively, and inverts the match |
The regular expression syntax supported by Materialize is documented by the
Rust regex
crate.
Time-like operators
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 operators
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 operators
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 operators
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. |
listany @> listany |
Check if the first list contains all elements of the second list. |
listany <@ listany |
Check if all elements of the first list are contained in the second list. |