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

docs: Example of calling Python UDF & UDAF in SQL #258

Merged
merged 3 commits into from
Mar 6, 2023

Conversation

simicd
Copy link
Contributor

@simicd simicd commented Mar 4, 2023

Which issue does this PR close?

Closes #138

Rationale for this change

Document how to utilize Python UDFs & UDAFs in SQL

What changes are included in this PR?

Document examples & link in README.md

Are there any user-facing changes?

Additional examples/documentation

@@ -190,7 +190,7 @@ def udaf(accum, input_type, return_type, state_type, volatility, name=None):
"`accum` must implement the abstract base class Accumulator"
)
if name is None:
name = accum.__qualname__
name = accum.__qualname__.lower()
Copy link
Contributor Author

@simicd simicd Mar 4, 2023

Choose a reason for hiding this comment

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

Previously calling the UDAF in SQL would fail if name is not specified (i.e. both MyAccumulator(b) and myaccumulator(b) would fail when running e.g. result_df = ctx.sql(f"select a, myaccumulator(b) as b_aggregated from {table_name}") ). With this change both the uppercase & lowercase variant will work.

Comment on lines +78 to +82
# Query the DataFrame using SQL
table_name = ctx.catalog().database().names().pop()
result_df = ctx.sql(
f"select a, my_accumulator(b) as b_aggregated from {table_name} group by a order by a"
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here it would be nice if the table name can be set in the from... context functions so table_name is known, have created ticket #259 as follow-up task

Copy link
Member

@andygrove andygrove left a comment

Choose a reason for hiding this comment

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

Thank you @simicd 🙏

@andygrove andygrove merged commit a3c108f into apache:main Mar 6, 2023
@simicd simicd deleted the docs/sql-python-udf branch March 8, 2023 22:42
@andygrove andygrove added the documentation Improvements or additions to documentation label Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add example that calls a Python UDF from SQL
2 participants