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