Search in sources :

Example 1 with LocalPartitionGenerator

use of com.facebook.presto.operator.exchange.LocalPartitionGenerator in project presto by prestodb.

the class TestOptimizedPartitionedOutputOperator method createOptimizedPartitionedOutputOperator.

private OptimizedPartitionedOutputOperator createOptimizedPartitionedOutputOperator(List<Type> types, boolean replicateAllRows) {
    TestingPartitionedOutputBuffer outputBuffer = createPartitionedOutputBuffer();
    PartitionFunction partitionFunction = new LocalPartitionGenerator(new PrecomputedHashGenerator(0), PARTITION_COUNT);
    if (replicateAllRows) {
        List<Type> replicatedTypes = updateBlockTypesWithHashBlockAndNullBlock(types, false, true);
        return createOptimizedPartitionedOutputOperator(replicatedTypes, ImmutableList.of(0), partitionFunction, outputBuffer, OptionalInt.of(replicatedTypes.size() - 1), MAX_MEMORY);
    } else {
        return createOptimizedPartitionedOutputOperator(types, ImmutableList.of(0), partitionFunction, outputBuffer, OptionalInt.empty(), MAX_MEMORY);
    }
}
Also used : PartitionFunction(com.facebook.presto.operator.PartitionFunction) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) LocalPartitionGenerator(com.facebook.presto.operator.exchange.LocalPartitionGenerator) PrecomputedHashGenerator(com.facebook.presto.operator.PrecomputedHashGenerator)

Example 2 with LocalPartitionGenerator

use of com.facebook.presto.operator.exchange.LocalPartitionGenerator 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 3 with LocalPartitionGenerator

use of com.facebook.presto.operator.exchange.LocalPartitionGenerator in project presto by prestodb.

the class TestPartitionedOutputOperator method createPartitionedOutputOperator.

private static PartitionedOutputOperator createPartitionedOutputOperator(boolean shouldReplicate) {
    PartitionFunction partitionFunction = new LocalPartitionGenerator(new InterpretedHashGenerator(ImmutableList.of(BIGINT), new int[] { 0 }), PARTITION_COUNT);
    OutputPartitioning outputPartitioning;
    if (shouldReplicate) {
        outputPartitioning = new OutputPartitioning(partitionFunction, ImmutableList.of(0), ImmutableList.of(Optional.empty()), true, OptionalInt.of(0));
    } else {
        outputPartitioning = new OutputPartitioning(partitionFunction, ImmutableList.of(0), ImmutableList.of(Optional.empty(), Optional.empty()), false, OptionalInt.empty());
    }
    PagesSerdeFactory serdeFactory = new PagesSerdeFactory(new BlockEncodingManager(), false);
    DriverContext driverContext = TestingTaskContext.builder(EXECUTOR, SCHEDULER, TEST_SESSION).setMemoryPoolSize(MAX_MEMORY).setQueryMaxTotalMemory(MAX_MEMORY).build().addPipelineContext(0, true, true, false).addDriverContext();
    OutputBuffers buffers = createInitialEmptyOutputBuffers(PARTITIONED);
    for (int partition = 0; partition < PARTITION_COUNT; partition++) {
        buffers = buffers.withBuffer(new OutputBuffers.OutputBufferId(partition), partition);
    }
    PartitionedOutputBuffer buffer = new PartitionedOutputBuffer("task-instance-id", new StateMachine<>("bufferState", SCHEDULER, OPEN, TERMINAL_BUFFER_STATES), buffers.withNoMoreBufferIds(), new DataSize(Long.MAX_VALUE, BYTE), () -> new SimpleLocalMemoryContext(newSimpleAggregatedMemoryContext(), "test"), SCHEDULER);
    buffer.registerLifespanCompletionCallback(ignore -> {
    });
    PartitionedOutputOperator.PartitionedOutputFactory operatorFactory;
    if (shouldReplicate) {
        operatorFactory = new PartitionedOutputOperator.PartitionedOutputFactory(buffer, PARTITION_MAX_MEMORY);
        return (PartitionedOutputOperator) operatorFactory.createOutputOperator(0, new PlanNodeId("plan-node-0"), REPLICATION_TYPES, Function.identity(), Optional.of(outputPartitioning), serdeFactory).createOperator(driverContext);
    } else {
        operatorFactory = new PartitionedOutputOperator.PartitionedOutputFactory(buffer, PARTITION_MAX_MEMORY);
        return (PartitionedOutputOperator) operatorFactory.createOutputOperator(0, new PlanNodeId("plan-node-0"), TYPES, Function.identity(), Optional.of(outputPartitioning), serdeFactory).createOperator(driverContext);
    }
}
Also used : PartitionedOutputBuffer(com.facebook.presto.execution.buffer.PartitionedOutputBuffer) SimpleLocalMemoryContext(com.facebook.presto.memory.context.SimpleLocalMemoryContext) PartitionedOutputOperator(com.facebook.presto.operator.repartition.PartitionedOutputOperator) LocalPartitionGenerator(com.facebook.presto.operator.exchange.LocalPartitionGenerator) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) PagesSerdeFactory(com.facebook.presto.execution.buffer.PagesSerdeFactory) OutputBuffers.createInitialEmptyOutputBuffers(com.facebook.presto.execution.buffer.OutputBuffers.createInitialEmptyOutputBuffers) OutputBuffers(com.facebook.presto.execution.buffer.OutputBuffers) BlockEncodingManager(com.facebook.presto.common.block.BlockEncodingManager) DataSize(io.airlift.units.DataSize) OutputPartitioning(com.facebook.presto.sql.planner.OutputPartitioning)

Example 4 with LocalPartitionGenerator

use of com.facebook.presto.operator.exchange.LocalPartitionGenerator in project presto by prestodb.

the class TestOptimizedPartitionedOutputOperator method testReplicated.

private void testReplicated(List<Type> types, List<Page> pages, DataSize maxMemory) {
    TestingPartitionedOutputBuffer outputBuffer = createPartitionedOutputBuffer();
    PartitionFunction partitionFunction = new LocalPartitionGenerator(new PrecomputedHashGenerator(0), PARTITION_COUNT);
    OptimizedPartitionedOutputOperator operator = createOptimizedPartitionedOutputOperator(types, ImmutableList.of(0), partitionFunction, outputBuffer, OptionalInt.of(types.size() - 1), maxMemory);
    for (Page page : pages) {
        operator.addInput(page);
    }
    operator.finish();
    Map<Integer, List<Page>> acutualPageLists = outputBuffer.getPages();
    assertEquals(acutualPageLists.size(), PARTITION_COUNT);
    Page expectedPage = mergePages(types, pages);
    acutualPageLists.values().forEach(pageList -> assertPageEquals(types, mergePages(types, pageList), expectedPage));
}
Also used : PartitionFunction(com.facebook.presto.operator.PartitionFunction) LocalPartitionGenerator(com.facebook.presto.operator.exchange.LocalPartitionGenerator) 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) PrecomputedHashGenerator(com.facebook.presto.operator.PrecomputedHashGenerator)

Aggregations

LocalPartitionGenerator (com.facebook.presto.operator.exchange.LocalPartitionGenerator)4 PartitionFunction (com.facebook.presto.operator.PartitionFunction)3 PrecomputedHashGenerator (com.facebook.presto.operator.PrecomputedHashGenerator)3 BlockAssertions.createMapType (com.facebook.presto.block.BlockAssertions.createMapType)2 Page (com.facebook.presto.common.Page)2 BlockEncodingManager (com.facebook.presto.common.block.BlockEncodingManager)2 ArrayType (com.facebook.presto.common.type.ArrayType)2 DecimalType.createDecimalType (com.facebook.presto.common.type.DecimalType.createDecimalType)2 Type (com.facebook.presto.common.type.Type)2 OutputBuffers (com.facebook.presto.execution.buffer.OutputBuffers)2 OutputBuffers.createInitialEmptyOutputBuffers (com.facebook.presto.execution.buffer.OutputBuffers.createInitialEmptyOutputBuffers)2 PagesSerdeFactory (com.facebook.presto.execution.buffer.PagesSerdeFactory)2 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.createRLEBlock (com.facebook.presto.block.BlockAssertions.createRLEBlock)1