Search in sources :

Example 6 with RunLengthEncodedBlock

use of com.facebook.presto.common.block.RunLengthEncodedBlock in project presto by prestodb.

the class DoubleSelectiveStreamReader method getBlockView.

@Override
public BlockLease getBlockView(int[] positions, int positionCount) {
    checkArgument(outputPositionCount > 0, "outputPositionCount must be greater than zero");
    checkState(outputRequired, "This stream reader doesn't produce output");
    checkState(positionCount <= outputPositionCount, "Not enough values");
    checkState(!valuesInUse, "BlockLease hasn't been closed yet");
    if (allNulls) {
        return newLease(new RunLengthEncodedBlock(NULL_BLOCK, positionCount));
    }
    boolean includeNulls = nullsAllowed && presentStream != null;
    if (positionCount != outputPositionCount) {
        compactValues(positions, positionCount, includeNulls);
    }
    return newLease(new LongArrayBlock(positionCount, Optional.ofNullable(includeNulls ? nulls : null), values));
}
Also used : LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock)

Example 7 with RunLengthEncodedBlock

use of com.facebook.presto.common.block.RunLengthEncodedBlock in project presto by prestodb.

the class FloatSelectiveStreamReader method getBlockView.

@Override
public BlockLease getBlockView(int[] positions, int positionCount) {
    checkArgument(outputPositionCount > 0, "outputPositionCount must be greater than zero");
    checkState(outputRequired, "This stream reader doesn't produce output");
    checkState(positionCount <= outputPositionCount, "Not enough values");
    checkState(!valuesInUse, "BlockLease hasn't been closed yet");
    if (allNulls) {
        return newLease(new RunLengthEncodedBlock(NULL_BLOCK, positionCount));
    }
    boolean includeNulls = nullsAllowed && presentStream != null;
    if (positionCount != outputPositionCount) {
        compactValues(positions, positionCount, includeNulls);
    }
    return newLease(new IntArrayBlock(positionCount, Optional.ofNullable(includeNulls ? nulls : null), values));
}
Also used : IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock)

Example 8 with RunLengthEncodedBlock

use of com.facebook.presto.common.block.RunLengthEncodedBlock in project presto by prestodb.

the class ListSelectiveStreamReader method getBlock.

@Override
public Block getBlock(int[] positions, int positionCount) {
    checkArgument(outputPositionCount > 0, "outputPositionCount must be greater than zero");
    checkState(outputRequired, "This stream reader doesn't produce output");
    checkState(positionCount <= outputPositionCount, "Not enough values: " + outputPositionCount + ", " + positionCount);
    checkState(!valuesInUse, "BlockLease hasn't been closed yet");
    if (allNulls) {
        return new RunLengthEncodedBlock(outputType.createBlockBuilder(null, 1).appendNull().build(), positionCount);
    }
    boolean mayHaveNulls = nullsAllowed && presentStream != null;
    if (positionCount == outputPositionCount) {
        Block block = ArrayBlock.fromElementBlock(positionCount, Optional.ofNullable(mayHaveNulls ? nulls : null), offsets, makeElementBlock());
        nulls = null;
        offsets = null;
        return block;
    }
    int[] offsetsCopy = new int[positionCount + 1];
    boolean[] nullsCopy = null;
    if (mayHaveNulls) {
        nullsCopy = new boolean[positionCount];
    }
    elementOutputPositionCount = 0;
    int positionIndex = 0;
    int nextPosition = positions[positionIndex];
    int skippedElements = 0;
    for (int i = 0; i < outputPositionCount; i++) {
        if (outputPositions[i] < nextPosition) {
            skippedElements += offsets[i + 1] - offsets[i];
            continue;
        }
        assert outputPositions[i] == nextPosition;
        offsetsCopy[positionIndex] = this.offsets[i] - skippedElements;
        if (nullsCopy != null) {
            nullsCopy[positionIndex] = this.nulls[i];
        }
        for (int j = 0; j < offsets[i + 1] - offsets[i]; j++) {
            elementOutputPositions[elementOutputPositionCount] = elementOutputPositions[elementOutputPositionCount + skippedElements];
            elementOutputPositionCount++;
        }
        positionIndex++;
        if (positionIndex >= positionCount) {
            offsetsCopy[positionCount] = this.offsets[i + 1] - skippedElements;
            break;
        }
        nextPosition = positions[positionIndex];
    }
    return ArrayBlock.fromElementBlock(positionCount, Optional.ofNullable(nullsCopy), offsetsCopy, makeElementBlock());
}
Also used : RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) ArrayBlock(com.facebook.presto.common.block.ArrayBlock) Block(com.facebook.presto.common.block.Block) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock)

Example 9 with RunLengthEncodedBlock

use of com.facebook.presto.common.block.RunLengthEncodedBlock in project presto by prestodb.

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);
    BytecodeNode forLoop = 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);
    for (int i = 0; i < parameterVariables.size(); i++) {
        if (!nullable.get(i)) {
            Variable variableDefinition = parameterVariables.get(i);
            forLoop = new IfStatement("if(!(%s instanceof RunLengthEncodedBlock && %s.isNull(0)))", variableDefinition.getName(), variableDefinition.getName()).condition(and(variableDefinition.instanceOf(RunLengthEncodedBlock.class), variableDefinition.invoke("isNull", boolean.class, constantInt(0)))).ifFalse(forLoop);
        }
    }
    // Skip input blocks that eliminate all input positions
    forLoop = new IfStatement("if(!(%s instanceof RunLengthEncodedBlock && !testMask(%s, 0)))", masksBlock.getName(), masksBlock.getName()).condition(and(masksBlock.instanceOf(RunLengthEncodedBlock.class), not(invokeStatic(type(CompilerOperations.class), "testMask", type(boolean.class), ImmutableList.of(type(Block.class), type(int.class)), masksBlock, constantInt(0))))).ifFalse(forLoop);
    block.append(new IfStatement("if(%s > 0)", rowsVariable.getName()).condition(new BytecodeBlock().getVariable(rowsVariable).push(0).invokeStatic(CompilerOperations.class, "greaterThan", boolean.class, int.class, int.class)).ifTrue(forLoop));
    return block;
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) ForLoop(com.facebook.presto.bytecode.control.ForLoop) CompilerOperations(com.facebook.presto.sql.gen.CompilerOperations) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ArrayList(java.util.ArrayList) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) GroupByIdBlock(com.facebook.presto.operator.GroupByIdBlock) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Block(com.facebook.presto.common.block.Block) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode) ParameterMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata)

Example 10 with RunLengthEncodedBlock

use of com.facebook.presto.common.block.RunLengthEncodedBlock in project presto by prestodb.

the class TestApproximatePercentileAggregation method createRLEBlock.

private static RunLengthEncodedBlock createRLEBlock(Iterable<Double> percentiles, int positionCount) {
    BlockBuilder rleBlockBuilder = new ArrayType(DOUBLE).createBlockBuilder(null, 1);
    BlockBuilder arrayBlockBuilder = rleBlockBuilder.beginBlockEntry();
    for (double percentile : percentiles) {
        DOUBLE.writeDouble(arrayBlockBuilder, percentile);
    }
    rleBlockBuilder.closeEntry();
    return new RunLengthEncodedBlock(rleBlockBuilder.build(), positionCount);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Aggregations

RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)50 Block (com.facebook.presto.common.block.Block)27 Page (com.facebook.presto.common.Page)14 Test (org.testng.annotations.Test)9 DictionaryBlock (com.facebook.presto.common.block.DictionaryBlock)7 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)6 LongArrayBlock (com.facebook.presto.common.block.LongArrayBlock)6 PrestoException (com.facebook.presto.spi.PrestoException)6 ByteArrayBlock (com.facebook.presto.common.block.ByteArrayBlock)5 Slice (io.airlift.slice.Slice)5 IOException (java.io.IOException)4 ArrayBlock (com.facebook.presto.common.block.ArrayBlock)3 LazyBlock (com.facebook.presto.common.block.LazyBlock)3 RowBlockBuilder (com.facebook.presto.common.block.RowBlockBuilder)3 BlockAssertions.createLongSequenceBlock (com.facebook.presto.block.BlockAssertions.createLongSequenceBlock)2 BlockLease (com.facebook.presto.common.block.BlockLease)2 IntArrayBlock (com.facebook.presto.common.block.IntArrayBlock)2 VariableWidthBlock (com.facebook.presto.common.block.VariableWidthBlock)2 Type (com.facebook.presto.common.type.Type)2 DriverYieldSignal (com.facebook.presto.operator.DriverYieldSignal)2