Search in sources :

Example 6 with QuantileDigest

use of com.facebook.airlift.stats.QuantileDigest in project presto by prestodb.

the class QuantileDigestFunctions method valuesAtQuantilesBigint.

@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.")
@SqlType("array(bigint)")
public static Block valuesAtQuantilesBigint(@SqlType("qdigest(bigint)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
    QuantileDigest digest = new QuantileDigest(input);
    BlockBuilder output = BIGINT.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
    for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
        BIGINT.writeLong(output, digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i)));
    }
    return output.build();
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 7 with QuantileDigest

use of com.facebook.airlift.stats.QuantileDigest in project presto by prestodb.

the class ApproximateDoublePercentileAggregations method output.

@OutputFunction(StandardTypes.DOUBLE)
public static void output(@AggregationState DigestAndPercentileState state, BlockBuilder out) {
    QuantileDigest digest = state.getDigest();
    double percentile = state.getPercentile();
    if (digest == null || digest.getCount() == 0.0) {
        out.appendNull();
    } else {
        checkState(percentile != -1.0, "Percentile is missing");
        checkCondition(0 <= percentile && percentile <= 1, INVALID_FUNCTION_ARGUMENT, "Percentile must be between 0 and 1");
        DOUBLE.writeDouble(out, sortableLongToDouble(digest.getQuantile(percentile)));
    }
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) OutputFunction(com.facebook.presto.spi.function.OutputFunction)

Example 8 with QuantileDigest

use of com.facebook.airlift.stats.QuantileDigest in project presto by prestodb.

the class ApproximateLongPercentileAggregations method addInput.

private static void addInput(@AggregationState DigestAndPercentileState state, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.BIGINT) long weight, @SqlType(StandardTypes.DOUBLE) double percentile, @SqlType(StandardTypes.DOUBLE) double accuracy) {
    QuantileDigest digest = state.getDigest();
    if (state.getDigest() == null) {
        checkAccuracy(accuracy);
        digest = new QuantileDigest(accuracy);
        state.setDigest(digest);
    } else {
        state.addMemoryUsage(-digest.estimatedInMemorySizeInBytes());
    }
    digest.add(value, weight);
    state.addMemoryUsage(digest.estimatedInMemorySizeInBytes());
    // use last percentile
    state.setPercentile(percentile);
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest)

Example 9 with QuantileDigest

use of com.facebook.airlift.stats.QuantileDigest in project presto by prestodb.

the class ApproximateDoublePercentileArrayAggregations method output.

@OutputFunction("array(double)")
public static void output(@AggregationState DigestAndPercentileArrayState state, BlockBuilder out) {
    QuantileDigest digest = state.getDigest();
    List<Double> percentiles = state.getPercentiles();
    if (percentiles == null || digest == null) {
        out.appendNull();
        return;
    }
    BlockBuilder blockBuilder = out.beginBlockEntry();
    for (int i = 0; i < percentiles.size(); i++) {
        Double percentile = percentiles.get(i);
        DOUBLE.writeDouble(blockBuilder, sortableLongToDouble(digest.getQuantile(percentile)));
    }
    out.closeEntry();
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) FloatingPointBitsConverterUtil.sortableLongToDouble(com.facebook.presto.operator.aggregation.FloatingPointBitsConverterUtil.sortableLongToDouble) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) OutputFunction(com.facebook.presto.spi.function.OutputFunction)

Example 10 with QuantileDigest

use of com.facebook.airlift.stats.QuantileDigest in project presto by prestodb.

the class ApproximateLongPercentileArrayAggregations method addInput.

private static void addInput(@AggregationState DigestAndPercentileArrayState state, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.BIGINT) long weight, @SqlType("array(double)") Block percentilesArrayBlock, @SqlType(StandardTypes.DOUBLE) double accuracy) {
    initializePercentilesArray(state, percentilesArrayBlock);
    QuantileDigest digest = state.getDigest();
    if (state.getDigest() == null) {
        checkAccuracy(accuracy);
        digest = new QuantileDigest(accuracy);
        state.setDigest(digest);
    } else {
        state.addMemoryUsage(-digest.estimatedInMemorySizeInBytes());
    }
    digest.add(value, weight);
    state.addMemoryUsage(digest.estimatedInMemorySizeInBytes());
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest)

Aggregations

QuantileDigest (com.facebook.airlift.stats.QuantileDigest)29 Test (org.testng.annotations.Test)8 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)7 OutputFunction (com.facebook.presto.spi.function.OutputFunction)6 SqlVarbinary (com.facebook.presto.common.type.SqlVarbinary)4 Description (com.facebook.presto.spi.function.Description)4 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)4 SqlType (com.facebook.presto.spi.function.SqlType)4 CombineFunction (com.facebook.presto.spi.function.CombineFunction)2 SliceInput (io.airlift.slice.SliceInput)2 Block (com.facebook.presto.common.block.Block)1 DoubleType (com.facebook.presto.common.type.DoubleType)1 Type (com.facebook.presto.common.type.Type)1 FloatingPointBitsConverterUtil.sortableLongToDouble (com.facebook.presto.operator.aggregation.FloatingPointBitsConverterUtil.sortableLongToDouble)1 SqlNullable (com.facebook.presto.spi.function.SqlNullable)1 ImmutableList (com.google.common.collect.ImmutableList)1