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")));
}
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();
}
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();
}
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")));
}
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);
}
Aggregations