Skip to content

Commit 4a73f67

Browse files
committed
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.
1 parent 3d01f7f commit 4a73f67

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/metrics/InternalGeoCentroid.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public long count() {
115115
public InternalGeoCentroid reduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
116116
double lonSum = Double.NaN;
117117
double latSum = Double.NaN;
118-
int totalCount = 0;
118+
long totalCount = 0;
119119
for (InternalAggregation aggregation : aggregations) {
120120
InternalGeoCentroid centroidAgg = (InternalGeoCentroid) aggregation;
121121
if (centroidAgg.count > 0) {

server/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalGeoCentroidTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.List;
3030
import java.util.Map;
3131

32+
import static org.hamcrest.Matchers.equalTo;
33+
3234
public class InternalGeoCentroidTests extends InternalAggregationTestCase<InternalGeoCentroid> {
3335

3436
@Override
@@ -52,7 +54,7 @@ protected InternalGeoCentroid createTestInstance(String name, Map<String, Object
5254
protected void assertReduced(InternalGeoCentroid reduced, List<InternalGeoCentroid> inputs) {
5355
double lonSum = 0;
5456
double latSum = 0;
55-
int totalCount = 0;
57+
long totalCount = 0;
5658
for (InternalGeoCentroid input : inputs) {
5759
if (input.count() > 0) {
5860
lonSum += (input.count() * input.centroid().getLon());
@@ -67,6 +69,14 @@ protected void assertReduced(InternalGeoCentroid reduced, List<InternalGeoCentro
6769
assertEquals(totalCount, reduced.count());
6870
}
6971

72+
public void testReduceMaxCount() {
73+
InternalGeoCentroid maxValueGeoCentroid = new InternalGeoCentroid("agg", new GeoPoint(10, 0),
74+
Long.MAX_VALUE, Collections.emptyMap());
75+
InternalGeoCentroid reducedGeoCentroid = maxValueGeoCentroid
76+
.reduce(Collections.singletonList(maxValueGeoCentroid), null);
77+
assertThat(reducedGeoCentroid.count(), equalTo(Long.MAX_VALUE));
78+
}
79+
7080
@Override
7181
protected void assertFromXContent(InternalGeoCentroid aggregation, ParsedAggregation parsedAggregation) {
7282
assertTrue(parsedAggregation instanceof ParsedGeoCentroid);

0 commit comments

Comments
 (0)