github-actions[bot] commented on code in PR #67965:
URL: https://github.com/apache/doris/pull/67965#discussion_r4006389668


##########
be/src/exprs/function/cast/cast_to_decimal.h:
##########
@@ -246,45 +246,63 @@ struct CastToDecimal {
                                    "to decimal");
             return false;
         }
-        using DoubleType = std::conditional_t<IsDecimal256<ToCppT>, long 
double, double>;
-        DoubleType tmp = from * static_cast<DoubleType>(scale_multiplier);
-        if (tmp <= DoubleType(min_result) || tmp >= DoubleType(max_result)) {
+        auto overflow = [&]() {
             if (params.is_strict) {
                 params.status = DECIMAL_CONVERT_OVERFLOW_ERROR(from, 
"float/double", to_precision,
                                                                to_scale);
             }
             return false;
+        };
+        using DoubleType = std::conditional_t<IsDecimal256<ToCppT>, long 
double, double>;
+        // Round before checking the inclusive decimal bounds. Adding 0.5 
would itself round
+        // exactly representable odd integers above 2^52 to the next even 
integer in double.

Review Comment:
   Keep the rounding rule independent of the decimal backing width. For a 
`DOUBLE` value of `0.15` at scale 1, the Decimal128 path multiplies in 
binary64, where the product rounds to exactly `1.5`, and stores `0.2`. 
Precision 39 selects Decimal256, which promotes before multiplying; wider `long 
double` retains `1.5 - 2^-54`, so this branch stores `0.1`. Thus `CAST(d AS 
DECIMALV3(38,1))` and `CAST(d AS DECIMALV3(39,1))` return different values even 
though `d` is well within both ranges. Default FE constant folding also 
produces `0.2` for the precision-39 expression, so a folded constant and the BE 
column path disagree; the new regression disables folding, and the generic 
oracle repeats the target-conditioned arithmetic. Please use one rule across 
backing families and FE/BE execution, then add `+/-0.15` cross-precision and 
constant-versus-column coverage.



-- 
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]

Reply via email to