Search in sources :

Example 1 with BytecodeExpression

use of com.facebook.presto.bytecode.expression.BytecodeExpression in project presto by prestodb.

the class OrderingCompiler method generateCompareTo.

private static void generateCompareTo(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> sortTypes, List<Integer> sortChannels, List<SortOrder> sortOrders) {
    Parameter pagesIndex = arg("pagesIndex", PagesIndex.class);
    Parameter leftPosition = arg("leftPosition", int.class);
    Parameter rightPosition = arg("rightPosition", int.class);
    MethodDefinition compareToMethod = classDefinition.declareMethod(a(PUBLIC), "compareTo", type(int.class), pagesIndex, leftPosition, rightPosition);
    Scope scope = compareToMethod.getScope();
    Variable valueAddresses = scope.declareVariable(LongArrayList.class, "valueAddresses");
    compareToMethod.getBody().comment("LongArrayList valueAddresses = pagesIndex.valueAddresses").append(valueAddresses.set(pagesIndex.invoke("getValueAddresses", LongArrayList.class)));
    Variable leftPageAddress = scope.declareVariable(long.class, "leftPageAddress");
    compareToMethod.getBody().comment("long leftPageAddress = valueAddresses.getLong(leftPosition)").append(leftPageAddress.set(valueAddresses.invoke("getLong", long.class, leftPosition)));
    Variable leftBlockIndex = scope.declareVariable(int.class, "leftBlockIndex");
    compareToMethod.getBody().comment("int leftBlockIndex = decodeSliceIndex(leftPageAddress)").append(leftBlockIndex.set(invokeStatic(SyntheticAddress.class, "decodeSliceIndex", int.class, leftPageAddress)));
    Variable leftBlockPosition = scope.declareVariable(int.class, "leftBlockPosition");
    compareToMethod.getBody().comment("int leftBlockPosition = decodePosition(leftPageAddress)").append(leftBlockPosition.set(invokeStatic(SyntheticAddress.class, "decodePosition", int.class, leftPageAddress)));
    Variable rightPageAddress = scope.declareVariable(long.class, "rightPageAddress");
    compareToMethod.getBody().comment("long rightPageAddress = valueAddresses.getLong(rightPosition);").append(rightPageAddress.set(valueAddresses.invoke("getLong", long.class, rightPosition)));
    Variable rightBlockIndex = scope.declareVariable(int.class, "rightBlockIndex");
    compareToMethod.getBody().comment("int rightBlockIndex = decodeSliceIndex(rightPageAddress)").append(rightBlockIndex.set(invokeStatic(SyntheticAddress.class, "decodeSliceIndex", int.class, rightPageAddress)));
    Variable rightBlockPosition = scope.declareVariable(int.class, "rightBlockPosition");
    compareToMethod.getBody().comment("int rightBlockPosition = decodePosition(rightPageAddress)").append(rightBlockPosition.set(invokeStatic(SyntheticAddress.class, "decodePosition", int.class, rightPageAddress)));
    for (int i = 0; i < sortChannels.size(); i++) {
        int sortChannel = sortChannels.get(i);
        SortOrder sortOrder = sortOrders.get(i);
        BytecodeBlock block = new BytecodeBlock().setDescription("compare channel " + sortChannel + " " + sortOrder);
        Type sortType = sortTypes.get(i);
        BytecodeExpression leftBlock = pagesIndex.invoke("getChannel", ObjectArrayList.class, constantInt(sortChannel)).invoke("get", Object.class, leftBlockIndex).cast(Block.class);
        BytecodeExpression rightBlock = pagesIndex.invoke("getChannel", ObjectArrayList.class, constantInt(sortChannel)).invoke("get", Object.class, rightBlockIndex).cast(Block.class);
        block.append(getStatic(SortOrder.class, sortOrder.name()).invoke("compareBlockValue", int.class, ImmutableList.of(Type.class, Block.class, int.class, Block.class, int.class), constantType(callSiteBinder, sortType), leftBlock, leftBlockPosition, rightBlock, rightBlockPosition));
        LabelNode equal = new LabelNode("equal");
        block.comment("if (compare != 0) return compare").dup().ifZeroGoto(equal).retInt().visitLabel(equal).pop(int.class);
        compareToMethod.getBody().append(block);
    }
    // values are equal
    compareToMethod.getBody().push(0).retInt();
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) Type(com.facebook.presto.spi.type.Type) SqlTypeBytecodeExpression.constantType(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) SortOrder(com.facebook.presto.spi.block.SortOrder) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 2 with BytecodeExpression

use of com.facebook.presto.bytecode.expression.BytecodeExpression in project presto by prestodb.

the class DereferenceCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext generator, Type returnType, List<RowExpression> arguments) {
    checkArgument(arguments.size() == 2);
    CallSiteBinder callSiteBinder = generator.getCallSiteBinder();
    BytecodeBlock block = new BytecodeBlock().comment("DEREFERENCE").setDescription("DEREFERENCE");
    Variable wasNull = generator.wasNull();
    Variable rowBlock = generator.getScope().createTempVariable(Block.class);
    int index = (int) ((ConstantExpression) arguments.get(1)).getValue();
    // clear the wasNull flag before evaluating the row value
    block.putVariable(wasNull, false);
    block.append(generator.generate(arguments.get(0))).putVariable(rowBlock);
    IfStatement ifRowBlockIsNull = new IfStatement("if row block is null...").condition(wasNull);
    Class<?> javaType = returnType.getJavaType();
    LabelNode end = new LabelNode("end");
    ifRowBlockIsNull.ifTrue().comment("if row block is null, push null to the stack and goto 'end' label (return)").putVariable(wasNull, true).pushJavaDefault(javaType).gotoLabel(end);
    block.append(ifRowBlockIsNull);
    IfStatement ifFieldIsNull = new IfStatement("if row field is null...");
    ifFieldIsNull.condition().comment("call rowBlock.isNull(index)").append(rowBlock).push(index).invokeInterface(Block.class, "isNull", boolean.class, int.class);
    ifFieldIsNull.ifTrue().comment("if the field is null, push null to stack").putVariable(wasNull, true).pushJavaDefault(javaType);
    BytecodeExpression value = constantType(callSiteBinder, returnType).getValue(rowBlock, constantInt(index));
    ifFieldIsNull.ifFalse().comment("otherwise call type.getTYPE(rowBlock, index)").append(value).putVariable(wasNull, false);
    block.append(ifFieldIsNull).visitLabel(end);
    return block;
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 3 with BytecodeExpression

use of com.facebook.presto.bytecode.expression.BytecodeExpression in project presto by prestodb.

the class PageProcessorCompiler method generateProjectColumnarMethod.

private static MethodDefinition generateProjectColumnarMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, String methodName, RowExpression projection, MethodDefinition projectionMethod) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter page = arg("page", Page.class);
    Parameter selectedPositions = arg("selectedPositions", int[].class);
    Parameter pageBuilder = arg("pageBuilder", PageBuilder.class);
    Parameter projectionIndex = arg("projectionIndex", int.class);
    List<Parameter> params = ImmutableList.<Parameter>builder().add(session).add(page).add(selectedPositions).add(pageBuilder).add(projectionIndex).build();
    MethodDefinition method = classDefinition.declareMethod(a(PRIVATE), methodName, type(Block.class), params);
    BytecodeBlock body = method.getBody();
    Scope scope = method.getScope();
    Variable thisVariable = method.getThis();
    ImmutableList.Builder<Variable> builder = ImmutableList.builder();
    for (int channel : getInputChannels(projection)) {
        Variable blockVariable = scope.declareVariable("block_" + channel, body, page.invoke("getBlock", Block.class, constantInt(channel)));
        builder.add(blockVariable);
    }
    List<Variable> inputs = builder.build();
    Variable positionCount = scope.declareVariable("positionCount", body, page.invoke("getPositionCount", int.class));
    Variable position = scope.declareVariable("position", body, constantInt(0));
    Variable cardinality = scope.declareVariable("cardinality", body, selectedPositions.length());
    Variable outputBlock = scope.declareVariable(Block.class, "outputBlock");
    Variable blockBuilder = scope.declareVariable("blockBuilder", body, pageBuilder.invoke("getBlockBuilder", BlockBuilder.class, projectionIndex));
    Variable type = scope.declareVariable("type", body, pageBuilder.invoke("getType", Type.class, projectionIndex));
    BytecodeBlock projectBlock = new BytecodeBlock().append(new ForLoop().initialize(position.set(constantInt(0))).condition(lessThan(position, cardinality)).update(position.increment()).body(invokeProject(thisVariable, session, inputs, selectedPositions.getElement(position), pageBuilder, projectionIndex, projectionMethod))).append(outputBlock.set(blockBuilder.invoke("build", Block.class)));
    if (isIdentityExpression(projection)) {
        // if nothing is filtered out, copy the entire block, else project it
        body.append(new IfStatement().condition(equal(cardinality, positionCount)).ifTrue(new BytecodeBlock().append(inputs.get(0).invoke("assureLoaded", void.class)).append(outputBlock.set(inputs.get(0)))).ifFalse(projectBlock));
    } else if (isConstantExpression(projection)) {
        // if projection is a constant, create RLE block of constant expression with cardinality positions
        ConstantExpression constantExpression = (ConstantExpression) projection;
        verify(getInputChannels(projection).isEmpty());
        BytecodeExpression value = loadConstant(callSiteBinder, constantExpression.getValue(), Object.class);
        body.append(outputBlock.set(invokeStatic(RunLengthEncodedBlock.class, "create", Block.class, type, value, cardinality)));
    } else {
        body.append(projectBlock);
    }
    body.append(outputBlock.ret());
    return method;
}
Also used : Variable(com.facebook.presto.bytecode.Variable) ForLoop(com.facebook.presto.bytecode.control.ForLoop) ImmutableList(com.google.common.collect.ImmutableList) ConstantExpression(com.facebook.presto.sql.relational.ConstantExpression) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Type(com.facebook.presto.spi.type.Type) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) LazyBlock(com.facebook.presto.spi.block.LazyBlock) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder)

Example 4 with BytecodeExpression

use of com.facebook.presto.bytecode.expression.BytecodeExpression in project presto by prestodb.

the class AccumulatorCompiler method generateEnsureCapacity.

private static void generateEnsureCapacity(Scope scope, FieldDefinition stateField, BytecodeBlock block) {
    Variable groupIdsBlock = scope.getVariable("groupIdsBlock");
    BytecodeExpression state = scope.getThis().getField(stateField);
    block.append(state.invoke("ensureCapacity", void.class, groupIdsBlock.invoke("getGroupCount", long.class)));
}
Also used : Variable(com.facebook.presto.bytecode.Variable) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 5 with BytecodeExpression

use of com.facebook.presto.bytecode.expression.BytecodeExpression in project presto by prestodb.

the class AccumulatorCompiler method generateGroupedEvaluateFinal.

private static void generateGroupedEvaluateFinal(ClassDefinition definition, FieldDefinition stateField, MethodHandle outputFunction, CallSiteBinder callSiteBinder) {
    Parameter groupId = arg("groupId", int.class);
    Parameter out = arg("out", BlockBuilder.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC), "evaluateFinal", type(void.class), groupId, out);
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    BytecodeExpression state = thisVariable.getField(stateField);
    body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class)));
    body.comment("output(state, out)");
    body.append(state);
    body.append(out);
    body.append(invoke(callSiteBinder.bind(outputFunction), "output"));
    body.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) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Aggregations

BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)49 Variable (com.facebook.presto.bytecode.Variable)39 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)37 Parameter (com.facebook.presto.bytecode.Parameter)35 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)31 IfStatement (com.facebook.presto.bytecode.control.IfStatement)19 Scope (com.facebook.presto.bytecode.Scope)13 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)9 SqlTypeBytecodeExpression.constantType (com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType)9 ImmutableList (com.google.common.collect.ImmutableList)8 Type (com.facebook.presto.common.type.Type)7 ArrayList (java.util.ArrayList)7 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)6 Block (com.facebook.presto.common.block.Block)6 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)6 Block (com.facebook.presto.spi.block.Block)6 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)5 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)5 ForLoop (com.facebook.presto.bytecode.control.ForLoop)5 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)5