Commit 1e6d743
[SPARK-54750][SQL] Fix ROUND returning NULL for Decimal values with l…
### What changes were proposed in this pull request?
Fixed a bug in `RoundBase` where `ROUND` function incorrectly returns `NULL` for certain Decimal values. The issue was caused by using the input decimal's runtime precision instead of the target type's precision when calling `toPrecision()`.
For example, `ROUND(PERCENTILE_APPROX(2150 / 1000.0, 0.95), 3)` incorrectly returned `NULL` instead of `2.15`.
### Why are the changes needed?
The fix changes `decimal.toPrecision(decimal.precision, s, mode)` to `decimal.toPrecision(p, s, mode)` in both `nullSafeEval` and `doGenCode` methods, where `p` is the target DecimalType's precision.
### Does this PR introduce _any_ user-facing change?
Yes, this fixes a bug where ROUND could return NULL for valid decimal inputs.
### How was this patch tested?
Added regression test in `ApproximatePercentileQuerySuite`.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #53529 from qindongliang/SPARK-54750-fix-round-precision.
Lead-authored-by: qindongliang <[email protected]>
Co-authored-by: Kent Yao <[email protected]>
Signed-off-by: Kent Yao <[email protected]>1 parent 0da9e05 commit 1e6d743
File tree
2 files changed
+15
-4
lines changed- sql
- catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions
- core/src/test/scala/org/apache/spark/sql
2 files changed
+15
-4
lines changedLines changed: 3 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1572 | 1572 | | |
1573 | 1573 | | |
1574 | 1574 | | |
1575 | | - | |
| 1575 | + | |
1576 | 1576 | | |
1577 | 1577 | | |
1578 | 1578 | | |
| |||
1644 | 1644 | | |
1645 | 1645 | | |
1646 | 1646 | | |
1647 | | - | |
1648 | | - | |
| 1647 | + | |
1649 | 1648 | | |
1650 | | - | |
| 1649 | + | |
1651 | 1650 | | |
1652 | 1651 | | |
1653 | 1652 | | |
| |||
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
370 | 370 | | |
371 | 371 | | |
372 | 372 | | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
373 | 385 | | |
0 commit comments