6060import  static  java .util .Collections .emptyMap ;
6161import  static  java .util .Collections .emptySet ;
6262import  static  java .util .Collections .singleton ;
63+ import  static  java .util .Collections .singletonList ;
6364import  static  java .util .Collections .singletonMap ;
6465import  static  java .util .stream .Collectors .toList ;
6566import  static  java .util .stream .Collectors .toSet ;
@@ -139,7 +140,7 @@ public void testRandomLongs() throws IOException {
139140    public  void  testSummationAccuracy () throws  IOException  {
140141        // Summing up a normal array and expect an accurate value 
141142        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 };
142-         verifySummationOfDoubles (values , 15.3 , 0.9 , 0d );
143+         verifySummationOfDoubles (values , 15.3 , 0.9 , 0d ,  values . length  *  TOLERANCE );
143144
144145        // Summing up an array which contains NaN and infinities and expect a result same as naive summation 
145146        int  n  = randomIntBetween (5 , 10 );
@@ -151,24 +152,29 @@ public void testSummationAccuracy() throws IOException {
151152                : randomDoubleBetween (Double .MIN_VALUE , Double .MAX_VALUE , true );
152153            sum  += values [i ];
153154        }
154-         verifySummationOfDoubles (values , sum , sum  / n , TOLERANCE );
155+         verifySummationOfDoubles (values , sum , sum  / n , TOLERANCE ,  n  *  TOLERANCE );
155156
156157        // Summing up some big double values and expect infinity result 
157158        n  = randomIntBetween (5 , 10 );
158159        double [] largeValues  = new  double [n ];
159160        for  (int  i  = 0 ; i  < n ; i ++) {
160161            largeValues [i ] = Double .MAX_VALUE ;
161162        }
162-         verifySummationOfDoubles (largeValues , Double .POSITIVE_INFINITY , Double .POSITIVE_INFINITY , 0d );
163+         verifySummationOfDoubles (largeValues , Double .POSITIVE_INFINITY , Double .POSITIVE_INFINITY , 0d ,  0d );
163164
164165        for  (int  i  = 0 ; i  < n ; i ++) {
165166            largeValues [i ] = -Double .MAX_VALUE ;
166167        }
167-         verifySummationOfDoubles (largeValues , Double .NEGATIVE_INFINITY , Double .NEGATIVE_INFINITY , 0d );
168+         verifySummationOfDoubles (largeValues , Double .NEGATIVE_INFINITY , Double .NEGATIVE_INFINITY , 0d ,  0d );
168169    }
169170
170-     private  void  verifySummationOfDoubles (double [] values , double  expectedSum ,
171-                                           double  expectedAvg , double  delta ) throws  IOException  {
171+     private  void  verifySummationOfDoubles (
172+         double [] values ,
173+         double  expectedSum ,
174+         double  expectedAvg ,
175+         double  singleSegmentDelta ,
176+         double  manySegmentDelta 
177+     ) throws  IOException  {
172178        MappedFieldType  ft  = new  NumberFieldMapper .NumberFieldType ("field" , NumberType .DOUBLE );
173179
174180        double  max  = Double .NEGATIVE_INFINITY ;
@@ -182,14 +188,33 @@ private void verifySummationOfDoubles(double[] values, double expectedSum,
182188        testCase (
183189            stats ("_name" ).field (ft .name ()),
184190            iw  -> {
191+                 List <List <NumericDocValuesField >> docs  = new  ArrayList <>();
185192                for  (double  value  : values ) {
186-                     iw . addDocument ( singleton (new  NumericDocValuesField (ft .name (), NumericUtils .doubleToSortableLong (value ))));
193+                     docs . add ( singletonList (new  NumericDocValuesField (ft .name (), NumericUtils .doubleToSortableLong (value ))));
187194                }
195+                 iw .addDocuments (docs );
188196            },
189197            stats  -> {
190198                assertEquals (values .length , stats .getCount ());
191-                 assertEquals (expectedAvg , stats .getAvg (), delta );
192-                 assertEquals (expectedSum , stats .getSum (), delta );
199+                 assertEquals (expectedAvg , stats .getAvg (), singleSegmentDelta );
200+                 assertEquals (expectedSum , stats .getSum (), singleSegmentDelta );
201+                 assertEquals (expectedMax , stats .getMax (), 0d );
202+                 assertEquals (expectedMin , stats .getMin (), 0d );
203+                 assertTrue (AggregationInspectionHelper .hasValue (stats ));
204+             },
205+             singleton (ft )
206+         );
207+         testCase (
208+             stats ("_name" ).field (ft .name ()),
209+             iw  -> {
210+                 for  (double  value  : values ) {
211+                     iw .addDocument (singletonList (new  NumericDocValuesField (ft .name (), NumericUtils .doubleToSortableLong (value ))));
212+                 }
213+             },
214+             stats  -> {
215+                 assertEquals (values .length , stats .getCount ());
216+                 assertEquals (expectedAvg , stats .getAvg (), manySegmentDelta );
217+                 assertEquals (expectedSum , stats .getSum (), manySegmentDelta );
193218                assertEquals (expectedMax , stats .getMax (), 0d );
194219                assertEquals (expectedMin , stats .getMin (), 0d );
195220                assertTrue (AggregationInspectionHelper .hasValue (stats ));
0 commit comments