Skip to content

Commit

Permalink
min and max star tree aggregators
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
  • Loading branch information
sarthakaggarwal97 committed Jul 9, 2024
1 parent fc772a4 commit e991606
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.apache.lucene.util.NumericUtils;
import org.opensearch.index.compositeindex.datacube.MetricStat;
import org.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;

/**
* Max value aggregator for star tree
*
* @opensearch.experimental
*/
public class MaxValueAggregator implements ValueAggregator<Double> {

public static final StarTreeNumericType VALUE_AGGREGATOR_TYPE = StarTreeNumericType.DOUBLE;

@Override
public MetricStat getAggregationType() {
return MetricStat.MAX;
}

@Override
public StarTreeNumericType getAggregatedValueType() {
return VALUE_AGGREGATOR_TYPE;
}

@Override
public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue, StarTreeNumericType starTreeNumericType) {
return starTreeNumericType.getDoubleValue(segmentDocValue);
}

@Override
public Double mergeAggregatedValueAndSegmentValue(Double value, Long segmentDocValue, StarTreeNumericType starTreeNumericType) {
return Math.max(value, starTreeNumericType.getDoubleValue(segmentDocValue));
}

@Override
public Double mergeAggregatedValues(Double value, Double aggregatedValue) {
return Math.max(value, aggregatedValue);
}

@Override
public Double getInitialAggregatedValue(Double value) {
return value;
}

@Override
public int getMaxAggregatedValueByteSize() {
return Double.BYTES;
}

@Override
public Long toLongValue(Double value) {
try {
return NumericUtils.doubleToSortableLong(value);
} catch (Exception e) {
throw new IllegalStateException("Cannot convert " + value + " to sortable long", e);
}
}

@Override
public Double toStarTreeNumericTypeValue(Long value, StarTreeNumericType type) {
try {
return type.getDoubleValue(value);
} catch (Exception e) {
throw new IllegalStateException("Cannot convert " + value + " to sortable aggregation type", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.apache.lucene.util.NumericUtils;
import org.opensearch.index.compositeindex.datacube.MetricStat;
import org.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;

/**
* Min value aggregator for star tree
*
* @opensearch.experimental
*/
public class MinValueAggregator implements ValueAggregator<Double> {

public static final StarTreeNumericType VALUE_AGGREGATOR_TYPE = StarTreeNumericType.DOUBLE;

@Override
public MetricStat getAggregationType() {
return MetricStat.MIN;
}

@Override
public StarTreeNumericType getAggregatedValueType() {
return VALUE_AGGREGATOR_TYPE;
}

@Override
public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue, StarTreeNumericType starTreeNumericType) {
return starTreeNumericType.getDoubleValue(segmentDocValue);
}

@Override
public Double mergeAggregatedValueAndSegmentValue(Double value, Long segmentDocValue, StarTreeNumericType starTreeNumericType) {
return Math.min(value, starTreeNumericType.getDoubleValue(segmentDocValue));
}

@Override
public Double mergeAggregatedValues(Double value, Double aggregatedValue) {
return Math.min(value, aggregatedValue);
}

@Override
public Double getInitialAggregatedValue(Double value) {
return value;
}

@Override
public int getMaxAggregatedValueByteSize() {
return Double.BYTES;
}

@Override
public Long toLongValue(Double value) {
try {
return NumericUtils.doubleToSortableLong(value);
} catch (Exception e) {
throw new IllegalStateException("Cannot convert " + value + " to sortable long", e);
}
}

@Override
public Double toStarTreeNumericTypeValue(Long value, StarTreeNumericType type) {
try {
return type.getDoubleValue(value);
} catch (Exception e) {
throw new IllegalStateException("Cannot convert " + value + " to sortable aggregation type", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ private ValueAggregatorFactory() {}
*/
public static ValueAggregator getValueAggregator(MetricStat aggregationType) {
switch (aggregationType) {
// other metric types (count, min, max, avg) will be supported in the future
// avg aggregator will be covered in the part of query (using count and sum)
case SUM:
return new SumValueAggregator();
case COUNT:
return new CountValueAggregator();
case MIN:
return new MinValueAggregator();
case MAX:
return new MaxValueAggregator();
default:
throw new IllegalStateException("Unsupported aggregation type: " + aggregationType);
}
Expand All @@ -49,6 +53,10 @@ public static StarTreeNumericType getAggregatedValueType(MetricStat aggregationT
return SumValueAggregator.VALUE_AGGREGATOR_TYPE;
case COUNT:
return CountValueAggregator.VALUE_AGGREGATOR_TYPE;
case MIN:
return MinValueAggregator.VALUE_AGGREGATOR_TYPE;
case MAX:
return MaxValueAggregator.VALUE_AGGREGATOR_TYPE;
default:
throw new IllegalStateException("Unsupported aggregation type: " + aggregationType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.apache.lucene.util.NumericUtils;
import org.opensearch.index.compositeindex.datacube.MetricStat;
import org.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;
import org.opensearch.test.OpenSearchTestCase;

public class MaxValueAggregatorTests extends OpenSearchTestCase {
private final MaxValueAggregator aggregator = new MaxValueAggregator();

public void testGetAggregationType() {
assertEquals(MetricStat.MAX.getTypeName(), aggregator.getAggregationType().getTypeName());
}

public void testGetAggregatedValueType() {
assertEquals(MaxValueAggregator.VALUE_AGGREGATOR_TYPE, aggregator.getAggregatedValueType());
}

public void testGetInitialAggregatedValueForSegmentDocValue() {
assertEquals(1.0, aggregator.getInitialAggregatedValueForSegmentDocValue(1L, StarTreeNumericType.LONG), 0.0);
assertThrows(
NullPointerException.class,
() -> aggregator.getInitialAggregatedValueForSegmentDocValue(null, StarTreeNumericType.DOUBLE)
);
}

public void testMergeAggregatedValueAndSegmentValue() {
assertEquals(3.0, aggregator.mergeAggregatedValueAndSegmentValue(2.0, 3L, StarTreeNumericType.LONG), 0.0);
}

public void testMergeAggregatedValues() {
assertEquals(3.0, aggregator.mergeAggregatedValues(2.0, 3.0), 0.0);
}

public void testGetInitialAggregatedValue() {
assertEquals(3.0, aggregator.getInitialAggregatedValue(3.0), 0.0);
}

public void testGetMaxAggregatedValueByteSize() {
assertEquals(Double.BYTES, aggregator.getMaxAggregatedValueByteSize());
}

public void testToLongValue() {
assertEquals(NumericUtils.doubleToSortableLong(3.0), aggregator.toLongValue(3.0), 0.0);
}

public void testToStarTreeNumericTypeValue() {
assertEquals(NumericUtils.sortableLongToDouble(3L), aggregator.toStarTreeNumericTypeValue(3L, StarTreeNumericType.DOUBLE), 0.0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.apache.lucene.util.NumericUtils;
import org.opensearch.index.compositeindex.datacube.MetricStat;
import org.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;
import org.opensearch.test.OpenSearchTestCase;

public class MinValueAggregatorTests extends OpenSearchTestCase {
private final MinValueAggregator aggregator = new MinValueAggregator();

public void testGetAggregationType() {
assertEquals(MetricStat.MIN.getTypeName(), aggregator.getAggregationType().getTypeName());
}

public void testGetAggregatedValueType() {
assertEquals(MinValueAggregator.VALUE_AGGREGATOR_TYPE, aggregator.getAggregatedValueType());
}

public void testGetInitialAggregatedValueForSegmentDocValue() {
assertEquals(1.0, aggregator.getInitialAggregatedValueForSegmentDocValue(1L, StarTreeNumericType.LONG), 0.0);
assertThrows(
NullPointerException.class,
() -> aggregator.getInitialAggregatedValueForSegmentDocValue(null, StarTreeNumericType.DOUBLE)
);
}

public void testMergeAggregatedValueAndSegmentValue() {
assertEquals(2.0, aggregator.mergeAggregatedValueAndSegmentValue(2.0, 3L, StarTreeNumericType.LONG), 0.0);
}

public void testMergeAggregatedValues() {
assertEquals(2.0, aggregator.mergeAggregatedValues(2.0, 3.0), 0.0);
}

public void testGetInitialAggregatedValue() {
assertEquals(3.0, aggregator.getInitialAggregatedValue(3.0), 0.0);
}

public void testGetMaxAggregatedValueByteSize() {
assertEquals(Double.BYTES, aggregator.getMaxAggregatedValueByteSize());
}

public void testToLongValue() {
assertEquals(NumericUtils.doubleToSortableLong(3.0), aggregator.toLongValue(3.0), 0.0);
}

public void testToStarTreeNumericTypeValue() {
assertEquals(NumericUtils.sortableLongToDouble(3L), aggregator.toStarTreeNumericTypeValue(3L, StarTreeNumericType.DOUBLE), 0.0);
}
}
Loading

0 comments on commit e991606

Please sign in to comment.