Search in sources :

Example 16 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class ShortEncoding method decodeColumn.

@Override
public Block decodeColumn(ColumnData columnData) {
    int size = columnData.rowCount();
    BlockBuilder builder = type.createBlockBuilder(null, size);
    Slice slice = columnData.getSlice();
    for (int i = 0; i < size; i++) {
        int length = columnData.getLength(i);
        if (length != 0) {
            checkState(length == SIZE_OF_SHORT, "Short should be 2 bytes");
            type.writeLong(builder, (long) Short.reverseBytes(slice.getShort(columnData.getOffset(i))));
        } else {
            builder.appendNull();
        }
    }
    return builder.build();
}
Also used : Slice(io.airlift.slice.Slice) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 17 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class StructEncoding method decodeValueInto.

@Override
public void decodeValueInto(BlockBuilder builder, Slice slice, int offset, int length) {
    int fieldId = 0;
    int nullByte = 0;
    int elementOffset = offset;
    BlockBuilder rowBuilder = builder.beginBlockEntry();
    while (fieldId < structFields.size() && elementOffset < offset + length) {
        BinaryColumnEncoding field = structFields.get(fieldId);
        // null byte prefixes every 8 fields
        if ((fieldId % 8) == 0) {
            nullByte = slice.getByte(elementOffset);
            elementOffset++;
        }
        // read field
        if ((nullByte & (1 << (fieldId % 8))) != 0) {
            int valueOffset = field.getValueOffset(slice, elementOffset);
            int valueLength = field.getValueLength(slice, elementOffset);
            field.decodeValueInto(rowBuilder, slice, elementOffset + valueOffset, valueLength);
            elementOffset = elementOffset + valueOffset + valueLength;
        } else {
            rowBuilder.appendNull();
        }
        fieldId++;
    }
    // so we fill with nulls
    while (fieldId < structFields.size()) {
        rowBuilder.appendNull();
        fieldId++;
    }
    builder.closeEntry();
}
Also used : BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 18 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class DateEncoding method decodeColumn.

@Override
public Block decodeColumn(ColumnData columnData) {
    int size = columnData.rowCount();
    BlockBuilder builder = type.createBlockBuilder(null, size);
    Slice slice = columnData.getSlice();
    for (int i = 0; i < size; i++) {
        int offset = columnData.getOffset(i);
        int length = columnData.getLength(i);
        if (length == 0) {
            builder.appendNull();
        } else {
            long daysSinceEpoch = readVInt(slice, offset, length);
            type.writeLong(builder, toIntExact(daysSinceEpoch));
        }
    }
    return builder.build();
}
Also used : Slice(io.airlift.slice.Slice) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 19 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class MapEncoding method decodeValueInto.

@Override
public void decodeValueInto(BlockBuilder builder, Slice slice, int offset, int length) {
    // entries in list
    int entries = toIntExact(readVInt(slice, offset));
    offset += decodeVIntSize(slice.getByte(offset));
    // null bytes
    int nullByteCur = offset;
    int nullByteEnd = offset + (entries * 2 + 7) / 8;
    // read elements starting after null bytes
    int elementOffset = nullByteEnd;
    BlockBuilder mapBuilder = builder.beginBlockEntry();
    for (int i = 0; i < entries; i++) {
        // read key
        boolean nullKey;
        if ((slice.getByte(nullByteCur) & (1 << ((i * 2) % 8))) != 0) {
            int keyOffset = keyReader.getValueOffset(slice, elementOffset);
            int keyLength = keyReader.getValueLength(slice, elementOffset);
            keyReader.decodeValueInto(mapBuilder, slice, elementOffset + keyOffset, keyLength);
            nullKey = false;
            elementOffset = elementOffset + keyOffset + keyLength;
        } else {
            nullKey = true;
        }
        // read value
        if ((slice.getByte(nullByteCur) & (1 << ((i * 2 + 1) % 8))) != 0) {
            int valueOffset = valueReader.getValueOffset(slice, elementOffset);
            int valueLength = valueReader.getValueLength(slice, elementOffset);
            // ignore entries with a null key
            if (!nullKey) {
                valueReader.decodeValueInto(mapBuilder, slice, elementOffset + valueOffset, valueLength);
            }
            elementOffset = elementOffset + valueOffset + valueLength;
        } else {
            // ignore entries with a null key
            if (!nullKey) {
                mapBuilder.appendNull();
            }
        }
        // move onto the next null byte
        if (3 == (i % 4)) {
            nullByteCur++;
        }
    }
    builder.closeEntry();
}
Also used : BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 20 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class StringEncoding method decodeColumn.

@Override
public Block decodeColumn(ColumnData columnData) {
    int size = columnData.rowCount();
    BlockBuilder builder = type.createBlockBuilder(null, size);
    Slice slice = columnData.getSlice();
    for (int i = 0; i < size; i++) {
        int length = columnData.getLength(i);
        if (length > 0) {
            int offset = columnData.getOffset(i);
            if ((length == 1) && slice.getByte(offset) == HIVE_EMPTY_STRING_BYTE) {
                type.writeSlice(builder, EMPTY_SLICE);
            } else {
                length = calculateTruncationLength(type, slice, offset, length);
                type.writeSlice(builder, slice.slice(offset, length));
            }
        } else {
            builder.appendNull();
        }
    }
    return builder.build();
}
Also used : Slice(io.airlift.slice.Slice) 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