Search in sources :

Example 31 with BlockBuilder

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();
}
Also used : BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 32 with BlockBuilder

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;
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) PrestoException(com.facebook.presto.spi.PrestoException) Page(com.facebook.presto.common.Page) IOException(java.io.IOException) RealType(com.facebook.presto.common.type.RealType) BigintType(com.facebook.presto.common.type.BigintType) DateTime(org.joda.time.DateTime) Type(com.facebook.presto.common.type.Type) BigintType(com.facebook.presto.common.type.BigintType) RealType(com.facebook.presto.common.type.RealType) DoubleType(com.facebook.presto.common.type.DoubleType) TimestampType(com.facebook.presto.common.type.TimestampType) DoubleType(com.facebook.presto.common.type.DoubleType) Slice(io.airlift.slice.Slice) TimestampType(com.facebook.presto.common.type.TimestampType) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 33 with BlockBuilder

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();
}
Also used : BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 34 with BlockBuilder

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();
}
Also used : BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 35 with BlockBuilder

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();
}
Also used : BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Aggregations

BlockBuilder (com.facebook.presto.common.block.BlockBuilder)493 Block (com.facebook.presto.common.block.Block)124 Test (org.testng.annotations.Test)106 Slice (io.airlift.slice.Slice)85 Type (com.facebook.presto.common.type.Type)76 Page (com.facebook.presto.common.Page)49 SqlType (com.facebook.presto.spi.function.SqlType)46 ArrayType (com.facebook.presto.common.type.ArrayType)44 MapType (com.facebook.presto.common.type.MapType)32 RowType (com.facebook.presto.common.type.RowType)28 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)26 RowBlockBuilder (com.facebook.presto.common.block.RowBlockBuilder)22 PrestoException (com.facebook.presto.spi.PrestoException)22 PageBuilder (com.facebook.presto.common.PageBuilder)21 StructuralTestUtil.appendToBlockBuilder (com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)21 Map (java.util.Map)21 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)20 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)19 DictionaryBlock (com.facebook.presto.common.block.DictionaryBlock)18 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)18