Commit e824f88
[SPARK-52821][PYTHON] add int->DecimalType pyspark udf return type coercion
### What changes were proposed in this pull request?
- implements int to decimal coercion for data returned from the python UDF worker
- the change is gated by a sql conf (default disabled)
- we are making this change to all pandas_udfs to keep a consistent behavior across all
- affected evalTypes: SQL_ARROW_TABLE_UDF, SQL_COGROUPED_MAP_PANDAS_UDF, SQL_GROUPED_MAP_PANDAS_UDF_WITH_STATE, SQL_TRANSFORM_WITH_STATE_PANDAS_UDF, SQL_TRANSFORM_WITH_STATE_PANDAS_INIT_STATE_UDF, SQL_ARROW_BATCHED_UDF, SQL_SCALAR_PANDAS_UDF, SQL_SCALAR_PANDAS_ITER_UDF, SQL_MAP_PANDAS_ITER_UDF
- mapInArrow UDFs are not affected, generally there is no casting/coercion done for UDFs that directly return Arrow data
### Why are the changes needed?
- python UDFs with useArrow=True do not support type coercion from int to DecimalType if the target precision of the DecimalType is too low.
```
udf(returnType=DecimalType(2, 1), useArrow=True)
def test:
return 1
spark.range(1,2,1,1).select(test(col('id'))).display()
# expected: (Decimal) 1.0
# actual: pyarrow.lib.ArrowInvalid: Precision is not great enough for the result. It should be at least 20
```
### Does this PR introduce _any_ user-facing change?
- yes, but this is purely additive change. Before this, int->decimal threw an error.
### How was this patch tested?
- added unit tests
### Was this patch authored or co-authored using generative AI tooling?
No
Closes #51538 from benrobby/SPARK-52821.
Authored-by: Ben Hurdelhey <ben.hurdelhey@databricks.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>1 parent 634362c commit e824f88
File tree
11 files changed
+362
-18
lines changed- python/pyspark
- sql
- connect
- pandas
- tests/pandas
- sql
- catalyst/src/main/scala/org/apache/spark/sql/internal
- core/src/main/scala/org/apache/spark/sql/execution/python
11 files changed
+362
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
619 | 619 | | |
620 | 620 | | |
621 | 621 | | |
622 | | - | |
| 622 | + | |
623 | 623 | | |
624 | 624 | | |
625 | 625 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
739 | 739 | | |
740 | 740 | | |
741 | 741 | | |
742 | | - | |
| 742 | + | |
743 | 743 | | |
744 | 744 | | |
745 | 745 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
251 | 252 | | |
252 | 253 | | |
253 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
254 | 258 | | |
255 | 259 | | |
256 | | - | |
| 260 | + | |
257 | 261 | | |
258 | 262 | | |
259 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
260 | 299 | | |
261 | 300 | | |
262 | 301 | | |
| |||
326 | 365 | | |
327 | 366 | | |
328 | 367 | | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
329 | 371 | | |
330 | 372 | | |
331 | 373 | | |
| |||
444 | 486 | | |
445 | 487 | | |
446 | 488 | | |
| 489 | + | |
447 | 490 | | |
448 | | - | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
449 | 494 | | |
450 | 495 | | |
451 | 496 | | |
| |||
799 | 844 | | |
800 | 845 | | |
801 | 846 | | |
802 | | - | |
| 847 | + | |
803 | 848 | | |
804 | 849 | | |
805 | 850 | | |
| |||
819 | 864 | | |
820 | 865 | | |
821 | 866 | | |
| 867 | + | |
| 868 | + | |
822 | 869 | | |
823 | 870 | | |
824 | 871 | | |
| |||
905 | 952 | | |
906 | 953 | | |
907 | 954 | | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
908 | 958 | | |
909 | 959 | | |
910 | 960 | | |
| |||
1036 | 1086 | | |
1037 | 1087 | | |
1038 | 1088 | | |
| 1089 | + | |
1039 | 1090 | | |
1040 | 1091 | | |
1041 | | - | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
1042 | 1096 | | |
1043 | 1097 | | |
1044 | 1098 | | |
| |||
1406 | 1460 | | |
1407 | 1461 | | |
1408 | 1462 | | |
1409 | | - | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
1410 | 1471 | | |
1411 | | - | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
1412 | 1476 | | |
1413 | 1477 | | |
1414 | 1478 | | |
| |||
1482 | 1546 | | |
1483 | 1547 | | |
1484 | 1548 | | |
1485 | | - | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
1486 | 1557 | | |
1487 | | - | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
1488 | 1563 | | |
1489 | 1564 | | |
1490 | 1565 | | |
| |||
Lines changed: 48 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
284 | 332 | | |
285 | 333 | | |
286 | 334 | | |
| |||
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
387 | 387 | | |
388 | 388 | | |
389 | 389 | | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
390 | 421 | | |
391 | 422 | | |
392 | 423 | | |
| |||
Lines changed: 28 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
62 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
63 | 70 | | |
64 | 71 | | |
65 | 72 | | |
| |||
75 | 82 | | |
76 | 83 | | |
77 | 84 | | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
| |||
314 | 318 | | |
315 | 319 | | |
316 | 320 | | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
317 | 341 | | |
318 | 342 | | |
319 | 343 | | |
| |||
0 commit comments