aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/QueryPipeline/narrowPipe.cpp
blob: 9dc579eb34f97926aab31263e6b88dbbad430b55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <QueryPipeline/narrowPipe.h>

#include <random>
#include <Common/thread_local_rng.h>
#include <Processors/ConcatProcessor.h>
#include <QueryPipeline/Pipe.h>


namespace DB
{

namespace
{
    using Distribution = std::vector<size_t>;
    Distribution getDistribution(size_t from, size_t to)
    {
        Distribution distribution(from);

        for (size_t i = 0; i < from; ++i)
            distribution[i] = i % to;

        std::shuffle(distribution.begin(), distribution.end(), thread_local_rng);
        return distribution;
    }
}

void narrowPipe(Pipe & pipe, size_t width)
{
    size_t size = pipe.numOutputPorts();
    if (size <= width)
        return;

    std::vector<std::vector<OutputPort *>> partitions(width);

    auto distribution = getDistribution(size, width);

    pipe.transform([&](OutputPortRawPtrs ports)
    {
        for (size_t i = 0; i < size; ++i)
            partitions[distribution[i]].emplace_back(ports[i]);

        Processors concats;
        concats.reserve(width);

        for (size_t i = 0; i < width; ++i)
        {
           auto concat = std::make_shared<ConcatProcessor>(partitions[i].at(0)->getHeader(),
                                                           partitions[i].size());
           size_t next_port = 0;
           for (auto & port : concat->getInputs())
           {
               connect(*partitions[i][next_port], port);
               ++next_port;
           }

           concats.emplace_back(std::move(concat));
        }

        return concats;
    });
}

}