-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Calculate sum in Kahan summation algorithm in aggregations (#27807) #27848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
30f7228
Calculate sum in Kahan summation algorithm in aggregations (#27807)
liketic 6c45e08
Compute summation with Kahan summation algorithm for internal aggrega…
liketic 95aff79
Merge remote-tracking branch 'upstream/master' into feature/issues/27807
liketic 15cdec2
Properly calculate sum of NaN and infinities
liketic 1f07cca
Simplify summing up algorithm for aggregations
liketic 594f750
Merge remote-tracking branch 'upstream/master' into feature/issues/27807
liketic b2ce7cb
Merge branch 'master' of https://github.com/elastic/elasticsearch int…
liketic 176be1f
Add test case for summing up NaN and infinities
liketic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,8 @@ public class AvgAggregator extends NumericMetricsAggregator.SingleValue { | |
| DoubleArray sums; | ||
| DocValueFormat format; | ||
|
|
||
| private DoubleArray compensations; | ||
|
|
||
| public AvgAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat formatter, SearchContext context, | ||
| Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { | ||
| super(name, context, parent, pipelineAggregators, metaData); | ||
|
|
@@ -55,6 +57,7 @@ public AvgAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFor | |
| final BigArrays bigArrays = context.bigArrays(); | ||
| counts = bigArrays.newLongArray(1, true); | ||
| sums = bigArrays.newDoubleArray(1, true); | ||
| compensations = bigArrays.newDoubleArray(1, true); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -76,15 +79,22 @@ public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, | |
| public void collect(int doc, long bucket) throws IOException { | ||
| counts = bigArrays.grow(counts, bucket + 1); | ||
| sums = bigArrays.grow(sums, bucket + 1); | ||
| compensations = bigArrays.grow(compensations, bucket + 1); | ||
|
|
||
| if (values.advanceExact(doc)) { | ||
| final int valueCount = values.docValueCount(); | ||
| counts.increment(bucket, valueCount); | ||
| double sum = 0; | ||
| double sum = sums.get(bucket); | ||
| double compensation = compensations.get(bucket); | ||
|
|
||
| for (int i = 0; i < valueCount; i++) { | ||
| sum += values.nextValue(); | ||
| double corrected = values.nextValue() - compensation; | ||
| double newSum = sum + corrected; | ||
| compensation = (newSum - sum) - corrected; | ||
| sum = newSum; | ||
|
||
| } | ||
| sums.increment(bucket, sum); | ||
| sums.set(bucket, sum); | ||
| compensations.set(bucket, compensation); | ||
| } | ||
| } | ||
| }; | ||
|
|
@@ -113,7 +123,7 @@ public InternalAggregation buildEmptyAggregation() { | |
|
|
||
| @Override | ||
| public void doClose() { | ||
| Releasables.close(counts, sums); | ||
| Releasables.close(counts, sums, compensations); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please declare it next to
sumsand using the same modifiers