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();
}
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();
}
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);
}
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);
}
}
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);
}
}
Aggregations