Search in sources :

Example 1 with ArrayBlockBuilder

use of com.facebook.presto.common.block.ArrayBlockBuilder 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 2 with ArrayBlockBuilder

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

the class TestDirectEntryBlockBuilder method testArrayWithNestedMap.

@Test
public void testArrayWithNestedMap() {
    MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
    ArrayBlockBuilder beginEntryBlockBuilder = new ArrayBlockBuilder(mapType, null, POSITION_COUNT);
    for (int i = 0; i < POSITION_COUNT; i++) {
        SingleArrayBlockWriter singleArrayBlockWriter = beginEntryBlockBuilder.beginBlockEntry();
        for (int j = 0; j < 10; j++) {
            BlockBuilder innerMapWriter = singleArrayBlockWriter.beginBlockEntry();
            BIGINT.writeLong(innerMapWriter, i);
            VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value1" + j));
            BIGINT.writeLong(innerMapWriter, i + 1);
            VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value2" + j));
            singleArrayBlockWriter.closeEntry();
            singleArrayBlockWriter.appendNull();
        }
        beginEntryBlockBuilder.closeEntry();
    }
    ArrayBlockBuilder directEntryBlockBuilder = new ArrayBlockBuilder(mapType, null, POSITION_COUNT);
    for (int i = 0; i < POSITION_COUNT; i++) {
        directEntryBlockBuilder.beginDirectEntry();
        MapBlockBuilder innerBuilder = (MapBlockBuilder) directEntryBlockBuilder.getElementBlockBuilder();
        BlockBuilder keyBuilder = innerBuilder.getKeyBlockBuilder();
        BlockBuilder valueBuilder = innerBuilder.getValueBlockBuilder();
        for (int j = 0; j < 10; j++) {
            innerBuilder.beginDirectEntry();
            BIGINT.writeLong(keyBuilder, i);
            BIGINT.writeLong(keyBuilder, i + 1);
            VARCHAR.writeSlice(valueBuilder, utf8Slice("Value1" + j));
            VARCHAR.writeSlice(valueBuilder, utf8Slice("Value2" + j));
            innerBuilder.closeEntry();
            innerBuilder.appendNull();
        }
        directEntryBlockBuilder.closeEntry();
    }
    Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
    Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
    assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
Also used : ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) SingleArrayBlockWriter(com.facebook.presto.common.block.SingleArrayBlockWriter) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) MapType(com.facebook.presto.common.type.MapType) 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 3 with ArrayBlockBuilder

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

the class TestArrayBlock method createBlockBuilderWithValues.

private static BlockBuilder createBlockBuilderWithValues(long[][][] expectedValues) {
    BlockBuilder blockBuilder = new ArrayBlockBuilder(new ArrayBlockBuilder(BIGINT, null, 100, 100), null, 100);
    for (long[][] expectedValue : expectedValues) {
        if (expectedValue == null) {
            blockBuilder.appendNull();
        } else {
            BlockBuilder intermediateBlockBuilder = new ArrayBlockBuilder(BIGINT, null, 100, 100);
            for (int j = 0; j < expectedValue.length; j++) {
                if (expectedValue[j] == null) {
                    intermediateBlockBuilder.appendNull();
                } else {
                    BlockBuilder innerMostBlockBuilder = BIGINT.createBlockBuilder(null, expectedValue.length);
                    for (long v : expectedValue[j]) {
                        BIGINT.writeLong(innerMostBlockBuilder, v);
                    }
                    intermediateBlockBuilder.appendStructure(innerMostBlockBuilder.build());
                }
            }
            blockBuilder.appendStructure(intermediateBlockBuilder.build());
        }
    }
    return blockBuilder;
}
Also used : ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder)

Example 4 with ArrayBlockBuilder

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

the class TestArrayBlock method createBlockBuilderWithValues.

private static BlockBuilder createBlockBuilderWithValues(Slice[][] expectedValues) {
    BlockBuilder blockBuilder = new ArrayBlockBuilder(VARCHAR, null, 100, 100);
    for (Slice[] expectedValue : expectedValues) {
        if (expectedValue == null) {
            blockBuilder.appendNull();
        } else {
            BlockBuilder elementBlockBuilder = VARCHAR.createBlockBuilder(null, expectedValue.length);
            for (Slice v : expectedValue) {
                VARCHAR.writeSlice(elementBlockBuilder, v);
            }
            blockBuilder.appendStructure(elementBlockBuilder.build());
        }
    }
    return blockBuilder;
}
Also used : ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Slice(io.airlift.slice.Slice) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder)

Example 5 with ArrayBlockBuilder

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

the class TestArrayBlock method testCopyEmptyRawElementPositions.

@Test
public void testCopyEmptyRawElementPositions() {
    int positionCount = 100;
    int[] offsets = new int[positionCount + 1];
    // Only the first array is non-empty
    Arrays.fill(offsets, 1, offsets.length, 1);
    // Array(LongArrayBlock(1)) - single element
    Block elements = fromElementBlock(positionCount, Optional.empty(), offsets, createRandomLongsBlock(1, 0));
    // Shift the array offsets index
    Block offsetsShifted = elements.getRegion(50, 50);
    assertEquals(offsetsShifted.getOffsetBase(), 50);
    // Copy the first, middle, and last elements via copyPositions
    Block copiedArray = offsetsShifted.copyPositions(new int[] { 0, 25, 49 }, 0, 3);
    assertEquals(copiedArray.getPositionCount(), 3);
    BlockBuilder blockBuilder = new ArrayBlockBuilder(BIGINT, null, 0, 0);
    long[][] expectedValues = new long[3][];
    for (int i = 0; i < expectedValues.length; i++) {
        // empty, but not null
        expectedValues[i] = new long[0];
    }
    writeValues(expectedValues, blockBuilder);
    assertBlockEquals(new ArrayType(BIGINT), copiedArray, blockBuilder.build());
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) BlockAssertions.createRandomLongsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongsBlock) BlockAssertions.createRLEBlock(com.facebook.presto.block.BlockAssertions.createRLEBlock) BlockAssertions.createRandomDictionaryBlock(com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock) ByteArrayBlock(com.facebook.presto.common.block.ByteArrayBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) BlockAssertions.createLongDictionaryBlock(com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock) Block(com.facebook.presto.common.block.Block) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Test(org.testng.annotations.Test)

Aggregations

ArrayBlockBuilder (com.facebook.presto.common.block.ArrayBlockBuilder)9 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)9 Test (org.testng.annotations.Test)6 Slice (io.airlift.slice.Slice)5 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)3 RowBlockBuilder (com.facebook.presto.common.block.RowBlockBuilder)3 ArrayType (com.facebook.presto.common.type.ArrayType)3 MapType (com.facebook.presto.common.type.MapType)3 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)3 BlockAssertions.createLongDictionaryBlock (com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock)2 BlockAssertions.createRLEBlock (com.facebook.presto.block.BlockAssertions.createRLEBlock)2 BlockAssertions.createRandomDictionaryBlock (com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock)2 BlockAssertions.createRandomLongsBlock (com.facebook.presto.block.BlockAssertions.createRandomLongsBlock)2 ArrayBlock.fromElementBlock (com.facebook.presto.common.block.ArrayBlock.fromElementBlock)2 Block (com.facebook.presto.common.block.Block)2 ByteArrayBlock (com.facebook.presto.common.block.ByteArrayBlock)2 SingleArrayBlockWriter (com.facebook.presto.common.block.SingleArrayBlockWriter)1 SingleRowBlockWriter (com.facebook.presto.common.block.SingleRowBlockWriter)1 RowType (com.facebook.presto.common.type.RowType)1 Type (com.facebook.presto.common.type.Type)1