2011-09-04 01:42:14 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2012-09-05 19:51:09 +00:00
|
|
|
|
#include <Yandex/logger_useful.h>
|
|
|
|
|
|
2011-09-04 01:42:14 +00:00
|
|
|
|
#include <DB/Core/SortDescription.h>
|
|
|
|
|
|
2011-09-04 21:23:19 +00:00
|
|
|
|
#include <DB/DataStreams/IProfilingBlockInputStream.h>
|
2011-09-04 01:42:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Соединяет поток сортированных по отдельности блоков в сортированный целиком поток.
|
|
|
|
|
*/
|
2011-09-04 21:23:19 +00:00
|
|
|
|
class MergeSortingBlockInputStream : public IProfilingBlockInputStream
|
2011-09-04 01:42:14 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2013-09-16 05:44:47 +00:00
|
|
|
|
/// limit - если не 0, то можно выдать только первые limit строк в сортированном порядке.
|
|
|
|
|
MergeSortingBlockInputStream(BlockInputStreamPtr input_, SortDescription & description_, size_t limit_ = 0)
|
|
|
|
|
: description(description_), limit(limit_), has_been_read(false), log(&Logger::get("MergeSortingBlockInputStream"))
|
2011-09-04 21:23:19 +00:00
|
|
|
|
{
|
2013-05-04 04:05:15 +00:00
|
|
|
|
children.push_back(input_);
|
2011-09-04 21:23:19 +00:00
|
|
|
|
}
|
2011-09-04 01:42:14 +00:00
|
|
|
|
|
2014-11-08 23:52:18 +00:00
|
|
|
|
String getName() const override { return "MergeSortingBlockInputStream"; }
|
2011-09-04 01:42:14 +00:00
|
|
|
|
|
2014-11-08 23:52:18 +00:00
|
|
|
|
String getID() const override
|
2013-05-03 10:20:53 +00:00
|
|
|
|
{
|
|
|
|
|
std::stringstream res;
|
2013-05-04 05:20:07 +00:00
|
|
|
|
res << "MergeSorting(" << children.back()->getID();
|
2014-11-08 23:52:18 +00:00
|
|
|
|
|
2013-05-03 10:20:53 +00:00
|
|
|
|
for (size_t i = 0; i < description.size(); ++i)
|
|
|
|
|
res << ", " << description[i].getID();
|
2014-11-08 23:52:18 +00:00
|
|
|
|
|
2013-05-03 10:20:53 +00:00
|
|
|
|
res << ")";
|
|
|
|
|
return res.str();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-20 02:10:47 +00:00
|
|
|
|
protected:
|
2014-11-08 23:52:18 +00:00
|
|
|
|
Block readImpl() override;
|
2012-10-20 02:10:47 +00:00
|
|
|
|
|
2011-09-04 01:42:14 +00:00
|
|
|
|
private:
|
|
|
|
|
SortDescription description;
|
2013-09-16 05:44:47 +00:00
|
|
|
|
size_t limit;
|
2011-09-04 01:42:14 +00:00
|
|
|
|
|
2012-07-23 20:01:29 +00:00
|
|
|
|
/// Всё было прочитано.
|
2012-03-05 02:34:20 +00:00
|
|
|
|
bool has_been_read;
|
|
|
|
|
|
2012-07-24 18:21:39 +00:00
|
|
|
|
Logger * log;
|
2014-11-08 23:52:18 +00:00
|
|
|
|
|
|
|
|
|
/** Слить сразу много блоков с помощью priority queue.
|
2012-07-25 20:33:43 +00:00
|
|
|
|
*/
|
2012-07-23 06:23:29 +00:00
|
|
|
|
Block merge(Blocks & blocks);
|
2014-11-08 23:52:18 +00:00
|
|
|
|
|
2013-05-28 16:56:05 +00:00
|
|
|
|
typedef std::vector<SortCursorImpl> CursorImpls;
|
2014-11-08 23:52:18 +00:00
|
|
|
|
|
2013-05-28 16:56:05 +00:00
|
|
|
|
/** Делаем поддержку двух разных курсоров - с Collation и без.
|
|
|
|
|
* Шаблоны используем вместо полиморфных SortCursor'ов и вызовов виртуальных функций.
|
|
|
|
|
*/
|
2013-09-16 05:44:47 +00:00
|
|
|
|
template <typename TSortCursor>
|
|
|
|
|
Block mergeImpl(Blocks & block, CursorImpls & cursors);
|
2011-09-04 01:42:14 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|