This article provides a step-by-step guide for defining Rust functions in RisingWave.
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.
wasm32-wasip1
target is installed:udf
:
Cargo.toml
:
src/lib.rs
, define your functions using the function
macro:
target/wasm32-wasip1/release/udf.wasm
.
Optional: It is recommended to strip the binary to reduce its size:
fs://
is URI schema, and /path/to/udf.wasm
is the real path.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 |