Search in sources :

Example 1 with TDigest

use of com.facebook.presto.tdigest.TDigest in project presto by prestodb.

the class TestMergeTDigestFunction method getSequenceBlocks.

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

Example 2 with TDigest

use of com.facebook.presto.tdigest.TDigest in project presto by prestodb.

the class TDigestFunctions method scaleTDigestDouble.

@ScalarFunction(value = "scale_tdigest", visibility = EXPERIMENTAL)
@Description("Scale a t-digest according to a new weight")
@SqlType("tdigest(double)")
public static Slice scaleTDigestDouble(@SqlType("tdigest(double)") Slice input, @SqlType(StandardTypes.DOUBLE) double scale) {
    checkCondition(scale > 0, INVALID_FUNCTION_ARGUMENT, "Scale factor should be positive.");
    TDigest digest = createTDigest(input);
    digest.scale(scale);
    return digest.serialize();
}
Also used : TDigest.createTDigest(com.facebook.presto.tdigest.TDigest.createTDigest) TDigest(com.facebook.presto.tdigest.TDigest) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 3 with TDigest

use of com.facebook.presto.tdigest.TDigest in project presto by prestodb.

the class TDigestFunctions method valuesAtQuantilesDouble.

@ScalarFunction(value = "values_at_quantiles", visibility = EXPERIMENTAL)
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the tdigest is qn.")
@SqlType("array(double)")
public static Block valuesAtQuantilesDouble(@SqlType("tdigest(double)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
    TDigest tDigest = createTDigest(input);
    BlockBuilder output = DOUBLE.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
    for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
        DOUBLE.writeDouble(output, tDigest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i)));
    }
    return output.build();
}
Also used : TDigest.createTDigest(com.facebook.presto.tdigest.TDigest.createTDigest) TDigest(com.facebook.presto.tdigest.TDigest) 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 TDigest

use of com.facebook.presto.tdigest.TDigest in project presto by prestodb.

the class TDigestFunctions method quantilesAtValuesDouble.

@ScalarFunction(value = "quantiles_at_values", visibility = EXPERIMENTAL)
@Description("For each input x between min/max values of t-digest, find which quantile is represented by that value")
@SqlType("array(double)")
public static Block quantilesAtValuesDouble(@SqlType("tdigest(double)") Slice input, @SqlType("array(double)") Block valuesArrayBlock) {
    TDigest tDigest = createTDigest(input);
    BlockBuilder output = DOUBLE.createBlockBuilder(null, valuesArrayBlock.getPositionCount());
    for (int i = 0; i < valuesArrayBlock.getPositionCount(); i++) {
        DOUBLE.writeDouble(output, tDigest.getCdf(DOUBLE.getDouble(valuesArrayBlock, i)));
    }
    return output.build();
}
Also used : TDigest.createTDigest(com.facebook.presto.tdigest.TDigest.createTDigest) TDigest(com.facebook.presto.tdigest.TDigest) 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 5 with TDigest

use of com.facebook.presto.tdigest.TDigest in project presto by prestodb.

the class TestTDigestFunctions method testScale.

@Test
public void testScale() {
    TDigest tDigest = createTDigest(STANDARD_COMPRESSION_FACTOR);
    addAll(tDigest, 0.0d, 1.0d, 2.0d, 3.0d, 4.0d, 5.0d, 6.0d, 7.0d, 8.0d, 9.0d);
    // Before scaling.
    List<Double> unscaledFrequencies = getFrequencies(tDigest, asList(2.0d, 4.0d, 6.0d, 8.0d));
    // Scale up.
    SqlVarbinary sqlVarbinary = functionAssertions.selectSingleValue(format("scale_tdigest(CAST(X'%s' AS tdigest(double)), 2)", new SqlVarbinary(tDigest.serialize().getBytes()).toString().replaceAll("\\s+", " ")), TDIGEST_DOUBLE, SqlVarbinary.class);
    TDigest scaledTdigest = createTDigest(wrappedBuffer(sqlVarbinary.getBytes()));
    List<Double> scaledDigestFrequencies = getFrequencies(scaledTdigest, asList(2.0d, 4.0d, 6.0d, 8.0d));
    List<Double> scaledUpFrequencies = new ArrayList<>();
    unscaledFrequencies.forEach(frequency -> scaledUpFrequencies.add(frequency * 2));
    assertEquals(scaledDigestFrequencies, scaledUpFrequencies);
    // Scale down.
    sqlVarbinary = functionAssertions.selectSingleValue(format("scale_tdigest(CAST(X'%s' AS tdigest(double)), 0.5)", new SqlVarbinary(tDigest.serialize().getBytes()).toString().replaceAll("\\s+", " ")), TDIGEST_DOUBLE, SqlVarbinary.class);
    scaledTdigest = createTDigest(wrappedBuffer(sqlVarbinary.getBytes()));
    scaledDigestFrequencies = getFrequencies(scaledTdigest, asList(2.0d, 4.0d, 6.0d, 8.0d));
    List<Double> scaledDownFrequencies = new ArrayList<>();
    unscaledFrequencies.forEach(frequency -> scaledDownFrequencies.add(frequency * 0.5));
    assertEquals(scaledDigestFrequencies, scaledDownFrequencies);
}
Also used : TDigest(com.facebook.presto.tdigest.TDigest) TDigest.createTDigest(com.facebook.presto.tdigest.TDigest.createTDigest) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Aggregations

TDigest (com.facebook.presto.tdigest.TDigest)27 TDigest.createTDigest (com.facebook.presto.tdigest.TDigest.createTDigest)27 Test (org.testng.annotations.Test)21 ArrayList (java.util.ArrayList)18 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)8 SqlVarbinary (com.facebook.presto.common.type.SqlVarbinary)6 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)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 Block (com.facebook.presto.common.block.Block)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 DoubleType (com.facebook.presto.common.type.DoubleType)1 Type (com.facebook.presto.common.type.Type)1 Centroid (com.facebook.presto.tdigest.Centroid)1 BinomialDistribution (org.apache.commons.math3.distribution.BinomialDistribution)1 GeometricDistribution (org.apache.commons.math3.distribution.GeometricDistribution)1 PoissonDistribution (org.apache.commons.math3.distribution.PoissonDistribution)1