Skip to content

Commit 2d8e244

Browse files
min and max star tree aggregators
Signed-off-by: Sarthak Aggarwal <[email protected]>
1 parent 367fb64 commit 2d8e244

File tree

6 files changed

+489
-130
lines changed

6 files changed

+489
-130
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
14+
/**
15+
* Max value aggregator for star tree
16+
*
17+
* @opensearch.experimental
18+
*/
19+
public class MaxValueAggregator implements ValueAggregator<Double> {
20+
21+
public static final StarTreeNumericType VALUE_AGGREGATOR_TYPE = StarTreeNumericType.DOUBLE;
22+
23+
@Override
24+
public MetricStat getAggregationType() {
25+
return MetricStat.MAX;
26+
}
27+
28+
@Override
29+
public StarTreeNumericType getAggregatedValueType() {
30+
return VALUE_AGGREGATOR_TYPE;
31+
}
32+
33+
@Override
34+
public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue, StarTreeNumericType starTreeNumericType) {
35+
return starTreeNumericType.getDoubleValue(segmentDocValue);
36+
}
37+
38+
@Override
39+
public Double mergeAggregatedValueAndSegmentValue(Double value, Long segmentDocValue, StarTreeNumericType starTreeNumericType) {
40+
return Math.max(value, starTreeNumericType.getDoubleValue(segmentDocValue));
41+
}
42+
43+
@Override
44+
public Double mergeAggregatedValues(Double value, Double aggregatedValue) {
45+
return Math.max(value, aggregatedValue);
46+
}
47+
48+
@Override
49+
public Double getInitialAggregatedValue(Double value) {
50+
return value;
51+
}
52+
53+
@Override
54+
public int getMaxAggregatedValueByteSize() {
55+
return Double.BYTES;
56+
}
57+
58+
@Override
59+
public Long toLongValue(Double value) {
60+
try {
61+
return NumericUtils.doubleToSortableLong(value);
62+
} catch (Exception e) {
63+
throw new IllegalStateException("Cannot convert " + value + " to sortable long", e);
64+
}
65+
}
66+
67+
@Override
68+
public Double toStarTreeNumericTypeValue(Long value, StarTreeNumericType type) {
69+
try {
70+
return type.getDoubleValue(value);
71+
} catch (Exception e) {
72+
throw new IllegalStateException("Cannot convert " + value + " to sortable aggregation type", e);
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
14+
/**
15+
* Min value aggregator for star tree
16+
*
17+
* @opensearch.experimental
18+
*/
19+
public class MinValueAggregator implements ValueAggregator<Double> {
20+
21+
public static final StarTreeNumericType VALUE_AGGREGATOR_TYPE = StarTreeNumericType.DOUBLE;
22+
23+
@Override
24+
public MetricStat getAggregationType() {
25+
return MetricStat.MIN;
26+
}
27+
28+
@Override
29+
public StarTreeNumericType getAggregatedValueType() {
30+
return VALUE_AGGREGATOR_TYPE;
31+
}
32+
33+
@Override
34+
public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue, StarTreeNumericType starTreeNumericType) {
35+
return starTreeNumericType.getDoubleValue(segmentDocValue);
36+
}
37+
38+
@Override
39+
public Double mergeAggregatedValueAndSegmentValue(Double value, Long segmentDocValue, StarTreeNumericType starTreeNumericType) {
40+
return Math.min(value, starTreeNumericType.getDoubleValue(segmentDocValue));
41+
}
42+
43+
@Override
44+
public Double mergeAggregatedValues(Double value, Double aggregatedValue) {
45+
return Math.min(value, aggregatedValue);
46+
}
47+
48+
@Override
49+
public Double getInitialAggregatedValue(Double value) {
50+
return value;
51+
}
52+
53+
@Override
54+
public int getMaxAggregatedValueByteSize() {
55+
return Double.BYTES;
56+
}
57+
58+
@Override
59+
public Long toLongValue(Double value) {
60+
try {
61+
return NumericUtils.doubleToSortableLong(value);
62+
} catch (Exception e) {
63+
throw new IllegalStateException("Cannot convert " + value + " to sortable long", e);
64+
}
65+
}
66+
67+
@Override
68+
public Double toStarTreeNumericTypeValue(Long value, StarTreeNumericType type) {
69+
try {
70+
return type.getDoubleValue(value);
71+
} catch (Exception e) {
72+
throw new IllegalStateException("Cannot convert " + value + " to sortable aggregation type", e);
73+
}
74+
}
75+
}

server/src/main/java/org/opensearch/index/compositeindex/datacube/startree/aggregators/ValueAggregatorFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ private ValueAggregatorFactory() {}
2626
*/
2727
public static ValueAggregator getValueAggregator(MetricStat aggregationType) {
2828
switch (aggregationType) {
29-
// other metric types (count, min, max, avg) will be supported in the future
29+
// avg aggregator will be covered in the part of query (using count and sum)
3030
case SUM:
3131
return new SumValueAggregator();
3232
case COUNT:
3333
return new CountValueAggregator();
34+
case MIN:
35+
return new MinValueAggregator();
36+
case MAX:
37+
return new MaxValueAggregator();
3438
default:
3539
throw new IllegalStateException("Unsupported aggregation type: " + aggregationType);
3640
}
@@ -49,6 +53,10 @@ public static StarTreeNumericType getAggregatedValueType(MetricStat aggregationT
4953
return SumValueAggregator.VALUE_AGGREGATOR_TYPE;
5054
case COUNT:
5155
return CountValueAggregator.VALUE_AGGREGATOR_TYPE;
56+
case MIN:
57+
return MinValueAggregator.VALUE_AGGREGATOR_TYPE;
58+
case MAX:
59+
return MaxValueAggregator.VALUE_AGGREGATOR_TYPE;
5260
default:
5361
throw new IllegalStateException("Unsupported aggregation type: " + aggregationType);
5462
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
9+
package org.opensearch.index.compositeindex.datacube.startree.aggregators;
10+
11+
import org.apache.lucene.util.NumericUtils;
12+
import org.opensearch.index.compositeindex.datacube.MetricStat;
13+
import org.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;
14+
import org.opensearch.test.OpenSearchTestCase;
15+
16+
public class MaxValueAggregatorTests extends OpenSearchTestCase {
17+
private final MaxValueAggregator aggregator = new MaxValueAggregator();
18+
19+
public void testGetAggregationType() {
20+
assertEquals(MetricStat.MAX.getTypeName(), aggregator.getAggregationType().getTypeName());
21+
}
22+
23+
public void testGetAggregatedValueType() {
24+
assertEquals(MaxValueAggregator.VALUE_AGGREGATOR_TYPE, aggregator.getAggregatedValueType());
25+
}
26+
27+
public void testGetInitialAggregatedValueForSegmentDocValue() {
28+
assertEquals(1.0, aggregator.getInitialAggregatedValueForSegmentDocValue(1L, StarTreeNumericType.LONG), 0.0);
29+
assertThrows(
30+
NullPointerException.class,
31+
() -> aggregator.getInitialAggregatedValueForSegmentDocValue(null, StarTreeNumericType.DOUBLE)
32+
);
33+
}
34+
35+
public void testMergeAggregatedValueAndSegmentValue() {
36+
assertEquals(3.0, aggregator.mergeAggregatedValueAndSegmentValue(2.0, 3L, StarTreeNumericType.LONG), 0.0);
37+
}
38+
39+
public void testMergeAggregatedValues() {
40+
assertEquals(3.0, aggregator.mergeAggregatedValues(2.0, 3.0), 0.0);
41+
}
42+
43+
public void testGetInitialAggregatedValue() {
44+
assertEquals(3.0, aggregator.getInitialAggregatedValue(3.0), 0.0);
45+
}
46+
47+
public void testGetMaxAggregatedValueByteSize() {
48+
assertEquals(Double.BYTES, aggregator.getMaxAggregatedValueByteSize());
49+
}
50+
51+
public void testToLongValue() {
52+
assertEquals(NumericUtils.doubleToSortableLong(3.0), aggregator.toLongValue(3.0), 0.0);
53+
}
54+
55+
public void testToStarTreeNumericTypeValue() {
56+
assertEquals(NumericUtils.sortableLongToDouble(3L), aggregator.toStarTreeNumericTypeValue(3L, StarTreeNumericType.DOUBLE), 0.0);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
9+
package org.opensearch.index.compositeindex.datacube.startree.aggregators;
10+
11+
import org.apache.lucene.util.NumericUtils;
12+
import org.opensearch.index.compositeindex.datacube.MetricStat;
13+
import org.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;
14+
import org.opensearch.test.OpenSearchTestCase;
15+
16+
public class MinValueAggregatorTests extends OpenSearchTestCase {
17+
private final MinValueAggregator aggregator = new MinValueAggregator();
18+
19+
public void testGetAggregationType() {
20+
assertEquals(MetricStat.MIN.getTypeName(), aggregator.getAggregationType().getTypeName());
21+
}
22+
23+
public void testGetAggregatedValueType() {
24+
assertEquals(MinValueAggregator.VALUE_AGGREGATOR_TYPE, aggregator.getAggregatedValueType());
25+
}
26+
27+
public void testGetInitialAggregatedValueForSegmentDocValue() {
28+
assertEquals(1.0, aggregator.getInitialAggregatedValueForSegmentDocValue(1L, StarTreeNumericType.LONG), 0.0);
29+
assertThrows(
30+
NullPointerException.class,
31+
() -> aggregator.getInitialAggregatedValueForSegmentDocValue(null, StarTreeNumericType.DOUBLE)
32+
);
33+
}
34+
35+
public void testMergeAggregatedValueAndSegmentValue() {
36+
assertEquals(2.0, aggregator.mergeAggregatedValueAndSegmentValue(2.0, 3L, StarTreeNumericType.LONG), 0.0);
37+
}
38+
39+
public void testMergeAggregatedValues() {
40+
assertEquals(2.0, aggregator.mergeAggregatedValues(2.0, 3.0), 0.0);
41+
}
42+
43+
public void testGetInitialAggregatedValue() {
44+
assertEquals(3.0, aggregator.getInitialAggregatedValue(3.0), 0.0);
45+
}
46+
47+
public void testGetMaxAggregatedValueByteSize() {
48+
assertEquals(Double.BYTES, aggregator.getMaxAggregatedValueByteSize());
49+
}
50+
51+
public void testToLongValue() {
52+
assertEquals(NumericUtils.doubleToSortableLong(3.0), aggregator.toLongValue(3.0), 0.0);
53+
}
54+
55+
public void testToStarTreeNumericTypeValue() {
56+
assertEquals(NumericUtils.sortableLongToDouble(3L), aggregator.toStarTreeNumericTypeValue(3L, StarTreeNumericType.DOUBLE), 0.0);
57+
}
58+
}

0 commit comments

Comments
 (0)