Merge pull request #1624 from nagamocha3000/fix-percentile-overflow

Fix percentile overflow error
This commit is contained in:
nagamocha3000 2021-06-04 00:24:19 +03:00 • committed by GitHub
commit 29002b3f62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 2 deletions

View file

@ -1295,7 +1295,10 @@ func (e *executor) executePercentile(ctx context.Context, qcx *Qcx, index string
min, max := minVal.Val, maxVal.Val
// estimate nth val, eg median when nth=0.5
for min < max {
possibleNthVal := (max + min) / 2
// compute average without integer overflow, then correct for division of
// odd numbers by 2
possibleNthVal := ((max / 2) + (min / 2)) + (((max % 2) + (min % 2)) / 2)
// possibleNthVal = (max + min) / 2
// get left count
rangeCall.Args[fieldName] = &pql.Condition{
Op: pql.Token(pql.LT),

View file

@ -6939,7 +6939,7 @@ func variousQueriesOnPercentiles(t *testing.T, c *test.Cluster) {
possibleNthVal := int64(0)
// bin search
for min < max {
possibleNthVal = (max + min) / 2
possibleNthVal = ((max / 2) + (min / 2)) + (((max % 2) + (min % 2)) / 2)
leftCount, rightCount := int64(0), int64(0)
for _, num := range nums {
if num < possibleNthVal {