8 releases

0.3.0 Apr 25, 2024
0.2.2 Apr 2, 2024
0.2.1 Feb 29, 2024
0.1.3 Feb 18, 2024
0.1.2 Jan 31, 2024

#92 in WebAssembly

Download history 277/week @ 2024-01-22 623/week @ 2024-01-29 405/week @ 2024-02-05 272/week @ 2024-02-12 598/week @ 2024-02-19 1176/week @ 2024-02-26 896/week @ 2024-03-04 815/week @ 2024-03-11 934/week @ 2024-03-18 889/week @ 2024-03-25 990/week @ 2024-04-01 930/week @ 2024-04-08 935/week @ 2024-04-15 961/week @ 2024-04-22 382/week @ 2024-04-29 738/week @ 2024-05-06

3,022 downloads per month

Apache-2.0

29KB
415 lines

Rust UDF for Apache Arrow

Crate Docs

Usage

Add the following lines to your Cargo.toml:

[dependencies]
arrow-udf = "0.2"

Define your functions with the #[function] macro:

use arrow_udf::function;

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

The macro will generate a function that takes a RecordBatch as input and returns a RecordBatch as output. The function can be named with the optional output parameter. If not specified, it will be named arbitrarily like gcd_int4_int4_int4_eval.

You can then call the generated function on a RecordBatch:

let input: RecordBatch = ...;
let output: RecordBatch = eval_gcd(&input).unwrap();

If you print the input and output batch, it will be like this:

 input     output
+----+----+-----+
| a  | b  | gcd |
+----+----+-----+
| 15 | 25 | 5   |
|    | 1  |     |
+----+----+-----+

Fallible Functions

If your function returns a Result:

use arrow_udf::function;

#[function("div(int, int) -> int", output = "eval_div")]
fn div(x: i32, y: i32) -> Result<i32, &'static str> {
    x.checked_div(y).ok_or("division by zero")
}

The output batch will contain a column of errors. Error rows will be filled with NULL in the output column, and the error message will be stored in the error column.

 input     output
+----+----+-----+------------------+
| a  | b  | div | error            |
+----+----+-----+------------------+
| 15 | 25 | 0   |                  |
| 5  | 0  |     | division by zero |
+----+----+-----+------------------+

Struct Types

You can define a struct type with the StructType trait:

use arrow_udf::types::StructType;

#[derive(StructType)]
struct Point {
    x: f64,
    y: f64,
}

Then you can use the struct type in function signatures:

use arrow_udf::function;

#[function("point(float8, float8) -> struct Point", output = "eval_point")]
fn point(x: f64, y: f64) -> Point {
    Point { x, y }
}

Currently struct types are only supported as return types.

Function Registry

If you want to lookup functions by signature, you can enable the global_registry feature:

[dependencies]
arrow-udf = { version = "0.2", features = ["global_registry"] }

Each function will be registered in a global registry when it is defined. Then you can lookup functions from the REGISTRY:

use arrow_schema::DataType::Int32;
use arrow_udf::sig::REGISTRY;

let sig = REGISTRY.get("gcd", &[Int32, Int32], &Int32).expect("gcd function");
let output = sig.function.as_scalar().unwrap()(&input).unwrap();

See the example for more details.

Dependencies

~11–18MB
~229K SLoC