Search in sources :

Example 11 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

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(new WorkProcessor.Transformation<PageWithPosition, Page>() {

        @RestorableConfig(stateClassName = "BuildPageState", uncapturedFields = { "val$outputTypes", "val$pageBreakPredicate", "val$outputChannels" })
        private final RestorableConfig restorableConfig = null;

        @Override
        public TransformationState<Page> process(@Nullable PageWithPosition pageWithPosition) {
            boolean finished = pageWithPosition == null;
            if (finished && pageBuilder.isEmpty()) {
                memoryContext.close();
                return TransformationState.finished();
            }
            if (finished || pageBreakPredicate.test(pageBuilder, pageWithPosition)) {
                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) {
                    pageWithPosition.appendTo(pageBuilder, outputChannels, outputTypes);
                }
                if (updateMemoryAfterEveryPosition) {
                    memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
                }
                return TransformationState.ofResult(page, !finished);
            }
            pageWithPosition.appendTo(pageBuilder, outputChannels, outputTypes);
            if (updateMemoryAfterEveryPosition) {
                memoryContext.setBytes(pageBuilder.getRetainedSizeInBytes());
            }
            return TransformationState.needsMoreData();
        }

        @Override
        public Object captureResult(Page result, BlockEncodingSerdeProvider serdeProvider) {
            if (result != null) {
                SerializedPage serializedPage = ((PagesSerde) serdeProvider).serialize(result);
                return serializedPage.capture(serdeProvider);
            }
            return null;
        }

        @Override
        public Page restoreResult(Object resultState, BlockEncodingSerdeProvider serdeProvider) {
            if (resultState != null) {
                return ((PagesSerde) serdeProvider).deserialize(SerializedPage.restoreSerializedPage(resultState));
            }
            return null;
        }

        @Override
        public Object captureInput(PageWithPosition input, BlockEncodingSerdeProvider serdeProvider) {
            if (input != null) {
                return input.capture(serdeProvider);
            }
            return null;
        }

        @Override
        public PageWithPosition restoreInput(Object inputState, PageWithPosition input, BlockEncodingSerdeProvider serdeProvider) {
            if (inputState != null) {
                return PageWithPosition.restorePageWithPosition(inputState, serdeProvider);
            }
            return null;
        }

        @Override
        public Object capture(BlockEncodingSerdeProvider serdeProvider) {
            BuildPageState myState = new BuildPageState();
            myState.pageBuilder = pageBuilder.capture(serdeProvider);
            myState.memoryContext = memoryContext.getBytes();
            return myState;
        }

        @Override
        public void restore(Object state, BlockEncodingSerdeProvider serdeProvider) {
            BuildPageState myState = (BuildPageState) state;
            pageBuilder.restore(myState.pageBuilder, serdeProvider);
            memoryContext.setBytes(myState.memoryContext);
        }
    });
}
Also used : LocalMemoryContext(io.prestosql.memory.context.LocalMemoryContext) WorkProcessor(io.prestosql.operator.WorkProcessor) BlockEncodingSerdeProvider(io.prestosql.spi.snapshot.BlockEncodingSerdeProvider) Page(io.prestosql.spi.Page) SerializedPage(io.hetu.core.transport.execution.buffer.SerializedPage) PageBuilder(io.prestosql.spi.PageBuilder) TransformationState(io.prestosql.operator.WorkProcessor.TransformationState) RestorableConfig(io.prestosql.spi.snapshot.RestorableConfig) SerializedPage(io.hetu.core.transport.execution.buffer.SerializedPage)

Example 12 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class InMemoryHashAggregationBuilder method buildResult.

protected WorkProcessor<Page> buildResult(IntIterator groupIds) {
    final PageBuilder pageBuilder = new PageBuilder(buildTypes());
    return WorkProcessor.create(() -> {
        if (!groupIds.hasNext()) {
            return WorkProcessor.ProcessState.finished();
        }
        pageBuilder.reset();
        List<Type> types = groupBy.getTypes();
        while (!pageBuilder.isFull() && groupIds.hasNext()) {
            int groupId = groupIds.nextInt();
            groupBy.appendValuesTo(groupId, pageBuilder, 0);
            pageBuilder.declarePosition();
            for (int i = 0; i < aggregators.size(); i++) {
                Aggregator aggregator = aggregators.get(i);
                BlockBuilder output = pageBuilder.getBlockBuilder(types.size() + i);
                aggregator.evaluate(groupId, output);
            }
        }
        return WorkProcessor.ProcessState.ofResult(pageBuilder.build());
    });
}
Also used : Type(io.prestosql.spi.type.Type) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 13 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class VacuumTableOperator method createFragmentsPage.

private Page createFragmentsPage() {
    Collection<Slice> fragments = getFutureValue(finishFuture);
    committed = true;
    updateWrittenBytes();
    // output page will only be constructed once,
    // so a new PageBuilder is constructed (instead of using PageBuilder.reset)
    PageBuilder page = new PageBuilder(fragments.size() + 1, ImmutableList.of(types.get(ROW_COUNT_CHANNEL), types.get(FRAGMENT_CHANNEL)));
    BlockBuilder rowsBuilder = page.getBlockBuilder(0);
    BlockBuilder fragmentBuilder = page.getBlockBuilder(1);
    // write row count
    page.declarePosition();
    BIGINT.writeLong(rowsBuilder, pageSink.getRowsWritten());
    fragmentBuilder.appendNull();
    // write fragments
    for (Slice fragment : fragments) {
        page.declarePosition();
        rowsBuilder.appendNull();
        VARBINARY.writeSlice(fragmentBuilder, fragment);
    }
    return page.build();
}
Also used : Slice(io.airlift.slice.Slice) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 14 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class TableUpdateOperator method getOutput.

@Override
public Page getOutput() {
    if (finished) {
        return null;
    }
    finished = true;
    OptionalLong rowsUpdatedCount = metadata.executeUpdate(session, tableHandle);
    // output page will only be constructed once,
    // so a new PageBuilder is constructed (instead of using PageBuilder.reset)
    PageBuilder page = new PageBuilder(1, TYPES);
    BlockBuilder rowsBuilder = page.getBlockBuilder(0);
    page.declarePosition();
    if (rowsUpdatedCount.isPresent()) {
        BIGINT.writeLong(rowsBuilder, rowsUpdatedCount.getAsLong());
    } else {
        rowsBuilder.appendNull();
    }
    return page.build();
}
Also used : OptionalLong(java.util.OptionalLong) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 15 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class TableWriterOperator method createFragmentsPage.

private Page createFragmentsPage() {
    Collection<Slice> fragments = getFutureValue(finishFuture);
    committed = true;
    updateWrittenBytes();
    // output page will only be constructed once,
    // so a new PageBuilder is constructed (instead of using PageBuilder.reset)
    PageBuilder page = new PageBuilder(fragments.size() + 1, ImmutableList.of(types.get(ROW_COUNT_CHANNEL), types.get(FRAGMENT_CHANNEL)));
    BlockBuilder rowsBuilder = page.getBlockBuilder(0);
    BlockBuilder fragmentBuilder = page.getBlockBuilder(1);
    // write row count
    page.declarePosition();
    BIGINT.writeLong(rowsBuilder, rowCount);
    fragmentBuilder.appendNull();
    // write fragments
    for (Slice fragment : fragments) {
        page.declarePosition();
        rowsBuilder.appendNull();
        VARBINARY.writeSlice(fragmentBuilder, fragment);
    }
    return page.build();
}
Also used : Slice(io.airlift.slice.Slice) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Aggregations

PageBuilder (io.prestosql.spi.PageBuilder)54 Page (io.prestosql.spi.Page)24 BlockBuilder (io.prestosql.spi.block.BlockBuilder)24 Type (io.prestosql.spi.type.Type)20 ImmutableList (com.google.common.collect.ImmutableList)14 Block (io.prestosql.spi.block.Block)11 Test (org.testng.annotations.Test)11 List (java.util.List)7 Slice (io.airlift.slice.Slice)6 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 ArrayType (io.prestosql.spi.type.ArrayType)5 ArrayList (java.util.ArrayList)5 BlockAssertions.createLongsBlock (io.prestosql.block.BlockAssertions.createLongsBlock)4 INTEGER (io.prestosql.spi.type.IntegerType.INTEGER)4 Collectors.toList (java.util.stream.Collectors.toList)4 Slices (io.airlift.slice.Slices)3 BlockAssertions.createLongSequenceBlock (io.prestosql.block.BlockAssertions.createLongSequenceBlock)3 BlockAssertions.createStringSequenceBlock (io.prestosql.block.BlockAssertions.createStringSequenceBlock)3 GroupByHash.createGroupByHash (io.prestosql.operator.GroupByHash.createGroupByHash)3 UsedByGeneratedCode (io.prestosql.spi.annotation.UsedByGeneratedCode)3