Search in sources :

Example 26 with LabelNode

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

the class RowIndeterminateOperator method generateIndeterminate.

private static Class<?> generateIndeterminate(Type type, FunctionAndTypeManager functionAndTypeManager) {
    CallSiteBinder binder = new CallSiteBinder();
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("RowIndeterminateOperator"), type(Object.class));
    Parameter value = arg("value", type.getJavaType());
    Parameter isNull = arg("isNull", boolean.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "indeterminate", type(boolean.class), value, isNull);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
    body.append(wasNull.set(constantFalse()));
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
    LabelNode end = new LabelNode("end");
    List<Type> fieldTypes = type.getTypeParameters();
    boolean hasUnknownFields = fieldTypes.stream().anyMatch(fieldType -> fieldType.equals(UNKNOWN));
    body.append(new IfStatement("if isNull is true...").condition(isNull).ifTrue(new BytecodeBlock().push(true).gotoLabel(end)));
    if (hasUnknownFields) {
        // if the current field type is UNKNOWN which means this field is null, directly return true
        body.push(true).gotoLabel(end);
    } else {
        for (int i = 0; i < fieldTypes.size(); i++) {
            IfStatement ifNullField = new IfStatement("if the field is null...");
            ifNullField.condition(value.invoke("isNull", boolean.class, constantInt(i))).ifTrue(new BytecodeBlock().push(true).gotoLabel(end));
            FunctionHandle functionHandle = functionAndTypeManager.resolveOperatorFunctionHandle(INDETERMINATE, fromTypes(fieldTypes.get(i)));
            BuiltInScalarFunctionImplementation function = functionAndTypeManager.getBuiltInScalarFunctionImplementation(functionHandle);
            BytecodeExpression element = constantType(binder, fieldTypes.get(i)).getValue(value, constantInt(i));
            ifNullField.ifFalse(new IfStatement("if the field is not null but indeterminate...").condition(invokeFunction(scope, cachedInstanceBinder, functionAndTypeManager.getFunctionMetadata(functionHandle).getName().getObjectName(), function, element)).ifTrue(new BytecodeBlock().push(true).gotoLabel(end)));
            body.append(ifNullField);
        }
        // if none of the fields is indeterminate, then push false
        body.push(false);
    }
    body.visitLabel(end).retBoolean();
    // create constructor
    MethodDefinition constructorDefinition = definition.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();
    return defineClass(definition, Object.class, binder.getBindings(), RowIndeterminateOperator.class.getClassLoader());
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) Variable(io.airlift.bytecode.Variable) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) ClassDefinition(io.airlift.bytecode.ClassDefinition) IfStatement(io.airlift.bytecode.control.IfStatement) SqlTypeBytecodeExpression.constantType(io.prestosql.sql.gen.SqlTypeBytecodeExpression.constantType) Type(io.prestosql.spi.type.Type) CachedInstanceBinder(io.prestosql.sql.gen.CachedInstanceBinder) Scope(io.airlift.bytecode.Scope) MethodDefinition(io.airlift.bytecode.MethodDefinition) CallSiteBinder(io.prestosql.sql.gen.CallSiteBinder) Parameter(io.airlift.bytecode.Parameter) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression) FunctionHandle(io.prestosql.spi.function.FunctionHandle)

Example 27 with LabelNode

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

the class OrCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(FunctionHandle functionHandle, BytecodeGeneratorContext generator, Type returnType, List<RowExpression> arguments) {
    Preconditions.checkArgument(arguments.size() == 2);
    Variable wasNull = generator.wasNull();
    BytecodeBlock block = new BytecodeBlock().comment("OR").setDescription("OR");
    BytecodeNode left = generator.generate(arguments.get(0));
    BytecodeNode right = generator.generate(arguments.get(1));
    block.append(left);
    IfStatement ifLeftIsNull = new IfStatement("if left wasNull...").condition(wasNull);
    LabelNode end = new LabelNode("end");
    ifLeftIsNull.ifTrue(new BytecodeBlock().comment("clear the null flag, pop left value off stack, and push left null flag on the stack (true)").append(wasNull.set(constantFalse())).pop(// discard left value
    arguments.get(0).getType().getJavaType()).push(true));
    LabelNode leftIsFalse = new LabelNode("leftIsFalse");
    ifLeftIsNull.ifFalse(new BytecodeBlock().comment("if left is true, push true, and goto end").ifFalseGoto(leftIsFalse).push(true).gotoLabel(end).comment("left was false; push left null flag on the stack (false)").visitLabel(leftIsFalse).push(false));
    block.append(ifLeftIsNull);
    // At this point we know the left expression was either NULL or FALSE.  The stack contains a single boolean
    // value for this expression which indicates if the left value was NULL.
    // eval right!
    block.append(right);
    IfStatement ifRightIsNull = new IfStatement("if right wasNull...").condition(wasNull);
    // this leaves a single boolean on the stack which is ignored since the value in NULL
    ifRightIsNull.ifTrue().comment("right was null, pop the right value off the stack; wasNull flag remains set to TRUE").pop(arguments.get(1).getType().getJavaType());
    LabelNode rightIsTrue = new LabelNode("rightIsTrue");
    ifRightIsNull.ifFalse().comment("if right is true, pop left null flag off stack, push true and goto end").ifFalseGoto(rightIsTrue).pop(boolean.class).push(true).gotoLabel(end).comment("right was false; store left null flag (on stack) in wasNull variable, and push false").visitLabel(rightIsTrue).putVariable(wasNull).push(false);
    block.append(ifRightIsNull).visitLabel(end);
    return block;
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) BytecodeNode(io.airlift.bytecode.BytecodeNode)

Example 28 with LabelNode

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

the class JoinCompiler method generateRowEqualsRowMethod.

private static void generateRowEqualsRowMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> joinChannelTypes) {
    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 rowEqualsRowMethod = classDefinition.declareMethod(a(PUBLIC), "rowEqualsRow", type(boolean.class), leftPosition, leftPage, rightPosition, rightPage);
    for (int index = 0; index < joinChannelTypes.size(); index++) {
        BytecodeExpression type = constantType(callSiteBinder, joinChannelTypes.get(index));
        BytecodeExpression leftBlock = leftPage.invoke("getBlock", Block.class, constantInt(index));
        BytecodeExpression rightBlock = rightPage.invoke("getBlock", Block.class, constantInt(index));
        LabelNode checkNextField = new LabelNode("checkNextField");
        rowEqualsRowMethod.getBody().append(typeEquals(type, leftBlock, leftPosition, rightBlock, rightPosition)).ifTrueGoto(checkNextField).push(false).retBoolean().visitLabel(checkNextField);
    }
    rowEqualsRowMethod.getBody().push(true).retInt();
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 29 with LabelNode

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

the class InCodeGenerator method buildInCase.

private static BytecodeBlock buildInCase(BytecodeGeneratorContext generatorContext, Scope scope, Type type, LabelNode matchLabel, LabelNode noMatchLabel, Variable value, Collection<BytecodeNode> testValues, boolean checkForNulls, BuiltInScalarFunctionImplementation isIndeterminateFunction) {
    // caseWasNull is set to true the first time a null in `testValues` is encountered
    Variable caseWasNull = null;
    if (checkForNulls) {
        caseWasNull = scope.createTempVariable(boolean.class);
    }
    BytecodeBlock caseBlock = new BytecodeBlock();
    if (checkForNulls) {
        caseBlock.putVariable(caseWasNull, false);
    }
    LabelNode elseLabel = new LabelNode("else");
    BytecodeBlock elseBlock = new BytecodeBlock().visitLabel(elseLabel);
    Variable wasNull = generatorContext.wasNull();
    if (checkForNulls) {
        // That is incorrect. Doing an explicit check for indeterminate is required to correctly return NULL.
        if (testValues.isEmpty()) {
            elseBlock.append(new BytecodeBlock().append(generatorContext.generateCall(INDETERMINATE.name(), isIndeterminateFunction, ImmutableList.of(value))).putVariable(wasNull));
        } else {
            elseBlock.append(wasNull.set(caseWasNull));
        }
    }
    elseBlock.gotoLabel(noMatchLabel);
    FunctionHandle equalsHandle = generatorContext.getFunctionManager().resolveOperatorFunctionHandle(EQUAL, fromTypes(type, type));
    BuiltInScalarFunctionImplementation equalsFunction = generatorContext.getFunctionManager().getBuiltInScalarFunctionImplementation(equalsHandle);
    BytecodeNode elseNode = elseBlock;
    for (BytecodeNode testNode : testValues) {
        LabelNode testLabel = new LabelNode("test");
        IfStatement test = new IfStatement();
        BytecodeNode equalsCall = generatorContext.generateCall(EQUAL.name(), equalsFunction, ImmutableList.of(value, testNode));
        test.condition().visitLabel(testLabel).append(equalsCall);
        if (checkForNulls) {
            IfStatement wasNullCheck = new IfStatement("if wasNull, set caseWasNull to true, clear wasNull, pop boolean, and goto next test value");
            wasNullCheck.condition(wasNull);
            wasNullCheck.ifTrue(new BytecodeBlock().append(caseWasNull.set(constantTrue())).append(wasNull.set(constantFalse())).pop(boolean.class).gotoLabel(elseLabel));
            test.condition().append(wasNullCheck);
        }
        test.ifTrue().gotoLabel(matchLabel);
        test.ifFalse(elseNode);
        elseNode = test;
        elseLabel = testLabel;
    }
    caseBlock.append(elseNode);
    return caseBlock;
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) BytecodeNode(io.airlift.bytecode.BytecodeNode) FunctionHandle(io.prestosql.spi.function.FunctionHandle)

Example 30 with LabelNode

use of io.airlift.bytecode.instruction.LabelNode 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)

Aggregations

LabelNode (io.airlift.bytecode.instruction.LabelNode)39 Variable (io.airlift.bytecode.Variable)32 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)29 MethodDefinition (io.airlift.bytecode.MethodDefinition)19 Parameter (io.airlift.bytecode.Parameter)19 IfStatement (io.airlift.bytecode.control.IfStatement)19 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)17 BytecodeNode (io.airlift.bytecode.BytecodeNode)16 Scope (io.airlift.bytecode.Scope)14 Type (io.trino.spi.type.Type)10 Type (io.prestosql.spi.type.Type)6 BigintType (io.trino.spi.type.BigintType)6 SqlTypeBytecodeExpression.constantType (io.trino.sql.gen.SqlTypeBytecodeExpression.constantType)6 ImmutableList (com.google.common.collect.ImmutableList)5 FunctionHandle (io.prestosql.spi.function.FunctionHandle)5 RowExpression (io.trino.sql.relational.RowExpression)5 BuiltInScalarFunctionImplementation (io.prestosql.spi.function.BuiltInScalarFunctionImplementation)4 RowExpression (io.prestosql.spi.relation.RowExpression)4 MethodHandle (java.lang.invoke.MethodHandle)4 ImmutableSet (com.google.common.collect.ImmutableSet)3