2019-10-21 15:33:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-04-14 01:26:34 +00:00
|
|
|
#include <Storages/MergeTree/IMergeTreeDataPartWriter.h>
|
2019-10-16 18:27:53 +00:00
|
|
|
#include <IO/WriteBufferFromFile.h>
|
2019-10-19 16:49:36 +00:00
|
|
|
#include <IO/WriteBufferFromFileBase.h>
|
2019-10-16 18:27:53 +00:00
|
|
|
#include <Compression/CompressedWriteBuffer.h>
|
|
|
|
#include <IO/HashingWriteBuffer.h>
|
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
|
|
|
#include <DataStreams/IBlockOutputStream.h>
|
2019-11-05 11:53:22 +00:00
|
|
|
#include <Storages/MergeTree/IMergeTreeDataPart.h>
|
2020-02-27 16:47:40 +00:00
|
|
|
#include <Disks/IDisk.h>
|
2019-10-16 18:27:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-02-03 12:08:40 +00:00
|
|
|
|
|
|
|
/// Writes data part to disk in different formats.
|
|
|
|
/// Calculates and serializes primary and skip indices if needed.
|
2020-04-14 01:26:34 +00:00
|
|
|
class MergeTreeDataPartWriterOnDisk : public IMergeTreeDataPartWriter
|
2019-10-16 18:27:53 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using WrittenOffsetColumns = std::set<std::string>;
|
|
|
|
|
2020-02-19 14:07:36 +00:00
|
|
|
/// Helper class, which holds chain of buffers to write data file with marks.
|
|
|
|
/// It is used to write: one column, skip index or all columns (in compact format).
|
|
|
|
struct Stream
|
2019-10-16 18:27:53 +00:00
|
|
|
{
|
2020-02-19 14:07:36 +00:00
|
|
|
Stream(
|
2019-10-16 18:27:53 +00:00
|
|
|
const String & escaped_column_name_,
|
2020-02-27 16:47:40 +00:00
|
|
|
DiskPtr disk_,
|
2019-10-16 18:27:53 +00:00
|
|
|
const String & data_path_,
|
|
|
|
const std::string & data_file_extension_,
|
|
|
|
const std::string & marks_path_,
|
|
|
|
const std::string & marks_file_extension_,
|
|
|
|
const CompressionCodecPtr & compression_codec_,
|
|
|
|
size_t max_compress_block_size_,
|
|
|
|
size_t estimated_size_,
|
|
|
|
size_t aio_threshold_);
|
|
|
|
|
|
|
|
String escaped_column_name;
|
|
|
|
std::string data_file_extension;
|
|
|
|
std::string marks_file_extension;
|
|
|
|
|
|
|
|
/// compressed -> compressed_buf -> plain_hashing -> plain_file
|
|
|
|
std::unique_ptr<WriteBufferFromFileBase> plain_file;
|
2019-10-20 23:36:27 +00:00
|
|
|
HashingWriteBuffer plain_hashing;
|
2019-10-16 18:27:53 +00:00
|
|
|
CompressedWriteBuffer compressed_buf;
|
|
|
|
HashingWriteBuffer compressed;
|
|
|
|
|
|
|
|
/// marks -> marks_file
|
2020-02-27 16:47:40 +00:00
|
|
|
std::unique_ptr<WriteBufferFromFileBase> marks_file;
|
2019-10-16 18:27:53 +00:00
|
|
|
HashingWriteBuffer marks;
|
|
|
|
|
|
|
|
void finalize();
|
|
|
|
|
2020-05-20 22:16:08 +00:00
|
|
|
void sync() const;
|
2019-10-16 18:27:53 +00:00
|
|
|
|
2019-10-21 00:28:29 +00:00
|
|
|
void addToChecksums(IMergeTreeDataPart::Checksums & checksums);
|
2019-10-16 18:27:53 +00:00
|
|
|
};
|
|
|
|
|
2020-02-19 14:07:36 +00:00
|
|
|
using StreamPtr = std::unique_ptr<Stream>;
|
2019-10-19 16:49:36 +00:00
|
|
|
|
2020-04-14 01:26:34 +00:00
|
|
|
MergeTreeDataPartWriterOnDisk(
|
2020-02-27 16:47:40 +00:00
|
|
|
DiskPtr disk,
|
2019-10-19 16:49:36 +00:00
|
|
|
const String & part_path,
|
|
|
|
const MergeTreeData & storage,
|
|
|
|
const NamesAndTypesList & columns_list,
|
2019-12-18 16:41:11 +00:00
|
|
|
const std::vector<MergeTreeIndexPtr> & indices_to_recalc,
|
2019-10-19 16:49:36 +00:00
|
|
|
const String & marks_file_extension,
|
|
|
|
const CompressionCodecPtr & default_codec,
|
2019-12-18 15:54:45 +00:00
|
|
|
const MergeTreeWriterSettings & settings,
|
2020-04-30 15:27:39 +00:00
|
|
|
const MergeTreeIndexGranularity & index_granularity);
|
2019-10-19 16:49:36 +00:00
|
|
|
|
2020-04-14 01:26:34 +00:00
|
|
|
void calculateAndSerializePrimaryIndex(const Block & primary_index_block) final;
|
|
|
|
void calculateAndSerializeSkipIndices(const Block & skip_indexes_block) final;
|
2019-10-16 18:27:53 +00:00
|
|
|
|
2020-04-30 15:27:39 +00:00
|
|
|
/// Count index_granularity for block and store in `index_granularity`
|
|
|
|
size_t computeIndexGranularity(const Block & block);
|
|
|
|
virtual void fillIndexGranularity(size_t index_granularity_for_block, size_t rows_in_block);
|
2019-11-05 11:53:22 +00:00
|
|
|
|
2020-04-14 01:26:34 +00:00
|
|
|
void initSkipIndices() final;
|
|
|
|
void initPrimaryIndex() final;
|
2019-11-07 11:11:38 +00:00
|
|
|
|
2020-05-14 20:08:15 +00:00
|
|
|
void finishPrimaryIndexSerialization(MergeTreeData::DataPart::Checksums & checksums) final;
|
|
|
|
void finishSkipIndicesSerialization(MergeTreeData::DataPart::Checksums & checksums) final;
|
2019-11-07 11:11:38 +00:00
|
|
|
|
2019-12-19 14:05:26 +00:00
|
|
|
void setWrittenOffsetColumns(WrittenOffsetColumns * written_offset_columns_)
|
2019-12-09 21:21:17 +00:00
|
|
|
{
|
|
|
|
written_offset_columns = written_offset_columns_;
|
|
|
|
}
|
|
|
|
|
2019-10-16 18:27:53 +00:00
|
|
|
protected:
|
2019-10-19 16:49:36 +00:00
|
|
|
using SerializationState = IDataType::SerializeBinaryBulkStatePtr;
|
2019-11-07 11:11:38 +00:00
|
|
|
using SerializationStates = std::unordered_map<String, SerializationState>;
|
2019-10-19 16:49:36 +00:00
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
DiskPtr disk;
|
2019-10-19 16:49:36 +00:00
|
|
|
String part_path;
|
|
|
|
const String marks_file_extension;
|
|
|
|
CompressionCodecPtr default_codec;
|
|
|
|
|
2019-11-05 11:53:22 +00:00
|
|
|
bool compute_granularity;
|
2019-11-26 09:48:22 +00:00
|
|
|
bool need_finish_last_granule;
|
2019-11-05 11:53:22 +00:00
|
|
|
|
2020-02-19 14:07:36 +00:00
|
|
|
/// Number of marsk in data from which skip indices have to start
|
2019-11-05 11:53:22 +00:00
|
|
|
/// aggregation. I.e. it's data mark number, not skip indices mark.
|
|
|
|
size_t skip_index_data_mark = 0;
|
|
|
|
|
2020-02-19 14:07:36 +00:00
|
|
|
std::vector<StreamPtr> skip_indices_streams;
|
2019-11-05 11:53:22 +00:00
|
|
|
MergeTreeIndexAggregators skip_indices_aggregators;
|
|
|
|
std::vector<size_t> skip_index_filling;
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
std::unique_ptr<WriteBufferFromFileBase> index_file_stream;
|
2019-11-05 11:53:22 +00:00
|
|
|
std::unique_ptr<HashingWriteBuffer> index_stream;
|
|
|
|
DataTypes index_types;
|
|
|
|
/// Index columns values from the last row from the last block
|
|
|
|
/// It's written to index file in the `writeSuffixAndFinalizePart` method
|
|
|
|
Row last_index_row;
|
|
|
|
|
2019-11-07 11:11:38 +00:00
|
|
|
bool data_written = false;
|
2019-11-08 14:36:10 +00:00
|
|
|
bool primary_index_initialized = false;
|
|
|
|
bool skip_indices_initialized = false;
|
2019-12-03 14:33:56 +00:00
|
|
|
|
2019-12-09 21:21:17 +00:00
|
|
|
/// To correctly write Nested elements column-by-column.
|
|
|
|
WrittenOffsetColumns * written_offset_columns = nullptr;
|
2020-04-30 15:27:39 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Index is already serialized up to this mark.
|
|
|
|
size_t index_mark = 0;
|
2019-10-16 18:27:53 +00:00
|
|
|
};
|
|
|
|
|
2019-10-19 16:49:36 +00:00
|
|
|
}
|