Search in sources :

Example 1 with IfStatement

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

the class AbstractMinMaxBy method generateCombineMethod.

private void generateCombineMethod(ClassDefinition definition, CallSiteBinder binder, MethodHandle compareMethod, Type keyType, Type valueType, Class<?> stateClass) {
    Parameter state = arg("state", stateClass);
    Parameter otherState = arg("otherState", stateClass);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "combine", type(void.class), state, otherState);
    Class<?> keyJavaType = keyType.getJavaType();
    BytecodeBlock ifBlock = new BytecodeBlock().append(state.invoke("setFirst", void.class, otherState.invoke("getFirst", keyJavaType))).append(state.invoke("setFirstNull", void.class, otherState.invoke("isFirstNull", boolean.class))).append(state.invoke("setSecondNull", void.class, otherState.invoke("isSecondNull", boolean.class)));
    if (valueType.getJavaType().isPrimitive()) {
        ifBlock.append(state.invoke("setSecond", void.class, otherState.invoke("getSecond", valueType.getJavaType())));
    } else {
        ifBlock.append(new BytecodeBlock().append(state.invoke("setSecondBlock", void.class, otherState.invoke("getSecondBlock", Block.class))).append(state.invoke("setSecondPosition", void.class, otherState.invoke("getSecondPosition", int.class))));
    }
    method.getBody().append(new IfStatement().condition(or(state.invoke("isFirstNull", boolean.class), and(not(otherState.invoke("isFirstNull", boolean.class)), loadConstant(binder, compareMethod, MethodHandle.class).invoke("invokeExact", boolean.class, otherState.invoke("getFirst", keyJavaType), state.invoke("getFirst", keyJavaType))))).ifTrue(ifBlock)).ret();
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Signature.orderableTypeParameter(io.prestosql.spi.function.Signature.orderableTypeParameter) Parameter(io.airlift.bytecode.Parameter) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with IfStatement

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

the class AccumulatorCompiler method generateInputForLoop.

private static BytecodeBlock generateInputForLoop(List<FieldDefinition> stateField, List<ParameterMetadata> parameterMetadatas, MethodHandle inputFunction, Scope scope, List<Variable> parameterVariables, List<Class<?>> lambdaInterfaces, List<FieldDefinition> lambdaProviderFields, Variable masksBlock, CallSiteBinder callSiteBinder, boolean grouped) {
    // For-loop over rows
    Variable page = scope.getVariable("page");
    Variable positionVariable = scope.declareVariable(int.class, "position");
    Variable rowsVariable = scope.declareVariable(int.class, "rows");
    BytecodeBlock block = new BytecodeBlock().append(page).invokeVirtual(Page.class, "getPositionCount", int.class).putVariable(rowsVariable).initializeVariable(positionVariable);
    BytecodeNode loopBody = generateInvokeInputFunction(scope, stateField, positionVariable, parameterVariables, parameterMetadatas, lambdaInterfaces, lambdaProviderFields, inputFunction, callSiteBinder, grouped);
    // Wrap with null checks
    List<Boolean> nullable = new ArrayList<>();
    for (ParameterMetadata metadata : parameterMetadatas) {
        switch(metadata.getParameterType()) {
            case INPUT_CHANNEL:
            case BLOCK_INPUT_CHANNEL:
                nullable.add(false);
                break;
            case NULLABLE_BLOCK_INPUT_CHANNEL:
                nullable.add(true);
                break;
            // do nothing
            default:
        }
    }
    checkState(nullable.size() == parameterVariables.size(), "Number of parameters does not match");
    for (int i = 0; i < parameterVariables.size(); i++) {
        if (!nullable.get(i)) {
            Variable variableDefinition = parameterVariables.get(i);
            loopBody = new IfStatement("if(!%s.isNull(position))", variableDefinition.getName()).condition(new BytecodeBlock().getVariable(variableDefinition).getVariable(positionVariable).invokeInterface(Block.class, "isNull", boolean.class, int.class)).ifFalse(loopBody);
        }
    }
    loopBody = new IfStatement("if(testMask(%s, position))", masksBlock.getName()).condition(new BytecodeBlock().getVariable(masksBlock).getVariable(positionVariable).invokeStatic(CompilerOperations.class, "testMask", boolean.class, Block.class, int.class)).ifTrue(loopBody);
    block.append(new ForLoop().initialize(new BytecodeBlock().putVariable(positionVariable, 0)).condition(new BytecodeBlock().getVariable(positionVariable).getVariable(rowsVariable).invokeStatic(CompilerOperations.class, "lessThan", boolean.class, int.class, int.class)).update(new BytecodeBlock().incrementVariable(positionVariable, (byte) 1)).body(loopBody));
    return block;
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) ForLoop(io.airlift.bytecode.control.ForLoop) CompilerOperations(io.prestosql.sql.gen.CompilerOperations) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ArrayList(java.util.ArrayList) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) GroupByIdBlock(io.prestosql.operator.GroupByIdBlock) Block(io.prestosql.spi.block.Block) BytecodeNode(io.airlift.bytecode.BytecodeNode) ParameterMetadata(io.prestosql.operator.aggregation.AggregationMetadata.ParameterMetadata)

Example 3 with IfStatement

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

the class NullIfCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(FunctionHandle functionHandle, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
    Scope scope = generatorContext.getScope();
    RowExpression first = arguments.get(0);
    RowExpression second = arguments.get(1);
    LabelNode notMatch = new LabelNode("notMatch");
    // push first arg on the stack
    Variable firstValue = scope.createTempVariable(first.getType().getJavaType());
    BytecodeBlock block = new BytecodeBlock().comment("check if first arg is null").append(generatorContext.generate(first)).append(ifWasNullPopAndGoto(scope, notMatch, void.class)).dup(first.getType().getJavaType()).putVariable(firstValue);
    Type firstType = first.getType();
    Type secondType = second.getType();
    // if (equal(cast(first as <common type>), cast(second as <common type>))
    FunctionAndTypeManager functionAndTypeManager = generatorContext.getFunctionManager();
    FunctionHandle equalFunction = functionAndTypeManager.resolveOperatorFunctionHandle(EQUAL, TypeSignatureProvider.fromTypes(firstType, secondType));
    FunctionMetadata equalFunctionMetadata = functionAndTypeManager.getFunctionMetadata(equalFunction);
    BuiltInScalarFunctionImplementation equalsFunction = generatorContext.getFunctionManager().getBuiltInScalarFunctionImplementation(equalFunction);
    BytecodeNode equalsCall = generatorContext.generateCall(EQUAL.name(), equalsFunction, ImmutableList.of(cast(generatorContext, firstValue, firstType, equalFunctionMetadata.getArgumentTypes().get(0)), cast(generatorContext, generatorContext.generate(second, Optional.empty()), secondType, equalFunctionMetadata.getArgumentTypes().get(1))));
    BytecodeBlock conditionBlock = new BytecodeBlock().append(equalsCall).append(BytecodeUtils.ifWasNullClearPopAndGoto(scope, notMatch, void.class, boolean.class));
    // if first and second are equal, return null
    BytecodeBlock trueBlock = new BytecodeBlock().append(generatorContext.wasNull().set(constantTrue())).pop(first.getType().getJavaType()).pushJavaDefault(first.getType().getJavaType());
    // else return first (which is still on the stack
    block.append(new IfStatement().condition(conditionBlock).ifTrue(trueBlock).ifFalse(notMatch));
    return block;
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) IfStatement(io.airlift.bytecode.control.IfStatement) CastType(io.prestosql.metadata.CastType) Type(io.prestosql.spi.type.Type) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) FunctionAndTypeManager(io.prestosql.metadata.FunctionAndTypeManager) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) RowExpression(io.prestosql.spi.relation.RowExpression) BytecodeNode(io.airlift.bytecode.BytecodeNode) FunctionHandle(io.prestosql.spi.function.FunctionHandle)

Example 4 with IfStatement

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

the class PageFunctionCompiler method generateProcessMethod.

private static MethodDefinition generateProcessMethod(ClassDefinition classDefinition, FieldDefinition blockBuilder, FieldDefinition session, FieldDefinition page, FieldDefinition selectedPositions, FieldDefinition nextIndexOrPosition, FieldDefinition result) {
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "process", type(boolean.class), ImmutableList.of());
    Scope scope = method.getScope();
    Variable thisVariable = method.getThis();
    BytecodeBlock body = method.getBody();
    Variable from = scope.declareVariable("from", body, thisVariable.getField(nextIndexOrPosition));
    Variable to = scope.declareVariable("to", body, add(thisVariable.getField(selectedPositions).invoke("getOffset", int.class), thisVariable.getField(selectedPositions).invoke("size", int.class)));
    Variable positions = scope.declareVariable(int[].class, "positions");
    Variable index = scope.declareVariable(int.class, "index");
    IfStatement ifStatement = new IfStatement().condition(thisVariable.getField(selectedPositions).invoke("isList", boolean.class));
    body.append(ifStatement);
    ifStatement.ifTrue(new BytecodeBlock().append(positions.set(thisVariable.getField(selectedPositions).invoke("getPositions", int[].class))).append(new ForLoop("positions loop").initialize(index.set(from)).condition(lessThan(index, to)).update(index.increment()).body(new BytecodeBlock().append(thisVariable.invoke("evaluate", void.class, thisVariable.getField(session), thisVariable.getField(page), positions.getElement(index))))));
    ifStatement.ifFalse(new ForLoop("range based loop").initialize(index.set(from)).condition(lessThan(index, to)).update(index.increment()).body(new BytecodeBlock().append(thisVariable.invoke("evaluate", void.class, thisVariable.getField(session), thisVariable.getField(page), index))));
    body.comment("result = this.blockBuilder.build(); return true;").append(thisVariable.setField(result, thisVariable.getField(blockBuilder).invoke("build", Block.class))).push(true).retBoolean();
    return method;
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) ForLoop(io.airlift.bytecode.control.ForLoop) MethodDefinition(io.airlift.bytecode.MethodDefinition) BytecodeBlock(io.airlift.bytecode.BytecodeBlock)

Example 5 with IfStatement

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

the class JoinCompiler method typeEquals.

private static BytecodeNode typeEquals(BytecodeExpression type, BytecodeExpression leftBlock, BytecodeExpression leftBlockPosition, BytecodeExpression rightBlock, BytecodeExpression rightBlockPosition) {
    IfStatement ifStatement = new IfStatement();
    ifStatement.condition().append(leftBlock.invoke("isNull", boolean.class, leftBlockPosition)).append(rightBlock.invoke("isNull", boolean.class, rightBlockPosition)).append(OpCode.IOR);
    ifStatement.ifTrue().append(leftBlock.invoke("isNull", boolean.class, leftBlockPosition)).append(rightBlock.invoke("isNull", boolean.class, rightBlockPosition)).append(OpCode.IAND);
    ifStatement.ifFalse().append(typeEqualsIgnoreNulls(type, leftBlock, leftBlockPosition, rightBlock, rightBlockPosition));
    return ifStatement;
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement)

Aggregations

IfStatement (io.airlift.bytecode.control.IfStatement)79 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)69 Variable (io.airlift.bytecode.Variable)68 MethodDefinition (io.airlift.bytecode.MethodDefinition)43 Scope (io.airlift.bytecode.Scope)42 Parameter (io.airlift.bytecode.Parameter)38 BytecodeNode (io.airlift.bytecode.BytecodeNode)26 ForLoop (io.airlift.bytecode.control.ForLoop)20 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)20 LabelNode (io.airlift.bytecode.instruction.LabelNode)18 ClassDefinition (io.airlift.bytecode.ClassDefinition)14 ImmutableList (com.google.common.collect.ImmutableList)12 Block (io.prestosql.spi.block.Block)12 Block (io.trino.spi.block.Block)11 Type (io.prestosql.spi.type.Type)10 ArrayList (java.util.ArrayList)10 VariableInstruction.incrementVariable (io.airlift.bytecode.instruction.VariableInstruction.incrementVariable)8 BlockBuilder (io.prestosql.spi.block.BlockBuilder)8 CallSiteBinder (io.prestosql.sql.gen.CallSiteBinder)8 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)7