forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for histogram fields to rate aggregation (elastic#63289)
The rate aggregation now supports histogram fields. At the moment only sum is supported. Closes elastic#62939
- Loading branch information
Showing
7 changed files
with
232 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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
62 changes: 62 additions & 0 deletions
62
...alytics/src/main/java/org/elasticsearch/xpack/analytics/rate/HistogramRateAggregator.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.analytics.rate; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.common.Rounding; | ||
import org.elasticsearch.common.util.BigArrays; | ||
import org.elasticsearch.index.fielddata.HistogramValue; | ||
import org.elasticsearch.index.fielddata.HistogramValues; | ||
import org.elasticsearch.search.aggregations.Aggregator; | ||
import org.elasticsearch.search.aggregations.LeafBucketCollector; | ||
import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; | ||
import org.elasticsearch.search.aggregations.metrics.CompensatedSum; | ||
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; | ||
import org.elasticsearch.search.internal.SearchContext; | ||
import org.elasticsearch.xpack.analytics.aggregations.support.HistogramValuesSource; | ||
|
||
public class HistogramRateAggregator extends AbstractRateAggregator { | ||
public HistogramRateAggregator( | ||
String name, | ||
ValuesSourceConfig valuesSourceConfig, | ||
Rounding.DateTimeUnit rateUnit, | ||
SearchContext context, | ||
Aggregator parent, | ||
Map<String, Object> metadata | ||
) throws IOException { | ||
super(name, valuesSourceConfig, rateUnit, context, parent, metadata); | ||
} | ||
|
||
@Override | ||
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException { | ||
final BigArrays bigArrays = context.bigArrays(); | ||
final CompensatedSum kahanSummation = new CompensatedSum(0, 0); | ||
final HistogramValues values = ((HistogramValuesSource.Histogram) valuesSource).getHistogramValues(ctx); | ||
return new LeafBucketCollectorBase(sub, values) { | ||
@Override | ||
public void collect(int doc, long bucket) throws IOException { | ||
sums = bigArrays.grow(sums, bucket + 1); | ||
compensations = bigArrays.grow(compensations, bucket + 1); | ||
|
||
if (values.advanceExact(doc)) { | ||
final HistogramValue sketch = values.histogram(); | ||
while (sketch.next()) { | ||
double sum = sums.get(bucket); | ||
double compensation = compensations.get(bucket); | ||
kahanSummation.reset(sum, compensation); | ||
kahanSummation.add(sketch.value()); | ||
compensations.set(bucket, kahanSummation.delta()); | ||
sums.set(bucket, kahanSummation.value()); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...analytics/src/main/java/org/elasticsearch/xpack/analytics/rate/NumericRateAggregator.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.analytics.rate; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.common.Rounding; | ||
import org.elasticsearch.common.util.BigArrays; | ||
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; | ||
import org.elasticsearch.search.aggregations.Aggregator; | ||
import org.elasticsearch.search.aggregations.LeafBucketCollector; | ||
import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; | ||
import org.elasticsearch.search.aggregations.metrics.CompensatedSum; | ||
import org.elasticsearch.search.aggregations.support.ValuesSource; | ||
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; | ||
import org.elasticsearch.search.internal.SearchContext; | ||
|
||
public class NumericRateAggregator extends AbstractRateAggregator { | ||
public NumericRateAggregator( | ||
String name, | ||
ValuesSourceConfig valuesSourceConfig, | ||
Rounding.DateTimeUnit rateUnit, | ||
SearchContext context, | ||
Aggregator parent, | ||
Map<String, Object> metadata | ||
) throws IOException { | ||
super(name, valuesSourceConfig, rateUnit, context, parent, metadata); | ||
} | ||
|
||
@Override | ||
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException { | ||
final BigArrays bigArrays = context.bigArrays(); | ||
final CompensatedSum kahanSummation = new CompensatedSum(0, 0); | ||
final SortedNumericDoubleValues values = ((ValuesSource.Numeric) valuesSource).doubleValues(ctx); | ||
return new LeafBucketCollectorBase(sub, values) { | ||
@Override | ||
public void collect(int doc, long bucket) throws IOException { | ||
sums = bigArrays.grow(sums, bucket + 1); | ||
compensations = bigArrays.grow(compensations, bucket + 1); | ||
|
||
if (values.advanceExact(doc)) { | ||
final int valuesCount = values.docValueCount(); | ||
// Compute the sum of double values with Kahan summation algorithm which is more | ||
// accurate than naive summation. | ||
double sum = sums.get(bucket); | ||
double compensation = compensations.get(bucket); | ||
kahanSummation.reset(sum, compensation); | ||
|
||
for (int i = 0; i < valuesCount; i++) { | ||
double value = values.nextValue(); | ||
kahanSummation.add(value); | ||
} | ||
|
||
compensations.set(bucket, kahanSummation.delta()); | ||
sums.set(bucket, kahanSummation.value()); | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.