swordfish_udf#
- swordfish.function.swordfish_udf(func=None, *, is_aggregation=False, is_state=False, mode='default')#
Registers a user-defined function (UDF) in Swordfish.
This enables Python functions to be used within Swordfish SQL queries and higher-order functions. The function can be registered in different modes.
- 参数:
func (Optional[Callable], optional) -- The UDF to be registered. Defaults to None.
is_aggregation (bool, optional) -- Specifies whether the UDF performs aggregation operations. Defaults to False.
is_state (bool, optional) -- Specifies whether the UDF maintains state across calls. Defaults to False.
mode (Literal["default", "translate"], optional) --
Determines how the UDF is registered. Defaults to "default".
"defalut": The function is saved as a Python UDF object which depends on the Python environment to run.
"translate": The function is translated into Swordfish's internal representation. This eliminates Python environment dependency but supports a limited subset of Python syntax.
- 返回:
The FunctionDef object representing the registered UDF.
- 返回类型:
示例
>>> import swordfish.function as F >>> @F.swordfish_udf(is_aggregation=True) >>> def avg_func(a: int, b: int) -> float: ... return (a + b) / 2
>>> @F.swordfish_udf(is_state=True) >>> def counter(state: int, increment: int) -> int: ... return state + increment
>>> @F.swordfish_udf(mode="translate") >>> def translate_to_uppercase(text: str) -> str: ... return text.upper()
>>> @F.swordfish_udf() >>> def add(a: int, b: int) -> int: ... return a + b