Class UDF
A class UDF uses object-oriented programming to enable stateful processing logic. To implement it, you must define methods such as __init__ and __call__.
Class UDF Registration
When registering a class UDF, you pass a Python class where __init__, __call__, and __del__ have specific meanings (refer to the table below for details). Other class attributes and instance methods can be freely added by you without restrictions.
|
Python Class Method |
Mandatory |
Parameter |
Description |
Use Case |
|---|---|---|---|---|
|
__init__(self, *args, **kwargs) |
No |
Parameters are passed through the with_arguments method. Only scalar values are allowed. |
Constructor of the UDF. |
Initializes UDF attributes (such as saving parameters, opening files, and establishing connections). |
|
__call__(self, *args, **kwargs) |
Yes |
Parameters are passed through the UDF operator. Both scalar values and column names are allowed. |
Entry point for the service logic of the UDF. |
Processes data and performs calculations in the UDF. |
|
__del__(self) |
No |
Does not support passing parameters. |
Destructor of the UDF. |
Clears UDF resources (for example, closing files and disconnecting networks). |
Example
import ibis
import fabric_data as fabric
from fabric_data.udf import RegisterType
class Hello:
def __init__(self, firstname: str, lastname: str):
self.firstname = firstname
self.lastname = lastname
def __call__(self, location: str) -> str:
return f"Hello{self.firstname} {self.lastname}! Welcome to {location}"
con = ibis.fabric.connect(...)
# Explicitly register a UDF directly.
udf = con.udf.python.register(Hello, database="your-database", register_type=RegisterType.STAGED)
# Alternatively, register a UDF from a file.
udf = con.udf.python.register_from_file("your-current-file-path", "Hello", database="your-database", register_type=RegisterType.STAGED)
# Use the class UDF.
t = con.table(name="your-table", schema="your-schema")
expression = t.select(udf(t.city).with_arguments(firstname="Ethan", lastname="Carter"))
print(expression.execute())
Feedback
Was this page helpful?
Provide feedbackThank 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