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

adding UDF example #52

Merged
merged 1 commit into from
Dec 30, 2022
Merged

adding UDF example #52

merged 1 commit into from
Dec 30, 2022

Conversation

edublancas
Copy link

closes #49

Registering SQLite UDFs is possible since version 0.5.1 when we added support for registering existing SQLAlchemy engines; this PR adds an example.

@edublancas edublancas requested a review from idomic December 30, 2022 18:29
Copy link

@idomic idomic left a comment

Choose a reason for hiding this comment

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

.


### Create connection with existing engine

```{versionadded} 0.5.1
Copy link

Choose a reason for hiding this comment

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

This is a good example. Can't we allow the functionality without passing existing engine? It adds complexity and friction to the users.

Copy link
Author

Choose a reason for hiding this comment

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

we cannot. the engine has to be prepared beforehand to register the function. I'm unsure what you're proposing

Copy link

Choose a reason for hiding this comment

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

Can't you do something like that?

from sqlalchemy import create_engine, func

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

# Create an engine and connect to a database
engine = create_engine('postgresql://username:password@localhost:5432/mydatabase')
connection = engine.connect()

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

# Use the UDF in a SELECT statement
query = (
    'SELECT *, {} AS squared_value FROM mytable'.format(square_udf)
)
result = connection.execute(query)

# Print the result set
for row in result:
    print(row)

# Close the connection
connection.close()
In this example, we define a function my_square_function that takes in a value and returns its square. We then use the func module to create a SQL expression for the function, and use it in a SELECT statement.

*Keep in mind that this example uses PostgreSQL as the database backend, and you may need to modify the example slightly to work with a different database.

Copy link
Author

Choose a reason for hiding this comment

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

can you provide an example of how you think the API should be when using %%sql? The snippet you shared is not using it

Copy link

@idomic idomic Dec 30, 2022

Choose a reason for hiding this comment

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

# 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 my_square_function

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

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I see. I think we can open an issue and discuss the API, I agree it's simpler. For now, I'd say let's get this example in the docs so people know they can do it, then we simplify it.

btw, I don't think sqlalchemy.func is what we want, based on this, it looks like it's module to construct SQL from Python, not really for UDFs.

Copy link

Choose a reason for hiding this comment

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

Alright, I'll keep the original issue open.

I think it's func with another thing like select(), the developer should dive into it.

@idomic idomic merged commit 2c987df into master Dec 30, 2022
@idomic idomic deleted the udf branch December 30, 2022 21:50
@idomic idomic changed the title adds example adding UDF example Dec 30, 2022
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.

UDF support
2 participants