Search in sources :

Example 26 with Scope

use of io.airlift.bytecode.Scope in project trino by trinodb.

the class PageFunctionCompiler method generateEvaluateMethod.

private MethodDefinition generateEvaluateMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, RowExpression projection, FieldDefinition blockBuilder) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter page = arg("page", Page.class);
    Parameter position = arg("position", int.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "evaluate", type(void.class), ImmutableList.<Parameter>builder().add(session).add(page).add(position).build());
    method.comment("Projection: %s", projection);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    declareBlockVariables(projection, page, scope, body);
    Variable wasNullVariable = scope.declareVariable("wasNull", body, constantFalse());
    RowExpressionCompiler compiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(callSiteBinder), functionManager, compiledLambdaMap);
    body.append(thisVariable.getField(blockBuilder)).append(compiler.compile(projection, scope)).append(generateWrite(callSiteBinder, scope, wasNullVariable, projection.getType())).ret();
    return method;
}
Also used : Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter)

Example 27 with Scope

use of io.airlift.bytecode.Scope in project trino by trinodb.

the class PageFunctionCompiler method generateFilterMethod.

private MethodDefinition generateFilterMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, RowExpression filter) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter page = arg("page", Page.class);
    Parameter position = arg("position", int.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "filter", type(boolean.class), ImmutableList.<Parameter>builder().add(session).add(page).add(position).build());
    method.comment("Filter: %s", filter);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    declareBlockVariables(filter, page, scope, body);
    Variable wasNullVariable = scope.declareVariable("wasNull", body, constantFalse());
    RowExpressionCompiler compiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(callSiteBinder), functionManager, compiledLambdaMap);
    Variable result = scope.declareVariable(boolean.class, "result");
    body.append(compiler.compile(filter, scope)).putVariable(result).append(and(not(wasNullVariable), result).ret());
    return method;
}
Also used : Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter)

Example 28 with Scope

use of io.airlift.bytecode.Scope in project trino by trinodb.

the class RowConstructorCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(BytecodeGeneratorContext context) {
    BytecodeBlock block = new BytecodeBlock().setDescription("Constructor for " + rowType);
    CallSiteBinder binder = context.getCallSiteBinder();
    Scope scope = context.getScope();
    List<Type> types = rowType.getTypeParameters();
    block.comment("Create new RowBlockBuilder; beginBlockEntry;");
    Variable blockBuilder = scope.createTempVariable(BlockBuilder.class);
    Variable singleRowBlockWriter = scope.createTempVariable(BlockBuilder.class);
    block.append(blockBuilder.set(constantType(binder, rowType).invoke("createBlockBuilder", BlockBuilder.class, constantNull(BlockBuilderStatus.class), constantInt(1))));
    block.append(singleRowBlockWriter.set(blockBuilder.invoke("beginBlockEntry", BlockBuilder.class)));
    for (int i = 0; i < arguments.size(); ++i) {
        Type fieldType = types.get(i);
        Variable field = scope.createTempVariable(fieldType.getJavaType());
        block.comment("Clean wasNull and Generate + " + i + "-th field of row");
        block.append(context.wasNull().set(constantFalse()));
        block.append(context.generate(arguments.get(i)));
        block.putVariable(field);
        block.append(new IfStatement().condition(context.wasNull()).ifTrue(singleRowBlockWriter.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(constantType(binder, fieldType).writeValue(singleRowBlockWriter, field).pop()));
    }
    block.comment("closeEntry; slice the SingleRowBlock; wasNull = false;");
    block.append(blockBuilder.invoke("closeEntry", BlockBuilder.class).pop());
    block.append(constantType(binder, rowType).invoke("getObject", Object.class, blockBuilder.cast(Block.class), constantInt(0)).cast(Block.class));
    block.append(context.wasNull().set(constantFalse()));
    return block;
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) Type(io.trino.spi.type.Type) SqlTypeBytecodeExpression.constantType(io.trino.sql.gen.SqlTypeBytecodeExpression.constantType) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Block(io.trino.spi.block.Block) BlockBuilderStatus(io.trino.spi.block.BlockBuilderStatus)

Example 29 with Scope

use of io.airlift.bytecode.Scope in project trino by trinodb.

the class JoinFilterFunctionCompiler method generateFilterMethod.

private void generateFilterMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, RowExpression filter, int leftBlocksSize, FieldDefinition sessionField) {
    // int leftPosition, Page leftPage, int rightPosition, Page rightPage
    Parameter leftPosition = arg("leftPosition", int.class);
    Parameter leftPage = arg("leftPage", Page.class);
    Parameter rightPosition = arg("rightPosition", int.class);
    Parameter rightPage = arg("rightPage", Page.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "filter", type(boolean.class), ImmutableList.<Parameter>builder().add(leftPosition).add(leftPage).add(rightPosition).add(rightPage).build());
    method.comment("filter: %s", filter.toString());
    BytecodeBlock body = method.getBody();
    Scope scope = method.getScope();
    Variable wasNullVariable = scope.declareVariable("wasNull", body, constantFalse());
    scope.declareVariable("session", body, method.getThis().getField(sessionField));
    RowExpressionCompiler compiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(callSiteBinder, leftPosition, leftPage, rightPosition, rightPage, leftBlocksSize), functionManager, compiledLambdaMap);
    BytecodeNode visitorBody = compiler.compile(filter, scope);
    Variable result = scope.declareVariable(boolean.class, "result");
    body.append(visitorBody).putVariable(result).append(new IfStatement().condition(wasNullVariable).ifTrue(constantFalse().ret()).ifFalse(result.ret()));
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter) BytecodeNode(io.airlift.bytecode.BytecodeNode)

Example 30 with Scope

use of io.airlift.bytecode.Scope in project trino by trinodb.

the class LambdaBytecodeGenerator method variableReferenceCompiler.

private static RowExpressionVisitor<BytecodeNode, Scope> variableReferenceCompiler(Map<String, ParameterAndType> parameterMap) {
    return new RowExpressionVisitor<>() {

        @Override
        public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitCall(CallExpression call, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitSpecialForm(SpecialForm specialForm, Scope context) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitConstant(ConstantExpression literal, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitLambda(LambdaDefinitionExpression lambda, Scope context) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
            ParameterAndType parameterAndType = parameterMap.get(reference.getName());
            Parameter parameter = parameterAndType.getParameter();
            Class<?> type = parameterAndType.getType();
            return new BytecodeBlock().append(parameter).append(unboxPrimitiveIfNecessary(context, type));
        }
    };
}
Also used : InputReferenceExpression(io.trino.sql.relational.InputReferenceExpression) ConstantExpression(io.trino.sql.relational.ConstantExpression) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Scope(io.airlift.bytecode.Scope) VariableReferenceExpression(io.trino.sql.relational.VariableReferenceExpression) RowExpressionVisitor(io.trino.sql.relational.RowExpressionVisitor) Parameter(io.airlift.bytecode.Parameter) CallExpression(io.trino.sql.relational.CallExpression) SpecialForm(io.trino.sql.relational.SpecialForm) LambdaDefinitionExpression(io.trino.sql.relational.LambdaDefinitionExpression)

Aggregations

Scope (io.airlift.bytecode.Scope)67 Variable (io.airlift.bytecode.Variable)63 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)56 MethodDefinition (io.airlift.bytecode.MethodDefinition)53 Parameter (io.airlift.bytecode.Parameter)49 IfStatement (io.airlift.bytecode.control.IfStatement)44 BytecodeNode (io.airlift.bytecode.BytecodeNode)21 ClassDefinition (io.airlift.bytecode.ClassDefinition)21 ImmutableList (com.google.common.collect.ImmutableList)17 ForLoop (io.airlift.bytecode.control.ForLoop)15 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)14 LabelNode (io.airlift.bytecode.instruction.LabelNode)14 Type (io.prestosql.spi.type.Type)12 Block (io.prestosql.spi.block.Block)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)10 CallSiteBinder (io.prestosql.sql.gen.CallSiteBinder)9 Block (io.trino.spi.block.Block)9 VariableInstruction.incrementVariable (io.airlift.bytecode.instruction.VariableInstruction.incrementVariable)8 BlockBuilder (io.prestosql.spi.block.BlockBuilder)8 Type (io.trino.spi.type.Type)8