Search before asking
Version
master (Nereids fold-constant divideDecimal).
What's Wrong?
NumericArithmetic.divideDecimal (the DECIMALV2 constant-folding divide function) guards against division by zero by checking the numerator (first) instead of the denominator (second):
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);
}
When both operands are constant DECIMALV2 literals (so the fold-constant rule invokes divideDecimal):
0 / x folds to NULL instead of 0 — a silent wrong result.
x / 0 skips the guard and calls BigDecimal.divide(ZERO), throwing ArithmeticException. This is caught by ExpressionEvaluator.invoke and the expression is left unfolded (BE then evaluates it, returning NULL), so it is not a crash, but the FE folding path is incorrect.
The sibling functions divideDouble and divideDecimalV3 both correctly check second.
What You Expected?
0 / x should fold to 0. Division by zero should return NULL (Doris/MySQL semantics), consistent with divideDouble and divideDecimalV3.
How to Reproduce?
Constant-fold a DECIMALV2 division whose numerator literal is 0, with both operands constant DECIMALV2 literals so the Nereids fold-constant rule dispatches to divideDecimal. The folded result is NULL where it should be 0.
Anything Else?
Fix proposed in PR #64675.
Are you willing to submit PR?
Search before asking
Version
master (Nereids fold-constant
divideDecimal).What's Wrong?
NumericArithmetic.divideDecimal(the DECIMALV2 constant-foldingdividefunction) guards against division by zero by checking the numerator (first) instead of the denominator (second):When both operands are constant DECIMALV2 literals (so the fold-constant rule invokes
divideDecimal):0 / xfolds to NULL instead of0— a silent wrong result.x / 0skips the guard and callsBigDecimal.divide(ZERO), throwingArithmeticException. This is caught byExpressionEvaluator.invokeand the expression is left unfolded (BE then evaluates it, returning NULL), so it is not a crash, but the FE folding path is incorrect.The sibling functions
divideDoubleanddivideDecimalV3both correctly checksecond.What You Expected?
0 / xshould fold to0. Division by zero should return NULL (Doris/MySQL semantics), consistent withdivideDoubleanddivideDecimalV3.How to Reproduce?
Constant-fold a DECIMALV2 division whose numerator literal is
0, with both operands constant DECIMALV2 literals so the Nereids fold-constant rule dispatches todivideDecimal. The folded result is NULL where it should be 0.Anything Else?
Fix proposed in PR #64675.
Are you willing to submit PR?