Pythonユーザー定義テーブル関数を SQL テーブル関数として登録します。
構文
register(name, f)
パラメーター
| パラメーター | タイプ | 説明 |
|---|---|---|
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)]