Search in sources :

Example 76 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.

the class JoinProbeCompiler method generateGetCurrentJoinPosition.

private static void generateGetCurrentJoinPosition(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, FieldDefinition lookupSourceField, FieldDefinition probePageField, FieldDefinition pageField, Optional<Integer> probeHashChannel, FieldDefinition probeHashBlockField, FieldDefinition positionField) {
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "getCurrentJoinPosition", type(long.class));
    Variable thisVariable = method.getThis();
    BytecodeBlock body = method.getBody().append(new IfStatement().condition(thisVariable.invoke("currentRowContainsNull", boolean.class)).ifTrue(constantLong(-1).ret()));
    BytecodeExpression position = thisVariable.getField(positionField);
    BytecodeExpression hashChannelsPage = thisVariable.getField(probePageField);
    BytecodeExpression allChannelsPage = thisVariable.getField(pageField);
    BytecodeExpression probeHashBlock = thisVariable.getField(probeHashBlockField);
    if (probeHashChannel.isPresent()) {
        body.append(thisVariable.getField(lookupSourceField).invoke("getJoinPosition", long.class, position, hashChannelsPage, allChannelsPage, constantType(callSiteBinder, BigintType.BIGINT).invoke("getLong", long.class, probeHashBlock, position))).retLong();
    } else {
        body.append(thisVariable.getField(lookupSourceField).invoke("getJoinPosition", long.class, position, hashChannelsPage, allChannelsPage)).retLong();
    }
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 77 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.

the class JoinCompiler method generateConstructor.

private static void generateConstructor(ClassDefinition classDefinition, List<Integer> joinChannels, FieldDefinition sizeField, List<FieldDefinition> channelFields, List<FieldDefinition> joinChannelFields, FieldDefinition hashChannelField) {
    Parameter channels = arg("channels", type(List.class, type(List.class, Block.class)));
    Parameter hashChannel = arg("hashChannel", type(Optional.class, Integer.class));
    MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC), channels, hashChannel);
    Variable thisVariable = constructorDefinition.getThis();
    Variable blockIndex = constructorDefinition.getScope().declareVariable(int.class, "blockIndex");
    BytecodeBlock constructor = constructorDefinition.getBody().comment("super();").append(thisVariable).invokeConstructor(Object.class);
    constructor.comment("this.size = 0").append(thisVariable.setField(sizeField, constantLong(0L)));
    constructor.comment("Set channel fields");
    for (int index = 0; index < channelFields.size(); index++) {
        BytecodeExpression channel = channels.invoke("get", Object.class, constantInt(index)).cast(type(List.class, Block.class));
        constructor.append(thisVariable.setField(channelFields.get(index), channel));
        BytecodeBlock loopBody = new BytecodeBlock();
        constructor.comment("for(blockIndex = 0; blockIndex < channel.size(); blockIndex++) { size += channel.get(i).getRetainedSizeInBytes() }").append(new ForLoop().initialize(blockIndex.set(constantInt(0))).condition(new BytecodeBlock().append(blockIndex).append(channel.invoke("size", int.class)).invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class)).update(new BytecodeBlock().incrementVariable(blockIndex, (byte) 1)).body(loopBody));
        loopBody.append(thisVariable).append(thisVariable).getField(sizeField).append(channel.invoke("get", Object.class, blockIndex).cast(type(Block.class)).invoke("getRetainedSizeInBytes", int.class).cast(long.class)).longAdd().putField(sizeField);
    }
    constructor.comment("Set join channel fields");
    for (int index = 0; index < joinChannelFields.size(); index++) {
        BytecodeExpression joinChannel = channels.invoke("get", Object.class, constantInt(joinChannels.get(index))).cast(type(List.class, Block.class));
        constructor.append(thisVariable.setField(joinChannelFields.get(index), joinChannel));
    }
    constructor.comment("Set hashChannel");
    constructor.append(new IfStatement().condition(hashChannel.invoke("isPresent", boolean.class)).ifTrue(thisVariable.setField(hashChannelField, channels.invoke("get", Object.class, hashChannel.invoke("get", Object.class).cast(Integer.class).cast(int.class)))).ifFalse(thisVariable.setField(hashChannelField, constantNull(hashChannelField.getType()))));
    constructor.ret();
}
Also used : Variable(com.facebook.presto.bytecode.Variable) Optional(java.util.Optional) ForLoop(com.facebook.presto.bytecode.control.ForLoop) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) IfStatement(com.facebook.presto.bytecode.control.IfStatement) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 78 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.

the class AbstractMinMaxBy method generateOutputMethod.

private void generateOutputMethod(ClassDefinition definition, CallSiteBinder binder, Type valueType, Class<?> stateClass) {
    Parameter state = arg("state", stateClass);
    Parameter out = arg("out", BlockBuilder.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "output", type(void.class), state, out);
    IfStatement ifStatement = new IfStatement().condition(or(state.invoke("isFirstNull", boolean.class), state.invoke("isSecondNull", boolean.class))).ifTrue(new BytecodeBlock().append(out.invoke("appendNull", BlockBuilder.class)).pop());
    if (!valueType.equals(UNKNOWN)) {
        ifStatement.ifFalse(constantType(binder, valueType).writeValue(out, state.invoke("getSecond", valueType.getJavaType())));
    }
    method.getBody().append(ifStatement).ret();
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) Signature.orderableTypeParameter(com.facebook.presto.metadata.Signature.orderableTypeParameter) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder)

Example 79 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.

the class AccumulatorCompiler method generateConstructor.

private static void generateConstructor(ClassDefinition definition, FieldDefinition stateSerializerField, FieldDefinition stateFactoryField, FieldDefinition inputChannelsField, FieldDefinition maskChannelField, FieldDefinition stateField, boolean grouped) {
    Parameter stateSerializer = arg("stateSerializer", AccumulatorStateSerializer.class);
    Parameter stateFactory = arg("stateFactory", AccumulatorStateFactory.class);
    Parameter inputChannels = arg("inputChannels", type(List.class, Integer.class));
    Parameter maskChannel = arg("maskChannel", type(Optional.class, Integer.class));
    MethodDefinition method = definition.declareConstructor(a(PUBLIC), stateSerializer, stateFactory, inputChannels, maskChannel);
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    body.comment("super();").append(thisVariable).invokeConstructor(Object.class);
    body.append(thisVariable.setField(stateSerializerField, generateRequireNotNull(stateSerializer)));
    body.append(thisVariable.setField(stateFactoryField, generateRequireNotNull(stateFactory)));
    body.append(thisVariable.setField(inputChannelsField, generateRequireNotNull(inputChannels)));
    body.append(thisVariable.setField(maskChannelField, generateRequireNotNull(maskChannel)));
    String createState;
    if (grouped) {
        createState = "createGroupedState";
    } else {
        createState = "createSingleState";
    }
    body.append(thisVariable.setField(stateField, stateFactory.invoke(createState, Object.class).cast(stateField.getType())));
    body.ret();
}
Also used : Variable(com.facebook.presto.bytecode.Variable) Optional(java.util.Optional) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BytecodeExpressions.constantString(com.facebook.presto.bytecode.expression.BytecodeExpressions.constantString)

Example 80 with BytecodeBlock

use of com.facebook.presto.bytecode.BytecodeBlock 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

BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)82 Variable (com.facebook.presto.bytecode.Variable)57 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)43 IfStatement (com.facebook.presto.bytecode.control.IfStatement)42 Parameter (com.facebook.presto.bytecode.Parameter)38 Scope (com.facebook.presto.bytecode.Scope)33 Block (com.facebook.presto.spi.block.Block)23 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)18 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)18 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)17 ImmutableList (com.google.common.collect.ImmutableList)16 ForLoop (com.facebook.presto.bytecode.control.ForLoop)12 Type (com.facebook.presto.spi.type.Type)12 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)11 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)11 LazyBlock (com.facebook.presto.spi.block.LazyBlock)11 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)11 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)10 List (java.util.List)9 RowExpression (com.facebook.presto.sql.relational.RowExpression)7