Search in sources :

Example 56 with PageBuilder

use of io.trino.spi.PageBuilder in project trino by trinodb.

the class TestBlockBuilder method testNewBlockBuilderLike.

@Test
public void testNewBlockBuilderLike() {
    ArrayType longArrayType = new ArrayType(BIGINT);
    ArrayType arrayType = new ArrayType(longArrayType);
    List<Type> channels = ImmutableList.of(BIGINT, VARCHAR, arrayType);
    PageBuilder pageBuilder = new PageBuilder(channels);
    BlockBuilder bigintBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder varcharBlockBuilder = pageBuilder.getBlockBuilder(1);
    BlockBuilder arrayBlockBuilder = pageBuilder.getBlockBuilder(2);
    for (int i = 0; i < 100; i++) {
        BIGINT.writeLong(bigintBlockBuilder, i);
        VARCHAR.writeSlice(varcharBlockBuilder, Slices.utf8Slice("test" + i));
        BlockBuilder blockBuilder = longArrayType.createBlockBuilder(null, 1);
        longArrayType.writeObject(blockBuilder, BIGINT.createBlockBuilder(null, 2).writeLong(i).writeLong(i * 2).build());
        arrayType.writeObject(arrayBlockBuilder, blockBuilder);
        pageBuilder.declarePosition();
    }
    PageBuilder newPageBuilder = pageBuilder.newPageBuilderLike();
    for (int i = 0; i < channels.size(); i++) {
        assertEquals(newPageBuilder.getType(i), pageBuilder.getType(i));
        // we should get new block builder instances
        assertNotEquals(pageBuilder.getBlockBuilder(i), newPageBuilder.getBlockBuilder(i));
        assertEquals(newPageBuilder.getBlockBuilder(i).getPositionCount(), 0);
        assertTrue(newPageBuilder.getBlockBuilder(i).getRetainedSizeInBytes() < pageBuilder.getBlockBuilder(i).getRetainedSizeInBytes());
    }
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) ArrayType(io.trino.spi.type.ArrayType) PageBuilder(io.trino.spi.PageBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 57 with PageBuilder

use of io.trino.spi.PageBuilder in project trino by trinodb.

the class TestPagesIndexPageSorter method createOutputPages.

private static List<Page> createOutputPages(List<Type> types, List<Page> inputPages, long[] sortedAddresses) {
    PageBuilder pageBuilder = new PageBuilder(types);
    pageBuilder.reset();
    for (long address : sortedAddresses) {
        int index = sorter.decodePageIndex(address);
        int position = sorter.decodePositionIndex(address);
        Page page = inputPages.get(index);
        for (int i = 0; i < types.size(); i++) {
            Type type = types.get(i);
            type.appendTo(page.getBlock(i), position, pageBuilder.getBlockBuilder(i));
        }
        pageBuilder.declarePosition();
    }
    return ImmutableList.of(pageBuilder.build());
}
Also used : Type(io.trino.spi.type.Type) Page(io.trino.spi.Page) PageBuilder(io.trino.spi.PageBuilder)

Example 58 with PageBuilder

use of io.trino.spi.PageBuilder in project trino by trinodb.

the class TestShardCompactor method sortAndMaterialize.

private static MaterializedResult sortAndMaterialize(List<Page> pages, List<Type> columnTypes, List<Integer> sortIndexes, List<SortOrder> sortOrders, List<Type> sortTypes) {
    long[] orderedAddresses = PAGE_SORTER.sort(columnTypes, pages, sortIndexes, sortOrders, 10_000);
    PageBuilder pageBuilder = new PageBuilder(columnTypes);
    for (long orderedAddress : orderedAddresses) {
        int pageIndex = PAGE_SORTER.decodePageIndex(orderedAddress);
        int positionIndex = PAGE_SORTER.decodePositionIndex(orderedAddress);
        Page page = pages.get(pageIndex);
        pageBuilder.declarePosition();
        for (int i = 0; i < columnTypes.size(); i++) {
            columnTypes.get(i).appendTo(page.getBlock(i), positionIndex, pageBuilder.getBlockBuilder(i));
        }
    }
    // extract the sortIndexes and reorder the blocks by sort indexes (useful for debugging)
    Page buildPage = pageBuilder.build();
    Block[] outputBlocks = new Block[buildPage.getChannelCount()];
    for (int i = 0; i < sortIndexes.size(); i++) {
        outputBlocks[i] = buildPage.getBlock(sortIndexes.get(i));
    }
    MaterializedResult.Builder resultBuilder = MaterializedResult.resultBuilder(SESSION, sortTypes);
    resultBuilder.page(new Page(outputBlocks));
    return resultBuilder.build();
}
Also used : Block(io.trino.spi.block.Block) Page(io.trino.spi.Page) SequencePageBuilder(io.trino.SequencePageBuilder) PageBuilder(io.trino.spi.PageBuilder) MaterializedResult(io.trino.testing.MaterializedResult)

Aggregations

PageBuilder (io.trino.spi.PageBuilder)58 BlockBuilder (io.trino.spi.block.BlockBuilder)24 Page (io.trino.spi.Page)23 Type (io.trino.spi.type.Type)22 ImmutableList (com.google.common.collect.ImmutableList)14 Block (io.trino.spi.block.Block)11 Test (org.testng.annotations.Test)10 List (java.util.List)8 Slice (io.airlift.slice.Slice)6 ArrayType (io.trino.spi.type.ArrayType)6 INTEGER (io.trino.spi.type.IntegerType.INTEGER)5 Slices (io.airlift.slice.Slices)4 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)4 DictionaryBlock (io.trino.spi.block.DictionaryBlock)4 RunLengthEncodedBlock (io.trino.spi.block.RunLengthEncodedBlock)4 BIGINT (io.trino.spi.type.BigintType.BIGINT)4 DOUBLE (io.trino.spi.type.DoubleType.DOUBLE)4 MapType (io.trino.spi.type.MapType)4 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)4 RowType (io.trino.spi.type.RowType)3