diff --git a/executor.go b/executor.go index 427691ed4..af0a1682f 100644 --- a/executor.go +++ b/executor.go @@ -1322,15 +1322,14 @@ func (e *executor) executePercentile(ctx context.Context, qcx *Qcx, index string return ValCount{}, errors.Wrap(err, "executing Max call for Percentile") } // set up reusables - countQuery, _ := pql.ParseString("Count(Row(fld < 0))") + countQuery, _ := pql.ParseString(fmt.Sprintf("Count(Row(%s < 0))", fieldName)) countCall := countQuery.Calls[0] - rangeQuery, _ := pql.ParseString("Row(fld < 0)") - rangeCall := rangeQuery.Calls[0] + rangeCall := countCall.Children[0] min, max := minVal.Val, maxVal.Val // estimate nth val, eg median when nth=0.5 for min < max { - possibleNthVal := (max - min) / 2 + possibleNthVal := (max + min) / 2 // get left count rangeCall.Args[fieldName] = &pql.Condition{ Op: pql.Token(pql.LT), diff --git a/executor_test.go b/executor_test.go index 80a7e2065..01633ef02 100644 --- a/executor_test.go +++ b/executor_test.go @@ -6896,7 +6896,7 @@ func variousQueriesOnPercentiles(t *testing.T, clusterSize int) { // Rows { query: `Percentile(field="net_worth", nth=0.5)`, - csvVerifier: "10,1\n", + csvVerifier: "40,1\n", }, }