Search in sources :

Example 11 with SqlVarbinary

use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.

the class TestMergeHyperLogLogAggregation method getExpectedValue.

@Override
public Object getExpectedValue(int start, int length) {
    if (length == 0) {
        return null;
    }
    HyperLogLog hll = HyperLogLog.newInstance(NUMBER_OF_BUCKETS);
    for (int i = start; i < start + length; i++) {
        hll.add(i);
    }
    hll.makeDense();
    return new SqlVarbinary(hll.serialize().getBytes());
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) HyperLogLog(com.facebook.airlift.stats.cardinality.HyperLogLog)

Example 12 with SqlVarbinary

use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.

the class TestQuantileDigestAggregationFunction method getExpectedValueDoubles.

@Override
protected Object getExpectedValueDoubles(double maxError, double... values) {
    if (values.length == 0) {
        return null;
    }
    QuantileDigest qdigest = new QuantileDigest(maxError);
    Arrays.stream(values).forEach(value -> qdigest.add(doubleToSortableLong(value)));
    return new SqlVarbinary(qdigest.serialize().getBytes());
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary)

Example 13 with SqlVarbinary

use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.

the class TestQuantileDigestAggregationFunction method testAggregationReal.

private void testAggregationReal(InternalAggregationFunction function, Page page, double maxError, float... inputs) {
    assertAggregation(function, QDIGEST_EQUALITY, "test multiple positions", page, getExpectedValuesFloats(maxError, inputs));
    // test scalars
    List<Double> rows = Floats.asList(inputs).stream().sorted().map(Float::doubleValue).collect(Collectors.toList());
    SqlVarbinary returned = (SqlVarbinary) AggregationTestUtils.aggregation(function, page);
    assertPercentileWithinError(QDIGEST, StandardTypes.REAL, returned, maxError, rows, 0.1, 0.5, 0.9, 0.99);
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary)

Example 14 with SqlVarbinary

use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.

the class TestQuantileDigestAggregationFunction method testAggregationDoubles.

@Override
protected void testAggregationDoubles(InternalAggregationFunction function, Page page, double maxError, double... inputs) {
    assertAggregation(function, QDIGEST_EQUALITY, "test multiple positions", page, getExpectedValueDoubles(maxError, inputs));
    // test scalars
    List<Double> rows = Doubles.asList(inputs).stream().sorted().collect(Collectors.toList());
    SqlVarbinary returned = (SqlVarbinary) AggregationTestUtils.aggregation(function, page);
    assertPercentileWithinError(QDIGEST, StandardTypes.DOUBLE, returned, maxError, rows, 0.1, 0.5, 0.9, 0.99);
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary)

Example 15 with SqlVarbinary

use of com.facebook.presto.common.type.SqlVarbinary in project presto by prestodb.

the class TestQuantileDigestFunctions method testScale.

@Test
public void testScale() {
    QuantileDigest qdigest = new QuantileDigest(1);
    addAll(qdigest, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    // Before scaling.
    assertEquals(qdigest.getHistogram(asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)), asList(new QuantileDigest.Bucket(0, Double.NaN), new QuantileDigest.Bucket(1, 0), new QuantileDigest.Bucket(1, 1), new QuantileDigest.Bucket(1, 2), new QuantileDigest.Bucket(1, 3), new QuantileDigest.Bucket(1, 4), new QuantileDigest.Bucket(1, 5), new QuantileDigest.Bucket(1, 6), new QuantileDigest.Bucket(1, 7), new QuantileDigest.Bucket(1, 8), new QuantileDigest.Bucket(1, 9)));
    // Scale up.
    SqlVarbinary sqlVarbinary = functionAssertions.selectSingleValue(format("scale_qdigest(CAST(X'%s' AS qdigest(bigint)), 2)", toHexString(qdigest)), QDIGEST_BIGINT, SqlVarbinary.class);
    QuantileDigest scaledQdigest = new QuantileDigest(wrappedBuffer(sqlVarbinary.getBytes()));
    assertEquals(scaledQdigest.getHistogram(asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)), asList(new QuantileDigest.Bucket(0, Double.NaN), new QuantileDigest.Bucket(2, 0), new QuantileDigest.Bucket(2, 1), new QuantileDigest.Bucket(2, 2), new QuantileDigest.Bucket(2, 3), new QuantileDigest.Bucket(2, 4), new QuantileDigest.Bucket(2, 5), new QuantileDigest.Bucket(2, 6), new QuantileDigest.Bucket(2, 7), new QuantileDigest.Bucket(2, 8), new QuantileDigest.Bucket(2, 9)));
    // Scale down.
    sqlVarbinary = functionAssertions.selectSingleValue(format("scale_qdigest(CAST(X'%s' AS qdigest(bigint)), 0.5)", toHexString(qdigest)), QDIGEST_BIGINT, SqlVarbinary.class);
    scaledQdigest = new QuantileDigest(wrappedBuffer(sqlVarbinary.getBytes()));
    assertEquals(scaledQdigest.getHistogram(asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)), asList(new QuantileDigest.Bucket(0, Double.NaN), new QuantileDigest.Bucket(0.5, 0), new QuantileDigest.Bucket(0.5, 1), new QuantileDigest.Bucket(0.5, 2), new QuantileDigest.Bucket(0.5, 3), new QuantileDigest.Bucket(0.5, 4), new QuantileDigest.Bucket(0.5, 5), new QuantileDigest.Bucket(0.5, 6), new QuantileDigest.Bucket(0.5, 7), new QuantileDigest.Bucket(0.5, 8), new QuantileDigest.Bucket(0.5, 9)));
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) Test(org.testng.annotations.Test)

Aggregations

SqlVarbinary (com.facebook.presto.common.type.SqlVarbinary)40 Test (org.testng.annotations.Test)19 DecimalType (com.facebook.presto.common.type.DecimalType)10 SqlDecimal (com.facebook.presto.common.type.SqlDecimal)10 ArrayList (java.util.ArrayList)10 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)9 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 SqlDate (com.facebook.presto.common.type.SqlDate)7 Type (com.facebook.presto.common.type.Type)7 Slice (io.airlift.slice.Slice)7 Collectors.toList (java.util.stream.Collectors.toList)7 ArrayType (com.facebook.presto.common.type.ArrayType)6 CharType (com.facebook.presto.common.type.CharType)6 TDigest (com.facebook.presto.tdigest.TDigest)6 TDigest.createTDigest (com.facebook.presto.tdigest.TDigest.createTDigest)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)5 VarcharType (com.facebook.presto.common.type.VarcharType)5