Search in sources :

Example 1 with SortedNumericDocValueAggregator

use of io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator in project crate by crate.

the class GeometricMeanAggregation method getDocValueAggregator.

@Nullable
@Override
public DocValueAggregator<?> getDocValueAggregator(List<Reference> aggregationReferences, DocTableInfo table, List<Literal<?>> optionalParams) {
    Reference reference = aggregationReferences.get(0);
    if (!reference.hasDocValues()) {
        return null;
    }
    switch(reference.valueType().id()) {
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(GeometricMeanStateType.INSTANCE.fixedSize());
                return new GeometricMeanState();
            }, (values, state) -> state.addValue(values.nextValue()));
        case FloatType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(GeometricMeanStateType.INSTANCE.fixedSize());
                return new GeometricMeanState();
            }, (values, state) -> {
                var value = NumericUtils.sortableIntToFloat((int) values.nextValue());
                state.addValue(value);
            });
        case DoubleType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(GeometricMeanStateType.INSTANCE.fixedSize());
                return new GeometricMeanState();
            }, (values, state) -> {
                var value = NumericUtils.sortableLongToDouble((values.nextValue()));
                state.addValue(value);
            });
        default:
            return null;
    }
}
Also used : Reference(io.crate.metadata.Reference) SortedNumericDocValueAggregator(io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator) Nullable(javax.annotation.Nullable)

Example 2 with SortedNumericDocValueAggregator

use of io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator in project crate by crate.

the class HyperLogLogDistinctAggregation method getDocValueAggregator.

@Nullable
@Override
public DocValueAggregator<?> getDocValueAggregator(List<Reference> aggregationReferences, DocTableInfo table, List<Literal<?>> optionalParams) {
    if (aggregationReferences.stream().anyMatch(x -> !x.hasDocValues())) {
        return null;
    }
    Reference reference = aggregationReferences.get(0);
    var dataType = reference.valueType();
    switch(dataType.id()) {
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                var state = new HllState(dataType, minNodeVersion.onOrAfter(Version.V_4_1_0));
                var precision = optionalParams.size() == 1 ? (Integer) optionalParams.get(0).value() : HyperLogLogPlusPlus.DEFAULT_PRECISION;
                return initIfNeeded(state, memoryManager, precision);
            }, (values, state) -> {
                var hash = BitMixer.mix64(values.nextValue());
                state.addHash(hash);
            });
        case DoubleType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                var state = new HllState(dataType, minNodeVersion.onOrAfter(Version.V_4_1_0));
                var precision = optionalParams.size() == 1 ? (Integer) optionalParams.get(0).value() : HyperLogLogPlusPlus.DEFAULT_PRECISION;
                return initIfNeeded(state, memoryManager, precision);
            }, (values, state) -> {
                // Murmur3Hash.Double in regular aggregation calls mix64 of doubleToLongBits(double value).
                // Equivalent of that in context of double DocValue is
                // mix64(doubleToLongBits(decoded))
                // => mix64(doubleToLongBits(NumericUtils.sortableLongToDouble(values.nextValue())))
                // => mix64(doubleToLongBits(Double.longBitsToDouble(sortableDoubleBits(values.nextValue()))))
                // => mix64(sortableDoubleBits(values.nextValue()))
                var hash = BitMixer.mix64(NumericUtils.sortableDoubleBits(values.nextValue()));
                state.addHash(hash);
            });
        case FloatType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                var state = new HllState(dataType, minNodeVersion.onOrAfter(Version.V_4_1_0));
                var precision = optionalParams.size() == 1 ? (Integer) optionalParams.get(0).value() : HyperLogLogPlusPlus.DEFAULT_PRECISION;
                return initIfNeeded(state, memoryManager, precision);
            }, (values, state) -> {
                // Murmur3Hash.Float in regular aggregation calls mix64 of doubleToLongBits(double value).
                // Equivalent of that in context of float DocValue is
                // mix64(doubleToLongBits(decoded))
                // => mix64(doubleToLongBits((double)NumericUtils.sortableIntToFloat((int)values.nextValue())))
                // => mix64(doubleToLongBits(NumericUtils.sortableIntToFloat((int)values.nextValue()))) - no need to double cast, auto widening
                var hash = BitMixer.mix64(doubleToLongBits(NumericUtils.sortableIntToFloat((int) values.nextValue())));
                state.addHash(hash);
            });
        case StringType.ID:
            var precision = optionalParams.size() == 1 ? (Integer) optionalParams.get(0).value() : HyperLogLogPlusPlus.DEFAULT_PRECISION;
            return new HllAggregator(reference.column().fqn(), dataType, precision) {

                @Override
                public void apply(RamAccounting ramAccounting, int doc, HllState state) throws IOException {
                    if (super.values.advanceExact(doc) && super.values.docValueCount() == 1) {
                        BytesRef ref = super.values.nextValue();
                        var hash = state.isAllOn4_1() ? MurmurHash3.hash64(ref.bytes, ref.offset, ref.length) : MurmurHash3.hash128(ref.bytes, ref.offset, ref.length, 0, super.hash128).h1;
                        state.addHash(hash);
                    }
                }
            };
        case IpType.ID:
            var ipPrecision = optionalParams.size() == 1 ? (Integer) optionalParams.get(0).value() : HyperLogLogPlusPlus.DEFAULT_PRECISION;
            return new HllAggregator(reference.column().fqn(), dataType, ipPrecision) {

                @Override
                public void apply(RamAccounting ramAccounting, int doc, HllState state) throws IOException {
                    if (super.values.advanceExact(doc) && super.values.docValueCount() == 1) {
                        BytesRef ref = super.values.nextValue();
                        byte[] bytes = ((String) DocValueFormat.IP.format(ref)).getBytes(StandardCharsets.UTF_8);
                        var hash = state.isAllOn4_1() ? MurmurHash3.hash64(bytes, 0, bytes.length) : MurmurHash3.hash128(bytes, 0, bytes.length, 0, super.hash128).h1;
                        state.addHash(hash);
                    }
                }
            };
        default:
            return null;
    }
}
Also used : RamAccounting(io.crate.breaker.RamAccounting) Reference(io.crate.metadata.Reference) SortedNumericDocValueAggregator(io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator) BytesRef(org.apache.lucene.util.BytesRef) Nullable(javax.annotation.Nullable)

Example 3 with SortedNumericDocValueAggregator

use of io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator in project crate by crate.

the class VarianceAggregation method getDocValueAggregator.

@Nullable
@Override
public DocValueAggregator<?> getDocValueAggregator(List<Reference> aggregationReferences, DocTableInfo table, List<Literal<?>> optionalParams) {
    Reference reference = aggregationReferences.get(0);
    if (!reference.hasDocValues()) {
        return null;
    }
    switch(reference.valueType().id()) {
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(VarianceStateType.INSTANCE.fixedSize());
                return new Variance();
            }, (values, state) -> state.increment(values.nextValue()));
        case FloatType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(VarianceStateType.INSTANCE.fixedSize());
                return new Variance();
            }, (values, state) -> {
                var value = NumericUtils.sortableIntToFloat((int) values.nextValue());
                state.increment(value);
            });
        case DoubleType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(VarianceStateType.INSTANCE.fixedSize());
                return new Variance();
            }, (values, state) -> {
                var value = NumericUtils.sortableLongToDouble((values.nextValue()));
                state.increment(value);
            });
        default:
            return null;
    }
}
Also used : Reference(io.crate.metadata.Reference) SortedNumericDocValueAggregator(io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator) Variance(io.crate.execution.engine.aggregation.statistics.Variance) Nullable(javax.annotation.Nullable)

Example 4 with SortedNumericDocValueAggregator

use of io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator in project crate by crate.

the class StandardDeviationAggregation method getDocValueAggregator.

@Nullable
@Override
public DocValueAggregator<?> getDocValueAggregator(List<Reference> aggregationReferences, DocTableInfo table, List<Literal<?>> optionalParams) {
    Reference reference = aggregationReferences.get(0);
    if (!reference.hasDocValues()) {
        return null;
    }
    switch(reference.valueType().id()) {
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(StdDevStateType.INSTANCE.fixedSize());
                return new StandardDeviation();
            }, (values, state) -> state.increment(values.nextValue()));
        case FloatType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(StdDevStateType.INSTANCE.fixedSize());
                return new StandardDeviation();
            }, (values, state) -> {
                var value = NumericUtils.sortableIntToFloat((int) values.nextValue());
                state.increment(value);
            });
        case DoubleType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(StdDevStateType.INSTANCE.fixedSize());
                return new StandardDeviation();
            }, (values, state) -> {
                var value = NumericUtils.sortableLongToDouble((values.nextValue()));
                state.increment(value);
            });
        default:
            return null;
    }
}
Also used : Reference(io.crate.metadata.Reference) SortedNumericDocValueAggregator(io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator) StandardDeviation(io.crate.execution.engine.aggregation.statistics.StandardDeviation) Nullable(javax.annotation.Nullable)

Example 5 with SortedNumericDocValueAggregator

use of io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator in project crate by crate.

the class AverageAggregation method getDocValueAggregator.

@Nullable
@Override
public DocValueAggregator<?> getDocValueAggregator(List<Reference> aggregationReferences, DocTableInfo table, List<Literal<?>> optionalParams) {
    Reference reference = aggregationReferences.get(0);
    if (!reference.hasDocValues()) {
        return null;
    }
    switch(reference.valueType().id()) {
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
        case LongType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(AverageStateType.INSTANCE.fixedSize());
                return new AverageState();
            }, (values, state) -> {
                // Mutates state.
                state.addNumber(values.nextValue(), true);
            });
        case FloatType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(AverageStateType.INSTANCE.fixedSize());
                return new AverageState();
            }, (values, state) -> {
                var value = NumericUtils.sortableIntToFloat((int) values.nextValue());
                // Mutates state.
                state.addNumber(value, false);
            });
        case DoubleType.ID:
            return new SortedNumericDocValueAggregator<>(reference.column().fqn(), (ramAccounting, memoryManager, minNodeVersion) -> {
                ramAccounting.addBytes(AverageStateType.INSTANCE.fixedSize());
                return new AverageState();
            }, (values, state) -> {
                var value = NumericUtils.sortableLongToDouble((values.nextValue()));
                // Mutates state.
                state.addNumber(value, false);
            });
        default:
            return null;
    }
}
Also used : Reference(io.crate.metadata.Reference) SortedNumericDocValueAggregator(io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator) Nullable(javax.annotation.Nullable)

Aggregations

SortedNumericDocValueAggregator (io.crate.execution.engine.aggregation.impl.templates.SortedNumericDocValueAggregator)5 Reference (io.crate.metadata.Reference)5 Nullable (javax.annotation.Nullable)5 RamAccounting (io.crate.breaker.RamAccounting)1 StandardDeviation (io.crate.execution.engine.aggregation.statistics.StandardDeviation)1 Variance (io.crate.execution.engine.aggregation.statistics.Variance)1 BytesRef (org.apache.lucene.util.BytesRef)1