zhang-arvin opened a new pull request, #11875: URL: https://github.com/apache/seatunnel/pull/11875
Closes #11723. ## Purpose of this pull request `ZetaSQLFunction.executeBinaryExpr` had two independent defects in its `DECIMAL` branch. ### 1. Operands were round-tripped through `double` `+`, `-`, `*` and `/` all converted with `BigDecimal.valueOf(value.doubleValue())`, collapsing each operand to a `double` before the arithmetic started and discarding everything beyond ~17 significant digits. On a `DECIMAL(38,2)` money column, `123456789012345678.99 + 0.01` returned `123456789012345680.01` -- off by `1.01`, with a spurious digit -- instead of `123456789012345679.00`. A `BIGINT` operand above 2^53 was rounded for the same reason. **Fix**: Convert exactly via `toBigDecimal()` helper -- `BigDecimal` operands are used as-is, integral types go through `BigDecimal.valueOf(longValue())`. ### 2. Division rounded away from zero It used `RoundingMode.UP`, which is not 'round up' but 'always away from zero', so at scale 2 `10 / 3` returned `3.34` instead of `3.33` and `1 / 1000` returned `0.01` instead of `0.00` -- manufacturing value from a quotient that should round to zero, with a systematic upward bias across a table. **Fix**: Changed to `RoundingMode.HALF_UP`. ### Additional changes - Multiplication now rounds its result to the declared column scale (`HALF_UP`), matching division's behavior. - Division by zero now throws a `TransformException` naming the operation, matching how `MOD` by zero has always been reported. - `%` (`MOD`) is unaffected and untouched. ## Does this PR introduce any user-facing change? Yes, and it is documented in `incompatible-changes.md` (en + zh). Results of `+ - * /` on `DECIMAL` columns change wherever the old `double` conversion was lossy, and every inexact division result changes where the old `UP` rounding inflated it. In both cases the new value is the correct one and the old value was wrong, but jobs reconciled against the old output will see a difference, so it is called out as breaking with migration guidance. No config options, defaults, or SPI contracts changed. ## How was this patch tested? Added `SQLDecimalArithmeticTest` (8 tests) covering: - Exact `+`/`-`/`*` on `DECIMAL(38,2)` - `DECIMAL` + `BIGINT` mix above 2^53 - Positive division rounding - Negative operand division rounding - `HALF_UP` tie - Division by zero `DECIMAL` - Emitted scale matches declared type **Test results**: `Tests run: 8, Failures: 0, Errors: 0, Skipped: 0` ## Check list - [x] If necessary, please update `incompatible-changes.md` to describe the incompatibility caused by this PR. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
