2015-12-01 14:43:51 +00:00
|
|
|
|
#include <DB/DataStreams/BlocksListBlockInputStream.h>
|
|
|
|
|
#include <DB/DataStreams/MergingAggregatedMemoryEfficientBlockInputStream.h>
|
2011-09-19 03:34:23 +00:00
|
|
|
|
#include <DB/DataStreams/AggregatingBlockInputStream.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Block AggregatingBlockInputStream::readImpl()
|
|
|
|
|
{
|
2015-01-02 03:16:28 +00:00
|
|
|
|
if (!executed)
|
|
|
|
|
{
|
|
|
|
|
executed = true;
|
2015-12-09 02:55:35 +00:00
|
|
|
|
AggregatedDataVariantsPtr data_variants = new AggregatedDataVariants;
|
2015-04-16 14:27:56 +00:00
|
|
|
|
|
|
|
|
|
Aggregator::CancellationHook hook = [&]() { return this->isCancelled(); };
|
|
|
|
|
aggregator.setCancellationHook(hook);
|
|
|
|
|
|
2015-12-09 02:55:35 +00:00
|
|
|
|
aggregator.execute(children.back(), *data_variants);
|
2015-12-01 14:43:51 +00:00
|
|
|
|
|
|
|
|
|
if (!aggregator.hasTemporaryFiles())
|
|
|
|
|
{
|
2015-12-09 02:55:35 +00:00
|
|
|
|
ManyAggregatedDataVariants many_data { data_variants };
|
|
|
|
|
impl = aggregator.mergeAndConvertToBlocks(many_data, final, 1);
|
2015-12-01 14:43:51 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/** Если есть временные файлы с частично-агрегированными данными на диске,
|
|
|
|
|
* то читаем и мерджим их, расходуя минимальное количество памяти.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-12-03 21:23:21 +00:00
|
|
|
|
ProfileEvents::increment(ProfileEvents::ExternalAggregationMerge);
|
|
|
|
|
|
2015-12-06 15:29:16 +00:00
|
|
|
|
if (!isCancelled())
|
|
|
|
|
{
|
|
|
|
|
/// Сбросим имеющиеся в оперативке данные тоже на диск. Так проще.
|
2015-12-09 02:55:35 +00:00
|
|
|
|
size_t rows = data_variants->sizeWithoutOverflowRow();
|
2015-12-06 15:29:16 +00:00
|
|
|
|
if (rows)
|
2015-12-09 02:55:35 +00:00
|
|
|
|
aggregator.writeToTemporaryFile(*data_variants, rows);
|
2015-12-06 15:29:16 +00:00
|
|
|
|
}
|
2015-12-01 14:43:51 +00:00
|
|
|
|
|
|
|
|
|
const auto & files = aggregator.getTemporaryFiles();
|
|
|
|
|
BlockInputStreams input_streams;
|
2015-12-01 16:58:15 +00:00
|
|
|
|
for (const auto & file : files.files)
|
2015-12-01 14:43:51 +00:00
|
|
|
|
{
|
|
|
|
|
temporary_inputs.emplace_back(new TemporaryFileStream(file->path()));
|
|
|
|
|
input_streams.emplace_back(temporary_inputs.back()->block_in);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-01 16:58:15 +00:00
|
|
|
|
LOG_TRACE(log, "Will merge " << files.files.size() << " temporary files of size "
|
|
|
|
|
<< (files.sum_size_compressed / 1048576.0) << " MiB compressed, "
|
|
|
|
|
<< (files.sum_size_uncompressed / 1048576.0) << " MiB uncompressed.");
|
|
|
|
|
|
2015-12-05 04:20:37 +00:00
|
|
|
|
impl.reset(new MergingAggregatedMemoryEfficientBlockInputStream(input_streams, params, final, 1, 1));
|
2015-12-01 14:43:51 +00:00
|
|
|
|
}
|
2015-01-02 03:16:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Block res;
|
2015-12-01 14:43:51 +00:00
|
|
|
|
if (isCancelled() || !impl)
|
2015-01-02 03:16:28 +00:00
|
|
|
|
return res;
|
|
|
|
|
|
2015-12-01 14:43:51 +00:00
|
|
|
|
return impl->read();
|
2011-09-19 03:34:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|