ClickHouse/src/DataStreams/NativeReader.h

102 lines
2.9 KiB
C++
Raw Normal View History

2011-08-19 19:18:15 +00:00
#pragma once
#include <DataStreams/MarkInCompressedFile.h>
#include <Common/PODArray.h>
2021-10-14 10:25:43 +00:00
#include <Core/Block.h>
2011-08-19 19:18:15 +00:00
namespace DB
{
class CompressedReadBufferFromFile;
2017-05-13 22:19:04 +00:00
/** The Native format can contain a separately located index,
* which allows you to understand where what column is located,
* and skip unnecessary columns.
*/
2017-05-13 22:19:04 +00:00
/** The position of one piece of a single column. */
struct IndexOfOneColumnForNativeFormat
{
String name;
String type;
MarkInCompressedFile location;
};
2017-05-13 22:19:04 +00:00
/** The index for the data block. */
struct IndexOfBlockForNativeFormat
{
using Columns = std::vector<IndexOfOneColumnForNativeFormat>;
size_t num_columns;
size_t num_rows;
Columns columns;
};
2017-05-13 22:19:04 +00:00
/** The whole index. */
struct IndexForNativeFormat
{
using Blocks = std::vector<IndexOfBlockForNativeFormat>;
Blocks blocks;
IndexForNativeFormat() {}
IndexForNativeFormat(ReadBuffer & istr, const NameSet & required_columns)
{
read(istr, required_columns);
}
2017-05-13 22:19:04 +00:00
/// Read the index, only for the required columns.
void read(ReadBuffer & istr, const NameSet & required_columns);
};
2017-05-13 22:19:04 +00:00
/** Deserializes the stream of blocks from the native binary format (with names and column types).
* Designed for communication between servers.
*
2017-05-13 22:19:04 +00:00
* Can also be used to store data on disk.
* In this case, can use the index.
2011-08-19 19:18:15 +00:00
*/
2021-10-08 17:21:19 +00:00
class NativeReader
2011-08-19 19:18:15 +00:00
{
public:
/// If a non-zero server_revision is specified, additional block information may be expected and read.
2021-10-08 17:21:19 +00:00
NativeReader(ReadBuffer & istr_, UInt64 server_revision_);
/// For cases when data structure (header) is known in advance.
/// NOTE We may use header for data validation and/or type conversions. It is not implemented.
2021-10-08 17:21:19 +00:00
NativeReader(ReadBuffer & istr_, const Block & header_, UInt64 server_revision_);
/// For cases when we have an index. It allows to skip columns. Only columns specified in the index will be read.
2021-10-08 17:21:19 +00:00
NativeReader(ReadBuffer & istr_, UInt64 server_revision_,
IndexForNativeFormat::Blocks::const_iterator index_block_it_,
IndexForNativeFormat::Blocks::const_iterator index_block_end_);
2020-11-10 17:32:00 +00:00
static void readData(const IDataType & type, ColumnPtr & column, ReadBuffer & istr, size_t rows, double avg_value_size_hint);
2021-10-08 17:21:19 +00:00
Block getHeader() const;
void resetParser();
2021-10-08 17:21:19 +00:00
Block read();
2012-10-20 02:10:47 +00:00
2011-08-19 19:18:15 +00:00
private:
ReadBuffer & istr;
Block header;
UInt64 server_revision;
bool use_index = false;
IndexForNativeFormat::Blocks::const_iterator index_block_it;
IndexForNativeFormat::Blocks::const_iterator index_block_end;
IndexOfBlockForNativeFormat::Columns::const_iterator index_column_it;
2018-12-19 17:54:46 +00:00
/// If an index is specified, then `istr` must be CompressedReadBufferFromFile. Unused otherwise.
CompressedReadBufferFromFile * istr_concrete = nullptr;
PODArray<double> avg_value_size_hints;
void updateAvgValueSizeHints(const Block & block);
2011-08-19 19:18:15 +00:00
};
}