Search in sources :

Example 1 with CompiledLambda

use of io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda in project trino by trinodb.

the class CursorProcessorCompiler method generateMethods.

@Override
public void generateMethods(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, RowExpression filter, List<RowExpression> projections) {
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
    generateProcessMethod(classDefinition, projections.size());
    Map<LambdaDefinitionExpression, CompiledLambda> filterCompiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, filter, "filter");
    generateFilterMethod(classDefinition, callSiteBinder, cachedInstanceBinder, filterCompiledLambdaMap, filter);
    for (int i = 0; i < projections.size(); i++) {
        String methodName = "project_" + i;
        Map<LambdaDefinitionExpression, CompiledLambda> projectCompiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, projections.get(i), methodName);
        generateProjectMethod(classDefinition, callSiteBinder, cachedInstanceBinder, projectCompiledLambdaMap, methodName, projections.get(i));
    }
    MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC));
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable thisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(thisVariable).invokeConstructor(Object.class);
    cachedInstanceBinder.generateInitializations(thisVariable, constructorBody);
    constructorBody.ret();
}
Also used : CompiledLambda(io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda) Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Example 2 with CompiledLambda

use of io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda in project trino by trinodb.

the class PageFunctionCompiler method generateMethodsForLambda.

private Map<LambdaDefinitionExpression, CompiledLambda> generateMethodsForLambda(ClassDefinition containerClassDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, RowExpression expression) {
    Set<LambdaDefinitionExpression> lambdaExpressions = ImmutableSet.copyOf(extractLambdaExpressions(expression));
    ImmutableMap.Builder<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = ImmutableMap.builder();
    int counter = 0;
    for (LambdaDefinitionExpression lambdaExpression : lambdaExpressions) {
        CompiledLambda compiledLambda = LambdaBytecodeGenerator.preGenerateLambdaExpression(lambdaExpression, "lambda_" + counter, containerClassDefinition, compiledLambdaMap.buildOrThrow(), callSiteBinder, cachedInstanceBinder, functionManager);
        compiledLambdaMap.put(lambdaExpression, compiledLambda);
        counter++;
    }
    return compiledLambdaMap.buildOrThrow();
}
Also used : CompiledLambda(io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda) ImmutableMap(com.google.common.collect.ImmutableMap) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Example 3 with CompiledLambda

use of io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda in project trino by trinodb.

the class PageFunctionCompiler method definePageProjectWorkClass.

private ClassDefinition definePageProjectWorkClass(RowExpression projection, CallSiteBinder callSiteBinder, Optional<String> classNameSuffix) {
    ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), generateProjectionWorkClassName(classNameSuffix), type(Object.class), type(Work.class));
    FieldDefinition blockBuilderField = classDefinition.declareField(a(PRIVATE), "blockBuilder", BlockBuilder.class);
    FieldDefinition sessionField = classDefinition.declareField(a(PRIVATE), "session", ConnectorSession.class);
    FieldDefinition pageField = classDefinition.declareField(a(PRIVATE), "page", Page.class);
    FieldDefinition selectedPositionsField = classDefinition.declareField(a(PRIVATE), "selectedPositions", SelectedPositions.class);
    FieldDefinition nextIndexOrPositionField = classDefinition.declareField(a(PRIVATE), "nextIndexOrPosition", int.class);
    FieldDefinition resultField = classDefinition.declareField(a(PRIVATE), "result", Block.class);
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
    // process
    generateProcessMethod(classDefinition, blockBuilderField, sessionField, pageField, selectedPositionsField, nextIndexOrPositionField, resultField);
    // getResult
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "getResult", type(Object.class), ImmutableList.of());
    method.getBody().append(method.getThis().getField(resultField)).ret(Object.class);
    // evaluate
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, projection);
    generateEvaluateMethod(classDefinition, callSiteBinder, cachedInstanceBinder, compiledLambdaMap, projection, blockBuilderField);
    // constructor
    Parameter blockBuilder = arg("blockBuilder", BlockBuilder.class);
    Parameter session = arg("session", ConnectorSession.class);
    Parameter page = arg("page", Page.class);
    Parameter selectedPositions = arg("selectedPositions", SelectedPositions.class);
    MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC), blockBuilder, session, page, selectedPositions);
    BytecodeBlock body = constructorDefinition.getBody();
    Variable thisVariable = constructorDefinition.getThis();
    body.comment("super();").append(thisVariable).invokeConstructor(Object.class).append(thisVariable.setField(blockBuilderField, blockBuilder)).append(thisVariable.setField(sessionField, session)).append(thisVariable.setField(pageField, page)).append(thisVariable.setField(selectedPositionsField, selectedPositions)).append(thisVariable.setField(nextIndexOrPositionField, selectedPositions.invoke("getOffset", int.class))).append(thisVariable.setField(resultField, constantNull(Block.class)));
    cachedInstanceBinder.generateInitializations(thisVariable, body);
    body.ret();
    return classDefinition;
}
Also used : CompiledLambda(io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda) Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) Work(io.trino.operator.Work) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter) ClassDefinition(io.airlift.bytecode.ClassDefinition) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Example 4 with CompiledLambda

use of io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda in project trino by trinodb.

the class PageFunctionCompiler method defineFilterClass.

private ClassDefinition defineFilterClass(RowExpression filter, InputChannels inputChannels, CallSiteBinder callSiteBinder, Optional<String> classNameSuffix) {
    ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), generateFilterClassName(classNameSuffix), type(Object.class), type(PageFilter.class));
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
    Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, filter);
    generateFilterMethod(classDefinition, callSiteBinder, cachedInstanceBinder, compiledLambdaMap, filter);
    FieldDefinition selectedPositions = classDefinition.declareField(a(PRIVATE), "selectedPositions", boolean[].class);
    generatePageFilterMethod(classDefinition, selectedPositions);
    // isDeterministic
    classDefinition.declareMethod(a(PUBLIC), "isDeterministic", type(boolean.class)).getBody().append(constantBoolean(isDeterministic(filter))).retBoolean();
    // getInputChannels
    classDefinition.declareMethod(a(PUBLIC), "getInputChannels", type(InputChannels.class)).getBody().append(invoke(callSiteBinder.bind(inputChannels, InputChannels.class), "getInputChannels")).retObject();
    // toString
    String toStringResult = toStringHelper(classDefinition.getType().getJavaClassName()).add("filter", filter).toString();
    classDefinition.declareMethod(a(PUBLIC), "toString", type(String.class)).getBody().append(invoke(callSiteBinder.bind(toStringResult, String.class), "toString")).retObject();
    // constructor
    generateConstructor(classDefinition, cachedInstanceBinder, method -> {
        Variable thisVariable = method.getScope().getThis();
        method.getBody().append(thisVariable.setField(selectedPositions, newArray(type(boolean[].class), 0)));
    });
    return classDefinition;
}
Also used : CompiledLambda(io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda) Variable(io.airlift.bytecode.Variable) FieldDefinition(io.airlift.bytecode.FieldDefinition) PageFilter(io.trino.operator.project.PageFilter) InputChannels(io.trino.operator.project.InputChannels) ClassDefinition(io.airlift.bytecode.ClassDefinition) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Example 5 with CompiledLambda

use of io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda in project trino by trinodb.

the class CursorProcessorCompiler method generateMethodsForLambda.

private Map<LambdaDefinitionExpression, CompiledLambda> generateMethodsForLambda(ClassDefinition containerClassDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, RowExpression projection, String methodPrefix) {
    Set<LambdaDefinitionExpression> lambdaExpressions = ImmutableSet.copyOf(extractLambdaExpressions(projection));
    ImmutableMap.Builder<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = ImmutableMap.builder();
    int counter = 0;
    for (LambdaDefinitionExpression lambdaExpression : lambdaExpressions) {
        String methodName = methodPrefix + "_lambda_" + counter;
        CompiledLambda compiledLambda = LambdaBytecodeGenerator.preGenerateLambdaExpression(lambdaExpression, methodName, containerClassDefinition, compiledLambdaMap.buildOrThrow(), callSiteBinder, cachedInstanceBinder, functionManager);
        compiledLambdaMap.put(lambdaExpression, compiledLambda);
        counter++;
    }
    return compiledLambdaMap.buildOrThrow();
}
Also used : CompiledLambda(io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda) ImmutableMap(com.google.common.collect.ImmutableMap) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Aggregations

CompiledLambda (io.trino.sql.gen.LambdaBytecodeGenerator.CompiledLambda)7 LambdaDefinitionExpression (io.trino.sql.relational.LambdaDefinitionExpression)7 ImmutableMap (com.google.common.collect.ImmutableMap)3 FieldDefinition (io.airlift.bytecode.FieldDefinition)3 Variable (io.airlift.bytecode.Variable)3 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)2 ClassDefinition (io.airlift.bytecode.ClassDefinition)2 MethodDefinition (io.airlift.bytecode.MethodDefinition)2 Parameter (io.airlift.bytecode.Parameter)1 Work (io.trino.operator.Work)1 InputChannels (io.trino.operator.project.InputChannels)1 PageFilter (io.trino.operator.project.PageFilter)1