Search in sources :

Example 21 with SqlVarbinary

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

the class TestQuantileDigestAggregationFunction method getExpectedValuesFloats.

private Object getExpectedValuesFloats(double maxError, float... values) {
    if (values.length == 0) {
        return null;
    }
    QuantileDigest qdigest = new QuantileDigest(maxError);
    Floats.asList(values).forEach(value -> qdigest.add(floatToSortableInt(value)));
    return new SqlVarbinary(qdigest.serialize().getBytes());
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary)

Example 22 with SqlVarbinary

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

the class TestQuantileDigestAggregationFunction method testAggregationBigints.

private void testAggregationBigints(InternalAggregationFunction function, Page page, double maxError, long... inputs) {
    // aggregate level
    assertAggregation(function, QDIGEST_EQUALITY, "test multiple positions", page, getExpectedValueLongs(maxError, inputs));
    // test scalars
    List<Long> rows = Arrays.stream(inputs).sorted().boxed().collect(Collectors.toList());
    SqlVarbinary returned = (SqlVarbinary) AggregationTestUtils.aggregation(function, page);
    assertPercentileWithinError(QDIGEST, StandardTypes.BIGINT, returned, maxError, rows, 0.1, 0.5, 0.9, 0.99);
}
Also used : FloatingPointBitsConverterUtil.doubleToSortableLong(com.facebook.presto.operator.aggregation.FloatingPointBitsConverterUtil.doubleToSortableLong) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary)

Example 23 with SqlVarbinary

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

the class TestQuantileDigestAggregationFunction method getExpectedValueLongs.

private Object getExpectedValueLongs(double maxError, long... values) {
    if (values.length == 0) {
        return null;
    }
    QuantileDigest qdigest = new QuantileDigest(maxError);
    Arrays.stream(values).forEach(qdigest::add);
    return new SqlVarbinary(qdigest.serialize().getBytes());
}
Also used : QuantileDigest(com.facebook.airlift.stats.QuantileDigest) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary)

Example 24 with SqlVarbinary

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

the class TestTDigestFunctions method testDestructureTDigestLarge.

@Test
public void testDestructureTDigestLarge() {
    TDigest tDigest = createTDigest(STANDARD_COMPRESSION_FACTOR);
    List<Double> values = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_ENTRIES; i++) {
        values.add((double) i);
    }
    values.stream().forEach(tDigest::add);
    double compression = Double.valueOf(STANDARD_COMPRESSION_FACTOR);
    double min = values.stream().reduce(Double.POSITIVE_INFINITY, Double::min);
    double max = values.stream().reduce(Double.NEGATIVE_INFINITY, Double::max);
    double sum = values.stream().reduce(0.0d, Double::sum);
    long count = values.size();
    String sql = format("destructure_tdigest(CAST(X'%s' AS tdigest(%s)))", new SqlVarbinary(tDigest.serialize().getBytes()).toString().replaceAll("\\s+", " "), DOUBLE);
    functionAssertions.assertFunction(format("%s.compression", sql), DOUBLE, compression);
    functionAssertions.assertFunction(format("%s.min", sql), DOUBLE, min);
    functionAssertions.assertFunction(format("%s.max", sql), DOUBLE, max);
    functionAssertions.assertFunction(format("%s.sum", sql), DOUBLE, sum);
    functionAssertions.assertFunction(format("%s.count", sql), BIGINT, count);
}
Also used : TDigest(com.facebook.presto.tdigest.TDigest) TDigest.createTDigest(com.facebook.presto.tdigest.TDigest.createTDigest) ArrayList(java.util.ArrayList) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 25 with SqlVarbinary

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

the class TestStringFunctions method testReplace.

@Test
public void testReplace() {
    assertFunction("REPLACE('aaa', 'a', 'aa')", createVarcharType(11), "aaaaaa");
    assertFunction("REPLACE('abcdefabcdef', 'cd', 'XX')", createVarcharType(38), "abXXefabXXef");
    assertFunction("REPLACE('abcdefabcdef', 'cd')", createVarcharType(12), "abefabef");
    assertFunction("REPLACE('123123tech', '123')", createVarcharType(10), "tech");
    assertFunction("REPLACE('123tech123', '123')", createVarcharType(10), "tech");
    assertFunction("REPLACE('222tech', '2', '3')", createVarcharType(15), "333tech");
    assertFunction("REPLACE('0000123', '0')", createVarcharType(7), "123");
    assertFunction("REPLACE('0000123', '0', ' ')", createVarcharType(15), "    123");
    assertFunction("REPLACE('foo', '')", createVarcharType(3), "foo");
    assertFunction("REPLACE('foo', '', '')", createVarcharType(3), "foo");
    assertFunction("REPLACE('foo', 'foo', '')", createVarcharType(3), "");
    assertFunction("REPLACE('abc', '', 'xx')", createVarcharType(11), "xxaxxbxxcxx");
    assertFunction("REPLACE('', '', 'xx')", createVarcharType(2), "xx");
    assertFunction("REPLACE('', '')", createVarcharType(0), "");
    assertFunction("REPLACE('', '', '')", createVarcharType(0), "");
    assertFunction("REPLACE('\u4FE1\u5FF5,\u7231,\u5E0C\u671B', ',', '\u2014')", createVarcharType(15), "\u4FE1\u5FF5\u2014\u7231\u2014\u5E0C\u671B");
    // \uD801\uDC2D is one character
    assertFunction("REPLACE('::\uD801\uDC2D::', ':', '')", createVarcharType(5), "\uD801\uDC2D");
    assertFunction("REPLACE('\u00D6sterreich', '\u00D6', 'Oe')", createVarcharType(32), "Oesterreich");
    assertFunction("CAST(REPLACE(utf8(from_hex('CE')), '', 'X') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { 'X', (byte) 0xCE, 'X' }));
    assertFunction("CAST(REPLACE('abc' || utf8(from_hex('CE')), '', 'X') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { 'X', 'a', 'X', 'b', 'X', 'c', 'X', (byte) 0xCE, 'X' }));
    assertFunction("CAST(REPLACE(utf8(from_hex('CE')) || 'xyz', '', 'X') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { 'X', (byte) 0xCE, 'X', 'x', 'X', 'y', 'X', 'z', 'X' }));
    assertFunction("CAST(REPLACE('abc' || utf8(from_hex('CE')) || 'xyz', '', 'X') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { 'X', 'a', 'X', 'b', 'X', 'c', 'X', (byte) 0xCE, 'X', 'x', 'X', 'y', 'X', 'z', 'X' }));
    String longString = new String(new char[1_000_000]);
    assertInvalidFunction(String.format("replace('%s', '', '%s')", longString, longString), "inputs to \"replace\" function are too large: when \"search\" parameter is empty, length of \"string\" times length of \"replace\" must not exceed 2147483647");
}
Also used : 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