use of io.trino.spi.PageBuilder in project trino by trinodb.
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();
}
use of io.trino.spi.PageBuilder in project trino by trinodb.
the class TableDeleteOperator method getOutput.
@Override
public Page getOutput() {
if (finished) {
return null;
}
finished = true;
OptionalLong rowsDeletedCount = metadata.executeDelete(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 (rowsDeletedCount.isPresent()) {
BIGINT.writeLong(rowsBuilder, rowsDeletedCount.getAsLong());
} else {
rowsBuilder.appendNull();
}
return page.build();
}
use of io.trino.spi.PageBuilder in project trino by trinodb.
the class MapZipWithFunction method mapZipWith.
public static Block mapZipWith(Type keyType, Type leftValueType, Type rightValueType, MapType outputMapType, Object state, Block leftBlock, Block rightBlock, MapZipWithLambda function) {
SingleMapBlock leftMapBlock = (SingleMapBlock) leftBlock;
SingleMapBlock rightMapBlock = (SingleMapBlock) rightBlock;
Type outputValueType = outputMapType.getValueType();
PageBuilder pageBuilder = (PageBuilder) state;
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
// seekKey() can take non-trivial time when key is complicated value, such as a long VARCHAR or ROW.
boolean[] keyFound = new boolean[rightMapBlock.getPositionCount()];
for (int leftKeyPosition = 0; leftKeyPosition < leftMapBlock.getPositionCount(); leftKeyPosition += 2) {
Object key = readNativeValue(keyType, leftMapBlock, leftKeyPosition);
Object leftValue = readNativeValue(leftValueType, leftMapBlock, leftKeyPosition + 1);
int rightValuePosition = rightMapBlock.seekKey(key);
Object rightValue = null;
if (rightValuePosition != -1) {
rightValue = readNativeValue(rightValueType, rightMapBlock, rightValuePosition);
keyFound[rightValuePosition / 2] = true;
}
Object outputValue;
try {
outputValue = function.apply(key, leftValue, rightValue);
} catch (Throwable throwable) {
// Restore pageBuilder into a consistent state.
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throwIfUnchecked(throwable);
throw new RuntimeException(throwable);
}
keyType.appendTo(leftMapBlock, leftKeyPosition, blockBuilder);
writeNativeValue(outputValueType, blockBuilder, outputValue);
}
// iterate over keys that only exists in rightMapBlock
for (int rightKeyPosition = 0; rightKeyPosition < rightMapBlock.getPositionCount(); rightKeyPosition += 2) {
if (!keyFound[rightKeyPosition / 2]) {
Object key = readNativeValue(keyType, rightMapBlock, rightKeyPosition);
Object rightValue = readNativeValue(rightValueType, rightMapBlock, rightKeyPosition + 1);
Object outputValue;
try {
outputValue = function.apply(key, null, rightValue);
} catch (Throwable throwable) {
// Restore pageBuilder into a consistent state.
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throwIfUnchecked(throwable);
throw new RuntimeException(throwable);
}
keyType.appendTo(rightMapBlock, rightKeyPosition, blockBuilder);
writeNativeValue(outputValueType, blockBuilder, outputValue);
}
}
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return outputMapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.trino.spi.PageBuilder in project trino by trinodb.
the class MapConcatFunction method mapConcat.
@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, BlockPositionEqual keyEqual, BlockPositionHashCode keyHashCode, Object state, Block[] maps) {
int entries = 0;
int lastMapIndex = maps.length - 1;
int firstMapIndex = lastMapIndex;
for (int i = 0; i < maps.length; i++) {
entries += maps[i].getPositionCount();
if (maps[i].getPositionCount() > 0) {
lastMapIndex = i;
firstMapIndex = min(firstMapIndex, i);
}
}
if (lastMapIndex == firstMapIndex) {
return maps[lastMapIndex];
}
PageBuilder pageBuilder = (PageBuilder) state;
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
// TODO: we should move TypedSet into user state as well
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
TypedSet typedSet = createEqualityTypedSet(keyType, keyEqual, keyHashCode, entries / 2, FUNCTION_NAME);
BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
// the last map
Block map = maps[lastMapIndex];
for (int i = 0; i < map.getPositionCount(); i += 2) {
typedSet.add(map, i);
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
// the map between the last and the first
for (int idx = lastMapIndex - 1; idx > firstMapIndex; idx--) {
map = maps[idx];
for (int i = 0; i < map.getPositionCount(); i += 2) {
if (typedSet.add(map, i)) {
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
}
}
// the first map
map = maps[firstMapIndex];
for (int i = 0; i < map.getPositionCount(); i += 2) {
if (!typedSet.contains(map, i)) {
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
}
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.trino.spi.PageBuilder in project trino by trinodb.
the class ParquetTester method writeParquetColumnTrino.
private static void writeParquetColumnTrino(File outputFile, List<Type> types, List<String> columnNames, Iterator<?>[] values, int size, CompressionCodecName compressionCodecName) throws Exception {
checkArgument(types.size() == columnNames.size() && types.size() == values.length);
ParquetSchemaConverter schemaConverter = new ParquetSchemaConverter(types, columnNames);
ParquetWriter writer = new ParquetWriter(new FileOutputStream(outputFile), schemaConverter.getMessageType(), schemaConverter.getPrimitiveTypes(), ParquetWriterOptions.builder().setMaxPageSize(DataSize.ofBytes(100)).setMaxBlockSize(DataSize.ofBytes(100000)).build(), compressionCodecName, "test-version");
PageBuilder pageBuilder = new PageBuilder(types);
for (int i = 0; i < types.size(); ++i) {
Type type = types.get(i);
Iterator<?> iterator = values[i];
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i);
for (int j = 0; j < size; ++j) {
checkState(iterator.hasNext());
Object value = iterator.next();
writeValue(type, blockBuilder, value);
}
}
pageBuilder.declarePositions(size);
writer.write(pageBuilder.build());
writer.close();
}
Aggregations