Skip to content

Commit d147e0f

Browse files
Correct calculation of avg value to avoid overflow
1 parent f6516aa commit d147e0f

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

server/src/main/java/org/elasticsearch/search/query/QueryPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ static long estimateMedianValue(PointValues pointValues) throws IOException {
584584
long minValue = LongPoint.decodeDimension(pointValues.getMinPackedValue(), 0);
585585
long maxValue = LongPoint.decodeDimension(pointValues.getMaxPackedValue(), 0);
586586
while (minValue < maxValue) {
587-
long avgValue = Math.floorDiv(minValue + maxValue, 2);
587+
long avgValue = Math.floorDiv(minValue, 2) + Math.floorDiv(maxValue, 2); // to avoid overflow first divide each value by 2
588588
long countLeft = estimatePointCount(pointValues, minValue, avgValue);
589589
long countRight = estimatePointCount(pointValues, avgValue + 1, maxValue);
590590
if (countLeft >= countRight) {

0 commit comments

Comments
 (0)