@@ -67,18 +67,33 @@ public void collect(int doc, long bucket) throws IOException {
6767
6868 @ Override
6969 protected LeafBucketCollector getLeafCollector (NumericDoubleValues values , final LeafBucketCollector sub ) {
70- final CompensatedSum kahanSummation = new CompensatedSum (0 , 0 );
7170 return new LeafBucketCollectorBase (sub , values ) {
7271 @ Override
7372 public void collect (int doc , long bucket ) throws IOException {
7473 if (values .advanceExact (doc )) {
7574 maybeGrow (bucket );
75+ var sums = SumAggregator .this .sums ;
7676 // Compute the sum of double values with Kahan summation algorithm which is more
7777 // accurate than naive summation.
78- kahanSummation .reset (sums .get (bucket ), compensations .get (bucket ));
79- kahanSummation .add (values .doubleValue ());
80- compensations .set (bucket , kahanSummation .delta ());
81- sums .set (bucket , kahanSummation .value ());
78+ double value = sums .get (bucket );
79+ // If the value is Inf or NaN, just add it to the running tally to "convert" to
80+ // Inf/NaN. This keeps the behavior bwc from before kahan summing
81+ double v = values .doubleValue ();
82+ if (Double .isFinite (v ) == false ) {
83+ value = v + value ;
84+ }
85+
86+ var compensations = SumAggregator .this .compensations ;
87+ double delta = compensations .get (bucket );
88+ if (Double .isFinite (value )) {
89+ double correctedSum = v + delta ;
90+ double updatedValue = value + correctedSum ;
91+ delta = correctedSum - (updatedValue - value );
92+ value = updatedValue ;
93+ compensations .set (bucket , delta );
94+ }
95+
96+ sums .set (bucket , value );
8297 }
8398 }
8499 };
0 commit comments