aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/comp_nodes/mkql_mapnext.cpp
blob: bbdf7970f5dd4d455a564d908d8adbee914142b6 (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
#include "mkql_mapnext.h"
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>

namespace NKikimr {
namespace NMiniKQL {

namespace {

struct TState : public TComputationValue<TState> {
    using TComputationValue::TComputationValue;

    std::optional<NUdf::TUnboxedValue> Prev;
    bool Finish = false;
};

class TFlowMapNextWrapper : public TStatefulFlowComputationNode<TFlowMapNextWrapper> {
    typedef TStatefulFlowComputationNode<TFlowMapNextWrapper> TBaseComputation;
public:
    TFlowMapNextWrapper(TComputationMutables& mutables, EValueRepresentation kind, IComputationNode* flow,
                        IComputationExternalNode* item, IComputationExternalNode* nextItem, IComputationNode* newItem)
        : TBaseComputation(mutables, flow, kind, EValueRepresentation::Any)
        , Flow(flow)
        , Item(item)
        , NextItem(nextItem)
        , NewItem(newItem)
    {}

    NUdf::TUnboxedValue DoCalculate(NUdf::TUnboxedValue& stateValue, TComputationContext& ctx) const {
        if (!stateValue.HasValue()) {
            stateValue = ctx.HolderFactory.Create<TState>();
        }
        TState& state = *static_cast<TState*>(stateValue.AsBoxed().Get());

        NUdf::TUnboxedValue result;
        for (;;) {
            if (state.Finish) {
                if (!state.Prev) {
                    return NUdf::TUnboxedValuePod::MakeFinish();
                }
                Item->SetValue(ctx, std::move(*state.Prev));
                state.Prev.reset();
                NextItem->SetValue(ctx, NUdf::TUnboxedValuePod());
                return NewItem->GetValue(ctx);
            }

            auto item = Flow->GetValue(ctx);
            if (item.IsYield()) {
                return item;
            }

            if (item.IsFinish()) {
                state.Finish = true;
                continue;
            }

            if (!state.Prev) {
                state.Prev = std::move(item);
                continue;
            }

            Item->SetValue(ctx, std::move(*state.Prev));
            state.Prev = item;
            NextItem->SetValue(ctx, std::move(item));
            result = NewItem->GetValue(ctx);
            break;
        }

        return result;
    }

private:
    void RegisterDependencies() const final {
        if (const auto flow = FlowDependsOn(Flow)) {
            Own(flow, Item);
            Own(flow, NextItem);
            DependsOn(flow, NewItem);
        }
    }

    IComputationNode* const Flow;
    IComputationExternalNode* const Item;
    IComputationExternalNode* const NextItem;
    IComputationNode* const NewItem;
};

class TStreamMapNextWrapper : public TMutableComputationNode<TStreamMapNextWrapper> {
    typedef TMutableComputationNode<TStreamMapNextWrapper> TBaseComputation;
public:
    TStreamMapNextWrapper(TComputationMutables& mutables, IComputationNode* stream,
                          IComputationExternalNode* item, IComputationExternalNode* nextItem, IComputationNode* newItem)
        : TBaseComputation(mutables)
        , Stream(stream)
        , Item(item)
        , NextItem(nextItem)
        , NewItem(newItem)
        , StateIndex(mutables.CurValueIndex++)
    {}

    NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
        return ctx.HolderFactory.Create<TStreamValue>(ctx, Stream->GetValue(ctx), Item, NextItem, NewItem, StateIndex);
    }

private:
    void RegisterDependencies() const final {
        DependsOn(Stream);
        Own(Item);
        Own(NextItem);
        DependsOn(NewItem);
    }

    class TStreamValue : public TComputationValue<TStreamValue> {
    public:
        using TBase = TComputationValue<TStreamValue>;

        TStreamValue(TMemoryUsageInfo* memInfo, TComputationContext& compCtx, NUdf::TUnboxedValue&& stream,
                     IComputationExternalNode* item, IComputationExternalNode* nextItem, IComputationNode* newItem, ui32 stateIndex)
            : TBase(memInfo)
            , CompCtx(compCtx)
            , Stream(std::move(stream))
            , Item(item)
            , NextItem(nextItem)
            , NewItem(newItem)
            , StateIndex(stateIndex)
        {
        }

    private:
        ui32 GetTraverseCount() const final {
            return 1U;
        }

        NUdf::TUnboxedValue GetTraverseItem(ui32) const final {
            return Stream;
        }

        NUdf::TUnboxedValue Save() const final {
            return NUdf::TUnboxedValuePod::Zero();
        }

        void Load(const NUdf::TStringRef&) final {}

        NUdf::EFetchStatus Fetch(NUdf::TUnboxedValue& result) final {
            auto& state = GetState();
            for (;;) {
                if (state.Finish) {
                    if (!state.Prev) {
                        return NUdf::EFetchStatus::Finish;
                    }
                    Item->SetValue(CompCtx, std::move(*state.Prev));
                    state.Prev.reset();
                    NextItem->SetValue(CompCtx, NUdf::TUnboxedValuePod());

                    result = NewItem->GetValue(CompCtx);
                    return NUdf::EFetchStatus::Ok;
                }

                NUdf::TUnboxedValue item;
                const auto status = Stream.Fetch(item);
                if (status == NUdf::EFetchStatus::Yield) {
                    return status;
                }

                if (status == NUdf::EFetchStatus::Finish) {
                    state.Finish = true;
                    continue;
                }

                if (!state.Prev) {
                    state.Prev = std::move(item);
                    continue;
                }

                Item->SetValue(CompCtx, std::move(*state.Prev));
                state.Prev = item;
                NextItem->SetValue(CompCtx, std::move(item));
                result = NewItem->GetValue(CompCtx);
                break;
            }
            return NUdf::EFetchStatus::Ok;
        }

        TState& GetState() const {
            auto& result = CompCtx.MutableValues[StateIndex];
            if (!result.HasValue()) {
                result = CompCtx.HolderFactory.Create<TState>();
            }
            return *static_cast<TState*>(result.AsBoxed().Get());
        }

        TComputationContext& CompCtx;
        const NUdf::TUnboxedValue Stream;
        IComputationExternalNode* const Item;
        IComputationExternalNode* const NextItem;
        IComputationNode* const NewItem;
        const ui32 StateIndex;
    };

    IComputationNode* const Stream;
    IComputationExternalNode* const Item;
    IComputationExternalNode* const NextItem;
    IComputationNode* const NewItem;
    const ui32 StateIndex;
};

}

IComputationNode* WrapMapNext(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
    MKQL_ENSURE(callable.GetInputsCount() == 4, "Expected 4 args, got " << callable.GetInputsCount());
    const auto type = callable.GetType()->GetReturnType();

    const auto input = LocateNode(ctx.NodeLocator, callable, 0);
    const auto itemArg = LocateExternalNode(ctx.NodeLocator, callable, 1);
    const auto nextItemArg = LocateExternalNode(ctx.NodeLocator, callable, 2);
    const auto newItem = LocateNode(ctx.NodeLocator, callable, 3);

    if (type->IsFlow()) {
        return new TFlowMapNextWrapper(ctx.Mutables, GetValueRepresentation(type), input, itemArg, nextItemArg, newItem);
    } else if (type->IsStream()) {
        return new TStreamMapNextWrapper(ctx.Mutables, input, itemArg, nextItemArg, newItem);
    }

    THROW yexception() << "Expected flow or stream.";
}

}
}