User-defined functions
SQL UDFs
You can define SQL UDFs in RisingWave by using the CREATE FUNCTION command.
Syntax
For more details about the supported syntax, see the examples of SQL UDFs below.
Parameters
Parameter or clause | Description |
---|---|
function_name | The name of the SQL UDF that you want to declare in RisingWave. |
argument_type | The data type of the input parameter(s) that the SQL UDF expects to receive. |
RETURNS return_type | Specifies the data type of the return value from the UDF. |
LANGUAGE sql | Its value must be sql. |
AS as_definition | Defines the implementation of the function using SQL statements. as_definition can be single quote definition (e.g., ‘select 2’) or double dollar definition (e.g., select $1 + $1). |
RETURN return_definition | Alternative to the AS clause. return_definition can be an expression (e.g., 2). Note that you must specify an AS definition or a RETURN definition, and they can not be specified simultaneously. |
NOTE
- Recursive definition is NOT supported at present. For example, the statement
create function recursive(INT, INT) returns int language sql as 'select recursive($1, $2) + recursive($1, $2)';
will fail.
Examples
At present, we support SQL UDFs with unnamed and named parameters. This section offers examples of the current supported syntax. We will offer some basic examples first to help you understand and grasp them. Then, we will offer more examples that are closer to real-world scenarios, such as a mock table, for your further practice and understanding.
Basic examples for SQL UDFs with unnamed and named parameters
- Create a SQL UDF with unnamed parameters and double dollar definition.
Create function
Call function
- Create a SQL UDF with unnamed parameters and single quote definition.
Create function
Call function
- Create a SQL UDF with unnamed parameters that calls other pre-defined SQL UDFs.
Create function
Call function
- Create a SQL UDF with named parameters and single quote definition.
Create function
Call function
- Create a SQL UDF with named parameters and double dollar definition.
Create function
Call function
- Create a SQL UDF with mixed named and unnamed parameters.
Create function
Call function
- Call a SQL UDF with unnamed parameters inside a SQL UDF with named parameters.
Create function
Call function
- Create a SQL UDF with unnamed parameters and a return expression.
Create function
Create function
- Create a SQL UDF with a return expression using previously defined UDFs.
Create function
Call function
- Create a SQL UDF with multiple types of parameters.
Create function
Call function
- Create a SQL UDF with complex types of unnamed parameters.
Create function
Call function
- Create a wrapper function.
Create function
Call function
Basic SQL UDFs integrated with the use of mock tables
The examples in this section are a basic simulation of real-world use cases.
Create table
Insert data
Create function
Call function
Create function
Call function
Create function
Call function
Examples of corner and special cases tests
Create function
Call function
Create function
Call function
Create function
Call function
Create function
Call function
NOTE
Note that double dollar signs should be used otherwise the parsing will fail.
Create function
Call function
Create function
Call function
Was this page helpful?