mrhhsg commented on code in PR #68223:
URL: https://github.com/apache/doris/pull/68223#discussion_r4710141489


##########
be/src/exprs/function/function_width_bucket.cpp:
##########
@@ -115,9 +115,6 @@ class FunctionWidthBucket : public IFunction {
             } else if (expr_data[i] >= max_value) {
                 nested_data[i] = num_buckets + 1;
             } else {
-                if ((max_value - min_value) / num_buckets == 0) {
-                    continue;
-                }
                 nested_data[i] = (int64_t)(1 + (expr_data[i] - min_value) / 
average_value);

Review Comment:
   [P2] Compute integer bucket boundaries without rounding through `double`.
   
   `width_bucket(9, 0, 18, 28)` should return `15` (`floor(9 * 28 / 18) + 1`). 
With this patch it returns `14`: rounding `18 / 28.0` first makes `1 + 9 / 
average_value` evaluate to `14.999999999999998`, which the cast truncates. This 
is still in the dense-bucket case the PR is meant to fix. Could we widen to 
`Int128` before subtracting and compute `distance * num_buckets / width + 1` on 
the integer path? This input would make a useful regression test.



##########
be/src/exprs/function/function_width_bucket.cpp:
##########
@@ -115,9 +115,6 @@ class FunctionWidthBucket : public IFunction {
             } else if (expr_data[i] >= max_value) {
                 nested_data[i] = num_buckets + 1;
             } else {
-                if ((max_value - min_value) / num_buckets == 0) {

Review Comment:
   [P2] Keep the floating-point path from casting NaN to `int64_t`.
   
   This guard also handles floating-point underflow. Consider 
`width_bucket(CAST(0 AS DOUBLE), CAST(0 AS DOUBLE), CAST('5e-324' AS DOUBLE), 
2)`. The upper bound rounds to the smallest positive `DOUBLE` (`2^-1074`), so 
the value is inside the interval, but dividing the width by two rounds the 
bucket width to `0.0`. The old check then skipped the calculation and returned 
`0`. With the check gone, the next line computes `0.0 / 0.0` and casts the 
resulting NaN to `int64_t`, which is undefined behavior. UBSan reported 
`float-cast-overflow` in a standalone run of the same arithmetic.
   
   Could we handle the floating-point path separately and add a regression test 
for this interval? Computing the scaled distance over the full width avoids 
rounding the per-bucket width to zero in this case and gives bucket `1`.



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