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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
#include "structured_table_formats.h"
#include "format_hints.h"
#include "skiff.h"
#include <yt/cpp/mapreduce/common/retry_lib.h>
#include <yt/cpp/mapreduce/http/retry_request.h>
#include <yt/cpp/mapreduce/interface/common.h>
#include <yt/cpp/mapreduce/interface/raw_client.h>
#include <yt/cpp/mapreduce/io/yamr_table_reader.h>
#include <yt/cpp/mapreduce/library/table_schema/protobuf.h>
#include <yt/cpp/mapreduce/raw_client/raw_requests.h>
#include <library/cpp/type_info/type_info.h>
#include <library/cpp/yson/writer.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
TMaybe<TNode> GetCommonTableFormat(
const TVector<TMaybe<TNode>>& formats)
{
TMaybe<TNode> result;
bool start = true;
for (auto& format : formats) {
if (start) {
result = format;
start = false;
continue;
}
if (result.Defined() != format.Defined()) {
ythrow yexception() << "Different formats of input tables";
}
if (!result.Defined()) {
continue;
}
auto& resultAttrs = result.Get()->GetAttributes();
auto& formatAttrs = format.Get()->GetAttributes();
if (resultAttrs["key_column_names"] != formatAttrs["key_column_names"]) {
ythrow yexception() << "Different formats of input tables";
}
bool hasSubkeyColumns = resultAttrs.HasKey("subkey_column_names");
if (hasSubkeyColumns != formatAttrs.HasKey("subkey_column_names")) {
ythrow yexception() << "Different formats of input tables";
}
if (hasSubkeyColumns &&
resultAttrs["subkey_column_names"] != formatAttrs["subkey_column_names"])
{
ythrow yexception() << "Different formats of input tables";
}
}
return result;
}
TMaybe<TNode> GetTableFormat(
const IClientRetryPolicyPtr& retryPolicy,
const IRawClientPtr& rawClient,
const TTransactionId& transactionId,
const TRichYPath& path)
{
auto formatPath = path.Path_ + "/@_format";
auto exists = NDetail::RequestWithRetry<bool>(
retryPolicy->CreatePolicyForGenericRequest(),
[&rawClient, &transactionId, &formatPath] (TMutationId /*mutationId*/) {
return rawClient->Exists(transactionId, formatPath);
});
if (!exists) {
return TMaybe<TNode>();
}
auto format = NDetail::RequestWithRetry<TMaybe<TNode>>(
retryPolicy->CreatePolicyForGenericRequest(),
[&rawClient, &transactionId, &formatPath] (TMutationId /*mutationId*/) {
return rawClient->Get(transactionId, formatPath);
});
if (format.Get()->AsString() != "yamred_dsv") {
return TMaybe<TNode>();
}
auto& formatAttrs = format.Get()->Attributes();
if (!formatAttrs.HasKey("key_column_names")) {
ythrow yexception() <<
"Table '" << path.Path_ << "': attribute 'key_column_names' is missing";
}
formatAttrs["has_subkey"] = "true";
formatAttrs["lenval"] = "true";
return format;
}
TMaybe<TNode> GetTableFormats(
const IClientRetryPolicyPtr& clientRetryPolicy,
const IRawClientPtr& rawClient,
const TTransactionId& transactionId,
const TVector<TRichYPath>& inputs)
{
TVector<TMaybe<TNode>> formats;
for (auto& table : inputs) {
formats.push_back(GetTableFormat(clientRetryPolicy, rawClient, transactionId, table));
}
return GetCommonTableFormat(formats);
}
////////////////////////////////////////////////////////////////////////////////
namespace NDetail {
////////////////////////////////////////////////////////////////////////////////
NSkiff::TSkiffSchemaPtr TryCreateSkiffSchema(
const TClientContext& context,
const IClientRetryPolicyPtr& clientRetryPolicy,
const TTransactionId& transactionId,
const TVector<TRichYPath>& tables,
const TOperationOptions& options,
ENodeReaderFormat nodeReaderFormat)
{
bool hasInputQuery = options.Spec_.Defined() && options.Spec_->IsMap() && options.Spec_->HasKey("input_query");
if (hasInputQuery) {
Y_ENSURE_EX(nodeReaderFormat != ENodeReaderFormat::Skiff,
TApiUsageError() << "Cannot use Skiff format for operations with 'input_query' in spec");
return nullptr;
}
return CreateSkiffSchemaIfNecessary(
context,
clientRetryPolicy,
transactionId,
nodeReaderFormat,
tables,
TCreateSkiffSchemaOptions()
.HasKeySwitch(true)
.HasRangeIndex(true));
}
TString CreateSkiffConfig(const NSkiff::TSkiffSchemaPtr& schema)
{
TString result;
TStringOutput stream(result);
::NYson::TYsonWriter writer(&stream);
Serialize(schema, &writer);
return result;
}
TString CreateProtoConfig(const TVector<const ::google::protobuf::Descriptor*>& descriptorList)
{
TString result;
TStringOutput messageTypeList(result);
for (const auto& descriptor : descriptorList) {
messageTypeList << descriptor->full_name() << Endl;
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
struct TGetTableStructureDescriptionStringImpl {
template<typename T>
TString operator()(const T& description) {
if constexpr (std::is_same_v<T, TUnspecifiedTableStructure>) {
return "Unspecified";
} else if constexpr (std::is_same_v<T, TProtobufTableStructure>) {
TString res;
TStringStream out(res);
if (description.Descriptor) {
out << description.Descriptor->full_name();
} else {
out << "<unknown>";
}
out << " protobuf message";
return res;
} else {
static_assert(TDependentFalse<T>, "Unknown type");
}
}
};
TString GetTableStructureDescriptionString(const TTableStructure& tableStructure)
{
return std::visit(TGetTableStructureDescriptionStringImpl(), tableStructure);
}
////////////////////////////////////////////////////////////////////////////////
TString JobTablePathString(const TStructuredJobTable& jobTable)
{
if (jobTable.RichYPath) {
return jobTable.RichYPath->Path_;
} else {
return "<intermediate-table>";
}
}
TStructuredJobTableList ToStructuredJobTableList(const TVector<TStructuredTablePath>& tableList)
{
TStructuredJobTableList result;
for (const auto& table : tableList) {
result.push_back(TStructuredJobTable{table.Description, table.RichYPath});
}
return result;
}
TStructuredJobTableList CanonizeStructuredTableList(const TClientContext& context, const TVector<TStructuredTablePath>& tableList)
{
TVector<TRichYPath> toCanonize;
toCanonize.reserve(tableList.size());
for (const auto& table : tableList) {
toCanonize.emplace_back(table.RichYPath);
}
const auto canonized = NRawClient::CanonizeYPaths(/* retryPolicy */ nullptr, context, toCanonize);
Y_ABORT_UNLESS(canonized.size() == tableList.size());
TStructuredJobTableList result;
result.reserve(tableList.size());
for (size_t i = 0; i != canonized.size(); ++i) {
result.emplace_back(TStructuredJobTable{tableList[i].Description, canonized[i]});
}
return result;
}
TVector<TRichYPath> GetPathList(
const TStructuredJobTableList& tableList,
const TMaybe<TVector<TTableSchema>>& jobSchemaInferenceResult,
bool inferSchemaFromDescriptions)
{
Y_ABORT_UNLESS(!jobSchemaInferenceResult || tableList.size() == jobSchemaInferenceResult->size());
auto maybeInferSchema = [&] (const TStructuredJobTable& table, ui32 tableIndex) -> TMaybe<TTableSchema> {
if (jobSchemaInferenceResult && !jobSchemaInferenceResult->at(tableIndex).Empty()) {
return jobSchemaInferenceResult->at(tableIndex);
}
if (inferSchemaFromDescriptions) {
return GetTableSchema(table.Description);
}
return Nothing();
};
TVector<TRichYPath> result;
result.reserve(tableList.size());
for (size_t tableIndex = 0; tableIndex != tableList.size(); ++tableIndex) {
const auto& table = tableList[tableIndex];
Y_ABORT_UNLESS(table.RichYPath, "Cannot get path for intermediate table");
auto richYPath = *table.RichYPath;
if (!richYPath.Schema_) {
if (auto schema = maybeInferSchema(table, tableIndex)) {
richYPath.Schema(std::move(*schema));
}
}
result.emplace_back(std::move(richYPath));
}
return result;
}
TStructuredRowStreamDescription GetJobStreamDescription(
const IStructuredJob& job,
EIODirection direction)
{
switch (direction) {
case EIODirection::Input:
return job.GetInputRowStreamDescription();
case EIODirection::Output:
return job.GetOutputRowStreamDescription();
default:
Y_ABORT("unreachable");
}
}
TString GetSuffix(EIODirection direction)
{
switch (direction) {
case EIODirection::Input:
return "_input";
case EIODirection::Output:
return "_output";
}
Y_ABORT("unreachable");
}
TString GetAddIOMethodName(EIODirection direction)
{
switch (direction) {
case EIODirection::Input:
return "AddInput<>";
case EIODirection::Output:
return "AddOutput<>";
}
Y_ABORT("unreachable");
}
////////////////////////////////////////////////////////////////////////////////
struct TFormatBuilder::TFormatSwitcher
{
template <typename T>
auto operator() (const T& /*t*/) {
if constexpr (std::is_same_v<T, TTNodeStructuredRowStream>) {
return &TFormatBuilder::CreateNodeFormat;
} else if constexpr (std::is_same_v<T, TTYaMRRowStructuredRowStream>) {
return &TFormatBuilder::CreateYamrFormat;
} else if constexpr (std::is_same_v<T, TProtobufStructuredRowStream>) {
return &TFormatBuilder::CreateProtobufFormat;
} else if constexpr (std::is_same_v<T, TVoidStructuredRowStream>) {
return &TFormatBuilder::CreateVoidFormat;
} else {
static_assert(TDependentFalse<T>, "unknown stream description");
}
}
};
TFormatBuilder::TFormatBuilder(
IRawClientPtr rawClient,
IClientRetryPolicyPtr clientRetryPolicy,
TClientContext context,
TTransactionId transactionId,
TOperationOptions operationOptions)
: RawClient_(std::move(rawClient))
, ClientRetryPolicy_(std::move(clientRetryPolicy))
, Context_(std::move(context))
, TransactionId_(transactionId)
, OperationOptions_(std::move(operationOptions))
{ }
std::pair <TFormat, TMaybe<TSmallJobFile>> TFormatBuilder::CreateFormat(
const IStructuredJob& job,
const EIODirection& direction,
const TStructuredJobTableList& structuredTableList,
const TMaybe <TFormatHints>& formatHints,
ENodeReaderFormat nodeReaderFormat,
bool allowFormatFromTableAttribute)
{
auto jobStreamDescription = GetJobStreamDescription(job, direction);
auto method = std::visit(TFormatSwitcher(), jobStreamDescription);
return (this->*method)(
job,
direction,
structuredTableList,
formatHints,
nodeReaderFormat,
allowFormatFromTableAttribute);
}
std::pair<TFormat, TMaybe<TSmallJobFile>> TFormatBuilder::CreateVoidFormat(
const IStructuredJob& /*job*/,
const EIODirection& /*direction*/,
const TStructuredJobTableList& /*structuredTableList*/,
const TMaybe<TFormatHints>& /*formatHints*/,
ENodeReaderFormat /*nodeReaderFormat*/,
bool /*allowFormatFromTableAttribute*/)
{
return {
TFormat(),
Nothing()
};
}
std::pair<TFormat, TMaybe<TSmallJobFile>> TFormatBuilder::CreateYamrFormat(
const IStructuredJob& job,
const EIODirection& direction,
const TStructuredJobTableList& structuredTableList,
const TMaybe<TFormatHints>& /*formatHints*/,
ENodeReaderFormat /*nodeReaderFormat*/,
bool allowFormatFromTableAttribute)
{
for (const auto& table: structuredTableList) {
if (!std::holds_alternative<TUnspecifiedTableStructure>(table.Description)) {
ythrow TApiUsageError()
<< "cannot use " << direction << " table '" << JobTablePathString(table)
<< "' with job " << TJobFactory::Get()->GetJobName(&job) << "; "
<< "table has unsupported structure description; check " << GetAddIOMethodName(direction) << " for this table";
}
}
TMaybe<TNode> formatFromTableAttributes;
if (allowFormatFromTableAttribute && OperationOptions_.UseTableFormats_) {
TVector<TRichYPath> tableList;
for (const auto& table: structuredTableList) {
Y_ABORT_UNLESS(table.RichYPath, "Cannot use format from table for intermediate table");
tableList.push_back(*table.RichYPath);
}
formatFromTableAttributes = GetTableFormats(ClientRetryPolicy_, RawClient_, TransactionId_, tableList);
}
if (formatFromTableAttributes) {
return {
TFormat(*formatFromTableAttributes),
Nothing()
};
} else {
auto formatNode = TNode("yamr");
formatNode.Attributes() = TNode()
("lenval", true)
("has_subkey", true)
("enable_table_index", true);
return {
TFormat(formatNode),
Nothing()
};
}
}
std::pair<TFormat, TMaybe<TSmallJobFile>> TFormatBuilder::CreateNodeFormat(
const IStructuredJob& job,
const EIODirection& direction,
const TStructuredJobTableList& structuredTableList,
const TMaybe<TFormatHints>& formatHints,
ENodeReaderFormat nodeReaderFormat,
bool /*allowFormatFromTableAttribute*/)
{
for (const auto& table: structuredTableList) {
if (!std::holds_alternative<TUnspecifiedTableStructure>(table.Description)) {
ythrow TApiUsageError()
<< "cannot use " << direction << " table '" << JobTablePathString(table)
<< "' with job " << TJobFactory::Get()->GetJobName(&job) << "; "
<< "table has unsupported structure description; check AddInput<> / AddOutput<> for this table";
}
}
NSkiff::TSkiffSchemaPtr skiffSchema = nullptr;
if (nodeReaderFormat != ENodeReaderFormat::Yson) {
TVector<TRichYPath> tableList;
for (const auto& table: structuredTableList) {
Y_ABORT_UNLESS(table.RichYPath, "Cannot use skiff with temporary tables");
tableList.emplace_back(*table.RichYPath);
}
skiffSchema = TryCreateSkiffSchema(
Context_,
ClientRetryPolicy_,
TransactionId_,
tableList,
OperationOptions_,
nodeReaderFormat);
}
if (skiffSchema) {
auto format = CreateSkiffFormat(skiffSchema);
NYT::NDetail::ApplyFormatHints<TNode>(&format, formatHints);
return {
format,
TSmallJobFile{
TString("skiff") + GetSuffix(direction),
CreateSkiffConfig(skiffSchema)
}
};
} else {
auto format = TFormat::YsonBinary();
NYT::NDetail::ApplyFormatHints<TNode>(&format, formatHints);
return {
format,
Nothing()
};
}
}
[[noreturn]] static void ThrowUnsupportedStructureDescription(
const EIODirection& direction,
const TStructuredJobTable& table,
const IStructuredJob& job)
{
ythrow TApiUsageError()
<< "cannot use " << direction << " table '" << JobTablePathString(table)
<< "' with job " << TJobFactory::Get()->GetJobName(&job) << "; "
<< "table has unsupported structure description; check " << GetAddIOMethodName(direction) << " for this table";
}
[[noreturn]] static void ThrowTypeDeriveFail(
const EIODirection& direction,
const IStructuredJob& job,
const TString& type)
{
ythrow TApiUsageError()
<< "Cannot derive exact " << type << " type for intermediate " << direction << " table for job "
<< TJobFactory::Get()->GetJobName(&job)
<< "; use one of TMapReduceOperationSpec::Hint* methods to specify intermediate table structure";
}
[[noreturn]] static void ThrowUnexpectedDifferentDescriptors(
const EIODirection& direction,
const TStructuredJobTable& table,
const IStructuredJob& job,
const TMaybe<TStringBuf> jobDescriptorName,
const TMaybe<TStringBuf> descriptorName)
{
ythrow TApiUsageError()
<< "Job " << TJobFactory::Get()->GetJobName(&job) << " expects "
<< jobDescriptorName << " as " << direction << ", but table " << JobTablePathString(table)
<< " is tagged with " << descriptorName;
}
std::pair<TFormat, TMaybe<TSmallJobFile>> TFormatBuilder::CreateProtobufFormat(
const IStructuredJob& job,
const EIODirection& direction,
const TStructuredJobTableList& structuredTableList,
const TMaybe<TFormatHints>& /*formatHints*/,
ENodeReaderFormat /*nodeReaderFormat*/,
bool /*allowFormatFromTableAttribute*/)
{
if (Context_.Config->UseClientProtobuf) {
return {
TFormat::YsonBinary(),
TSmallJobFile{
TString("proto") + GetSuffix(direction),
CreateProtoConfig({}),
},
};
}
const ::google::protobuf::Descriptor* const jobDescriptor =
std::get<TProtobufStructuredRowStream>(GetJobStreamDescription(job, direction)).Descriptor;
Y_ENSURE(!structuredTableList.empty(),
"empty " << direction << " tables for job " << TJobFactory::Get()->GetJobName(&job));
TVector<const ::google::protobuf::Descriptor*> descriptorList;
for (const auto& table : structuredTableList) {
const ::google::protobuf::Descriptor* descriptor = nullptr;
if (std::holds_alternative<TProtobufTableStructure>(table.Description)) {
descriptor = std::get<TProtobufTableStructure>(table.Description).Descriptor;
} else if (table.RichYPath) {
ThrowUnsupportedStructureDescription(direction, table, job);
}
if (!descriptor) {
// It must be intermediate table, because there is no proper way to add such table to spec
// (AddInput requires to specify proper message).
Y_ABORT_UNLESS(!table.RichYPath, "Descriptors for all tables except intermediate must be known");
if (jobDescriptor) {
descriptor = jobDescriptor;
} else {
ThrowTypeDeriveFail(direction, job, "protobuf");
}
}
if (jobDescriptor && descriptor != jobDescriptor) {
ThrowUnexpectedDifferentDescriptors(
direction,
table,
job,
jobDescriptor->full_name(),
descriptor->full_name());
}
descriptorList.push_back(descriptor);
}
Y_ABORT_UNLESS(!descriptorList.empty(), "Messages for proto format are unknown (empty ProtoDescriptors)");
return {
TFormat::Protobuf(descriptorList, Context_.Config->ProtobufFormatWithDescriptors),
TSmallJobFile{
TString("proto") + GetSuffix(direction),
CreateProtoConfig(descriptorList)
},
};
}
////////////////////////////////////////////////////////////////////////////////
struct TGetTableSchemaImpl
{
template <typename T>
TMaybe<TTableSchema> operator() (const T& description) {
if constexpr (std::is_same_v<T, TUnspecifiedTableStructure>) {
return Nothing();
} else if constexpr (std::is_same_v<T, TProtobufTableStructure>) {
if (!description.Descriptor) {
return Nothing();
}
return CreateTableSchema(*description.Descriptor);
} else {
static_assert(TDependentFalse<T>, "unknown type");
}
}
};
TMaybe<TTableSchema> GetTableSchema(const TTableStructure& tableStructure)
{
return std::visit(TGetTableSchemaImpl(), tableStructure);
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NDetail
} // namespace NYT
|