aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/computation/mkql_computation_node_impl.cpp
blob: 8a0cbf65ba983ec0325ea411caaec0f609eb81f0 (plain) (blame)
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
#include "mkql_computation_node_impl.h"

#include "yql/essentials/minikql/mkql_string_util.h"

namespace NKikimr {
namespace NMiniKQL {

void ThrowNotSupportedImplForClass(const TString& className, const char *func) {
    THROW yexception() << "Unsupported access to '" << func << "' method of: " << className;
} 

template <class IComputationNodeInterface>
void TRefCountedComputationNode<IComputationNodeInterface>::Ref() {
    ++Refs_;
}

template <class IComputationNodeInterface>
void TRefCountedComputationNode<IComputationNodeInterface>::UnRef() {
    Y_ABORT_UNLESS(Refs_ > 0);
    if (--Refs_ == 0) {
        delete this;
    }
}

template <class IComputationNodeInterface>
ui32 TRefCountedComputationNode<IComputationNodeInterface>::RefCount() const {
    return Refs_;
}

template class TRefCountedComputationNode<IComputationWideFlowNode>;
template class TRefCountedComputationNode<IComputationWideFlowProxyNode>;

TUnboxedImmutableComputationNode::TUnboxedImmutableComputationNode(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& value)
    : MemInfo(memInfo)
    , UnboxedValue(std::move(value))
    , RepresentationKind(UnboxedValue.HasValue() ? (UnboxedValue.IsBoxed() ? EValueRepresentation::Boxed : (UnboxedValue.IsString() ? EValueRepresentation::String : EValueRepresentation::Embedded)) : EValueRepresentation::Embedded)
{
    MKQL_MEM_TAKE(MemInfo, this, sizeof(*this), __MKQL_LOCATION__);
    TlsAllocState->LockObject(UnboxedValue);
}

TUnboxedImmutableComputationNode::~TUnboxedImmutableComputationNode() {
    MKQL_MEM_RETURN(MemInfo, this, sizeof(*this));
    TlsAllocState->UnlockObject(UnboxedValue);
}

NUdf::TUnboxedValue TUnboxedImmutableComputationNode::GetValue(TComputationContext& compCtx) const {
    Y_UNUSED(compCtx);
    if (!TlsAllocState->UseRefLocking && RepresentationKind == EValueRepresentation::String) {
        /// TODO: YQL-4461
        return MakeString(UnboxedValue.AsStringRef());
    }
    return UnboxedValue;
}

const IComputationNode* TUnboxedImmutableComputationNode::GetSource() const { return nullptr; }

IComputationNode* TUnboxedImmutableComputationNode::AddDependence(const IComputationNode*) { return nullptr; }

void TUnboxedImmutableComputationNode::RegisterDependencies() const {}

ui32 TUnboxedImmutableComputationNode::GetIndex() const {
    THROW yexception() << "Failed to get index.";
}

void TUnboxedImmutableComputationNode::CollectDependentIndexes(const IComputationNode*, TIndexesMap&) const {
    THROW yexception() << "Failed to collect dependent indexes.";
}

ui32 TUnboxedImmutableComputationNode::GetDependencyWeight() const {
    THROW yexception() << "Can't get dependency weight from const node.";
}

ui32 TUnboxedImmutableComputationNode::GetDependencesCount() const {
    THROW yexception() << "Can't get dependences count from const node.";
}

bool TUnboxedImmutableComputationNode::IsTemporaryValue() const { return false; }

void TUnboxedImmutableComputationNode::PrepareStageOne() {}
void TUnboxedImmutableComputationNode::PrepareStageTwo() {}

TString TUnboxedImmutableComputationNode::DebugString() const {
    return UnboxedValue ? (UnboxedValue.IsBoxed() ? "Boxed" : "Literal") : "Empty";
}

EValueRepresentation TUnboxedImmutableComputationNode::GetRepresentation() const {
    return RepresentationKind;
}

Y_NO_INLINE TStatefulComputationNodeBase::TStatefulComputationNodeBase(ui32 valueIndex, EValueRepresentation kind)
    : ValueIndex(valueIndex)
    , RepresentationKind(kind)
{}

Y_NO_INLINE TStatefulComputationNodeBase::~TStatefulComputationNodeBase()
{}

Y_NO_INLINE void TStatefulComputationNodeBase::AddDependenceImpl(const IComputationNode* node) {
    Dependencies.emplace_back(node);
}

Y_NO_INLINE void TStatefulComputationNodeBase::CollectDependentIndexesImpl(const IComputationNode* self, const IComputationNode* owner, 
    IComputationNode::TIndexesMap& dependencies, bool stateless) const {
    if (self == owner)
        return;

    if (const auto ins = dependencies.emplace(ValueIndex, RepresentationKind); ins.second) {
        std::for_each(Dependencies.cbegin(), Dependencies.cend(), std::bind(&IComputationNode::CollectDependentIndexes, std::placeholders::_1, owner, std::ref(dependencies)));

        if (stateless) {
            dependencies.erase(ins.first);
        }
    }
}


Y_NO_INLINE TStatefulSourceComputationNodeBase::TStatefulSourceComputationNodeBase()
{}

Y_NO_INLINE TStatefulSourceComputationNodeBase::~TStatefulSourceComputationNodeBase()
{}

Y_NO_INLINE void TStatefulSourceComputationNodeBase::PrepareStageOneImpl(const TConstComputationNodePtrVector& dependencies) {
    if (!Stateless) {
        Stateless = std::accumulate(dependencies.cbegin(), dependencies.cend(), 0,
            std::bind(std::plus<i32>(), std::placeholders::_1, std::bind(&IComputationNode::GetDependencyWeight, std::placeholders::_2))) <= 1;
    }
}

Y_NO_INLINE void TStatefulSourceComputationNodeBase::AddSource(IComputationNode* source) const {
    Sources.emplace(source);
}

template <class IComputationNodeInterface, bool SerializableState>
TStatefulComputationNode<IComputationNodeInterface, SerializableState>::TStatefulComputationNode(TComputationMutables& mutables, EValueRepresentation kind)
    : TStatefulComputationNodeBase(mutables.CurValueIndex++, kind)
{
    if constexpr (SerializableState) {
        mutables.SerializableValues.push_back(ValueIndex);
    }
}

template <class IComputationNodeInterface, bool SerializableState>
IComputationNode* TStatefulComputationNode<IComputationNodeInterface, SerializableState>::AddDependence(const IComputationNode* node) {
    AddDependenceImpl(node);
    return this;
}

template <class IComputationNodeInterface, bool SerializableState>
EValueRepresentation TStatefulComputationNode<IComputationNodeInterface, SerializableState>::GetRepresentation() const {
    return RepresentationKind;
}

template <class IComputationNodeInterface, bool SerializableState>
void TStatefulComputationNode<IComputationNodeInterface, SerializableState>::InitNode(TComputationContext&) const {}

template <class IComputationNodeInterface, bool SerializableState>
ui32 TStatefulComputationNode<IComputationNodeInterface, SerializableState>::GetIndex() const { return ValueIndex; }

template <class IComputationNodeInterface, bool SerializableState>
ui32 TStatefulComputationNode<IComputationNodeInterface, SerializableState>::GetDependencesCount() const { return Dependencies.size(); }

template class TStatefulComputationNode<IComputationNode, false>;
template class TStatefulComputationNode<IComputationWideFlowNode, false>;
template class TStatefulComputationNode<IComputationExternalNode, false>;
template class TStatefulComputationNode<IComputationNode, true>;
template class TStatefulComputationNode<IComputationWideFlowNode, true>;
template class TStatefulComputationNode<IComputationExternalNode, true>;

Y_NO_INLINE ui32 TStatelessFlowComputationNodeBase::GetIndexImpl() const {
    THROW yexception() << "Failed to get stateless node index.";
}

Y_NO_INLINE void TStatelessFlowComputationNodeBase::CollectDependentIndexesImpl(const IComputationNode* self,
    const IComputationNode* owner, IComputationNode::TIndexesMap& dependencies,
    const IComputationNode* dependence) const {
    if (self == owner)
        return;

    if (dependence) {
        dependence->CollectDependentIndexes(owner, dependencies);
    }
}

Y_NO_INLINE TStatefulFlowComputationNodeBase::TStatefulFlowComputationNodeBase(ui32 stateIndex, EValueRepresentation stateKind)
    : StateIndex(stateIndex)
    , StateKind(stateKind)
{}

Y_NO_INLINE void TStatefulFlowComputationNodeBase::CollectDependentIndexesImpl(const IComputationNode* self, const IComputationNode* owner, 
    IComputationNode::TIndexesMap& dependencies, const IComputationNode* dependence) const {
    if (self == owner)
        return;

    const auto ins = dependencies.emplace(StateIndex, StateKind);
    if (ins.second && dependence) {
        dependence->CollectDependentIndexes(owner, dependencies);
    }
}

Y_NO_INLINE TPairStateFlowComputationNodeBase::TPairStateFlowComputationNodeBase(ui32 stateIndex, EValueRepresentation firstKind, EValueRepresentation secondKind)
    : StateIndex(stateIndex)
    , FirstKind(firstKind)
    , SecondKind(secondKind)
{}

Y_NO_INLINE void TPairStateFlowComputationNodeBase::CollectDependentIndexesImpl(const IComputationNode* self, const IComputationNode* owner, 
    IComputationNode::TIndexesMap& dependencies, const IComputationNode* dependence) const {
    if (self == owner)
        return;

    const auto ins1 = dependencies.emplace(StateIndex, FirstKind);
    const auto ins2 = dependencies.emplace(StateIndex + 1U, SecondKind);
    if (ins1.second && ins2.second && dependence) {
        dependence->CollectDependentIndexes(owner, dependencies);
    }
}

Y_NO_INLINE ui32 TStatelessWideFlowComputationNodeBase::GetIndexImpl() const {
    THROW yexception() << "Failed to get stateless node index.";
}

Y_NO_INLINE void TStatelessWideFlowComputationNodeBase::CollectDependentIndexesImpl(const IComputationNode* self, const IComputationNode* owner, 
    IComputationNode::TIndexesMap& dependencies, const IComputationNode* dependence) const {
    if (self == owner)
        return;

    if (dependence) {
        dependence->CollectDependentIndexes(owner, dependencies);
    }
}

Y_NO_INLINE EValueRepresentation TWideFlowBaseComputationNodeBase::GetRepresentationImpl() const {
    THROW yexception() << "Failed to get representation kind.";
}

Y_NO_INLINE NUdf::TUnboxedValue TWideFlowBaseComputationNodeBase::GetValueImpl(TComputationContext&) const {
    THROW yexception() << "Failed to get value from wide flow node.";
}

Y_NO_INLINE TStatefulWideFlowComputationNodeBase::TStatefulWideFlowComputationNodeBase(ui32 stateIndex, EValueRepresentation stateKind)
    : StateIndex(stateIndex)
    , StateKind(stateKind)
{}

Y_NO_INLINE void TStatefulWideFlowComputationNodeBase::CollectDependentIndexesImpl(const IComputationNode* self,
    const IComputationNode* owner, IComputationNode::TIndexesMap& dependencies, const IComputationNode* dependence) const {
    if (self == owner)
        return;

    const auto ins = dependencies.emplace(StateIndex, StateKind);
    if (ins.second && dependence) {
        dependence->CollectDependentIndexes(owner, dependencies);
    }
}

Y_NO_INLINE TPairStateWideFlowComputationNodeBase::TPairStateWideFlowComputationNodeBase(
    ui32 stateIndex, EValueRepresentation firstKind, EValueRepresentation secondKind)
    : StateIndex(stateIndex)
    , FirstKind(firstKind)
    , SecondKind(secondKind)
{}

Y_NO_INLINE void TPairStateWideFlowComputationNodeBase::CollectDependentIndexesImpl(
    const IComputationNode* self, const IComputationNode* owner, 
    IComputationNode::TIndexesMap& dependencies, const IComputationNode* dependence) const {
    if (self == owner)
        return;

    const auto ins1 = dependencies.emplace(StateIndex, FirstKind);
    const auto ins2 = dependencies.emplace(StateIndex + 1U, SecondKind);
    if (ins1.second && ins2.second && dependence) {
        dependence->CollectDependentIndexes(owner, dependencies);
    }
}

Y_NO_INLINE TDecoratorComputationNodeBase::TDecoratorComputationNodeBase(IComputationNode* node, EValueRepresentation kind)
    : Node(node)
    , Kind(kind)
{}

Y_NO_INLINE ui32 TDecoratorComputationNodeBase::GetIndexImpl() const {
    THROW yexception() << "Can't get index from decorator node.";
}

Y_NO_INLINE TString TDecoratorComputationNodeBase::DebugStringImpl(const TString& typeName) const {
    return typeName + "(" + Node->DebugString() + ")";
}

Y_NO_INLINE TBinaryComputationNodeBase::TBinaryComputationNodeBase(IComputationNode* left, IComputationNode* right, EValueRepresentation kind)
    : Left(left)
    , Right(right)
    , Kind(kind)
{}

Y_NO_INLINE ui32 TBinaryComputationNodeBase::GetIndexImpl() const {
    THROW yexception() << "Can't get index from decorator node.";
}

Y_NO_INLINE TString TBinaryComputationNodeBase::DebugStringImpl(const TString& typeName) const {
    return typeName + "(" + Left->DebugString() + "," + Right->DebugString() + ")";
}

void TExternalComputationNode::CollectDependentIndexes(const IComputationNode*, TIndexesMap& map) const {
    map.emplace(ValueIndex, RepresentationKind);
}

TExternalComputationNode::TExternalComputationNode(TComputationMutables& mutables, EValueRepresentation kind)
    : TStatefulComputationNode(mutables, kind)
{}

NUdf::TUnboxedValue TExternalComputationNode::GetValue(TComputationContext& ctx) const {
    return Getter ? Getter(ctx) : ValueRef(ctx);
}

NUdf::TUnboxedValue& TExternalComputationNode::RefValue(TComputationContext& ctx) const {
    InvalidateValue(ctx);
    return ValueRef(ctx);
}

void TExternalComputationNode::SetValue(TComputationContext& ctx, NUdf::TUnboxedValue&& value) const {
    ValueRef(ctx) = std::move(value);
    InvalidateValue(ctx);
}

TString TExternalComputationNode::DebugString() const {
    return "External";
}

void TExternalComputationNode::RegisterDependencies() const {}

void TExternalComputationNode::SetOwner(const IComputationNode* owner) {
    Y_DEBUG_ABORT_UNLESS(!Owner);
    Owner = owner;
}

void TExternalComputationNode::PrepareStageOne() {
    std::sort(Dependencies.begin(), Dependencies.end());
    Dependencies.erase(std::unique(Dependencies.begin(), Dependencies.end()), Dependencies.cend());
    if (const auto it = std::find(Dependencies.cbegin(), Dependencies.cend(), Owner); Dependencies.cend() != it)
        Dependencies.erase(it);
}

void TExternalComputationNode::PrepareStageTwo() {
    TIndexesMap dependencies;
    std::for_each(Dependencies.cbegin(), Dependencies.cend(),
        std::bind(&IComputationNode::CollectDependentIndexes, std::placeholders::_1, Owner, std::ref(dependencies)));
    InvalidationSet.assign(dependencies.cbegin(), dependencies.cend());
}

const IComputationNode* TExternalComputationNode::GetSource() const { return nullptr; }

ui32 TExternalComputationNode::GetDependencyWeight() const { return 0U; }

bool TExternalComputationNode::IsTemporaryValue() const {
    return bool(Getter);
}

void TExternalComputationNode::SetGetter(TGetter&& getter) {
    Getter = std::move(getter);
}

void TExternalComputationNode::InvalidateValue(TComputationContext& ctx) const {
    for (const auto& index : InvalidationSet) {
        ctx.MutableValues[index.first] = NUdf::TUnboxedValuePod::Invalid();
    }
}

bool TComputationValueBaseNotSupportedStub::HasFastListLength() const {
    ThrowNotSupported(__func__);
    return false;
}

ui64 TComputationValueBaseNotSupportedStub::GetListLength() const {
    ThrowNotSupported(__func__);
    return 0;
}

ui64 TComputationValueBaseNotSupportedStub::GetEstimatedListLength() const {
    ThrowNotSupported(__func__);
    return 0;
}

bool TComputationValueBaseNotSupportedStub::HasListItems() const {
    ThrowNotSupported(__func__);
    return false;
}

const NUdf::TOpaqueListRepresentation* TComputationValueBaseNotSupportedStub::GetListRepresentation() const {
    return nullptr;
}

NUdf::IBoxedValuePtr TComputationValueBaseNotSupportedStub::ReverseListImpl(const NUdf::IValueBuilder& builder) const {
    Y_UNUSED(builder);
    return nullptr;
}

NUdf::IBoxedValuePtr TComputationValueBaseNotSupportedStub::SkipListImpl(const NUdf::IValueBuilder& builder, ui64 count) const {
    Y_UNUSED(builder);
    Y_UNUSED(count);
    return nullptr;
}

NUdf::IBoxedValuePtr TComputationValueBaseNotSupportedStub::TakeListImpl(const NUdf::IValueBuilder& builder, ui64 count) const {
    Y_UNUSED(builder);
    Y_UNUSED(count);
    return nullptr;
}

NUdf::IBoxedValuePtr TComputationValueBaseNotSupportedStub::ToIndexDictImpl(const NUdf::IValueBuilder& builder) const {
    Y_UNUSED(builder);
    return nullptr;
}

ui64 TComputationValueBaseNotSupportedStub::GetDictLength() const {
    ThrowNotSupported(__func__);
    return 0;
}

bool TComputationValueBaseNotSupportedStub::HasDictItems() const {
    ThrowNotSupported(__func__);
    return false;
}

NUdf::TStringRef TComputationValueBaseNotSupportedStub::GetResourceTag() const {
    ThrowNotSupported(__func__);
    return NUdf::TStringRef();
}

void* TComputationValueBaseNotSupportedStub::GetResource() {
    ThrowNotSupported(__func__);
    return nullptr;
}

void TComputationValueBaseNotSupportedStub::Apply(NUdf::IApplyContext& applyCtx) const {
    Y_UNUSED(applyCtx);
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::GetListIterator() const {
    ThrowNotSupported(__func__);
    return {};
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::GetDictIterator() const {
    ThrowNotSupported(__func__);
    return {};
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::GetKeysIterator() const {
    ThrowNotSupported(__func__);
    return {};
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::GetPayloadsIterator() const {
    ThrowNotSupported(__func__);
    return {};
}

bool TComputationValueBaseNotSupportedStub::Contains(const NUdf::TUnboxedValuePod& key) const {
    Y_UNUSED(key);
    ThrowNotSupported(__func__);
    return false;
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::Lookup(const NUdf::TUnboxedValuePod& key) const {
    Y_UNUSED(key);
    ThrowNotSupported(__func__);
    return NUdf::TUnboxedValuePod();
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::GetElement(ui32 index) const {
    Y_UNUSED(index);
    ThrowNotSupported(__func__);
    return {};
}

const NUdf::TUnboxedValue* TComputationValueBaseNotSupportedStub::GetElements() const {
    return nullptr;
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::Run(
        const NUdf::IValueBuilder* valueBuilder,
        const NUdf::TUnboxedValuePod* args) const
{
    Y_UNUSED(valueBuilder);
    Y_UNUSED(args);
    ThrowNotSupported(__func__);
    return {};
}

bool TComputationValueBaseNotSupportedStub::Skip() {
    NUdf::TUnboxedValue stub;
    return Next(stub);
}

bool TComputationValueBaseNotSupportedStub::Next(NUdf::TUnboxedValue&) {
    ThrowNotSupported(__func__);
    return false;
}

bool TComputationValueBaseNotSupportedStub::NextPair(NUdf::TUnboxedValue&, NUdf::TUnboxedValue&) {
    ThrowNotSupported(__func__);
    return false;
}

ui32 TComputationValueBaseNotSupportedStub::GetVariantIndex() const {
    ThrowNotSupported(__func__);
    return 0;
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::GetVariantItem() const {
    ThrowNotSupported(__func__);
    return {};
}

NUdf::EFetchStatus TComputationValueBaseNotSupportedStub::Fetch(NUdf::TUnboxedValue& result) {
    Y_UNUSED(result);
    ThrowNotSupported(__func__);
    return NUdf::EFetchStatus::Finish;
}

ui32 TComputationValueBaseNotSupportedStub::GetTraverseCount() const {
    ThrowNotSupported(__func__);
    return 0;
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::GetTraverseItem(ui32 index) const {
    Y_UNUSED(index);
    ThrowNotSupported(__func__);
    return {};
}

NUdf::TUnboxedValue TComputationValueBaseNotSupportedStub::Save() const {
    ThrowNotSupported(__func__);
    return NUdf::TUnboxedValue::Zero();
}

void TComputationValueBaseNotSupportedStub::Load(const NUdf::TStringRef& state) {
    Y_UNUSED(state);
    ThrowNotSupported(__func__);
}

bool TComputationValueBaseNotSupportedStub::Load2(const NUdf::TUnboxedValue& state) {
    Y_UNUSED(state);
    ThrowNotSupported(__func__);
    return false;
}

void TComputationValueBaseNotSupportedStub::Push(const NUdf::TUnboxedValuePod& value) {
    Y_UNUSED(value);
    ThrowNotSupported(__func__);
}

bool TComputationValueBaseNotSupportedStub::IsSortedDict() const {
    ThrowNotSupported(__func__);
    return false;
}

void TComputationValueBaseNotSupportedStub::Unused1() {
    ThrowNotSupported(__func__);
}

void TComputationValueBaseNotSupportedStub::Unused2() {
    ThrowNotSupported(__func__);
}

void TComputationValueBaseNotSupportedStub::Unused3() {
    ThrowNotSupported(__func__);
}

void TComputationValueBaseNotSupportedStub::Unused4() {
    ThrowNotSupported(__func__);
}

void TComputationValueBaseNotSupportedStub::Unused5() {
    ThrowNotSupported(__func__);
}

void TComputationValueBaseNotSupportedStub::Unused6() {
    ThrowNotSupported(__func__);
}

NUdf::EFetchStatus TComputationValueBaseNotSupportedStub::WideFetch(NUdf::TUnboxedValue* result, ui32 width) {
    Y_UNUSED(result);
    Y_UNUSED(width);
    ThrowNotSupported(__func__);
    return NUdf::EFetchStatus::Finish;
}

TString TWideFlowProxyComputationNode::DebugString() const { return "WideFlowArg"; }

EValueRepresentation TWideFlowProxyComputationNode::GetRepresentation() const {
    THROW yexception() << "Failed to get representation kind.";
}

NUdf::TUnboxedValue TWideFlowProxyComputationNode::GetValue(TComputationContext&) const {
    THROW yexception() << "Failed to get value from wide flow node.";
}

ui32 TWideFlowProxyComputationNode::GetIndex() const {
    THROW yexception() << "Failed to get proxy node index.";
}

ui32 TWideFlowProxyComputationNode::GetDependencyWeight() const {
    THROW yexception() << "Failed to get dependency weight.";
}

ui32 TWideFlowProxyComputationNode::GetDependencesCount() const {
    return Dependence ? 1U : 0U;
}

IComputationNode* TWideFlowProxyComputationNode::AddDependence(const IComputationNode* node) {
    Y_DEBUG_ABORT_UNLESS(!Dependence);
    Dependence = node;
    return this;
}

const IComputationNode* TWideFlowProxyComputationNode::GetSource() const { return nullptr; }

bool TWideFlowProxyComputationNode::IsTemporaryValue() const { return true; }

void TWideFlowProxyComputationNode::RegisterDependencies() const {}

void TWideFlowProxyComputationNode::PrepareStageOne() {}

void TWideFlowProxyComputationNode::PrepareStageTwo() {
    if (Dependence) {
        TIndexesMap dependencies;
        Dependence->CollectDependentIndexes(Owner, dependencies);
        InvalidationSet.assign(dependencies.cbegin(), dependencies.cend());
    }
}

void TWideFlowProxyComputationNode::SetOwner(const IComputationNode* owner) {
    Y_DEBUG_ABORT_UNLESS(!Owner);
    Owner = owner;
}

void TWideFlowProxyComputationNode::CollectDependentIndexes(const IComputationNode*, TIndexesMap&) const {
    THROW yexception() << "Failed to collect dependent indexes.";
}

void TWideFlowProxyComputationNode::InvalidateValue(TComputationContext& ctx) const {
    for (const auto& index : InvalidationSet) {
        ctx.MutableValues[index.first] = NUdf::TUnboxedValuePod::Invalid();
    }
}

void TWideFlowProxyComputationNode::SetFetcher(TFetcher&& fetcher) {
    Fetcher = std::move(fetcher);
}

EFetchResult TWideFlowProxyComputationNode::FetchValues(TComputationContext& ctx, NUdf::TUnboxedValue*const* values) const {
    return Fetcher(ctx, values);
}

IComputationNode* LocateNode(const TNodeLocator& nodeLocator, TCallable& callable, ui32 index, bool pop) {
    return nodeLocator(callable.GetInput(index).GetNode(), pop);
}

IComputationNode* LocateNode(const TNodeLocator& nodeLocator, TNode& node, bool pop) {
    return nodeLocator(&node, pop);
}

IComputationExternalNode* LocateExternalNode(const TNodeLocator& nodeLocator, TCallable& callable, ui32 index, bool pop) {
    return dynamic_cast<IComputationExternalNode*>(LocateNode(nodeLocator, callable, index, pop));
}

template<class TContainerOne, class TContainerTwo>
TPasstroughtMap GetPasstroughtMap(const TContainerOne& from, const TContainerTwo& to) {
    TPasstroughtMap map(from.size());
    for (size_t i = 0U; i < map.size(); ++i) {
        for (size_t j = 0U; j < to.size(); ++j) {
            if (from[i] == to[j]) {
                map[i].emplace(j);
                break;
            }
        }
    }
    return map;
}

template<class TContainerOne, class TContainerTwo>
TPasstroughtMap GetPasstroughtMapOneToOne(const TContainerOne& from, const TContainerTwo& to) {
    TPasstroughtMap map(from.size());
    std::unordered_map<typename TContainerOne::value_type, size_t> unique(map.size());
    for (size_t i = 0U; i < map.size(); ++i) {
        if (const auto ins = unique.emplace(from[i], i); ins.second) {
            for (size_t j = 0U; j < to.size(); ++j) {
                if (from[i] == to[j]) {
                    if (auto& item = map[i]) {
                        item.reset();
                        break;
                    } else
                        item.emplace(j);

                }
            }
        } else
            map[ins.first->second].reset();
    }
    return map;
}

template TPasstroughtMap GetPasstroughtMap(const TComputationExternalNodePtrVector& from, const TComputationNodePtrVector& to);
template TPasstroughtMap GetPasstroughtMap(const TComputationNodePtrVector& from, const TComputationExternalNodePtrVector& to);
template TPasstroughtMap GetPasstroughtMapOneToOne(const TComputationExternalNodePtrVector& from, const TComputationNodePtrVector& to);
template TPasstroughtMap GetPasstroughtMapOneToOne(const TComputationNodePtrVector& from, const TComputationExternalNodePtrVector& to);

std::optional<size_t> IsPasstrought(const IComputationNode* root, const TComputationExternalNodePtrVector& args) {
    for (size_t i = 0U; i < args.size(); ++i)
        if (root == args[i])
            return {i};
    return std::nullopt;
}

TPasstroughtMap MergePasstroughtMaps(const TPasstroughtMap& lhs, const TPasstroughtMap& rhs) {
    TPasstroughtMap map(std::min(lhs.size(), rhs.size()));
    auto i = 0U;
    for (auto& item : map) {
        if (const auto l = lhs[i], r = rhs[i]; l && r && *l == *r) {
            item.emplace(*l);
        }
        ++i;
    }
    return map;
}

void ApplyChanges(const NUdf::TUnboxedValue& list, NUdf::IApplyContext& applyCtx) {
    TThresher<false>::DoForEachItem(list,
        [&applyCtx] (const NUdf::TUnboxedValue& item) {
            if (item.IsBoxed())
                item.Apply(applyCtx);
        }
    );
}

const IComputationNode* GetCommonSource(const IComputationNode* first, const IComputationNode* second, const IComputationNode* common) {
    const auto f = first->GetSource();
    const auto s = second->GetSource();
    if (f && s) {
        return f == s ? f : common;
    } else if (f) {
        return f;
    } else if (s) {
        return s;
    }
    return common;
}

void CleanupCurrentContext() {
    auto& allocState = *TlsAllocState;
    if (allocState.CurrentContext) {
        TAllocState::CleanupPAllocList(allocState.CurrentPAllocList);
    }
}

}
}