2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/MergeSortingBlockInputStream.h>
|
|
|
|
#include <DataStreams/MergingSortedBlockInputStream.h>
|
|
|
|
#include <DataStreams/NativeBlockOutputStream.h>
|
2019-10-15 16:31:49 +00:00
|
|
|
#include <DataStreams/TemporaryFileStream.h>
|
2018-10-19 12:02:31 +00:00
|
|
|
#include <DataStreams/processConstants.h>
|
2018-09-24 20:07:30 +00:00
|
|
|
#include <Common/formatReadable.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferFromFile.h>
|
2018-12-28 18:15:26 +00:00
|
|
|
#include <Compression/CompressedWriteBuffer.h>
|
2018-10-04 10:24:51 +00:00
|
|
|
#include <Interpreters/sortBlock.h>
|
2020-01-19 14:26:28 +00:00
|
|
|
#include <Disks/DiskSpaceMonitor.h>
|
2011-09-04 01:42:14 +00:00
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Event ExternalSortWritePart;
|
|
|
|
extern const Event ExternalSortMerge;
|
2016-10-24 02:02:37 +00:00
|
|
|
}
|
|
|
|
|
2011-09-04 01:42:14 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_ENOUGH_SPACE;
|
|
|
|
}
|
2011-09-04 01:42:14 +00:00
|
|
|
|
2018-04-07 01:46:50 +00:00
|
|
|
MergeSortingBlockInputStream::MergeSortingBlockInputStream(
|
|
|
|
const BlockInputStreamPtr & input, SortDescription & description_,
|
2019-02-10 15:17:45 +00:00
|
|
|
size_t max_merged_block_size_, UInt64 limit_, size_t max_bytes_before_remerge_,
|
2020-01-19 14:26:28 +00:00
|
|
|
size_t max_bytes_before_external_sort_, VolumePtr tmp_volume_, size_t min_free_disk_space_)
|
2018-04-07 01:46:50 +00:00
|
|
|
: description(description_), max_merged_block_size(max_merged_block_size_), limit(limit_),
|
2018-09-24 20:07:30 +00:00
|
|
|
max_bytes_before_remerge(max_bytes_before_remerge_),
|
2020-01-19 14:26:28 +00:00
|
|
|
max_bytes_before_external_sort(max_bytes_before_external_sort_), tmp_volume(tmp_volume_),
|
2019-08-27 00:23:07 +00:00
|
|
|
min_free_disk_space(min_free_disk_space_)
|
2011-09-04 01:42:14 +00:00
|
|
|
{
|
2018-04-07 01:46:50 +00:00
|
|
|
children.push_back(input);
|
2018-04-08 04:38:27 +00:00
|
|
|
header = children.at(0)->getHeader();
|
|
|
|
header_without_constants = header;
|
|
|
|
removeConstantsFromBlock(header_without_constants);
|
2018-04-07 01:46:50 +00:00
|
|
|
removeConstantsFromSortDescription(header, description);
|
|
|
|
}
|
2018-02-19 00:45:32 +00:00
|
|
|
|
2018-04-07 01:46:50 +00:00
|
|
|
|
|
|
|
Block MergeSortingBlockInputStream::readImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Algorithm:
|
|
|
|
* - read to memory blocks from source stream;
|
2018-03-09 23:23:15 +00:00
|
|
|
* - if too many of them and if external sorting is enabled,
|
2017-04-01 07:20:54 +00:00
|
|
|
* - merge all blocks to sorted stream and write it to temporary file;
|
|
|
|
* - at the end, merge all sorted streams from temporary files and also from rest of blocks in memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// If has not read source blocks.
|
|
|
|
if (!impl)
|
|
|
|
{
|
|
|
|
while (Block block = children.back()->read())
|
|
|
|
{
|
2017-06-08 12:50:58 +00:00
|
|
|
/// If there were only const columns in sort description, then there is no need to sort.
|
|
|
|
/// Return the blocks as is.
|
|
|
|
if (description.empty())
|
|
|
|
return block;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
removeConstantsFromBlock(block);
|
|
|
|
|
|
|
|
blocks.push_back(block);
|
2018-09-26 01:30:07 +00:00
|
|
|
sum_rows_in_blocks += block.rows();
|
2018-09-24 20:07:30 +00:00
|
|
|
sum_bytes_in_blocks += block.allocatedBytes();
|
|
|
|
|
|
|
|
/** If significant amount of data was accumulated, perform preliminary merging step.
|
|
|
|
*/
|
2018-09-26 01:30:07 +00:00
|
|
|
if (blocks.size() > 1
|
|
|
|
&& limit
|
|
|
|
&& limit * 2 < sum_rows_in_blocks /// 2 is just a guess.
|
|
|
|
&& remerge_is_useful
|
|
|
|
&& max_bytes_before_remerge
|
|
|
|
&& sum_bytes_in_blocks > max_bytes_before_remerge)
|
|
|
|
{
|
2018-09-24 20:07:30 +00:00
|
|
|
remerge();
|
2018-09-26 01:30:07 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-03-09 23:23:15 +00:00
|
|
|
/** If too many of them and if external sorting is enabled,
|
2017-04-01 07:20:54 +00:00
|
|
|
* will merge blocks that we have in memory at this moment and write merged stream to temporary (compressed) file.
|
|
|
|
* NOTE. It's possible to check free space in filesystem.
|
|
|
|
*/
|
|
|
|
if (max_bytes_before_external_sort && sum_bytes_in_blocks > max_bytes_before_external_sort)
|
|
|
|
{
|
2020-01-19 14:26:28 +00:00
|
|
|
size_t size = sum_bytes_in_blocks + min_free_disk_space;
|
|
|
|
auto reservation = tmp_volume->reserve(size);
|
|
|
|
if (!reservation)
|
|
|
|
throw Exception("Not enough space for external sort in temporary storage", ErrorCodes::NOT_ENOUGH_SPACE);
|
2019-08-27 18:59:21 +00:00
|
|
|
|
2020-01-19 14:26:28 +00:00
|
|
|
const std::string tmp_path(reservation->getDisk()->getPath());
|
2019-10-01 18:51:33 +00:00
|
|
|
temporary_files.emplace_back(createTemporaryFile(tmp_path));
|
2020-01-19 14:26:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string & path = temporary_files.back()->path();
|
|
|
|
MergeSortingBlocksBlockInputStream block_in(blocks, description, max_merged_block_size, limit);
|
|
|
|
|
|
|
|
LOG_INFO(log, "Sorting and writing part of data into temporary file " + path);
|
|
|
|
ProfileEvents::increment(ProfileEvents::ExternalSortWritePart);
|
2019-10-15 16:31:49 +00:00
|
|
|
TemporaryFileStream::write(path, header_without_constants, block_in, &is_cancelled); /// NOTE. Possibly limit disk usage.
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_INFO(log, "Done writing part of data into temporary file " + path);
|
|
|
|
|
|
|
|
blocks.clear();
|
|
|
|
sum_bytes_in_blocks = 0;
|
2018-09-26 01:30:07 +00:00
|
|
|
sum_rows_in_blocks = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:09:39 +00:00
|
|
|
if ((blocks.empty() && temporary_files.empty()) || isCancelledOrThrowIfKilled())
|
2017-04-01 07:20:54 +00:00
|
|
|
return Block();
|
|
|
|
|
|
|
|
if (temporary_files.empty())
|
|
|
|
{
|
|
|
|
impl = std::make_unique<MergeSortingBlocksBlockInputStream>(blocks, description, max_merged_block_size, limit);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// If there was temporary files.
|
|
|
|
ProfileEvents::increment(ProfileEvents::ExternalSortMerge);
|
|
|
|
|
|
|
|
LOG_INFO(log, "There are " << temporary_files.size() << " temporary sorted parts to merge.");
|
|
|
|
|
|
|
|
/// Create sorted streams to merge.
|
|
|
|
for (const auto & file : temporary_files)
|
|
|
|
{
|
2018-04-08 04:38:27 +00:00
|
|
|
temporary_inputs.emplace_back(std::make_unique<TemporaryFileStream>(file->path(), header_without_constants));
|
2017-04-01 07:20:54 +00:00
|
|
|
inputs_to_merge.emplace_back(temporary_inputs.back()->block_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rest of blocks in memory.
|
|
|
|
if (!blocks.empty())
|
|
|
|
inputs_to_merge.emplace_back(std::make_shared<MergeSortingBlocksBlockInputStream>(blocks, description, max_merged_block_size, limit));
|
|
|
|
|
|
|
|
/// Will merge that sorted streams.
|
|
|
|
impl = std::make_unique<MergingSortedBlockInputStream>(inputs_to_merge, description, max_merged_block_size, limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Block res = impl->read();
|
|
|
|
if (res)
|
2018-02-19 00:45:32 +00:00
|
|
|
enrichBlockWithConstants(res, header);
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2011-09-04 01:42:14 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 06:23:29 +00:00
|
|
|
|
2015-01-07 15:30:11 +00:00
|
|
|
MergeSortingBlocksBlockInputStream::MergeSortingBlocksBlockInputStream(
|
2019-10-15 16:31:49 +00:00
|
|
|
Blocks & blocks_, const SortDescription & description_, size_t max_merged_block_size_, UInt64 limit_)
|
2018-02-19 04:29:56 +00:00
|
|
|
: blocks(blocks_), header(blocks.at(0).cloneEmpty()), description(description_), max_merged_block_size(max_merged_block_size_), limit(limit_)
|
2015-01-07 15:30:11 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Blocks nonempty_blocks;
|
|
|
|
for (const auto & block : blocks)
|
|
|
|
{
|
|
|
|
if (block.rows() == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nonempty_blocks.push_back(block);
|
|
|
|
cursors.emplace_back(block, description);
|
|
|
|
has_collation |= cursors.back().has_collation;
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks.swap(nonempty_blocks);
|
|
|
|
|
2019-12-22 01:49:38 +00:00
|
|
|
if (has_collation)
|
|
|
|
queue_with_collation = SortingHeap<SortCursorWithCollation>(cursors);
|
|
|
|
else if (description.size() > 1)
|
2019-12-20 23:57:57 +00:00
|
|
|
queue_without_collation = SortingHeap<SortCursor>(cursors);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2019-12-22 01:49:38 +00:00
|
|
|
queue_simple = SortingHeap<SimpleSortCursor>(cursors);
|
2015-01-07 15:30:11 +00:00
|
|
|
}
|
2014-08-22 02:31:54 +00:00
|
|
|
|
|
|
|
|
2015-01-07 15:30:11 +00:00
|
|
|
Block MergeSortingBlocksBlockInputStream::readImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (blocks.empty())
|
|
|
|
return Block();
|
|
|
|
|
|
|
|
if (blocks.size() == 1)
|
|
|
|
{
|
|
|
|
Block res = blocks[0];
|
|
|
|
blocks.clear();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-12-22 01:49:38 +00:00
|
|
|
if (has_collation)
|
|
|
|
return mergeImpl(queue_with_collation);
|
|
|
|
else if (description.size() > 1)
|
|
|
|
return mergeImpl(queue_without_collation);
|
|
|
|
else
|
|
|
|
return mergeImpl(queue_simple);
|
2013-05-28 16:56:05 +00:00
|
|
|
}
|
2012-07-23 06:23:29 +00:00
|
|
|
|
2015-01-07 15:30:11 +00:00
|
|
|
|
2019-12-20 23:57:57 +00:00
|
|
|
template <typename TSortingHeap>
|
|
|
|
Block MergeSortingBlocksBlockInputStream::mergeImpl(TSortingHeap & queue)
|
2013-05-28 16:56:05 +00:00
|
|
|
{
|
2019-12-21 20:28:51 +00:00
|
|
|
size_t num_columns = header.columns();
|
|
|
|
MutableColumns merged_columns = header.cloneEmptyColumns();
|
2019-12-21 22:16:39 +00:00
|
|
|
|
|
|
|
/// Reserve
|
|
|
|
if (queue.isValid() && !blocks.empty())
|
|
|
|
{
|
|
|
|
/// The expected size of output block is the same as input block
|
|
|
|
size_t size_to_reserve = blocks[0].rows();
|
|
|
|
for (auto & column : merged_columns)
|
|
|
|
column->reserve(size_to_reserve);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-12-22 00:19:07 +00:00
|
|
|
/// TODO: Optimization when a single block left.
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Take rows from queue in right order and push to 'merged'.
|
|
|
|
size_t merged_rows = 0;
|
2019-12-20 23:57:57 +00:00
|
|
|
while (queue.isValid())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-12-20 23:57:57 +00:00
|
|
|
auto current = queue.current();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-12-21 20:28:51 +00:00
|
|
|
/// Append a row from queue.
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < num_columns; ++i)
|
|
|
|
merged_columns[i]->insertFrom(*current->all_columns[i], current->pos);
|
|
|
|
|
|
|
|
++total_merged_rows;
|
2019-12-21 20:28:51 +00:00
|
|
|
++merged_rows;
|
|
|
|
|
|
|
|
/// We don't need more rows because of limit has reached.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (limit && total_merged_rows == limit)
|
|
|
|
{
|
|
|
|
blocks.clear();
|
2019-12-21 20:28:51 +00:00
|
|
|
break;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:28:51 +00:00
|
|
|
queue.next();
|
|
|
|
|
|
|
|
/// It's enough for current output block but we will continue.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (merged_rows == max_merged_block_size)
|
2019-12-21 20:28:51 +00:00
|
|
|
break;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 22:16:39 +00:00
|
|
|
if (!queue.isValid())
|
|
|
|
blocks.clear();
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (merged_rows == 0)
|
2017-12-15 00:01:59 +00:00
|
|
|
return {};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-12-21 20:28:51 +00:00
|
|
|
return header.cloneWithColumns(std::move(merged_columns));
|
2012-07-23 20:01:29 +00:00
|
|
|
}
|
2012-07-23 06:23:29 +00:00
|
|
|
|
2015-01-07 15:30:11 +00:00
|
|
|
|
2018-09-24 20:07:30 +00:00
|
|
|
void MergeSortingBlockInputStream::remerge()
|
|
|
|
{
|
2018-09-26 01:30:07 +00:00
|
|
|
LOG_DEBUG(log, "Re-merging intermediate ORDER BY data (" << blocks.size() << " blocks with " << sum_rows_in_blocks << " rows) to save memory consumption");
|
2018-09-24 20:07:30 +00:00
|
|
|
|
2018-09-24 20:30:02 +00:00
|
|
|
/// NOTE Maybe concat all blocks and partial sort will be faster than merge?
|
2018-09-24 20:07:30 +00:00
|
|
|
MergeSortingBlocksBlockInputStream merger(blocks, description, max_merged_block_size, limit);
|
|
|
|
|
|
|
|
Blocks new_blocks;
|
2018-09-26 01:30:07 +00:00
|
|
|
size_t new_sum_rows_in_blocks = 0;
|
2018-09-24 20:07:30 +00:00
|
|
|
size_t new_sum_bytes_in_blocks = 0;
|
|
|
|
|
|
|
|
merger.readPrefix();
|
|
|
|
while (Block block = merger.read())
|
|
|
|
{
|
2018-09-26 01:30:07 +00:00
|
|
|
new_sum_rows_in_blocks += block.rows();
|
2018-09-24 20:07:30 +00:00
|
|
|
new_sum_bytes_in_blocks += block.allocatedBytes();
|
|
|
|
new_blocks.emplace_back(std::move(block));
|
|
|
|
}
|
|
|
|
merger.readSuffix();
|
|
|
|
|
|
|
|
LOG_DEBUG(log, "Memory usage is lowered from "
|
|
|
|
<< formatReadableSizeWithBinarySuffix(sum_bytes_in_blocks) << " to "
|
|
|
|
<< formatReadableSizeWithBinarySuffix(new_sum_bytes_in_blocks));
|
|
|
|
|
|
|
|
/// If the memory consumption was not lowered enough - we will not perform remerge anymore. 2 is a guess.
|
|
|
|
if (new_sum_bytes_in_blocks * 2 > sum_bytes_in_blocks)
|
|
|
|
remerge_is_useful = false;
|
|
|
|
|
|
|
|
blocks = std::move(new_blocks);
|
2018-09-26 01:30:07 +00:00
|
|
|
sum_rows_in_blocks = new_sum_rows_in_blocks;
|
2018-09-24 20:07:30 +00:00
|
|
|
sum_bytes_in_blocks = new_sum_bytes_in_blocks;
|
|
|
|
}
|
2011-09-04 01:42:14 +00:00
|
|
|
}
|