Search in sources :

Example 96 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class JoinCompiler method generatePositionNotDistinctFromRowWithPageMethod.

private void generatePositionNotDistinctFromRowWithPageMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> joinChannelTypes, List<FieldDefinition> joinChannelFields) {
    Parameter leftBlockIndex = arg("leftBlockIndex", int.class);
    Parameter leftBlockPosition = arg("leftBlockPosition", int.class);
    Parameter rightPosition = arg("rightPosition", int.class);
    Parameter page = arg("page", Page.class);
    Parameter rightChannels = arg("rightChannels", int[].class);
    MethodDefinition positionNotDistinctFromRowMethod = classDefinition.declareMethod(a(PUBLIC), "positionNotDistinctFromRow", type(boolean.class), leftBlockIndex, leftBlockPosition, rightPosition, page, rightChannels);
    Variable thisVariable = positionNotDistinctFromRowMethod.getThis();
    Scope scope = positionNotDistinctFromRowMethod.getScope();
    BytecodeBlock body = positionNotDistinctFromRowMethod.getBody();
    scope.declareVariable("wasNull", body, constantFalse());
    for (int index = 0; index < joinChannelTypes.size(); index++) {
        BytecodeExpression leftBlock = thisVariable.getField(joinChannelFields.get(index)).invoke("get", Object.class, leftBlockIndex).cast(Block.class);
        BytecodeExpression rightBlock = page.invoke("getBlock", Block.class, rightChannels.getElement(index));
        Type type = joinChannelTypes.get(index);
        // At that point, we'll be able to fully deprecate Type.equalTo (and friends) and remove this hack.
        if (type.getJavaType().equals(Slice.class)) {
            switch(type.getTypeSignature().getBase()) {
                case StandardTypes.CHAR:
                case StandardTypes.IPADDRESS:
                case StandardTypes.JSON:
                case StandardTypes.DECIMAL:
                case StandardTypes.VARBINARY:
                case StandardTypes.VARCHAR:
                    body.append(new IfStatement().condition(typeEquals(constantType(callSiteBinder, type), leftBlock, leftBlockPosition, rightBlock, rightPosition)).ifFalse(constantFalse().ret()));
                    continue;
            }
        }
        BuiltInScalarFunctionImplementation operator = functionAndTypeManager.getBuiltInScalarFunctionImplementation(functionAndTypeManager.resolveOperatorFunctionHandle(OperatorType.IS_DISTINCT_FROM, fromTypes(type, type)));
        Binding binding = callSiteBinder.bind(operator.getMethodHandle());
        List<BytecodeNode> argumentsBytecode = new ArrayList<>();
        argumentsBytecode.add(generateInputReference(callSiteBinder, scope, type, leftBlock, leftBlockPosition));
        argumentsBytecode.add(generateInputReference(callSiteBinder, scope, type, rightBlock, rightPosition));
        body.append(new IfStatement().condition(BytecodeUtils.generateInvocation(scope, "isDistinctFrom", operator, Optional.empty(), argumentsBytecode, callSiteBinder)).ifTrue(constantFalse().ret()));
    }
    body.append(constantTrue().ret());
}
Also used : Variable(io.airlift.bytecode.Variable) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ArrayList(java.util.ArrayList) IfStatement(io.airlift.bytecode.control.IfStatement) SqlTypeBytecodeExpression.constantType(io.prestosql.sql.gen.SqlTypeBytecodeExpression.constantType) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) BigintType(io.prestosql.spi.type.BigintType) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter) BytecodeNode(io.airlift.bytecode.BytecodeNode) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 97 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class JoinCompiler method generateIsPositionNull.

private static void generateIsPositionNull(ClassDefinition classDefinition, List<FieldDefinition> joinChannelFields) {
    Parameter blockIndex = arg("blockIndex", int.class);
    Parameter blockPosition = arg("blockPosition", int.class);
    MethodDefinition isPositionNullMethod = classDefinition.declareMethod(a(PUBLIC), "isPositionNull", type(boolean.class), blockIndex, blockPosition);
    for (FieldDefinition joinChannelField : joinChannelFields) {
        BytecodeExpression block = isPositionNullMethod.getThis().getField(joinChannelField).invoke("get", Object.class, blockIndex).cast(Block.class);
        IfStatement ifStatement = new IfStatement();
        ifStatement.condition(block.invoke("isNull", boolean.class, blockPosition));
        ifStatement.ifTrue(constantTrue().ret());
        isPositionNullMethod.getBody().append(ifStatement);
    }
    isPositionNullMethod.getBody().append(constantFalse().ret());
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) MethodDefinition(io.airlift.bytecode.MethodDefinition) FieldDefinition(io.airlift.bytecode.FieldDefinition) Parameter(io.airlift.bytecode.Parameter) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 98 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class JoinCompiler method generateConstructor.

private static void generateConstructor(ClassDefinition classDefinition, List<Integer> joinChannels, FieldDefinition sizeField, FieldDefinition instanceSizeField, List<FieldDefinition> channelFields, List<FieldDefinition> joinChannelFields, FieldDefinition hashChannelField) {
    Parameter channels = arg("channels", type(List.class, type(List.class, Block.class)));
    Parameter hashChannel = arg("hashChannel", type(OptionalInt.class));
    MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC), channels, hashChannel);
    Variable thisVariable = constructorDefinition.getThis();
    Variable blockIndex = constructorDefinition.getScope().declareVariable(int.class, "blockIndex");
    BytecodeBlock constructor = constructorDefinition.getBody().comment("super();").append(thisVariable).invokeConstructor(Object.class);
    constructor.comment("this.size = INSTANCE_SIZE").append(thisVariable.setField(sizeField, getStatic(instanceSizeField)));
    constructor.comment("Set channel fields");
    for (int index = 0; index < channelFields.size(); index++) {
        BytecodeExpression channel = channels.invoke("get", Object.class, constantInt(index)).cast(type(List.class, Block.class));
        constructor.append(thisVariable.setField(channelFields.get(index), channel));
        BytecodeBlock loopBody = new BytecodeBlock();
        constructor.comment("for(blockIndex = 0; blockIndex < channel.size(); blockIndex++) { size += channel.get(i).getRetainedSizeInBytes() }").append(new ForLoop().initialize(blockIndex.set(constantInt(0))).condition(new BytecodeBlock().append(blockIndex).append(channel.invoke("size", int.class)).invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class)).update(new BytecodeBlock().incrementVariable(blockIndex, (byte) 1)).body(loopBody));
        loopBody.append(thisVariable).append(thisVariable).getField(sizeField).append(channel.invoke("get", Object.class, blockIndex).cast(type(Block.class)).invoke("getRetainedSizeInBytes", long.class)).longAdd().putField(sizeField);
    }
    constructor.comment("Set join channel fields");
    for (int index = 0; index < joinChannelFields.size(); index++) {
        BytecodeExpression joinChannel = channels.invoke("get", Object.class, constantInt(joinChannels.get(index))).cast(type(List.class, Block.class));
        constructor.append(thisVariable.setField(joinChannelFields.get(index), joinChannel));
    }
    constructor.comment("Set hashChannel");
    constructor.append(new IfStatement().condition(hashChannel.invoke("isPresent", boolean.class)).ifTrue(thisVariable.setField(hashChannelField, channels.invoke("get", Object.class, hashChannel.invoke("getAsInt", int.class)))).ifFalse(thisVariable.setField(hashChannelField, constantNull(hashChannelField.getType()))));
    constructor.ret();
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) ForLoop(io.airlift.bytecode.control.ForLoop) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Parameter(io.airlift.bytecode.Parameter) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Block(io.prestosql.spi.block.Block) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) OptionalInt(java.util.OptionalInt) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 99 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class CursorProcessorCompiler method generateFilterMethod.

private void generateFilterMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, RowExpression filter) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter cursor = arg("cursor", RecordCursor.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "filter", type(boolean.class), session, cursor);
    method.comment("Filter: %s", filter);
    Scope scope = method.getScope();
    Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull");
    RowExpressionCompiler compiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(cursor), metadata, compiledLambdaMap, new ClassContext(classDefinition, scope, (newFilterName, rowExpressionCompiler, generator, subFilter, callerScope) -> {
        MethodDefinition newFilterMethod = classDefinition.declareMethod(a(PUBLIC), newFilterName, type(boolean.class), ImmutableList.<Parameter>builder().add(session).add(cursor).build());
        newFilterMethod.comment("Filter: %s", subFilter.toString());
        Scope innerScope = newFilterMethod.getScope();
        Variable wasNullVariable1 = innerScope.declareVariable(type(boolean.class), "wasNull");
        LabelNode end1 = new LabelNode("end");
        BytecodeGeneratorContext generatorContext = new BytecodeGeneratorContext(rowExpressionCompiler, innerScope, callSiteBinder, cachedInstanceBinder, metadata.getFunctionAndTypeManager());
        newFilterMethod.getBody().comment("boolean wasNull = false;").putVariable(wasNullVariable1, false).comment("evaluate filter: " + subFilter).append(generator.generateExpression(null, generatorContext, subFilter.getType(), subFilter.getArguments())).comment("if (wasNull) return false;").getVariable(wasNullVariable1).ifFalseGoto(end1).pop(boolean.class).push(false).visitLabel(end1).retBoolean();
        /* Call the sub-method: Important use caller scope to pass parameters to new function */
        BytecodeBlock block = new BytecodeBlock().setDescription("INVOKE " + newFilterName);
        LabelNode endCall = new LabelNode("end");
        block.append(callerScope.getThis()).getVariable(session).getVariable(cursor).invokeVirtual(classDefinition.getType(), newFilterName, type(boolean.class), type(ConnectorSession.class), type(RecordCursor.class));
        block.visitLabel(endCall);
        return block;
    }));
    LabelNode end = new LabelNode("end");
    method.getBody().comment("boolean wasNull = false;").putVariable(wasNullVariable, false).comment("evaluate filter: " + filter).append(compiler.compile(filter, scope)).comment("if (wasNull) return false;").getVariable(wasNullVariable).ifFalseGoto(end).pop(boolean.class).push(false).visitLabel(end).retBoolean();
}
Also used : ParameterizedType.type(io.airlift.bytecode.ParameterizedType.type) Variable(io.airlift.bytecode.Variable) Slice(io.airlift.slice.Slice) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) JumpInstruction.jump(io.airlift.bytecode.instruction.JumpInstruction.jump) RowExpressionVisitor(io.prestosql.spi.relation.RowExpressionVisitor) DriverYieldSignal(io.prestosql.operator.DriverYieldSignal) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) BytecodeNode(io.airlift.bytecode.BytecodeNode) Scope(io.airlift.bytecode.Scope) CursorProcessorOutput(io.prestosql.operator.project.CursorProcessorOutput) Parameter(io.airlift.bytecode.Parameter) WhileLoop(io.airlift.bytecode.control.WhileLoop) Access.a(io.airlift.bytecode.Access.a) CallExpression(io.prestosql.spi.relation.CallExpression) Parameter.arg(io.airlift.bytecode.Parameter.arg) ImmutableList(com.google.common.collect.ImmutableList) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) BytecodeExpressions.constantTrue(io.airlift.bytecode.expression.BytecodeExpressions.constantTrue) LabelNode(io.airlift.bytecode.instruction.LabelNode) Locale(java.util.Locale) Map(java.util.Map) RecordCursor(io.prestosql.spi.connector.RecordCursor) Type(io.prestosql.spi.type.Type) SpecialForm(io.prestosql.spi.relation.SpecialForm) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeExpressions.newInstance(io.airlift.bytecode.expression.BytecodeExpressions.newInstance) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BytecodeUtils.generateWrite(io.prestosql.sql.gen.BytecodeUtils.generateWrite) BlockBuilder(io.prestosql.spi.block.BlockBuilder) CompiledLambda(io.prestosql.sql.gen.LambdaBytecodeGenerator.CompiledLambda) Set(java.util.Set) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) PageBuilder(io.prestosql.spi.PageBuilder) Metadata(io.prestosql.metadata.Metadata) Primitives(com.google.common.primitives.Primitives) IfStatement(io.airlift.bytecode.control.IfStatement) List(java.util.List) PUBLIC(io.airlift.bytecode.Access.PUBLIC) BytecodeExpressions.or(io.airlift.bytecode.expression.BytecodeExpressions.or) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression) InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) LambdaExpressionExtractor.extractLambdaExpressions(io.prestosql.sql.gen.LambdaExpressionExtractor.extractLambdaExpressions) ClassDefinition(io.airlift.bytecode.ClassDefinition) LabelNode(io.airlift.bytecode.instruction.LabelNode) 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 100 with MethodDefinition

use of io.airlift.bytecode.MethodDefinition in project hetu-core by openlookeng.

the class CursorProcessorCompiler method generateProjectMethod.

private void generateProjectMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap, String methodName, RowExpression projection) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter cursor = arg("cursor", RecordCursor.class);
    Parameter output = arg("output", BlockBuilder.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(void.class), session, cursor, output);
    method.comment("Projection: %s", projection.toString());
    Scope scope = method.getScope();
    Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull");
    RowExpressionCompiler compiler = new RowExpressionCompiler(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(cursor), metadata, compiledLambdaMap);
    method.getBody().comment("boolean wasNull = false;").putVariable(wasNullVariable, false).getVariable(output).comment("evaluate projection: " + projection.toString()).append(compiler.compile(projection, scope)).append(generateWrite(callSiteBinder, scope, wasNullVariable, projection.getType())).ret();
}
Also used : Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter)

Aggregations

MethodDefinition (io.airlift.bytecode.MethodDefinition)126 Parameter (io.airlift.bytecode.Parameter)104 Variable (io.airlift.bytecode.Variable)102 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)83 Scope (io.airlift.bytecode.Scope)53 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)52 IfStatement (io.airlift.bytecode.control.IfStatement)45 ClassDefinition (io.airlift.bytecode.ClassDefinition)32 FieldDefinition (io.airlift.bytecode.FieldDefinition)28 BytecodeNode (io.airlift.bytecode.BytecodeNode)24 ImmutableList (com.google.common.collect.ImmutableList)21 ArrayList (java.util.ArrayList)20 LabelNode (io.airlift.bytecode.instruction.LabelNode)19 ForLoop (io.airlift.bytecode.control.ForLoop)17 Type (io.trino.spi.type.Type)15 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)13 List (java.util.List)13 Block (io.prestosql.spi.block.Block)12 BlockBuilder (io.prestosql.spi.block.BlockBuilder)12 SqlTypeBytecodeExpression.constantType (io.trino.sql.gen.SqlTypeBytecodeExpression.constantType)12