Commit 3456d4f
[SPARK-47681][FOLLOWUP] Fix schema_of_variant(decimal)
### What changes were proposed in this pull request?
The PR #46338 found `schema_of_variant` sometimes could not correctly handle variant decimals and had a fix. However, I found that the fix is incomplete and `schema_of_variant` can still fail on some inputs. The reason is that `VariantUtil.getDecimal` calls `stripTrailingZeros`. For an input decimal `10.00`, the resulting scale is -1 and the unscaled value is 1. However, negative decimal scale is not allowed by Spark. The correct approach is to use the `BigDecimal` to construct a `Decimal` and read its precision and scale, as what we did in `VariantGet`.
This PR also includes a minor change for `VariantGet`, where a duplicated expression is computed twice.
### Why are the changes needed?
They are bug fixes and are required to process decimals correctly.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
More unit tests. Some of them would fail without the change in this PR (e.g., `check("10.00", "DECIMAL(2,0)")`). Others wouldn't fail, but can still enhance test coverage.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #46549 from chenhao-db/fix_decimal_schema.
Authored-by: Chenhao Li <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>1 parent 42f2132 commit 3456d4f
File tree
2 files changed
+13
-4
lines changed- sql
- catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/variant
- core/src/test/scala/org/apache/spark/sql
2 files changed
+13
-4
lines changedLines changed: 3 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
341 | 341 | | |
342 | 342 | | |
343 | 343 | | |
344 | | - | |
| 344 | + | |
345 | 345 | | |
346 | 346 | | |
347 | 347 | | |
| |||
682 | 682 | | |
683 | 683 | | |
684 | 684 | | |
685 | | - | |
686 | | - | |
687 | | - | |
| 685 | + | |
| 686 | + | |
688 | 687 | | |
689 | 688 | | |
690 | 689 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
163 | 173 | | |
164 | 174 | | |
165 | 175 | | |
| |||
0 commit comments