2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/ColumnGathererStream.h>
|
2017-01-21 04:24:28 +00:00
|
|
|
#include <common/logger_useful.h>
|
2016-12-13 16:19:57 +00:00
|
|
|
#include <iomanip>
|
2016-11-03 12:00:44 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2016-11-03 12:00:44 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int INCOMPATIBLE_COLUMNS;
|
|
|
|
extern const int INCORRECT_NUMBER_OF_COLUMNS;
|
|
|
|
extern const int EMPTY_DATA_PASSED;
|
|
|
|
extern const int RECEIVED_EMPTY_DATA;
|
2016-11-03 12:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ColumnGathererStream::ColumnGathererStream(const BlockInputStreams & source_streams, const String & column_name_,
|
2017-04-01 07:20:54 +00:00
|
|
|
const MergedRowSources & row_source_, size_t block_preferred_size_)
|
|
|
|
: name(column_name_), row_source(row_source_), block_preferred_size(block_preferred_size_), log(&Logger::get("ColumnGathererStream"))
|
2016-11-03 12:00:44 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (source_streams.empty())
|
|
|
|
throw Exception("There are no streams to gather", ErrorCodes::EMPTY_DATA_PASSED);
|
2016-11-03 12:00:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
children.assign(source_streams.begin(), source_streams.end());
|
2017-04-01 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 12:00:44 +00:00
|
|
|
|
2017-04-01 21:43:25 +00:00
|
|
|
String ColumnGathererStream::getID() const
|
|
|
|
{
|
|
|
|
std::stringstream res;
|
2017-03-16 10:39:44 +00:00
|
|
|
|
2017-04-01 21:43:25 +00:00
|
|
|
res << getName() << "(";
|
|
|
|
for (size_t i = 0; i < children.size(); i++)
|
|
|
|
res << (i == 0 ? "" : ", " ) << children[i]->getID();
|
|
|
|
res << ")";
|
|
|
|
|
|
|
|
return res.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnGathererStream::init()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
sources.reserve(children.size());
|
|
|
|
for (size_t i = 0; i < children.size(); i++)
|
|
|
|
{
|
|
|
|
sources.emplace_back(children[i]->read(), name);
|
2016-11-03 12:00:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Block & block = sources.back().block;
|
2016-11-03 12:00:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Sometimes MergeTreeReader injects additional column with partitioning key
|
|
|
|
if (block.columns() > 2 || !block.has(name))
|
|
|
|
throw Exception("Block should have 1 or 2 columns and contain column with requested name", ErrorCodes::INCORRECT_NUMBER_OF_COLUMNS);
|
2016-11-03 12:00:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
column.name = name;
|
|
|
|
column.type = block.getByName(name).type->clone();
|
|
|
|
column.column = column.type->createColumn();
|
|
|
|
}
|
2016-11-03 12:00:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (block.getByName(name).column->getName() != column.column->getName())
|
|
|
|
throw Exception("Column types don't match", ErrorCodes::INCOMPATIBLE_COLUMNS);
|
|
|
|
}
|
2016-11-03 12:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block ColumnGathererStream::readImpl()
|
|
|
|
{
|
2017-04-01 21:43:25 +00:00
|
|
|
/// Special case: single source and there are no skipped rows
|
|
|
|
if (children.size() == 1 && row_source.size() == 0)
|
2017-04-01 07:20:54 +00:00
|
|
|
return children[0]->read();
|
|
|
|
|
2017-04-01 21:43:25 +00:00
|
|
|
/// Initialize first source blocks
|
|
|
|
if (sources.empty())
|
|
|
|
init();
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (pos_global_start >= row_source.size())
|
|
|
|
return Block();
|
|
|
|
|
2017-07-06 13:54:55 +00:00
|
|
|
block_res = Block{column.cloneEmpty()};
|
2017-04-01 07:20:54 +00:00
|
|
|
IColumn & column_res = *block_res.getByPosition(0).column;
|
|
|
|
|
2017-07-06 13:54:55 +00:00
|
|
|
column_res.gather(*this);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-06 13:54:55 +00:00
|
|
|
return std::move(block_res);
|
2016-11-03 12:00:44 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 16:19:57 +00:00
|
|
|
|
2016-12-20 14:58:23 +00:00
|
|
|
void ColumnGathererStream::fetchNewBlock(Source & source, size_t source_num)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
source.block = children[source_num]->read();
|
|
|
|
source.update(name);
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
e.addMessage("Cannot fetch required block. Stream " + children[source_num]->getID() + ", part " + toString(source_num));
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == source.size)
|
|
|
|
{
|
|
|
|
throw Exception("Fetched block is empty. Stream " + children[source_num]->getID() + ", part " + toString(source_num),
|
|
|
|
ErrorCodes::RECEIVED_EMPTY_DATA);
|
|
|
|
}
|
2016-12-20 14:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-13 16:19:57 +00:00
|
|
|
void ColumnGathererStream::readSuffixImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const BlockStreamProfileInfo & profile_info = getProfileInfo();
|
|
|
|
double seconds = profile_info.total_stopwatch.elapsedSeconds();
|
|
|
|
LOG_DEBUG(log, std::fixed << std::setprecision(2)
|
2017-04-01 21:43:25 +00:00
|
|
|
<< "Gathered column " << name
|
2017-04-01 07:20:54 +00:00
|
|
|
<< " (" << static_cast<double>(profile_info.bytes) / profile_info.rows << " bytes/elem.)"
|
|
|
|
<< " in " << seconds << " sec., "
|
|
|
|
<< profile_info.rows / seconds << " rows/sec., "
|
|
|
|
<< profile_info.bytes / 1048576.0 / seconds << " MiB/sec.");
|
2016-12-13 16:19:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 12:00:44 +00:00
|
|
|
}
|