Search in sources :

Example 81 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class CursorProcessorCompiler method fieldReferenceCompiler.

private static RowExpressionVisitor<Scope, BytecodeNode> fieldReferenceCompiler(Variable cursorVariable) {
    return new RowExpressionVisitor<Scope, BytecodeNode>() {

        @Override
        public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
            int field = node.getField();
            Type type = node.getType();
            Variable wasNullVariable = scope.getVariable("wasNull");
            Class<?> javaType = type.getJavaType();
            if (!javaType.isPrimitive() && javaType != Slice.class) {
                javaType = Object.class;
            }
            IfStatement ifStatement = new IfStatement();
            ifStatement.condition().setDescription(format("cursor.get%s(%d)", type, field)).getVariable(cursorVariable).push(field).invokeInterface(RecordCursor.class, "isNull", boolean.class, int.class);
            ifStatement.ifTrue().putVariable(wasNullVariable, true).pushJavaDefault(javaType);
            ifStatement.ifFalse().getVariable(cursorVariable).push(field).invokeInterface(RecordCursor.class, "get" + Primitives.wrap(javaType).getSimpleName(), javaType, int.class);
            return ifStatement;
        }

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

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

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

        @Override
        public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : InputReferenceExpression(com.facebook.presto.sql.relational.InputReferenceExpression) Variable(com.facebook.presto.bytecode.Variable) ConstantExpression(com.facebook.presto.sql.relational.ConstantExpression) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Type(com.facebook.presto.spi.type.Type) Scope(com.facebook.presto.bytecode.Scope) Slice(io.airlift.slice.Slice) VariableReferenceExpression(com.facebook.presto.sql.relational.VariableReferenceExpression) RowExpressionVisitor(com.facebook.presto.sql.relational.RowExpressionVisitor) CallExpression(com.facebook.presto.sql.relational.CallExpression) LambdaDefinitionExpression(com.facebook.presto.sql.relational.LambdaDefinitionExpression)

Example 82 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class IfCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext context, Type returnType, List<RowExpression> arguments) {
    Preconditions.checkArgument(arguments.size() == 3);
    Variable wasNull = context.wasNull();
    BytecodeBlock condition = new BytecodeBlock().append(context.generate(arguments.get(0))).comment("... and condition value was not null").append(wasNull).invokeStatic(CompilerOperations.class, "not", boolean.class, boolean.class).invokeStatic(CompilerOperations.class, "and", boolean.class, boolean.class, boolean.class).append(wasNull.set(constantFalse()));
    return new IfStatement().condition(condition).ifTrue(context.generate(arguments.get(1))).ifFalse(context.generate(arguments.get(2)));
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 83 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class InCodeGenerator method buildInCase.

private static BytecodeBlock buildInCase(BytecodeGeneratorContext generatorContext, Scope scope, Type type, LabelNode caseLabel, LabelNode matchLabel, LabelNode noMatchLabel, Collection<BytecodeNode> testValues, boolean checkForNulls) {
    // 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().visitLabel(caseLabel);
    if (checkForNulls) {
        caseBlock.putVariable(caseWasNull, false);
    }
    LabelNode elseLabel = new LabelNode("else");
    BytecodeBlock elseBlock = new BytecodeBlock().visitLabel(elseLabel);
    Variable wasNull = generatorContext.wasNull();
    if (checkForNulls) {
        elseBlock.append(wasNull.set(caseWasNull));
    }
    elseBlock.gotoLabel(noMatchLabel);
    ScalarFunctionImplementation operator = generatorContext.getRegistry().getScalarFunctionImplementation(internalOperator(EQUAL, BOOLEAN, ImmutableList.of(type, type)));
    Binding equalsFunction = generatorContext.getCallSiteBinder().bind(operator.getMethodHandle());
    BytecodeNode elseNode = elseBlock;
    for (BytecodeNode testNode : testValues) {
        LabelNode testLabel = new LabelNode("test");
        IfStatement test = new IfStatement();
        test.condition().visitLabel(testLabel).dup(type.getJavaType()).append(testNode);
        if (checkForNulls) {
            IfStatement wasNullCheck = new IfStatement("if wasNull, set caseWasNull to true, clear wasNull, pop 2 values of type, and goto next test value");
            wasNullCheck.condition(wasNull);
            wasNullCheck.ifTrue(new BytecodeBlock().append(caseWasNull.set(constantTrue())).append(wasNull.set(constantFalse())).pop(type.getJavaType()).pop(type.getJavaType()).gotoLabel(elseLabel));
            test.condition().append(wasNullCheck);
        }
        test.condition().append(invoke(equalsFunction, EQUAL.name()));
        test.ifTrue().gotoLabel(matchLabel);
        test.ifFalse(elseNode);
        elseNode = test;
        elseLabel = testLabel;
    }
    caseBlock.append(elseNode);
    return caseBlock;
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode)

Example 84 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class JoinProbeCompiler method generateConstructor.

private static void generateConstructor(ClassDefinition classDefinition, List<Integer> probeChannels, Optional<Integer> probeHashChannel, FieldDefinition lookupSourceField, List<FieldDefinition> blockFields, List<FieldDefinition> probeChannelFields, FieldDefinition probeBlocksArrayField, FieldDefinition probePageField, FieldDefinition pageField, FieldDefinition probeHashBlockField, FieldDefinition positionField, FieldDefinition positionCountField) {
    Parameter lookupSource = arg("lookupSource", LookupSource.class);
    Parameter page = arg("page", Page.class);
    MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC), lookupSource, page);
    Variable thisVariable = constructorDefinition.getThis();
    BytecodeBlock constructor = constructorDefinition.getBody().comment("super();").append(thisVariable).invokeConstructor(Object.class);
    constructor.comment("this.lookupSource = lookupSource;").append(thisVariable.setField(lookupSourceField, lookupSource));
    constructor.comment("this.positionCount = page.getPositionCount();").append(thisVariable.setField(positionCountField, page.invoke("getPositionCount", int.class)));
    constructor.comment("Set block fields");
    for (int index = 0; index < blockFields.size(); index++) {
        constructor.append(thisVariable.setField(blockFields.get(index), page.invoke("getBlock", Block.class, constantInt(index))));
    }
    constructor.comment("Set probe channel fields");
    for (int index = 0; index < probeChannelFields.size(); index++) {
        constructor.append(thisVariable.setField(probeChannelFields.get(index), thisVariable.getField(blockFields.get(probeChannels.get(index)))));
    }
    constructor.comment("this.probeBlocks = new Block[<probeChannelCount>];");
    constructor.append(thisVariable).push(probeChannelFields.size()).newArray(Block.class).putField(probeBlocksArrayField);
    for (int index = 0; index < probeChannelFields.size(); index++) {
        constructor.append(thisVariable).getField(probeBlocksArrayField).push(index).append(thisVariable).getField(probeChannelFields.get(index)).putObjectArrayElement();
    }
    constructor.comment("this.page = page").append(thisVariable.setField(pageField, page));
    constructor.comment("this.probePage = new Page(probeBlocks)").append(thisVariable.setField(probePageField, newInstance(Page.class, thisVariable.getField(probeBlocksArrayField))));
    if (probeHashChannel.isPresent()) {
        Integer index = probeHashChannel.get();
        constructor.comment("this.probeHashBlock = blocks[hashChannel.get()]").append(thisVariable.setField(probeHashBlockField, thisVariable.getField(blockFields.get(index))));
    }
    constructor.comment("this.position = -1;").append(thisVariable.setField(positionField, constantInt(-1)));
    constructor.ret();
}
Also used : Variable(com.facebook.presto.bytecode.Variable) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 85 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class JoinProbeCompiler method generateAppendToMethod.

private static void generateAppendToMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> types, List<Integer> probeOutputChannels, List<FieldDefinition> blockFields, FieldDefinition positionField) {
    Parameter pageBuilder = arg("pageBuilder", PageBuilder.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "appendTo", type(void.class), pageBuilder);
    Variable thisVariable = method.getThis();
    int pageBuilderOutputChannel = 0;
    for (int outputChannel : probeOutputChannels) {
        Type type = types.get(outputChannel);
        method.getBody().comment("%s.appendTo(block_%s, position, pageBuilder.getBlockBuilder(%s));", type.getClass(), outputChannel, pageBuilderOutputChannel).append(constantType(callSiteBinder, type).invoke("appendTo", void.class, thisVariable.getField(blockFields.get(outputChannel)), thisVariable.getField(positionField), pageBuilder.invoke("getBlockBuilder", BlockBuilder.class, constantInt(pageBuilderOutputChannel++))));
    }
    method.getBody().ret();
}
Also used : JoinType(com.facebook.presto.operator.LookupJoinOperators.JoinType) Type(com.facebook.presto.spi.type.Type) SqlTypeBytecodeExpression.constantType(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType) BigintType(com.facebook.presto.spi.type.BigintType) Variable(com.facebook.presto.bytecode.Variable) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter)

Aggregations

Variable (com.facebook.presto.bytecode.Variable)131 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)104 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)91 Parameter (com.facebook.presto.bytecode.Parameter)75 Scope (com.facebook.presto.bytecode.Scope)68 IfStatement (com.facebook.presto.bytecode.control.IfStatement)68 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)40 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)36 ImmutableList (com.google.common.collect.ImmutableList)30 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)28 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)21 ForLoop (com.facebook.presto.bytecode.control.ForLoop)21 Block (com.facebook.presto.spi.block.Block)19 Block (com.facebook.presto.common.block.Block)18 List (java.util.List)17 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)16 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)15 Type (com.facebook.presto.common.type.Type)15 RowExpression (com.facebook.presto.spi.relation.RowExpression)15 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)14