Condividi tramite


arrow_udtf

Crea una funzione tabella definita dall'utente nativa di PyArrow. Questa funzione fornisce un'interfaccia nativa di PyArrow per le funzioni definite dall'utente, in cui il metodo eval riceve PyArrow RecordBatches o Array e restituisce un iteratore di tabelle PyArrow o RecordBatches. In questo modo è possibile calcolare true vettorializzato senza sovraccarico di elaborazione riga per riga.

Sintassi

from pyspark.databricks.sql import functions as dbf

@dbf.arrow_udtf(returnType=<returnType>)
class MyUDTF:
    def eval(self, ...):
        ...

Parametri

Parametro TIPO Description
cls classopzionale Classe del gestore della funzione tabella definita dall'utente python.
returnType pyspark.sql.types.StructType o str, facoltativo Tipo restituito della funzione di tabella definita dall'utente. Il valore può essere un oggetto StructType o una stringa di tipo struct formattato DDL.

Esempi

UDTF con input RecordBatch PyArrow:

import pyarrow as pa
from pyspark.databricks.sql.functions import arrow_udtf

@arrow_udtf(returnType="x int, y int")
class MyUDTF:
    def eval(self, batch: pa.RecordBatch):
        # Process the entire batch vectorized
        x_array = batch.column('x')
        y_array = batch.column('y')
        result_table = pa.table({
            'x': x_array,
            'y': y_array
        })
        yield result_table

df = spark.range(10).selectExpr("id as x", "id as y")
MyUDTF(df.asTable()).show()

UDTF con input della matrice PyArrow:

@arrow_udtf(returnType="x int, y int")
class MyUDTF2:
    def eval(self, x: pa.Array, y: pa.Array):
        # Process arrays vectorized
        result_table = pa.table({
            'x': x,
            'y': y
        })
        yield result_table

MyUDTF2(lit(1), lit(2)).show()

Annotazioni

  • Il metodo eval deve accettare PyArrow RecordBatches o Array come input
  • Il metodo eval deve restituire tabelle PyArrow o RecordBatches come output