hello-stephen commented on code in PR #68223:
URL: https://github.com/apache/doris/pull/68223#discussion_r4730963969
##########
be/src/exprs/function/function_width_bucket.cpp:
##########
@@ -106,19 +108,33 @@ class FunctionWidthBucket : public IFunction {
const auto* max_value_data =
max_value_column_concrete.get_data().data();
auto* nested_data = nested_column_concrete.get_data().data();
+ using ValueType =
std::remove_cv_t<std::remove_reference_t<decltype(expr_data[0])>>;
for (size_t i = 0; i < input_rows_count; ++i) {
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 (expr_data[i] < min_value) {
+ auto expr_value = expr_data[i];
+ if (expr_value < min_value) {
continue;
- } else if (expr_data[i] >= max_value) {
+ } else if (expr_value >= max_value) {
nested_data[i] = num_buckets + 1;
+ } else if constexpr (std::is_integral_v<ValueType>) {
+ const __int128 width =
+ static_cast<__int128>(max_value) -
static_cast<__int128>(min_value);
+ const __int128 distance =
+ static_cast<__int128>(expr_value) -
static_cast<__int128>(min_value);
+ nested_data[i] =
+ static_cast<int64_t>(1 + distance *
static_cast<__int128>(num_buckets) / width);
} else {
- if ((max_value - min_value) / num_buckets == 0) {
+ const long double width =
+ static_cast<long double>(max_value) - static_cast<long
double>(min_value);
+ const long double scaled =
+ (static_cast<long double>(expr_value) -
static_cast<long double>(min_value)) *
+ static_cast<long double>(num_buckets) / width;
+ if (!std::isfinite(scaled)) {
continue;
}
- nested_data[i] = (int64_t)(1 + (expr_data[i] - min_value) /
average_value);
+ nested_data[i] = static_cast<int64_t>(
+ 1 + std::min(scaled, static_cast<long
double>(num_buckets - 1)));
Review Comment:
[P2] Preserve exact DOUBLE bucket boundaries before truncating
On Linux x86_64, this call changes from `127` to `126`:
```sql
width_bucket(CAST('0.25000000000000067' AS DOUBLE),
CAST('-62.74999999999996' AS DOUBLE),
CAST('0.7500000000000003' AS DOUBLE), 127)
```
For these stored DOUBLE values, `distance * 127 / width` is exactly `126`,
so the value belongs in bucket `127`. With the [64-bit significand used by x86
extended precision](https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html), the
new multiplication rounds down and the division produces `126 - 2^-57`; adding
one and truncating therefore yields `126`. Please preserve exact bucket
boundaries during this calculation and add a DOUBLE regression for this case.
##########
be/src/exprs/function/function_width_bucket.cpp:
##########
@@ -106,19 +108,33 @@ class FunctionWidthBucket : public IFunction {
const auto* max_value_data =
max_value_column_concrete.get_data().data();
auto* nested_data = nested_column_concrete.get_data().data();
+ using ValueType =
std::remove_cv_t<std::remove_reference_t<decltype(expr_data[0])>>;
for (size_t i = 0; i < input_rows_count; ++i) {
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 (expr_data[i] < min_value) {
+ auto expr_value = expr_data[i];
+ if (expr_value < min_value) {
continue;
- } else if (expr_data[i] >= max_value) {
+ } else if (expr_value >= max_value) {
nested_data[i] = num_buckets + 1;
+ } else if constexpr (std::is_integral_v<ValueType>) {
+ const __int128 width =
+ static_cast<__int128>(max_value) -
static_cast<__int128>(min_value);
+ const __int128 distance =
+ static_cast<__int128>(expr_value) -
static_cast<__int128>(min_value);
+ nested_data[i] =
+ static_cast<int64_t>(1 + distance *
static_cast<__int128>(num_buckets) / width);
} else {
- if ((max_value - min_value) / num_buckets == 0) {
+ const long double width =
+ static_cast<long double>(max_value) - static_cast<long
double>(min_value);
+ const long double scaled =
+ (static_cast<long double>(expr_value) -
static_cast<long double>(min_value)) *
+ static_cast<long double>(num_buckets) / width;
Review Comment:
[P2] Avoid intermediate overflow when long double is binary64
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 `width_bucket(CAST('1e308' AS DOUBLE), CAST(0 AS DOUBLE), CAST('1.7e308'
AS DOUBLE), 2)`, both the interval width and the old bucket-width calculation
remain finite, giving bucket `2`. Here `distance * num_buckets` overflows
before division, so the new `isfinite` check leaves the initialized result `0`.
Please use overflow-safe scaling that works without extended `long double`
range and add a regression for this finite interval.
--
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]