-
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 JavaScript UDF (#14513)
Signed-off-by: Runji Wang <[email protected]> Co-authored-by: wangrunji0408 <[email protected]>
- Loading branch information
1 parent
3b8c942
commit 705be19
Showing
21 changed files
with
538 additions
and
158 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
statement ok | ||
create function int_42() returns int language javascript as $$ | ||
return 42; | ||
$$; | ||
|
||
query I | ||
select int_42(); | ||
---- | ||
42 | ||
|
||
statement ok | ||
drop function int_42; | ||
|
||
|
||
statement ok | ||
create function gcd(a int, b int) returns int language javascript as $$ | ||
// required before we support `RETURNS NULL ON NULL INPUT` | ||
if(a == null || b == null) { | ||
return null; | ||
} | ||
while (b != 0) { | ||
let t = b; | ||
b = a % b; | ||
a = t; | ||
} | ||
return 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 javascript as $$ | ||
return a + b; | ||
$$; | ||
|
||
query R | ||
select decimal_add(1.11, 2.22); | ||
---- | ||
3.33 | ||
|
||
statement ok | ||
drop function decimal_add; | ||
|
||
|
||
statement ok | ||
create function to_string(a boolean, b smallint, c int, d bigint, e real, f float, g decimal, h varchar, i bytea, j jsonb) returns varchar language javascript as $$ | ||
return a.toString() + b.toString() + c.toString() + d.toString() + e.toString() + f.toString() + g.toString() + h.toString() + i.toString() + JSON.stringify(j); | ||
$$; | ||
|
||
query T | ||
select to_string(false, 1::smallint, 2, 3, 4.5, 6.7, 8.9, 'abc', '\x010203', '{"key": 1}'); | ||
---- | ||
false1234.56.78.9abc1,2,3{"key":1} | ||
|
||
statement ok | ||
drop function to_string; | ||
|
||
|
||
# show data types in javascript | ||
statement ok | ||
create function js_typeof(a boolean, b smallint, c int, d bigint, e real, f float, g decimal, h varchar, i bytea, j jsonb) returns jsonb language javascript as $$ | ||
return { | ||
boolean: typeof a, | ||
smallint: typeof b, | ||
int: typeof c, | ||
bigint: typeof d, | ||
real: typeof e, | ||
float: typeof f, | ||
decimal: typeof g, | ||
varchar: typeof h, | ||
bytea: typeof i, | ||
jsonb: typeof j, | ||
}; | ||
$$; | ||
|
||
query T | ||
select js_typeof(false, 1::smallint, 2, 3, 4.5, 6.7, 8.9, 'abc', '\x010203', '{"key": 1}'); | ||
---- | ||
{"bigint": "number", "boolean": "boolean", "bytea": "object", "decimal": "bigdecimal", "float": "number", "int": "number", "jsonb": "object", "real": "number", "smallint": "number", "varchar": "string"} | ||
|
||
statement ok | ||
drop function js_typeof; | ||
|
||
|
||
statement ok | ||
create function return_all(a boolean, b smallint, c int, d bigint, e real, f float, g decimal, h varchar, i bytea, j jsonb, s struct<f1 int, f2 int>) | ||
returns struct<a boolean, b smallint, c int, d bigint, e real, f float, g decimal, h varchar, i bytea, j jsonb, s struct<f1 int, f2 int>> | ||
language javascript as $$ | ||
return {a,b,c,d,e,f,g,h,i,j,s}; | ||
$$; | ||
|
||
query T | ||
select (return_all( | ||
true, | ||
1 ::smallint, | ||
1, | ||
1, | ||
1, | ||
1, | ||
12345678901234567890.12345678, | ||
'string', | ||
'bytes', | ||
'{"key":1}', | ||
row(1, 2)::struct<f1 int, f2 int> | ||
)).*; | ||
---- | ||
t 1 1 1 1 1 12345678901234567890.12345678 string \x6279746573 {"key": 1} (1,2) | ||
|
||
statement ok | ||
drop function return_all; | ||
|
||
|
||
statement ok | ||
create function series(n int) returns table (x int) language javascript as $$ | ||
for(let i = 0; i < n; i++) { | ||
yield i; | ||
} | ||
$$; | ||
|
||
query I | ||
select series(5); | ||
---- | ||
0 | ||
1 | ||
2 | ||
3 | ||
4 | ||
|
||
statement ok | ||
drop function series; | ||
|
||
|
||
statement ok | ||
create function split(s varchar) returns table (word varchar, length int) language javascript as $$ | ||
for(let word of s.split(' ')) { | ||
yield { word: word, length: word.length }; | ||
} | ||
$$; | ||
|
||
query IT | ||
select * from split('rising wave'); | ||
---- | ||
rising 6 | ||
wave 4 | ||
|
||
statement ok | ||
drop function split; |
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.