use of com.facebook.presto.bytecode.Parameter in project presto by prestodb.
the class AccumulatorCompiler method generateGroupedEvaluateIntermediate.
private static void generateGroupedEvaluateIntermediate(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors) {
Parameter groupId = arg("groupId", int.class);
Parameter out = arg("out", BlockBuilder.class);
MethodDefinition method = definition.declareMethod(a(PUBLIC), "evaluateIntermediate", type(void.class), groupId, out);
Variable thisVariable = method.getThis();
BytecodeBlock body = method.getBody();
if (stateFieldAndDescriptors.size() == 1) {
BytecodeExpression stateSerializer = thisVariable.getField(getOnlyElement(stateFieldAndDescriptors).getStateSerializerField());
BytecodeExpression state = thisVariable.getField(getOnlyElement(stateFieldAndDescriptors).getStateField());
body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class))).append(stateSerializer.invoke("serialize", void.class, state.cast(Object.class), out)).ret();
} else {
Variable rowBuilder = method.getScope().declareVariable(BlockBuilder.class, "rowBuilder");
body.append(rowBuilder.set(out.invoke("beginBlockEntry", BlockBuilder.class)));
for (int i = 0; i < stateFieldAndDescriptors.size(); i++) {
BytecodeExpression stateSerializer = thisVariable.getField(stateFieldAndDescriptors.get(i).getStateSerializerField());
BytecodeExpression state = thisVariable.getField(stateFieldAndDescriptors.get(i).getStateField());
body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class))).append(stateSerializer.invoke("serialize", void.class, state.cast(Object.class), rowBuilder));
}
body.append(out.invoke("closeEntry", BlockBuilder.class).pop()).ret();
}
}
use of com.facebook.presto.bytecode.Parameter in project presto by prestodb.
the class AccumulatorCompiler method generateAddInputWindowIndex.
private static void generateAddInputWindowIndex(ClassDefinition definition, List<FieldDefinition> stateField, List<ParameterMetadata> parameterMetadatas, List<Class> lambdaInterfaces, List<FieldDefinition> lambdaProviderFields, MethodHandle inputFunction, CallSiteBinder callSiteBinder) {
// TODO: implement masking based on maskChannel field once Window Functions support DISTINCT arguments to the functions.
Parameter index = arg("index", WindowIndex.class);
Parameter channels = arg("channels", type(List.class, Integer.class));
Parameter startPosition = arg("startPosition", int.class);
Parameter endPosition = arg("endPosition", int.class);
MethodDefinition method = definition.declareMethod(a(PUBLIC), "addInput", type(void.class), ImmutableList.of(index, channels, startPosition, endPosition));
Scope scope = method.getScope();
Variable position = scope.declareVariable(int.class, "position");
Binding binding = callSiteBinder.bind(inputFunction);
BytecodeExpression invokeInputFunction = invokeDynamic(BOOTSTRAP_METHOD, ImmutableList.of(binding.getBindingId()), "input", binding.getType(), getInvokeFunctionOnWindowIndexParameters(scope, inputFunction.type().parameterArray(), parameterMetadatas, lambdaInterfaces, lambdaProviderFields, stateField, index, channels, position));
method.getBody().append(new ForLoop().initialize(position.set(startPosition)).condition(BytecodeExpressions.lessThanOrEqual(position, endPosition)).update(position.increment()).body(new IfStatement().condition(anyParametersAreNull(parameterMetadatas, index, channels, position)).ifFalse(invokeInputFunction))).ret();
}
use of com.facebook.presto.bytecode.Parameter in project presto by prestodb.
the class CursorProcessorCompiler method generateProjectMethod.
private void generateProjectMethod(ClassDefinition classDefinition, RowExpressionCompiler compiler, String methodName, RowExpression projection) {
Parameter properties = arg("properties", SqlFunctionProperties.class);
Parameter cursor = arg("cursor", RecordCursor.class);
Parameter output = arg("output", BlockBuilder.class);
MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(void.class), properties, cursor, output);
method.comment("Projection: %s", projection.toString());
Scope scope = method.getScope();
Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull");
method.getBody().comment("boolean wasNull = false;").putVariable(wasNullVariable, false).comment("evaluate projection: " + projection.toString()).append(compiler.compile(projection, scope, Optional.of(output))).ret();
}
use of com.facebook.presto.bytecode.Parameter in project presto by prestodb.
the class CursorProcessorCompiler method generateCommonSubExpressionMethods.
private List<MethodDefinition> generateCommonSubExpressionMethods(ClassDefinition classDefinition, RowExpressionCompiler compiler, Map<Integer, Map<RowExpression, VariableReferenceExpression>> commonSubExpressionsByLevel, Map<VariableReferenceExpression, CommonSubExpressionFields> commonSubExpressionFieldsMap) {
Parameter properties = arg("properties", SqlFunctionProperties.class);
Parameter cursor = arg("cursor", RecordCursor.class);
ImmutableList.Builder<MethodDefinition> methods = ImmutableList.builder();
Map<VariableReferenceExpression, CommonSubExpressionFields> cseMap = new HashMap<>();
int startLevel = commonSubExpressionsByLevel.keySet().stream().reduce(Math::min).get();
int maxLevel = commonSubExpressionsByLevel.keySet().stream().reduce(Math::max).get();
for (int i = startLevel; i <= maxLevel; i++) {
if (commonSubExpressionsByLevel.containsKey(i)) {
for (Map.Entry<RowExpression, VariableReferenceExpression> entry : commonSubExpressionsByLevel.get(i).entrySet()) {
RowExpression cse = entry.getKey();
Class<?> type = Primitives.wrap(cse.getType().getJavaType());
VariableReferenceExpression cseVariable = entry.getValue();
CommonSubExpressionFields cseFields = commonSubExpressionFieldsMap.get(cseVariable);
MethodDefinition method = classDefinition.declareMethod(a(PRIVATE), "get" + cseVariable.getName(), type(cseFields.getResultType()), properties, cursor);
method.comment("cse: %s", cse);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable thisVariable = method.getThis();
scope.declareVariable("wasNull", body, constantFalse());
IfStatement ifStatement = new IfStatement().condition(thisVariable.getField(cseFields.getEvaluatedField())).ifFalse(new BytecodeBlock().append(thisVariable).append(compiler.compile(cse, scope, Optional.empty())).append(boxPrimitiveIfNecessary(scope, type)).putField(cseFields.getResultField()).append(thisVariable.setField(cseFields.getEvaluatedField(), constantBoolean(true))));
body.append(ifStatement).append(thisVariable).getField(cseFields.getResultField()).retObject();
methods.add(method);
cseMap.put(cseVariable, cseFields);
}
}
}
return methods.build();
}
use of com.facebook.presto.bytecode.Parameter in project presto by prestodb.
the class CursorProcessorCompiler method generateFilterMethod.
private void generateFilterMethod(ClassDefinition classDefinition, RowExpressionCompiler compiler, RowExpression filter) {
Parameter properties = arg("properties", SqlFunctionProperties.class);
Parameter cursor = arg("cursor", RecordCursor.class);
MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "filter", type(boolean.class), properties, cursor);
method.comment("Filter: %s", filter);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull");
LabelNode end = new LabelNode("end");
body.comment("boolean wasNull = false;").putVariable(wasNullVariable, false).comment("evaluate filter: " + filter).append(compiler.compile(filter, scope, Optional.empty())).comment("if (wasNull) return false;").getVariable(wasNullVariable).ifFalseGoto(end).pop(boolean.class).push(false).visitLabel(end).retBoolean();
}
Aggregations