Skip to main content

Use UDFs in JavaScript

This article provides a step-by-step guide for defining JavaScript functions in RisingWave.

JavaScript code is inlined in 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.

Define your functions

You can use the CREATE FUNCTION command to create JavaScript UDFs. See the syntax as follows:

CREATE FUNCTION function_name ( arg_name arg_type [, ...] )
[ RETURNS return_type | RETURNS TABLE ( column_name column_type [, ...] ) ]
LANGUAGE javascript
AS [ $$ function_body $$ | 'function_body' ];

The argument names you defined can be used in the function body. For example:

CREATE FUNCTION gcd(a int, b int) RETURNS int LANGUAGE javascript AS $$
if(a == null || b == null) {
return null;
}
while (b != 0) {
let t = b;
b = a % b;
a = t;
}
return a;
$$;

See the correspondence between SQL types and JavaScript types in the Data type mapping. You need to ensure that the type of the return value is either 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:

CREATE FUNCTION series(n int) RETURNS TABLE (x int) LANGUAGE javascript AS $$
for(let i = 0; i < n; i++) {
yield i;
}
$$;

Use your functions

Once the UDFs are created in RisingWave, you can use them in SQL queries just like any built-in functions. For example:

SELECT gcd(25, 15);
SELECT * from series(5);

Data type mapping

The following table shows the data type mapping between SQL and JavaScript:

SQL TypeJavaScript TypeNote
booleanboolean
smallintnumber
intnumber
bigintnumber
realnumber
double precisionnumber
decimalBigDecimalBigDecimal is in TC39 proposal stage, implemented by QuickJS
datenot supported yet
timenot supported yet
timestampnot supported yet
timestamptznot supported yet
intervalnot supported yet
varcharstring
byteaUint8Array
jsonbnull, boolean, number, string, array or objectJSON.parse(string)
smallint[]Int16Array
int[]Int32Array
bigint[]BigInt64Array
real[]Float32Array
double precision[]Float64Array
others[]array
struct<..>object

Help us make this doc better!

Was this page helpful?

Happy React is loading...