ClickHouse/dbms/src/DataStreams/narrowBlockInputStreams.cpp
2019-07-28 18:30:38 +03:00

36 lines
847 B
C++

#include <random>
#include <Common/thread_local_rng.h>
#include <DataStreams/ConcatBlockInputStream.h>
namespace DB
{
BlockInputStreams narrowBlockInputStreams(BlockInputStreams & inputs, size_t width)
{
size_t size = inputs.size();
if (size <= width)
return inputs;
std::vector<BlockInputStreams> partitions(width);
using Distribution = std::vector<size_t>;
Distribution distribution(size);
for (size_t i = 0; i < size; ++i)
distribution[i] = i % width;
std::shuffle(distribution.begin(), distribution.end(), thread_local_rng);
for (size_t i = 0; i < size; ++i)
partitions[distribution[i]].push_back(inputs[i]);
BlockInputStreams res(width);
for (size_t i = 0; i < width; ++i)
res[i] = std::make_shared<ConcatBlockInputStream>(partitions[i]);
return res;
}
}