Search in sources :

Example 26 with Page

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

the class MeasureComputation method computeEmpty.

public Block computeEmpty(long matchNumber) {
    // prepare input for the projection as an array of single-value blocks. for empty match:
    // - match_number() is the sequential number of the match
    // - classifier() is null
    // - all value references are null
    Block[] blocks = new Block[expectedLayout.size()];
    for (int i = 0; i < expectedLayout.size(); i++) {
        PhysicalValueAccessor accessor = expectedLayout.get(i);
        if (accessor instanceof PhysicalValuePointer) {
            PhysicalValuePointer pointer = (PhysicalValuePointer) accessor;
            if (pointer.getSourceChannel() == MATCH_NUMBER) {
                blocks[i] = nativeValueToBlock(BIGINT, matchNumber);
            } else {
                blocks[i] = nativeValueToBlock(pointer.getType(), null);
            }
        } else {
            MatchAggregation aggregation = aggregations[((MatchAggregationPointer) accessor).getIndex()];
            blocks[i] = aggregation.aggregateEmpty();
        }
    }
    // wrap block array into a single-row page
    Page page = new Page(1, blocks);
    // evaluate expression
    Work<Block> work = projection.project(session, new DriverYieldSignal(), projection.getInputChannels().getInputChannels(page), positionsRange(0, 1));
    boolean done = false;
    while (!done) {
        done = work.process();
    }
    return work.getResult();
}
Also used : Utils.nativeValueToBlock(io.trino.spi.predicate.Utils.nativeValueToBlock) Block(io.trino.spi.block.Block) DriverYieldSignal(io.trino.operator.DriverYieldSignal) Page(io.trino.spi.Page)

Example 27 with Page

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

the class MeasureComputation method compute.

// TODO This method allocates an intermediate block and passes it as the input to the pre-compiled expression.
// Instead, the expression should be compiled directly against the row navigations.
public static Block compute(int currentRow, ArrayView matchedLabels, MatchAggregation[] aggregations, int partitionStart, int searchStart, int searchEnd, int patternStart, long matchNumber, ProjectingPagesWindowIndex windowIndex, PageProjection projection, List<PhysicalValueAccessor> expectedLayout, Block[] nulls, List<String> labelNames, ConnectorSession session) {
    // get values at appropriate positions and prepare input for the projection as an array of single-value blocks
    Block[] blocks = new Block[expectedLayout.size()];
    for (int i = 0; i < expectedLayout.size(); i++) {
        PhysicalValueAccessor accessor = expectedLayout.get(i);
        if (accessor instanceof PhysicalValuePointer) {
            PhysicalValuePointer pointer = (PhysicalValuePointer) accessor;
            int channel = pointer.getSourceChannel();
            if (channel == MATCH_NUMBER) {
                blocks[i] = nativeValueToBlock(BIGINT, matchNumber);
            } else {
                int position = pointer.getLogicalIndexNavigation().resolvePosition(currentRow, matchedLabels, searchStart, searchEnd, patternStart);
                if (position >= 0) {
                    if (channel == CLASSIFIER) {
                        Type type = VARCHAR;
                        if (position < patternStart || position >= patternStart + matchedLabels.length()) {
                            // position out of match. classifier() function returns null.
                            blocks[i] = nativeValueToBlock(type, null);
                        } else {
                            // position within match. get the assigned label from matchedLabels.
                            // note: when computing measures, all labels of the match can be accessed (even those exceeding the current running position), both in RUNNING and FINAL semantics
                            blocks[i] = nativeValueToBlock(type, utf8Slice(labelNames.get(matchedLabels.get(position - patternStart))));
                        }
                    } else {
                        // TODO Block#getRegion
                        blocks[i] = windowIndex.getSingleValueBlock(channel, position - partitionStart);
                    }
                } else {
                    blocks[i] = nulls[i];
                }
            }
        } else {
            MatchAggregation aggregation = aggregations[((MatchAggregationPointer) accessor).getIndex()];
            blocks[i] = aggregation.aggregate(currentRow, matchedLabels, matchNumber, windowIndex, partitionStart, patternStart);
        }
    }
    // wrap block array into a single-row page
    Page page = new Page(1, blocks);
    // evaluate expression
    Work<Block> work = projection.project(session, new DriverYieldSignal(), projection.getInputChannels().getInputChannels(page), positionsRange(0, 1));
    boolean done = false;
    while (!done) {
        done = work.process();
    }
    return work.getResult();
}
Also used : Type(io.trino.spi.type.Type) Utils.nativeValueToBlock(io.trino.spi.predicate.Utils.nativeValueToBlock) Block(io.trino.spi.block.Block) DriverYieldSignal(io.trino.operator.DriverYieldSignal) Page(io.trino.spi.Page)

Example 28 with Page

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

the class UnnestOperator method getOutput.

@Override
public Page getOutput() {
    if (currentPage == null) {
        return null;
    }
    PageBuilderStatus pageBuilderStatus = new PageBuilderStatus(MAX_BYTES_PER_PAGE);
    prepareForNewOutput(pageBuilderStatus);
    int outputRowCount = 0;
    while (currentPosition < currentPage.getPositionCount()) {
        outputRowCount += processCurrentPosition();
        currentPosition++;
        if (outputRowCount >= MAX_ROWS_PER_BLOCK || pageBuilderStatus.isFull()) {
            break;
        }
    }
    Block[] outputBlocks = buildOutputBlocks();
    if (currentPosition == currentPage.getPositionCount()) {
        currentPage = null;
        currentPosition = 0;
    }
    return new Page(outputBlocks);
}
Also used : PageBuilderStatus(io.trino.spi.block.PageBuilderStatus) Block(io.trino.spi.block.Block) Page(io.trino.spi.Page)

Example 29 with Page

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

the class FileSingleStreamSpiller method readPages.

private Iterator<Page> readPages() {
    checkState(writable, "Repeated reads are disallowed to prevent potential resource leaks");
    writable = false;
    try {
        InputStream input = closer.register(targetFile.newInputStream());
        Iterator<Page> pages = PagesSerdeUtil.readPages(serde, input);
        return closeWhenExhausted(pages, input);
    } catch (IOException e) {
        fileSystemErrorHandler.run();
        throw new TrinoException(GENERIC_INTERNAL_ERROR, "Failed to read spilled pages", e);
    }
}
Also used : InputStream(java.io.InputStream) TrinoException(io.trino.spi.TrinoException) Page(io.trino.spi.Page) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 30 with Page

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

the class FileSingleStreamSpiller method writePages.

private void writePages(Iterator<Page> pageIterator) {
    checkState(writable, "Spilling no longer allowed. The spiller has been made non-writable on first read for subsequent reads to be consistent");
    try (SliceOutput output = new OutputStreamSliceOutput(targetFile.newOutputStream(APPEND), BUFFER_SIZE);
        PagesSerde.PagesSerdeContext context = serde.newContext()) {
        while (pageIterator.hasNext()) {
            Page page = pageIterator.next();
            spilledPagesInMemorySize += page.getSizeInBytes();
            Slice serializedPage = serde.serialize(context, page);
            long pageSize = serializedPage.length();
            localSpillContext.updateBytes(pageSize);
            spillerStats.addToTotalSpilledBytes(pageSize);
            output.writeBytes(serializedPage);
        }
    } catch (UncheckedIOException | IOException e) {
        fileSystemErrorHandler.run();
        throw new TrinoException(GENERIC_INTERNAL_ERROR, "Failed to spill pages", e);
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) OutputStreamSliceOutput(io.airlift.slice.OutputStreamSliceOutput) PagesSerde(io.trino.execution.buffer.PagesSerde) Slice(io.airlift.slice.Slice) TrinoException(io.trino.spi.TrinoException) Page(io.trino.spi.Page) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) OutputStreamSliceOutput(io.airlift.slice.OutputStreamSliceOutput)

Aggregations

Page (io.trino.spi.Page)579 Test (org.testng.annotations.Test)334 Block (io.trino.spi.block.Block)153 Type (io.trino.spi.type.Type)127 MaterializedResult (io.trino.testing.MaterializedResult)109 PlanNodeId (io.trino.sql.planner.plan.PlanNodeId)91 RowPagesBuilder (io.trino.RowPagesBuilder)72 RunLengthEncodedBlock (io.trino.spi.block.RunLengthEncodedBlock)68 ImmutableList (com.google.common.collect.ImmutableList)65 ArrayList (java.util.ArrayList)48 BlockBuilder (io.trino.spi.block.BlockBuilder)46 Optional (java.util.Optional)43 TaskContext (io.trino.operator.TaskContext)42 TestingTaskContext (io.trino.testing.TestingTaskContext)41 List (java.util.List)41 DictionaryBlock (io.trino.spi.block.DictionaryBlock)38 OperatorAssertion.toMaterializedResult (io.trino.operator.OperatorAssertion.toMaterializedResult)37 Slice (io.airlift.slice.Slice)36 OperatorFactory (io.trino.operator.OperatorFactory)32 LazyBlock (io.trino.spi.block.LazyBlock)32