Search in sources :

Example 51 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class StatisticsWriterOperator method getOutput.

@Override
public Page getOutput() {
    if (state != State.FINISHING) {
        return null;
    }
    state = State.FINISHED;
    Collection<ComputedStatistics> computedStatistics = computedStatisticsBuilder.build();
    statisticsWriter.writeStatistics(computedStatistics);
    // output page will only be constructed once,
    // so a new PageBuilder is constructed (instead of using PageBuilder.reset)
    PageBuilder page = new PageBuilder(1, TYPES);
    page.declarePosition();
    BlockBuilder rowsBuilder = page.getBlockBuilder(0);
    if (rowCountEnabled) {
        BIGINT.writeLong(rowsBuilder, getRowCount(computedStatistics));
    } else {
        rowsBuilder.appendNull();
    }
    return page.build();
}
Also used : ComputedStatistics(com.facebook.presto.spi.statistics.ComputedStatistics) PageBuilder(com.facebook.presto.common.PageBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 52 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class MergeSortedPages method buildPage.

private static WorkProcessor<Page> buildPage(WorkProcessor<PageWithPosition> pageWithPositions, List<Integer> outputChannels, List<Type> outputTypes, BiPredicate<PageBuilder, PageWithPosition> pageBreakPredicate, boolean updateMemoryAfterEveryPosition, AggregatedMemoryContext aggregatedMemoryContext, DriverYieldSignal yieldSignal) {
    LocalMemoryContext memoryContext = aggregatedMemoryContext.newLocalMemoryContext(MergeSortedPages.class.getSimpleName());
    PageBuilder pageBuilder = new PageBuilder(outputTypes);
    return pageWithPositions.yielding(yieldSignal::isSet).transform(pageWithPositionOptional -> {
        boolean finished = !pageWithPositionOptional.isPresent();
        if (finished && pageBuilder.isEmpty()) {
            memoryContext.close();
            return TransformationState.finished();
        }
        if (finished || pageBreakPredicate.test(pageBuilder, pageWithPositionOptional.get())) {
            if (!updateMemoryAfterEveryPosition) {
                // update memory usage just before producing page to cap from top
                memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
            }
            Page page = pageBuilder.build();
            pageBuilder.reset();
            if (!finished) {
                pageWithPositionOptional.get().appendTo(pageBuilder, outputChannels, outputTypes);
            }
            if (updateMemoryAfterEveryPosition) {
                memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
            }
            return TransformationState.ofResult(page, !finished);
        }
        pageWithPositionOptional.get().appendTo(pageBuilder, outputChannels, outputTypes);
        if (updateMemoryAfterEveryPosition) {
            memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
        }
        return TransformationState.needsMoreData();
    });
}
Also used : LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) Page(com.facebook.presto.common.Page) PageBuilder(com.facebook.presto.common.PageBuilder)

Example 53 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class TestBlockBuilder method testNewBlockBuilderLikeForLargeBlockBuilder.

@Test
public void testNewBlockBuilderLikeForLargeBlockBuilder() {
    if (true) {
        throw new SkipException("https://github.com/prestodb/presto/issues/15653 - Skipped because it OOMs");
    }
    List<Type> channels = ImmutableList.of(VARCHAR);
    PageBuilder pageBuilder = new PageBuilder(channels);
    BlockBuilder largeVarcharBlockBuilder = pageBuilder.getBlockBuilder(0);
    // Construct a string of 64 * 16 = 2^11 bytes
    Slice largeSlice = Slices.utf8Slice(String.join("", Collections.nCopies(64, "CowMonsterKing:)")));
    // Write the string to the largeVarcharBlockBuilder for 2^20 times
    for (int i = 0; i < 1_048_576; i++) {
        VARCHAR.writeSlice(largeVarcharBlockBuilder, largeSlice);
        pageBuilder.declarePosition();
    }
    BlockBuilder newVarcharBlockBuilder = largeVarcharBlockBuilder.newBlockBuilderLike(null, 1_048_576 * 8);
    assertEquals(newVarcharBlockBuilder.getPositionCount(), 0);
    assertEquals(newVarcharBlockBuilder.getRetainedSizeInBytes(), 164);
// We are not going to test real reservation here because allocating large amount of memory fails the Travis.
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) Slice(io.airlift.slice.Slice) SkipException(org.testng.SkipException) PageBuilder(com.facebook.presto.common.PageBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 54 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class MultiChannelGroupByHash method startNewPage.

private void startNewPage() {
    if (currentPageBuilder != null) {
        completedPagesMemorySize += currentPageBuilder.getRetainedSizeInBytes();
        currentPageBuilder = currentPageBuilder.newPageBuilderLike();
    } else {
        currentPageBuilder = new PageBuilder(types);
    }
    for (int i = 0; i < types.size(); i++) {
        channelBuilders.get(i).add(currentPageBuilder.getBlockBuilder(i));
    }
}
Also used : PageBuilder(com.facebook.presto.common.PageBuilder)

Example 55 with PageBuilder

use of com.facebook.presto.common.PageBuilder in project presto by prestodb.

the class TestTypedSet method testGetElementPositionWithProvidedNonEmptyBlockBuilder.

@Test
public void testGetElementPositionWithProvidedNonEmptyBlockBuilder() {
    int elementCount = 100;
    // Set initialTypedSetEntryCount to a small number to trigger rehash()
    int initialTypedSetEntryCount = 10;
    PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(BIGINT));
    BlockBuilder firstBlockBuilder = pageBuilder.getBlockBuilder(0);
    for (int i = 0; i < elementCount; i++) {
        BIGINT.writeLong(firstBlockBuilder, i);
    }
    pageBuilder.declarePositions(elementCount);
    // The secondBlockBuilder should already have elementCount rows.
    BlockBuilder secondBlockBuilder = pageBuilder.getBlockBuilder(0);
    TypedSet typedSet = new TypedSet(BIGINT, secondBlockBuilder, initialTypedSetEntryCount, FUNCTION_NAME);
    BlockBuilder externalBlockBuilder = BIGINT.createFixedSizeBlockBuilder(elementCount);
    for (int i = 0; i < elementCount; i++) {
        if (i % 10 == 0) {
            externalBlockBuilder.appendNull();
        } else {
            BIGINT.writeLong(externalBlockBuilder, i);
        }
        typedSet.add(externalBlockBuilder, i);
    }
    assertEquals(typedSet.size(), secondBlockBuilder.getPositionCount() - elementCount);
    assertEquals(typedSet.size(), elementCount - elementCount / 10 + 1);
    for (int i = 0; i < typedSet.size(); i++) {
        int expectedPositionInSecondBlockBuilder = i + elementCount;
        assertEquals(typedSet.positionOf(secondBlockBuilder, expectedPositionInSecondBlockBuilder), expectedPositionInSecondBlockBuilder);
    }
}
Also used : PageBuilder(com.facebook.presto.common.PageBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Aggregations

PageBuilder (com.facebook.presto.common.PageBuilder)57 Page (com.facebook.presto.common.Page)27 Type (com.facebook.presto.common.type.Type)25 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)23 ImmutableList (com.google.common.collect.ImmutableList)14 Block (com.facebook.presto.common.block.Block)11 Test (org.testng.annotations.Test)11 ArrayType (com.facebook.presto.common.type.ArrayType)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Benchmark (org.openjdk.jmh.annotations.Benchmark)7 INTEGER (com.facebook.presto.common.type.IntegerType.INTEGER)4 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)3 DOUBLE (com.facebook.presto.common.type.DoubleType.DOUBLE)3 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)3 ConnectorPageSource (com.facebook.presto.spi.ConnectorPageSource)3 ConnectorSession (com.facebook.presto.spi.ConnectorSession)3 MaterializedResult (com.facebook.presto.testing.MaterializedResult)3 Slices (io.airlift.slice.Slices)3 File (java.io.File)3