Search in sources :

Example 6 with SqlVarbinary

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

the class TestUuidOperators method testUUIDToVarbinaryCast.

@Test
public void testUUIDToVarbinaryCast() {
    assertFunction("CAST(UUID '00000000-0000-0000-0000-000000000000' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("00000000000000000000000000000000")));
    assertFunction("CAST(UUID '6b5f5b65-67e4-43b0-8ee3-586cd49f58a0' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("B043E467655B5F6BA0589FD46C58E38E")));
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 7 with SqlVarbinary

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

the class TestExpressionCompiler method smokedTest.

@Test
public void smokedTest() throws Exception {
    assertExecute("cast(true as boolean)", BOOLEAN, true);
    assertExecute("true", BOOLEAN, true);
    assertExecute("false", BOOLEAN, false);
    assertExecute("42", INTEGER, 42);
    assertExecute("'foo'", createVarcharType(3), "foo");
    assertExecute("4.2E0", DOUBLE, 4.2);
    assertExecute("10000000000 + 1", BIGINT, 10000000001L);
    assertExecute("4.2", createDecimalType(2, 1), new SqlDecimal(BigInteger.valueOf(42), 2, 1));
    assertExecute("DECIMAL '4.2'", createDecimalType(2, 1), new SqlDecimal(BigInteger.valueOf(42), 2, 1));
    assertExecute("X' 1 f'", VARBINARY, new SqlVarbinary(Slices.wrappedBuffer((byte) 0x1f).getBytes()));
    assertExecute("X' '", VARBINARY, new SqlVarbinary(new byte[0]));
    assertExecute("bound_integer", INTEGER, 1234);
    assertExecute("bound_long", BIGINT, 1234L);
    assertExecute("bound_string", VARCHAR, "hello");
    assertExecute("bound_double", DOUBLE, 12.34);
    assertExecute("bound_boolean", BOOLEAN, true);
    assertExecute("bound_timestamp", BIGINT, new DateTime(2001, 8, 22, 3, 4, 5, 321, UTC).getMillis());
    assertExecute("bound_pattern", VARCHAR, "%el%");
    assertExecute("bound_null_string", VARCHAR, null);
    assertExecute("bound_timestamp_with_timezone", TIMESTAMP_WITH_TIME_ZONE, new SqlTimestampWithTimeZone(new DateTime(1970, 1, 1, 0, 1, 0, 999, DateTimeZone.UTC).getMillis(), TimeZoneKey.getTimeZoneKey("Z")));
    assertExecute("bound_binary_literal", VARBINARY, new SqlVarbinary(new byte[] { (byte) 0xab }));
    // todo enable when null output type is supported
    // assertExecute("null", null);
    Futures.allAsList(futures).get();
}
Also used : SqlTimestampWithTimeZone(com.facebook.presto.common.type.SqlTimestampWithTimeZone) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 8 with SqlVarbinary

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

the class LiteralInterpreter method evaluate.

public static Object evaluate(ConnectorSession session, ConstantExpression node) {
    Type type = node.getType();
    SqlFunctionProperties properties = session.getSqlFunctionProperties();
    if (node.getValue() == null) {
        return null;
    }
    if (type instanceof BooleanType) {
        return node.getValue();
    }
    if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
        return node.getValue();
    }
    if (type instanceof DoubleType) {
        return node.getValue();
    }
    if (type instanceof RealType) {
        Long number = (Long) node.getValue();
        return intBitsToFloat(number.intValue());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            checkState(node.getValue() instanceof Long);
            return decodeDecimal(BigInteger.valueOf((long) node.getValue()), decimalType);
        }
        checkState(node.getValue() instanceof Slice);
        Slice value = (Slice) node.getValue();
        return decodeDecimal(decodeUnscaledValue(value), decimalType);
    }
    if (type instanceof VarcharType || type instanceof CharType) {
        return ((Slice) node.getValue()).toStringUtf8();
    }
    if (type instanceof VarbinaryType) {
        return new SqlVarbinary(((Slice) node.getValue()).getBytes());
    }
    if (type instanceof DateType) {
        return new SqlDate(((Long) node.getValue()).intValue());
    }
    if (type instanceof TimeType) {
        if (properties.isLegacyTimestamp()) {
            return new SqlTime((long) node.getValue(), properties.getTimeZoneKey());
        }
        return new SqlTime((long) node.getValue());
    }
    if (type instanceof TimestampType) {
        try {
            if (properties.isLegacyTimestamp()) {
                return new SqlTimestamp((long) node.getValue(), properties.getTimeZoneKey());
            }
            return new SqlTimestamp((long) node.getValue());
        } catch (RuntimeException e) {
            throw new PrestoException(GENERIC_USER_ERROR, format("'%s' is not a valid timestamp literal", (String) node.getValue()));
        }
    }
    if (type instanceof IntervalDayTimeType) {
        return new SqlIntervalDayTime((long) node.getValue());
    }
    if (type instanceof IntervalYearMonthType) {
        return new SqlIntervalYearMonth(((Long) node.getValue()).intValue());
    }
    if (type.getJavaType().equals(Slice.class)) {
        // DO NOT ever remove toBase64. Calling toString directly on Slice whose base is not byte[] will cause JVM to crash.
        return "'" + VarbinaryFunctions.toBase64((Slice) node.getValue()).toStringUtf8() + "'";
    }
    // We should not fail at the moment; just return the raw value (block, regex, etc) to the user
    return node.getValue();
}
Also used : IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlTime(com.facebook.presto.common.type.SqlTime) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) PrestoException(com.facebook.presto.spi.PrestoException) RealType(com.facebook.presto.common.type.RealType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TimeType(com.facebook.presto.common.type.TimeType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) SmallintType(com.facebook.presto.common.type.SmallintType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) BooleanType(com.facebook.presto.common.type.BooleanType) IntegerType(com.facebook.presto.common.type.IntegerType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) TimeType(com.facebook.presto.common.type.TimeType) DateType(com.facebook.presto.common.type.DateType) DoubleType(com.facebook.presto.common.type.DoubleType) DoubleType(com.facebook.presto.common.type.DoubleType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlDate(com.facebook.presto.common.type.SqlDate) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

Example 9 with SqlVarbinary

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

the class TestIpAddressOperators method testIpAddressToVarbinaryCast.

@Test
public void testIpAddressToVarbinaryCast() {
    assertFunction("CAST(IPADDRESS '::ffff:1.2.3.4' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("00000000000000000000FFFF01020304")));
    assertFunction("CAST(IPADDRESS '2001:0db8:0000:0000:0000:ff00:0042:8329' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("20010DB8000000000000FF0000428329")));
    assertFunction("CAST(IPADDRESS '2001:db8::ff00:42:8329' AS VARBINARY)", VARBINARY, new SqlVarbinary(base16().decode("20010DB8000000000000FF0000428329")));
}
Also used : SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) Test(org.testng.annotations.Test)

Example 10 with SqlVarbinary

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

the class TestTDigestAggregationFunction method testAggregationDoubles.

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

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