kuzudb / kuzu

Embeddable property graph database management system built for query speed and scalability. Implements Cypher.
https://kuzudb.com/
MIT License
1.07k stars 77 forks source link

Add Python UDF for Primitive Types #3390

Closed mxwli closed 1 week ago

mxwli commented 2 weeks ago

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.

manh9203 commented 2 weeks ago

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.

mxwli commented 1 week ago

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"