Search in sources :

Example 31 with PageBuilder

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

the class PinotSegmentPageSource method getNextPage.

/**
 * @return constructed page for pinot data.
 */
@Override
public Page getNextPage() {
    if (isFinished()) {
        close();
        return null;
    }
    if (!isPinotDataFetched) {
        fetchPinotData();
    }
    // To reduce memory usage, remove dataTable from dataTableList once it's processed.
    if (currentDataTable != null) {
        estimatedMemoryUsageInBytes -= currentDataTable.getEstimatedSizeInBytes();
    }
    if (dataTableList.size() == 0) {
        close();
        return null;
    }
    currentDataTable = dataTableList.pop();
    PageBuilder pageBuilder = new PageBuilder(columnTypes);
    // Note that declared positions in the Page should be the same with number of rows in each Block
    pageBuilder.declarePositions(currentDataTable.getDataTable().getNumberOfRows());
    for (int columnHandleIdx = 0; columnHandleIdx < columnHandles.size(); columnHandleIdx++) {
        BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(columnHandleIdx);
        Type columnType = columnTypes.get(columnHandleIdx);
        // Write a block for each column in the original order.
        writeBlock(blockBuilder, columnType, columnHandleIdx);
    }
    return pageBuilder.build();
}
Also used : Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) ColumnDataType(org.apache.pinot.common.utils.DataSchema.ColumnDataType) VarbinaryType(io.trino.spi.type.VarbinaryType) PageBuilder(io.trino.spi.PageBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 32 with PageBuilder

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

the class ArrayTransformFunction method generateTransform.

private static Class<?> generateTransform(Type inputType, Type outputType) {
    CallSiteBinder binder = new CallSiteBinder();
    Class<?> inputJavaType = Primitives.wrap(inputType.getJavaType());
    Class<?> outputJavaType = Primitives.wrap(outputType.getJavaType());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("ArrayTransform"), type(Object.class));
    definition.declareDefaultConstructor(a(PRIVATE));
    // define createPageBuilder
    MethodDefinition createPageBuilderMethod = definition.declareMethod(a(PUBLIC, STATIC), "createPageBuilder", type(PageBuilder.class));
    createPageBuilderMethod.getBody().append(newInstance(PageBuilder.class, constantType(binder, new ArrayType(outputType)).invoke("getTypeParameters", List.class)).ret());
    // define transform method
    Parameter pageBuilder = arg("pageBuilder", PageBuilder.class);
    Parameter block = arg("block", Block.class);
    Parameter function = arg("function", UnaryFunctionInterface.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "transform", type(Block.class), ImmutableList.of(pageBuilder, block, function));
    BytecodeBlock body = method.getBody();
    Scope scope = method.getScope();
    Variable positionCount = scope.declareVariable(int.class, "positionCount");
    Variable position = scope.declareVariable(int.class, "position");
    Variable blockBuilder = scope.declareVariable(BlockBuilder.class, "blockBuilder");
    Variable inputElement = scope.declareVariable(inputJavaType, "inputElement");
    Variable outputElement = scope.declareVariable(outputJavaType, "outputElement");
    // invoke block.getPositionCount()
    body.append(positionCount.set(block.invoke("getPositionCount", int.class)));
    // reset page builder if it is full
    body.append(new IfStatement().condition(pageBuilder.invoke("isFull", boolean.class)).ifTrue(pageBuilder.invoke("reset", void.class)));
    // get block builder
    body.append(blockBuilder.set(pageBuilder.invoke("getBlockBuilder", BlockBuilder.class, constantInt(0))));
    BytecodeNode loadInputElement;
    if (!inputType.equals(UNKNOWN)) {
        loadInputElement = new IfStatement().condition(block.invoke("isNull", boolean.class, position)).ifTrue(inputElement.set(constantNull(inputJavaType))).ifFalse(inputElement.set(constantType(binder, inputType).getValue(block, position).cast(inputJavaType)));
    } else {
        loadInputElement = new BytecodeBlock().append(inputElement.set(constantNull(inputJavaType)));
    }
    BytecodeNode writeOutputElement;
    if (!outputType.equals(UNKNOWN)) {
        writeOutputElement = new IfStatement().condition(equal(outputElement, constantNull(outputJavaType))).ifTrue(blockBuilder.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(constantType(binder, outputType).writeValue(blockBuilder, outputElement.cast(outputType.getJavaType())));
    } else {
        writeOutputElement = new BytecodeBlock().append(blockBuilder.invoke("appendNull", BlockBuilder.class).pop());
    }
    body.append(new ForLoop().initialize(position.set(constantInt(0))).condition(lessThan(position, positionCount)).update(incrementVariable(position, (byte) 1)).body(new BytecodeBlock().append(loadInputElement).append(outputElement.set(function.invoke("apply", Object.class, inputElement.cast(Object.class)).cast(outputJavaType))).append(writeOutputElement)));
    body.append(pageBuilder.invoke("declarePositions", void.class, positionCount));
    body.append(blockBuilder.invoke("getRegion", Block.class, subtract(blockBuilder.invoke("getPositionCount", int.class), positionCount), positionCount).ret());
    return defineClass(definition, Object.class, binder.getBindings(), ArrayTransformFunction.class.getClassLoader());
}
Also used : Signature.typeVariable(io.trino.metadata.Signature.typeVariable) Variable(io.airlift.bytecode.Variable) VariableInstruction.incrementVariable(io.airlift.bytecode.instruction.VariableInstruction.incrementVariable) ForLoop(io.airlift.bytecode.control.ForLoop) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) PageBuilder(io.trino.spi.PageBuilder) ClassDefinition(io.airlift.bytecode.ClassDefinition) ArrayType(io.trino.spi.type.ArrayType) IfStatement(io.airlift.bytecode.control.IfStatement) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) CallSiteBinder(io.trino.sql.gen.CallSiteBinder) Parameter(io.airlift.bytecode.Parameter) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Block(io.trino.spi.block.Block) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) BytecodeNode(io.airlift.bytecode.BytecodeNode) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 33 with PageBuilder

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

the class MapConstructor method createMap.

@UsedByGeneratedCode
public static Block createMap(MapType mapType, MethodHandle keyIndeterminate, State state, ConnectorSession session, Block keyBlock, Block valueBlock) {
    checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length");
    PageBuilder pageBuilder = state.getPageBuilder();
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) pageBuilder.getBlockBuilder(0);
    mapBlockBuilder.strict();
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keyBlock.getPositionCount(); i++) {
        if (keyBlock.isNull(i)) {
            // close block builder before throwing as we may be in a TRY() call
            // so that subsequent calls do not find it in an inconsistent state
            mapBlockBuilder.closeEntry();
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        Object keyObject = readNativeValue(mapType.getKeyType(), keyBlock, i);
        try {
            if ((boolean) keyIndeterminate.invoke(keyObject)) {
                throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be indeterminate: " + mapType.getKeyType().getObjectValue(session, keyBlock, i));
            }
        } catch (Throwable t) {
            mapBlockBuilder.closeEntry();
            throw internalError(t);
        }
        mapType.getKeyType().appendTo(keyBlock, i, blockBuilder);
        mapType.getValueType().appendTo(valueBlock, i, blockBuilder);
    }
    try {
        mapBlockBuilder.closeEntry();
    } catch (DuplicateMapKeyException e) {
        throw e.withDetailedMessage(mapType.getKeyType(), session);
    } finally {
        pageBuilder.declarePosition();
    }
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) TrinoException(io.trino.spi.TrinoException) DuplicateMapKeyException(io.trino.spi.block.DuplicateMapKeyException) PageBuilder(io.trino.spi.PageBuilder) MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 34 with PageBuilder

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

the class GeoFunctions method geometryNearestPoints.

@SqlNullable
@Description("Return the closest points on the two geometries")
@ScalarFunction("geometry_nearest_points")
@SqlType("row(" + GEOMETRY_TYPE_NAME + "," + GEOMETRY_TYPE_NAME + ")")
public static Block geometryNearestPoints(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) {
    Geometry leftGeometry = JtsGeometrySerde.deserialize(left);
    Geometry rightGeometry = JtsGeometrySerde.deserialize(right);
    if (leftGeometry.isEmpty() || rightGeometry.isEmpty()) {
        return null;
    }
    RowType rowType = RowType.anonymous(ImmutableList.of(GEOMETRY, GEOMETRY));
    PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(rowType));
    GeometryFactory geometryFactory = leftGeometry.getFactory();
    Coordinate[] nearestCoordinates = DistanceOp.nearestPoints(leftGeometry, rightGeometry);
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder entryBlockBuilder = blockBuilder.beginBlockEntry();
    GEOMETRY.writeSlice(entryBlockBuilder, JtsGeometrySerde.serialize(geometryFactory.createPoint(nearestCoordinates[0])));
    GEOMETRY.writeSlice(entryBlockBuilder, JtsGeometrySerde.serialize(geometryFactory.createPoint(nearestCoordinates[1])));
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return rowType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OGCGeometry.createFromEsriGeometry(com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry) GeometryUtils.jsonFromJtsGeometry(io.trino.geospatial.GeometryUtils.jsonFromJtsGeometry) Geometry(org.locationtech.jts.geom.Geometry) MultiVertexGeometry(com.esri.core.geometry.MultiVertexGeometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) RowType(io.trino.spi.type.RowType) PageBuilder(io.trino.spi.PageBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 35 with PageBuilder

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

the class MultiChannelGroupByHash method startNewPage.

private void startNewPage() {
    if (currentPageBuilder != null) {
        completedPagesMemorySize += currentPageBuilder.getRetainedSizeInBytes();
        currentPageBuilder = currentPageBuilder.newPageBuilderLike();
    } else {
        currentPageBuilder = new PageBuilder(types);
    }
    for (int i = 0; i < types.size(); i++) {
        channelBuilders.get(i).add(currentPageBuilder.getBlockBuilder(i));
    }
}
Also used : PageBuilder(io.trino.spi.PageBuilder)

Aggregations

PageBuilder (io.trino.spi.PageBuilder)58 BlockBuilder (io.trino.spi.block.BlockBuilder)24 Page (io.trino.spi.Page)23 Type (io.trino.spi.type.Type)22 ImmutableList (com.google.common.collect.ImmutableList)14 Block (io.trino.spi.block.Block)11 Test (org.testng.annotations.Test)10 List (java.util.List)8 Slice (io.airlift.slice.Slice)6 ArrayType (io.trino.spi.type.ArrayType)6 INTEGER (io.trino.spi.type.IntegerType.INTEGER)5 Slices (io.airlift.slice.Slices)4 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)4 DictionaryBlock (io.trino.spi.block.DictionaryBlock)4 RunLengthEncodedBlock (io.trino.spi.block.RunLengthEncodedBlock)4 BIGINT (io.trino.spi.type.BigintType.BIGINT)4 DOUBLE (io.trino.spi.type.DoubleType.DOUBLE)4 MapType (io.trino.spi.type.MapType)4 VarcharType.createUnboundedVarcharType (io.trino.spi.type.VarcharType.createUnboundedVarcharType)4 RowType (io.trino.spi.type.RowType)3