Search in sources :

Example 96 with Variable

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

the class AccumulatorCompiler method generateEnsureCapacity.

private static void generateEnsureCapacity(Scope scope, List<FieldDefinition> stateFields, BytecodeBlock block) {
    Variable groupIdsBlock = scope.getVariable("groupIdsBlock");
    for (FieldDefinition stateField : stateFields) {
        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) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 97 with Variable

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

the class AccumulatorCompiler method generateAddIntermediateAsCombine.

private static void generateAddIntermediateAsCombine(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors, List<Class> lambdaInterfaces, List<FieldDefinition> lambdaProviderFields, MethodHandle combineFunction, CallSiteBinder callSiteBinder, boolean grouped) {
    MethodDefinition method = declareAddIntermediate(definition, grouped);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    int stateCount = stateFieldAndDescriptors.size();
    List<Variable> scratchStates = new ArrayList<>();
    for (int i = 0; i < stateCount; i++) {
        Class<?> scratchStateClass = stateFieldAndDescriptors.get(i).getStateDescriptor().getFactory().getSingleStateClass();
        scratchStates.add(scope.declareVariable(scratchStateClass, "scratchState_" + i));
    }
    List<Variable> block;
    if (stateCount == 1) {
        block = ImmutableList.of(scope.getVariable("block"));
    } else {
        // ColumnarRow is used to get the column blocks represents each state, this allows to
        // 1. handle single state and multiple states in a unified way
        // 2. avoid the cost of constructing SingleRowBlock for each group
        Variable columnarRow = scope.declareVariable(ColumnarRow.class, "columnarRow");
        body.append(columnarRow.set(invokeStatic(ColumnarRow.class, "toColumnarRow", ColumnarRow.class, scope.getVariable("block"))));
        block = new ArrayList<>();
        for (int i = 0; i < stateCount; i++) {
            Variable columnBlock = scope.declareVariable(Block.class, "columnBlock_" + i);
            body.append(columnBlock.set(columnarRow.invoke("getField", Block.class, constantInt(i))));
            block.add(columnBlock);
        }
    }
    Variable position = scope.declareVariable(int.class, "position");
    for (int i = 0; i < stateCount; i++) {
        FieldDefinition stateFactoryField = stateFieldAndDescriptors.get(i).getStateFactoryField();
        body.comment(format("scratchState_%s = stateFactory[%s].createSingleState();", i, i)).append(thisVariable.getField(stateFactoryField)).invokeInterface(AccumulatorStateFactory.class, "createSingleState", Object.class).checkCast(scratchStates.get(i).getType()).putVariable(scratchStates.get(i));
    }
    List<FieldDefinition> stateFields = stateFieldAndDescriptors.stream().map(StateFieldAndDescriptor::getStateField).collect(toImmutableList());
    if (grouped) {
        generateEnsureCapacity(scope, stateFields, body);
    }
    BytecodeBlock loopBody = new BytecodeBlock();
    loopBody.comment("combine(state_0, state_1, ... scratchState_0, scratchState_1, ... lambda_0, lambda_1, ...)");
    for (FieldDefinition stateField : stateFields) {
        if (grouped) {
            Variable groupIdsBlock = scope.getVariable("groupIdsBlock");
            loopBody.append(thisVariable.getField(stateField).invoke("setGroupId", void.class, groupIdsBlock.invoke("getGroupId", long.class, position)));
        }
        loopBody.append(thisVariable.getField(stateField));
    }
    for (int i = 0; i < stateCount; i++) {
        FieldDefinition stateSerializerField = stateFieldAndDescriptors.get(i).getStateSerializerField();
        loopBody.append(thisVariable.getField(stateSerializerField).invoke("deserialize", void.class, block.get(i), position, scratchStates.get(i).cast(Object.class)));
        loopBody.append(scratchStates.get(i));
    }
    for (int i = 0; i < lambdaInterfaces.size(); i++) {
        loopBody.append(scope.getThis().getField(lambdaProviderFields.get(i)).invoke("getLambda", Object.class).cast(lambdaInterfaces.get(i)));
    }
    loopBody.append(invoke(callSiteBinder.bind(combineFunction), "combine"));
    if (grouped) {
        // skip rows with null group id
        IfStatement ifStatement = new IfStatement("if (!groupIdsBlock.isNull(position))").condition(not(scope.getVariable("groupIdsBlock").invoke("isNull", boolean.class, position))).ifTrue(loopBody);
        loopBody = new BytecodeBlock().append(ifStatement);
    }
    body.append(generateBlockNonNullPositionForLoop(scope, position, loopBody)).ret();
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ArrayList(java.util.ArrayList)

Example 98 with Variable

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

the class AccumulatorCompiler method generateAddInputWindowIndex.

private static void generateAddInputWindowIndex(ClassDefinition definition, List<FieldDefinition> stateField, List<ParameterMetadata> parameterMetadatas, List<Class> lambdaInterfaces, List<FieldDefinition> lambdaProviderFields, MethodHandle inputFunction, CallSiteBinder callSiteBinder) {
    // TODO: implement masking based on maskChannel field once Window Functions support DISTINCT arguments to the functions.
    Parameter index = arg("index", WindowIndex.class);
    Parameter channels = arg("channels", type(List.class, Integer.class));
    Parameter startPosition = arg("startPosition", int.class);
    Parameter endPosition = arg("endPosition", int.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC), "addInput", type(void.class), ImmutableList.of(index, channels, startPosition, endPosition));
    Scope scope = method.getScope();
    Variable position = scope.declareVariable(int.class, "position");
    Binding binding = callSiteBinder.bind(inputFunction);
    BytecodeExpression invokeInputFunction = invokeDynamic(BOOTSTRAP_METHOD, ImmutableList.of(binding.getBindingId()), "input", binding.getType(), getInvokeFunctionOnWindowIndexParameters(scope, inputFunction.type().parameterArray(), parameterMetadatas, lambdaInterfaces, lambdaProviderFields, stateField, index, channels, position));
    method.getBody().append(new ForLoop().initialize(position.set(startPosition)).condition(BytecodeExpressions.lessThanOrEqual(position, endPosition)).update(position.increment()).body(new IfStatement().condition(anyParametersAreNull(parameterMetadatas, index, channels, position)).ifFalse(invokeInputFunction))).ret();
}
Also used : Binding(com.facebook.presto.bytecode.Binding) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) ForLoop(com.facebook.presto.bytecode.control.ForLoop) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 99 with Variable

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

the class AccumulatorCompiler method generateSetGroupIdFromGroupIdsBlock.

private static void generateSetGroupIdFromGroupIdsBlock(Scope scope, List<FieldDefinition> stateFields, BytecodeBlock block) {
    Variable groupIdsBlock = scope.getVariable("groupIdsBlock");
    Variable position = scope.getVariable("position");
    for (FieldDefinition stateField : stateFields) {
        BytecodeExpression state = scope.getThis().getField(stateField);
        block.append(state.invoke("setGroupId", void.class, groupIdsBlock.invoke("getGroupId", long.class, position)));
    }
}
Also used : Variable(com.facebook.presto.bytecode.Variable) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 100 with Variable

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

the class CursorProcessorCompiler method generateProjectMethod.

private void generateProjectMethod(ClassDefinition classDefinition, RowExpressionCompiler compiler, String methodName, RowExpression projection) {
    Parameter properties = arg("properties", SqlFunctionProperties.class);
    Parameter cursor = arg("cursor", RecordCursor.class);
    Parameter output = arg("output", BlockBuilder.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(void.class), properties, cursor, output);
    method.comment("Projection: %s", projection.toString());
    Scope scope = method.getScope();
    Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull");
    method.getBody().comment("boolean wasNull = false;").putVariable(wasNullVariable, false).comment("evaluate projection: " + projection.toString()).append(compiler.compile(projection, scope, Optional.of(output))).ret();
}
Also used : Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) 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