7 releases

0.2.2 Apr 25, 2024
0.2.1 Apr 7, 2024
0.2.0 Feb 29, 2024
0.1.3 Feb 4, 2024
0.1.1 Jan 31, 2024

#385 in WebAssembly

Download history 3738/week @ 2024-01-22 7471/week @ 2024-01-29 3592/week @ 2024-02-05 1140/week @ 2024-02-12 3216/week @ 2024-02-19 5240/week @ 2024-02-26 4709/week @ 2024-03-04 3945/week @ 2024-03-11 4669/week @ 2024-03-18 4567/week @ 2024-03-25 4395/week @ 2024-04-01 4237/week @ 2024-04-08 4463/week @ 2024-04-15 3983/week @ 2024-04-22 1610/week @ 2024-04-29 3564/week @ 2024-05-06

13,640 downloads per month

Apache-2.0

32KB
564 lines

WebAssembly UDF for Apache Arrow

Crate Docs

For untrusted user-defined functions, you can compile them into WebAssembly and run them in a sandboxed environment.

Build UDF in WebAssembly

Create a project and add the following lines to your Cargo.toml:

[dependencies]
arrow-udf = "0.1"

Define your functions with the #[function] macro:

use arrow_udf::function;

#[function("gcd(int, int) -> int")]
fn gcd(mut a: i32, mut b: i32) -> i32 {
    while b != 0 {
        (a, b) = (b, a % b);
    }
    a
}

Then compile the project into WebAssembly:

cargo build --release --target wasm32-wasi

You can find the generated WebAssembly module in target/wasm32-wasi/release/*.wasm.

Run UDF in WebAssembly

Add the following lines to your Cargo.toml:

[dependencies]
arrow-udf-wasm = "0.1"

You can then load the WebAssembly module and call the functions:

use arrow_udf_wasm::Runtime;

// load the WebAssembly module
let binary = std::fs::read("udf.wasm").unwrap();
// create a runtime from the module
let runtime = Runtime::new(&binary).unwrap();
// list available functions in the module:
for name in runtime.functions() {
    println!("{}", name);
}
// call the function with a RecordBatch
let input: RecordBatch = ...;
let output = runtime.call("gcd(int4,int4)->int4", &input).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:

cargo build --release -p arrow-udf-example --target wasm32-wasi
cargo run --example wasm -- target/wasm32-wasi/release/arrow_udf_example.wasm

Build WASM UDF at Runtime

Enable the build feature to build the wasm binary from source:

[dependencies]
arrow-udf-wasm = { version = "0.1", features = ["build"] }

You can then build the WebAssembly module at runtime:

let manifest = r#"
[dependencies]
chrono = "0.4"
"#;

let script = r#"
use arrow_udf::function;

#[function("gcd(int, int) -> int")]
fn gcd(mut a: i32, mut b: i32) -> i32 {
    while b != 0 {
        (a, b) = (b, a % b);
    }
    a
}
"#;
let binary = arrow_udf_wasm::build::build(manifest, script).unwrap();

See the build module for more details.

Dependencies

~22–34MB
~552K SLoC