2017-06-25 00:01:10 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeBlockOutputStream.h>
|
2020-05-20 12:02:02 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataPartInMemory.h>
|
2017-06-25 00:01:10 +00:00
|
|
|
#include <Storages/StorageMergeTree.h>
|
|
|
|
#include <Interpreters/PartLog.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-02-19 00:45:32 +00:00
|
|
|
Block MergeTreeBlockOutputStream::getHeader() const
|
|
|
|
{
|
2020-06-16 15:51:29 +00:00
|
|
|
return metadata_snapshot->getSampleBlock();
|
2018-02-19 00:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-29 15:08:02 +00:00
|
|
|
void MergeTreeBlockOutputStream::writePrefix()
|
2017-06-25 00:01:10 +00:00
|
|
|
{
|
2020-11-29 15:08:02 +00:00
|
|
|
/// Only check "too many parts" before write,
|
|
|
|
/// because interrupting long-running INSERT query in the middle is not convenient for users.
|
2019-05-03 02:00:57 +00:00
|
|
|
storage.delayInsertOrThrowIfNeeded();
|
2020-11-29 15:08:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 00:01:10 +00:00
|
|
|
|
2020-11-29 15:08:02 +00:00
|
|
|
void MergeTreeBlockOutputStream::write(const Block & block)
|
|
|
|
{
|
2021-05-20 12:36:18 +00:00
|
|
|
auto part_blocks = storage.writer.splitBlockIntoParts(block, max_parts_per_block, metadata_snapshot);
|
2017-06-25 00:01:10 +00:00
|
|
|
for (auto & current_block : part_blocks)
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
|
2021-02-10 14:12:49 +00:00
|
|
|
MergeTreeData::MutableDataPartPtr part = storage.writer.writeTempPart(current_block, metadata_snapshot, context);
|
2021-02-12 14:02:04 +00:00
|
|
|
|
|
|
|
/// If optimize_on_insert setting is true, current_block could become empty after merge
|
|
|
|
/// and we didn't create part.
|
|
|
|
if (!part)
|
|
|
|
continue;
|
|
|
|
|
2021-04-02 17:54:24 +00:00
|
|
|
/// Part can be deduplicated, so increment counters and add to part log only if it's really added
|
2021-04-02 16:45:18 +00:00
|
|
|
if (storage.renameTempPartAndAdd(part, &storage.increment, nullptr, storage.getDeduplicationLog()))
|
2021-04-02 11:46:42 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
PartLog::addNewPart(storage.getContext(), part, watch.elapsed());
|
2017-06-25 00:01:10 +00:00
|
|
|
|
2021-04-02 16:45:18 +00:00
|
|
|
/// Initiate async merge - it will be done if it's good time for merge and if there are space in 'background_pool'.
|
|
|
|
storage.background_executor.triggerTask();
|
|
|
|
}
|
2017-06-25 00:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|