signature
The signature describes the function signature of a UDF, including the type information of the input parameters and return values. It consists of a parameter list and a return_annotation. During function registration, the signature is written into the LakeFormation metadata for the engine to match parameters and types during invocation.
signature parameters
A signature object is composed of a parameter list and a return_annotation.
| Property | Type | Description |
|---|---|---|
| name | str | Parameter name. |
| kind | ParameterKind | Parameter type. The value can be POSITIONAL_ONLY, POSITIONAL_OR_KEYWORD, KEYWORD_ONLY, VAR_POSITIONAL, or VAR_KEYWORD. |
| default | Any | Default value. If there is no default value, the value is EMPTY. |
| annotation | Any | Type annotation. If there is no annotation, the value is EMPTY. |
When to Use
| Scenario | Recommendation |
|---|---|
| The UDF has complete and accurate type annotations. | Pass signature=None and let the SDK automatically infer the signature. |
| The UDF annotation does not match the expected signature or needs to be overwritten. | Explicitly pass a signature object. |
| Batch mode requires the element types for the signature. | Explicitly pass a signature with element types so the SDK can map them to LakeFormation column types. |
Auto Inference Rules (signature=None)
When the signature is not explicitly passed (i.e., None), the SDK automatically infers the function signature based on the UDF's code structure and type annotations. You do not need to manually construct a signature object. If the inference fails, a SignatureInferenceError is thrown.
Syntax example:
Annotate the parameters and return value of the UDF to trigger automatic inference.
# UDF with type annotations (recommended)
def to_celsius(fahrenheit: float) -> float:
return (fahrenheit - 32) * 5 / 9
# When signature=None, the SDK automatically infers the equivalent signature:
# signature = [Parameter(name="fahrenheit", kind=POSITIONAL_OR_KEYWORD, default=EMPTY, annotation=float)]
# return_annotation = float
client.create_udf(to_celsius, name="to_celsius") Inference rules:
| UDF Form | Inference Behavior |
|---|---|
| Regular functions | Use inspect.signature and get_type_hints to read parameters and return annotations. |
| Class-defined UDFs | Check the __call__ method in the class and read its annotations. If the class has no custom __call__ method, a SignatureInferenceError is raised. |
| Class-instance UDFs | Check the __call__ method of the class to which the instance belongs. The instance has been serialized using cloudpickle, and class_name is derived from type(udf).__name__. |
| Binding method | Automatically unpacked and inferred as a regular function. |
- All parameters must have type annotations. FunctionSignature.from_signature will raise a ValueError for parameters without annotations.
- Raw container types (such as list and dict without element types) are not invalid. You must use annotated types like list[T] and dict[K, V].
- If the signature is None and inference fails (for example, the function has no type annotations or the class lacks __call__ method), the SDK will raise a SignatureInferenceError.
Signature Requirements in Batch Mode (is_batch=True)
In batch mode (is_batch=True), the UDF processes a batch of data at once rather than row by row. Therefore, the parameter annotations of the signature must use the batch types (PyArrow or Pandas array/table structures), and all parameters must have the same batch types to match the engine's batch parameter passing mechanism.
Syntax example:
Define a batch UDF using batch type annotations and enable is_batch during registration.
import pyarrow as pa
def double_batch(values: pa.Array) -> pa.Array:
return pa.array([v * 2 for v in values])
client.create_udf(double_batch, name="double_batch", is_batch=True, batch_size=1024) Batch type mapping
| Batch Type | Detected input_type |
|---|---|
| pyarrow.Array/pyarrow.Table | pyarrow |
| pandas.Series/pandas.DataFrame | pandas |
- If the signature is automatically inferred from the annotations and the parameters are non-batch element types, the SDK throws a ValueError.
- If an explicit signature is passed using element types, the SDK will infer the input_type from the UDF annotations.
- It is recommended to use list[T] to carry element type information, which facilitates LakeFormation to store the original column data types. A raw pyarrow.Array or pandas.Series without element types will cause errors during the from_signature phase.
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