Declare your functions in RisingWave
You can utilize the CREATE FUNCTION command to develop UDFs in Rust. The syntax is outlined below:gcd
can be defined as follows:
fn
function, and the function’s name, parameters, and return type must match those declared in the CREATE FUNCTION
statement. Refer to the Data type mapping for details on the correspondence between SQL types and Rust types.
For table functions, your function must return an impl Iterator<Item = T>
type, where T
is the type of the returned elements. For example, to generate a sequence from 0 to n-1:
struct
and annotate it with #[derive(StructType)]
. Its fields must match the struct type declared in the CREATE FUNCTION
statement. Then, define the function and annotate it with the #[function("...")]
macro. The string in the macro represents the SQL signature of the function, with the tail return type being struct StructName
. For the specific syntax, see arrow-udf.
Currently, in CREATE FUNCTION
statement, Rust code can only use libraries from the standard library, chrono
, rust_decimal
, serde_json
, and does not support other third-party libraries. If you wish to use other libraries, you may consider compiling WebAssembly modules manually.
Use your functions in RisingWave
After creating UDFs in RisingWave, you can employ them in SQL queries just as you would with any built-in functions. For example:Alternative: Manually build your functions into a WebAssembly module
If you want to use other libraries in your Rust functions, you can manually build your functions into a WebAssembly module and then load it into RisingWave.Prerequisites
- Ensure that you have Rust toolchain (stable channel) installed on your computer.
- Ensure that the Rust standard library for
wasm32-wasip1
target is installed:
1. Create a project
Create a Rust project namedudf
:
Cargo.toml
:
2. Define your functions
Insrc/lib.rs
, define your functions using the function
macro:
3. Build the project
Build your functions into a WebAssembly module:target/wasm32-wasip1/release/udf.wasm
.
Optional: It is recommended to strip the binary to reduce its size:
4. Declare your functions in RisingWave
In RisingWave, use the CREATE FUNCTION command to declare the functions you defined. There are two ways to load the WASM module:- Embed the WASM binary into SQL with base64 encoding. You can use the following command in psql:
- Load the WASM binary from the local file system of the frontend. Note the
fs://
is URI schema, and/path/to/udf.wasm
is the real path.
5. Use your functions in RisingWave
Once the UDFs are created in RisingWave, you can use them in SQL queries just like any built-in functions. For example:Data type mapping
The following table shows the data type mapping between SQL and Rust:SQL type | Rust type as argument | Rust type as return value |
---|---|---|
boolean | bool | bool |
smallint | i16 | i16 |
integer | i32 | i32 |
bigint | i64 | i64 |
real | f32 | f32 |
double precision | f64 | f64 |
decimal | rust_decimal::Decimal | rust_decimal::Decimal |
date | chrono::NaiveDate | chrono::NaiveDate |
time | chrono::NaiveTime | chrono::NaiveTime |
timestamp | chrono::NaiveDateTime | chrono::NaiveDateTime |
timestamptz | not supported yet | not supported yet |
interval | arrow_udf::types::Interval | arrow_udf::types::Interval |
jsonb | serde_json::Value | serde_json::Value |
varchar | &str | impl AsRef<str>, e.g. String, Box<str>, &str |
bytea | &[u8] | impl AsRef<[u8]>, e.g. Vec<u8>, Box<[u8]>, &[u8] |
smallint[] | &[i16] | impl Iterator<Item = i16> |
integer[] | &[i32] | impl Iterator<Item = i32> |
bigint[] | &[i64] | impl Iterator<Item = i64> |
real[] | &[f32] | impl Iterator<Item = f32> |
double precision[] | &[f64] | impl Iterator<Item = f64> |
varchar[] | &arrow::array::StringArray | impl Iterator<Item = &str> |
bytea[] | &arrow::array::BinaryArray | impl Iterator<Item = &[u8]> |
others[] | not supported yet | not supported yet |
struct<..> | not supported yet | user defined struct |