yy782 opened a new pull request, #68223:
URL: https://github.com/apache/doris/pull/68223
### What problem does this PR solve?
Issue Number: close #68222
Related PR: #xxx
Problem Summary:
`width_bucket` returns a wrong result `0` for values that fall inside the
bucket range `[min_value, max_value)` when `num_buckets > (max_value -
min_value)`.
The root cause is the following guard in `_execute`:
if ((max_value - min_value) / num_buckets == 0) {
continue;
}
For integer arguments this is integer division, so it truncates to `0`
whenever `num_buckets > (max_value - min_value)`. The code then skips the
bucket computation and leaves the result at its initial value `0`, even
though the value is inside the range and should return a bucket number in
`[1, num_buckets]`.
Why remove the guard instead of replacing it with `max_value == min_value`?
The guard was meant to avoid a division by zero when `max_value ==
min_value`,
but that scenario never reaches the `else` branch. When `max_value ==
min_value`,
the two preceding conditions `expr < min_value` and `expr >= max_value` are
complementary and cover every possible `expr`, so the `else` branch
(`min_value <= expr < max_value`) is unreachable and `average_value` can
never
be `0` when the division is actually executed. The guard is therefore dead
code and is removed, making the implementation follow the PostgreSQL/Oracle
semantics:
floor(num_buckets * (expr - min_value) / (max_value - min_value)) + 1
which is always in `[1, num_buckets]` for values inside the range.
### Release note
None
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [x] Regression test
- [x] Unit Test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason <!-- Add your reason? -->
- Behavior changed:
- [ ] No.
- [x] Yes. `width_bucket` now returns the correct bucket number in `[1,
num_buckets]`
for values inside `[min_value, max_value)` when `num_buckets > (max_value -
min_value)`,
instead of the incorrect `0`.
- Does this need documentation?
- [x] No.
- [ ] Yes. <!-- Add document PR link here. eg:
https://github.com/apache/doris-website/pull/1214 -->
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR should
merge into -->
--
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]