Skip to content
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

Add Python UDF for Primitive Types #3390

Merged
merged 8 commits into from
May 6, 2024
Merged

Add Python UDF for Primitive Types #3390

merged 8 commits into from
May 6, 2024

Conversation

mxwli
Copy link
Contributor

@mxwli mxwli commented Apr 26, 2024

Here's how it's to be used, as well as an explanation of some of its features

import kuzu
db = kuzu.Database("db")
conn = kuzu.Connection(db)

def aplusb(a: int, b: int) -> int:
    return a + b

conn.create_function(name="aplusb", udf=aplusb)
# we can use annotations to help bind the function

print(conn.execute("return aplusb(1, 2)").get_next()[0])
# 0

def atimesb(a, b):
    return a*b

conn.create_function(name="atimesb", udf=atimesb, parameters=[kuzu.Type.INT64, kuzu.Type.INT64], return_type=kuzu.Type.INT64)
# we can explicitly state the parameters and the return type

print(conn.execute("return atimesb(5, 10)").get_next()[0])
# 50

def vector3dDistance(x1: float, y1: float, z1: float, x2: float, y2: float, z2: float) -> float:
    return ( (x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2 ) ** 0.5
    
conn.create_function("v3dDist", vector3dDistance)
# unlike with the C++ UDF, we can specify as many parameters as we want for python UDFs

print(conn.execute("return v3dDist(-100, 1, 2, 200, -5, 10)").get_next()[0])
# just over 300

Nested types haven't been implemented yet, but it's been decided to be done.

Copy link
Collaborator

@acquamarin acquamarin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is going to happen if a pyhton function has optional arugments or position arguments? How are we going to use them in kuzu?

tools/python_api/src_cpp/py_udf.cpp Show resolved Hide resolved
tools/python_api/src_cpp/py_udf.cpp Outdated Show resolved Hide resolved
tools/python_api/src_py/connection.py Outdated Show resolved Hide resolved
@manh9203 manh9203 marked this pull request as ready for review April 29, 2024 05:33
@manh9203
Copy link
Contributor

What is going to happen if a python function has optional arguments or position arguments? How are we going to use them in kuzu?

@acquamarin If we allow Python UDF to have optional or position arguments, we cannot match the right function based on user's parameters with our function framework. So we'll require users to provide all parameters for Python UDF by now.

I haven't read much into how Python handles optional arguments, @mxwli could take a look at this case when he gets back to work.

@manh9203 manh9203 force-pushed the python-udf branch 3 times, most recently from 6f17f99 to a117478 Compare May 3, 2024 19:01
@manh9203 manh9203 force-pushed the python-udf branch 2 times, most recently from 57fb1e4 to 9a69c07 Compare May 3, 2024 21:04
@mxwli
Copy link
Contributor Author

mxwli commented May 6, 2024

What is going to happen if a python function has optional arguments or position arguments? How are we going to use them in kuzu?

@acquamarin If we allow Python UDF to have optional or position arguments, we cannot match the right function based on user's parameters with our function framework. So we'll require users to provide all parameters for Python UDF by now.

I haven't read much into how Python handles optional arguments, @mxwli could take a look at this case when he gets back to work.

Optional arguments and keyword arguments don't have a very good equivalent in Kuzu, and I'm not sure if we even have a mechanism to handle binding for all possible cases, so it's not handled at the moment. No error is thrown, it's just that we ignore the "optional" property and consider all parameters "mandatory"

@mxwli mxwli merged commit 6dc0e9f into master May 6, 2024
17 checks passed
@mxwli mxwli deleted the python-udf branch May 6, 2024 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants