-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(udf): introduce
#[derive(StructType)]
for struct types in Rust…
… UDF (#15372) Signed-off-by: Runji Wang <[email protected]>
- Loading branch information
1 parent
2993ab6
commit 2a03233
Showing
9 changed files
with
349 additions
and
272 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
statement ok | ||
create function int_42() returns int language rust as $$ | ||
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 $$ | ||
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; | ||
|
||
statement ok | ||
create function decimal_add(a decimal, b decimal) returns decimal language rust as $$ | ||
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 $$ | ||
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; | ||
|
||
statement ok | ||
create function jsonb_access(json jsonb, index int) returns jsonb language rust as $$ | ||
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 $$ | ||
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