LuciferYang opened a new pull request, #64675:
URL: https://github.com/apache/doris/pull/64675
### What problem does this PR solve?
Problem Summary:
`NumericArithmetic.divideDecimal` (the DECIMALV2 constant-folding `divide`
function) guarded against division by zero by checking the **numerator**
(`first`) instead of the **denominator** (`second`):
```java
public static Expression divideDecimal(DecimalLiteral first, DecimalLiteral
second) {
if (first.getValue().compareTo(BigDecimal.ZERO) == 0) { // wrong
operand
return new NullLiteral(first.getDataType());
}
BigDecimal result = first.getValue().divide(second.getValue());
return new DecimalLiteral(result);
}
```
As a result, when both operands are DECIMALV2 literals:
- `0 / x` folded to **NULL** instead of `0` — a silent wrong result.
- `x / 0` skipped the guard and called `BigDecimal.divide(ZERO)`, throwing
`ArithmeticException`. This is caught by `ExpressionEvaluator.invoke` and the
expression is left unfolded (BE then evaluates it and returns NULL), so it is
not a crash, but the FE folding path is incorrect.
The sibling functions `divideDouble` and `divideDecimalV3` both correctly
check `second`. This PR aligns `divideDecimal` with them by checking the
denominator. Division by zero continues to return NULL, matching Doris/MySQL
semantics (Doris is MySQL-compatible here; it does not raise the ANSI `division
by zero` error).
### Release note
Fix incorrect constant folding of DECIMALV2 division: `0 / x` was folded to
NULL instead of 0.
### Check List (For Author)
- Test
- [x] Unit Test (added `testDivideDecimalZeroNumerator` and
`testDivideDecimalZeroDenominator` in `NumericArithmeticTest`)
- Behavior changed:
- [x] Yes. `0 / x` over constant DECIMALV2 values now correctly folds to
`0` instead of `NULL`. Division by zero still returns NULL (unchanged).
- Does this need documentation?
- [x] No.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]