2021-01-11 01:50:30 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-09-28 12:57:31 +00:00
|
|
|
#include "config.h"
|
2021-01-11 01:50:30 +00:00
|
|
|
|
|
|
|
#if USE_PROTOBUF
|
|
|
|
# include <Columns/IColumn.h>
|
2021-12-15 11:30:57 +00:00
|
|
|
#include <Core/NamesAndTypes.h>
|
2021-01-11 01:50:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace google::protobuf { class Descriptor; }
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
class ProtobufReader;
|
|
|
|
class ProtobufWriter;
|
|
|
|
class IDataType;
|
|
|
|
using DataTypePtr = std::shared_ptr<const IDataType>;
|
|
|
|
using DataTypes = std::vector<DataTypePtr>;
|
2021-12-01 18:19:47 +00:00
|
|
|
class WriteBuffer;
|
2021-01-11 01:50:30 +00:00
|
|
|
|
|
|
|
/// Utility class, does all the work for serialization in the Protobuf format.
|
|
|
|
class ProtobufSerializer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~ProtobufSerializer() = default;
|
|
|
|
|
|
|
|
virtual void setColumns(const ColumnPtr * columns, size_t num_columns) = 0;
|
|
|
|
virtual void writeRow(size_t row_num) = 0;
|
Implement ProtobufList - fixes ClickHouse#16436
Introduce IO format "ProtobufList" with protobuf schema
// schemafile.proto
message Envelope {
message MessageType {
uint32 colA = 1;
string colB = 2;
}
repeated MessageType mt = 1;
}
where "Envelope" is a hard-coded/expected top-level message and
"MessageType" is a message with user-provided name containing the table
fields to export/import, e.g.
SELECT * FROM db1.tab1 FORMAT ProtobufList SETTINGS format_schema =
'schemafile:MessageType'
As a result, the new format wraps a list of messages (one per row) into
a single, containing message. Compare that to the schema of the existing
IO formats "Protobuf" and "ProtobufSingle":
message MessageType {
uint32 colA = 1;
string colB = 2;
}
The new format does not save space compared to the existing formats, but
it is conceptually a bit more beautiful and also more convenenient.
Implementation details:
- Created new files ProtobufList(Input|Output)Format which use the
existing ProtobufSerializer mechanism. The goal was to reuse as much
code as possible and avoid copypasta.
- I was torn between inheriting from I(Input|Output)Format vs.
IRow(Input|Output)Format for ProtobufList(Input|Output)Format. The
former is chunk-based which can be better for performance. Since the
ProtobufSerializer mechanism is row-based but data is generally passed
around in chunks, I decided for the latter to leverage the existing
chunk <--> row mapping code in IRow(InputOutput)Format.
- A new ProtobufSerializer called ProtobufSerializerEnvelope was
introduced (--> ProtobufSerializer.cpp). It represents the top-level
message which encloses the list of inner nested messages, i.e. the
rows.
- With the new format, parsing the schema file and matching the fields in
the schema file to table column works like for the old formats. The only
difference is that parsing starts one level below the "Envelope" (-->
ProtobufSchema.cpp). This is more natural than forcing customers to
have table columns start with "Envelope".
- Creation of the ProtobufSerializer tree also works like before. What
is different is that we finally add a ProtobufSerializerEnvelope as
new root of the tree. It's only purpose is to write/read the top-level
message for the first/last row to write/read.
Caveats:
- The low-level serialization code in ProtobufWriter uses an internal
buffer which is flushed to the output file only in endMessage().
In the existing "Protobuf" format, this happens once per row, in the
new format this happens only at the end of the serialization
since row-level messages now call start/endNestedMessage(). As a
future TODO to, the buffer should be flushed also in
start/endNestedMessage() to reduce memory consumption.
2022-03-13 19:24:46 +00:00
|
|
|
virtual void finalizeWrite() {}
|
2022-10-28 16:41:10 +00:00
|
|
|
virtual void reset() {}
|
2021-01-11 01:50:30 +00:00
|
|
|
|
|
|
|
virtual void setColumns(const MutableColumnPtr * columns, size_t num_columns) = 0;
|
|
|
|
virtual void readRow(size_t row_num) = 0;
|
|
|
|
virtual void insertDefaults(size_t row_num) = 0;
|
|
|
|
|
2021-12-01 18:19:47 +00:00
|
|
|
virtual void describeTree(WriteBuffer & out, size_t indent) const = 0;
|
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
static std::unique_ptr<ProtobufSerializer> create(
|
|
|
|
const Strings & column_names,
|
|
|
|
const DataTypes & data_types,
|
|
|
|
std::vector<size_t> & missing_column_indices,
|
|
|
|
const google::protobuf::Descriptor & message_descriptor,
|
|
|
|
bool with_length_delimiter,
|
Implement ProtobufList - fixes ClickHouse#16436
Introduce IO format "ProtobufList" with protobuf schema
// schemafile.proto
message Envelope {
message MessageType {
uint32 colA = 1;
string colB = 2;
}
repeated MessageType mt = 1;
}
where "Envelope" is a hard-coded/expected top-level message and
"MessageType" is a message with user-provided name containing the table
fields to export/import, e.g.
SELECT * FROM db1.tab1 FORMAT ProtobufList SETTINGS format_schema =
'schemafile:MessageType'
As a result, the new format wraps a list of messages (one per row) into
a single, containing message. Compare that to the schema of the existing
IO formats "Protobuf" and "ProtobufSingle":
message MessageType {
uint32 colA = 1;
string colB = 2;
}
The new format does not save space compared to the existing formats, but
it is conceptually a bit more beautiful and also more convenenient.
Implementation details:
- Created new files ProtobufList(Input|Output)Format which use the
existing ProtobufSerializer mechanism. The goal was to reuse as much
code as possible and avoid copypasta.
- I was torn between inheriting from I(Input|Output)Format vs.
IRow(Input|Output)Format for ProtobufList(Input|Output)Format. The
former is chunk-based which can be better for performance. Since the
ProtobufSerializer mechanism is row-based but data is generally passed
around in chunks, I decided for the latter to leverage the existing
chunk <--> row mapping code in IRow(InputOutput)Format.
- A new ProtobufSerializer called ProtobufSerializerEnvelope was
introduced (--> ProtobufSerializer.cpp). It represents the top-level
message which encloses the list of inner nested messages, i.e. the
rows.
- With the new format, parsing the schema file and matching the fields in
the schema file to table column works like for the old formats. The only
difference is that parsing starts one level below the "Envelope" (-->
ProtobufSchema.cpp). This is more natural than forcing customers to
have table columns start with "Envelope".
- Creation of the ProtobufSerializer tree also works like before. What
is different is that we finally add a ProtobufSerializerEnvelope as
new root of the tree. It's only purpose is to write/read the top-level
message for the first/last row to write/read.
Caveats:
- The low-level serialization code in ProtobufWriter uses an internal
buffer which is flushed to the output file only in endMessage().
In the existing "Protobuf" format, this happens once per row, in the
new format this happens only at the end of the serialization
since row-level messages now call start/endNestedMessage(). As a
future TODO to, the buffer should be flushed also in
start/endNestedMessage() to reduce memory consumption.
2022-03-13 19:24:46 +00:00
|
|
|
bool with_envelope,
|
2022-04-27 11:47:28 +00:00
|
|
|
bool flatten_google_wrappers,
|
2021-01-11 01:50:30 +00:00
|
|
|
ProtobufReader & reader);
|
|
|
|
|
|
|
|
static std::unique_ptr<ProtobufSerializer> create(
|
|
|
|
const Strings & column_names,
|
|
|
|
const DataTypes & data_types,
|
|
|
|
const google::protobuf::Descriptor & message_descriptor,
|
|
|
|
bool with_length_delimiter,
|
Implement ProtobufList - fixes ClickHouse#16436
Introduce IO format "ProtobufList" with protobuf schema
// schemafile.proto
message Envelope {
message MessageType {
uint32 colA = 1;
string colB = 2;
}
repeated MessageType mt = 1;
}
where "Envelope" is a hard-coded/expected top-level message and
"MessageType" is a message with user-provided name containing the table
fields to export/import, e.g.
SELECT * FROM db1.tab1 FORMAT ProtobufList SETTINGS format_schema =
'schemafile:MessageType'
As a result, the new format wraps a list of messages (one per row) into
a single, containing message. Compare that to the schema of the existing
IO formats "Protobuf" and "ProtobufSingle":
message MessageType {
uint32 colA = 1;
string colB = 2;
}
The new format does not save space compared to the existing formats, but
it is conceptually a bit more beautiful and also more convenenient.
Implementation details:
- Created new files ProtobufList(Input|Output)Format which use the
existing ProtobufSerializer mechanism. The goal was to reuse as much
code as possible and avoid copypasta.
- I was torn between inheriting from I(Input|Output)Format vs.
IRow(Input|Output)Format for ProtobufList(Input|Output)Format. The
former is chunk-based which can be better for performance. Since the
ProtobufSerializer mechanism is row-based but data is generally passed
around in chunks, I decided for the latter to leverage the existing
chunk <--> row mapping code in IRow(InputOutput)Format.
- A new ProtobufSerializer called ProtobufSerializerEnvelope was
introduced (--> ProtobufSerializer.cpp). It represents the top-level
message which encloses the list of inner nested messages, i.e. the
rows.
- With the new format, parsing the schema file and matching the fields in
the schema file to table column works like for the old formats. The only
difference is that parsing starts one level below the "Envelope" (-->
ProtobufSchema.cpp). This is more natural than forcing customers to
have table columns start with "Envelope".
- Creation of the ProtobufSerializer tree also works like before. What
is different is that we finally add a ProtobufSerializerEnvelope as
new root of the tree. It's only purpose is to write/read the top-level
message for the first/last row to write/read.
Caveats:
- The low-level serialization code in ProtobufWriter uses an internal
buffer which is flushed to the output file only in endMessage().
In the existing "Protobuf" format, this happens once per row, in the
new format this happens only at the end of the serialization
since row-level messages now call start/endNestedMessage(). As a
future TODO to, the buffer should be flushed also in
start/endNestedMessage() to reduce memory consumption.
2022-03-13 19:24:46 +00:00
|
|
|
bool with_envelope,
|
2022-04-27 11:47:28 +00:00
|
|
|
bool defaults_for_nullable_google_wrappers,
|
2021-01-11 01:50:30 +00:00
|
|
|
ProtobufWriter & writer);
|
|
|
|
};
|
|
|
|
|
2022-07-20 11:16:25 +00:00
|
|
|
NamesAndTypesList protobufSchemaToCHSchema(const google::protobuf::Descriptor * message_descriptor, bool skip_unsupported_fields);
|
2021-12-15 11:30:57 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
}
|
|
|
|
#endif
|