Search in sources :

Example 6 with ParameterMetadata

use of com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata in project presto by prestodb.

the class AccumulatorCompiler method generateInputForLoop.

private static BytecodeBlock generateInputForLoop(FieldDefinition stateField, List<ParameterMetadata> parameterMetadatas, MethodHandle inputFunction, Scope scope, List<Variable> parameterVariables, Variable masksBlock, CallSiteBinder callSiteBinder, boolean grouped) {
    // For-loop over rows
    Variable page = scope.getVariable("page");
    Variable positionVariable = scope.declareVariable(int.class, "position");
    Variable rowsVariable = scope.declareVariable(int.class, "rows");
    BytecodeBlock block = new BytecodeBlock().append(page).invokeVirtual(Page.class, "getPositionCount", int.class).putVariable(rowsVariable).initializeVariable(positionVariable);
    BytecodeNode loopBody = generateInvokeInputFunction(scope, stateField, positionVariable, parameterVariables, parameterMetadatas, inputFunction, callSiteBinder, grouped);
    //  Wrap with null checks
    List<Boolean> nullable = new ArrayList<>();
    for (ParameterMetadata metadata : parameterMetadatas) {
        switch(metadata.getParameterType()) {
            case INPUT_CHANNEL:
            case BLOCK_INPUT_CHANNEL:
                nullable.add(false);
                break;
            case NULLABLE_BLOCK_INPUT_CHANNEL:
                nullable.add(true);
                break;
            // do nothing
            default:
        }
    }
    checkState(nullable.size() == parameterVariables.size(), "Number of parameters does not match");
    for (int i = 0; i < parameterVariables.size(); i++) {
        if (!nullable.get(i)) {
            Variable variableDefinition = parameterVariables.get(i);
            loopBody = new IfStatement("if(!%s.isNull(position))", variableDefinition.getName()).condition(new BytecodeBlock().getVariable(variableDefinition).getVariable(positionVariable).invokeInterface(Block.class, "isNull", boolean.class, int.class)).ifFalse(loopBody);
        }
    }
    loopBody = new IfStatement("if(testMask(%s, position))", masksBlock.getName()).condition(new BytecodeBlock().getVariable(masksBlock).getVariable(positionVariable).invokeStatic(CompilerOperations.class, "testMask", boolean.class, Block.class, int.class)).ifTrue(loopBody);
    block.append(new ForLoop().initialize(new BytecodeBlock().putVariable(positionVariable, 0)).condition(new BytecodeBlock().getVariable(positionVariable).getVariable(rowsVariable).invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class)).update(new BytecodeBlock().incrementVariable(positionVariable, (byte) 1)).body(loopBody));
    return block;
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) ForLoop(com.facebook.presto.bytecode.control.ForLoop) CompilerOperations(com.facebook.presto.sql.gen.CompilerOperations) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ArrayList(java.util.ArrayList) Block(com.facebook.presto.spi.block.Block) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) GroupByIdBlock(com.facebook.presto.operator.GroupByIdBlock) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode) ParameterMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata)

Aggregations

ParameterMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata)6 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)3 Type (com.facebook.presto.spi.type.Type)3 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)2 ArrayType (com.facebook.presto.type.ArrayType)2 MethodHandle (java.lang.invoke.MethodHandle)2 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)1 CompilerUtils.defineClass (com.facebook.presto.bytecode.CompilerUtils.defineClass)1 Variable (com.facebook.presto.bytecode.Variable)1 ForLoop (com.facebook.presto.bytecode.control.ForLoop)1 IfStatement (com.facebook.presto.bytecode.control.IfStatement)1 GroupByIdBlock (com.facebook.presto.operator.GroupByIdBlock)1 ParameterMetadata.fromSqlType (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromSqlType)1 ArrayAggregationState (com.facebook.presto.operator.aggregation.state.ArrayAggregationState)1 ArrayAggregationStateFactory (com.facebook.presto.operator.aggregation.state.ArrayAggregationStateFactory)1 ArrayAggregationStateSerializer (com.facebook.presto.operator.aggregation.state.ArrayAggregationStateSerializer)1 BlockState (com.facebook.presto.operator.aggregation.state.BlockState)1 BlockStateSerializer (com.facebook.presto.operator.aggregation.state.BlockStateSerializer)1 MinMaxNStateFactory (com.facebook.presto.operator.aggregation.state.MinMaxNStateFactory)1 MinMaxNStateSerializer (com.facebook.presto.operator.aggregation.state.MinMaxNStateSerializer)1