use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class Utils method nativeValueToBlock.
public static Block nativeValueToBlock(Type type, Object object) {
if (object != null && !Primitives.wrap(type.getJavaType()).isInstance(object)) {
throw new IllegalArgumentException(String.format("Object '%s' does not match type %s", object, type.getJavaType()));
}
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
writeNativeValue(type, blockBuilder, object);
return blockBuilder.build();
}
use of com.facebook.presto.common.block.BlockBuilder 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.block.BlockBuilder in project presto by prestodb.
the class DoubleColumnReader method readBlock.
@Override
public Block readBlock(Type type, int batchSize) {
checkArgument(type == DOUBLE);
BlockBuilder builder = type.createBlockBuilder(null, batchSize);
for (int i = 0; i < batchSize; i++) {
type.writeDouble(builder, valueSelector.getDouble());
}
return builder.build();
}
use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class LongColumnReader method readBlock.
@Override
public Block readBlock(Type type, int batchSize) {
// TODO: use batch value selector
checkArgument(type == BIGINT);
BlockBuilder builder = type.createBlockBuilder(null, batchSize);
for (int i = 0; i < batchSize; i++) {
type.writeLong(builder, valueSelector.getLong());
}
return builder.build();
}
use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class StringColumnReader method readBlock.
@Override
public Block readBlock(Type type, int batchSize) {
checkArgument(type == VARCHAR);
BlockBuilder builder = type.createBlockBuilder(null, batchSize);
for (int i = 0; i < batchSize; i++) {
String value = String.valueOf(valueSelector.getObject());
if (value != null) {
type.writeSlice(builder, Slices.utf8Slice(value));
} else {
builder.appendNull();
}
}
return builder.build();
}
Aggregations