Search in sources :

Example 21 with FieldDefinition

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

the class AccumulatorCompiler method generateEnsureCapacity.

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

Example 22 with FieldDefinition

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

the class AccumulatorCompiler method generateWindowAccumulatorClass.

public static Constructor<? extends WindowAccumulator> generateWindowAccumulatorClass(BoundSignature boundSignature, AggregationMetadata metadata, FunctionNullability functionNullability) {
    DynamicClassLoader classLoader = new DynamicClassLoader(AccumulatorCompiler.class.getClassLoader());
    List<Boolean> argumentNullable = functionNullability.getArgumentNullable().subList(0, functionNullability.getArgumentNullable().size() - metadata.getLambdaInterfaces().size());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(boundSignature.getName() + WindowAccumulator.class.getSimpleName()), type(Object.class), type(WindowAccumulator.class));
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    List<AccumulatorStateDescriptor<?>> stateDescriptors = metadata.getAccumulatorStateDescriptors();
    List<StateFieldAndDescriptor> stateFieldAndDescriptors = new ArrayList<>();
    for (int i = 0; i < stateDescriptors.size(); i++) {
        stateFieldAndDescriptors.add(new StateFieldAndDescriptor(stateDescriptors.get(i), definition.declareField(a(PRIVATE, FINAL), "stateSerializer_" + i, AccumulatorStateSerializer.class), definition.declareField(a(PRIVATE, FINAL), "stateFactory_" + i, AccumulatorStateFactory.class), definition.declareField(a(PRIVATE, FINAL), "state_" + i, AccumulatorState.class)));
    }
    List<FieldDefinition> stateFields = stateFieldAndDescriptors.stream().map(StateFieldAndDescriptor::getStateField).collect(toImmutableList());
    int lambdaCount = metadata.getLambdaInterfaces().size();
    List<FieldDefinition> lambdaProviderFields = new ArrayList<>(lambdaCount);
    for (int i = 0; i < lambdaCount; i++) {
        lambdaProviderFields.add(definition.declareField(a(PRIVATE, FINAL), "lambdaProvider_" + i, Supplier.class));
    }
    // Generate constructor
    generateWindowAccumulatorConstructor(definition, stateFieldAndDescriptors, lambdaProviderFields, callSiteBinder);
    generateCopyConstructor(definition, stateFieldAndDescriptors, lambdaProviderFields);
    // Generate methods
    generateCopy(definition, WindowAccumulator.class);
    generateAddOrRemoveInputWindowIndex(definition, stateFields, argumentNullable, lambdaProviderFields, metadata.getInputFunction(), "addInput", callSiteBinder);
    metadata.getRemoveInputFunction().ifPresent(removeInputFunction -> generateAddOrRemoveInputWindowIndex(definition, stateFields, argumentNullable, lambdaProviderFields, removeInputFunction, "removeInput", callSiteBinder));
    generateEvaluateFinal(definition, stateFields, metadata.getOutputFunction(), callSiteBinder);
    generateGetEstimatedSize(definition, stateFields);
    Class<? extends WindowAccumulator> windowAccumulatorClass = defineClass(definition, WindowAccumulator.class, callSiteBinder.getBindings(), classLoader);
    try {
        return windowAccumulatorClass.getConstructor(List.class);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) FieldDefinition(io.airlift.bytecode.FieldDefinition) ArrayList(java.util.ArrayList) ClassDefinition(io.airlift.bytecode.ClassDefinition) CallSiteBinder(io.trino.sql.gen.CallSiteBinder) Supplier(java.util.function.Supplier)

Example 23 with FieldDefinition

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

the class AccumulatorCompiler method generateInvokeInputFunction.

private static BytecodeBlock generateInvokeInputFunction(Scope scope, List<FieldDefinition> stateField, Variable position, List<Variable> parameterVariables, List<FieldDefinition> lambdaProviderFields, MethodHandle inputFunction, CallSiteBinder callSiteBinder, boolean grouped) {
    BytecodeBlock block = new BytecodeBlock();
    if (grouped) {
        generateSetGroupIdFromGroupIdsBlock(scope, stateField, block);
    }
    block.comment("Call input function with unpacked Block arguments");
    List<BytecodeExpression> parameters = new ArrayList<>();
    // state parameters
    for (FieldDefinition field : stateField) {
        parameters.add(scope.getThis().getField(field));
    }
    // input parameters
    parameters.addAll(parameterVariables);
    // position parameter
    if (!parameterVariables.isEmpty()) {
        parameters.add(position);
    }
    // lambda parameters
    for (FieldDefinition lambdaProviderField : lambdaProviderFields) {
        parameters.add(scope.getThis().getField(lambdaProviderField).invoke("get", Object.class));
    }
    block.append(invoke(callSiteBinder.bind(inputFunction), "input", parameters));
    return block;
}
Also used : FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ArrayList(java.util.ArrayList) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 24 with FieldDefinition

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

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 25 with FieldDefinition

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

the class EmbedVersion method implementCallable.

private static void implementCallable(ClassDefinition classDefinition) {
    FieldDefinition field = classDefinition.declareField(a(PRIVATE), "callable", Callable.class);
    Parameter parameter = arg("callable", type(Callable.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), "call", type(Object.class));
    run.getBody().comment("callable.call();").append(run.getThis()).getField(field).invokeInterface(Callable.class, "call", Object.class).ret(Object.class);
}
Also used : MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) Parameter(io.airlift.bytecode.Parameter) Callable(java.util.concurrent.Callable)

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