aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/computation/mkql_computation_list_adapter.h
blob: 948052d33a299b8beea26aace3276315dd6daa77 (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
#pragma once
#include <yql/essentials/minikql/defs.h>
#include "mkql_computation_node_impl.h"
#include <yql/essentials/minikql/mkql_alloc.h>

namespace NKikimr {
namespace NMiniKQL {

template <typename TVectorType>
class TVectorListAdapter : public TComputationValue<TVectorListAdapter<TVectorType>> {
public:
    typedef typename TVectorType::value_type TItem;
    typedef TVectorListAdapter<TVectorType> TSelf;
    typedef std::function<NUdf::TUnboxedValue(const TItem&)> TItemFactory;
    typedef TComputationValue<TVectorListAdapter<TVectorType>> TBase;

    class TIterator: public TTemporaryComputationValue<TIterator> {
    public:
        TIterator(TMemoryUsageInfo* memInfo, const TVectorType& list, TItemFactory itemFactory, ui64 start, ui64 finish, bool reversed)
            : TTemporaryComputationValue<TIterator>(memInfo)
            , List(list)
            , ItemFactory(itemFactory)
            , Start(start)
            , Finish(finish)
            , Reversed(reversed)
        {
            Index = Reversed ? Finish : (Start - 1);
        }

    private:
        bool Next(NUdf::TUnboxedValue& value) override {
            if (!Skip())
                return false;
            value = ItemFactory(List[Index]);
            return true;
        }

        bool Skip() override {
            if (!Reversed) {
                if (Index + 1 >= Finish)
                    return false;
                ++Index;
            } else {
                if (Index < Start + 1)
                    return false;
                --Index;
            }

            return true;
        }

        const TVectorType& List;
        const TItemFactory ItemFactory;
        const ui64 Start;
        const ui64 Finish;
        const bool Reversed;
        ui64 Index;
    };

    class TDictIterator : public TTemporaryComputationValue<TDictIterator> {
    public:
        TDictIterator(TMemoryUsageInfo* memInfo, THolder<TIterator>&& iter)
            : TTemporaryComputationValue<TDictIterator>(memInfo)
            , Iter(std::move(iter))
            , Index(Max<ui64>())
        {}

    private:
        bool Next(NUdf::TUnboxedValue& key) override {
            if (NUdf::TBoxedValueAccessor::Skip(*Iter)) {
                key = NUdf::TUnboxedValuePod(++Index);
                return true;
            }

            return false;
        }

        bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
            if (NUdf::TBoxedValueAccessor::Next(*Iter, payload)) {
                key = NUdf::TUnboxedValuePod(++Index);
                return true;
            }

            return false;
        }

        bool Skip() override {
            if (NUdf::TBoxedValueAccessor::Skip(*Iter)) {
                ++Index;
                return true;
            }

            return false;
        }

        THolder<TIterator> Iter;
        ui64 Index;
    };

    TVectorListAdapter(
            TMemoryUsageInfo* memInfo,
            const TVectorType& list,
            TItemFactory itemFactory,
            ui64 start, ui64 finish,
            bool reversed)
        : TBase(memInfo)
        , List(list)
        , ItemFactory(itemFactory)
        , Start(start)
        , Finish(finish)
        , Reversed(reversed)
    {
        Y_ABORT_UNLESS(Start <= Finish && Finish <= List.size());
    }

private:
    bool HasFastListLength() const override {
        return true;
    }

    ui64 GetListLength() const override {
        return Finish - Start;
    }

    ui64 GetEstimatedListLength() const override {
        return Finish - Start;
    }

    bool HasListItems() const override {
        return Finish != Start;
    }

    NUdf::IBoxedValuePtr ReverseListImpl(const NUdf::IValueBuilder& builder) const override {
        Y_UNUSED(builder);
        return new TSelf(this->GetMemInfo(), List, ItemFactory, Start, Finish, !Reversed);
    }

    NUdf::IBoxedValuePtr SkipListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
        Y_UNUSED(builder);
        const ui64 newStart = Min(Start + count, Finish);
        if (newStart == Start) {
            return const_cast<TVectorListAdapter*>(this);
        }

        return new TSelf(this->GetMemInfo(), List, ItemFactory, newStart, Finish, Reversed);
    }

    NUdf::IBoxedValuePtr TakeListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
        Y_UNUSED(builder);
        const ui64 newFinish = Min(Start + count, Finish);
        if (newFinish == Finish) {
            return const_cast<TVectorListAdapter*>(this);
        }

        return new TSelf(this->GetMemInfo(), List, ItemFactory, Start, newFinish, Reversed);
    }

    NUdf::IBoxedValuePtr ToIndexDictImpl(const NUdf::IValueBuilder& builder) const override {
        Y_UNUSED(builder);
        return const_cast<TVectorListAdapter*>(this);
    }

    NUdf::TUnboxedValue GetListIterator() const override {
        return NUdf::TUnboxedValuePod(new TIterator(this->GetMemInfo(), List, ItemFactory, Start, Finish, Reversed));
    }

    bool Contains(const NUdf::TUnboxedValuePod& key) const override {
        const ui64 index = key.Get<ui64>();
        return (index < GetListLength());
    }

    NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
        const ui64 index = key.Get<ui64>();
        if (index >= GetListLength()) {
            return NUdf::TUnboxedValuePod();
        }

        const ui64 realIndex = Reversed ? (Finish - 1 - index) : (Start + index);
        return ItemFactory(List[realIndex]).Release().MakeOptional();
    }

    NUdf::TUnboxedValue GetKeysIterator() const override {
        return NUdf::TUnboxedValuePod(new TDictIterator(this->GetMemInfo(), MakeHolder<TIterator>(this->GetMemInfo(), List, ItemFactory, Start, Finish, Reversed)));
    }

    NUdf::TUnboxedValue GetDictIterator() const override {
        return NUdf::TUnboxedValuePod(new TDictIterator(this->GetMemInfo(), MakeHolder<TIterator>(this->GetMemInfo(), List, ItemFactory, Start, Finish, Reversed)));
    }

    ui64 GetDictLength() const override {
        return GetListLength();
    }

    bool HasDictItems() const override {
        return Finish != Start;
    }

    bool IsSortedDict() const override {
        return true;
    }

private:
    const TVectorType& List;
    const TItemFactory ItemFactory;
    const ui64 Start;
    const ui64 Finish;
    const bool Reversed;
};

template <typename TVectorType>
class TOwningVectorListAdapter : private TVectorType, public TVectorListAdapter<TVectorType> {
public:
    using TAdapterBase = TVectorListAdapter<TVectorType>;

    TOwningVectorListAdapter(
            TMemoryUsageInfo* memInfo,
            TVectorType&& list,
            typename TAdapterBase::TItemFactory itemFactory,
            ui64 start, ui64 finish,
            bool reversed)
        : TVectorType(std::move(list))
        , TAdapterBase(memInfo, *this, itemFactory, start, finish, reversed) {}

    TOwningVectorListAdapter(
            TMemoryUsageInfo* memInfo,
            const TVectorType& list,
            typename TAdapterBase::TItemFactory itemFactory,
            ui64 start, ui64 finish,
            bool reversed)
        : TVectorType(list)
        , TAdapterBase(memInfo, *this, itemFactory, start, finish, reversed) {}
};

template<typename TVectorType>
NUdf::TUnboxedValue CreateOwningVectorListAdapter(
    TVectorType&& list,
    typename TVectorListAdapter<std::remove_reference_t<TVectorType>>::TItemFactory itemFactory,
    ui64 start, ui64 finish,
    bool reversed,
    TMemoryUsageInfo& memInfo)
{
    return NUdf::TUnboxedValuePod(new TOwningVectorListAdapter<std::remove_reference_t<TVectorType>>(
        &memInfo, std::forward<TVectorType>(list), itemFactory, start, finish, reversed));
}

}
}