Search in sources :

Example 1 with RowBlockBuilder

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

the class OperatorAssertion method toRow.

public static Block toRow(List<Type> parameterTypes, Object... values) {
    checkArgument(parameterTypes.size() == values.length, "parameterTypes.size(" + parameterTypes.size() + ") does not equal to values.length(" + values.length + ")");
    RowType rowType = RowType.anonymous(parameterTypes);
    BlockBuilder blockBuilder = new RowBlockBuilder(parameterTypes, null, 1);
    BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
    for (int i = 0; i < values.length; i++) {
        appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter);
    }
    blockBuilder.closeEntry();
    return rowType.getObject(blockBuilder, 0);
}
Also used : RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) RowType(com.facebook.presto.common.type.RowType) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Example 2 with RowBlockBuilder

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

the class TestSimpleRowType method createTestBlock.

private static Block createTestBlock() {
    RowBlockBuilder blockBuilder = (RowBlockBuilder) TYPE.createBlockBuilder(null, 3);
    SingleRowBlockWriter singleRowBlockWriter;
    singleRowBlockWriter = blockBuilder.beginBlockEntry();
    BIGINT.writeLong(singleRowBlockWriter, 1);
    VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice("cat"));
    blockBuilder.closeEntry();
    singleRowBlockWriter = blockBuilder.beginBlockEntry();
    BIGINT.writeLong(singleRowBlockWriter, 2);
    VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice("cats"));
    blockBuilder.closeEntry();
    singleRowBlockWriter = blockBuilder.beginBlockEntry();
    BIGINT.writeLong(singleRowBlockWriter, 3);
    VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice("dog"));
    blockBuilder.closeEntry();
    return blockBuilder.build();
}
Also used : RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) SingleRowBlockWriter(com.facebook.presto.common.block.SingleRowBlockWriter)

Example 3 with RowBlockBuilder

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

the class TestDirectEntryBlockBuilder method testNestedRow.

@Test
public void testNestedRow() {
    MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
    ArrayType arrayType = new ArrayType(DOUBLE);
    RowType.Field nestedRowField = new RowType.Field(Optional.of("my_struct"), INTEGER);
    RowType nestedRowType = RowType.from(ImmutableList.of(nestedRowField));
    List<Type> rowType = ImmutableList.of(REAL, mapType, arrayType, nestedRowType);
    RowBlockBuilder beginEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
    for (int i = 0; i < POSITION_COUNT; i++) {
        SingleRowBlockWriter singleRowBlockWriter = beginEntryBlockBuilder.beginBlockEntry();
        REAL.writeLong(singleRowBlockWriter, i);
        // Write Map<Bigint, Varchar> with 5 entries.
        BlockBuilder mapWriter = singleRowBlockWriter.beginBlockEntry();
        for (int j = 0; j < 5; j++) {
            BIGINT.writeLong(mapWriter, i + j);
            VARCHAR.writeSlice(mapWriter, utf8Slice("Value1" + j));
        }
        singleRowBlockWriter.closeEntry();
        // Write array .
        BlockBuilder arrayWriter = singleRowBlockWriter.beginBlockEntry();
        for (int j = 0; j < 8; j++) {
            DOUBLE.writeDouble(arrayWriter, i * 3 + j);
        }
        singleRowBlockWriter.closeEntry();
        // Write row type.
        BlockBuilder rowWriter = singleRowBlockWriter.beginBlockEntry();
        if (i % 2 == 0) {
            rowWriter.appendNull();
        } else {
            INTEGER.writeLong(rowWriter, i);
        }
        singleRowBlockWriter.closeEntry();
        beginEntryBlockBuilder.closeEntry();
        beginEntryBlockBuilder.appendNull();
    }
    RowBlockBuilder directEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
    for (int i = 0; i < POSITION_COUNT; i++) {
        directEntryBlockBuilder.beginDirectEntry();
        REAL.writeLong(directEntryBlockBuilder.getBlockBuilder(0), i);
        // Write Map<Bigint, Varchar> with 5 entries.
        MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) directEntryBlockBuilder.getBlockBuilder(1);
        mapBlockBuilder.beginDirectEntry();
        BlockBuilder keyBuilder = mapBlockBuilder.getKeyBlockBuilder();
        BlockBuilder valueBuilder = mapBlockBuilder.getValueBlockBuilder();
        for (int j = 0; j < 5; j++) {
            BIGINT.writeLong(keyBuilder, i + j);
            VARCHAR.writeSlice(valueBuilder, utf8Slice("Value1" + j));
        }
        mapBlockBuilder.closeEntry();
        // Write array .
        ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getBlockBuilder(2);
        arrayBuilder.beginDirectEntry();
        for (int j = 0; j < 8; j++) {
            DOUBLE.writeDouble(arrayBuilder.getElementBlockBuilder(), i * 3 + j);
        }
        arrayBuilder.closeEntry();
        // Write row type.
        RowBlockBuilder nestedRowBuilder = (RowBlockBuilder) directEntryBlockBuilder.getBlockBuilder(3);
        nestedRowBuilder.beginDirectEntry();
        BlockBuilder nestedRowValueBuilder = nestedRowBuilder.getBlockBuilder(0);
        if (i % 2 == 0) {
            nestedRowValueBuilder.appendNull();
        } else {
            INTEGER.writeLong(nestedRowValueBuilder, i);
        }
        nestedRowBuilder.closeEntry();
        directEntryBlockBuilder.closeEntry();
        directEntryBlockBuilder.appendNull();
    }
    Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
    Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
    assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
Also used : RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) RowType(com.facebook.presto.common.type.RowType) SingleRowBlockWriter(com.facebook.presto.common.block.SingleRowBlockWriter) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Test(org.testng.annotations.Test)

Example 4 with RowBlockBuilder

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

the class TestRowBlock method createBlockBuilderWithValues.

private BlockBuilder createBlockBuilderWithValues(List<Type> fieldTypes, List<Object>[] rows) {
    BlockBuilder rowBlockBuilder = new RowBlockBuilder(fieldTypes, null, 1);
    for (List<Object> row : rows) {
        if (row == null) {
            rowBlockBuilder.appendNull();
        } else {
            BlockBuilder singleRowBlockWriter = rowBlockBuilder.beginBlockEntry();
            for (Object fieldValue : row) {
                if (fieldValue == null) {
                    singleRowBlockWriter.appendNull();
                } else {
                    if (fieldValue instanceof Long) {
                        BIGINT.writeLong(singleRowBlockWriter, ((Long) fieldValue).longValue());
                    } else if (fieldValue instanceof String) {
                        VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice((String) fieldValue));
                    } else {
                        throw new IllegalArgumentException();
                    }
                }
            }
            rowBlockBuilder.closeEntry();
        }
    }
    return rowBlockBuilder;
}
Also used : RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 5 with RowBlockBuilder

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

the class BlockAssertions method createRowBlock.

public static Block createRowBlock(List<Type> fieldTypes, Object[]... rows) {
    BlockBuilder rowBlockBuilder = new RowBlockBuilder(fieldTypes, null, 1);
    for (Object[] row : rows) {
        if (row == null) {
            rowBlockBuilder.appendNull();
            continue;
        }
        BlockBuilder singleRowBlockWriter = rowBlockBuilder.beginBlockEntry();
        for (Object fieldValue : row) {
            if (fieldValue == null) {
                singleRowBlockWriter.appendNull();
                continue;
            }
            if (fieldValue instanceof String) {
                VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice((String) fieldValue));
            } else if (fieldValue instanceof Slice) {
                VARBINARY.writeSlice(singleRowBlockWriter, (Slice) fieldValue);
            } else if (fieldValue instanceof Double) {
                DOUBLE.writeDouble(singleRowBlockWriter, ((Double) fieldValue).doubleValue());
            } else if (fieldValue instanceof Long) {
                BIGINT.writeLong(singleRowBlockWriter, ((Long) fieldValue).longValue());
            } else if (fieldValue instanceof Boolean) {
                BOOLEAN.writeBoolean(singleRowBlockWriter, ((Boolean) fieldValue).booleanValue());
            } else if (fieldValue instanceof Block) {
                singleRowBlockWriter.appendStructure((Block) fieldValue);
            } else if (fieldValue instanceof Integer) {
                INTEGER.writeLong(singleRowBlockWriter, ((Integer) fieldValue).intValue());
            } else {
                throw new IllegalArgumentException();
            }
        }
        rowBlockBuilder.closeEntry();
    }
    return rowBlockBuilder.build();
}
Also used : BigInteger(java.math.BigInteger) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Aggregations

RowBlockBuilder (com.facebook.presto.common.block.RowBlockBuilder)6 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)4 SingleRowBlockWriter (com.facebook.presto.common.block.SingleRowBlockWriter)3 Block (com.facebook.presto.common.block.Block)2 RowType (com.facebook.presto.common.type.RowType)2 StructuralTestUtil.appendToBlockBuilder (com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)2 Slice (io.airlift.slice.Slice)2 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)2 ArrayBlock.fromElementBlock (com.facebook.presto.common.block.ArrayBlock.fromElementBlock)1 ArrayBlockBuilder (com.facebook.presto.common.block.ArrayBlockBuilder)1 DictionaryBlock (com.facebook.presto.common.block.DictionaryBlock)1 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)1 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 MapType (com.facebook.presto.common.type.MapType)1 Type (com.facebook.presto.common.type.Type)1 BigInteger (java.math.BigInteger)1 Test (org.testng.annotations.Test)1