Search in sources :

Example 26 with FieldDefinition

use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.

the class EmbedVersion method implementRunnable.

private static void implementRunnable(ClassDefinition classDefinition) {
    FieldDefinition field = classDefinition.declareField(a(PRIVATE), "runnable", Runnable.class);
    Parameter parameter = arg("runnable", type(Runnable.class));
    MethodDefinition constructor = classDefinition.declareConstructor(a(PUBLIC), parameter);
    constructor.getBody().comment("super();").append(constructor.getThis()).invokeConstructor(Object.class).append(constructor.getThis()).append(parameter).putField(field).ret();
    MethodDefinition run = classDefinition.declareMethod(a(PUBLIC), "run", type(void.class));
    run.getBody().comment("runnable.run();").append(run.getThis()).getField(field).invokeInterface(Runnable.class, "run", void.class).ret();
}
Also used : MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) Parameter(io.airlift.bytecode.Parameter)

Example 27 with FieldDefinition

use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.

the class AccumulatorCompiler method generateConstructor.

private static void generateConstructor(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors, FieldDefinition inputChannelsField, 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(inputChannelsField, generateRequireNotNull(inputChannels)));
    body.append(thisVariable.setField(maskChannelField, generateRequireNotNull(maskChannel)));
    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(io.airlift.bytecode.Variable) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) Optional(java.util.Optional) FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) BytecodeExpressions.constantString(io.airlift.bytecode.expression.BytecodeExpressions.constantString) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.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(io.airlift.bytecode.expression.BytecodeExpression)

Example 28 with FieldDefinition

use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.

the class AccumulatorCompiler method generateAddIntermediateAsCombine.

private static void generateAddIntermediateAsCombine(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors, List<Class<?>> lambdaInterfaces, List<FieldDefinition> lambdaProviderFields, MethodHandle combineFunction, CallSiteBinder callSiteBinder, boolean grouped) {
    MethodDefinition method = declareAddIntermediate(definition, grouped);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    int stateCount = stateFieldAndDescriptors.size();
    List<Variable> scratchStates = new ArrayList<>();
    for (int i = 0; i < stateCount; i++) {
        Class<?> scratchStateClass = stateFieldAndDescriptors.get(i).getStateDescriptor().getFactory().getSingleStateClass();
        scratchStates.add(scope.declareVariable(scratchStateClass, "scratchState_" + i));
    }
    List<Variable> block;
    if (stateCount == 1) {
        block = ImmutableList.of(scope.getVariable("block"));
    } else {
        // ColumnarRow is used to get the column blocks represents each state, this allows to
        // 1. handle single state and multiple states in a unified way
        // 2. avoid the cost of constructing SingleRowBlock for each group
        Variable columnarRow = scope.declareVariable(ColumnarRow.class, "columnarRow");
        body.append(columnarRow.set(invokeStatic(ColumnarRow.class, "toColumnarRow", ColumnarRow.class, scope.getVariable("block"))));
        block = new ArrayList<>();
        for (int i = 0; i < stateCount; i++) {
            Variable columnBlock = scope.declareVariable(Block.class, "columnBlock_" + i);
            body.append(columnBlock.set(columnarRow.invoke("getField", Block.class, constantInt(i))));
            block.add(columnBlock);
        }
    }
    Variable position = scope.declareVariable(int.class, "position");
    for (int i = 0; i < stateCount; i++) {
        FieldDefinition stateFactoryField = stateFieldAndDescriptors.get(i).getStateFactoryField();
        body.comment(format("scratchState_%s = stateFactory[%s].createSingleState();", i, i)).append(thisVariable.getField(stateFactoryField)).invokeInterface(AccumulatorStateFactory.class, "createSingleState", Object.class).checkCast(scratchStates.get(i).getType()).putVariable(scratchStates.get(i));
    }
    List<FieldDefinition> stateFields = stateFieldAndDescriptors.stream().map(StateFieldAndDescriptor::getStateField).collect(toImmutableList());
    if (grouped) {
        generateEnsureCapacity(scope, stateFields, body);
    }
    BytecodeBlock loopBody = new BytecodeBlock();
    loopBody.comment("combine(state_0, state_1, ... scratchState_0, scratchState_1, ... lambda_0, lambda_1, ...)");
    for (FieldDefinition stateField : stateFields) {
        if (grouped) {
            Variable groupIdsBlock = scope.getVariable("groupIdsBlock");
            loopBody.append(thisVariable.getField(stateField).invoke("setGroupId", void.class, groupIdsBlock.invoke("getGroupId", long.class, position)));
        }
        loopBody.append(thisVariable.getField(stateField));
    }
    for (int i = 0; i < stateCount; i++) {
        FieldDefinition stateSerializerField = stateFieldAndDescriptors.get(i).getStateSerializerField();
        loopBody.append(thisVariable.getField(stateSerializerField).invoke("deserialize", void.class, block.get(i), position, scratchStates.get(i).cast(Object.class)));
        loopBody.append(scratchStates.get(i));
    }
    for (int i = 0; i < lambdaInterfaces.size(); i++) {
        loopBody.append(scope.getThis().getField(lambdaProviderFields.get(i)).invoke("getLambda", Object.class).cast(lambdaInterfaces.get(i)));
    }
    loopBody.append(invoke(callSiteBinder.bind(combineFunction), "combine"));
    if (grouped) {
        // skip rows with null group id
        IfStatement ifStatement = new IfStatement("if (!groupIdsBlock.isNull(position))").condition(not(scope.getVariable("groupIdsBlock").invoke("isNull", boolean.class, position))).ifTrue(loopBody);
        loopBody = new BytecodeBlock().append(ifStatement);
    }
    body.append(generateBlockNonNullPositionForLoop(scope, position, loopBody)).ret();
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ArrayList(java.util.ArrayList)

Example 29 with FieldDefinition

use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.

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(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ArrayList(java.util.ArrayList) Parameter(io.airlift.bytecode.Parameter) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 30 with FieldDefinition

use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.

the class AccumulatorCompiler method generateSetGroupIdFromGroupIdsBlock.

private static void generateSetGroupIdFromGroupIdsBlock(Scope scope, List<FieldDefinition> stateFields, BytecodeBlock block) {
    Variable groupIdsBlock = scope.getVariable("groupIdsBlock");
    Variable position = scope.getVariable("position");
    for (FieldDefinition stateField : stateFields) {
        BytecodeExpression state = scope.getThis().getField(stateField);
        block.append(state.invoke("setGroupId", void.class, groupIdsBlock.invoke("getGroupId", long.class, position)));
    }
}
Also used : Variable(io.airlift.bytecode.Variable) FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Aggregations

FieldDefinition (io.airlift.bytecode.FieldDefinition)50 MethodDefinition (io.airlift.bytecode.MethodDefinition)27 Variable (io.airlift.bytecode.Variable)25 Parameter (io.airlift.bytecode.Parameter)20 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)19 ArrayList (java.util.ArrayList)17 ClassDefinition (io.airlift.bytecode.ClassDefinition)15 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)14 Scope (io.airlift.bytecode.Scope)6 IfStatement (io.airlift.bytecode.control.IfStatement)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 LambdaDefinitionExpression (io.prestosql.spi.relation.LambdaDefinitionExpression)4 AccumulatorState (io.trino.spi.function.AccumulatorState)4 LambdaDefinitionExpression (io.trino.sql.relational.LambdaDefinitionExpression)4 List (java.util.List)4 CompiledLambda (io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda)3 GroupedAccumulatorState (io.trino.spi.function.GroupedAccumulatorState)3 CompiledLambda (io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda)3 CompilerUtils.defineClass (io.trino.util.CompilerUtils.defineClass)3