Skip to content

Commit 70128e0

Browse files
committed
fix stats aggregator tests
With #60683 we stopped forcing aggregating all docs using a single Aggregator which made some of our accuracy assumptions about the stats aggregator incorrect. This adds a test that does the forcing and asserts the old accuracy and adds a test without the forcing with much looser accuracy guarantees. Closes #61132
1 parent b1aa0d8 commit 70128e0

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

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

+34-9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import static java.util.Collections.emptyMap;
6262
import static java.util.Collections.emptySet;
6363
import static java.util.Collections.singleton;
64+
import static java.util.Collections.singletonList;
6465
import static java.util.Collections.singletonMap;
6566
import static java.util.stream.Collectors.toList;
6667
import static java.util.stream.Collectors.toSet;
@@ -140,7 +141,7 @@ public void testRandomLongs() throws IOException {
140141
public void testSummationAccuracy() throws IOException {
141142
// Summing up a normal array and expect an accurate value
142143
double[] values = new double[]{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7};
143-
verifySummationOfDoubles(values, 15.3, 0.9, 0d);
144+
verifySummationOfDoubles(values, 15.3, 0.9, 0d, values.length * TOLERANCE);
144145

145146
// Summing up an array which contains NaN and infinities and expect a result same as naive summation
146147
int n = randomIntBetween(5, 10);
@@ -152,24 +153,29 @@ public void testSummationAccuracy() throws IOException {
152153
: randomDoubleBetween(Double.MIN_VALUE, Double.MAX_VALUE, true);
153154
sum += values[i];
154155
}
155-
verifySummationOfDoubles(values, sum, sum / n, TOLERANCE);
156+
verifySummationOfDoubles(values, sum, sum / n, TOLERANCE, n * TOLERANCE);
156157

157158
// Summing up some big double values and expect infinity result
158159
n = randomIntBetween(5, 10);
159160
double[] largeValues = new double[n];
160161
for (int i = 0; i < n; i++) {
161162
largeValues[i] = Double.MAX_VALUE;
162163
}
163-
verifySummationOfDoubles(largeValues, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 0d);
164+
verifySummationOfDoubles(largeValues, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 0d, 0d);
164165

165166
for (int i = 0; i < n; i++) {
166167
largeValues[i] = -Double.MAX_VALUE;
167168
}
168-
verifySummationOfDoubles(largeValues, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 0d);
169+
verifySummationOfDoubles(largeValues, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 0d, 0d);
169170
}
170171

171-
private void verifySummationOfDoubles(double[] values, double expectedSum,
172-
double expectedAvg, double delta) throws IOException {
172+
private void verifySummationOfDoubles(
173+
double[] values,
174+
double expectedSum,
175+
double expectedAvg,
176+
double singleSegmentDelta,
177+
double manySegmentDelta
178+
) throws IOException {
173179
MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.DOUBLE);
174180

175181
double max = Double.NEGATIVE_INFINITY;
@@ -183,14 +189,33 @@ private void verifySummationOfDoubles(double[] values, double expectedSum,
183189
testCase(
184190
stats("_name").field(ft.name()),
185191
iw -> {
192+
List<List<NumericDocValuesField>> docs = new ArrayList<>();
186193
for (double value : values) {
187-
iw.addDocument(singleton(new NumericDocValuesField(ft.name(), NumericUtils.doubleToSortableLong(value))));
194+
docs.add(singletonList(new NumericDocValuesField(ft.name(), NumericUtils.doubleToSortableLong(value))));
188195
}
196+
iw.addDocuments(docs);
189197
},
190198
stats -> {
191199
assertEquals(values.length, stats.getCount());
192-
assertEquals(expectedAvg, stats.getAvg(), delta);
193-
assertEquals(expectedSum, stats.getSum(), delta);
200+
assertEquals(expectedAvg, stats.getAvg(), singleSegmentDelta);
201+
assertEquals(expectedSum, stats.getSum(), singleSegmentDelta);
202+
assertEquals(expectedMax, stats.getMax(), 0d);
203+
assertEquals(expectedMin, stats.getMin(), 0d);
204+
assertTrue(AggregationInspectionHelper.hasValue(stats));
205+
},
206+
singleton(ft)
207+
);
208+
testCase(
209+
stats("_name").field(ft.name()),
210+
iw -> {
211+
for (double value : values) {
212+
iw.addDocument(singletonList(new NumericDocValuesField(ft.name(), NumericUtils.doubleToSortableLong(value))));
213+
}
214+
},
215+
stats -> {
216+
assertEquals(values.length, stats.getCount());
217+
assertEquals(expectedAvg, stats.getAvg(), manySegmentDelta);
218+
assertEquals(expectedSum, stats.getSum(), manySegmentDelta);
194219
assertEquals(expectedMax, stats.getMax(), 0d);
195220
assertEquals(expectedMin, stats.getMin(), 0d);
196221
assertTrue(AggregationInspectionHelper.hasValue(stats));

0 commit comments

Comments
 (0)