Search in sources :

Example 1 with HashGenerator

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

the class TypeUtils method getHashPosition.

public static long getHashPosition(List<? extends Type> hashTypes, Block[] hashBlocks, int position) {
    HashGenerator hashGenerator = InterpretedHashGenerator.createPositionalWithTypes(ImmutableList.copyOf(hashTypes));
    Page page = new Page(hashBlocks);
    return hashGenerator.hashPosition(position, page);
}
Also used : Page(com.facebook.presto.common.Page) HashGenerator(com.facebook.presto.operator.HashGenerator) InterpretedHashGenerator(com.facebook.presto.operator.InterpretedHashGenerator)

Example 2 with HashGenerator

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

the class TypeUtils method getHashBlock.

public static Block getHashBlock(List<? extends Type> hashTypes, Block... hashBlocks) {
    checkArgument(hashTypes.size() == hashBlocks.length);
    HashGenerator hashGenerator = InterpretedHashGenerator.createPositionalWithTypes(ImmutableList.copyOf(hashTypes));
    int positionCount = hashBlocks[0].getPositionCount();
    BlockBuilder builder = BIGINT.createFixedSizeBlockBuilder(positionCount);
    Page page = new Page(hashBlocks);
    for (int i = 0; i < positionCount; i++) {
        BIGINT.writeLong(builder, hashGenerator.hashPosition(i, page));
    }
    return builder.build();
}
Also used : Page(com.facebook.presto.common.Page) HashGenerator(com.facebook.presto.operator.HashGenerator) InterpretedHashGenerator(com.facebook.presto.operator.InterpretedHashGenerator) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 3 with HashGenerator

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

the class TestOptimizedPartitionedOutputOperator method testPartitioned.

private void testPartitioned(List<Type> types, List<Page> pages, DataSize maxMemory, List<Integer> partitionChannel, HashGenerator hashGenerator) {
    TestingPartitionedOutputBuffer outputBuffer = createPartitionedOutputBuffer();
    PartitionFunction partitionFunction = new LocalPartitionGenerator(hashGenerator, PARTITION_COUNT);
    OptimizedPartitionedOutputOperator operator = createOptimizedPartitionedOutputOperator(types, partitionChannel, partitionFunction, outputBuffer, OptionalInt.empty(), maxMemory);
    Map<Integer, List<Page>> expectedPageList = new HashMap<>();
    for (Page page : pages) {
        Map<Integer, List<Integer>> positionsByPartition = new HashMap<>();
        for (int i = 0; i < page.getPositionCount(); i++) {
            int partitionNumber = partitionFunction.getPartition(page, i);
            positionsByPartition.computeIfAbsent(partitionNumber, k -> new ArrayList<>()).add(i);
        }
        for (Map.Entry<Integer, List<Integer>> entry : positionsByPartition.entrySet()) {
            if (!entry.getValue().isEmpty()) {
                expectedPageList.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).add(copyPositions(page, entry.getValue()));
            }
        }
        operator.addInput(page);
    }
    operator.finish();
    Map<Integer, Page> expectedPages = Maps.transformValues(expectedPageList, outputPages -> mergePages(types, outputPages));
    Map<Integer, Page> actualPages = Maps.transformValues(outputBuffer.getPages(), outputPages -> mergePages(types, outputPages));
    assertEquals(actualPages.size(), expectedPages.size());
    assertEquals(actualPages.keySet(), expectedPages.keySet());
    for (Map.Entry<Integer, Page> entry : expectedPages.entrySet()) {
        int key = entry.getKey();
        assertPageEquals(types, actualPages.get(key), entry.getValue());
    }
}
Also used : PartitionFunction(com.facebook.presto.operator.PartitionFunction) OutputBuffers.createInitialEmptyOutputBuffers(com.facebook.presto.execution.buffer.OutputBuffers.createInitialEmptyOutputBuffers) Page(com.facebook.presto.common.Page) HashGenerator(com.facebook.presto.operator.HashGenerator) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) SerializedPage(com.facebook.presto.spi.page.SerializedPage) BlockAssertions.createRandomLongsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongsBlock) Test(org.testng.annotations.Test) Random(java.util.Random) DICTIONARY(com.facebook.presto.block.BlockAssertions.Encoding.DICTIONARY) Executors.newScheduledThreadPool(java.util.concurrent.Executors.newScheduledThreadPool) Map(java.util.Map) KILOBYTE(io.airlift.units.DataSize.Unit.KILOBYTE) Lifespan(com.facebook.presto.execution.Lifespan) LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) TINY_SCHEMA_NAME(com.facebook.presto.tpch.TpchMetadata.TINY_SCHEMA_NAME) Assertions.assertBetweenInclusive(com.facebook.airlift.testing.Assertions.assertBetweenInclusive) Threads.daemonThreadsNamed(com.facebook.airlift.concurrent.Threads.daemonThreadsNamed) DataSize(io.airlift.units.DataSize) List(java.util.List) StateMachine(com.facebook.presto.execution.StateMachine) BlockAssertions.createLongDictionaryBlock(com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) Optional(java.util.Optional) OutputPartitioning(com.facebook.presto.sql.planner.OutputPartitioning) BlockAssertions.createRandomStringBlock(com.facebook.presto.block.BlockAssertions.createRandomStringBlock) IntStream(java.util.stream.IntStream) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) PARTITIONED(com.facebook.presto.execution.buffer.OutputBuffers.BufferType.PARTITIONED) OutputBuffers(com.facebook.presto.execution.buffer.OutputBuffers) Slice(io.airlift.slice.Slice) PagesSerdeFactory(com.facebook.presto.execution.buffer.PagesSerdeFactory) RowType.withDefaultFieldNames(com.facebook.presto.common.type.RowType.withDefaultFieldNames) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) MEGABYTE(io.airlift.units.DataSize.Unit.MEGABYTE) PageAssertions.assertPageEquals(com.facebook.presto.operator.PageAssertions.assertPageEquals) Assert.assertEquals(org.testng.Assert.assertEquals) VariableWidthBlock(com.facebook.presto.common.block.VariableWidthBlock) HashMap(java.util.HashMap) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) Supplier(java.util.function.Supplier) PartitionedOutputBuffer(com.facebook.presto.execution.buffer.PartitionedOutputBuffer) REAL(com.facebook.presto.common.type.RealType.REAL) PageAssertions(com.facebook.presto.operator.PageAssertions) ArrayList(java.util.ArrayList) MAX_SHORT_PRECISION(com.facebook.presto.common.type.Decimals.MAX_SHORT_PRECISION) OptimizedPartitionedOutputFactory(com.facebook.presto.operator.repartition.OptimizedPartitionedOutputOperator.OptimizedPartitionedOutputFactory) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) PageAssertions.updateBlockTypesWithHashBlockAndNullBlock(com.facebook.presto.operator.PageAssertions.updateBlockTypesWithHashBlockAndNullBlock) GIGABYTE(io.airlift.units.DataSize.Unit.GIGABYTE) ImmutableList(com.google.common.collect.ImmutableList) BlockEncodingManager(com.facebook.presto.common.block.BlockEncodingManager) BlockAssertions.createRLEBlock(com.facebook.presto.block.BlockAssertions.createRLEBlock) SimpleLocalMemoryContext(com.facebook.presto.memory.context.SimpleLocalMemoryContext) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) ArrayType(com.facebook.presto.common.type.ArrayType) RUN_LENGTH(com.facebook.presto.block.BlockAssertions.Encoding.RUN_LENGTH) AggregatedMemoryContext.newSimpleAggregatedMemoryContext(com.facebook.presto.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext) TestingTaskContext(com.facebook.presto.testing.TestingTaskContext) Type(com.facebook.presto.common.type.Type) ExecutorService(java.util.concurrent.ExecutorService) PrecomputedHashGenerator(com.facebook.presto.operator.PrecomputedHashGenerator) BlockAssertions.createLongSequenceBlock(com.facebook.presto.block.BlockAssertions.createLongSequenceBlock) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) Executor(java.util.concurrent.Executor) Session(com.facebook.presto.Session) BlockAssertions.wrapBlock(com.facebook.presto.block.BlockAssertions.wrapBlock) TestingSession.testSessionBuilder(com.facebook.presto.testing.TestingSession.testSessionBuilder) PartitionFunction(com.facebook.presto.operator.PartitionFunction) Maps(com.google.common.collect.Maps) LocalPartitionGenerator(com.facebook.presto.operator.exchange.LocalPartitionGenerator) OPEN(com.facebook.presto.execution.buffer.BufferState.OPEN) InterpretedHashGenerator(com.facebook.presto.operator.InterpretedHashGenerator) BufferState(com.facebook.presto.execution.buffer.BufferState) SMALLINT(com.facebook.presto.common.type.SmallintType.SMALLINT) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) PagesSerde(com.facebook.presto.spi.page.PagesSerde) DriverContext(com.facebook.presto.operator.DriverContext) Block(com.facebook.presto.common.block.Block) OperatorContext(com.facebook.presto.operator.OperatorContext) TERMINAL_BUFFER_STATES(com.facebook.presto.execution.buffer.BufferState.TERMINAL_BUFFER_STATES) BYTE(io.airlift.units.DataSize.Unit.BYTE) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) PageAssertions.mergePages(com.facebook.presto.operator.PageAssertions.mergePages) HashMap(java.util.HashMap) LocalPartitionGenerator(com.facebook.presto.operator.exchange.LocalPartitionGenerator) ArrayList(java.util.ArrayList) Page(com.facebook.presto.common.Page) SerializedPage(com.facebook.presto.spi.page.SerializedPage) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with HashGenerator

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

the class LocalExchange method createPartitionFunction.

static PartitionFunction createPartitionFunction(PartitioningProviderManager partitioningProviderManager, Session session, PartitioningHandle partitioning, int partitionCount, List<Type> partitioningChannelTypes, boolean isHashPrecomputed) {
    if (partitioning.getConnectorHandle() instanceof SystemPartitioningHandle) {
        HashGenerator hashGenerator;
        if (isHashPrecomputed) {
            hashGenerator = new PrecomputedHashGenerator(0);
        } else {
            hashGenerator = InterpretedHashGenerator.createPositionalWithTypes(partitioningChannelTypes);
        }
        return new LocalPartitionGenerator(hashGenerator, partitionCount);
    }
    ConnectorNodePartitioningProvider partitioningProvider = partitioningProviderManager.getPartitioningProvider(partitioning.getConnectorId().get());
    int bucketCount = partitioningProvider.getBucketCount(partitioning.getTransactionHandle().orElse(null), session.toConnectorSession(partitioning.getConnectorId().get()), partitioning.getConnectorHandle());
    int[] bucketToPartition = new int[bucketCount];
    for (int bucket = 0; bucket < bucketCount; bucket++) {
        bucketToPartition[bucket] = bucket % partitionCount;
    }
    BucketFunction bucketFunction = partitioningProvider.getBucketFunction(partitioning.getTransactionHandle().orElse(null), session.toConnectorSession(), partitioning.getConnectorHandle(), partitioningChannelTypes, bucketCount);
    checkArgument(bucketFunction != null, "No bucket function for partitioning: %s", partitioning);
    return new BucketPartitionFunction(bucketFunction, bucketToPartition);
}
Also used : BucketPartitionFunction(com.facebook.presto.operator.BucketPartitionFunction) ConnectorNodePartitioningProvider(com.facebook.presto.spi.connector.ConnectorNodePartitioningProvider) SystemPartitioningHandle(com.facebook.presto.sql.planner.SystemPartitioningHandle) PrecomputedHashGenerator(com.facebook.presto.operator.PrecomputedHashGenerator) HashGenerator(com.facebook.presto.operator.HashGenerator) PrecomputedHashGenerator(com.facebook.presto.operator.PrecomputedHashGenerator) InterpretedHashGenerator(com.facebook.presto.operator.InterpretedHashGenerator) BucketFunction(com.facebook.presto.spi.BucketFunction)

Aggregations

HashGenerator (com.facebook.presto.operator.HashGenerator)4 InterpretedHashGenerator (com.facebook.presto.operator.InterpretedHashGenerator)4 Page (com.facebook.presto.common.Page)3 Threads.daemonThreadsNamed (com.facebook.airlift.concurrent.Threads.daemonThreadsNamed)1 Assertions.assertBetweenInclusive (com.facebook.airlift.testing.Assertions.assertBetweenInclusive)1 Session (com.facebook.presto.Session)1 DICTIONARY (com.facebook.presto.block.BlockAssertions.Encoding.DICTIONARY)1 RUN_LENGTH (com.facebook.presto.block.BlockAssertions.Encoding.RUN_LENGTH)1 BlockAssertions.createLongDictionaryBlock (com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock)1 BlockAssertions.createLongSequenceBlock (com.facebook.presto.block.BlockAssertions.createLongSequenceBlock)1 BlockAssertions.createMapType (com.facebook.presto.block.BlockAssertions.createMapType)1 BlockAssertions.createRLEBlock (com.facebook.presto.block.BlockAssertions.createRLEBlock)1 BlockAssertions.createRandomLongsBlock (com.facebook.presto.block.BlockAssertions.createRandomLongsBlock)1 BlockAssertions.createRandomStringBlock (com.facebook.presto.block.BlockAssertions.createRandomStringBlock)1 BlockAssertions.wrapBlock (com.facebook.presto.block.BlockAssertions.wrapBlock)1 Block (com.facebook.presto.common.block.Block)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 BlockEncodingManager (com.facebook.presto.common.block.BlockEncodingManager)1 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)1 VariableWidthBlock (com.facebook.presto.common.block.VariableWidthBlock)1