Search in sources :

Example 1 with LookupSource

use of com.facebook.presto.operator.LookupSource in project presto by prestodb.

the class TestJoinProbeCompiler method testSingleChannel.

@Test(dataProvider = "hashEnabledValues")
public void testSingleChannel(boolean hashEnabled) throws Exception {
    taskContext.addPipelineContext(0, true, true).addDriverContext();
    ImmutableList<Type> types = ImmutableList.of(VARCHAR, DOUBLE);
    ImmutableList<Type> outputTypes = ImmutableList.of(VARCHAR);
    List<Integer> outputChannels = ImmutableList.of(0);
    LookupSourceSupplierFactory lookupSourceSupplierFactory = joinCompiler.compileLookupSourceFactory(types, Ints.asList(0));
    // crate hash strategy with a single channel blocks -- make sure there is some overlap in values
    List<Block> varcharChannel = ImmutableList.of(BlockAssertions.createStringSequenceBlock(10, 20), BlockAssertions.createStringSequenceBlock(20, 30), BlockAssertions.createStringSequenceBlock(15, 25));
    List<Block> extraUnusedDoubleChannel = ImmutableList.of(BlockAssertions.createDoubleSequenceBlock(10, 20), BlockAssertions.createDoubleSequenceBlock(20, 30), BlockAssertions.createDoubleSequenceBlock(15, 25));
    LongArrayList addresses = new LongArrayList();
    for (int blockIndex = 0; blockIndex < varcharChannel.size(); blockIndex++) {
        Block block = varcharChannel.get(blockIndex);
        for (int positionIndex = 0; positionIndex < block.getPositionCount(); positionIndex++) {
            addresses.add(encodeSyntheticAddress(blockIndex, positionIndex));
        }
    }
    Optional<Integer> hashChannel = Optional.empty();
    List<List<Block>> channels = ImmutableList.of(varcharChannel, extraUnusedDoubleChannel);
    if (hashEnabled) {
        ImmutableList.Builder<Block> hashChannelBuilder = ImmutableList.builder();
        for (Block block : varcharChannel) {
            hashChannelBuilder.add(TypeUtils.getHashBlock(ImmutableList.<Type>of(VARCHAR), block));
        }
        types = ImmutableList.of(VARCHAR, DOUBLE, BigintType.BIGINT);
        hashChannel = Optional.of(2);
        channels = ImmutableList.of(varcharChannel, extraUnusedDoubleChannel, hashChannelBuilder.build());
        outputChannels = ImmutableList.of(0, 2);
        outputTypes = ImmutableList.of(VARCHAR, BigintType.BIGINT);
    }
    LookupSource lookupSource = lookupSourceSupplierFactory.createLookupSourceSupplier(taskContext.getSession().toConnectorSession(), addresses, channels, hashChannel, Optional.empty()).get();
    JoinProbeCompiler joinProbeCompiler = new JoinProbeCompiler();
    JoinProbeFactory probeFactory = joinProbeCompiler.internalCompileJoinProbe(types, outputChannels, Ints.asList(0), hashChannel);
    Page page = SequencePageBuilder.createSequencePage(types, 10, 10, 10);
    Page outputPage = new Page(page.getBlock(0));
    if (hashEnabled) {
        page = new Page(page.getBlock(0), page.getBlock(1), TypeUtils.getHashBlock(ImmutableList.of(VARCHAR), page.getBlock(0)));
        outputPage = new Page(page.getBlock(0), page.getBlock(2));
    }
    JoinProbe joinProbe = probeFactory.createJoinProbe(lookupSource, page);
    // verify channel count
    assertEquals(joinProbe.getOutputChannelCount(), outputChannels.size());
    PageBuilder pageBuilder = new PageBuilder(outputTypes);
    for (int position = 0; position < page.getPositionCount(); position++) {
        assertTrue(joinProbe.advanceNextPosition());
        pageBuilder.declarePosition();
        joinProbe.appendTo(pageBuilder);
        assertEquals(joinProbe.getCurrentJoinPosition(), lookupSource.getJoinPosition(position, page, page));
    }
    assertFalse(joinProbe.advanceNextPosition());
    assertPageEquals(outputTypes, pageBuilder.build(), outputPage);
}
Also used : LookupSource(com.facebook.presto.operator.LookupSource) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ImmutableList(com.google.common.collect.ImmutableList) Page(com.facebook.presto.spi.Page) SequencePageBuilder(com.facebook.presto.SequencePageBuilder) PageBuilder(com.facebook.presto.spi.PageBuilder) JoinProbe(com.facebook.presto.operator.JoinProbe) Type(com.facebook.presto.spi.type.Type) BigintType(com.facebook.presto.spi.type.BigintType) JoinProbeFactory(com.facebook.presto.operator.JoinProbeFactory) Block(com.facebook.presto.spi.block.Block) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) LookupSourceSupplierFactory(com.facebook.presto.sql.gen.JoinCompiler.LookupSourceSupplierFactory) Test(org.testng.annotations.Test)

Example 2 with LookupSource

use of com.facebook.presto.operator.LookupSource in project presto by prestodb.

the class JoinProbeCompiler method internalCompileJoinOperatorFactory.

@VisibleForTesting
public HashJoinOperatorFactoryFactory internalCompileJoinOperatorFactory(List<Type> types, List<Integer> probeOutputChannels, List<Integer> probeJoinChannel, Optional<Integer> probeHashChannel) {
    Class<? extends JoinProbe> joinProbeClass = compileJoinProbe(types, probeOutputChannels, probeJoinChannel, probeHashChannel);
    ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("JoinProbeFactory"), type(Object.class), type(JoinProbeFactory.class));
    classDefinition.declareDefaultConstructor(a(PUBLIC));
    Parameter lookupSource = arg("lookupSource", LookupSource.class);
    Parameter page = arg("page", Page.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "createJoinProbe", type(JoinProbe.class), lookupSource, page);
    method.getBody().newObject(joinProbeClass).dup().append(lookupSource).append(page).invokeConstructor(joinProbeClass, LookupSource.class, Page.class).retObject();
    DynamicClassLoader classLoader = new DynamicClassLoader(joinProbeClass.getClassLoader());
    JoinProbeFactory joinProbeFactory;
    if (probeJoinChannel.isEmpty()) {
        // see comment in PagesIndex#createLookupSource
        joinProbeFactory = new SimpleJoinProbe.SimpleJoinProbeFactory(types, probeOutputChannels, probeJoinChannel, probeHashChannel);
    } else {
        Class<? extends JoinProbeFactory> joinProbeFactoryClass = defineClass(classDefinition, JoinProbeFactory.class, classLoader);
        try {
            joinProbeFactory = joinProbeFactoryClass.newInstance();
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    }
    Class<? extends OperatorFactory> operatorFactoryClass = IsolatedClass.isolateClass(classLoader, OperatorFactory.class, LookupJoinOperatorFactory.class, LookupJoinOperator.class);
    return new HashJoinOperatorFactoryFactory(joinProbeFactory, operatorFactoryClass);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) LookupSource(com.facebook.presto.operator.LookupSource) SimpleJoinProbe(com.facebook.presto.operator.SimpleJoinProbe) Page(com.facebook.presto.spi.Page) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) JoinProbe(com.facebook.presto.operator.JoinProbe) SimpleJoinProbe(com.facebook.presto.operator.SimpleJoinProbe) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) JoinProbeFactory(com.facebook.presto.operator.JoinProbeFactory) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with LookupSource

use of com.facebook.presto.operator.LookupSource 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(), Optional.empty(), ImmutableList.of()).get();
    // Build a page containing the keys that produced no output rows, so in future requests can skip these keys
    verify(missingKeysPageBuilder.isEmpty());
    UnloadedIndexKeyRecordCursor indexKeysRecordCursor = indexKeysRecordSet.cursor();
    while (indexKeysRecordCursor.advanceNextPosition()) {
        Page page = indexKeysRecordCursor.getPage();
        int position = indexKeysRecordCursor.getPosition();
        if (lookupSource.getJoinPosition(position, page, page) < 0) {
            missingKeysPageBuilder.declarePosition();
            for (int i = 0; i < page.getChannelCount(); i++) {
                Block block = page.getBlock(i);
                Type type = indexKeysRecordCursor.getType(i);
                type.appendTo(block, position, missingKeysPageBuilder.getBlockBuilder(i));
            }
        }
    }
    Page missingKeysPage = missingKeysPageBuilder.build();
    if (!missingKeysPageBuilder.isEmpty()) {
        missingKeysPageBuilder.reset();
    }
    memoryInBytes += missingKeysPage.getSizeInBytes();
    if (isMemoryExceeded()) {
        return null;
    }
    // only update missing keys if we have new missing keys
    if (missingKeysPage.getPositionCount() != 0) {
        missingKeysIndex.addPage(missingKeysPage);
        missingKeys = missingKeysIndex.createLookupSourceSupplier(session, missingKeysChannels).get();
    }
    return new IndexSnapshot(lookupSource, missingKeys);
}
Also used : Type(com.facebook.presto.common.type.Type) LookupSource(com.facebook.presto.operator.LookupSource) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page) UnloadedIndexKeyRecordCursor(com.facebook.presto.operator.index.UnloadedIndexKeyRecordSet.UnloadedIndexKeyRecordCursor)

Aggregations

LookupSource (com.facebook.presto.operator.LookupSource)3 JoinProbe (com.facebook.presto.operator.JoinProbe)2 JoinProbeFactory (com.facebook.presto.operator.JoinProbeFactory)2 Page (com.facebook.presto.spi.Page)2 SequencePageBuilder (com.facebook.presto.SequencePageBuilder)1 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)1 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)1 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)1 Parameter (com.facebook.presto.bytecode.Parameter)1 Page (com.facebook.presto.common.Page)1 Block (com.facebook.presto.common.block.Block)1 Type (com.facebook.presto.common.type.Type)1 SimpleJoinProbe (com.facebook.presto.operator.SimpleJoinProbe)1 UnloadedIndexKeyRecordCursor (com.facebook.presto.operator.index.UnloadedIndexKeyRecordSet.UnloadedIndexKeyRecordCursor)1 PageBuilder (com.facebook.presto.spi.PageBuilder)1 Block (com.facebook.presto.spi.block.Block)1 BigintType (com.facebook.presto.spi.type.BigintType)1 Type (com.facebook.presto.spi.type.Type)1 LookupSourceSupplierFactory (com.facebook.presto.sql.gen.JoinCompiler.LookupSourceSupplierFactory)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1