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)));
}
}
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);
}
}
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;
}
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();
}
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);
}
Aggregations