Fix max-int limit for number of points reduced in geo_centroid (#56370)

A bug in InternalGeoCentroid#reduce existed that summed up
the aggregation's long-valued counts into a local integer variable.
Since it is definitely possible to reduce more than Integer.MAX points,
this change simply updates that variable to be a long-valued number.

Closes #55992.
This commit is contained in:
Tal Levy 2020-05-07 14:29:48 -07:00 committed by Tal Levy
parent 6e0178fb68
commit 13944b1bf9
2 changed files with 12 additions and 2 deletions

View File

@ -115,7 +115,7 @@ public class InternalGeoCentroid extends InternalAggregation implements GeoCentr
public InternalGeoCentroid reduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
double lonSum = Double.NaN;
double latSum = Double.NaN;
int totalCount = 0;
long totalCount = 0;
for (InternalAggregation aggregation : aggregations) {
InternalGeoCentroid centroidAgg = (InternalGeoCentroid) aggregation;
if (centroidAgg.count > 0) {

View File

@ -29,6 +29,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.equalTo;
public class InternalGeoCentroidTests extends InternalAggregationTestCase<InternalGeoCentroid> {
@Override
@ -52,7 +54,7 @@ public class InternalGeoCentroidTests extends InternalAggregationTestCase<Intern
protected void assertReduced(InternalGeoCentroid reduced, List<InternalGeoCentroid> inputs) {
double lonSum = 0;
double latSum = 0;
int totalCount = 0;
long totalCount = 0;
for (InternalGeoCentroid input : inputs) {
if (input.count() > 0) {
lonSum += (input.count() * input.centroid().getLon());
@ -67,6 +69,14 @@ public class InternalGeoCentroidTests extends InternalAggregationTestCase<Intern
assertEquals(totalCount, reduced.count());
}
public void testReduceMaxCount() {
InternalGeoCentroid maxValueGeoCentroid = new InternalGeoCentroid("agg", new GeoPoint(10, 0),
Long.MAX_VALUE, Collections.emptyMap());
InternalGeoCentroid reducedGeoCentroid = maxValueGeoCentroid
.reduce(Collections.singletonList(maxValueGeoCentroid), null);
assertThat(reducedGeoCentroid.count(), equalTo(Long.MAX_VALUE));
}
@Override
protected void assertFromXContent(InternalGeoCentroid aggregation, ParsedAggregation parsedAggregation) {
assertTrue(parsedAggregation instanceof ParsedGeoCentroid);