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);
}
Aggregations