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

显式注册Scalar UDF

显式注册Scalar UDF,即用户需要手动在Python代码中侵入式添加注册逻辑代码。详细用法请参见Scalar UDF显式注册语法

  • 对于Scalar Python UDF,注册Scalar Python UDF的作用是将一个原始的Python函数或者类注册进数据库中。显式注册的示例代码如下,传入参数说明请参见Scalar Python UDF注册参数
    • Scalar Python UDF显式注册Python函数示例
      import ibis
      import ibis_fabric as fabric
      from ibis_fabric.udf import RegisterType
      
      def calculate_product(price: float, quantity: int) -> float:    
          return price * quantity
      
      con = ibis.fabric.connect(...)
      
      # 显式,直接注册UDF
      udf = con.udf.python.register(calculate_product, database="your-database", register_type=RegisterType.OBS)
      # 显式,从文件注册UDF
      udf = con.udf.python.register_from_file("your-current-file-path", "calculate_product", database="your-database", register_type=RegisterType.OBS)
      
      # 使用UDF
      t = con.table(name="your-table", schema="your-schema")
      expression = t.select(udf(t.price, t.quantity).name("sum column"))
      
      print(expression.execute())
      
    • Scalar Python UDF显式注册Python类示例
      import ibis
      import ibis_fabric as fabric
      from ibis_fabric.udf import RegisterType
      
      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}"
      
      con = ibis.fabric.connect(...)
      
      # 显式,直接注册UDF
      udf = con.udf.python.register(Hello, database="your-database", register_type=RegisterType.OBS)
      # 显式,从文件注册UDF
      udf = con.udf.python.register_from_file("your-current-file-path", "Hello", database="your-database", register_type=RegisterType.OBS)
      
      # 使用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())
  • 对于Scalar Builtin UDF,注册Scalar Builtin UDF的作用是获得数据库已存在的函数的句柄,无实际注册的操作。显式注册的示例代码如下,传入参数说明请参见Scalar Builtin UDF注册参数
    • Scalar Builtin UDF显式注册Python函数示例
      import ibis
      import ibis_fabric as fabric
      # 数据库中已有power函数
      def power(a: float, b: float) -> float: 
          ...
      
      con = ibis.fabric.connect(...)
      
      # 显式,直接注册UDF
      udf = con.udf.builtin.register(power, database="your-database")
      # 显式,从文件注册UDF
      udf = con.udf.builtin.register_from_file("your-current-file-path", "power", database="your-database")
      
      # 使用UDF
      t = con.table(name="your-table", schema="your-schema")
      expression = t.select(udf(t.interest, t.interval).name("yield column"))
      
      print(expression.execute())
    • Scalar Builtin UDF显式注册Python类示例
      import ibis
      import ibis_fabric as fabric
      # 数据库中已有Power类
      class Power:
          def __init__(self, base: float): ...
      
          def process(self, exp: float) -> float: ...
      
      con = ibis.fabric.connect(...)
      
      # 显式,直接注册UDF
      udf = con.udf.builtin.register(Power, database="your-database")
      # 显式,从文件注册UDF
      udf = con.udf.builtin.register_from_file("your-current-file-path", "Power", database="your-database")
      
      # 使用UDF
      t = con.table(name="your-table", schema="your-schema")
      expression = t.select(udf(t.interval).with_arguments(base=2048.0).name("yield column"))
      
      print(expression.execute())