Custom WASM user-defined functions

Seafowl uses Wasmtime to support creating user-defined functions (UDFs). You can write a function in any language that can produce WASM bytecode and use it in Seafowl.

Compiling your function to WASM

This is not covered by this guide. Your compiler needs to produce WASM bytecode with your desired function available as an export.

The function must only take and output scalar integer/float types like f32, f64, i32 and i64. Seafowl does not support functions that input/output other types, like memory addresses.

For an example of using Emscripten to compile a C library implementing the Speck block cipher to WASM, see this repository.

Creating the function in Seafowl

With the WASM bytecode ready, you can now create the UDF in Seafowl by encoding it as Base64 and using the CREATE FUNCTION statement. This example is for WASM sin/log/exp functions available here):

CREATE FUNCTION sintau AS '
{
  "entrypoint": "sintau",
  "language": "wasm",
  "input_types": ["f32"],
  "return_type": "f32",
  "data": "AGFzbQEAAAABDQJgAX0BfWADfX9/AX0DBQQAAAABBQQBAUREBxgDBnNpbnRhdQAABGV4cDIAAQRsb2cyAAIKjgEEKQECfUMAAAA/IgIgACAAjpMiACACk4siAZMgAZZBAEEYEAMgAiAAk5gLGQAgACAAjiIAk0EYQSwQA7wgAKhBF3RqvgslAQF/IAC8IgFBF3ZB/wBrsiABQQl0s0MAAIBPlUEsQcQAEAOSCyIBAX0DQCADIACUIAEqAgCSIQMgAUEEaiIBIAJrDQALIAMLC0oBAEEAC0Q/x2FC2eATQUuqKsJzsqY9QAHJQH6V0DZv+V88kPJTPSJndz6sZjE/HQCAP/clMD0D/T++F6bRPkzcNL/Tgrg//IiKNwBqBG5hbWUBHwQABnNpbnRhdQEEZXhwMgIEbG9nMgMIZXZhbHBvbHkCNwQAAwABeAECeDECBGhhbGYBAQABeAICAAF4AQJ4aQMEAAF4AQVzdGFydAIDZW5kAwZyZXN1bHQDCQEDAQAEbG9vcA=="
}';

Calling the function

Finally, you can call the function:

SELECT
  v,
  ROUND(sintau(CAST(v AS REAL)) * 100) AS sintau
FROM
  (VALUES (0.1), (0.2), (0.3), (0.4), (0.5)) d(v)

results in:

+-----+--------+
| v   | sintau |
+-----+--------+
| 0.1 | 59     |
| 0.2 | 95     |
| 0.3 | 95     |
| 0.4 | 59     |
| 0.5 | 0      |
+-----+--------+

Limitations

  • Seafowl UDFs only support scalar integer/float types: f32, f64, i32, i64. Other types, such as strings, are currently unsupported.
  • Only UDFs are supported. User-defined aggregation functions are unsupported.