2010-03-12 18:25:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <DB/DataStreams/LimitBlockInputStream.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-09-04 19:40:55 +00:00
|
|
|
LimitBlockInputStream::LimitBlockInputStream(BlockInputStreamPtr input_, size_t limit_, size_t offset_, bool always_read_till_end_)
|
|
|
|
: limit(limit_), offset(offset_), always_read_till_end(always_read_till_end_)
|
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;
|
2016-05-28 14:14:18 +00:00
|
|
|
|
2010-03-12 20:44:25 +00:00
|
|
|
/// pos - сколько строк было прочитано, включая последний прочитанный блок
|
|
|
|
|
2010-03-12 18:25:35 +00:00
|
|
|
if (pos >= offset + limit)
|
2015-09-04 19:40:55 +00:00
|
|
|
{
|
|
|
|
if (!always_read_till_end)
|
|
|
|
return res;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (children.back()->read())
|
|
|
|
;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2010-03-12 18:25:35 +00:00
|
|
|
|
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)
|
2017-01-02 20:12:12 +00:00
|
|
|
res.safeGetByPosition(i).column = res.safeGetByPosition(i).column->cut(start, length);
|
2013-05-03 05:23:14 +00:00
|
|
|
|
2010-03-12 18:25:35 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|