Search in sources :

Example 1 with Centroid

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

the class TDigestFunctions method destructureTDigest.

@ScalarFunction(value = "destructure_tdigest", visibility = EXPERIMENTAL)
@Description("Return the raw TDigest, including arrays of centroid means and weights, as well as min, max, sum, count, and compression factor.")
@SqlType("row(centroid_means array(double), centroid_weights array(integer), compression double, min double, max double, sum double, count bigint)")
public static Block destructureTDigest(@SqlType("tdigest(double)") Slice input) {
    TDigest tDigest = createTDigest(input);
    BlockBuilder blockBuilder = TDIGEST_CENTROIDS_ROW_TYPE.createBlockBuilder(null, 1);
    BlockBuilder rowBuilder = blockBuilder.beginBlockEntry();
    // Centroid means / weights
    BlockBuilder meansBuilder = DOUBLE.createBlockBuilder(null, tDigest.centroidCount());
    BlockBuilder weightsBuilder = INTEGER.createBlockBuilder(null, tDigest.centroidCount());
    for (Centroid centroid : tDigest.centroids()) {
        int weight = (int) centroid.getWeight();
        DOUBLE.writeDouble(meansBuilder, centroid.getMean());
        INTEGER.writeLong(weightsBuilder, weight);
    }
    rowBuilder.appendStructure(meansBuilder);
    rowBuilder.appendStructure(weightsBuilder);
    // Compression, min, max, sum, count
    DOUBLE.writeDouble(rowBuilder, tDigest.getCompressionFactor());
    DOUBLE.writeDouble(rowBuilder, tDigest.getMin());
    DOUBLE.writeDouble(rowBuilder, tDigest.getMax());
    DOUBLE.writeDouble(rowBuilder, tDigest.getSum());
    BIGINT.writeLong(rowBuilder, (long) tDigest.getSize());
    blockBuilder.closeEntry();
    return TDIGEST_CENTROIDS_ROW_TYPE.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : Centroid(com.facebook.presto.tdigest.Centroid) 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)

Aggregations

BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 Description (com.facebook.presto.spi.function.Description)1 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)1 SqlType (com.facebook.presto.spi.function.SqlType)1 Centroid (com.facebook.presto.tdigest.Centroid)1 TDigest (com.facebook.presto.tdigest.TDigest)1 TDigest.createTDigest (com.facebook.presto.tdigest.TDigest.createTDigest)1