Skip to content

[Bug] DECIMALV2 constant folding: 0 / x folds to NULL instead of 0 (divideDecimal checks wrong operand) #64681

Description

@LuciferYang

Search before asking

  • I had searched in the issues and found no similar issues.

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?

  • Yes I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions