ClickHouse/dbms/src/DataStreams/LimitBlockInputStream.cpp

50 lines
1.0 KiB
C++
Raw Normal View History

2010-03-12 18:25:35 +00:00
#include <algorithm>
#include <DB/DataStreams/LimitBlockInputStream.h>
namespace DB
{
using Poco::SharedPtr;
LimitBlockInputStream::LimitBlockInputStream(SharedPtr<IBlockInputStream> input_, size_t limit_, size_t offset_)
: input(input_), limit(limit_), offset(offset_), pos(0)
{
}
Block LimitBlockInputStream::read()
{
Block res;
size_t rows = 0;
2010-03-12 20:44:25 +00:00
/// pos - сколько строк было прочитано, включая последний прочитанный блок
2010-03-12 18:25:35 +00:00
if (pos >= offset + limit)
return res;
2010-03-12 20:44:25 +00:00
do
2010-03-12 18:25:35 +00:00
{
res = input->read();
rows = res.rows();
pos += rows;
2010-03-12 20:44:25 +00:00
} while (pos <= offset);
2010-03-12 18:25:35 +00:00
2010-03-12 20:44:25 +00:00
/// отдать целый блок
if (pos >= offset + rows && pos <= offset + limit)
2010-03-12 18:25:35 +00:00
return res;
2010-03-12 20:44:25 +00:00
/// отдать кусок блока
size_t start = std::max(0, static_cast<int>(offset) + static_cast<int>(rows) - static_cast<int>(pos));
size_t length = std::min(rows - start, limit + offset + rows - pos);
2010-03-12 18:25:35 +00:00
for (size_t i = 0; i < res.columns(); ++i)
2010-03-12 20:44:25 +00:00
res.getByPosition(i).column->cut(start, length);
2010-03-12 18:25:35 +00:00
return res;
}
}