-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(udf): add initial support for WASM-based Rust UDF (#14271)
Signed-off-by: Runji Wang <[email protected]> Co-authored-by: xxchan <[email protected]> Co-authored-by: wangrunji0408 <[email protected]>
- Loading branch information
1 parent
89a8297
commit e8f1eb9
Showing
34 changed files
with
1,864 additions
and
131 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[build] | ||
target = "wasm32-wasi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Cargo.lock | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "udf" | ||
version = "0.1.0" | ||
edition = "2021" | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
arrow-udf = { git = "https://github.com/risingwavelabs/arrow-udf.git", rev = "f9a9e0d" } | ||
genawaiter = "0.99" | ||
rust_decimal = "1" | ||
serde_json = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use arrow_udf::function; | ||
use rust_decimal::Decimal; | ||
|
||
#[function("int_42() -> int")] | ||
fn int_42() -> i32 { | ||
42 | ||
} | ||
|
||
#[function("gcd(int, int) -> int")] | ||
fn gcd(mut a: i32, mut b: i32) -> i32 { | ||
while b != 0 { | ||
let t = b; | ||
b = a % b; | ||
a = t; | ||
} | ||
a | ||
} | ||
|
||
#[function("gcd(int, int, int) -> int")] | ||
fn gcd3(a: i32, b: i32, c: i32) -> i32 { | ||
gcd(gcd(a, b), c) | ||
} | ||
|
||
#[function("sleep(int) -> int")] | ||
fn sleep(second: i32) -> i32 { | ||
std::thread::sleep(std::time::Duration::from_secs(second as u64)); | ||
0 | ||
} | ||
|
||
#[function("segfault() -> int")] | ||
fn segfault() -> i32 { | ||
unsafe { (usize::MAX as *const i32).read_volatile() } | ||
} | ||
|
||
#[function("oom() -> int")] | ||
fn oom() -> i32 { | ||
_ = vec![0u8; usize::MAX]; | ||
0 | ||
} | ||
|
||
#[function("create_file() -> int")] | ||
fn create_file() -> i32 { | ||
std::fs::File::create("test").unwrap(); | ||
0 | ||
} | ||
|
||
#[function("length(varchar) -> int")] | ||
#[function("length(bytea) -> int")] | ||
fn length(s: impl AsRef<[u8]>) -> i32 { | ||
s.as_ref().len() as i32 | ||
} | ||
|
||
#[function("extract_tcp_info(bytea) -> struct<src_addr:varchar,dst_addr:varchar,src_port:smallint,dst_port:smallint>")] | ||
fn extract_tcp_info(tcp_packet: &[u8]) -> (String, String, i16, i16) { | ||
let src_addr = std::net::Ipv4Addr::from(<[u8; 4]>::try_from(&tcp_packet[12..16]).unwrap()); | ||
let dst_addr = std::net::Ipv4Addr::from(<[u8; 4]>::try_from(&tcp_packet[16..20]).unwrap()); | ||
let src_port = u16::from_be_bytes(<[u8; 2]>::try_from(&tcp_packet[20..22]).unwrap()); | ||
let dst_port = u16::from_be_bytes(<[u8; 2]>::try_from(&tcp_packet[22..24]).unwrap()); | ||
( | ||
src_addr.to_string(), | ||
dst_addr.to_string(), | ||
src_port as i16, | ||
dst_port as i16, | ||
) | ||
} | ||
|
||
#[function("decimal_add(decimal, decimal) -> decimal")] | ||
fn decimal_add(a: Decimal, b: Decimal) -> Decimal { | ||
a + b | ||
} | ||
|
||
#[function("jsonb_access(json, int) -> json")] | ||
fn jsonb_access(json: serde_json::Value, index: i32) -> Option<serde_json::Value> { | ||
json.get(index as usize).cloned() | ||
} | ||
|
||
#[function("series(int) -> setof int")] | ||
fn series(n: i32) -> impl Iterator<Item = i32> { | ||
0..n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Before running this test: | ||
# cd e2e_test/udf/wasm && cargo build --release | ||
|
||
statement ok | ||
create function int_42() returns int | ||
language wasm using link 'fs://e2e_test/udf/wasm/target/wasm32-wasi/release/udf.wasm'; | ||
|
||
statement ok | ||
create function gcd(int, int) returns int | ||
language wasm using link 'fs://e2e_test/udf/wasm/target/wasm32-wasi/release/udf.wasm'; | ||
|
||
statement ok | ||
create function gcd(int, int, int) returns int | ||
language wasm using link 'fs://e2e_test/udf/wasm/target/wasm32-wasi/release/udf.wasm'; | ||
|
||
statement ok | ||
create function extract_tcp_info(bytea) returns struct<src_addr varchar, dst_addr varchar, src_port smallint, dst_port smallint> | ||
language wasm using link 'fs://e2e_test/udf/wasm/target/wasm32-wasi/release/udf.wasm'; | ||
|
||
statement ok | ||
create function decimal_add(decimal, decimal) returns decimal | ||
language wasm using link 'fs://e2e_test/udf/wasm/target/wasm32-wasi/release/udf.wasm'; | ||
|
||
statement ok | ||
create function jsonb_access(jsonb, int) returns jsonb | ||
language wasm using link 'fs://e2e_test/udf/wasm/target/wasm32-wasi/release/udf.wasm'; | ||
|
||
statement ok | ||
create function series(int) returns table (x int) | ||
language wasm using link 'fs://e2e_test/udf/wasm/target/wasm32-wasi/release/udf.wasm'; | ||
|
||
query I | ||
select int_42(); | ||
---- | ||
42 | ||
|
||
query I | ||
select gcd(25, 15); | ||
---- | ||
5 | ||
|
||
query I | ||
select gcd(25, 15, 3); | ||
---- | ||
1 | ||
|
||
query T | ||
select extract_tcp_info(E'\\x45000034a8a8400040065b8ac0a8000ec0a80001035d20b6d971b900000000080020200493310000020405b4' :: bytea); | ||
---- | ||
(192.168.0.14,192.168.0.1,861,8374) | ||
|
||
query R | ||
select decimal_add(1.11, 2.22); | ||
---- | ||
3.33 | ||
|
||
query T | ||
select jsonb_access(a::jsonb, 1) from | ||
(values ('["a", "b", "c"]'), (null), ('[0, false]')) t(a); | ||
---- | ||
"b" | ||
NULL | ||
false | ||
|
||
query I | ||
select series(5); | ||
---- | ||
0 | ||
1 | ||
2 | ||
3 | ||
4 | ||
|
||
statement ok | ||
drop function int_42; | ||
|
||
statement ok | ||
drop function gcd(int,int); | ||
|
||
statement ok | ||
drop function gcd(int,int,int); | ||
|
||
statement ok | ||
drop function extract_tcp_info; | ||
|
||
statement ok | ||
drop function decimal_add; | ||
|
||
statement ok | ||
drop function jsonb_access; | ||
|
||
statement ok | ||
drop function series; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.