Skip to content

Commit 29d3f99

Browse files
sarthakaggarwal97akolarkunnu
authored andcommitted
Star Tree Min and Max Value Aggregators (opensearch-project#14625)
--------- Signed-off-by: Sarthak Aggarwal <[email protected]>
1 parent 831a928 commit 29d3f99

18 files changed

+1049
-309
lines changed

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,66 @@
77
*/
88
package org.opensearch.index.compositeindex.datacube.startree.aggregators;
99

10-
import org.opensearch.index.compositeindex.datacube.MetricStat;
1110
import org.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;
1211

1312
/**
1413
* Count value aggregator for star tree
1514
*
1615
* @opensearch.experimental
1716
*/
18-
public class CountValueAggregator implements ValueAggregator<Long> {
19-
public static final StarTreeNumericType VALUE_AGGREGATOR_TYPE = StarTreeNumericType.LONG;
17+
class CountValueAggregator implements ValueAggregator<Long> {
18+
2019
public static final long DEFAULT_INITIAL_VALUE = 1L;
21-
private StarTreeNumericType starTreeNumericType;
20+
private final StarTreeNumericType starTreeNumericType;
21+
private static final StarTreeNumericType VALUE_AGGREGATOR_TYPE = StarTreeNumericType.LONG;
2222

2323
public CountValueAggregator(StarTreeNumericType starTreeNumericType) {
2424
this.starTreeNumericType = starTreeNumericType;
2525
}
2626

27-
@Override
28-
public MetricStat getAggregationType() {
29-
return MetricStat.COUNT;
30-
}
31-
3227
@Override
3328
public StarTreeNumericType getAggregatedValueType() {
3429
return VALUE_AGGREGATOR_TYPE;
3530
}
3631

3732
@Override
3833
public Long getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue) {
34+
35+
if (segmentDocValue == null) {
36+
return getIdentityMetricValue();
37+
}
38+
3939
return DEFAULT_INITIAL_VALUE;
4040
}
4141

4242
@Override
4343
public Long mergeAggregatedValueAndSegmentValue(Long value, Long segmentDocValue) {
44-
return value + 1;
44+
assert value != null;
45+
if (segmentDocValue != null) {
46+
return value + 1;
47+
}
48+
return value;
4549
}
4650

4751
@Override
4852
public Long mergeAggregatedValues(Long value, Long aggregatedValue) {
53+
if (value == null) {
54+
value = getIdentityMetricValue();
55+
}
56+
if (aggregatedValue == null) {
57+
aggregatedValue = getIdentityMetricValue();
58+
}
4959
return value + aggregatedValue;
5060
}
5161

52-
@Override
53-
public Long getInitialAggregatedValue(Long value) {
54-
return value;
55-
}
56-
57-
@Override
58-
public int getMaxAggregatedValueByteSize() {
59-
return Long.BYTES;
60-
}
61-
62-
@Override
63-
public Long toLongValue(Long value) {
64-
return value;
65-
}
66-
6762
@Override
6863
public Long toStarTreeNumericTypeValue(Long value) {
6964
return value;
7065
}
7166

7267
@Override
7368
public Long getIdentityMetricValue() {
69+
// in present aggregations, if the metric behind count is missing, we treat it as 0
7470
return 0L;
7571
}
7672
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;
11+
12+
/**
13+
* Max value aggregator for star tree
14+
*
15+
* @opensearch.experimental
16+
*/
17+
class MaxValueAggregator extends StatelessDoubleValueAggregator {
18+
19+
public MaxValueAggregator(StarTreeNumericType starTreeNumericType) {
20+
super(starTreeNumericType, null);
21+
}
22+
23+
@Override
24+
protected Double performValueAggregation(Double aggregatedValue, Double segmentDocValue) {
25+
return Math.max(aggregatedValue, segmentDocValue);
26+
}
27+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
/**
1818
* Builds aggregation function and doc values field pair to support various aggregations
19+
*
1920
* @opensearch.experimental
2021
*/
2122
public class MetricAggregatorInfo implements Comparable<MetricAggregatorInfo> {
@@ -79,7 +80,15 @@ public StarTreeNumericType getAggregatedValueType() {
7980
* @return field name with metric type and field
8081
*/
8182
public String toFieldName() {
82-
return starFieldName + DELIMITER + field + DELIMITER + metricStat.getTypeName();
83+
return toFieldName(starFieldName, field, metricStat.getTypeName());
84+
85+
}
86+
87+
/**
88+
* @return field name with star-tree field name metric type and field
89+
*/
90+
public static String toFieldName(String starFieldName, String field, String metricName) {
91+
return starFieldName + DELIMITER + field + DELIMITER + metricName;
8392
}
8493

8594
@Override
@@ -94,7 +103,7 @@ public boolean equals(Object obj) {
94103
}
95104
if (obj instanceof MetricAggregatorInfo) {
96105
MetricAggregatorInfo anotherPair = (MetricAggregatorInfo) obj;
97-
return metricStat == anotherPair.metricStat && field.equals(anotherPair.field);
106+
return metricStat.equals(anotherPair.metricStat) && field.equals(anotherPair.field);
98107
}
99108
return false;
100109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;
11+
12+
/**
13+
* Min value aggregator for star tree
14+
*
15+
* @opensearch.experimental
16+
*/
17+
class MinValueAggregator extends StatelessDoubleValueAggregator {
18+
19+
public MinValueAggregator(StarTreeNumericType starTreeNumericType) {
20+
super(starTreeNumericType, null);
21+
}
22+
23+
@Override
24+
protected Double performValueAggregation(Double aggregatedValue, Double segmentDocValue) {
25+
return Math.min(aggregatedValue, segmentDocValue);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.opensearch.index.compositeindex.datacube.startree.aggregators.numerictype.StarTreeNumericType;
11+
12+
/**
13+
* This is an abstract class that defines the common methods for all double value aggregators
14+
* It is stateless.
15+
*
16+
* @opensearch.experimental
17+
*/
18+
abstract class StatelessDoubleValueAggregator implements ValueAggregator<Double> {
19+
20+
protected final StarTreeNumericType starTreeNumericType;
21+
protected final Double identityValue;
22+
private static final StarTreeNumericType VALUE_AGGREGATOR_TYPE = StarTreeNumericType.DOUBLE;
23+
24+
public StatelessDoubleValueAggregator(StarTreeNumericType starTreeNumericType, Double identityValue) {
25+
this.starTreeNumericType = starTreeNumericType;
26+
this.identityValue = identityValue;
27+
}
28+
29+
@Override
30+
public StarTreeNumericType getAggregatedValueType() {
31+
return VALUE_AGGREGATOR_TYPE;
32+
}
33+
34+
@Override
35+
public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue) {
36+
if (segmentDocValue == null) {
37+
return getIdentityMetricValue();
38+
}
39+
return starTreeNumericType.getDoubleValue(segmentDocValue);
40+
}
41+
42+
@Override
43+
public Double mergeAggregatedValues(Double value, Double aggregatedValue) {
44+
if (value == null && aggregatedValue != null) {
45+
return aggregatedValue;
46+
} else if (value != null && aggregatedValue == null) {
47+
return value;
48+
} else if (value == null) {
49+
return getIdentityMetricValue();
50+
}
51+
return performValueAggregation(value, aggregatedValue);
52+
}
53+
54+
@Override
55+
public Double toStarTreeNumericTypeValue(Long value) {
56+
try {
57+
if (value == null) {
58+
return getIdentityMetricValue();
59+
}
60+
return starTreeNumericType.getDoubleValue(value);
61+
} catch (Exception e) {
62+
throw new IllegalStateException("Cannot convert " + value + " to sortable aggregation type", e);
63+
}
64+
}
65+
66+
@Override
67+
public Double getIdentityMetricValue() {
68+
// the identity value that we return should be inline with the existing aggregations
69+
return identityValue;
70+
}
71+
72+
/**
73+
* Performs stateless aggregation on the value and the segmentDocValue based on the implementation
74+
*
75+
* @param aggregatedValue aggregated value for the segment so far
76+
* @param segmentDocValue current segment doc value
77+
* @return aggregated value
78+
*/
79+
protected abstract Double performValueAggregation(Double aggregatedValue, Double segmentDocValue);
80+
81+
}

0 commit comments

Comments
 (0)