Search in sources :

Example 26 with SqlVarbinary

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

the class TestStringFunctions method testLower.

@Test
public void testLower() {
    assertFunction("LOWER('')", createVarcharType(0), "");
    assertFunction("LOWER('Hello World')", createVarcharType(11), "hello world");
    assertFunction("LOWER('WHAT!!')", createVarcharType(6), "what!!");
    assertFunction("LOWER('\u00D6STERREICH')", createVarcharType(10), lowerByCodePoint("\u00D6sterreich"));
    assertFunction("LOWER('From\uD801\uDC2DTo')", createVarcharType(7), lowerByCodePoint("from\uD801\uDC2Dto"));
    assertFunction("CAST(LOWER(utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { (byte) 0xCE }));
    assertFunction("CAST(LOWER('HELLO' || utf8(from_hex('CE'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { 'h', 'e', 'l', 'l', 'o', (byte) 0xCE }));
    assertFunction("CAST(LOWER(utf8(from_hex('CE')) || 'HELLO') AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { (byte) 0xCE, 'h', 'e', 'l', 'l', 'o' }));
    assertFunction("CAST(LOWER(utf8(from_hex('C8BAFF'))) AS VARBINARY)", VARBINARY, new SqlVarbinary(new byte[] { (byte) 0xE2, (byte) 0xB1, (byte) 0xA5, (byte) 0xFF }));
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 27 with SqlVarbinary

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

the class TestTDigestFunctions method testDestructureTDigest.

@Test
public void testDestructureTDigest() {
    TDigest tDigest = createTDigest(STANDARD_COMPRESSION_FACTOR);
    ImmutableList<Double> values = ImmutableList.of(0.0d, 1.0d, 2.0d, 3.0d, 4.0d, 5.0d, 6.0d, 7.0d, 8.0d, 9.0d);
    values.stream().forEach(tDigest::add);
    List<Integer> weights = Collections.nCopies(values.size(), 1);
    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(sql, TDIGEST_CENTROIDS_ROW_TYPE, ImmutableList.of(values, weights, compression, min, max, sum, count));
    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);
    functionAssertions.assertFunction(format("%s.centroid_means", sql), new ArrayType(DOUBLE), values);
    functionAssertions.assertFunction(format("%s.centroid_weights", sql), new ArrayType(INTEGER), weights);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) 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 28 with SqlVarbinary

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

the class TestTDigestFunctions method testScaleNegative.

@Test(expectedExceptions = PrestoException.class, expectedExceptionsMessageRegExp = "Scale factor should be positive\\.")
public void testScaleNegative() {
    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);
    functionAssertions.selectSingleValue(format("scale_tdigest(CAST(X'%s' AS tdigest(double)), -1)", new SqlVarbinary(tDigest.serialize().getBytes()).toString().replaceAll("\\s+", " ")), TDIGEST_DOUBLE, SqlVarbinary.class);
}
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 29 with SqlVarbinary

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

the class OrcTester method testValue.

private static boolean testValue(Type type, Object value, TupleDomainFilter filter) {
    if (value == null) {
        return filter.testNull();
    }
    if (filter == IS_NULL) {
        return false;
    }
    if (filter == IS_NOT_NULL) {
        return true;
    }
    if (type == BOOLEAN) {
        return filter.testBoolean((Boolean) value);
    }
    if (type == TINYINT || type == BIGINT || type == INTEGER || type == SMALLINT) {
        return filter.testLong(((Number) value).longValue());
    }
    if (type == REAL) {
        return filter.testFloat(((Number) value).floatValue());
    }
    if (type == DOUBLE) {
        return filter.testDouble((double) value);
    }
    if (type == DATE) {
        return filter.testLong(((SqlDate) value).getDays());
    }
    if (type == TIMESTAMP) {
        return filter.testLong(((SqlTimestamp) value).getMillisUtc());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        BigDecimal bigDecimal = ((SqlDecimal) value).toBigDecimal();
        if (decimalType.isShort()) {
            return filter.testLong(bigDecimal.unscaledValue().longValue());
        } else {
            Slice encodedDecimal = Decimals.encodeScaledValue(bigDecimal);
            return filter.testDecimal(encodedDecimal.getLong(0), encodedDecimal.getLong(Long.BYTES));
        }
    }
    if (type == VARCHAR) {
        return filter.testBytes(((String) value).getBytes(), 0, ((String) value).length());
    }
    if (type instanceof CharType) {
        String charString = String.valueOf(value);
        return filter.testBytes(charString.getBytes(), 0, charString.length());
    }
    if (type == VARBINARY) {
        byte[] binary = ((SqlVarbinary) value).getBytes();
        return filter.testBytes(binary, 0, binary.length);
    }
    fail("Unsupported type: " + type);
    return false;
}
Also used : Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) DecimalType(com.facebook.presto.common.type.DecimalType) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) CharType(com.facebook.presto.common.type.CharType) BigDecimal(java.math.BigDecimal)

Example 30 with SqlVarbinary

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

the class TestMapOperators method testMapKeys.

@Test
public void testMapKeys() {
    assertFunction("MAP_KEYS(MAP(ARRAY['1', '3'], ARRAY['2', '4']))", new ArrayType(createVarcharType(1)), ImmutableList.of("1", "3"));
    assertFunction("MAP_KEYS(MAP(ARRAY[1.0E0, 2.0E0], ARRAY[ARRAY[1, 2], ARRAY[3]]))", new ArrayType(DOUBLE), ImmutableList.of(1.0, 2.0));
    assertFunction("MAP_KEYS(MAP(ARRAY['puppies'], ARRAY['kittens']))", new ArrayType(createVarcharType(7)), ImmutableList.of("puppies"));
    assertFunction("MAP_KEYS(MAP(ARRAY[TRUE], ARRAY[2]))", new ArrayType(BOOLEAN), ImmutableList.of(true));
    assertFunction("MAP_KEYS(MAP(ARRAY[TIMESTAMP '1970-01-01 00:00:01'], ARRAY[1.0E0]))", new ArrayType(TIMESTAMP), ImmutableList.of(sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0, TEST_SESSION)));
    assertFunction("MAP_KEYS(MAP(ARRAY[CAST('puppies' as varbinary)], ARRAY['kittens']))", new ArrayType(VARBINARY), ImmutableList.of(new SqlVarbinary("puppies".getBytes(UTF_8))));
    assertFunction("MAP_KEYS(MAP(ARRAY[1,2],  ARRAY[ARRAY[1, 2], ARRAY[3]]))", new ArrayType(INTEGER), ImmutableList.of(1, 2));
    assertFunction("MAP_KEYS(MAP(ARRAY[1,4], ARRAY[MAP(ARRAY[2], ARRAY[3]), MAP(ARRAY[5], ARRAY[6])]))", new ArrayType(INTEGER), ImmutableList.of(1, 4));
    assertFunction("MAP_KEYS(MAP(ARRAY [ARRAY [1], ARRAY [2, 3]],  ARRAY [ARRAY [3, 4], ARRAY [5]]))", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1), ImmutableList.of(2, 3)));
    assertFunction("MAP_KEYS(MAP(ARRAY [1.0, 383838383838383.12324234234234], ARRAY [2.2, 3.3]))", new ArrayType(createDecimalType(29, 14)), ImmutableList.of(decimal("000000000000001.00000000000000"), decimal("383838383838383.12324234234234")));
    assertFunction("MAP_KEYS(MAP(ARRAY [1.0, 2.01], ARRAY [2.2, 3.3]))", new ArrayType(createDecimalType(3, 2)), ImmutableList.of(decimal("1.00"), decimal("2.01")));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) 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