Skip to main content

athena-python-udf

PyPI Changelog License

Athena User Defined Functions(UDFs) in Python made easy!

This library implements the Athena UDF protocol in Python, so you don't have to use Java, and you can use any Python library you wish, including numpy/pandas!

Installation

Install this library using pip:

pip install athena-udf

Usage

  • Install the package
  • Create a lambda handler Python file subclass BaseAthenaUDF
  • Implement the handle_athena_record static method with your required functionality like this:
import athena_udf


class SimpleVarcharUDF(athena_udf.BaseAthenaUDF):

    @staticmethod
    def handle_athena_record(input_schema, output_schema, arguments):
        varchar = arguments[0]
        return varchar.lower()


lambda_handler = SimpleVarcharUDF().lambda_handler

This very basic example takes a varchar input, and returns the lowercase version.

varchar is converted to a python string on the way in and way out.

input_schema contains a PyArrow schema representing the schema of the data being passed

output_schema contains a PyArrow schema representing the schema of what athena expects to be returned.

arguments contains a list of arguments given to the function. Can be more than one with different types.

If you package the above into a zip, with dependencies and name your lambda function my-lambda you can then run it from the athena console like so:

USING EXTERNAL FUNCTION my_udf(col1 varchar) RETURNS varchar LAMBDA 'athena-test'

SELECT my_udf('FooBar');

Which will yield the result foobar

See other examples in the examples folder of this repo.

Important information before using

Each lambda instance will take multiple requests for the same query. Each request can contain multiple rows, athena-udf handles this for you and your implementation will receive a single row.

Athena will group your data into around 1MB chunks in a single request. The maximum your function can return is 6MB per chunk.

This library uses PyArrow. This is a large library, so the Lambdas will be around 50MB zipped.

Timestamps seem to be truncated into Python date objects missing the time.

Functions can return one value only. To return more complex data structures, consider returning a JSON payload and parsing on athena.

Development

To contribute to this library, first checkout the code. Then create a new virtual environment:

poetry install --no-root

Now install the dependencies and test dependencies:

source .venv/bin/activate

To run the tests:

pytest

Metadata

Release files for athena-python-udf 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for athena-python-udf 0.1.0
File Size Uploaded Provenance Actions
athena_python_udf-0.1.0.tar.gz 7.2 kB Without provenance Details for athena_python_udf-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for athena-python-udf 0.1.0
File Interpreter ABI Platform Actions
athena_python_udf-0.1.0-py3-none-any.whl Python 3 none any Details for athena_python_udf-0.1.0-py3-none-any.whl

Total release size: 14.7 kB

Release files / athena_python_udf-0.1.0.tar.gz

Download URL athena_python_udf-0.1.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7c926c66684f0c6db22c0da34c366deb1988316d31cda34d5955cad1cc5266e1
BLAKE2b-256 checksum
How to use checksums
fc11bb1015dfcda51e2e1a68caef8803861c83a00491f2c80f64e123798c71eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.1 Linux/6.5.0-1016-azure

Release files / athena_python_udf-0.1.0-py3-none-any.whl

Download URL athena_python_udf-0.1.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
41f682a49db7b6f890857845f6e5b2a8f9b612d5ab1b1dcb698e7df6bf65740c
BLAKE2b-256 checksum
How to use checksums
295222734eb5dfbc380570d167c9c334621b11e97c3c64a4e94dd6cf272b0258
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.1 Linux/6.5.0-1016-azure

Security for athena-python-udf 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page