Search in sources :

Example 6 with ArrayBlockBuilder

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

the class TestColumnarArray method createBlockBuilderWithValues.

public 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) {
                if (v == null) {
                    elementBlockBuilder.appendNull();
                } else {
                    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 7 with ArrayBlockBuilder

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

the class TestArrayBlock method testSingleValueBlock.

@Test
public void testSingleValueBlock() {
    // 1 entry array.
    long[][] values = createTestArray(50);
    BlockBuilder arrayBlockBuilder = createBlockBuilderWithValues(values);
    Block arrayBlock = arrayBlockBuilder.build();
    assertSame(arrayBlock, arrayBlock.getSingleValueBlock(0));
    assertNotSame(arrayBlockBuilder, arrayBlockBuilder.getSingleValueBlock(0));
    // 2 entries array.
    values = createTestArray(50, 50);
    arrayBlockBuilder = createBlockBuilderWithValues(values);
    arrayBlock = arrayBlockBuilder.build();
    Block firstElement = arrayBlock.getRegion(0, 1);
    assertNotSame(firstElement, firstElement.getSingleValueBlock(0));
    Block secondElementCopy = arrayBlock.copyRegion(1, 1);
    assertSame(secondElementCopy, secondElementCopy.getSingleValueBlock(0));
    // Test with null elements.
    values = new long[][] { null };
    arrayBlockBuilder = new ArrayBlockBuilder(BIGINT, null, 1, 100);
    writeValues(values, arrayBlockBuilder);
    arrayBlock = arrayBlockBuilder.build();
    assertSame(arrayBlock, arrayBlock.getSingleValueBlock(0));
    assertNotSame(arrayBlock, arrayBlockBuilder.getSingleValueBlock(0));
    // Test with 2 null elements.
    values = new long[][] { null, null };
    arrayBlockBuilder = createBlockBuilderWithValues(values);
    arrayBlock = arrayBlockBuilder.build();
    assertNotSame(arrayBlock, arrayBlock.getSingleValueBlock(0));
}
Also used : 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)

Example 8 with ArrayBlockBuilder

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

the class TestArrayBlock method testLazyBlockBuilderInitialization.

@Test
public void testLazyBlockBuilderInitialization() {
    long[][] expectedValues = new long[ARRAY_SIZES.length][];
    Random rand = new Random(47);
    for (int i = 0; i < ARRAY_SIZES.length; i++) {
        expectedValues[i] = rand.longs(ARRAY_SIZES[i]).toArray();
    }
    BlockBuilder emptyBlockBuilder = new ArrayBlockBuilder(BIGINT, null, 0, 0);
    BlockBuilder blockBuilder = new ArrayBlockBuilder(BIGINT, null, 100, 100);
    assertEquals(blockBuilder.getSizeInBytes(), emptyBlockBuilder.getSizeInBytes());
    assertEquals(blockBuilder.getRetainedSizeInBytes(), emptyBlockBuilder.getRetainedSizeInBytes());
    writeValues(expectedValues, blockBuilder);
    assertTrue(blockBuilder.getSizeInBytes() > emptyBlockBuilder.getSizeInBytes());
    assertTrue(blockBuilder.getRetainedSizeInBytes() > emptyBlockBuilder.getRetainedSizeInBytes());
    blockBuilder = blockBuilder.newBlockBuilderLike(null);
    assertEquals(blockBuilder.getSizeInBytes(), emptyBlockBuilder.getSizeInBytes());
    assertEquals(blockBuilder.getRetainedSizeInBytes(), emptyBlockBuilder.getRetainedSizeInBytes());
}
Also used : ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Random(java.util.Random) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Test(org.testng.annotations.Test)

Example 9 with ArrayBlockBuilder

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

the class TestDirectEntryBlockBuilder method testNestedMap.

@Test
public void testNestedMap() {
    MapType innerMap = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
    ArrayType arrayType = new ArrayType(innerMap);
    MapType mapType = new MapType(BIGINT, arrayType, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
    MapBlockBuilder beginEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
    for (int i = 0; i < POSITION_COUNT; i++) {
        BlockBuilder mapWriter = beginEntryBlockBuilder.beginBlockEntry();
        for (int j = 0; j < 7; j++) {
            // Key
            BIGINT.writeLong(mapWriter, i * 7 + j);
            BlockBuilder arrayWriter = mapWriter.beginBlockEntry();
            // Map contains 4 elements.
            for (int k = 0; k < 10; k++) {
                BlockBuilder innerMapWriter = arrayWriter.beginBlockEntry();
                for (int l = 0; l < 3; l++) {
                    BIGINT.writeLong(innerMapWriter, k * 10 + l);
                    VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value" + l));
                }
                arrayWriter.closeEntry();
                arrayWriter.appendNull();
            }
            mapWriter.closeEntry();
        }
        beginEntryBlockBuilder.closeEntry();
    }
    MapBlockBuilder directEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
    for (int i = 0; i < POSITION_COUNT; i++) {
        directEntryBlockBuilder.beginDirectEntry();
        BlockBuilder keyBuilder = directEntryBlockBuilder.getKeyBlockBuilder();
        ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getValueBlockBuilder();
        MapBlockBuilder innerMapBuilder = (MapBlockBuilder) arrayBuilder.getElementBlockBuilder();
        BlockBuilder innerMapKeyBuilder = innerMapBuilder.getKeyBlockBuilder();
        BlockBuilder innerMapValueBuilder = innerMapBuilder.getValueBlockBuilder();
        for (int j = 0; j < 7; j++) {
            // Key
            BIGINT.writeLong(keyBuilder, i * 7 + j);
            arrayBuilder.beginDirectEntry();
            // Map contains 4 elements.
            for (int k = 0; k < 10; k++) {
                innerMapBuilder.beginDirectEntry();
                for (int l = 0; l < 3; l++) {
                    BIGINT.writeLong(innerMapKeyBuilder, k * 10 + l);
                    VARCHAR.writeSlice(innerMapValueBuilder, utf8Slice("Value" + l));
                }
                innerMapBuilder.closeEntry();
                innerMapBuilder.appendNull();
            }
            arrayBuilder.closeEntry();
        }
        directEntryBlockBuilder.closeEntry();
    }
    Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
    Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
    assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) 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)

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