Search in sources :

Example 16 with SqlVarbinary

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

the class TestStringFunctions method testUpper.

@Test
public void testUpper() {
    assertFunction("UPPER('')", createVarcharType(0), "");
    assertFunction("UPPER('Hello World')", createVarcharType(11), "HELLO WORLD");
    assertFunction("UPPER('what!!')", createVarcharType(6), "WHAT!!");
    assertFunction("UPPER('\u00D6sterreich')", createVarcharType(10), upperByCodePoint("\u00D6") + "STERREICH");
    assertFunction("UPPER('From\uD801\uDC2DTo')", createVarcharType(7), "FROM" + upperByCodePoint("\uD801\uDC2D") + "TO");
    assertFunction("CAST(UPPER(utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { (byte) 0xCE }));
    assertFunction("CAST(UPPER('hello' || utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { 'H', 'E', 'L', 'L', 'O', (byte) 0xCE }));
    assertFunction("CAST(UPPER(utf8(from_hex('CE')) || 'hello') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { (byte) 0xCE, 'H', 'E', 'L', 'L', 'O' }));
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 17 with SqlVarbinary

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

the class TestStringFunctions method testReverse.

@Test
public void testReverse() {
    assertFunction("REVERSE('')", createVarcharType(0), "");
    assertFunction("REVERSE('hello')", createVarcharType(5), "olleh");
    assertFunction("REVERSE('Quadratically')", createVarcharType(13), "yllacitardauQ");
    assertFunction("REVERSE('racecar')", createVarcharType(7), "racecar");
    // Test REVERSE for non-ASCII
    assertFunction("REVERSE('\u4FE1\u5FF5,\u7231,\u5E0C\u671B')", createVarcharType(7), "\u671B\u5E0C,\u7231,\u5FF5\u4FE1");
    assertFunction("REVERSE('\u00D6sterreich')", createVarcharType(10), "hcierrets\u00D6");
    assertFunction("REVERSE('na\u00EFve')", createVarcharType(5), "ev\u00EFan");
    assertFunction("REVERSE('\uD801\uDC2Dend')", createVarcharType(4), "dne\uD801\uDC2D");
    assertFunction("CAST(REVERSE(utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { (byte) 0xCE }));
    assertFunction("CAST(REVERSE('hello' || utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { (byte) 0xCE, 'o', 'l', 'l', 'e', 'h' }));
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 18 with SqlVarbinary

use of com.facebook.presto.common.type.SqlVarbinary 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);
}
Also used : TDigest(com.facebook.presto.tdigest.TDigest) TDigest.createTDigest(com.facebook.presto.tdigest.TDigest.createTDigest) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 19 with SqlVarbinary

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

the class TestTDigestFunctions method testGetQuantileAtValueOutsideRange.

@Test
public void testGetQuantileAtValueOutsideRange() {
    TDigest tDigest = createTDigest(STANDARD_COMPRESSION_FACTOR);
    for (int i = 0; i < NUMBER_OF_ENTRIES; i++) {
        double value = Math.random() * NUMBER_OF_ENTRIES;
        tDigest.add(value);
    }
    functionAssertions.assertFunction(format("quantile_at_value(CAST(X'%s' AS tdigest(%s)), %s) = 1", new SqlVarbinary(tDigest.serialize().getBytes()).toString().replaceAll("\\s+", " "), DOUBLE, 1_000_000_000d), BOOLEAN, true);
    functionAssertions.assertFunction(format("quantile_at_value(CAST(X'%s' AS tdigest(%s)), %s) = 0", new SqlVarbinary(tDigest.serialize().getBytes()).toString().replaceAll("\\s+", " "), DOUBLE, -500d), BOOLEAN, true);
}
Also used : TDigest(com.facebook.presto.tdigest.TDigest) TDigest.createTDigest(com.facebook.presto.tdigest.TDigest.createTDigest) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 20 with SqlVarbinary

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

the class TestKHyperLogLogAggregationFunction method testAggregation.

private void testAggregation(Type valueType, List<?> values, Type uiiType, List<?> uiis) {
    InternalAggregationFunction aggregationFunction = getAggregation(valueType, uiiType);
    KHyperLogLog khll = null;
    long value;
    long uii;
    for (int i = 0; i < values.size(); i++) {
        if (values.get(i) == null || uiis.get(i) == null) {
            continue;
        }
        if (khll == null) {
            khll = new KHyperLogLog();
        }
        value = toLong(values.get(i), valueType);
        uii = toLong(uiis.get(i), uiiType);
        if (valueType == VARCHAR) {
            khll.add((Slice) values.get(i), uii);
        } else {
            khll.add(value, uii);
        }
    }
    assertAggregation(aggregationFunction, (khll == null) ? null : new SqlVarbinary(khll.serialize().getBytes()), buildBlock(values, valueType), buildBlock(uiis, uiiType));
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction)

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