2019-03-26 18:28:37 +00:00
|
|
|
#include <Processors/Sources/SourceFromInputStream.h>
|
2019-04-05 15:45:46 +00:00
|
|
|
#include <Processors/Transforms/AggregatingTransform.h>
|
|
|
|
#include <DataTypes/DataTypeAggregateFunction.h>
|
2019-03-26 18:28:37 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-04-17 14:38:16 +00:00
|
|
|
SourceFromInputStream::SourceFromInputStream(BlockInputStreamPtr stream_, bool force_add_aggregating_info)
|
|
|
|
: ISource(stream_->getHeader())
|
2019-04-12 13:09:36 +00:00
|
|
|
, force_add_aggregating_info(force_add_aggregating_info)
|
2019-04-17 14:38:16 +00:00
|
|
|
, stream(std::move(stream_))
|
2019-03-26 18:28:37 +00:00
|
|
|
{
|
2019-04-05 15:45:46 +00:00
|
|
|
auto & sample = getPort().getHeader();
|
|
|
|
for (auto & type : sample.getDataTypes())
|
|
|
|
if (typeid_cast<const DataTypeAggregateFunction *>(type.get()))
|
|
|
|
has_aggregate_functions = true;
|
2019-03-26 18:28:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 14:38:16 +00:00
|
|
|
void SourceFromInputStream::addTotalsPort()
|
|
|
|
{
|
|
|
|
if (has_totals_port)
|
|
|
|
throw Exception("Totals port was already added for SourceFromInputStream.", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
outputs.emplace_back(outputs.front().getHeader(), this);
|
2019-04-17 15:13:08 +00:00
|
|
|
has_totals_port = true;
|
2019-04-17 14:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IProcessor::Status SourceFromInputStream::prepare()
|
|
|
|
{
|
|
|
|
auto status = ISource::prepare();
|
|
|
|
|
|
|
|
if (status == Status::Finished)
|
|
|
|
{
|
|
|
|
/// Read postfix and get totals if needed.
|
|
|
|
if (!is_stream_finished)
|
|
|
|
return Status::Ready;
|
|
|
|
|
2019-04-17 15:06:19 +00:00
|
|
|
if (has_totals_port)
|
2019-04-17 14:38:16 +00:00
|
|
|
{
|
|
|
|
auto & totals_out = outputs.back();
|
|
|
|
|
|
|
|
if (totals_out.isFinished())
|
|
|
|
return Status::Finished;
|
|
|
|
|
2019-04-17 15:06:19 +00:00
|
|
|
if (has_totals)
|
|
|
|
{
|
|
|
|
if (!totals_out.canPush())
|
|
|
|
return Status::PortFull;
|
|
|
|
|
|
|
|
totals_out.push(std::move(totals));
|
|
|
|
has_totals = false;
|
|
|
|
}
|
2019-04-17 14:38:16 +00:00
|
|
|
|
|
|
|
totals_out.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceFromInputStream::work()
|
|
|
|
{
|
|
|
|
if (!finished)
|
|
|
|
return ISource::work();
|
|
|
|
|
|
|
|
if (is_stream_finished)
|
|
|
|
return;
|
|
|
|
|
|
|
|
stream->cancel(false);
|
|
|
|
stream->readSuffix();
|
|
|
|
|
|
|
|
if (auto totals_block = stream->getTotals())
|
|
|
|
{
|
|
|
|
totals.setColumns(totals_block.getColumns(), 1);
|
|
|
|
has_totals = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_stream_finished = true;
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:28:37 +00:00
|
|
|
Chunk SourceFromInputStream::generate()
|
|
|
|
{
|
2019-04-17 14:38:16 +00:00
|
|
|
if (is_stream_finished)
|
2019-03-26 18:28:37 +00:00
|
|
|
return {};
|
|
|
|
|
2019-04-17 14:38:16 +00:00
|
|
|
auto block = stream->read();
|
2019-03-26 18:28:37 +00:00
|
|
|
if (!block)
|
|
|
|
{
|
2019-04-17 14:38:16 +00:00
|
|
|
stream->readSuffix();
|
|
|
|
|
|
|
|
if (auto totals_block = stream->getTotals())
|
|
|
|
{
|
|
|
|
totals.setColumns(totals_block.getColumns(), 1);
|
|
|
|
has_totals = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_stream_finished = true;
|
2019-03-26 18:28:37 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
assertBlocksHaveEqualStructure(getPort().getHeader(), block, "SourceFromInputStream");
|
|
|
|
|
|
|
|
UInt64 num_rows = block.rows();
|
2019-04-05 15:45:46 +00:00
|
|
|
Chunk chunk(block.getColumns(), num_rows);
|
|
|
|
|
2019-04-12 13:14:03 +00:00
|
|
|
if (force_add_aggregating_info || has_aggregate_functions)
|
2019-04-05 15:45:46 +00:00
|
|
|
{
|
|
|
|
auto info = std::make_shared<AggregatedChunkInfo>();
|
|
|
|
info->bucket_num = block.info.bucket_num;
|
|
|
|
info->is_overflows = block.info.is_overflows;
|
|
|
|
chunk.setChunkInfo(std::move(info));
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunk;
|
2019-03-26 18:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|