jacktengg commented on code in PR #71040:
URL: https://github.com/apache/doris/pull/71040#discussion_r4703192355


##########
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:
   fixed



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