use of com.facebook.presto.common.type.TimestampType in project presto by prestodb.
the class DruidBrokerPageSource method getNextPage.
@Override
public Page getNextPage() {
if (finished) {
return null;
}
long start = System.nanoTime();
boolean columnHandlesHasErrorMessageField = columnHandles.stream().anyMatch(handle -> ((DruidColumnHandle) handle).getColumnName().equals("errorMessage"));
try {
String readLine;
while ((readLine = responseStream.readLine()) != null) {
// if read a blank line,it means read finish
if (readLine.isEmpty()) {
finished = true;
break;
} else {
JsonNode rootNode = OBJECT_MAPPER.readTree(readLine);
if (rootNode.has("errorMessage") && !columnHandlesHasErrorMessageField) {
throw new PrestoException(DRUID_BROKER_RESULT_ERROR, rootNode.findValue("errorMessage").asText());
}
for (int i = 0; i < columnHandles.size(); i++) {
Type type = columnTypes.get(i);
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i);
JsonNode value = rootNode.get(((DruidColumnHandle) columnHandles.get(i)).getColumnName());
if (value == null) {
blockBuilder.appendNull();
continue;
}
if (type instanceof BigintType) {
type.writeLong(blockBuilder, value.longValue());
} else if (type instanceof DoubleType) {
type.writeDouble(blockBuilder, value.doubleValue());
} else if (type instanceof RealType) {
type.writeLong(blockBuilder, floatToRawIntBits(value.floatValue()));
} else if (type instanceof TimestampType) {
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser().withChronology(ISOChronology.getInstanceUTC()).withOffsetParsed();
DateTime dateTime = formatter.parseDateTime(value.textValue());
type.writeLong(blockBuilder, dateTime.getMillis());
} else {
Slice slice = Slices.utf8Slice(value.textValue());
type.writeSlice(blockBuilder, slice);
}
}
}
pageBuilder.declarePosition();
if (pageBuilder.isFull()) {
break;
}
}
// if responseStream.readLine() is null, it means read finish
if (readLine == null) {
finished = true;
return null;
}
// only return a page if the buffer is full or we are finishing
if (pageBuilder.isEmpty() || (!finished && !pageBuilder.isFull())) {
return null;
}
Page page = pageBuilder.build();
completedPositions += page.getPositionCount();
completedBytes += page.getSizeInBytes();
pageBuilder.reset();
return page;
} catch (IOException e) {
finished = true;
throw new PrestoException(DRUID_BROKER_RESULT_ERROR, "Parse druid client response error", e);
} finally {
readTimeNanos += System.nanoTime() - start;
}
}
use of com.facebook.presto.common.type.TimestampType in project presto by prestodb.
the class PinotBrokerPageSourceBase method setValue.
protected void setValue(Type type, BlockBuilder blockBuilder, String value) {
if (blockBuilder == null) {
return;
}
if (value == null) {
blockBuilder.appendNull();
return;
}
if (!(type instanceof FixedWidthType) && !(type instanceof VarcharType) && !(type instanceof JsonType)) {
throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
}
if (type instanceof FixedWidthType) {
completedBytes += ((FixedWidthType) type).getFixedSize();
if (type instanceof BigintType) {
type.writeLong(blockBuilder, parseDouble(value).longValue());
} else if (type instanceof IntegerType) {
blockBuilder.writeInt(parseDouble(value).intValue());
} else if (type instanceof TinyintType) {
blockBuilder.writeByte(parseDouble(value).byteValue());
} else if (type instanceof SmallintType) {
blockBuilder.writeShort(parseDouble(value).shortValue());
} else if (type instanceof BooleanType) {
type.writeBoolean(blockBuilder, parseBoolean(value));
} else if (type instanceof DecimalType || type instanceof DoubleType) {
type.writeDouble(blockBuilder, parseDouble(value));
} else if (type instanceof TimestampType) {
type.writeLong(blockBuilder, parseTimestamp(value));
} else if (type instanceof DateType) {
type.writeLong(blockBuilder, parseLong(value));
} else {
throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
}
} else {
Slice slice = Slices.utf8Slice(value);
blockBuilder.writeBytes(slice, 0, slice.length()).closeEntry();
completedBytes += slice.length();
}
}
use of com.facebook.presto.common.type.TimestampType 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.TimestampType in project presto by prestodb.
the class TestJmxSplitManager method readTimeStampsFrom.
private List<Long> readTimeStampsFrom(RecordSet recordSet) {
ImmutableList.Builder<Long> result = ImmutableList.builder();
try (RecordCursor cursor = recordSet.cursor()) {
while (cursor.advanceNextPosition()) {
for (int i = 0; i < recordSet.getColumnTypes().size(); i++) {
cursor.isNull(i);
}
if (cursor.isNull(0)) {
return result.build();
}
assertTrue(recordSet.getColumnTypes().get(0) instanceof TimestampType);
result.add(cursor.getLong(0));
}
}
return result.build();
}
use of com.facebook.presto.common.type.TimestampType in project presto by prestodb.
the class AbstractRowEncoder method appendColumnValue.
@Override
public void appendColumnValue(Block block, int position) {
checkArgument(currentColumnIndex < columnHandles.size(), format("currentColumnIndex '%d' is greater than number of columns '%d'", currentColumnIndex, columnHandles.size()));
Type type = columnHandles.get(currentColumnIndex).getType();
if (block.isNull(position)) {
appendNullValue();
} else if (type == BOOLEAN) {
appendBoolean(type.getBoolean(block, position));
} else if (type == BIGINT) {
appendLong(type.getLong(block, position));
} else if (type == INTEGER) {
appendInt(toIntExact(type.getLong(block, position)));
} else if (type == SMALLINT) {
appendShort(Shorts.checkedCast(type.getLong(block, position)));
} else if (type == TINYINT) {
appendByte(SignedBytes.checkedCast(type.getLong(block, position)));
} else if (type == DOUBLE) {
appendDouble(type.getDouble(block, position));
} else if (type == REAL) {
appendFloat(intBitsToFloat(toIntExact(type.getLong(block, position))));
} else if (isVarcharType(type)) {
appendString(type.getSlice(block, position).toStringUtf8());
} else if (isVarbinaryType(type)) {
appendByteBuffer(type.getSlice(block, position).toByteBuffer());
} else if (type == DATE) {
appendSqlDate((SqlDate) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
} else if (type == TIME) {
appendSqlTime((SqlTime) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
} else if (type == TIME_WITH_TIME_ZONE) {
appendSqlTimeWithTimeZone((SqlTimeWithTimeZone) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
} else if (type instanceof TimestampType) {
appendSqlTimestamp((SqlTimestamp) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
} else if (type instanceof TimestampWithTimeZoneType) {
appendSqlTimestampWithTimeZone((SqlTimestampWithTimeZone) type.getObjectValue(session.getSqlFunctionProperties(), block, position));
} else {
throw new UnsupportedOperationException(format("Column '%s' does not support 'null' value", columnHandles.get(currentColumnIndex).getName()));
}
currentColumnIndex++;
}
Aggregations