Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.sql.gen;
 
 
 import java.util.List;
 
 import static com.facebook.presto.byteCode.Access.PUBLIC;
 import static com.facebook.presto.byteCode.Access.a;
 import static com.facebook.presto.byteCode.Parameter.arg;
 import static com.facebook.presto.byteCode.OpCode.NOP;
 import static com.facebook.presto.byteCode.ParameterizedType.type;
 import static com.facebook.presto.sql.gen.ByteCodeUtils.generateWrite;
 import static java.lang.String.format;
 
 public class CursorProcessorCompiler
         implements BodyCompiler<CursorProcessor>
 {
     private final Metadata metadata;
 
     public CursorProcessorCompiler(Metadata metadata)
     {
         this. = metadata;
     }
 
     @Override
     public void generateMethods(ClassDefinition classDefinitionCallSiteBinder callSiteBinderRowExpression filterList<RowExpressionprojections)
     {
         generateProcessMethod(classDefinitionprojections.size());
         generateFilterMethod(classDefinitioncallSiteBinderfilter);
 
         for (int i = 0; i < projections.size(); i++) {
             generateProjectMethod(classDefinitioncallSiteBinder"project_" + iprojections.get(i));
         }
     }
 
     private void generateProcessMethod(ClassDefinition classDefinitionint projections)
     {
         Parameter session = arg("session"ConnectorSession.class);
         Parameter cursor = arg("cursor"RecordCursor.class);
         Parameter count = arg("count"int.class);
         Parameter pageBuilder = arg("pageBuilder"PageBuilder.class);
         MethodDefinition method = classDefinition.declareMethod(a(), "process"type(int.class), sessioncursorcountpageBuilder);
 
         Scope scope = method.getScope();
         Variable thisVariable = method.getThis();
         Variable completedPositionsVariable = scope.declareVariable(int.class"completedPositions");
 
         method.getBody()
                 .comment("int completedPositions = 0;")
                 .putVariable(completedPositionsVariable, 0);
 
         //
         // for loop loop body
         //
         LabelNode done = new LabelNode("done");
         ForLoop forLoop = new ForLoop()
                 .initialize()
                 .condition(new Block()
                                 .comment("completedPositions < count")
                                 .getVariable(completedPositionsVariable)
                                 .getVariable(count)
                                 .invokeStatic(CompilerOperations.class"lessThan"boolean.classint.classint.class)
                 )
                 .update(new Block()
                                .comment("completedPositions++")
                                .incrementVariable(completedPositionsVariable, (byte) 1)
                );
        Block forLoopBody = new Block()
                .comment("if (pageBuilder.isFull()) break;")
                .append(new Block()
                        .getVariable(pageBuilder)
                        .invokeVirtual(PageBuilder.class"isFull"boolean.class)
                        .ifTrueGoto(done))
                .comment("if (!cursor.advanceNextPosition()) break;")
                .append(new Block()
                        .getVariable(cursor)
                        .invokeInterface(RecordCursor.class"advanceNextPosition"boolean.class)
                        .ifFalseGoto(done));
        forLoop.body(forLoopBody);
        // if (filter(cursor))
        IfStatement ifStatement = new IfStatement();
        ifStatement.condition()
                .append(method.getThis())
                .getVariable(session)
                .getVariable(cursor)
                .invokeVirtual(classDefinition.getType(), "filter"type(boolean.class), type(ConnectorSession.class), type(RecordCursor.class));
        // pageBuilder.declarePosition();
        ifStatement.ifTrue()
                .getVariable(pageBuilder)
                .invokeVirtual(PageBuilder.class"declarePosition"void.class);
        // this.project_43(session, cursor, pageBuilder.getBlockBuilder(42)));
        for (int projectionIndex = 0; projectionIndex < projectionsprojectionIndex++) {
            ifStatement.ifTrue()
                    .append(method.getThis())
                    .getVariable(session)
                    .getVariable(cursor);
            // pageBuilder.getBlockBuilder(0)
            ifStatement.ifTrue()
                    .getVariable(pageBuilder)
                    .push(projectionIndex)
                    .invokeVirtual(PageBuilder.class"getBlockBuilder"BlockBuilder.classint.class);
            // project(block..., blockBuilder)
            ifStatement.ifTrue()
                    .invokeVirtual(classDefinition.getType(),
                    "project_" + projectionIndex,
                    type(void.class),
                    type(ConnectorSession.class),
                    type(RecordCursor.class),
                    type(BlockBuilder.class));
        }
        forLoopBody.append(ifStatement);
        method.getBody()
                .append(forLoop)
                .visitLabel(done)
                .comment("return completedPositions;")
                .getVariable(completedPositionsVariable)
                .retInt();
    }
    private void generateFilterMethod(ClassDefinition classDefinitionCallSiteBinder callSiteBinderRowExpression filter)
    {
        Parameter session = arg("session"ConnectorSession.class);
        Parameter cursor = arg("cursor"RecordCursor.class);
        MethodDefinition method = classDefinition.declareMethod(a(), "filter"type(boolean.class), sessioncursor);
        method.comment("Filter: %s"filter);
        Scope scope = method.getScope();
        Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull");
        ByteCodeExpressionVisitor visitor = new ByteCodeExpressionVisitor(callSiteBinderfieldReferenceCompiler(cursorwasNullVariable), .getFunctionRegistry());
        LabelNode end = new LabelNode("end");
        method.getBody()
                .comment("boolean wasNull = false;")
                .putVariable(wasNullVariablefalse)
                .comment("evaluate filter: " + filter)
                .append(filter.accept(visitorscope))
                .comment("if (wasNull) return false;")
                .getVariable(wasNullVariable)
                .ifFalseGoto(end)
                .pop(boolean.class)
                .push(false)
                .visitLabel(end)
                .retBoolean();
    }
    private void generateProjectMethod(ClassDefinition classDefinitionCallSiteBinder callSiteBinderString methodNameRowExpression projection)
    {
        Parameter session = arg("session"ConnectorSession.class);
        Parameter cursor = arg("cursor"RecordCursor.class);
        Parameter output = arg("output"BlockBuilder.class);
        MethodDefinition method = classDefinition.declareMethod(a(), methodNametype(void.class), sessioncursoroutput);
        method.comment("Projection: %s"projection.toString());
        Scope scope = method.getScope();
        Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull");
        Block body = method.getBody()
                .comment("boolean wasNull = false;")
                .putVariable(wasNullVariablefalse);
        ByteCodeExpressionVisitor visitor = new ByteCodeExpressionVisitor(callSiteBinderfieldReferenceCompiler(cursorwasNullVariable), .getFunctionRegistry());
        body.getVariable(output)
                .comment("evaluate projection: " + projection.toString())
                .append(projection.accept(visitorscope))
                .append(generateWrite(callSiteBinderscopewasNullVariableprojection.getType()))
                .ret();
    }
    private RowExpressionVisitor<ScopeByteCodeNodefieldReferenceCompiler(final Variable cursorVariablefinal Variable wasNullVariable)
    {
        return new RowExpressionVisitor<ScopeByteCodeNode>()
        {
            @Override
            public ByteCodeNode visitInputReference(InputReferenceExpression nodeScope scope)
            {
                int field = node.getField();
                Type type = node.getType();
                Class<?> javaType = type.getJavaType();
                IfStatement ifStatement = new IfStatement();
                ifStatement.condition()
                        .setDescription(format("cursor.get%s(%d)"typefield))
                        .getVariable(cursorVariable)
                        .push(field)
                        .invokeInterface(RecordCursor.class"isNull"boolean.classint.class);
                ifStatement.ifTrue()
                        .putVariable(wasNullVariabletrue)
                        .pushJavaDefault(javaType);
                ifStatement.ifFalse()
                        .getVariable(cursorVariable)
                        .push(field)
                        .invokeInterface(RecordCursor.class"get" + Primitives.wrap(javaType).getSimpleName(), javaTypeint.class);
                return ifStatement;
            }
            @Override
            public ByteCodeNode visitCall(CallExpression callScope scope)
            {
                throw new UnsupportedOperationException("not yet implemented");
            }
            @Override
            public ByteCodeNode visitConstant(ConstantExpression literalScope scope)
            {
                throw new UnsupportedOperationException("not yet implemented");
            }
        };
    }
New to GrepCode? Check out our FAQ X