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