WebAssembly UDF for Apache Arrow
For untrusted user-defined functions, you can compile them into WebAssembly and run them in a sandboxed environment.
First, create a project and add the following lines to your Cargo.toml:
[]
= "0.1"
Define your functions with the #[function] macro:
use function;
Then compile the project into WebAssembly:
You can find the generated WebAssembly module in target/wasm32-wasi/release/*.wasm.
Next, add the following lines to your project:
[]
= "0.1"
You can then load the WebAssembly module and call the functions:
use Runtime;
// load the WebAssembly module
let binary = read.unwrap;
// create a runtime from the module
let runtime = new.unwrap;
// list available functions in the module:
for name in runtime.functions
// call the function with a RecordBatch
let input: RecordBatch = ...;
let output = runtime.call.unwrap;
The WebAssembly runtime is powered by wasmtime. Notice that each WebAssembly instance can only run single-threaded, we maintain an instance pool internally to support parallel calls from multiple threads.
See the example for more details. To run the example: