use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class AccumulatorCompiler method generateBlockNonNullPositionForLoop.
// Generates a for-loop with a local variable named "position" defined, with the current position in the block,
// loopBody will only be executed for non-null positions in the Block
private static BytecodeBlock generateBlockNonNullPositionForLoop(Scope scope, Variable positionVariable, BytecodeBlock loopBody) {
Variable rowsVariable = scope.declareVariable(int.class, "rows");
Variable blockVariable = scope.getVariable("block");
BytecodeBlock block = new BytecodeBlock().append(blockVariable).invokeInterface(Block.class, "getPositionCount", int.class).putVariable(rowsVariable);
IfStatement ifStatement = new IfStatement("if(!block.isNull(position))").condition(new BytecodeBlock().append(blockVariable).append(positionVariable).invokeInterface(Block.class, "isNull", boolean.class, int.class)).ifFalse(loopBody);
block.append(new ForLoop().initialize(positionVariable.set(constantInt(0))).condition(new BytecodeBlock().append(positionVariable).append(rowsVariable).invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class)).update(new BytecodeBlock().incrementVariable(positionVariable, (byte) 1)).body(ifStatement));
return block;
}
use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class AccumulatorCompiler method generateAddInput.
private static void generateAddInput(ClassDefinition definition, FieldDefinition stateField, FieldDefinition inputChannelsField, FieldDefinition maskChannelField, List<ParameterMetadata> parameterMetadatas, MethodHandle inputFunction, CallSiteBinder callSiteBinder, boolean grouped) {
ImmutableList.Builder<Parameter> parameters = ImmutableList.builder();
if (grouped) {
parameters.add(arg("groupIdsBlock", GroupByIdBlock.class));
}
Parameter page = arg("page", Page.class);
parameters.add(page);
MethodDefinition method = definition.declareMethod(a(PUBLIC), "addInput", type(void.class), parameters.build());
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable thisVariable = method.getThis();
if (grouped) {
generateEnsureCapacity(scope, stateField, body);
}
List<Variable> parameterVariables = new ArrayList<>();
for (int i = 0; i < countInputChannels(parameterMetadatas); i++) {
parameterVariables.add(scope.declareVariable(Block.class, "block" + i));
}
Variable masksBlock = scope.declareVariable(Block.class, "masksBlock");
body.comment("masksBlock = maskChannel.map(page.blockGetter()).orElse(null);").append(thisVariable.getField(maskChannelField)).append(page).invokeStatic(type(AggregationUtils.class), "pageBlockGetter", type(Function.class, Integer.class, Block.class), type(Page.class)).invokeVirtual(Optional.class, "map", Optional.class, Function.class).pushNull().invokeVirtual(Optional.class, "orElse", Object.class, Object.class).checkCast(Block.class).putVariable(masksBlock);
// Get all parameter blocks
for (int i = 0; i < countInputChannels(parameterMetadatas); i++) {
body.comment("%s = page.getBlock(inputChannels.get(%d));", parameterVariables.get(i).getName(), i).append(page).append(thisVariable.getField(inputChannelsField)).push(i).invokeInterface(List.class, "get", Object.class, int.class).checkCast(Integer.class).invokeVirtual(Integer.class, "intValue", int.class).invokeVirtual(Page.class, "getBlock", Block.class, int.class).putVariable(parameterVariables.get(i));
}
BytecodeBlock block = generateInputForLoop(stateField, parameterMetadatas, inputFunction, scope, parameterVariables, masksBlock, callSiteBinder, grouped);
body.append(block);
body.ret();
}
use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class AccumulatorCompiler method generateGroupedEvaluateFinal.
private static void generateGroupedEvaluateFinal(ClassDefinition definition, FieldDefinition stateField, 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();
BytecodeExpression state = thisVariable.getField(stateField);
body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class)));
body.comment("output(state, out)");
body.append(state);
body.append(out);
body.append(invoke(callSiteBinder.bind(outputFunction), "output"));
body.ret();
}
use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class AccumulatorCompiler method generateEvaluateFinal.
private static void generateEvaluateFinal(ClassDefinition definition, FieldDefinition stateField, MethodHandle outputFunction, CallSiteBinder callSiteBinder) {
Parameter out = arg("out", BlockBuilder.class);
MethodDefinition method = definition.declareMethod(a(PUBLIC), "evaluateFinal", type(void.class), out);
BytecodeBlock body = method.getBody();
Variable thisVariable = method.getThis();
BytecodeExpression state = thisVariable.getField(stateField);
body.comment("output(state, out)");
body.append(state);
body.append(out);
body.append(invoke(callSiteBinder.bind(outputFunction), "output"));
body.ret();
}
use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class ConcatFunction method generateConcat.
private static Class<?> generateConcat(int arity) {
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), CompilerUtils.makeClassName("Concat" + arity + "ScalarFunction"), type(Object.class));
// Generate constructor
definition.declareDefaultConstructor(a(PRIVATE));
// Generate concat()
List<Parameter> parameters = IntStream.range(0, arity).mapToObj(i -> arg("arg" + i, Slice.class)).collect(toImmutableList());
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "concat", type(Slice.class), parameters);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable length = scope.declareVariable(int.class, "length");
body.append(length.set(constantInt(0)));
for (int i = 0; i < arity; ++i) {
body.append(length.set(generateCheckedAdd(length, parameters.get(i).invoke("length", int.class))));
}
Variable result = scope.declareVariable(Slice.class, "result");
body.append(result.set(invokeStatic(Slices.class, "allocate", Slice.class, length)));
Variable position = scope.declareVariable(int.class, "position");
body.append(position.set(constantInt(0)));
for (int i = 0; i < arity; ++i) {
body.append(result.invoke("setBytes", void.class, position, parameters.get(i)));
body.append(position.set(add(position, parameters.get(i).invoke("length", int.class))));
}
body.getVariable(result).retObject();
return defineClass(definition, Object.class, ImmutableMap.of(), new DynamicClassLoader(ConcatFunction.class.getClassLoader()));
}
Aggregations