Scalar UDF
A scalar UDF is a basic row-level function. It takes zero or more input parameters, processes the data in one row, and returns one result per row.
Scalar UDFs are typically used for mathematical operations, complex type conversions, and custom formatting.
The behavior of a scalar UDF is similar to that of built-in functions such as COS(), MAX(), and MIN(). However, its implementation is user-defined.
Example
- Establish a connection.
import ibis import fabric_data as fabric from fabric_data.udf import RegisterType con = ibis.fabric.connect(...)
- Define a UDF.
def add_udf(lhs: int, rhs: int) -> int: return lhs + rhs def calculate_product(price: float, quantity: int) -> float: return price * quantity con.create_scalar_function( add_udf, input_type=fabric.udf.InputType.PYTHON, database=db, comment="To test create_scalar_function" ) - Register a UDF.
# Explicitly register a UDF directly. udf = con.udf.python.register(calculate_product, 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", "calculate_product", database="your-database", register_type=RegisterType.STAGED) - Use and execute the UDF.
# Use the UDF. t = con.table(name="your-table", schema="your-schema") expression = t.select(udf(t.price, t.quantity).name("sum column")) 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