更新时间:2025-09-18 GMT+08:00

隐式注册Scalar UDF

隐式注册Scalar UDF,即依赖Python运行时自动发现并注册Scalar UDF。详细用法请参见Scalar UDF隐式注册语法

  • 对于Scalar Python UDF,注册Scalar Python UDF的作用是将一个原始的Python函数注册进数据库中。隐式注册的示例代码如下,传入参数说明请参见Scalar Python UDF注册参数
    • Scalar Python UDF隐式注册Python函数示例
      import ibis_fabric as fabric
      from ibis_fabric.udf import RegisterType
      
      # 隐式注册UDF
      @fabric.udf.python(database="your-database", register_type=RegisterType.OBS)
      def calculate_product(price: float, quantity: int) -> float:    
          return price * quantity
      
      # 使用UDF
      con = ibis.fabric.connect(...)
      t = con.table("your-table", database="your-database")
      expression = t.select(calculate_product(t.price, t.quantity).name("product column"))
      print(expression.execute())
    • Scalar Python UDF隐式注册Python类示例
      import ibis
      import ibis_fabric as fabric
      from ibis_fabric.udf import RegisterType
      
      # 隐式注册UDF
      @fabric.udf.python(database="your-database", register_type=RegisterType.OBS)
      class Hello:
          def __init__(self, firstname: str, lastname: str): 
              self.firstname = firstname
              self.lastname = lastname
      
          def process(self, location: str) -> str:
              return f"Hello{self.firstname} {self.lastname}! Welcome to {location}"
      
      # 使用UDF
      con = ibis.fabric.connect(...)
      t = con.table(name="your-table", schema="your-schema")
      expression = t.select(Hello(t.city).with_arguments(firstname="Ethan", lastname="Carter"))
      print(expression.execute())
  • 对于Scalar Builtin UDF,注册Scalar Builtin UDF的作用是获得数据库已存在的函数的句柄,无实际注册的操作。隐式注册的示例代码如下,传入参数说明请参见Scalar Builtin UDF注册参数
    • Scalar Builtin UDF隐式注册Python函数示例
      import ibis
      import ibis_fabric as fabric
      # 隐式注册UDF,数据库中已有power函数
      @fabric.builtin.python(database="your-database")
      def power(a: float, b: float) -> float: 
          ...
      
      # 使用UDF
      con = ibis.fabric.connect(...)
      t = con.table("your-table", database="your-database")
      expression = t.select(power(t.interest, t.interval).name("yield column"))
      print(expression.execute())
    • Scalar Builtin UDF隐式注册Python类示例
      import ibis
      import ibis_fabric as fabric
      # 隐式注册UDF,数据库中已有Power类
      @fabric.builtin.python(database="your-database")
      class Power:
          def __init__(self, base: float): ...
      
          def process(self, exp: float) -> float: ...
      
      # 使用UDF
      con = ibis.fabric.connect(...)
      t = con.table("your-table", database="your-database")
      expression = t.select(Power(t.interval).with_arguments(base=1024.0).name("yield column"))
      print(expression.execute())