将Python用户定义的表函数注册为 SQL 表函数。
Syntax
register(name, f)
参数
| 参数 | 类型 | Description |
|---|---|---|
name |
str | SQL 语句中用户定义的表函数的名称。 |
f |
UserDefinedTableFunction |
用户定义的表函数。 |
退货
UserDefinedTableFunction
注释
Spark 使用给定用户定义表函数的返回类型作为已注册函数的返回类型。
若要注册不确定Python表函数,请先生成一个不确定的用户定义表函数,然后将其注册为 SQL 函数。
示例
from pyspark.sql.functions import udtf
@udtf(returnType="c1: int, c2: int")
class PlusOne:
def eval(self, x: int):
yield x, x + 1
spark.udtf.register(name="plus_one", f=PlusOne)
spark.sql("SELECT * FROM plus_one(1)").collect()
# [Row(c1=1, c2=2)]
# Use it with a lateral join.
spark.sql("SELECT * FROM VALUES (0, 1), (1, 2) t(x, y), LATERAL plus_one(x)").collect()
# [Row(x=0, y=1, c1=0, c2=1), Row(x=1, y=2, c1=1, c2=2)]