Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onheap impl startree minmax #32

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class CountValueAggregator implements ValueAggregator<Long> {
public static final StarTreeNumericType VALUE_AGGREGATOR_TYPE = StarTreeNumericType.LONG;
public static final long DEFAULT_INITIAL_VALUE = 1L;
private StarTreeNumericType starTreeNumericType;
private final StarTreeNumericType starTreeNumericType;

public CountValueAggregator(StarTreeNumericType starTreeNumericType) {
this.starTreeNumericType = starTreeNumericType;
Expand All @@ -36,21 +36,38 @@ public StarTreeNumericType getAggregatedValueType() {

@Override
public Long getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue) {

if (segmentDocValue == null) {
return getIdentityMetricValue();
}

return DEFAULT_INITIAL_VALUE;
}

@Override
public Long mergeAggregatedValueAndSegmentValue(Long value, Long segmentDocValue) {
if (value == null) {
return getIdentityMetricValue();
}
return value + 1;
}

@Override
public Long mergeAggregatedValues(Long value, Long aggregatedValue) {
if (value == null) {
value = getIdentityMetricValue();
}
if (aggregatedValue == null) {
aggregatedValue = getIdentityMetricValue();
}
return value + aggregatedValue;
}

@Override
public Long getInitialAggregatedValue(Long value) {
if (value == null) {
return getIdentityMetricValue();
}
return value;
}

Expand All @@ -68,4 +85,9 @@ public Long toLongValue(Long value) {
public Long toStarTreeNumericTypeValue(Long value) {
return value;
}

@Override
public Long getIdentityMetricValue() {
return 0L;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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;
private final StarTreeNumericType starTreeNumericType;

public MaxValueAggregator(StarTreeNumericType starTreeNumericType) {
this.starTreeNumericType = starTreeNumericType;
}

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

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

@Override
public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue) {
if (segmentDocValue == null) {
return getIdentityMetricValue();
}
return starTreeNumericType.getDoubleValue(segmentDocValue);
}

@Override
public Double mergeAggregatedValueAndSegmentValue(Double value, Long segmentDocValue) {
if (segmentDocValue == null && value != null) {
return value;
} else if (segmentDocValue != null && value == null) {
return starTreeNumericType.getDoubleValue(segmentDocValue);
} else if (segmentDocValue == null) {
return getIdentityMetricValue();
}
return Math.max(value, starTreeNumericType.getDoubleValue(segmentDocValue));
}

@Override
public Double mergeAggregatedValues(Double value, Double aggregatedValue) {
if (value == null && aggregatedValue != null) {
return aggregatedValue;
} else if (value != null && aggregatedValue == null) {
return value;
} else if (value == null) {
return getIdentityMetricValue();
}
return Math.max(value, aggregatedValue);
}

@Override
public Double getInitialAggregatedValue(Double value) {
if (value == null) {
return getIdentityMetricValue();
}
return value;
}

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

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

@Override
public Double toStarTreeNumericTypeValue(Long value) {
try {
if (value == null) {
return getIdentityMetricValue();
}
return starTreeNumericType.getDoubleValue(value);
} catch (Exception e) {
throw new IllegalStateException("Cannot convert " + value + " to sortable aggregation type", e);
}
}

@Override
public Double getIdentityMetricValue() {
// in present aggregations, if the metric behind max is missing, we treat it as null
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/**
* Builds aggregation function and doc values field pair to support various aggregations
*
* @opensearch.experimental
*/
public class MetricAggregatorInfo implements Comparable<MetricAggregatorInfo> {
Expand Down Expand Up @@ -79,7 +80,15 @@ public StarTreeNumericType getAggregatedValueType() {
* @return field name with metric type and field
*/
public String toFieldName() {
return starFieldName + DELIMITER + field + DELIMITER + metricStat.getTypeName();
return toFieldName(starFieldName, field, metricStat.getTypeName());

}

/**
* @return field name with star-tree field name metric type and field
*/
public static String toFieldName(String starFieldName, String field, String metricName) {
return starFieldName + DELIMITER + field + DELIMITER + metricName;
}

@Override
Expand All @@ -94,7 +103,7 @@ public boolean equals(Object obj) {
}
if (obj instanceof MetricAggregatorInfo) {
MetricAggregatorInfo anotherPair = (MetricAggregatorInfo) obj;
return metricStat == anotherPair.metricStat && field.equals(anotherPair.field);
return metricStat.equals(anotherPair.metricStat) && field.equals(anotherPair.field);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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;
private final StarTreeNumericType starTreeNumericType;

public MinValueAggregator(StarTreeNumericType starTreeNumericType) {
this.starTreeNumericType = starTreeNumericType;
}

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

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

@Override
public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue) {
if (segmentDocValue == null) {
return getIdentityMetricValue();
}
return starTreeNumericType.getDoubleValue(segmentDocValue);
}

@Override
public Double mergeAggregatedValueAndSegmentValue(Double value, Long segmentDocValue) {
if (segmentDocValue == null && value != null) {
return value;
} else if (segmentDocValue != null && value == null) {
return starTreeNumericType.getDoubleValue(segmentDocValue);
} else if (segmentDocValue == null) {
return getIdentityMetricValue();
}
return Math.min(value, starTreeNumericType.getDoubleValue(segmentDocValue));
}

@Override
public Double mergeAggregatedValues(Double value, Double aggregatedValue) {
if (value == null && aggregatedValue != null) {
return aggregatedValue;
} else if (value != null && aggregatedValue == null) {
return value;
} else if (value == null) {
return getIdentityMetricValue();
}
return Math.min(value, aggregatedValue);
}

@Override
public Double getInitialAggregatedValue(Double value) {
if (value == null) {
return getIdentityMetricValue();
}
return value;
}

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

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

@Override
public Double toStarTreeNumericTypeValue(Long value) {
try {
if (value == null) {
return getIdentityMetricValue();
}
return starTreeNumericType.getDoubleValue(value);
} catch (Exception e) {
throw new IllegalStateException("Cannot convert " + value + " to sortable aggregation type", e);
}
}

@Override
public Double getIdentityMetricValue() {
// in present aggregations, if the metric behind min is missing, we treat it as null
return null;
}
}
Loading
Loading