hello-stephen commented on code in PR #71040:
URL: https://github.com/apache/doris/pull/71040#discussion_r4736105581
##########
be/src/exprs/function/function_width_bucket.cpp:
##########
@@ -107,50 +159,89 @@ 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;
+ // Multiply first so 15 in [0, 21) with 21 buckets stays
exactly
+ // on its boundary instead of rounding below 15 before the
cast.
+ long double bucket = distance * num_buckets / width;
+ // Eight epsilons conservatively cover rounding in the two
subtractions,
+ // multiplication, division and interval endpoints. Clamp
to interior buckets:
+ // the comparisons above already establish min <= value <
max.
+ long double error =
+ 8 * std::numeric_limits<long double>::epsilon() *
num_buckets;
+ auto lower_bucket = static_cast<Int64>(std::max(bucket -
error, 0.0L));
Review Comment:
[P2] Check floating intermediates before converting them to BIGINT
On supported Apple arm64 builds, [`long double` has the same range as
`double`](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms).
For a DOUBLE column containing `DBL_MAX / 2`, bounds `[0, DBL_MAX)`, and count
`4`, all input checks pass, but `distance * num_buckets` overflows to infinity.
This line then converts infinity to `Int64` before the exact fallback can run,
which is undefined behavior. The base computes `DBL_MAX / 4` first and returns
bucket `3` through finite arithmetic. Please route nonfinite or unrepresentable
intermediate endpoints to exact arithmetic before either integer cast, and
cover this finite-input case on the narrow-long-double target.
--
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]