Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <[email protected]>
  • Loading branch information
sarthakaggarwal97 committed Aug 6, 2024
1 parent c7f01c8 commit 406c6d6
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @opensearch.experimental
*/
public class CountValueAggregator implements ValueAggregator<Long> {
class CountValueAggregator implements ValueAggregator<Long> {

public static final long DEFAULT_INITIAL_VALUE = 1L;
private final StarTreeNumericType starTreeNumericType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @opensearch.experimental
*/
public class MaxValueAggregator extends StatelessDoubleValueAggregator {
class MaxValueAggregator extends StatelessDoubleValueAggregator {

public MaxValueAggregator(StarTreeNumericType starTreeNumericType) {
super(starTreeNumericType, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @opensearch.experimental
*/
public class MinValueAggregator extends StatelessDoubleValueAggregator {
class MinValueAggregator extends StatelessDoubleValueAggregator {

public MinValueAggregator(StarTreeNumericType starTreeNumericType) {
super(starTreeNumericType, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @opensearch.experimental
*/
public abstract class StatelessDoubleValueAggregator implements ValueAggregator<Double> {
abstract class StatelessDoubleValueAggregator implements ValueAggregator<Double> {

protected final StarTreeNumericType starTreeNumericType;
protected final Double identityValue;
Expand Down Expand Up @@ -64,7 +64,7 @@ public Double getIdentityMetricValue() {
}

/**
* Performs min or max aggregation on the value and the segmentDocValue based on the implementation
* Performs stateless aggregation on the value and the segmentDocValue based on the implementation
*
* @param aggregatedValue aggregated value for the segment so far
* @param segmentDocValue current segment doc value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
*
* @opensearch.experimental
*/
public class SumValueAggregator implements ValueAggregator<Double> {
class SumValueAggregator implements ValueAggregator<Double> {

private final StarTreeNumericType starTreeNumericType;

private double sum = 0;
private double compensation = 0;
private CompensatedSum kahanSummation = new CompensatedSum(0, 0);

public SumValueAggregator(StarTreeNumericType starTreeNumericType) {
Expand All @@ -35,13 +33,12 @@ public SumValueAggregator(StarTreeNumericType starTreeNumericType) {
@Override
public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue) {
kahanSummation.reset(0, 0);
// add takes care of the sum and compensation internally
if (segmentDocValue != null) {
kahanSummation.add(starTreeNumericType.getDoubleValue(segmentDocValue));
} else {
kahanSummation.add(getIdentityMetricValue());
}
compensation = kahanSummation.delta();
sum = kahanSummation.value();
return kahanSummation.value();
}

Expand All @@ -50,41 +47,36 @@ public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue)
@Override
public Double mergeAggregatedValueAndSegmentValue(Double value, Long segmentDocValue) {
assert value == null || kahanSummation.value() == value;
kahanSummation.reset(sum, compensation);
// add takes care of the sum and compensation internally
if (segmentDocValue != null) {
kahanSummation.add(starTreeNumericType.getDoubleValue(segmentDocValue));
} else {
kahanSummation.add(getIdentityMetricValue());
}
compensation = kahanSummation.delta();
sum = kahanSummation.value();
return kahanSummation.value();
}

@Override
public Double mergeAggregatedValues(Double value, Double aggregatedValue) {
assert aggregatedValue == null || kahanSummation.value() == aggregatedValue;
kahanSummation.reset(sum, compensation);
// add takes care of the sum and compensation internally
if (value != null) {
kahanSummation.add(value);
} else {
kahanSummation.add(getIdentityMetricValue());
}
compensation = kahanSummation.delta();
sum = kahanSummation.value();
return kahanSummation.value();
}

@Override
public Double getInitialAggregatedValue(Double value) {
kahanSummation.reset(0, 0);
// add takes care of the sum and compensation internally
if (value != null) {
kahanSummation.add(value);
} else {
kahanSummation.add(getIdentityMetricValue());
}
compensation = kahanSummation.delta();
sum = kahanSummation.value();
return kahanSummation.value();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.apache.lucene.util.NumericUtils;
import org.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;
import org.opensearch.search.aggregations.metrics.CompensatedSum;
import org.opensearch.test.OpenSearchTestCase;

public class StaticValueAggregatorTests extends OpenSearchTestCase {
Expand Down Expand Up @@ -46,15 +47,11 @@ private static double getAggregatedValue(double[] numbers) {
}

private double kahanSum(double[] numbers) {
double sum = 0.0;
double c = 0.0;
CompensatedSum compensatedSum = new CompensatedSum(0, 0);
for (double num : numbers) {
double y = num - c;
double t = sum + y;
c = (t - sum) - y;
sum = t;
compensatedSum.add(num);
}
return sum;
return compensatedSum.value();
}

private double normalSum(double[] numbers) {
Expand Down

0 comments on commit 406c6d6

Please sign in to comment.