Fix errenous estimation that caused infinite loop

This commit is contained in:
nagamocha3000 2021-02-26 18:48:36 +03:00 committed by Matt Jaffee
parent ef58b66e2c
commit a6c157d5db
No known key found for this signature in database
GPG key ID: 08A3DFFF987B11BF
2 changed files with 11 additions and 5 deletions

View file

@ -1365,7 +1365,7 @@ 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 := int64(math.Round(float64(max+min) * nth))
possibleNthVal := (max + min) / 2
// get left count
rangeCall.Args[fieldName] = &pql.Condition{
Op: pql.Token(pql.LT),

View file

@ -6878,14 +6878,19 @@ func variousQueriesOnPercentiles(t *testing.T, clusterSize int) {
num int64
rowKey string
}
size := 100
nths := []float64{0.25}
size := 1000
nths := []float64{0.1, 0.25, 0.5, 0.75, 0.9, 0.99, 0.999}
testValues := make([]testValue, size)
rowKeys := [2]string{"foo", "bar"}
for i := 0; i < size; i++ {
num := int64(r.Uint32())
// flip coin to negate
if r.Uint64()%2 == 0 {
num = -num
}
testValues[i] = testValue{
colKey: fmt.Sprintf("user%d", i+1),
num: int64(r.Uint64()),
num: num,
rowKey: rowKeys[r.Uint64()%2], // flip a coin
}
}
@ -6919,9 +6924,10 @@ func variousQueriesOnPercentiles(t *testing.T, clusterSize int) {
}
k := (1 - nth) / nth
possibleNthVal := int64(0)
// bin search
for min < max {
possibleNthVal := int64(math.Round(float64(max+min) * nth))
possibleNthVal = (max + min) / 2
leftCount, rightCount := int64(0), int64(0)
for _, num := range nums {
if num < possibleNthVal {