Updated on 2025-12-19 GMT+08:00

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

  1. Establish a connection.
    import ibis
    import fabric_data as fabric
    from fabric_data.udf import RegisterType
    
    con = ibis.fabric.connect(...)
  2. 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"
    )
  3. 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)
  4. 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())