2010-03-12 18:25:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <DB/DataStreams/LimitBlockInputStream.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
using Poco::SharedPtr;
|
|
|
|
|
2011-08-28 02:22:23 +00:00
|
|
|
LimitBlockInputStream::LimitBlockInputStream(BlockInputStreamPtr input_, size_t limit_, size_t offset_)
|
2013-05-04 04:05:15 +00:00
|
|
|
: limit(limit_), offset(offset_), pos(0)
|
2010-03-12 18:25:35 +00:00
|
|
|
{
|
2013-05-04 04:05:15 +00:00
|
|
|
children.push_back(input_);
|
2010-03-12 18:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-04 21:23:19 +00:00
|
|
|
Block LimitBlockInputStream::readImpl()
|
2010-03-12 18:25:35 +00:00
|
|
|
{
|
|
|
|
Block res;
|
|
|
|
size_t rows = 0;
|
2013-05-20 12:21:51 +00:00
|
|
|
|
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
|
|
|
{
|
2013-05-04 05:20:07 +00:00
|
|
|
res = children.back()->read();
|
2011-08-28 08:02:11 +00:00
|
|
|
if (!res)
|
|
|
|
return res;
|
2010-03-12 18:25:35 +00:00
|
|
|
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
|
|
|
/// отдать кусок блока
|
2011-08-14 01:11:33 +00:00
|
|
|
size_t start = std::max(
|
|
|
|
static_cast<Int64>(0),
|
|
|
|
static_cast<Int64>(offset) - static_cast<Int64>(pos) + static_cast<Int64>(rows));
|
|
|
|
|
|
|
|
size_t length = std::min(
|
|
|
|
static_cast<Int64>(limit), std::min(
|
|
|
|
static_cast<Int64>(pos) - static_cast<Int64>(offset),
|
|
|
|
static_cast<Int64>(limit) + static_cast<Int64>(offset) - static_cast<Int64>(pos) + static_cast<Int64>(rows)));
|
2011-08-14 00:49:30 +00:00
|
|
|
|
2010-03-12 18:25:35 +00:00
|
|
|
for (size_t i = 0; i < res.columns(); ++i)
|
2013-05-03 05:23:14 +00:00
|
|
|
res.getByPosition(i).column = res.getByPosition(i).column->cut(start, length);
|
|
|
|
|
2010-03-12 18:25:35 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|