UDF Overview
A UDF is a function defined by a user according to a specific requirement, and is used to extend a function of a database or a data processing system. UDFs can perform various operations, such as data transformation, complex computing, and data aggregation. These operations may exceed the capabilities of built-in functions provided by the system.
UDFs are used to:
- Extended functions: UDFs allow you to extend the functions of the database or data processing system and perform operations that are not supported by built-in functions.
- Complex computation: UDFs can be used to perform complex mathematical or logical computations that may involve multiple steps and conditions.
- Data conversion: UDFs can be used for data cleansing and conversion, such as formatting dates, converting strings, or encoding data.
- Service logic encapsulation: UDFs can encapsulate service logic within functions, making data processing more modular and reusable.
- Performance optimization: In some cases, complex computational logic can be executed on the database server via UDFs to reduce data transfer and enhance performance.
In big data processing, if you need to execute a UDF on a DataFrame to handle complex logic or call third-party libraries, but the container or cluster executing the UDF only has basic Python libraries pre-installed, you can use the pip install package command within the UDF to install the latest version of the dependency, thereby flexibly utilizing various Python libraries. To install a dependency of a specified version, include the version number in the command, for example, pip install pandas==2.2.2.
Notes and Constraints
- UDFs can be programmed only in Python.
- Default values cannot be set for UDF parameters. Additionally, if a function is created with precision specified for its parameters or return value, no precision validation will be performed.
- Function overloading is not supported.
UDF Types
The Aura DataFrame SDK supports the registration and usage of various types of UDFs, including Scalar UDF, Class UDF, Vectorized UDF, UDTF, and UDAF.
| Type | Input: | Output | Description |
|---|---|---|---|
| Scalar UDF | Single row, multiple parameters, for example, (a, b, c) | Single row output, a single scalar value (e.g., int, float, string, date) | This is the most common form, where only one row of data is processed each time the function is called. It can be used for simple calculations, formatting, cleansing, or conversions. For example, string cleansing, mathematical calculations, and date conversion. Analogous to regular functions in Python. |
| Class UDF | Same as Scalar UDF: single row input | Single row output | It is an OOP encapsulation of scalar UDFs and is suitable for scenarios involving internal caching, initialization parameters, or model objects. For example, a model, regular expression, or configuration needs to be loaded once inside the function. The database initializes resources internally only once, avoiding reconstruction with each call. |
| Vectorized UDF | Batch input of rows, with the type of pandas.Series/pyarrow.ChunkedArray | Batch output of an equal number of rows in vectorized structure | The most efficient method for single-row conversions, designed for batch processing. It is suitable for vectorized mathematical computations, batch text processing, embedding handling, NLP tokenization. It has SIMD/batch execution advantages and is commonly used with Arrow and Pandas. |
| UDTF | Single-row input | Multiple row outputs | A single input may produce multiple output rows. It is similar to Python's yield. The function can be used for splitting, expanding, or unpacking structured data. For example, splitting JSON arrays, expanding multimodal data, audio/video slicing, tokenizing, and explosive expansion. |
| UDAF | Multiple row inputs, possibly from different partitions | Single output | Aggregates a group of data: summation, statistics, clustering, embedding aggregation. Features lifecycle methods like accumulate, merge, and finish, supporting distributed execution. |
UDF Registration Methods
There are two primary methods for registering a UDF: explicit registration and implicit registration.
| Registration Method | Description | Dependent on Session Object | Intrusively Add Registration Logic | Applicable Scenarios | Reference |
|---|---|---|---|---|---|
| Explicit registration | Code explicitly specifies the registration information of the UDF. | Yes | Yes | If you wish to precisely control the registration timing, allow intrusive addition of registration logic, or require separation between registration and usage of scalar UDFs under the same backend connection. | |
| Implicit registration | Automatically discovers and registers UDFs during runtime. | No | No | If you prefer non-intrusive registration of scalar UDFs and do not require separation between registration and usage of scalar UDFs under the same backend connection. |
UDF Registration Types
Whether using explicit or implicit registration, the meaning of registration differs depending on the UDF type. Details are provided in the table below.
| UDF Registration Type | Description | Vectorized | Applicable Scenarios & Characteristics | Reference |
|---|---|---|---|---|
| Python | Registers a raw Python function or class into the database. | No | Processes data row by row, suitable for simple or specific calculations, but with lower performance. | |
| Builtin | Obtains the handle of an existing function in the database; no actual registration operation is performed. | No | Directly calls existing functions from the database backend, suitable for using native database functions. | |
| PyArrow | Registers a Python function or class that accepts pyarrow.ChunkedArray as input and output with the database. | Yes | Uses PyArrow's high-performance computing capabilities, perfect for handling large datasets or requiring efficient computations. | |
| Pandas | Registers a Python function or class that accepts pandas.Series as input and output with the database. | Yes | Uses Pandas' vectorized operations, ideal for performing complex data processing at the Python level. |
Scalar UDFs currently support all the preceding four registration types.
For UDAFs and UDTFs, only Python and builtin registration types are supported.
Additional Arguments During UDF Runtime
Regarding runtime parameters for UDFs, both explicit and implicit registrations allow additional arguments to be passed using the with_arguments method. These parameters include concurrency, min_concurrency, max_concurrency, timeout, dpu, and apu. They enable fine-grained resource allocation, concurrency control, and execution time limits for UDFs.
For details, see WITH ARGUMENTS Syntax.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot