Search in sources :

Example 1 with ParameterizedType

use of com.facebook.presto.bytecode.ParameterizedType in project presto by prestodb.

the class LambdaBytecodeGenerator method generateLambda.

public static BytecodeNode generateLambda(BytecodeGeneratorContext context, List<RowExpression> captureExpressions, CompiledLambda compiledLambda, Class lambdaInterface) {
    if (!lambdaInterface.isAnnotationPresent(FunctionalInterface.class)) {
        // lambdaInterface is checked to be annotated with FunctionalInterface when generating ScalarFunctionImplementation
        throw new VerifyException("lambda should be generated as class annotated with FunctionalInterface");
    }
    BytecodeBlock block = new BytecodeBlock().setDescription("Partial apply");
    Scope scope = context.getScope();
    Variable wasNull = scope.getVariable("wasNull");
    // generate values to be captured
    ImmutableList.Builder<BytecodeExpression> captureVariableBuilder = ImmutableList.builder();
    for (RowExpression captureExpression : captureExpressions) {
        Class<?> valueType = Primitives.wrap(captureExpression.getType().getJavaType());
        Variable valueVariable = scope.createTempVariable(valueType);
        block.append(context.generate(captureExpression, Optional.empty()));
        block.append(boxPrimitiveIfNecessary(scope, valueType));
        block.putVariable(valueVariable);
        block.append(wasNull.set(constantFalse()));
        captureVariableBuilder.add(valueVariable);
    }
    List<BytecodeExpression> captureVariables = ImmutableList.<BytecodeExpression>builder().add(scope.getThis(), scope.getVariable("properties")).addAll(captureVariableBuilder.build()).build();
    Type instantiatedMethodAsmType = getMethodType(compiledLambda.getReturnType().getAsmType(), compiledLambda.getParameterTypes().stream().skip(// skip capture variables and ConnectorSession
    captureExpressions.size() + 1).map(ParameterizedType::getAsmType).collect(toImmutableList()).toArray(new Type[0]));
    block.append(invokeDynamic(LAMBDA_CAPTURE_METHOD, ImmutableList.of(getType(getSingleApplyMethod(lambdaInterface)), compiledLambda.getLambdaAsmHandle(), instantiatedMethodAsmType), "apply", type(lambdaInterface), captureVariables));
    return block;
}
Also used : Type(org.objectweb.asm.Type) Type.getType(org.objectweb.asm.Type.getType) Type.getMethodType(org.objectweb.asm.Type.getMethodType) ParameterizedType(com.facebook.presto.bytecode.ParameterizedType) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) VerifyException(com.google.common.base.VerifyException) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) RowExpression(com.facebook.presto.spi.relation.RowExpression) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Aggregations

BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)1 ParameterizedType (com.facebook.presto.bytecode.ParameterizedType)1 Scope (com.facebook.presto.bytecode.Scope)1 Variable (com.facebook.presto.bytecode.Variable)1 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)1 RowExpression (com.facebook.presto.spi.relation.RowExpression)1 VerifyException (com.google.common.base.VerifyException)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Type (org.objectweb.asm.Type)1 Type.getMethodType (org.objectweb.asm.Type.getMethodType)1 Type.getType (org.objectweb.asm.Type.getType)1