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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
#pragma once
#ifndef OPERATION_INL_H_
#error "Direct inclusion of this file is not allowed, use operation.h"
#include "operation.h"
#endif
#undef OPERATION_INL_H_
#include "errors.h"
#include <util/generic/bt_exception.h>
#include <util/generic/singleton.h>
#include <util/system/type_name.h>
#include <util/stream/file.h>
#include <util/stream/buffer.h>
#include <util/string/subst.h>
#include <typeindex>
namespace NYT {
namespace NDetail {
////////////////////////////////////////////////////////////////////////////////
template<class T>
void Assign(TVector<T>& array, size_t idx, const T& value) {
array.resize(std::max(array.size(), idx + 1));
array[idx] = value;
}
////////////////////////////////////////////////////////////////////////////////
template <typename TRow>
TStructuredRowStreamDescription GetStructuredRowStreamDescription()
{
if constexpr (std::is_same_v<TRow, NYT::TNode>) {
return TTNodeStructuredRowStream{};
} else if constexpr (std::is_same_v<TRow, NYT::TYaMRRow>) {
return TTYaMRRowStructuredRowStream{};
} else if constexpr (std::is_same_v<::google::protobuf::Message, TRow>) {
return TProtobufStructuredRowStream{nullptr};
} else if constexpr (TIsBaseOf<::google::protobuf::Message, TRow>::Value) {
return TProtobufStructuredRowStream{TRow::descriptor()};
} else if constexpr (TIsProtoOneOf<TRow>::value) {
return TProtobufStructuredRowStream{nullptr};
} else {
static_assert(TDependentFalse<TRow>, "Unknown row type");
}
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NDetail
////////////////////////////////////////////////////////////////////////////////
template <typename TRow>
TStructuredTablePath Structured(TRichYPath richYPath)
{
return TStructuredTablePath(std::move(richYPath), StructuredTableDescription<TRow>());
}
template <typename TRow>
TTableStructure StructuredTableDescription()
{
if constexpr (std::is_same_v<TRow, NYT::TNode>) {
return TUnspecifiedTableStructure{};
} else if constexpr (std::is_same_v<TRow, NYT::TYaMRRow>) {
return TUnspecifiedTableStructure{};
} else if constexpr (std::is_base_of_v<::google::protobuf::Message, TRow>) {
if constexpr (std::is_same_v<::google::protobuf::Message, TRow>) {
static_assert(TDependentFalse<TRow>, "Cannot use ::google::protobuf::Message as table descriptor");
} else {
return TProtobufTableStructure{TRow::descriptor()};
}
} else {
static_assert(TDependentFalse<TRow>, "Unknown row type");
}
}
////////////////////////////////////////////////////////////////////////////////
template <typename TDerived>
TDerived& TRawOperationIoTableSpec<TDerived>::AddInput(const TRichYPath& path)
{
Inputs_.push_back(path);
return static_cast<TDerived&>(*this);
}
template <typename TDerived>
TDerived& TRawOperationIoTableSpec<TDerived>::SetInput(size_t tableIndex, const TRichYPath& path)
{
NDetail::Assign(Inputs_, tableIndex, path);
}
template <typename TDerived>
TDerived& TRawOperationIoTableSpec<TDerived>::AddOutput(const TRichYPath& path)
{
Outputs_.push_back(path);
return static_cast<TDerived&>(*this);
}
template <typename TDerived>
TDerived& TRawOperationIoTableSpec<TDerived>::SetOutput(size_t tableIndex, const TRichYPath& path)
{
NDetail::Assign(Outputs_, tableIndex, path);
}
template <typename TDerived>
const TVector<TRichYPath>& TRawOperationIoTableSpec<TDerived>::GetInputs() const
{
return Inputs_;
}
template <typename TDerived>
const TVector<TRichYPath>& TRawOperationIoTableSpec<TDerived>::GetOutputs() const
{
return Outputs_;
}
////////////////////////////////////////////////////////////////////////////////
template <typename TDerived>
TDerived& TRawMapReduceOperationIoSpec<TDerived>::AddMapOutput(const TRichYPath& path)
{
MapOutputs_.push_back(path);
return static_cast<TDerived&>(*this);
}
template <typename TDerived>
TDerived& TRawMapReduceOperationIoSpec<TDerived>::SetMapOutput(size_t tableIndex, const TRichYPath& path)
{
NDetail::Assign(MapOutputs_, tableIndex, path);
}
template <typename TDerived>
const TVector<TRichYPath>& TRawMapReduceOperationIoSpec<TDerived>::GetMapOutputs() const
{
return MapOutputs_;
}
////////////////////////////////////////////////////////////////////////////////
::TIntrusivePtr<INodeReaderImpl> CreateJobNodeReader(TRawTableReaderPtr rawTableReader);
::TIntrusivePtr<IYaMRReaderImpl> CreateJobYaMRReader(TRawTableReaderPtr rawTableReader);
::TIntrusivePtr<IProtoReaderImpl> CreateJobProtoReader(TRawTableReaderPtr rawTableReader);
::TIntrusivePtr<INodeWriterImpl> CreateJobNodeWriter(THolder<IProxyOutput> rawTableWriter);
::TIntrusivePtr<IYaMRWriterImpl> CreateJobYaMRWriter(THolder<IProxyOutput> rawTableWriter);
::TIntrusivePtr<IProtoWriterImpl> CreateJobProtoWriter(THolder<IProxyOutput> rawTableWriter);
////////////////////////////////////////////////////////////////////////////////
template <class T>
inline ::TIntrusivePtr<typename TRowTraits<T>::IReaderImpl> CreateJobReaderImpl(TRawTableReaderPtr rawTableReader);
template <>
inline ::TIntrusivePtr<INodeReaderImpl> CreateJobReaderImpl<TNode>(TRawTableReaderPtr rawTableReader)
{
return CreateJobNodeReader(rawTableReader);
}
template <>
inline ::TIntrusivePtr<IYaMRReaderImpl> CreateJobReaderImpl<TYaMRRow>(TRawTableReaderPtr rawTableReader)
{
return CreateJobYaMRReader(rawTableReader);
}
template <>
inline ::TIntrusivePtr<IProtoReaderImpl> CreateJobReaderImpl<Message>(TRawTableReaderPtr rawTableReader)
{
return CreateJobProtoReader(rawTableReader);
}
template <class T>
inline ::TIntrusivePtr<typename TRowTraits<T>::IReaderImpl> CreateJobReaderImpl(TRawTableReaderPtr rawTableReader)
{
if constexpr (TIsBaseOf<Message, T>::Value || NDetail::TIsProtoOneOf<T>::value) {
return CreateJobProtoReader(rawTableReader);
} else {
static_assert(TDependentFalse<T>, "Unknown row type");
}
}
template <class T>
inline TTableReaderPtr<T> CreateJobReader(TRawTableReaderPtr rawTableReader)
{
return new TTableReader<T>(CreateJobReaderImpl<T>(rawTableReader));
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
TTableWriterPtr<T> CreateJobWriter(THolder<IProxyOutput> rawJobWriter);
template <>
inline TTableWriterPtr<TNode> CreateJobWriter<TNode>(THolder<IProxyOutput> rawJobWriter)
{
return new TTableWriter<TNode>(CreateJobNodeWriter(std::move(rawJobWriter)));
}
template <>
inline TTableWriterPtr<TYaMRRow> CreateJobWriter<TYaMRRow>(THolder<IProxyOutput> rawJobWriter)
{
return new TTableWriter<TYaMRRow>(CreateJobYaMRWriter(std::move(rawJobWriter)));
}
template <>
inline TTableWriterPtr<Message> CreateJobWriter<Message>(THolder<IProxyOutput> rawJobWriter)
{
return new TTableWriter<Message>(CreateJobProtoWriter(std::move(rawJobWriter)));
}
template <class T, class = void>
struct TProtoWriterCreator;
template <class T>
struct TProtoWriterCreator<T, std::enable_if_t<TIsBaseOf<Message, T>::Value>>
{
static TTableWriterPtr<T> Create(::TIntrusivePtr<IProtoWriterImpl> writer)
{
return new TTableWriter<T>(writer);
}
};
template <class T>
inline TTableWriterPtr<T> CreateJobWriter(THolder<IProxyOutput> rawJobWriter)
{
if constexpr (TIsBaseOf<Message, T>::Value) {
return TProtoWriterCreator<T>::Create(CreateJobProtoWriter(std::move(rawJobWriter)));
} else {
static_assert(TDependentFalse<T>, "Unknown row type");
}
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
void TOperationInputSpecBase::AddInput(const TRichYPath& path)
{
Inputs_.push_back(path);
StructuredInputs_.emplace_back(Structured<T>(path));
}
template <class T>
void TOperationInputSpecBase::SetInput(size_t tableIndex, const TRichYPath& path)
{
NDetail::Assign(Inputs_, tableIndex, path);
NDetail::Assign(StructuredInputs_, tableIndex, Structured<T>(path));
}
template <class T>
void TOperationOutputSpecBase::AddOutput(const TRichYPath& path)
{
Outputs_.push_back(path);
StructuredOutputs_.emplace_back(Structured<T>(path));
}
template <class T>
void TOperationOutputSpecBase::SetOutput(size_t tableIndex, const TRichYPath& path)
{
NDetail::Assign(Outputs_, tableIndex, path);
NDetail::Assign(StructuredOutputs_, tableIndex, Structured<T>(path));
}
template <class TDerived>
template <class T>
TDerived& TOperationIOSpec<TDerived>::AddInput(const TRichYPath& path)
{
static_assert(!std::is_same<T, Message>::value, "input type can't be Message, it can only be its strict subtype (see st.yandex-team.ru/YT-7609)");
TOperationInputSpecBase::AddInput<T>(path);
return *static_cast<TDerived*>(this);
}
template <class TDerived>
template <class T>
TDerived& TOperationIOSpec<TDerived>::SetInput(size_t tableIndex, const TRichYPath& path)
{
static_assert(!std::is_same<T, Message>::value, "input type can't be Message, it can only be its strict subtype (see st.yandex-team.ru/YT-7609)");
TOperationInputSpecBase::SetInput<T>(tableIndex, path);
return *static_cast<TDerived*>(this);
}
template <class TDerived>
template <class T>
TDerived& TOperationIOSpec<TDerived>::AddOutput(const TRichYPath& path)
{
static_assert(!std::is_same<T, Message>::value, "output type can't be Message, it can only be its strict subtype (see st.yandex-team.ru/YT-7609)");
TOperationOutputSpecBase::AddOutput<T>(path);
return *static_cast<TDerived*>(this);
}
template <class TDerived>
template <class T>
TDerived& TOperationIOSpec<TDerived>::SetOutput(size_t tableIndex, const TRichYPath& path)
{
static_assert(!std::is_same<T, Message>::value, "output type can't be Message, it can only be its strict subtype (see st.yandex-team.ru/YT-7609)");
TOperationOutputSpecBase::SetOutput<T>(tableIndex, path);
return *static_cast<TDerived*>(this);
}
template <class TDerived>
TDerived& TOperationIOSpec<TDerived>::AddStructuredInput(TStructuredTablePath path)
{
TOperationInputSpecBase::AddStructuredInput(std::move(path));
return *static_cast<TDerived*>(this);
}
template <class TDerived>
TDerived& TOperationIOSpec<TDerived>::AddStructuredOutput(TStructuredTablePath path)
{
TOperationOutputSpecBase::AddStructuredOutput(std::move(path));
return *static_cast<TDerived*>(this);
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
TVanillaTask& TVanillaTask::AddOutput(const TRichYPath& path)
{
static_assert(!std::is_same<T, Message>::value, "output type can't be Message, it can only be its strict subtype (see st.yandex-team.ru/YT-7609)");
TOperationOutputSpecBase::AddOutput<T>(path);
return *this;
}
template <class T>
TVanillaTask& TVanillaTask::SetOutput(size_t tableIndex, const TRichYPath& path)
{
static_assert(!std::is_same<T, Message>::value, "output type can't be Message, it can only be its strict subtype (see st.yandex-team.ru/YT-7609)");
TOperationOutputSpecBase::SetOutput<T>(tableIndex, path);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
namespace NDetail {
void ResetUseClientProtobuf(const char* methodName);
} // namespace NDetail
template <class TDerived>
TDerived& TOperationIOSpec<TDerived>::AddProtobufInput_VerySlow_Deprecated(const TRichYPath& path)
{
NDetail::ResetUseClientProtobuf("AddProtobufInput_VerySlow_Deprecated");
Inputs_.push_back(path);
StructuredInputs_.emplace_back(TStructuredTablePath(path, TProtobufTableStructure{nullptr}));
return *static_cast<TDerived*>(this);
}
template <class TDerived>
TDerived& TOperationIOSpec<TDerived>::AddProtobufOutput_VerySlow_Deprecated(const TRichYPath& path)
{
NDetail::ResetUseClientProtobuf("AddProtobufOutput_VerySlow_Deprecated");
Outputs_.push_back(path);
StructuredOutputs_.emplace_back(TStructuredTablePath(path, TProtobufTableStructure{nullptr}));
return *static_cast<TDerived*>(this);
}
////////////////////////////////////////////////////////////////////////////////
template <typename TRow>
TJobOperationPreparer::TInputGroup& TJobOperationPreparer::TInputGroup::Description()
{
for (auto i : Indices_) {
Preparer_.InputDescription<TRow>(i);
}
return *this;
}
template <typename TRow>
TJobOperationPreparer::TOutputGroup& TJobOperationPreparer::TOutputGroup::Description(bool inferSchema)
{
for (auto i : Indices_) {
Preparer_.OutputDescription<TRow>(i, inferSchema);
}
return *this;
}
////////////////////////////////////////////////////////////////////////////////
template <typename TCont>
TJobOperationPreparer::TInputGroup TJobOperationPreparer::BeginInputGroup(const TCont& indices)
{
for (auto i : indices) {
ValidateInputTableIndex(i, TStringBuf("BeginInputGroup()"));
}
return TInputGroup(*this, TVector<int>(std::begin(indices), std::end(indices)));
}
template <typename TCont>
TJobOperationPreparer::TOutputGroup TJobOperationPreparer::BeginOutputGroup(const TCont& indices)
{
for (auto i : indices) {
ValidateOutputTableIndex(i, TStringBuf("BeginOutputGroup()"));
}
return TOutputGroup(*this, indices);
}
template <typename TRow>
TJobOperationPreparer& TJobOperationPreparer::InputDescription(int tableIndex)
{
ValidateMissingInputDescription(tableIndex);
InputTableDescriptions_[tableIndex] = StructuredTableDescription<TRow>();
return *this;
}
template <typename TRow>
TJobOperationPreparer& TJobOperationPreparer::OutputDescription(int tableIndex, bool inferSchema)
{
ValidateMissingOutputDescription(tableIndex);
OutputTableDescriptions_[tableIndex] = StructuredTableDescription<TRow>();
if (inferSchema && !OutputSchemas_[tableIndex]) {
OutputSchemas_[tableIndex] = CreateTableSchema<TRow>();
}
return *this;
}
////////////////////////////////////////////////////////////////////////////////
template <class TDerived>
template <class TRow>
TDerived& TIntermediateTablesHintSpec<TDerived>::HintMapOutput()
{
IntermediateMapOutputDescription_ = StructuredTableDescription<TRow>();
return *static_cast<TDerived*>(this);
}
template <class TDerived>
template <class TRow>
TDerived& TIntermediateTablesHintSpec<TDerived>::AddMapOutput(const TRichYPath& path)
{
MapOutputs_.push_back(path);
StructuredMapOutputs_.emplace_back(Structured<TRow>(path));
return *static_cast<TDerived*>(this);
}
template <class TDerived>
template <class TRow>
TDerived& TIntermediateTablesHintSpec<TDerived>::HintReduceCombinerInput()
{
IntermediateReduceCombinerInputDescription_ = StructuredTableDescription<TRow>();
return *static_cast<TDerived*>(this);
}
template <class TDerived>
template <class TRow>
TDerived& TIntermediateTablesHintSpec<TDerived>::HintReduceCombinerOutput()
{
IntermediateReduceCombinerOutputDescription_ = StructuredTableDescription<TRow>();
return *static_cast<TDerived*>(this);
}
template <class TDerived>
template <class TRow>
TDerived& TIntermediateTablesHintSpec<TDerived>::HintReduceInput()
{
IntermediateReducerInputDescription_ = StructuredTableDescription<TRow>();
return *static_cast<TDerived*>(this);
}
template <class TDerived>
const TVector<TStructuredTablePath>& TIntermediateTablesHintSpec<TDerived>::GetStructuredMapOutputs() const
{
return StructuredMapOutputs_;
}
template <class TDerived>
const TMaybe<TTableStructure>& TIntermediateTablesHintSpec<TDerived>::GetIntermediateMapOutputDescription() const
{
return IntermediateMapOutputDescription_;
}
template <class TDerived>
const TMaybe<TTableStructure>& TIntermediateTablesHintSpec<TDerived>::GetIntermediateReduceCombinerInputDescription() const
{
return IntermediateReduceCombinerInputDescription_;
}
template <class TDerived>
const TMaybe<TTableStructure>& TIntermediateTablesHintSpec<TDerived>::GetIntermediateReduceCombinerOutputDescription() const
{
return IntermediateReduceCombinerOutputDescription_;
}
template <class TDerived>
const TMaybe<TTableStructure>& TIntermediateTablesHintSpec<TDerived>::GetIntermediateReducerInputDescription() const
{
return IntermediateReducerInputDescription_;
}
////////////////////////////////////////////////////////////////////////////////
struct TReducerContext
{
bool Break = false;
static TReducerContext* Get() { return Singleton<TReducerContext>(); }
};
template <class TR, class TW>
inline void IReducer<TR, TW>::Break()
{
TReducerContext::Get()->Break = true;
}
template <typename TReader, typename TWriter>
void FeedJobInput(
IMapper<TReader, TWriter>* mapper,
typename TRowTraits<typename TReader::TRowType>::IReaderImpl* readerImpl,
TWriter* writer)
{
using TInputRow = typename TReader::TRowType;
auto reader = MakeIntrusive<TTableReader<TInputRow>>(readerImpl);
mapper->Do(reader.Get(), writer);
}
template <typename TReader, typename TWriter>
void FeedJobInput(
IReducer<TReader, TWriter>* reducer,
typename TRowTraits<typename TReader::TRowType>::IReaderImpl* readerImpl,
TWriter* writer)
{
using TInputRow = typename TReader::TRowType;
auto rangesReader = MakeIntrusive<TTableRangesReader<TInputRow>>(readerImpl);
for (; rangesReader->IsValid(); rangesReader->Next()) {
reducer->Do(&rangesReader->GetRange(), writer);
if (TReducerContext::Get()->Break) {
break;
}
}
}
template <typename TReader, typename TWriter>
void FeedJobInput(
IAggregatorReducer<TReader, TWriter>* reducer,
typename TRowTraits<typename TReader::TRowType>::IReaderImpl* readerImpl,
TWriter* writer)
{
using TInputRow = typename TReader::TRowType;
auto rangesReader = MakeIntrusive<TTableRangesReader<TInputRow>>(readerImpl);
reducer->Do(rangesReader.Get(), writer);
}
template <class TRawJob>
int RunRawJob(size_t outputTableCount, IInputStream& jobStateStream)
{
TRawJobContext context(outputTableCount);
TRawJob job;
job.Load(jobStateStream);
job.Do(context);
return 0;
}
template <>
inline int RunRawJob<TCommandRawJob>(size_t /* outputTableCount */, IInputStream& /* jobStateStream */)
{
Y_ABORT();
}
template <class TVanillaJob>
int RunVanillaJob(size_t outputTableCount, IInputStream& jobStateStream)
{
TVanillaJob job;
job.Load(jobStateStream);
if constexpr (std::is_base_of<IVanillaJob<>, TVanillaJob>::value) {
Y_ABORT_UNLESS(outputTableCount == 0, "Void vanilla job expects zero 'outputTableCount'");
job.Do();
} else {
Y_ABORT_UNLESS(outputTableCount, "Vanilla job with table writer expects nonzero 'outputTableCount'");
using TOutputRow = typename TVanillaJob::TWriter::TRowType;
THolder<IProxyOutput> rawJobWriter;
if (auto customWriter = job.CreateCustomRawJobWriter(outputTableCount)) {
rawJobWriter = std::move(customWriter);
} else {
rawJobWriter = CreateRawJobWriter(outputTableCount);
}
auto writer = CreateJobWriter<TOutputRow>(std::move(rawJobWriter));
job.Start(writer.Get());
job.Do(writer.Get());
job.Finish(writer.Get());
writer->Finish();
}
return 0;
}
template <>
inline int RunVanillaJob<TCommandVanillaJob>(size_t /* outputTableCount */, IInputStream& /* jobStateStream */)
{
Y_ABORT();
}
template <class TJob>
requires TIsBaseOf<IStructuredJob, TJob>::Value
int RunJob(size_t outputTableCount, IInputStream& jobStateStream)
{
using TInputRow = typename TJob::TReader::TRowType;
using TOutputRow = typename TJob::TWriter::TRowType;
auto job = MakeIntrusive<TJob>();
job->Load(jobStateStream);
TRawTableReaderPtr rawJobReader;
if (auto customReader = job->CreateCustomRawJobReader(/*fd*/ 0)) {
rawJobReader = customReader;
} else {
rawJobReader = CreateRawJobReader(/*fd*/ 0);
}
auto readerImpl = CreateJobReaderImpl<TInputRow>(rawJobReader);
// Many users don't expect to have jobs with empty input so we skip such jobs.
if (!readerImpl->IsValid()) {
return 0;
}
THolder<IProxyOutput> rawJobWriter;
if (auto customWriter = job->CreateCustomRawJobWriter(outputTableCount)) {
rawJobWriter = std::move(customWriter);
} else {
rawJobWriter = CreateRawJobWriter(outputTableCount);
}
auto writer = CreateJobWriter<TOutputRow>(std::move(rawJobWriter));
job->Start(writer.Get());
FeedJobInput(job.Get(), readerImpl.Get(), writer.Get());
job->Finish(writer.Get());
writer->Finish();
return 0;
}
//
// We leave RunMapJob/RunReduceJob/RunAggregatorReducer for backward compatibility,
// some user use them already. :(
template <class TMapper>
int RunMapJob(size_t outputTableCount, IInputStream& jobStateStream)
{
return RunJob<TMapper>(outputTableCount, jobStateStream);
}
template <class TReducer>
int RunReduceJob(size_t outputTableCount, IInputStream& jobStateStream)
{
return RunJob<TReducer>(outputTableCount, jobStateStream);
}
template <class TReducer>
int RunAggregatorReducer(size_t outputTableCount, IInputStream& jobStateStream)
{
return RunJob<TReducer>(outputTableCount, jobStateStream);
}
////////////////////////////////////////////////////////////////////////////////
template <typename T, typename = void>
struct TIsConstructibleFromNode
: std::false_type
{ };
template <typename T>
struct TIsConstructibleFromNode<T, std::void_t<decltype(T::FromNode(std::declval<TNode&>()))>>
: std::true_type
{ };
template <class TJob>
::TIntrusivePtr<NYT::IStructuredJob> ConstructJobFromNode(const TNode& node)
{
if constexpr (TIsConstructibleFromNode<TJob>::value) {
Y_ENSURE(node.GetType() != TNode::Undefined,
"job has FromNode method but constructor arguments were not provided");
return TJob::FromNode(node);
} else {
Y_ENSURE(node.GetType() == TNode::Undefined,
"constructor arguments provided but job does not contain FromNode method");
return MakeIntrusive<TJob>();
}
}
////////////////////////////////////////////////////////////////////////////////
using TJobFunction = int (*)(size_t, IInputStream&);
using TConstructJobFunction = ::TIntrusivePtr<NYT::IStructuredJob> (*)(const TNode&);
class TJobFactory
{
public:
static TJobFactory* Get()
{
return Singleton<TJobFactory>();
}
template <class TJob>
void RegisterJob(const char* name)
{
RegisterJobImpl<TJob>(name, RunJob<TJob>);
JobConstructors[name] = ConstructJobFromNode<TJob>;
}
template <class TRawJob>
void RegisterRawJob(const char* name)
{
RegisterJobImpl<TRawJob>(name, RunRawJob<TRawJob>);
}
template <class TVanillaJob>
void RegisterVanillaJob(const char* name)
{
RegisterJobImpl<TVanillaJob>(name, RunVanillaJob<TVanillaJob>);
}
TString GetJobName(const IJob* job)
{
const auto typeIndex = std::type_index(typeid(*job));
CheckJobRegistered(typeIndex);
return JobNames[typeIndex];
}
TJobFunction GetJobFunction(const char* name)
{
CheckNameRegistered(name);
return JobFunctions[name];
}
TConstructJobFunction GetConstructingFunction(const char* name)
{
CheckNameRegistered(name);
return JobConstructors[name];
}
private:
TMap<std::type_index, TString> JobNames;
THashMap<TString, TJobFunction> JobFunctions;
THashMap<TString, TConstructJobFunction> JobConstructors;
template <typename TJob, typename TRunner>
void RegisterJobImpl(const char* name, TRunner runner) {
const auto typeIndex = std::type_index(typeid(TJob));
CheckNotRegistered(typeIndex, name);
JobNames[typeIndex] = name;
JobFunctions[name] = runner;
}
void CheckNotRegistered(const std::type_index& typeIndex, const char* name)
{
Y_ENSURE(!JobNames.contains(typeIndex),
"type_info '" << typeIndex.name() << "'"
"is already registered under name '" << JobNames[typeIndex] << "'");
Y_ENSURE(!JobFunctions.contains(name),
"job with name '" << name << "' is already registered");
}
void CheckJobRegistered(const std::type_index& typeIndex)
{
Y_ENSURE(JobNames.contains(typeIndex),
"type_info '" << typeIndex.name() << "' is not registered, use REGISTER_* macros");
}
void CheckNameRegistered(const char* name)
{
Y_ENSURE(JobFunctions.contains(name),
"job with name '" << name << "' is not registered, use REGISTER_* macros");
}
};
////////////////////////////////////////////////////////////////////////////////
template <class TMapper>
struct TMapperRegistrator
{
TMapperRegistrator(const char* name)
{
static_assert(TMapper::JobType == IJob::EType::Mapper,
"REGISTER_MAPPER is not compatible with this job class");
NYT::TJobFactory::Get()->RegisterJob<TMapper>(name);
}
};
template <class TReducer>
struct TReducerRegistrator
{
TReducerRegistrator(const char* name)
{
static_assert(TReducer::JobType == IJob::EType::Reducer ||
TReducer::JobType == IJob::EType::ReducerAggregator,
"REGISTER_REDUCER is not compatible with this job class");
NYT::TJobFactory::Get()->RegisterJob<TReducer>(name);
}
};
template <class TRawJob>
struct TRawJobRegistrator
{
TRawJobRegistrator(const char* name)
{
static_assert(TRawJob::JobType == IJob::EType::RawJob,
"REGISTER_RAW_JOB is not compatible with this job class");
NYT::TJobFactory::Get()->RegisterRawJob<TRawJob>(name);
}
};
template <class TVanillaJob>
struct TVanillaJobRegistrator
{
TVanillaJobRegistrator(const char* name)
{
static_assert(TVanillaJob::JobType == IJob::EType::VanillaJob,
"REGISTER_VANILLA_JOB is not compatible with this job class");
NYT::TJobFactory::Get()->RegisterVanillaJob<TVanillaJob>(name);
}
};
////////////////////////////////////////////////////////////////////////////////
inline TString YtRegistryTypeName(const TString& name) {
TString res = name;
#ifdef _win_
SubstGlobal(res, "class ", "");
#endif
return res;
}
////////////////////////////////////////////////////////////////////////////////
#define REGISTER_MAPPER(...) \
static const NYT::TMapperRegistrator<__VA_ARGS__> \
Y_GENERATE_UNIQUE_ID(TJobRegistrator)(NYT::YtRegistryTypeName(TypeName<__VA_ARGS__>()).data());
#define REGISTER_NAMED_MAPPER(name, ...) \
static const NYT::TMapperRegistrator<__VA_ARGS__> \
Y_GENERATE_UNIQUE_ID(TJobRegistrator)(name);
#define REGISTER_REDUCER(...) \
static const NYT::TReducerRegistrator<__VA_ARGS__> \
Y_GENERATE_UNIQUE_ID(TJobRegistrator)(NYT::YtRegistryTypeName(TypeName<__VA_ARGS__>()).data());
#define REGISTER_NAMED_REDUCER(name, ...) \
static const NYT::TReducerRegistrator<__VA_ARGS__> \
Y_GENERATE_UNIQUE_ID(TJobRegistrator)(name);
#define REGISTER_NAMED_RAW_JOB(name, ...) \
static const NYT::TRawJobRegistrator<__VA_ARGS__> \
Y_GENERATE_UNIQUE_ID(TJobRegistrator)(name);
#define REGISTER_RAW_JOB(...) \
REGISTER_NAMED_RAW_JOB((NYT::YtRegistryTypeName(TypeName<__VA_ARGS__>()).data()), __VA_ARGS__)
#define REGISTER_NAMED_VANILLA_JOB(name, ...) \
static NYT::TVanillaJobRegistrator<__VA_ARGS__> \
Y_GENERATE_UNIQUE_ID(TJobRegistrator)(name);
#define REGISTER_VANILLA_JOB(...) \
REGISTER_NAMED_VANILLA_JOB((NYT::YtRegistryTypeName(TypeName<__VA_ARGS__>()).data()), __VA_ARGS__)
////////////////////////////////////////////////////////////////////////////////
template <typename TReader, typename TWriter>
TStructuredRowStreamDescription IMapper<TReader, TWriter>::GetInputRowStreamDescription() const
{
return NYT::NDetail::GetStructuredRowStreamDescription<typename TReader::TRowType>();
}
template <typename TReader, typename TWriter>
TStructuredRowStreamDescription IMapper<TReader, TWriter>::GetOutputRowStreamDescription() const
{
return NYT::NDetail::GetStructuredRowStreamDescription<typename TWriter::TRowType>();
}
////////////////////////////////////////////////////////////////////////////////
template <typename TReader, typename TWriter>
TStructuredRowStreamDescription IReducer<TReader, TWriter>::GetInputRowStreamDescription() const
{
return NYT::NDetail::GetStructuredRowStreamDescription<typename TReader::TRowType>();
}
template <typename TReader, typename TWriter>
TStructuredRowStreamDescription IReducer<TReader, TWriter>::GetOutputRowStreamDescription() const
{
return NYT::NDetail::GetStructuredRowStreamDescription<typename TWriter::TRowType>();
}
////////////////////////////////////////////////////////////////////////////////
template <typename TReader, typename TWriter>
TStructuredRowStreamDescription IAggregatorReducer<TReader, TWriter>::GetInputRowStreamDescription() const
{
return NYT::NDetail::GetStructuredRowStreamDescription<typename TReader::TRowType>();
}
template <typename TReader, typename TWriter>
TStructuredRowStreamDescription IAggregatorReducer<TReader, TWriter>::GetOutputRowStreamDescription() const
{
return NYT::NDetail::GetStructuredRowStreamDescription<typename TWriter::TRowType>();
}
////////////////////////////////////////////////////////////////////////////////
template <typename TWriter>
TStructuredRowStreamDescription IVanillaJob<TWriter>::GetInputRowStreamDescription() const
{
return TVoidStructuredRowStream();
}
template <typename TWriter>
TStructuredRowStreamDescription IVanillaJob<TWriter>::GetOutputRowStreamDescription() const
{
return NYT::NDetail::GetStructuredRowStreamDescription<typename TWriter::TRowType>();
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|