-
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): minimal Python UDF SDK (#7943)
This PR designs a minimal SDK for Python UDFs. Now you can define a function in Python like this: ```python from risingwave.udf import udf, UdfServer @udf(input_types=['INT', 'INT'], result_type='INT') def gcd(x: int, y: int) -> int: while y != 0: (x, y) = (y, x % y) return x if __name__ == '__main__': server = UdfServer() server.add_function(gcd) server.serve() ``` This PR also fixes the problem when functions have no input arguments. Approved-By: xxchan Approved-By: BugenZhao
- Loading branch information
1 parent
f11bb62
commit 7a0316b
Showing
21 changed files
with
386 additions
and
148 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -23,6 +23,9 @@ cmake-build-debug/ | |
*.app | ||
build/ | ||
|
||
# Python | ||
*.pyc | ||
|
||
# Golang | ||
go/bin/ | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Before running this test: | ||
# python3 e2e_test/udf/test.py | ||
|
||
# TODO: check the service on creation | ||
# Currently whether the function exists in backend and whether the signature matches is checked on execution. Create function will always succeed. | ||
|
||
# Create a function. | ||
statement ok | ||
create function int_42() returns int as 'http://localhost:8815' language arrow_flight; | ||
|
||
statement ok | ||
create function gcd(int, int) returns int as 'http://localhost:8815' language arrow_flight; | ||
|
||
# Create a function with the same name but different arguments. | ||
statement ok | ||
create function gcd(int, int, int) returns int as 'http://localhost:8815' language arrow_flight; | ||
|
||
# Create a function with the same name and arguments. | ||
statement error exists | ||
create function gcd(int, int) returns int as 'http://localhost:8815' language arrow_flight; | ||
|
||
query I | ||
select int_42(); | ||
---- | ||
42 | ||
|
||
query I | ||
select gcd(25, 15); | ||
---- | ||
5 | ||
|
||
query I | ||
select gcd(25, 15, 3); | ||
---- | ||
1 | ||
|
||
# TODO: drop function without arguments | ||
|
||
# # Drop a function but ambiguous. | ||
# statement error is not unique | ||
# drop function gcd; | ||
|
||
# Drop a function | ||
statement ok | ||
drop function gcd(int, int); | ||
|
||
# Drop a function | ||
statement ok | ||
drop function gcd(int, int, int); |
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,29 @@ | ||
import sys | ||
sys.path.append('src/udf/python') # noqa | ||
|
||
from risingwave.udf import udf, UdfServer | ||
|
||
|
||
@udf(input_types=[], result_type='INT') | ||
def int_42() -> int: | ||
return 42 | ||
|
||
|
||
@udf(input_types=['INT', 'INT'], result_type='INT') | ||
def gcd(x: int, y: int) -> int: | ||
while y != 0: | ||
(x, y) = (y, x % y) | ||
return x | ||
|
||
|
||
@udf(name='gcd', input_types=['INT', 'INT', 'INT'], result_type='INT') | ||
def gcd3(x: int, y: int, z: int) -> int: | ||
return gcd(gcd(x, y), z) | ||
|
||
|
||
if __name__ == '__main__': | ||
server = UdfServer() | ||
server.add_function(int_42) | ||
server.add_function(gcd) | ||
server.add_function(gcd3) | ||
server.serve() |
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.
7a0316b
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.
Wow, this is super cool!