Search in sources :

Example 11 with LambdaDefinitionExpression

use of io.trino.sql.relational.LambdaDefinitionExpression in project trino by trinodb.

the class LambdaBytecodeGenerator method compileLambdaProvider.

public static Class<? extends Supplier<Object>> compileLambdaProvider(LambdaDefinitionExpression lambdaExpression, FunctionManager functionManager, Class<?> lambdaInterface) {
    ClassDefinition lambdaProviderClassDefinition = new ClassDefinition(a(PUBLIC, Access.FINAL), makeClassName("LambdaProvider"), type(Object.class), type(Supplier.class, Object.class));
    FieldDefinition sessionField = lambdaProviderClassDefinition.declareField(a(PRIVATE), "session", ConnectorSession.class);
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(lambdaProviderClassDefinition, callSiteBinder);
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(lambdaProviderClassDefinition, callSiteBinder, cachedInstanceBinder, lambdaExpression, functionManager);
    MethodDefinition method = lambdaProviderClassDefinition.declareMethod(a(PUBLIC), "get", type(Object.class), ImmutableList.of());
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    scope.declareVariable("wasNull", body, constantFalse());
    scope.declareVariable("session", body, method.getThis().getField(sessionField));
    RowExpressionCompiler rowExpressionCompiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, variableReferenceCompiler(ImmutableMap.of()), functionManager, compiledLambdaMap);
    BytecodeGeneratorContext generatorContext = new BytecodeGeneratorContext(rowExpressionCompiler, scope, callSiteBinder, cachedInstanceBinder, functionManager);
    body.append(generateLambda(generatorContext, ImmutableList.of(), compiledLambdaMap.get(lambdaExpression), lambdaInterface)).retObject();
    // constructor
    Parameter sessionParameter = arg("session", ConnectorSession.class);
    MethodDefinition constructorDefinition = lambdaProviderClassDefinition.declareConstructor(a(PUBLIC), sessionParameter);
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable constructorThisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(constructorThisVariable).invokeConstructor(Object.class).append(constructorThisVariable.setField(sessionField, sessionParameter));
    cachedInstanceBinder.generateInitializations(constructorThisVariable, constructorBody);
    constructorBody.ret();
    // noinspection unchecked
    return (Class<? extends Supplier<Object>>) defineClass(lambdaProviderClassDefinition, Supplier.class, callSiteBinder.getBindings(), AccumulatorCompiler.class.getClassLoader());
}
Also used : Variable(io.airlift.bytecode.Variable) FieldDefinition(io.airlift.bytecode.FieldDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter) Supplier(java.util.function.Supplier) CompilerUtils.defineClass(io.trino.util.CompilerUtils.defineClass) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Aggregations

LambdaDefinitionExpression (io.trino.sql.relational.LambdaDefinitionExpression)11 CompiledLambda (io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda)7 Variable (io.airlift.bytecode.Variable)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)4 FieldDefinition (io.airlift.bytecode.FieldDefinition)4 ClassDefinition (io.airlift.bytecode.ClassDefinition)3 MethodDefinition (io.airlift.bytecode.MethodDefinition)3 Parameter (io.airlift.bytecode.Parameter)3 Scope (io.airlift.bytecode.Scope)3 CallExpression (io.trino.sql.relational.CallExpression)2 ConstantExpression (io.trino.sql.relational.ConstantExpression)2 InputReferenceExpression (io.trino.sql.relational.InputReferenceExpression)2 RowExpressionVisitor (io.trino.sql.relational.RowExpressionVisitor)2 SpecialForm (io.trino.sql.relational.SpecialForm)2 VariableReferenceExpression (io.trino.sql.relational.VariableReferenceExpression)2 IfStatement (io.airlift.bytecode.control.IfStatement)1 Slice (io.airlift.slice.Slice)1 Work (io.trino.operator.Work)1 InputChannels (io.trino.operator.project.InputChannels)1