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

UDF support #49

Closed
idomic opened this issue Dec 29, 2022 · 9 comments · Fixed by #52
Closed

UDF support #49

idomic opened this issue Dec 29, 2022 · 9 comments · Fixed by #52

Comments

@idomic
Copy link

idomic commented Dec 29, 2022

Currently the package doesn't support it.
We should add a feature to accept it and execute it via the engine.

@idomic
Copy link
Author

idomic commented Dec 29, 2022

cc: @jonnickerson
If you have a specific UDF you've tried please tag it here!

@edublancas
Copy link

This is already possible since 0.5.1! I opened a PR with an example

@idomic
Copy link
Author

idomic commented Dec 30, 2022

Reopening this as an easier solution can be made here

@idomic idomic reopened this Dec 30, 2022
@edublancas
Copy link

I think this should be a separate issue with more details (the proposed API)

@idomic
Copy link
Author

idomic commented Jan 2, 2023

Proposed API:

# Load the extension and connect to the database
%load_ext sql
%sql postgresql://username:password@localhost:5432/mydatabase

# Define a function that takes in a value and returns its square
def my_square_function(x):
    return x * x

# Use the func module to define the UDF as a SQL expression
from sqlalchemy import func
square_udf = func.my_square_function(column)

# Use the UDF in a SELECT statement
result = %sql SELECT *, :square_udf AS squared_value FROM mytable

# Print the result set
print(result)

We might want to even create a tighter integration to the func part, maybe something like:

# Define the UDF as a SQL expression
%sql --udf square_udf=my_square_function

# And then consume it
%sql SELECT *, :square_udf AS squared_value FROM mytable

In the second snippet we take the extra step to wrap func.user_function for the user, when given the --udf flag. This allows a complete abstraction of sqlalchemy like it should be.

@edublancas
Copy link

this is highly db dependent, I think we should close it

@idomic
Copy link
Author

idomic commented Apr 6, 2023

The question I have is how many users actually have UDFs, I know it's pretty common with DWHs, and is supported by most if not all of them (At least snowflake, bigquery, redshift).

@edublancas
Copy link

ok, then let's start with a single one, the one that has the most users.

@edublancas
Copy link

each db has its own mechanism for defining UDFs so it'll take a lot of effort to unify them in a single API

@edublancas edublancas closed this as not planned Won't fix, can't repro, duplicate, stale May 23, 2023
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 a pull request may close this issue.

2 participants