Skip to content

Commit

Permalink
dimension type and some 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 28, 2024
1 parent 6ba6739 commit 46becbd
Show file tree
Hide file tree
Showing 25 changed files with 195 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.opensearch.common.xcontent.support.XContentMapValues;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.core.xcontent.XContentParser.Token;
import org.opensearch.index.compositeindex.datacube.DimensionType;
import org.opensearch.index.fielddata.FieldData;
import org.opensearch.index.fielddata.IndexFieldData;
import org.opensearch.index.fielddata.IndexNumericFieldData;
Expand All @@ -71,6 +72,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

/** A {@link FieldMapper} for scaled floats. Values are internally multiplied
Expand Down Expand Up @@ -165,8 +167,8 @@ public ScaledFloatFieldMapper build(BuilderContext context) {
}

@Override
public boolean isDataCubeDimensionSupported() {
return true;
public Optional<DimensionType> getSupportedDataCubeDimensionType() {
return Optional.of(DimensionType.NUMERIC);
}

@Override
Expand All @@ -177,7 +179,7 @@ public boolean isDataCubeMetricSupported() {

public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n, c.getSettings()));

public static final class ScaledFloatFieldType extends SimpleMappedFieldType implements NumericPointEncoder, NumericFieldConverter {
public static final class ScaledFloatFieldType extends SimpleMappedFieldType implements NumericPointEncoder, FieldValueConverter {

private final double scalingFactor;
private final Double nullValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.common.xcontent.support.XContentMapValues;
import org.opensearch.index.compositeindex.datacube.startree.StarTreeIndexSettings;
import org.opensearch.index.mapper.DateFieldMapper;
import org.opensearch.index.mapper.Mapper;

import java.util.ArrayList;
Expand Down Expand Up @@ -54,11 +53,13 @@ public static Dimension parseAndCreateDimension(
Map<String, Object> dimensionMap,
Mapper.TypeParser.ParserContext c
) {
if (builder instanceof DateFieldMapper.Builder) {
if (builder.getSupportedDataCubeDimensionType().isPresent()
&& builder.getSupportedDataCubeDimensionType().get().equals(DimensionType.DATE)) {
return parseAndCreateDateDimension(name, dimensionMap, c);
} else if (builder.isDataCubeDimensionSupported()) {
return new NumericDimension(name);
}
} else if (builder.getSupportedDataCubeDimensionType().isPresent()
&& builder.getSupportedDataCubeDimensionType().get().equals(DimensionType.NUMERIC)) {
return new NumericDimension(name);
}
throw new IllegalArgumentException(
String.format(Locale.ROOT, "unsupported field type associated with star tree dimension [%s]", name)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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;

/**
* Represents the types of dimensions supported in a data cube.
* <p>
* This enum defines the possible types of dimensions that can be used
* in a data cube structure within the composite index.
*
* @opensearch.experimental
*/
public enum DimensionType {
/**
* Represents a numeric dimension type.
* This is used for dimensions that contain numerical values.
*/
NUMERIC,

/**
* Represents a date dimension type.
* This is used for dimensions that contain date or timestamp values.
*/
DATE
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.opensearch.index.mapper.FieldValueConverter;
import org.opensearch.index.mapper.NumberFieldMapper;

/**
Expand All @@ -17,12 +18,12 @@
public class CountValueAggregator implements ValueAggregator<Long> {

public static final long DEFAULT_INITIAL_VALUE = 1L;
private static final NumberFieldMapper.NumberType VALUE_AGGREGATOR_TYPE = NumberFieldMapper.NumberType.LONG;
private static final FieldValueConverter VALUE_AGGREGATOR_TYPE = NumberFieldMapper.NumberType.LONG;

public CountValueAggregator() {}

@Override
public NumberFieldMapper.NumberType getAggregatedValueType() {
public FieldValueConverter getAggregatedValueType() {
return VALUE_AGGREGATOR_TYPE;
}

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

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

import org.opensearch.index.mapper.FieldValueConverter;
import org.opensearch.index.mapper.NumberFieldMapper;

/**
Expand All @@ -17,12 +18,12 @@
*/
public class DocCountAggregator implements ValueAggregator<Long> {

private static final NumberFieldMapper.NumberType VALUE_AGGREGATOR_TYPE = NumberFieldMapper.NumberType.LONG;
private static final FieldValueConverter VALUE_AGGREGATOR_TYPE = NumberFieldMapper.NumberType.LONG;

public DocCountAggregator() {}

@Override
public NumberFieldMapper.NumberType getAggregatedValueType() {
public FieldValueConverter getAggregatedValueType() {
return VALUE_AGGREGATOR_TYPE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.opensearch.index.mapper.NumericFieldConverter;
import org.opensearch.index.mapper.FieldValueConverter;

/**
* Max value aggregator for star tree
Expand All @@ -16,8 +16,8 @@
*/
class MaxValueAggregator extends StatelessDoubleValueAggregator {

public MaxValueAggregator(NumericFieldConverter numericFieldConverter) {
super(numericFieldConverter, null);
public MaxValueAggregator(FieldValueConverter fieldValueConverter) {
super(fieldValueConverter, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.opensearch.index.compositeindex.datacube.MetricStat;
import org.opensearch.index.mapper.NumericFieldConverter;
import org.opensearch.index.mapper.FieldValueConverter;

import java.util.Comparator;
import java.util.Objects;
Expand All @@ -26,15 +26,15 @@ public class MetricAggregatorInfo implements Comparable<MetricAggregatorInfo> {
private final MetricStat metricStat;
private final String field;
private final ValueAggregator valueAggregators;
private final NumericFieldConverter numericFieldConverter;
private final FieldValueConverter fieldValueConverter;

/**
* Constructor for MetricAggregatorInfo
*/
public MetricAggregatorInfo(MetricStat metricStat, String field, String starFieldName, NumericFieldConverter numericFieldConverter) {
public MetricAggregatorInfo(MetricStat metricStat, String field, String starFieldName, FieldValueConverter fieldValueConverter) {
this.metricStat = metricStat;
this.numericFieldConverter = numericFieldConverter;
this.valueAggregators = ValueAggregatorFactory.getValueAggregator(metricStat, this.numericFieldConverter);
this.fieldValueConverter = fieldValueConverter;
this.valueAggregators = ValueAggregatorFactory.getValueAggregator(metricStat, this.fieldValueConverter);
this.field = field;
this.starFieldName = starFieldName;
this.metric = toFieldName();
Expand Down Expand Up @@ -71,8 +71,8 @@ public ValueAggregator getValueAggregators() {
/**
* @return star tree aggregated value type
*/
public NumericFieldConverter getNumericFieldConverter() {
return numericFieldConverter;
public FieldValueConverter getNumericFieldConverter() {
return fieldValueConverter;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.opensearch.index.mapper.NumericFieldConverter;
import org.opensearch.index.mapper.FieldValueConverter;

/**
* Min value aggregator for star tree
Expand All @@ -16,8 +16,8 @@
*/
class MinValueAggregator extends StatelessDoubleValueAggregator {

public MinValueAggregator(NumericFieldConverter numericFieldConverter) {
super(numericFieldConverter, null);
public MinValueAggregator(FieldValueConverter fieldValueConverter) {
super(fieldValueConverter, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.opensearch.index.mapper.FieldValueConverter;
import org.opensearch.index.mapper.NumberFieldMapper;
import org.opensearch.index.mapper.NumericFieldConverter;

/**
* This is an abstract class that defines the common methods for all double value aggregators
Expand All @@ -18,17 +18,17 @@
*/
abstract class StatelessDoubleValueAggregator implements ValueAggregator<Double> {

protected final NumericFieldConverter numericFieldConverter;
protected final FieldValueConverter fieldValueConverter;
protected final Double identityValue;
private static final NumberFieldMapper.NumberType VALUE_AGGREGATOR_TYPE = NumberFieldMapper.NumberType.DOUBLE;
private static final FieldValueConverter VALUE_AGGREGATOR_TYPE = NumberFieldMapper.NumberType.DOUBLE;

public StatelessDoubleValueAggregator(NumericFieldConverter numericFieldConverter, Double identityValue) {
this.numericFieldConverter = numericFieldConverter;
public StatelessDoubleValueAggregator(FieldValueConverter fieldValueConverter, Double identityValue) {
this.fieldValueConverter = fieldValueConverter;
this.identityValue = identityValue;
}

@Override
public NumberFieldMapper.NumberType getAggregatedValueType() {
public FieldValueConverter getAggregatedValueType() {
return VALUE_AGGREGATOR_TYPE;
}

Expand All @@ -37,7 +37,7 @@ public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue)
if (segmentDocValue == null) {
return getIdentityMetricValue();
}
return numericFieldConverter.toDoubleValue(segmentDocValue);
return fieldValueConverter.toDoubleValue(segmentDocValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*/
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.opensearch.index.mapper.FieldValueConverter;
import org.opensearch.index.mapper.NumberFieldMapper;
import org.opensearch.index.mapper.NumericFieldConverter;
import org.opensearch.search.aggregations.metrics.CompensatedSum;

/**
Expand All @@ -23,17 +23,17 @@
*/
class SumValueAggregator implements ValueAggregator<Double> {

private final NumericFieldConverter numericFieldConverter;
private static final NumberFieldMapper.NumberType VALUE_AGGREGATOR_TYPE = NumberFieldMapper.NumberType.DOUBLE;
private final FieldValueConverter fieldValueConverter;
private static final FieldValueConverter VALUE_AGGREGATOR_TYPE = NumberFieldMapper.NumberType.DOUBLE;

private CompensatedSum kahanSummation = new CompensatedSum(0, 0);

public SumValueAggregator(NumericFieldConverter numericFieldConverter) {
this.numericFieldConverter = numericFieldConverter;
public SumValueAggregator(FieldValueConverter fieldValueConverter) {
this.fieldValueConverter = fieldValueConverter;
}

@Override
public NumberFieldMapper.NumberType getAggregatedValueType() {
public FieldValueConverter getAggregatedValueType() {
return VALUE_AGGREGATOR_TYPE;
}

Expand All @@ -42,7 +42,7 @@ public Double getInitialAggregatedValueForSegmentDocValue(Long segmentDocValue)
kahanSummation.reset(0, 0);
// add takes care of the sum and compensation internally
if (segmentDocValue != null) {
kahanSummation.add(numericFieldConverter.toDoubleValue(segmentDocValue));
kahanSummation.add(fieldValueConverter.toDoubleValue(segmentDocValue));
} else {
kahanSummation.add(getIdentityMetricValue());
}
Expand All @@ -56,7 +56,7 @@ public Double mergeAggregatedValueAndSegmentValue(Double value, Long segmentDocV
assert value == null || kahanSummation.value() == value;
// add takes care of the sum and compensation internally
if (segmentDocValue != null) {
kahanSummation.add(numericFieldConverter.toDoubleValue(segmentDocValue));
kahanSummation.add(fieldValueConverter.toDoubleValue(segmentDocValue));
} else {
kahanSummation.add(getIdentityMetricValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.opensearch.index.mapper.NumberFieldMapper;
import org.opensearch.index.mapper.FieldValueConverter;

/**
* A value aggregator that pre-aggregates on the input values for a specific type of aggregation.
Expand All @@ -19,7 +19,7 @@ public interface ValueAggregator<A> {
/**
* Returns the data type of the aggregated value.
*/
NumberFieldMapper.NumberType getAggregatedValueType();
FieldValueConverter getAggregatedValueType();

/**
* Returns the initial aggregated value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package org.opensearch.index.compositeindex.datacube.startree.aggregators;

import org.opensearch.index.compositeindex.datacube.MetricStat;
import org.opensearch.index.mapper.NumericFieldConverter;
import org.opensearch.index.mapper.FieldValueConverter;

/**
* Value aggregator factory for a given aggregation type
Expand All @@ -22,20 +22,20 @@ private ValueAggregatorFactory() {}
* Returns a new instance of value aggregator for the given aggregation type.
*
* @param aggregationType Aggregation type
* @param numericFieldConverter Numeric type converter associated with star tree field ( as specified in index mapping )
* @param fieldValueConverter Numeric type converter associated with star tree field ( as specified in index mapping )
* @return Value aggregator
*/
public static ValueAggregator getValueAggregator(MetricStat aggregationType, NumericFieldConverter numericFieldConverter) {
public static ValueAggregator getValueAggregator(MetricStat aggregationType, FieldValueConverter fieldValueConverter) {
switch (aggregationType) {
// avg aggregator will be covered in the part of query (using count and sum)
case SUM:
return new SumValueAggregator(numericFieldConverter);
return new SumValueAggregator(fieldValueConverter);
case VALUE_COUNT:
return new CountValueAggregator();
case MIN:
return new MinValueAggregator(numericFieldConverter);
return new MinValueAggregator(fieldValueConverter);
case MAX:
return new MaxValueAggregator(numericFieldConverter);
return new MaxValueAggregator(fieldValueConverter);
case DOC_COUNT:
return new DocCountAggregator();
default:
Expand Down
Loading

0 comments on commit 46becbd

Please sign in to comment.