61
61
import static java .util .Collections .emptyMap ;
62
62
import static java .util .Collections .emptySet ;
63
63
import static java .util .Collections .singleton ;
64
+ import static java .util .Collections .singletonList ;
64
65
import static java .util .Collections .singletonMap ;
65
66
import static java .util .stream .Collectors .toList ;
66
67
import static java .util .stream .Collectors .toSet ;
@@ -140,7 +141,7 @@ public void testRandomLongs() throws IOException {
140
141
public void testSummationAccuracy () throws IOException {
141
142
// Summing up a normal array and expect an accurate value
142
143
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 );
144
145
145
146
// Summing up an array which contains NaN and infinities and expect a result same as naive summation
146
147
int n = randomIntBetween (5 , 10 );
@@ -152,24 +153,29 @@ public void testSummationAccuracy() throws IOException {
152
153
: randomDoubleBetween (Double .MIN_VALUE , Double .MAX_VALUE , true );
153
154
sum += values [i ];
154
155
}
155
- verifySummationOfDoubles (values , sum , sum / n , TOLERANCE );
156
+ verifySummationOfDoubles (values , sum , sum / n , TOLERANCE , n * TOLERANCE );
156
157
157
158
// Summing up some big double values and expect infinity result
158
159
n = randomIntBetween (5 , 10 );
159
160
double [] largeValues = new double [n ];
160
161
for (int i = 0 ; i < n ; i ++) {
161
162
largeValues [i ] = Double .MAX_VALUE ;
162
163
}
163
- verifySummationOfDoubles (largeValues , Double .POSITIVE_INFINITY , Double .POSITIVE_INFINITY , 0d );
164
+ verifySummationOfDoubles (largeValues , Double .POSITIVE_INFINITY , Double .POSITIVE_INFINITY , 0d , 0d );
164
165
165
166
for (int i = 0 ; i < n ; i ++) {
166
167
largeValues [i ] = -Double .MAX_VALUE ;
167
168
}
168
- verifySummationOfDoubles (largeValues , Double .NEGATIVE_INFINITY , Double .NEGATIVE_INFINITY , 0d );
169
+ verifySummationOfDoubles (largeValues , Double .NEGATIVE_INFINITY , Double .NEGATIVE_INFINITY , 0d , 0d );
169
170
}
170
171
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 {
173
179
MappedFieldType ft = new NumberFieldMapper .NumberFieldType ("field" , NumberType .DOUBLE );
174
180
175
181
double max = Double .NEGATIVE_INFINITY ;
@@ -183,14 +189,33 @@ private void verifySummationOfDoubles(double[] values, double expectedSum,
183
189
testCase (
184
190
stats ("_name" ).field (ft .name ()),
185
191
iw -> {
192
+ List <List <NumericDocValuesField >> docs = new ArrayList <>();
186
193
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 ))));
188
195
}
196
+ iw .addDocuments (docs );
189
197
},
190
198
stats -> {
191
199
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 );
194
219
assertEquals (expectedMax , stats .getMax (), 0d );
195
220
assertEquals (expectedMin , stats .getMin (), 0d );
196
221
assertTrue (AggregationInspectionHelper .hasValue (stats ));
0 commit comments