hello-stephen commented on code in PR #71040:
URL: https://github.com/apache/doris/pull/71040#discussion_r4697807262


##########
be/src/exprs/function/function_width_bucket.cpp:
##########
@@ -107,50 +136,75 @@ class FunctionWidthBucket : public IFunction {
         auto* nested_data = nested_column_concrete.get_data().data();
 
         for (size_t i = 0; i < input_rows_count; ++i) {
+            // A NULL bound can have a zero payload. Do not report equal 
bounds for that row.
+            if (null_map[i]) {
+                continue;
+            }
             auto min_value = min_value_data[i];
             auto max_value = max_value_data[i];
-            auto average_value = (max_value - min_value) / (1.0 * num_buckets);
+            if (min_value == max_value) {
+                throw Exception(ErrorCode::INVALID_ARGUMENT,
+                                "width_bucket bounds must be different");
+            }
+            if constexpr (std::is_floating_point_v<typename 
ColumnType::value_type>) {
+                if (!std::isfinite(expr_data[i]) || !std::isfinite(min_value) 
||
+                    !std::isfinite(max_value)) {
+                    throw Exception(ErrorCode::INVALID_ARGUMENT,
+                                    "width_bucket values and bounds must be 
finite");
+                }
+            }
             if (expr_data[i] < min_value) {
                 continue;
             } else if (expr_data[i] >= max_value) {
                 nested_data[i] = num_buckets + 1;
             } else {
-                if ((max_value - min_value) / num_buckets == 0) {
-                    continue;
+                if constexpr (std::is_integral_v<typename 
ColumnType::value_type>) {
+                    // Widen before subtraction and multiply before division. 
For example,
+                    // width_bucket(-1, -1, 0, 2) is 1 even though the bucket 
width is below 1.
+                    Int128 width = static_cast<Int128>(max_value) - min_value;
+                    Int128 distance = static_cast<Int128>(expr_data[i]) - 
min_value;
+                    nested_data[i] = static_cast<Int64>(distance * num_buckets 
/ width) + 1;
+                } else {
+                    long double width = static_cast<long double>(max_value) - 
min_value;
+                    long double distance = static_cast<long 
double>(expr_data[i]) - min_value;
+                    long double bucket = distance / width * num_buckets;

Review Comment:
   [P2] Preserve exact interior boundaries in the floating calculation
   
   For a DOUBLE column `d = 15`, `width_bucket(d, CAST(0 AS DOUBLE), CAST(21 AS 
DOUBLE), 21)` now produces 15 instead of 16 on x86_64. With 64-bit-significand 
`long double`, `(15 / 21) * 21` rounds to `15 - 2^-60`, so the following 
integer cast truncates to 14 before adding 1. The previous formula computes a 
bucket width of exactly 1 and returns 16. The upper-bound clamp cannot correct 
this interior boundary, so valid rows move into the preceding bucket. Please 
preserve interior boundaries in the floating calculation (for example, multiply 
before dividing) and add this case to the floating tests.



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