1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
#include "proto_table_reader.h"
#include "node_table_reader.h"
#include "proto_helpers.h"
#include <yt/yt_proto/yt/formats/extension.pb.h>
#include <util/string/escape.h>
#include <util/string/printf.h>
namespace NYT {
using ::google::protobuf::Descriptor;
using ::google::protobuf::FieldDescriptor;
using ::google::protobuf::EnumValueDescriptor;
const TString& GetFieldColumnName(const FieldDescriptor* fieldDesc) {
const auto& columnName = fieldDesc->options().GetExtension(column_name);
if (!columnName.empty()) {
return columnName;
}
const auto& keyColumnName = fieldDesc->options().GetExtension(key_column_name);
if (!keyColumnName.empty()) {
return keyColumnName;
}
return fieldDesc->name();
}
void ReadMessageFromNode(const TNode& node, Message* row)
{
auto* descriptor = row->GetDescriptor();
auto* reflection = row->GetReflection();
int count = descriptor->field_count();
for (int i = 0; i < count; ++i) {
auto* fieldDesc = descriptor->field(i);
const auto& columnName = GetFieldColumnName(fieldDesc);
const auto& nodeMap = node.AsMap();
auto it = nodeMap.find(columnName);
if (it == nodeMap.end()) {
continue; // no such column
}
auto actualType = it->second.GetType();
if (actualType == TNode::Null) {
continue; // null field
}
auto checkType = [&columnName] (TNode::EType expected, TNode::EType actual) {
if (expected != actual) {
ythrow TNode::TTypeError() << "expected node type " << expected
<< ", actual " << actual << " for node " << columnName.data();
}
};
switch (fieldDesc->type()) {
case FieldDescriptor::TYPE_STRING:
case FieldDescriptor::TYPE_BYTES:
checkType(TNode::String, actualType);
reflection->SetString(row, fieldDesc, it->second.AsString());
break;
case FieldDescriptor::TYPE_INT64:
case FieldDescriptor::TYPE_SINT64:
case FieldDescriptor::TYPE_SFIXED64:
checkType(TNode::Int64, actualType);
reflection->SetInt64(row, fieldDesc, it->second.AsInt64());
break;
case FieldDescriptor::TYPE_INT32:
case FieldDescriptor::TYPE_SINT32:
case FieldDescriptor::TYPE_SFIXED32:
checkType(TNode::Int64, actualType);
reflection->SetInt32(row, fieldDesc, it->second.AsInt64());
break;
case FieldDescriptor::TYPE_UINT64:
case FieldDescriptor::TYPE_FIXED64:
checkType(TNode::Uint64, actualType);
reflection->SetUInt64(row, fieldDesc, it->second.AsUint64());
break;
case FieldDescriptor::TYPE_UINT32:
case FieldDescriptor::TYPE_FIXED32:
checkType(TNode::Uint64, actualType);
reflection->SetUInt32(row, fieldDesc, it->second.AsUint64());
break;
case FieldDescriptor::TYPE_DOUBLE:
checkType(TNode::Double, actualType);
reflection->SetDouble(row, fieldDesc, it->second.AsDouble());
break;
case FieldDescriptor::TYPE_FLOAT:
checkType(TNode::Double, actualType);
reflection->SetFloat(row, fieldDesc, it->second.AsDouble());
break;
case FieldDescriptor::TYPE_BOOL:
checkType(TNode::Bool, actualType);
reflection->SetBool(row, fieldDesc, it->second.AsBool());
break;
case FieldDescriptor::TYPE_ENUM: {
TNode::EType columnType = TNode::String;
for (const auto& flag : fieldDesc->options().GetRepeatedExtension(flags)) {
if (flag == EWrapperFieldFlag::ENUM_INT) {
columnType = TNode::Int64;
break;
}
}
checkType(columnType, actualType);
const EnumValueDescriptor* valueDesc = nullptr;
TString stringValue;
if (columnType == TNode::String) {
const auto& value = it->second.AsString();
valueDesc = fieldDesc->enum_type()->FindValueByName(value);
stringValue = value;
} else if (columnType == TNode::Int64) {
const auto& value = it->second.AsInt64();
valueDesc = fieldDesc->enum_type()->FindValueByNumber(value);
stringValue = ToString(value);
} else {
Y_FAIL();
}
if (valueDesc == nullptr) {
ythrow yexception() << "Failed to parse value '" << EscapeC(stringValue) << "' as " << fieldDesc->enum_type()->full_name();
}
reflection->SetEnum(row, fieldDesc, valueDesc);
break;
}
case FieldDescriptor::TYPE_MESSAGE: {
checkType(TNode::String, actualType);
Message* message = reflection->MutableMessage(row, fieldDesc);
if (!message->ParseFromArray(it->second.AsString().data(), it->second.AsString().size())) {
ythrow yexception() << "Failed to parse protobuf message";
}
break;
}
default:
ythrow yexception() << "Incorrect protobuf type";
}
}
}
////////////////////////////////////////////////////////////////////////////////
TProtoTableReader::TProtoTableReader(
::TIntrusivePtr<TRawTableReader> input,
TVector<const Descriptor*>&& descriptors)
: NodeReader_(new TNodeTableReader(std::move(input)))
, Descriptors_(std::move(descriptors))
{ }
TProtoTableReader::~TProtoTableReader()
{ }
void TProtoTableReader::ReadRow(Message* row)
{
const auto& node = NodeReader_->GetRow();
ReadMessageFromNode(node, row);
}
bool TProtoTableReader::IsValid() const
{
return NodeReader_->IsValid();
}
void TProtoTableReader::Next()
{
NodeReader_->Next();
}
ui32 TProtoTableReader::GetTableIndex() const
{
return NodeReader_->GetTableIndex();
}
ui32 TProtoTableReader::GetRangeIndex() const
{
return NodeReader_->GetRangeIndex();
}
ui64 TProtoTableReader::GetRowIndex() const
{
return NodeReader_->GetRowIndex();
}
void TProtoTableReader::NextKey()
{
NodeReader_->NextKey();
}
TMaybe<size_t> TProtoTableReader::GetReadByteCount() const
{
return NodeReader_->GetReadByteCount();
}
bool TProtoTableReader::IsEndOfStream() const
{
return NodeReader_->IsEndOfStream();
}
bool TProtoTableReader::IsRawReaderExhausted() const
{
return NodeReader_->IsRawReaderExhausted();
}
////////////////////////////////////////////////////////////////////////////////
TLenvalProtoTableReader::TLenvalProtoTableReader(
::TIntrusivePtr<TRawTableReader> input,
TVector<const Descriptor*>&& descriptors)
: TLenvalTableReader(std::move(input))
, Descriptors_(std::move(descriptors))
{ }
TLenvalProtoTableReader::~TLenvalProtoTableReader()
{ }
void TLenvalProtoTableReader::ReadRow(Message* row)
{
ValidateProtoDescriptor(*row, GetTableIndex(), Descriptors_, true);
while (true) {
try {
ParseFromArcadiaStream(&Input_, *row, Length_);
RowTaken_ = true;
// We successfully parsed one more row from the stream,
// so reset retry count to their initial value.
Input_.ResetRetries();
break;
} catch (const std::exception& ) {
if (!TLenvalTableReader::Retry()) {
throw;
}
}
}
}
bool TLenvalProtoTableReader::IsValid() const
{
return TLenvalTableReader::IsValid();
}
void TLenvalProtoTableReader::Next()
{
TLenvalTableReader::Next();
}
ui32 TLenvalProtoTableReader::GetTableIndex() const
{
return TLenvalTableReader::GetTableIndex();
}
ui32 TLenvalProtoTableReader::GetRangeIndex() const
{
return TLenvalTableReader::GetRangeIndex();
}
ui64 TLenvalProtoTableReader::GetRowIndex() const
{
return TLenvalTableReader::GetRowIndex();
}
void TLenvalProtoTableReader::NextKey()
{
TLenvalTableReader::NextKey();
}
TMaybe<size_t> TLenvalProtoTableReader::GetReadByteCount() const
{
return TLenvalTableReader::GetReadByteCount();
}
bool TLenvalProtoTableReader::IsEndOfStream() const
{
return TLenvalTableReader::IsEndOfStream();
}
bool TLenvalProtoTableReader::IsRawReaderExhausted() const
{
return TLenvalTableReader::IsRawReaderExhausted();
}
void TLenvalProtoTableReader::SkipRow()
{
while (true) {
try {
size_t skipped = Input_.Skip(Length_);
if (skipped != Length_) {
ythrow yexception() << "Premature end of stream";
}
break;
} catch (const std::exception& ) {
if (!TLenvalTableReader::Retry()) {
throw;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|