|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | +package org.opensearch.index.compositeindex.datacube.startree.aggregators; |
| 9 | + |
| 10 | +import org.apache.lucene.util.NumericUtils; |
| 11 | +import org.opensearch.index.compositeindex.datacube.MetricStat; |
| 12 | +import org.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType; |
| 13 | +import org.opensearch.search.aggregations.metrics.CompensatedSum; |
| 14 | + |
| 15 | +/** |
| 16 | + * Sum value aggregator for star tree |
| 17 | + * |
| 18 | + * @opensearch.experimental |
| 19 | + */ |
| 20 | +public class SumValueAggregator implements ValueAggregator<Double> { |
| 21 | + |
| 22 | + public static final StarTreeNumericType VALUE_AGGREGATOR_TYPE = StarTreeNumericType.DOUBLE; |
| 23 | + private double sum = 0; |
| 24 | + private double compensation = 0; |
| 25 | + private CompensatedSum kahanSummation = new CompensatedSum(0, 0); |
| 26 | + |
| 27 | + @Override |
| 28 | + public MetricStat getAggregationType() { |
| 29 | + return MetricStat.SUM; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public StarTreeNumericType getAggregatedValueType() { |
| 34 | + return VALUE_AGGREGATOR_TYPE; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue, StarTreeNumericType starTreeNumericType) { |
| 39 | + kahanSummation.reset(0, 0); |
| 40 | + kahanSummation.add(starTreeNumericType.getDoubleValue(segmentDocValue)); |
| 41 | + compensation = kahanSummation.delta(); |
| 42 | + sum = kahanSummation.value(); |
| 43 | + return kahanSummation.value(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Double mergeAggregatedValueAndSegmentValue(Double value, Long segmentDocValue, StarTreeNumericType starTreeNumericType) { |
| 48 | + assert kahanSummation.value() == value; |
| 49 | + kahanSummation.reset(sum, compensation); |
| 50 | + kahanSummation.add(starTreeNumericType.getDoubleValue(segmentDocValue)); |
| 51 | + compensation = kahanSummation.delta(); |
| 52 | + sum = kahanSummation.value(); |
| 53 | + return kahanSummation.value(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Double mergeAggregatedValues(Double value, Double aggregatedValue) { |
| 58 | + assert kahanSummation.value() == aggregatedValue; |
| 59 | + kahanSummation.reset(sum, compensation); |
| 60 | + kahanSummation.add(value); |
| 61 | + compensation = kahanSummation.delta(); |
| 62 | + sum = kahanSummation.value(); |
| 63 | + return kahanSummation.value(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Double getInitialAggregatedValue(Double value) { |
| 68 | + kahanSummation.reset(0, 0); |
| 69 | + kahanSummation.add(value); |
| 70 | + compensation = kahanSummation.delta(); |
| 71 | + sum = kahanSummation.value(); |
| 72 | + return kahanSummation.value(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int getMaxAggregatedValueByteSize() { |
| 77 | + return Double.BYTES; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Long toLongValue(Double value) { |
| 82 | + try { |
| 83 | + return NumericUtils.doubleToSortableLong(value); |
| 84 | + } catch (Exception e) { |
| 85 | + throw new IllegalStateException("Cannot convert " + value + " to sortable long", e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Double toStarTreeNumericTypeValue(Long value, StarTreeNumericType type) { |
| 91 | + try { |
| 92 | + return type.getDoubleValue(value); |
| 93 | + } catch (Exception e) { |
| 94 | + throw new IllegalStateException("Cannot convert " + value + " to sortable aggregation type", e); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments