2019-10-10 16:30:30 +00:00
|
|
|
#include "MergeTreeDataPartWide.h"
|
2018-03-14 03:19:23 +00:00
|
|
|
|
2019-05-31 04:03:46 +00:00
|
|
|
#include <optional>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2018-12-28 18:15:26 +00:00
|
|
|
#include <Compression/CompressedReadBuffer.h>
|
|
|
|
#include <Compression/CompressedWriteBuffer.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/ReadBufferFromFile.h>
|
2017-08-31 15:40:34 +00:00
|
|
|
#include <IO/HashingWriteBuffer.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Defines.h>
|
|
|
|
#include <Common/SipHash.h>
|
|
|
|
#include <Common/escapeForFileName.h>
|
2018-01-15 19:07:47 +00:00
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
2018-05-21 13:49:54 +00:00
|
|
|
#include <Common/localBackup.h>
|
2018-12-21 13:25:39 +00:00
|
|
|
#include <Compression/CompressionInfo.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
2016-02-14 04:58:47 +00:00
|
|
|
#include <Poco/File.h>
|
2017-05-16 15:40:32 +00:00
|
|
|
#include <Poco/Path.h>
|
|
|
|
#include <Poco/DirectoryIterator.h>
|
|
|
|
#include <common/logger_useful.h>
|
2019-04-15 09:30:45 +00:00
|
|
|
#include <common/JSON.h>
|
2016-02-14 04:58:47 +00:00
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeReaderWide.h>
|
|
|
|
#include <Storages/MergeTree/IMergeTreeReader.h>
|
2019-10-21 17:23:06 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataPartWriterWide.h>
|
|
|
|
#include <Storages/MergeTree/IMergeTreeDataPartWriter.h>
|
2019-10-10 16:30:30 +00:00
|
|
|
|
|
|
|
|
2016-02-14 04:58:47 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
// namespace
|
|
|
|
// {
|
|
|
|
// }
|
|
|
|
|
2016-02-14 04:58:47 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int FILE_DOESNT_EXIST;
|
|
|
|
extern const int NO_FILE_IN_DATA_PART;
|
|
|
|
extern const int EXPECTED_END_OF_FILE;
|
2017-09-11 17:55:41 +00:00
|
|
|
extern const int CORRUPTED_DATA;
|
2017-11-20 19:33:12 +00:00
|
|
|
extern const int NOT_FOUND_EXPECTED_DATA_PART;
|
2018-03-21 20:21:34 +00:00
|
|
|
extern const int BAD_SIZE_OF_FILE_IN_DATA_PART;
|
2019-04-15 09:30:45 +00:00
|
|
|
extern const int BAD_TTL_FILE;
|
2019-06-16 19:32:52 +00:00
|
|
|
extern const int CANNOT_UNLINK;
|
2016-02-14 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-31 14:44:17 +00:00
|
|
|
// static ReadBufferFromFile openForReading(const String & path)
|
|
|
|
// {
|
|
|
|
// return ReadBufferFromFile(path, std::min(static_cast<Poco::File::FileSize>(DBMS_DEFAULT_BUFFER_SIZE), Poco::File(path).getSize()));
|
|
|
|
// }
|
2017-08-31 15:40:34 +00:00
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
MergeTreeDataPartWide::MergeTreeDataPartWide(
|
|
|
|
MergeTreeData & storage_,
|
|
|
|
const String & name_,
|
2019-10-16 18:27:53 +00:00
|
|
|
const MergeTreeIndexGranularityInfo & index_granularity_info_,
|
2019-10-10 16:30:30 +00:00
|
|
|
const DiskSpace::DiskPtr & disk_,
|
|
|
|
const std::optional<String> & relative_path_)
|
2019-10-16 18:27:53 +00:00
|
|
|
: IMergeTreeDataPart(storage_, name_, index_granularity_info_, disk_, relative_path_)
|
2017-08-16 19:24:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
MergeTreeDataPartWide::MergeTreeDataPartWide(
|
|
|
|
const MergeTreeData & storage_,
|
|
|
|
const String & name_,
|
|
|
|
const MergeTreePartInfo & info_,
|
2019-10-16 18:27:53 +00:00
|
|
|
const MergeTreeIndexGranularityInfo & index_granularity_info_,
|
2019-10-10 16:30:30 +00:00
|
|
|
const DiskSpace::DiskPtr & disk_,
|
|
|
|
const std::optional<String> & relative_path_)
|
2019-10-16 18:27:53 +00:00
|
|
|
: IMergeTreeDataPart(storage_, name_, info_, index_granularity_info_, disk_, relative_path_)
|
2017-08-25 20:41:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
IMergeTreeDataPart::MergeTreeReaderPtr MergeTreeDataPartWide::getReader(
|
|
|
|
const NamesAndTypesList & columns_to_read,
|
|
|
|
const MarkRanges & mark_ranges,
|
|
|
|
UncompressedCache * uncompressed_cache,
|
|
|
|
MarkCache * mark_cache,
|
|
|
|
const ReaderSettings & reader_settings,
|
|
|
|
const ValueSizeMap & avg_value_size_hints,
|
|
|
|
const ReadBufferFromFileBase::ProfileCallback & profile_callback) const
|
2019-03-22 12:56:58 +00:00
|
|
|
{
|
2019-10-10 16:30:30 +00:00
|
|
|
return std::make_unique<MergeTreeReaderWide>(shared_from_this(), columns_to_read, uncompressed_cache,
|
|
|
|
mark_cache, mark_ranges, reader_settings, avg_value_size_hints, profile_callback);
|
2019-03-22 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:33:59 +00:00
|
|
|
IMergeTreeDataPart::MergeTreeWriterPtr MergeTreeDataPartWide::getWriter(
|
|
|
|
const NamesAndTypesList & columns_list,
|
|
|
|
const CompressionCodecPtr & default_codec,
|
|
|
|
const WriterSettings & writer_settings) const
|
|
|
|
{
|
2019-10-21 17:23:06 +00:00
|
|
|
return std::make_unique<MergeTreeDataPartWriterWide>(
|
|
|
|
getFullPath(), storage, columns_list,
|
|
|
|
index_granularity_info.marks_file_extension,
|
|
|
|
default_codec, writer_settings);
|
2019-10-21 15:33:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 12:56:58 +00:00
|
|
|
|
2018-03-26 14:18:04 +00:00
|
|
|
/// Takes into account the fact that several columns can e.g. share their .size substreams.
|
|
|
|
/// When calculating totals these should be counted only once.
|
2019-10-10 16:30:30 +00:00
|
|
|
ColumnSize MergeTreeDataPartWide::getColumnSizeImpl(
|
2019-01-04 12:10:00 +00:00
|
|
|
const String & column_name, const IDataType & type, std::unordered_set<String> * processed_substreams) const
|
2017-03-24 13:52:50 +00:00
|
|
|
{
|
2018-03-26 14:18:04 +00:00
|
|
|
ColumnSize size;
|
|
|
|
if (checksums.empty())
|
|
|
|
return size;
|
|
|
|
|
|
|
|
type.enumerateStreams([&](const IDataType::SubstreamPath & substream_path)
|
|
|
|
{
|
2019-01-04 12:10:00 +00:00
|
|
|
String file_name = IDataType::getFileNameForStream(column_name, substream_path);
|
2018-03-26 14:18:04 +00:00
|
|
|
|
|
|
|
if (processed_substreams && !processed_substreams->insert(file_name).second)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto bin_checksum = checksums.files.find(file_name + ".bin");
|
|
|
|
if (bin_checksum != checksums.files.end())
|
|
|
|
{
|
|
|
|
size.data_compressed += bin_checksum->second.file_size;
|
|
|
|
size.data_uncompressed += bin_checksum->second.uncompressed_size;
|
|
|
|
}
|
|
|
|
|
2019-06-19 10:07:56 +00:00
|
|
|
auto mrk_checksum = checksums.files.find(file_name + index_granularity_info.marks_file_extension);
|
2018-03-26 14:18:04 +00:00
|
|
|
if (mrk_checksum != checksums.files.end())
|
|
|
|
size.marks += mrk_checksum->second.file_size;
|
|
|
|
}, {});
|
2016-02-14 04:58:47 +00:00
|
|
|
|
2018-03-26 14:18:04 +00:00
|
|
|
return size;
|
2017-03-24 13:52:50 +00:00
|
|
|
}
|
2016-02-14 04:58:47 +00:00
|
|
|
|
|
|
|
/** Returns the name of a column with minimum compressed size (as returned by getColumnSize()).
|
2017-06-05 20:43:23 +00:00
|
|
|
* If no checksums are present returns the name of the first physically existing column.
|
|
|
|
*/
|
2019-10-10 16:30:30 +00:00
|
|
|
String MergeTreeDataPartWide::getColumnNameWithMinumumCompressedSize() const
|
2016-02-14 04:58:47 +00:00
|
|
|
{
|
2019-01-04 12:10:00 +00:00
|
|
|
const auto & storage_columns = storage.getColumns().getAllPhysical();
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string * minimum_size_column = nullptr;
|
2018-03-03 15:36:20 +00:00
|
|
|
UInt64 minimum_size = std::numeric_limits<UInt64>::max();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
for (const auto & column : storage_columns)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-06-16 19:32:52 +00:00
|
|
|
if (!hasColumnFiles(column.name, *column.type))
|
2017-04-01 07:20:54 +00:00
|
|
|
continue;
|
|
|
|
|
2019-10-16 18:27:53 +00:00
|
|
|
const auto size = getColumnSizeImpl(column.name, *column.type, nullptr).data_compressed;
|
2017-04-01 07:20:54 +00:00
|
|
|
if (size < minimum_size)
|
|
|
|
{
|
|
|
|
minimum_size = size;
|
|
|
|
minimum_size_column = &column.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!minimum_size_column)
|
2017-08-17 10:28:57 +00:00
|
|
|
throw Exception("Could not find a column of minimum size in MergeTree, part " + getFullPath(), ErrorCodes::LOGICAL_ERROR);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return *minimum_size_column;
|
2016-02-14 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
void MergeTreeDataPartWide::loadIndexGranularity()
|
2016-02-14 04:58:47 +00:00
|
|
|
{
|
2019-06-19 10:07:56 +00:00
|
|
|
String full_path = getFullPath();
|
2019-10-16 18:27:53 +00:00
|
|
|
index_granularity_info.changeGranularityIfRequired(full_path);
|
2019-06-19 10:07:56 +00:00
|
|
|
|
2018-11-15 14:06:54 +00:00
|
|
|
if (columns.empty())
|
|
|
|
throw Exception("No columns in part " + name, ErrorCodes::NO_FILE_IN_DATA_PART);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-15 14:06:54 +00:00
|
|
|
/// We can use any column, it doesn't matter
|
2019-06-19 12:30:56 +00:00
|
|
|
std::string marks_file_path = index_granularity_info.getMarksFilePath(full_path + escapeForFileName(columns.front().name));
|
2019-03-25 13:55:24 +00:00
|
|
|
if (!Poco::File(marks_file_path).exists())
|
|
|
|
throw Exception("Marks file '" + marks_file_path + "' doesn't exist", ErrorCodes::NO_FILE_IN_DATA_PART);
|
2018-11-15 14:06:54 +00:00
|
|
|
|
|
|
|
size_t marks_file_size = Poco::File(marks_file_path).getSize();
|
2018-11-12 17:44:43 +00:00
|
|
|
|
2019-06-19 10:07:56 +00:00
|
|
|
if (!index_granularity_info.is_adaptive)
|
2018-11-15 14:06:54 +00:00
|
|
|
{
|
2019-06-19 10:07:56 +00:00
|
|
|
size_t marks_count = marks_file_size / index_granularity_info.mark_size_in_bytes;
|
|
|
|
index_granularity.resizeWithFixedGranularity(marks_count, index_granularity_info.fixed_index_granularity); /// all the same
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-11-15 14:06:54 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ReadBufferFromFile buffer(marks_file_path, marks_file_size, -1);
|
|
|
|
while (!buffer.eof())
|
|
|
|
{
|
|
|
|
buffer.seek(sizeof(size_t) * 2, SEEK_CUR); /// skip offset_in_compressed file and offset_in_decompressed_block
|
2019-03-25 13:55:24 +00:00
|
|
|
size_t granularity;
|
|
|
|
readIntBinary(granularity, buffer);
|
|
|
|
index_granularity.appendMark(granularity);
|
2018-11-15 14:06:54 +00:00
|
|
|
}
|
2019-10-31 14:44:17 +00:00
|
|
|
|
2019-06-19 10:07:56 +00:00
|
|
|
if (index_granularity.getMarksCount() * index_granularity_info.mark_size_in_bytes != marks_file_size)
|
2018-11-15 14:06:54 +00:00
|
|
|
throw Exception("Cannot read all marks from file " + marks_file_path, ErrorCodes::CANNOT_READ_ALL_DATA);
|
|
|
|
}
|
2016-02-14 04:58:47 +00:00
|
|
|
|
2019-10-31 14:44:17 +00:00
|
|
|
index_granularity.setInitialized();
|
2019-04-15 09:30:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
// void MergeTreeDataPartWide::accumulateColumnSizes(ColumnToSize & column_to_size) const
|
|
|
|
// {
|
|
|
|
// std::shared_lock<std::shared_mutex> part_lock(columns_lock);
|
2017-12-02 22:12:27 +00:00
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
// for (const NameAndTypePair & name_type : storage.getColumns().getAllPhysical())
|
|
|
|
// {
|
|
|
|
// IDataType::SubstreamPath path;
|
|
|
|
// name_type.type->enumerateStreams([&](const IDataType::SubstreamPath & substream_path)
|
|
|
|
// {
|
|
|
|
// Poco::File bin_file(getFullPath() + IDataType::getFileNameForStream(name_type.name, substream_path) + ".bin");
|
|
|
|
// if (bin_file.exists())
|
|
|
|
// column_to_size[name_type.name] += bin_file.getSize();
|
|
|
|
// }, path);
|
|
|
|
// }
|
|
|
|
// }
|
2016-02-14 04:58:47 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
// bool MergeTreeDataPartWide::hasColumnFiles(const String & column_name, const IDataType & type) const
|
|
|
|
// {
|
|
|
|
// bool res = true;
|
2019-01-05 03:33:22 +00:00
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
// type.enumerateStreams([&](const IDataType::SubstreamPath & substream_path)
|
|
|
|
// {
|
|
|
|
// String file_name = IDataType::getFileNameForStream(column_name, substream_path);
|
2017-09-11 22:40:51 +00:00
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
// auto bin_checksum = checksums.files.find(file_name + ".bin");
|
|
|
|
// auto mrk_checksum = checksums.files.find(file_name + index_granularity_info.marks_file_extension);
|
2017-09-11 22:40:51 +00:00
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
// if (bin_checksum == checksums.files.end() || mrk_checksum == checksums.files.end())
|
|
|
|
// res = false;
|
|
|
|
// }, {});
|
2017-11-20 19:33:12 +00:00
|
|
|
|
2019-10-10 16:30:30 +00:00
|
|
|
// return res;
|
|
|
|
// }
|
2017-11-20 19:33:12 +00:00
|
|
|
|
2016-02-14 04:58:47 +00:00
|
|
|
}
|