-
Notifications
You must be signed in to change notification settings - Fork 591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(udf): introduce #[derive(StructType)]
for struct types in Rust UDF
#15372
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1e32bac
support arrow-udf v0.2
wangrunji0408 04a892c
use a fixed tempdir to reuse the build cache
wangrunji0408 0548a91
add `rust_udf.slt`
wangrunji0408 76146b3
switch to arrow-udf-wasm v0.2
wangrunji0408 536a675
revert breaking change on function body and fix `rust_udf.slt`
wangrunji0408 b1fe696
Merge remote-tracking branch 'origin/main' into wrj/rust-udf-0.2
wangrunji0408 bc94656
add comments on identifiers v1 and v2
wangrunji0408 0729ca5
Merge remote-tracking branch 'origin/main' into wrj/rust-udf-0.2
wangrunji0408 869421b
Merge remote-tracking branch 'origin/main' into wrj/rust-udf-0.2
wangrunji0408 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
statement ok | ||
create function int_42() returns int language rust as $$ | ||
#[function("int_42() -> int")] | ||
fn int_42() -> i32 { | ||
42 | ||
} | ||
$$; | ||
|
||
query I | ||
select int_42(); | ||
---- | ||
42 | ||
|
||
statement ok | ||
drop function int_42; | ||
|
||
|
||
statement ok | ||
create function gcd(int, int) returns int language rust as $$ | ||
#[function("gcd(int, int) -> int")] | ||
fn gcd(mut a: i32, mut b: i32) -> i32 { | ||
while b != 0 { | ||
(a, b) = (b, a % b); | ||
} | ||
a | ||
} | ||
$$; | ||
|
||
query I | ||
select gcd(25, 15); | ||
---- | ||
5 | ||
|
||
statement ok | ||
drop function gcd; | ||
|
||
|
||
create function decimal_add(a decimal, b decimal) returns decimal language rust as $$ | ||
#[function("decimal_add(decimal, decimal) -> decimal")] | ||
fn decimal_add(a: Decimal, b: Decimal) -> Decimal { | ||
a + b | ||
} | ||
$$; | ||
|
||
query R | ||
select decimal_add(1.11, 2.22); | ||
---- | ||
3.33 | ||
|
||
statement ok | ||
drop function decimal_add; | ||
|
||
statement ok | ||
create function datetime(d date, t time) returns timestamp language rust as $$ | ||
#[function("datetime(date, time) -> timestamp")] | ||
fn datetime(date: NaiveDate, time: NaiveTime) -> NaiveDateTime { | ||
NaiveDateTime::new(date, time) | ||
} | ||
$$; | ||
|
||
query T | ||
select datetime('2020-01-01', '12:34:56'); | ||
---- | ||
2020-01-01 12:34:56 | ||
|
||
statement ok | ||
drop function datetime; | ||
|
||
|
||
create function jsonb_access(json jsonb, index int) returns jsonb language rust as $$ | ||
#[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() | ||
} | ||
$$; | ||
|
||
query T | ||
select jsonb_access(a::jsonb, 1) from | ||
(values ('["a", "b", "c"]'), (null), ('[0, false]')) t(a); | ||
---- | ||
"b" | ||
NULL | ||
false | ||
|
||
statement ok | ||
drop function jsonb_access; | ||
|
||
|
||
statement ok | ||
create function key_value(varchar) returns struct<key varchar, value varchar> language rust as $$ | ||
#[derive(StructType)] | ||
struct KeyValue<'a> { | ||
key: &'a str, | ||
value: &'a str, | ||
} | ||
|
||
#[function("key_value(varchar) -> struct KeyValue")] | ||
fn key_value(kv: &str) -> Option<KeyValue<'_>> { | ||
let (key, value) = kv.split_once('=')?; | ||
Some(KeyValue { key, value }) | ||
} | ||
$$; | ||
|
||
query T | ||
select key_value('a=1'); | ||
---- | ||
(a,1) | ||
|
||
statement ok | ||
drop function key_value; | ||
|
||
|
||
statement ok | ||
create function series(n int) returns table (x int) language rust as $$ | ||
#[function("series(int) -> setof int")] | ||
fn series(n: i32) -> impl Iterator<Item = i32> { | ||
(0..n).into_iter() | ||
} | ||
$$; | ||
|
||
query I | ||
select series(3); | ||
---- | ||
0 | ||
1 | ||
2 | ||
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about checks whether user uses
#[function
in the script, and decide whether to added this for them? 🤡 In this way, we can keep the simple old syntax.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds a little hacky. But I like this idea. 🤡
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final rule:
#[function]
by themselves.