You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Darcy Shen (Jira)" <ji...@apache.org> on 2021/03/19 02:54:00 UTC

[jira] [Updated] (SPARK-34799) Return User-defined types from Pandas UDF

     [ https://issues.apache.org/jira/browse/SPARK-34799?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Darcy Shen updated SPARK-34799:
-------------------------------
    Description: 
Because Pandas UDF uses pyarrow to passing data, it does not currently support UserDefinedTypes, as what normal python udf does.

For example:


{code:python}
class BoxType(UserDefinedType):
    @classmethod
    def sqlType(cls) -> StructType:
        return StructType(
            fields=[
                StructField("xmin", DoubleType(), False),
                StructField("ymin", DoubleType(), False),
                StructField("xmax", DoubleType(), False),
                StructField("ymax", DoubleType(), False),
            ]
        )

@pandas_udf(
     returnType=StructType([StructField("boxes", ArrayType(Box()))]
)
def pandas_pf(s: pd.DataFrame) -> pd.DataFrame:
       yield s
{code}

The logs show
{code}
try:
                to_arrow_type(self._returnType_placeholder)
            except TypeError:
>               raise NotImplementedError(
                    "Invalid return type with scalar Pandas UDFs: %s is "
E                   NotImplementedError: Invalid return type with scalar Pandas UDFs: StructType(List(StructField(boxes,ArrayType(Box,true),true))) is not supported
{code}


> Return User-defined types from Pandas UDF
> -----------------------------------------
>
>                 Key: SPARK-34799
>                 URL: https://issues.apache.org/jira/browse/SPARK-34799
>             Project: Spark
>          Issue Type: Sub-task
>          Components: PySpark, SQL
>    Affects Versions: 3.0.2, 3.1.1
>            Reporter: Darcy Shen
>            Priority: Major
>
> Because Pandas UDF uses pyarrow to passing data, it does not currently support UserDefinedTypes, as what normal python udf does.
> For example:
> {code:python}
> class BoxType(UserDefinedType):
>     @classmethod
>     def sqlType(cls) -> StructType:
>         return StructType(
>             fields=[
>                 StructField("xmin", DoubleType(), False),
>                 StructField("ymin", DoubleType(), False),
>                 StructField("xmax", DoubleType(), False),
>                 StructField("ymax", DoubleType(), False),
>             ]
>         )
> @pandas_udf(
>      returnType=StructType([StructField("boxes", ArrayType(Box()))]
> )
> def pandas_pf(s: pd.DataFrame) -> pd.DataFrame:
>        yield s
> {code}
> The logs show
> {code}
> try:
>                 to_arrow_type(self._returnType_placeholder)
>             except TypeError:
> >               raise NotImplementedError(
>                     "Invalid return type with scalar Pandas UDFs: %s is "
> E                   NotImplementedError: Invalid return type with scalar Pandas UDFs: StructType(List(StructField(boxes,ArrayType(Box,true),true))) is not supported
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org