This article provides a step-by-step guide for defining JavaScript functions in RisingWave.
CREATE FUNCTION
statement and then run on the embedded QuickJS virtual machine in RisingWave. It does not support access to external networks and is limited to computational tasks only. Compared to other languages, JavaScript UDFs offer the easiest way to define UDFs in RisingWave.
null
or consistent with the type in the RETURNS
clause.
If the function you defined returns a table, you need to use the yield
statement to return the data of each row. For example:
async
: Set to true
to enable async functions like await fetch()
.batch
: Set to true
to process multiple batches. Make sure each argument of the function accepts an array of arg_type
.create_state() -> state
: Create a new state.accumulate(state, *args) -> state
: Accumulate a new value into the state, returning the updated state.finish(state) -> value
: Get the result of the aggregate function. If not defined, the state is returned as the result.retract(state, *args) -> state
: Retract a value from the state, returning the updated state. If not defined, the state can not be updated incrementally in materialized views and performance may be affected.weighted_avg
to calculate the weighted average.
SQL Type | JavaScript Type | Note |
---|---|---|
boolean | boolean | |
smallint | number | |
int | number | |
bigint | number | |
real | number | |
double precision | number | |
decimal | BigDecimal | BigDecimal is in TC39 proposal stage, implemented by QuickJS |
date | not supported yet | |
time | not supported yet | |
timestamp | not supported yet | |
timestamptz | not supported yet | |
interval | not supported yet | |
varchar | string | |
bytea | Uint8Array | |
jsonb | null, boolean, number, string, array or object | JSON.parse(string) |
smallint[] | Int16Array | |
int[] | Int32Array | |
bigint[] | BigInt64Array | |
real[] | Float32Array | |
double precision[] | Float64Array | |
others[] | array | |
struct<..> | object |