2019-06-18 12:54:27 +00:00
|
|
|
#include <Storages/MergeTree/IMergedBlockOutputStream.h>
|
2019-12-18 15:54:45 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeIOSettings.h>
|
2019-10-21 15:33:59 +00:00
|
|
|
#include <Storages/MergeTree/IMergeTreeDataPartWriter.h>
|
|
|
|
|
2019-06-18 12:54:27 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
IMergedBlockOutputStream::IMergedBlockOutputStream(
|
2020-06-16 15:51:29 +00:00
|
|
|
const MergeTreeDataPartPtr & data_part,
|
|
|
|
const StorageMetadataPtr & metadata_snapshot_)
|
2019-10-19 16:49:36 +00:00
|
|
|
: storage(data_part->storage)
|
2020-06-16 15:51:29 +00:00
|
|
|
, metadata_snapshot(metadata_snapshot_)
|
2020-05-11 17:20:26 +00:00
|
|
|
, volume(data_part->volume)
|
2020-04-14 19:47:19 +00:00
|
|
|
, part_path(data_part->isStoredOnDisk() ? data_part->getFullRelativePath() : "")
|
2019-06-18 12:54:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:09:55 +00:00
|
|
|
NameSet IMergedBlockOutputStream::removeEmptyColumnsFromPart(
|
|
|
|
const MergeTreeDataPartPtr & data_part,
|
|
|
|
NamesAndTypesList & columns,
|
|
|
|
MergeTreeData::DataPart::Checksums & checksums)
|
|
|
|
{
|
|
|
|
const NameSet & empty_columns = data_part->expired_columns;
|
|
|
|
|
|
|
|
/// For compact part we have to override whole file with data, it's not
|
|
|
|
/// worth it
|
|
|
|
if (empty_columns.empty() || isCompactPart(data_part))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
/// Collect counts for shared streams of different columns. As an example, Nested columns have shared stream with array sizes.
|
|
|
|
std::map<String, size_t> stream_counts;
|
|
|
|
for (const NameAndTypePair & column : columns)
|
|
|
|
{
|
2021-03-09 14:46:52 +00:00
|
|
|
auto serialization = data_part->getSerializationForColumn(column);
|
|
|
|
serialization->enumerateStreams(
|
|
|
|
[&](const ISerialization::SubstreamPath & substream_path)
|
2020-03-13 15:09:55 +00:00
|
|
|
{
|
2021-03-09 14:46:52 +00:00
|
|
|
++stream_counts[ISerialization::getFileNameForStream(column, substream_path)];
|
2020-03-13 15:09:55 +00:00
|
|
|
},
|
|
|
|
{});
|
|
|
|
}
|
|
|
|
|
|
|
|
NameSet remove_files;
|
|
|
|
const String mrk_extension = data_part->getMarksFileExtension();
|
|
|
|
for (const auto & column_name : empty_columns)
|
|
|
|
{
|
2020-10-21 23:02:20 +00:00
|
|
|
auto column_with_type = columns.tryGetByName(column_name);
|
|
|
|
if (!column_with_type)
|
|
|
|
continue;
|
|
|
|
|
2021-03-09 14:46:52 +00:00
|
|
|
ISerialization::StreamCallback callback = [&](const ISerialization::SubstreamPath & substream_path)
|
2020-03-13 17:38:26 +00:00
|
|
|
{
|
2021-03-09 14:46:52 +00:00
|
|
|
String stream_name = ISerialization::getFileNameForStream(*column_with_type, substream_path);
|
2020-03-13 15:09:55 +00:00
|
|
|
/// Delete files if they are no longer shared with another column.
|
|
|
|
if (--stream_counts[stream_name] == 0)
|
|
|
|
{
|
|
|
|
remove_files.emplace(stream_name + ".bin");
|
|
|
|
remove_files.emplace(stream_name + mrk_extension);
|
|
|
|
}
|
|
|
|
};
|
2020-10-21 23:02:20 +00:00
|
|
|
|
2021-03-09 14:46:52 +00:00
|
|
|
auto serialization = data_part->getSerializationForColumn(*column_with_type);
|
|
|
|
serialization->enumerateStreams(callback);
|
2020-03-13 15:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove files on disk and checksums
|
|
|
|
for (const String & removed_file : remove_files)
|
|
|
|
{
|
|
|
|
if (checksums.files.count(removed_file))
|
|
|
|
{
|
2021-01-14 16:24:13 +00:00
|
|
|
data_part->volume->getDisk()->removeFile(data_part->getFullRelativePath() + removed_file);
|
2020-03-13 15:09:55 +00:00
|
|
|
checksums.files.erase(removed_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove columns from columns array
|
|
|
|
for (const String & empty_column_name : empty_columns)
|
|
|
|
{
|
|
|
|
auto find_func = [&empty_column_name](const auto & pair) -> bool
|
|
|
|
{
|
|
|
|
return pair.name == empty_column_name;
|
|
|
|
};
|
|
|
|
auto remove_it
|
|
|
|
= std::find_if(columns.begin(), columns.end(), find_func);
|
|
|
|
|
|
|
|
if (remove_it != columns.end())
|
|
|
|
columns.erase(remove_it);
|
|
|
|
}
|
|
|
|
return remove_files;
|
|
|
|
}
|
|
|
|
|
2019-06-18 12:54:27 +00:00
|
|
|
}
|