ClickHouse/dbms/src/DataStreams/narrowBlockInputStreams.cpp

36 lines
847 B
C++
Raw Normal View History

#include <random>
2019-07-28 15:30:38 +00:00
#include <Common/thread_local_rng.h>
#include <DataStreams/ConcatBlockInputStream.h>
2012-06-25 03:04:34 +00:00
namespace DB
{
BlockInputStreams narrowBlockInputStreams(BlockInputStreams & inputs, size_t width)
{
size_t size = inputs.size();
if (size <= width)
return inputs;
2012-06-25 03:04:34 +00:00
std::vector<BlockInputStreams> partitions(width);
2012-06-25 03:04:34 +00:00
using Distribution = std::vector<size_t>;
Distribution distribution(size);
2012-08-21 18:34:55 +00:00
for (size_t i = 0; i < size; ++i)
distribution[i] = i % width;
2012-08-21 18:34:55 +00:00
2019-07-28 15:30:38 +00:00
std::shuffle(distribution.begin(), distribution.end(), thread_local_rng);
2012-08-21 18:34:55 +00:00
for (size_t i = 0; i < size; ++i)
partitions[distribution[i]].push_back(inputs[i]);
2012-06-25 03:04:34 +00:00
BlockInputStreams res(width);
for (size_t i = 0; i < width; ++i)
res[i] = std::make_shared<ConcatBlockInputStream>(partitions[i]);
2012-06-25 03:04:34 +00:00
return res;
2012-06-25 03:04:34 +00:00
}
}