use of com.facebook.presto.common.type.SqlDate 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.SqlDate in project presto by prestodb.
the class TestArrayOperators method sqlDate.
private static SqlDate sqlDate(String dateString) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setTimeZone(TimeZone.getTimeZone(ZoneId.of("UTC")));
return new SqlDate(toIntExact(TimeUnit.MILLISECONDS.toDays(dateFormat.parse(dateString).getTime())));
}
use of com.facebook.presto.common.type.SqlDate in project presto by prestodb.
the class TestDateBase method testLeast.
@Test
public void testLeast() {
int days = (int) TimeUnit.MILLISECONDS.toDays(new DateTime(2012, 5, 23, 0, 0, UTC).getMillis());
assertFunction("least(DATE '2013-03-30', DATE '2012-05-23')", DATE, new SqlDate(days));
assertFunction("least(DATE '2013-03-30', DATE '2012-05-23', DATE '2012-06-01')", DATE, new SqlDate(days));
}
use of com.facebook.presto.common.type.SqlDate in project presto by prestodb.
the class TestDateBase method testLiteral.
@Test
public void testLiteral() {
long millis = new DateTime(2001, 1, 22, 0, 0, UTC).getMillis();
assertFunction("DATE '2001-1-22'", DATE, new SqlDate((int) TimeUnit.MILLISECONDS.toDays(millis)));
}
use of com.facebook.presto.common.type.SqlDate in project presto by prestodb.
the class KuduPageSink method appendColumn.
private void appendColumn(PartialRow row, Page page, int position, int channel, int destChannel) {
Block block = page.getBlock(channel);
Type type = columnTypes.get(destChannel);
if (block.isNull(position)) {
row.setNull(destChannel);
} else if (TIMESTAMP.equals(type)) {
row.addLong(destChannel, type.getLong(block, position) * 1000);
} else if (REAL.equals(type)) {
row.addFloat(destChannel, intBitsToFloat((int) type.getLong(block, position)));
} else if (BIGINT.equals(type)) {
row.addLong(destChannel, type.getLong(block, position));
} else if (INTEGER.equals(type)) {
row.addInt(destChannel, (int) type.getLong(block, position));
} else if (SMALLINT.equals(type)) {
row.addShort(destChannel, (short) type.getLong(block, position));
} else if (TINYINT.equals(type)) {
row.addByte(destChannel, (byte) type.getLong(block, position));
} else if (BOOLEAN.equals(type)) {
row.addBoolean(destChannel, type.getBoolean(block, position));
} else if (DOUBLE.equals(type)) {
row.addDouble(destChannel, type.getDouble(block, position));
} else if (isVarcharType(type)) {
Type originalType = originalColumnTypes.get(destChannel);
if (DATE.equals(originalType)) {
SqlDate date = (SqlDate) originalType.getObjectValue(connectorSession.getSqlFunctionProperties(), block, position);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(TimeUnit.DAYS.toSeconds(date.getDays()), 0, ZoneOffset.UTC);
byte[] bytes = ldt.format(DateTimeFormatter.ISO_LOCAL_DATE).getBytes(StandardCharsets.UTF_8);
row.addStringUtf8(destChannel, bytes);
} else {
row.addString(destChannel, type.getSlice(block, position).toStringUtf8());
}
} else if (VARBINARY.equals(type)) {
row.addBinary(destChannel, type.getSlice(block, position).toByteBuffer());
} else if (type instanceof DecimalType) {
SqlDecimal sqlDecimal = (SqlDecimal) type.getObjectValue(connectorSession.getSqlFunctionProperties(), block, position);
row.addDecimal(destChannel, sqlDecimal.toBigDecimal());
} else {
throw new UnsupportedOperationException("Type is not supported: " + type);
}
}
Aggregations