use of io.trino.spi.Page in project trino by trinodb.
the class DictionaryAwarePageFilter method processDictionary.
private Optional<boolean[]> processDictionary(ConnectorSession session, Block dictionary) {
if (lastInputDictionary == dictionary) {
return lastOutputDictionary;
}
// Process dictionary if:
// this is the first block
// there is only entry in the dictionary
// the last dictionary was used for more positions than were in the dictionary
boolean shouldProcessDictionary = lastInputDictionary == null || dictionary.getPositionCount() == 1 || lastDictionaryUsageCount >= lastInputDictionary.getPositionCount();
lastDictionaryUsageCount = 0;
lastInputDictionary = dictionary;
if (shouldProcessDictionary) {
try {
SelectedPositions selectedDictionaryPositions = filter.filter(session, new Page(dictionary));
lastOutputDictionary = Optional.of(toPositionsMask(selectedDictionaryPositions, dictionary.getPositionCount()));
} catch (Exception ignored) {
// Processing of dictionary failed, but we ignore the exception here
// and force reprocessing of the whole block using the normal code.
// The second pass may not fail due to filtering.
// todo dictionary processing should be able to tolerate failures of unused elements
lastOutputDictionary = Optional.empty();
}
} else {
lastOutputDictionary = Optional.empty();
}
return lastOutputDictionary;
}
use of io.trino.spi.Page in project trino by trinodb.
the class ExtractSpatialJoins method loadKdbTree.
private static KdbTree loadKdbTree(String tableName, Session session, Metadata metadata, SplitManager splitManager, PageSourceManager pageSourceManager) {
QualifiedObjectName name = toQualifiedObjectName(tableName, session.getCatalog().get(), session.getSchema().get());
TableHandle tableHandle = metadata.getTableHandle(session, name).orElseThrow(() -> new TrinoException(INVALID_SPATIAL_PARTITIONING, format("Table not found: %s", name)));
Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
List<ColumnHandle> visibleColumnHandles = columnHandles.values().stream().filter(handle -> !metadata.getColumnMetadata(session, tableHandle, handle).isHidden()).collect(toImmutableList());
checkSpatialPartitioningTable(visibleColumnHandles.size() == 1, "Expected single column for table %s, but found %s columns", name, columnHandles.size());
ColumnHandle kdbTreeColumn = Iterables.getOnlyElement(visibleColumnHandles);
Optional<KdbTree> kdbTree = Optional.empty();
try (SplitSource splitSource = splitManager.getSplits(session, tableHandle, UNGROUPED_SCHEDULING, EMPTY, alwaysTrue())) {
while (!Thread.currentThread().isInterrupted()) {
SplitBatch splitBatch = getFutureValue(splitSource.getNextBatch(NOT_PARTITIONED, Lifespan.taskWide(), 1000));
List<Split> splits = splitBatch.getSplits();
for (Split split : splits) {
try (ConnectorPageSource pageSource = pageSourceManager.createPageSource(session, split, tableHandle, ImmutableList.of(kdbTreeColumn), DynamicFilter.EMPTY)) {
do {
getFutureValue(pageSource.isBlocked());
Page page = pageSource.getNextPage();
if (page != null && page.getPositionCount() > 0) {
checkSpatialPartitioningTable(kdbTree.isEmpty(), "Expected exactly one row for table %s, but found more", name);
checkSpatialPartitioningTable(page.getPositionCount() == 1, "Expected exactly one row for table %s, but found %s rows", name, page.getPositionCount());
String kdbTreeJson = VARCHAR.getSlice(page.getBlock(0), 0).toStringUtf8();
try {
kdbTree = Optional.of(KdbTreeUtils.fromJson(kdbTreeJson));
} catch (IllegalArgumentException e) {
checkSpatialPartitioningTable(false, "Invalid JSON string for KDB tree: %s", e.getMessage());
}
}
} while (!pageSource.isFinished());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
if (splitBatch.isLastBatch()) {
break;
}
}
}
checkSpatialPartitioningTable(kdbTree.isPresent(), "Expected exactly one row for table %s, but got none", name);
return kdbTree.get();
}
use of io.trino.spi.Page in project trino by trinodb.
the class DynamicFilterSourceOperator method getOutput.
@Override
public Page getOutput() {
Page result = current;
current = null;
return result;
}
use of io.trino.spi.Page in project trino by trinodb.
the class PageSplitterUtil method splitPage.
private static List<Page> splitPage(Page page, long maxPageSizeInBytes, long previousPageSize) {
checkArgument(page.getPositionCount() > 0, "page is empty");
checkArgument(maxPageSizeInBytes > 0, "maxPageSizeInBytes must be > 0");
// if the size of the page doesn't improve from the previous call we terminate the recursion.
if (page.getSizeInBytes() == previousPageSize || page.getSizeInBytes() <= maxPageSizeInBytes || page.getPositionCount() == 1) {
return ImmutableList.of(page);
}
ImmutableList.Builder<Page> outputPages = ImmutableList.builder();
long previousSize = page.getSizeInBytes();
int positionCount = page.getPositionCount();
int half = positionCount / 2;
Page leftHalf = page.getRegion(0, half);
outputPages.addAll(splitPage(leftHalf, maxPageSizeInBytes, previousSize));
Page rightHalf = page.getRegion(half, positionCount - half);
outputPages.addAll(splitPage(rightHalf, maxPageSizeInBytes, previousSize));
return outputPages.build();
}
use of io.trino.spi.Page in project trino by trinodb.
the class PagesSerdeUtil method writePages.
public static long writePages(PagesSerde serde, SliceOutput sliceOutput, Iterator<Page> pages) {
long size = 0;
try (PagesSerde.PagesSerdeContext context = serde.newContext()) {
while (pages.hasNext()) {
Page page = pages.next();
sliceOutput.writeBytes(serde.serialize(context, page));
size += page.getSizeInBytes();
}
}
return size;
}
Aggregations