Updated on 2026-11-06 GMT+08:00

Creating a UDAF

This section describes how to register a UDAF with LakeFormation. A UDAF can perform aggregation calculations on multiple rows of data. Except that udf_type is fixed as udaf, its parameters are the same as those of create_udf.

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_udaf(
    udaf, /, *,
    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

udaf

Yes

Any (location parameter)

-

UDAF 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.

Sample Code

class WeightedSum:
    """UDAF: weighted sum (sum(value * weight)).

    accumulate accumulates the intermediate state (sum_vw) row by row; merge merges partitions;
    finish returns the final aggregation result.
    """

    def __init__(self):
        self._agg_state = 0.0

    @property
    def aggregate_state(self) -> float:
        return self._agg_state
    def accumulate(self, value: int, weight: int) -> None:
        """Accumulate row by row: sum of value * weight"""
        if value is not None and weight is not None:
            self._agg_state += value * weight
    def merge(self, other_state: float) -> None:
        """Merge the intermediate states of other partitions"""
        self._agg_state += other_state
    def finish(self) -> float:
        """Return the final aggregation result"""
        return self._agg_state

func = client.create_agg_function(
    WeightedSum,
    database=database,
    name="weighted_sum_udaf",
    signature=sig,
    comment="UDAF: Weighted sum (sum(value * weight))",
)