Skip to content

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

Merged
andygrove merged 3 commits into
apache:mainfrom
simicd:docs/sql-python-udf
Mar 6, 2023
Merged

docs: Example of calling Python UDF & UDAF in SQL#258
andygrove merged 3 commits into
apache:mainfrom
simicd:docs/sql-python-udf

Conversation

@simicd
Copy link
Copy Markdown
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

Comment thread datafusion/__init__.py
)
if name is None:
name = accum.__qualname__
name = accum.__qualname__.lower()
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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