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

Issue found on page 'Python Function API' #4058

Open
skinkie opened this issue Nov 13, 2024 · 0 comments
Open

Issue found on page 'Python Function API' #4058

skinkie opened this issue Nov 13, 2024 · 0 comments

Comments

@skinkie
Copy link

skinkie commented Nov 13, 2024

The Python UDF interface is extremely strong. But it does lack one very valuable example for example using functools.partial. I think you want to show the end-user the ability to provide arguments directly from Python, for example, passing a context without using a global variable. The example below shows:

  1. How to provide an actual function and call it within the UDF.
  2. How to provide a predfined variable and use it within the UDF.
  3. Notice that within, SQL can influence the UDF's output.
import functools
import duckdb
import time

def print_udf(func, arg1: str, arg2: str) -> str:
    return ' '.join([str(func()), arg1, arg2])

with duckdb.connect() as con:
    con.create_function('print', functools.partial(print_udf, time.time, 'hello'))
    con.execute("SELECT print('world');")
    print(con.fetchone()[0])

Maybe we need some discussion, therefore I did not create a pull request yet.

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

No branches or pull requests

1 participant