Search in sources :

Example 1 with WhileLoop

use of com.facebook.presto.bytecode.control.WhileLoop in project presto by prestodb.

the class CursorProcessorCompiler method generateProcessMethod.

private static void generateProcessMethod(ClassDefinition classDefinition, int projections, Map<VariableReferenceExpression, CommonSubExpressionFields> cseFields) {
    Parameter properties = arg("properties", SqlFunctionProperties.class);
    Parameter yieldSignal = arg("yieldSignal", DriverYieldSignal.class);
    Parameter cursor = arg("cursor", RecordCursor.class);
    Parameter pageBuilder = arg("pageBuilder", PageBuilder.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "process", type(CursorProcessorOutput.class), properties, yieldSignal, cursor, pageBuilder);
    Scope scope = method.getScope();
    Variable completedPositionsVariable = scope.declareVariable(int.class, "completedPositions");
    Variable finishedVariable = scope.declareVariable(boolean.class, "finished");
    method.getBody().comment("int completedPositions = 0;").putVariable(completedPositionsVariable, 0).comment("boolean finished = false;").putVariable(finishedVariable, false);
    // while loop loop body
    LabelNode done = new LabelNode("done");
    BytecodeBlock whileFunctionBlock = new BytecodeBlock().comment("if (pageBuilder.isFull() || yieldSignal.isSet()) return new CursorProcessorOutput(completedPositions, false);").append(new IfStatement().condition(or(pageBuilder.invoke("isFull", boolean.class), yieldSignal.invoke("isSet", boolean.class))).ifTrue(jump(done))).comment("if (!cursor.advanceNextPosition()) return new CursorProcessorOutput(completedPositions, true);").append(new IfStatement().condition(cursor.invoke("advanceNextPosition", boolean.class)).ifFalse(new BytecodeBlock().putVariable(finishedVariable, true).gotoLabel(done)));
    // reset the CSE evaluatedField = false for every row
    cseFields.values().forEach(field -> whileFunctionBlock.append(scope.getThis().setField(field.getEvaluatedField(), constantBoolean(false))));
    whileFunctionBlock.comment("do the projection").append(createProjectIfStatement(classDefinition, method, properties, cursor, pageBuilder, projections)).comment("completedPositions++;").incrementVariable(completedPositionsVariable, (byte) 1);
    WhileLoop whileLoop = new WhileLoop().condition(constantTrue()).body(whileFunctionBlock);
    method.getBody().append(whileLoop).visitLabel(done).append(newInstance(CursorProcessorOutput.class, completedPositionsVariable, finishedVariable).ret());
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) WhileLoop(com.facebook.presto.bytecode.control.WhileLoop) CursorProcessorOutput(com.facebook.presto.operator.project.CursorProcessorOutput)

Aggregations

BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)1 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)1 Parameter (com.facebook.presto.bytecode.Parameter)1 Scope (com.facebook.presto.bytecode.Scope)1 Variable (com.facebook.presto.bytecode.Variable)1 IfStatement (com.facebook.presto.bytecode.control.IfStatement)1 WhileLoop (com.facebook.presto.bytecode.control.WhileLoop)1 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)1 CursorProcessorOutput (com.facebook.presto.operator.project.CursorProcessorOutput)1