Search in sources :

Example 26 with PageBuilder

use of com.facebook.presto.spi.PageBuilder in project presto by prestodb.

the class PageProcessorCompiler method generateProcessColumnarMethod.

private static void generateProcessColumnarMethod(ClassDefinition classDefinition, List<RowExpression> projections, List<MethodDefinition> projectColumnarMethods) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter page = arg("page", Page.class);
    Parameter types = arg("types", List.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "processColumnar", type(Page.class), session, page, types);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    Variable selectedPositions = scope.declareVariable("selectedPositions", body, thisVariable.invoke("filterPage", int[].class, session, page));
    Variable cardinality = scope.declareVariable("cardinality", body, selectedPositions.length());
    body.comment("if no rows selected return null").append(new IfStatement().condition(equal(cardinality, constantInt(0))).ifTrue(constantNull(Page.class).ret()));
    if (projections.isEmpty()) {
        // if no projections, return new page with selected rows
        body.append(newInstance(Page.class, cardinality, newArray(type(Block[].class), 0)).ret());
        return;
    }
    Variable pageBuilder = scope.declareVariable("pageBuilder", body, newInstance(PageBuilder.class, cardinality, types));
    Variable outputBlocks = scope.declareVariable("outputBlocks", body, newArray(type(Block[].class), projections.size()));
    for (int projectionIndex = 0; projectionIndex < projections.size(); projectionIndex++) {
        List<BytecodeExpression> params = ImmutableList.<BytecodeExpression>builder().add(session).add(page).add(selectedPositions).add(pageBuilder).add(constantInt(projectionIndex)).build();
        body.append(outputBlocks.setElement(projectionIndex, thisVariable.invoke(projectColumnarMethods.get(projectionIndex), params)));
    }
    // create new page from outputBlocks
    body.append(newInstance(Page.class, cardinality, outputBlocks).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) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) 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) Page(com.facebook.presto.spi.Page) PageBuilder(com.facebook.presto.spi.PageBuilder) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 27 with PageBuilder

use of com.facebook.presto.spi.PageBuilder in project presto by prestodb.

the class PageProcessorCompiler method generateProcessColumnarDictionaryMethod.

private static void generateProcessColumnarDictionaryMethod(ClassDefinition classDefinition, List<RowExpression> projections, List<MethodDefinition> projectDictionaryMethods) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter page = arg("page", Page.class);
    Parameter types = arg("types", List.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "processColumnarDictionary", type(Page.class), session, page, types);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    Variable selectedPositions = scope.declareVariable("selectedPositions", body, thisVariable.invoke("filterPage", int[].class, session, page));
    Variable cardinality = scope.declareVariable("cardinality", body, selectedPositions.length());
    Variable dictionarySourceIds = scope.declareVariable(type(Map.class, DictionaryId.class, DictionaryId.class), "dictionarySourceIds");
    body.append(dictionarySourceIds.set(newInstance(type(HashMap.class, DictionaryId.class, DictionaryId.class))));
    body.comment("if no rows selected return null").append(new IfStatement().condition(equal(cardinality, constantInt(0))).ifTrue(constantNull(Page.class).ret()));
    if (projections.isEmpty()) {
        // if no projections, return new page with selected rows
        body.append(newInstance(Page.class, cardinality, newArray(type(Block[].class), 0)).ret());
        return;
    }
    // create PageBuilder
    Variable pageBuilder = scope.declareVariable("pageBuilder", body, newInstance(PageBuilder.class, cardinality, types));
    body.append(page.set(thisVariable.invoke("getNonLazyPage", Page.class, page)));
    // create outputBlocks
    Variable outputBlocks = scope.declareVariable("outputBlocks", body, newArray(type(Block[].class), projections.size()));
    for (int projectionIndex = 0; projectionIndex < projections.size(); projectionIndex++) {
        List<BytecodeExpression> params = ImmutableList.<BytecodeExpression>builder().add(session).add(page).add(selectedPositions).add(pageBuilder).add(constantInt(projectionIndex)).add(dictionarySourceIds).build();
        body.append(outputBlocks.setElement(projectionIndex, thisVariable.invoke(projectDictionaryMethods.get(projectionIndex), params)));
    }
    body.append(newInstance(Page.class, cardinality, outputBlocks).ret());
}
Also used : Variable(com.facebook.presto.bytecode.Variable) HashMap(java.util.HashMap) DictionaryId(com.facebook.presto.spi.block.DictionaryId) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Page(com.facebook.presto.spi.Page) PageBuilder(com.facebook.presto.spi.PageBuilder) IfStatement(com.facebook.presto.bytecode.control.IfStatement) 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) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 28 with PageBuilder

use of com.facebook.presto.spi.PageBuilder in project presto by prestodb.

the class JoinCompiler method generateAppendToMethod.

private static void generateAppendToMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> types, List<Integer> outputChannels, List<FieldDefinition> channelFields) {
    Parameter blockIndex = arg("blockIndex", int.class);
    Parameter blockPosition = arg("blockPosition", int.class);
    Parameter pageBuilder = arg("pageBuilder", PageBuilder.class);
    Parameter outputChannelOffset = arg("outputChannelOffset", int.class);
    MethodDefinition appendToMethod = classDefinition.declareMethod(a(PUBLIC), "appendTo", type(void.class), blockIndex, blockPosition, pageBuilder, outputChannelOffset);
    Variable thisVariable = appendToMethod.getThis();
    BytecodeBlock appendToBody = appendToMethod.getBody();
    int pageBuilderOutputChannel = 0;
    for (int outputChannel : outputChannels) {
        Type type = types.get(outputChannel);
        BytecodeExpression typeExpression = constantType(callSiteBinder, type);
        BytecodeExpression block = thisVariable.getField(channelFields.get(outputChannel)).invoke("get", Object.class, blockIndex).cast(Block.class);
        appendToBody.comment("%s.appendTo(channel_%s.get(outputChannel), blockPosition, pageBuilder.getBlockBuilder(outputChannelOffset + %s));", type.getClass(), outputChannel, pageBuilderOutputChannel).append(typeExpression).append(block).append(blockPosition).append(pageBuilder).append(outputChannelOffset).push(pageBuilderOutputChannel++).append(OpCode.IADD).invokeVirtual(PageBuilder.class, "getBlockBuilder", BlockBuilder.class, int.class).invokeInterface(Type.class, "appendTo", void.class, Block.class, int.class, BlockBuilder.class);
    }
    appendToBody.ret();
}
Also used : 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) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) PageBuilder(com.facebook.presto.spi.PageBuilder) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder)

Example 29 with PageBuilder

use of com.facebook.presto.spi.PageBuilder in project presto by prestodb.

the class TestDoubleHistogramAggregation method makeInput.

private static Page makeInput(int numberOfBuckets) {
    PageBuilder builder = new PageBuilder(ImmutableList.of(BIGINT, DOUBLE, DOUBLE));
    for (int i = 0; i < 100; i++) {
        builder.declarePosition();
        BIGINT.writeLong(builder.getBlockBuilder(0), numberOfBuckets);
        // value
        DOUBLE.writeDouble(builder.getBlockBuilder(1), i);
        // weight
        DOUBLE.writeDouble(builder.getBlockBuilder(2), 1);
    }
    return builder.build();
}
Also used : PageBuilder(com.facebook.presto.spi.PageBuilder)

Example 30 with PageBuilder

use of com.facebook.presto.spi.PageBuilder in project presto by prestodb.

the class IndexSnapshotBuilder method createIndexSnapshot.

public IndexSnapshot createIndexSnapshot(UnloadedIndexKeyRecordSet indexKeysRecordSet) {
    checkArgument(indexKeysRecordSet.getColumnTypes().equals(missingKeysTypes), "indexKeysRecordSet must have same schema as missingKeys");
    checkState(!isMemoryExceeded(), "Max memory exceeded");
    for (Page page : pages) {
        outputPagesIndex.addPage(page);
    }
    pages.clear();
    LookupSource lookupSource = outputPagesIndex.createLookupSourceSupplier(session, keyOutputChannels, keyOutputHashChannel, Optional.empty()).get();
    // Build a page containing the keys that produced no output rows, so in future requests can skip these keys
    PageBuilder missingKeysPageBuilder = new PageBuilder(missingKeysIndex.getTypes());
    UnloadedIndexKeyRecordCursor indexKeysRecordCursor = indexKeysRecordSet.cursor();
    while (indexKeysRecordCursor.advanceNextPosition()) {
        Block[] blocks = indexKeysRecordCursor.getBlocks();
        Page page = indexKeysRecordCursor.getPage();
        int position = indexKeysRecordCursor.getPosition();
        if (lookupSource.getJoinPosition(position, page, page) < 0) {
            missingKeysPageBuilder.declarePosition();
            for (int i = 0; i < blocks.length; i++) {
                Block block = blocks[i];
                Type type = indexKeysRecordCursor.getType(i);
                type.appendTo(block, position, missingKeysPageBuilder.getBlockBuilder(i));
            }
        }
    }
    Page missingKeysPage = missingKeysPageBuilder.build();
    memoryInBytes += missingKeysPage.getSizeInBytes();
    if (isMemoryExceeded()) {
        return null;
    }
    // only update missing keys if we have new missing keys
    if (!missingKeysPageBuilder.isEmpty()) {
        missingKeysIndex.addPage(missingKeysPage);
        missingKeys = missingKeysIndex.createLookupSourceSupplier(session, missingKeysChannels).get();
    }
    return new IndexSnapshot(lookupSource, missingKeys);
}
Also used : Type(com.facebook.presto.spi.type.Type) LookupSource(com.facebook.presto.operator.LookupSource) Block(com.facebook.presto.spi.block.Block) Page(com.facebook.presto.spi.Page) PageBuilder(com.facebook.presto.spi.PageBuilder) UnloadedIndexKeyRecordCursor(com.facebook.presto.operator.index.UnloadedIndexKeyRecordSet.UnloadedIndexKeyRecordCursor)

Aggregations

PageBuilder (com.facebook.presto.spi.PageBuilder)45 Page (com.facebook.presto.spi.Page)23 Block (com.facebook.presto.spi.block.Block)16 Type (com.facebook.presto.spi.type.Type)15 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)14 ImmutableList (com.google.common.collect.ImmutableList)12 Test (org.testng.annotations.Test)10 List (java.util.List)7 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)4 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)4 Parameter (com.facebook.presto.bytecode.Parameter)4 Variable (com.facebook.presto.bytecode.Variable)4 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)4 LazyBlock (com.facebook.presto.spi.block.LazyBlock)4 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)4 ArrayList (java.util.ArrayList)4 SequencePageBuilder (com.facebook.presto.SequencePageBuilder)3 Scope (com.facebook.presto.bytecode.Scope)3 IfStatement (com.facebook.presto.bytecode.control.IfStatement)3