Commit 44fe840
committed
[SPARK-23352][PYTHON] Explicitly specify supported types in Pandas UDFs
This PR targets to explicitly specify supported types in Pandas UDFs.
The main change here is to add a deduplicated and explicit type checking in `returnType` ahead with documenting this; however, it happened to fix multiple things.
1. Currently, we don't support `BinaryType` in Pandas UDFs, for example, see:
```python
from pyspark.sql.functions import pandas_udf
pudf = pandas_udf(lambda x: x, "binary")
df = spark.createDataFrame([[bytearray(1)]])
df.select(pudf("_1")).show()
```
```
...
TypeError: Unsupported type in conversion to Arrow: BinaryType
```
We can document this behaviour for its guide.
2. Also, the grouped aggregate Pandas UDF fails fast on `ArrayType` but seems we can support this case.
```python
from pyspark.sql.functions import pandas_udf, PandasUDFType
foo = pandas_udf(lambda v: v.mean(), 'array<double>', PandasUDFType.GROUPED_AGG)
df = spark.range(100).selectExpr("id", "array(id) as value")
df.groupBy("id").agg(foo("value")).show()
```
```
...
NotImplementedError: ArrayType, StructType and MapType are not supported with PandasUDFType.GROUPED_AGG
```
3. Since we can check the return type ahead, we can fail fast before actual execution.
```python
# we can fail fast at this stage because we know the schema ahead
pandas_udf(lambda x: x, BinaryType())
```
Manually tested and unit tests for `BinaryType` and `ArrayType(...)` were added.
Author: hyukjinkwon <gurwls223@gmail.com>
Closes #20531 from HyukjinKwon/pudf-cleanup.
(cherry picked from commit c338c8c)
Signed-off-by: hyukjinkwon <gurwls223@gmail.com>1 parent 79e8650 commit 44fe840
5 files changed
Lines changed: 77 additions & 44 deletions
File tree
- docs
- python/pyspark/sql
- sql/catalyst/src/main/scala/org/apache/spark/sql/internal
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1676 | 1676 | | |
1677 | 1677 | | |
1678 | 1678 | | |
1679 | | - | |
| 1679 | + | |
1680 | 1680 | | |
1681 | 1681 | | |
1682 | 1682 | | |
| |||
1734 | 1734 | | |
1735 | 1735 | | |
1736 | 1736 | | |
1737 | | - | |
| 1737 | + | |
1738 | 1738 | | |
1739 | 1739 | | |
1740 | 1740 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3736 | 3736 | | |
3737 | 3737 | | |
3738 | 3738 | | |
3739 | | - | |
| 3739 | + | |
3740 | 3740 | | |
3741 | 3741 | | |
3742 | | - | |
| 3742 | + | |
3743 | 3743 | | |
3744 | 3744 | | |
3745 | 3745 | | |
| |||
3776 | 3776 | | |
3777 | 3777 | | |
3778 | 3778 | | |
3779 | | - | |
| 3779 | + | |
3780 | 3780 | | |
3781 | 3781 | | |
3782 | 3782 | | |
| |||
3825 | 3825 | | |
3826 | 3826 | | |
3827 | 3827 | | |
3828 | | - | |
| 3828 | + | |
3829 | 3829 | | |
3830 | 3830 | | |
3831 | 3831 | | |
3832 | 3832 | | |
3833 | 3833 | | |
3834 | 3834 | | |
3835 | 3835 | | |
3836 | | - | |
| 3836 | + | |
| 3837 | + | |
3837 | 3838 | | |
3838 | 3839 | | |
3839 | 3840 | | |
| |||
3842 | 3843 | | |
3843 | 3844 | | |
3844 | 3845 | | |
| 3846 | + | |
3845 | 3847 | | |
3846 | 3848 | | |
3847 | 3849 | | |
3848 | | - | |
| 3850 | + | |
3849 | 3851 | | |
3850 | 3852 | | |
3851 | 3853 | | |
| |||
4050 | 4052 | | |
4051 | 4053 | | |
4052 | 4054 | | |
4053 | | - | |
4054 | 4055 | | |
4055 | | - | |
4056 | | - | |
| 4056 | + | |
| 4057 | + | |
| 4058 | + | |
| 4059 | + | |
4057 | 4060 | | |
4058 | 4061 | | |
4059 | 4062 | | |
| |||
4088 | 4091 | | |
4089 | 4092 | | |
4090 | 4093 | | |
4091 | | - | |
4092 | | - | |
4093 | | - | |
4094 | | - | |
| 4094 | + | |
4095 | 4095 | | |
4096 | | - | |
4097 | | - | |
| 4096 | + | |
| 4097 | + | |
| 4098 | + | |
| 4099 | + | |
| 4100 | + | |
| 4101 | + | |
| 4102 | + | |
| 4103 | + | |
| 4104 | + | |
| 4105 | + | |
4098 | 4106 | | |
4099 | 4107 | | |
4100 | 4108 | | |
| |||
4325 | 4333 | | |
4326 | 4334 | | |
4327 | 4335 | | |
4328 | | - | |
4329 | | - | |
4330 | | - | |
| 4336 | + | |
| 4337 | + | |
| 4338 | + | |
4331 | 4339 | | |
4332 | 4340 | | |
4333 | 4341 | | |
4334 | 4342 | | |
4335 | 4343 | | |
4336 | 4344 | | |
| 4345 | + | |
4337 | 4346 | | |
4338 | 4347 | | |
4339 | 4348 | | |
| |||
4436 | 4445 | | |
4437 | 4446 | | |
4438 | 4447 | | |
4439 | | - | |
4440 | | - | |
4441 | | - | |
4442 | | - | |
4443 | | - | |
4444 | | - | |
4445 | | - | |
4446 | 4448 | | |
4447 | 4449 | | |
4448 | | - | |
4449 | | - | |
| 4450 | + | |
| 4451 | + | |
| 4452 | + | |
| 4453 | + | |
| 4454 | + | |
| 4455 | + | |
| 4456 | + | |
4450 | 4457 | | |
4451 | 4458 | | |
4452 | 4459 | | |
| |||
4465 | 4472 | | |
4466 | 4473 | | |
4467 | 4474 | | |
4468 | | - | |
4469 | | - | |
| 4475 | + | |
4470 | 4476 | | |
4471 | 4477 | | |
4472 | | - | |
4473 | | - | |
| 4478 | + | |
4474 | 4479 | | |
4475 | 4480 | | |
4476 | | - | |
| 4481 | + | |
4477 | 4482 | | |
4478 | 4483 | | |
4479 | 4484 | | |
4480 | | - | |
4481 | | - | |
4482 | 4485 | | |
4483 | | - | |
4484 | | - | |
| 4486 | + | |
| 4487 | + | |
| 4488 | + | |
| 4489 | + | |
| 4490 | + | |
| 4491 | + | |
| 4492 | + | |
| 4493 | + | |
| 4494 | + | |
| 4495 | + | |
| 4496 | + | |
| 4497 | + | |
| 4498 | + | |
4485 | 4499 | | |
4486 | 4500 | | |
4487 | 4501 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1638 | 1638 | | |
1639 | 1639 | | |
1640 | 1640 | | |
| 1641 | + | |
| 1642 | + | |
1641 | 1643 | | |
1642 | 1644 | | |
1643 | 1645 | | |
| |||
1680 | 1682 | | |
1681 | 1683 | | |
1682 | 1684 | | |
| 1685 | + | |
| 1686 | + | |
1683 | 1687 | | |
1684 | 1688 | | |
1685 | 1689 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
116 | 131 | | |
117 | 132 | | |
118 | 133 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1052 | 1052 | | |
1053 | 1053 | | |
1054 | 1054 | | |
1055 | | - | |
| 1055 | + | |
1056 | 1056 | | |
1057 | 1057 | | |
1058 | 1058 | | |
| |||
0 commit comments