Creating a UDF
This section describes how to register a UDF (which processes input columns row by row and returns a single value) with LakeFormation. The registration process is as follows: Type inference -> Packaging -> Uploading to OBS -> Writing metadata to LakeFormation. If the registration in LakeFormation fails, the uploaded OBS package will be automatically rolled back.
Prerequisites
- The SDK has been installed. For details, see Obtaining and Installing the Catalog SDK.
- The UDF callable object (function, function class, or class instance) has been prepared and annotated with type annotations.
Function Signature
client.create_udf(
udf, /, *,
database=None,
name,
signature=None,
imports=None,
runtime_env=None,
description=None,
pretty_name=None,
is_strict=True,
volatility="VOLATILE",
is_batch=False,
batch_size=None,
cpu_arch=None,
cpus=None,
memory=None,
device_type=None,
devices=None,
) -> Function Parameter Description
| Parameter | Mandatory (Yes/No) | Type | Default Value | Description |
|---|---|---|---|---|
| udf | Yes | Any (location parameter) | - | UDF callable object (function or callable class). |
| database | No | str | None | default_database | Name of the target database. If this parameter is set to None, the default database on the client is used. |
| name | Yes | str | - | Function name. The value must start with a letter and contain 3 to 128 characters, including only letters, digits, and underscores (_). |
| signature | No | Signature | None | None | Explicit function signature. If this parameter is set to None, it is automatically inferred from UDF annotations. For more information about the signature, see signature. |
| imports | No | str | Iterable[str] | None | None | List of file/directory paths or URLs to be included in the UDF package. |
| runtime_env | No | str | Mapping | None | None | Runtime environment configuration (such as python_version and PyPI dependencies). The value can be a JSON string or dictionary. For more information about runtime_env, see runtime_env. |
| description | No | str | None | None | Description of the function. |
| pretty_name | No | str | None | None | Display name. If the parameter is set to None, the value of name is used. |
| is_strict | No | bool | True | Whether the function is strict (whether NULL is returned when the input is NULL). |
| volatility | No | str | VOLATILE | Function volatility. The options are IMMUTABLE, STABLE, and VOLATILE. |
| is_batch | No | bool | False | Whether to enable the batch processing mode. |
| batch_size | No | int | None | None | Size of each batch. This parameter is recommended when is_batch is set to True. The value must be a positive integer. |
| cpu_arch | No | str | None | None | CPU architecture. The value can be arm or x86. |
| cpus | No | float | None | None | Number of CPU cores. The minimum value is 0.33. |
| memory | No | int | None | None | Memory size, in MB. The minimum value is 1. |
| device_type | No | str | None | None | Device type, for example, NPU or GPU. |
| devices | No | float | None | None | Number of devices. The value must be a positive number. |
Return Value
Returns a Function object containing the version_id assigned by LakeFormation, resource_uris (OBS package addresses), and parameters (metadata such as the signature and runtime environment).
Sample Code
- If no signature is provided, the SDK infers the signature from the UDF's type annotations using infer_signature. For class-based UDFs, the __call__ method is used.
- For class-based UDFs, the constructor definition (ConstructorDef) is extracted and the class name is recorded to help the Aura engine locate the class.
- For class instance UDFs, the instance is serialized via cloudpickle, and the class name is derived from the instance type.
# 1. Define a UDF with type annotations.
def to_celsius(fahrenheit: float) -> float:
return (fahrenheit - 32.0) * 5.0 / 9.0
# 2. Register with LakeFormation.
func = client.create_udf(
udf=to_celsius,
name="to_celsius",
description="Convert Fahrenheit to Celsius",
runtime_env={"python_version": "3.11", "pypi": []},
)
print(f"Registered: {func.function_name}, version={func.version_id}") 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