Search in sources :

Example 1 with QuantileDigest

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

the class TestMergeQuantileDigestFunction method getSequenceBlocks.

@Override
public Block[] getSequenceBlocks(int start, int length) {
    Type type = QDIGEST.createType(ImmutableList.of(TypeParameter.of(DoubleType.DOUBLE)));
    BlockBuilder blockBuilder = type.createBlockBuilder(null, length);
    for (int i = start; i < start + length; i++) {
        QuantileDigest qdigest = new QuantileDigest(0.0);
        qdigest.add(i);
        type.writeSlice(blockBuilder, qdigest.serialize());
    }
    return new Block[] { blockBuilder.build() };
}
Also used : Type(com.facebook.presto.common.type.Type) DoubleType(com.facebook.presto.common.type.DoubleType) QuantileDigest(com.facebook.airlift.stats.QuantileDigest) Block(com.facebook.presto.common.block.Block) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 2 with QuantileDigest

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

the class TestQuantileDigestAggregationFunction method getExpectedValueDoubles.

@Override
protected Object getExpectedValueDoubles(double maxError, double... values) {
    if (values.length == 0) {
        return null;
    }
    QuantileDigest qdigest = new QuantileDigest(maxError);
    Arrays.stream(values).forEach(value -> qdigest.add(doubleToSortableLong(value)));
    return new SqlVarbinary(qdigest.serialize().getBytes());
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary)

Example 3 with QuantileDigest

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

the class QuantileDigestFunctions method valuesAtQuantilesReal.

@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(real)")
public static Block valuesAtQuantilesReal(@SqlType("qdigest(real)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
    QuantileDigest digest = new QuantileDigest(input);
    BlockBuilder output = REAL.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
    for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
        REAL.writeLong(output, floatToRawIntBits(sortableIntToFloat((int) 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 4 with QuantileDigest

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

the class QuantileDigestFunctions method quantileAtValueBigint.

@ScalarFunction("quantile_at_value")
@Description("Given an input x between min/max values of qdigest, find which quantile is represented by that value")
@SqlType(StandardTypes.DOUBLE)
@SqlNullable
public static Double quantileAtValueBigint(@SqlType("qdigest(bigint)") Slice input, @SqlType(StandardTypes.BIGINT) long value) {
    QuantileDigest digest = new QuantileDigest(input);
    if (digest.getCount() == 0 || value > digest.getMax() || value < digest.getMin()) {
        return null;
    }
    double bucketCount = digest.getHistogram(ImmutableList.of(value)).get(0).getCount();
    return bucketCount / digest.getCount();
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 5 with QuantileDigest

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

the class QuantileDigestFunctions method valuesAtQuantilesDouble.

@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(double)")
public static Block valuesAtQuantilesDouble(@SqlType("qdigest(double)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
    QuantileDigest digest = new QuantileDigest(input);
    BlockBuilder output = DOUBLE.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
    for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
        DOUBLE.writeDouble(output, sortableLongToDouble(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)

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