Search in sources :

Example 51 with Variable

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

the class BytecodeUtils method unboxPrimitiveIfNecessary.

public static BytecodeBlock unboxPrimitiveIfNecessary(Scope scope, Class<?> boxedType) {
    BytecodeBlock block = new BytecodeBlock();
    LabelNode end = new LabelNode("end");
    Class<?> unboxedType = Primitives.unwrap(boxedType);
    Variable wasNull = scope.getVariable("wasNull");
    if (unboxedType.isPrimitive()) {
        LabelNode notNull = new LabelNode("notNull");
        block.dup(boxedType).ifNotNullGoto(notNull).append(wasNull.set(constantTrue())).comment("swap boxed null with unboxed default").pop(boxedType).pushJavaDefault(unboxedType).gotoLabel(end).visitLabel(notNull).append(unboxPrimitive(unboxedType));
    } else {
        block.dup(boxedType).ifNotNullGoto(end).append(wasNull.set(constantTrue()));
    }
    block.visitLabel(end);
    return block;
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 52 with Variable

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

the class BytecodeUtils method handleNullValue.

public static BytecodeNode handleNullValue(Scope scope, LabelNode label, Class<?> methodReturnType, List<Class<?>> stackArgsToPop, Optional<Variable> outputBlockVariable, boolean clearNullFlag) {
    if (outputBlockVariable.isPresent()) {
        checkArgument(methodReturnType == void.class);
    }
    Variable wasNull = scope.getVariable("wasNull");
    BytecodeBlock nullCheck = new BytecodeBlock().setDescription("ifWasNullGoto").append(wasNull);
    String clearComment = null;
    if (clearNullFlag) {
        nullCheck.append(wasNull.set(constantFalse()));
        clearComment = "clear wasNull";
    }
    BytecodeBlock isNull = new BytecodeBlock();
    for (Class<?> parameterType : stackArgsToPop) {
        isNull.pop(parameterType);
    }
    String loadDefaultOrAppendNullComment;
    if (!outputBlockVariable.isPresent()) {
        isNull.pushJavaDefault(methodReturnType);
        loadDefaultOrAppendNullComment = format("loadJavaDefault(%s)", methodReturnType.getName());
    } else {
        isNull.append(outputBlockVariable.get().invoke("appendNull", BlockBuilder.class).pop());
        loadDefaultOrAppendNullComment = "appendNullToOutputBlock";
    }
    isNull.gotoLabel(label);
    String popComment = null;
    if (!stackArgsToPop.isEmpty()) {
        popComment = format("pop(%s)", Joiner.on(", ").join(stackArgsToPop));
    }
    return new IfStatement("if wasNull then %s", Joiner.on(", ").skipNulls().join(clearComment, popComment, loadDefaultOrAppendNullComment, "goto " + label.getLabel())).condition(nullCheck).ifTrue(isNull);
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 53 with Variable

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

the class CoalesceCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments, Optional<Variable> outputBlockVariable) {
    Variable wasNull = generatorContext.wasNull();
    LabelNode endLabel = new LabelNode("end");
    BytecodeBlock block = new BytecodeBlock();
    // Generate all but the last one.
    for (RowExpression expression : arguments.subList(0, arguments.size() - 1)) {
        block.append(generatorContext.generate(expression, Optional.empty()));
        // Check if null
        IfStatement ifStatement = new IfStatement().condition(wasNull);
        ifStatement.ifTrue().pop(returnType.getJavaType()).append(wasNull.set(constantFalse()));
        ifStatement.ifFalse().gotoLabel(endLabel);
        block.append(ifStatement);
    }
    // Just return the last one here if control reaches here.
    block.append(generatorContext.generate(arguments.get(arguments.size() - 1), Optional.empty()));
    block.visitLabel(endLabel);
    outputBlockVariable.ifPresent(output -> block.append(generateWrite(generatorContext, returnType, output)));
    return block;
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Example 54 with Variable

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

the class AccumulatorCompiler method generateGroupedEvaluateFinal.

private static void generateGroupedEvaluateFinal(ClassDefinition definition, List<FieldDefinition> stateFields, MethodHandle outputFunction, CallSiteBinder callSiteBinder) {
    Parameter groupId = arg("groupId", int.class);
    Parameter out = arg("out", BlockBuilder.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC), "evaluateFinal", type(void.class), groupId, out);
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    List<BytecodeExpression> states = new ArrayList<>();
    for (FieldDefinition stateField : stateFields) {
        BytecodeExpression state = thisVariable.getField(stateField);
        body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class)));
        states.add(state);
    }
    body.comment("output(state_0, state_1, ..., out)");
    states.forEach(body::append);
    body.append(out);
    body.append(invoke(callSiteBinder.bind(outputFunction), "output"));
    body.ret();
}
Also used : Variable(com.facebook.presto.bytecode.Variable) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ArrayList(java.util.ArrayList) Parameter(com.facebook.presto.bytecode.Parameter) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 55 with Variable

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

the class AccumulatorCompiler method generateConstructor.

private static void generateConstructor(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors, List<FieldDefinition> inputChannelFields, FieldDefinition maskChannelField, List<FieldDefinition> lambdaProviderFields, boolean grouped) {
    Parameter stateDescriptors = arg("stateDescriptors", type(List.class, AccumulatorStateDescriptor.class));
    Parameter inputChannels = arg("inputChannels", type(List.class, Integer.class));
    Parameter maskChannel = arg("maskChannel", type(Optional.class, Integer.class));
    Parameter lambdaProviders = arg("lambdaProviders", type(List.class, LambdaProvider.class));
    MethodDefinition method = definition.declareConstructor(a(PUBLIC), stateDescriptors, inputChannels, maskChannel, lambdaProviders);
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    body.comment("super();").append(thisVariable).invokeConstructor(Object.class);
    for (int i = 0; i < stateFieldAndDescriptors.size(); i++) {
        body.append(thisVariable.setField(stateFieldAndDescriptors.get(i).getStateSerializerField(), stateDescriptors.invoke("get", Object.class, constantInt(i)).cast(AccumulatorStateDescriptor.class).invoke("getSerializer", AccumulatorStateSerializer.class)));
        body.append(thisVariable.setField(stateFieldAndDescriptors.get(i).getStateFactoryField(), stateDescriptors.invoke("get", Object.class, constantInt(i)).cast(AccumulatorStateDescriptor.class).invoke("getFactory", AccumulatorStateFactory.class)));
    }
    for (int i = 0; i < lambdaProviderFields.size(); i++) {
        body.append(thisVariable.setField(lambdaProviderFields.get(i), lambdaProviders.invoke("get", Object.class, constantInt(i)).cast(LambdaProvider.class)));
    }
    body.append(thisVariable.setField(maskChannelField, invokeStatic(type(CompilerOperations.class), "optionalChannelToIntOrNegative", type(int.class), ImmutableList.of(type(Optional.class, Integer.class)), generateRequireNotNull(maskChannel))));
    // Validate inputChannels argument list
    body.append(generateRequireNotNull(inputChannels)).push(inputChannelFields.size()).invokeStatic(type(CompilerOperations.class), "validateChannelsListLength", type(void.class), ImmutableList.of(type(List.class, Integer.class), type(int.class)));
    // Initialize inputChannels fields
    if (!inputChannelFields.isEmpty()) {
        // Assign each inputChannel field from the list or assign -1 if the input list is empty (eg: intermediate aggregations don't use the input parameters)
        BytecodeBlock assignFromList = new BytecodeBlock().comment("(this.inputChannel = %s.get(i), ...)", inputChannels.getName());
        BytecodeBlock assignNegative = new BytecodeBlock().comment("(this.inputChannel = -1, ...)");
        for (int i = 0; i < inputChannelFields.size(); i++) {
            FieldDefinition inputChannelField = inputChannelFields.get(i);
            assignFromList.append(thisVariable.setField(inputChannelField, inputChannels.invoke("get", Object.class, constantInt(i)).cast(int.class)));
            assignNegative.append(thisVariable.setField(inputChannelField, constantInt(-1)));
        }
        body.append(new IfStatement("if(%s.isEmpty())", inputChannels.getName()).condition(new BytecodeBlock().append(inputChannels).invokeInterface(List.class, "isEmpty", boolean.class)).ifTrue(assignNegative).ifFalse(assignFromList));
    }
    String createState;
    if (grouped) {
        createState = "createGroupedState";
    } else {
        createState = "createSingleState";
    }
    for (StateFieldAndDescriptor stateFieldAndDescriptor : stateFieldAndDescriptors) {
        FieldDefinition stateField = stateFieldAndDescriptor.getStateField();
        BytecodeExpression stateFactory = thisVariable.getField(stateFieldAndDescriptor.getStateFactoryField());
        body.append(thisVariable.setField(stateField, stateFactory.invoke(createState, Object.class).cast(stateField.getType())));
    }
    body.ret();
}
Also used : Variable(com.facebook.presto.bytecode.Variable) AccumulatorStateDescriptor(com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) Optional(java.util.Optional) CompilerOperations(com.facebook.presto.sql.gen.CompilerOperations) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) BytecodeExpressions.constantString(com.facebook.presto.bytecode.expression.BytecodeExpressions.constantString) IfStatement(com.facebook.presto.bytecode.control.IfStatement) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Aggregations

Variable (com.facebook.presto.bytecode.Variable)131 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)104 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)91 Parameter (com.facebook.presto.bytecode.Parameter)75 Scope (com.facebook.presto.bytecode.Scope)68 IfStatement (com.facebook.presto.bytecode.control.IfStatement)68 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)40 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)36 ImmutableList (com.google.common.collect.ImmutableList)30 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)28 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)21 ForLoop (com.facebook.presto.bytecode.control.ForLoop)21 Block (com.facebook.presto.spi.block.Block)19 Block (com.facebook.presto.common.block.Block)18 List (java.util.List)17 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)16 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)15 Type (com.facebook.presto.common.type.Type)15 RowExpression (com.facebook.presto.spi.relation.RowExpression)15 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)14