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
|
#include "mkql_replicate.h"
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_custom_list.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_program_builder.h>
namespace NKikimr {
namespace NMiniKQL {
namespace {
class TReplicateWrapper : public TMutableComputationNode<TReplicateWrapper> {
typedef TMutableComputationNode<TReplicateWrapper> TBaseComputation;
public:
class TValue : public TCustomListValue {
public:
template <EDictItems Mode>
class TIterator : public TComputationValue<TIterator<Mode>> {
public:
TIterator(TMemoryUsageInfo* memInfo, const NUdf::TUnboxedValue& item, ui64 count)
: TComputationValue<TIterator<Mode>>(memInfo)
, Item(item)
, Current(0)
, End(count)
{}
private:
bool NextPair(NUdf::TUnboxedValue& key, NUdf::TUnboxedValue& payload) override {
if (Current < End) {
switch (Mode) {
case EDictItems::Payloads:
this->ThrowNotSupported(__func__);
break;
case EDictItems::Keys:
this->ThrowNotSupported(__func__);
break;
case EDictItems::Both:
key = NUdf::TUnboxedValuePod(ui64(Current));
payload = Item;
break;
}
++Current;
return true;
}
return false;
}
bool Next(NUdf::TUnboxedValue& value) override {
if (Current < End) {
switch (Mode) {
case EDictItems::Payloads:
value = Item;
break;
case EDictItems::Keys:
value = NUdf::TUnboxedValuePod(ui64(Current));
break;
case EDictItems::Both:
this->ThrowNotSupported(__func__);
break;
}
++Current;
return true;
}
return false;
}
bool Skip() override {
if (Current < End) {
++Current;
return true;
}
return false;
}
const NUdf::TUnboxedValue Item;
ui64 Current;
const ui64 End;
};
TValue(TMemoryUsageInfo* memInfo, TComputationContext& ctx, const NUdf::TUnboxedValue& item, ui64 count)
: TCustomListValue(memInfo)
, Ctx(ctx)
, Item(item)
, Count(count)
{
}
private:
NUdf::TUnboxedValue GetListIterator() const override {
return Ctx.HolderFactory.Create<TIterator<EDictItems::Payloads>>(Item, Count);
}
bool HasFastListLength() const override {
return true;
}
ui64 GetListLength() const override {
return Count;
}
ui64 GetEstimatedListLength() const override {
return Count;
}
bool HasListItems() const override {
return Count > 0;
}
NUdf::IBoxedValuePtr ReverseListImpl(const NUdf::IValueBuilder& builder) const override {
Y_UNUSED(builder);
return const_cast<TValue*>(this);
}
NUdf::IBoxedValuePtr SkipListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
Y_UNUSED(builder);
if (count == 0) {
return const_cast<TValue*>(this);
}
if (count >= Count) {
return Ctx.HolderFactory.GetEmptyContainerLazy().AsBoxed();
}
return Ctx.HolderFactory.Create<TValue>(Ctx, Item, Count - count).AsBoxed();
}
NUdf::IBoxedValuePtr TakeListImpl(const NUdf::IValueBuilder& builder, ui64 count) const override {
Y_UNUSED(builder);
if (count == 0) {
return Ctx.HolderFactory.GetEmptyContainerLazy().AsBoxed();
}
if (count >= Count) {
return const_cast<TValue*>(this);
}
return Ctx.HolderFactory.Create<TValue>(Ctx, Item, count).AsBoxed();
}
NUdf::IBoxedValuePtr ToIndexDictImpl(const NUdf::IValueBuilder& builder) const override {
Y_UNUSED(builder);
return const_cast<TValue*>(this);
}
ui64 GetDictLength() const override {
return Count;
}
bool HasDictItems() const override {
return Count > 0;
}
bool Contains(const NUdf::TUnboxedValuePod& key) const override {
return key.Get<ui64>() < Count;
}
NUdf::TUnboxedValue Lookup(const NUdf::TUnboxedValuePod& key) const override {
if (key.Get<ui64>() < Count) {
return Item.MakeOptional();
}
return {};
}
NUdf::TUnboxedValue GetDictIterator() const override {
return Ctx.HolderFactory.Create<TIterator<EDictItems::Both>>(Item, Count);
}
NUdf::TUnboxedValue GetKeysIterator() const override {
return Ctx.HolderFactory.Create<TIterator<EDictItems::Keys>>(Item, Count);
}
NUdf::TUnboxedValue GetPayloadsIterator() const override {
return GetListIterator();
}
bool IsSortedDict() const override {
return true;
}
TComputationContext& Ctx;
const NUdf::TUnboxedValue Item;
const ui64 Count;
};
TReplicateWrapper(TComputationMutables& mutables, IComputationNode* item, IComputationNode* count,
NUdf::TSourcePosition pos)
: TBaseComputation(mutables)
, Item(item)
, Count(count)
, Pos(pos)
{
}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
const auto count = Count->GetValue(ctx).Get<ui64>();
const ui64 MAX_VALUE = 1ull << 32;
if (count >= MAX_VALUE) {
TStringBuilder res;
res << Pos << " Second argument in ListReplicate = " << count << " exceeds maximum value = " << MAX_VALUE;
UdfTerminate(res.data());
}
if (!count) {
return ctx.HolderFactory.GetEmptyContainerLazy();
}
return ctx.HolderFactory.Create<TValue>(ctx, Item->GetValue(ctx), count);
}
private:
void RegisterDependencies() const final {
DependsOn(Item);
DependsOn(Count);
}
IComputationNode* const Item;
IComputationNode* const Count;
const NUdf::TSourcePosition Pos;
};
}
IComputationNode* WrapReplicate(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 2 || callable.GetInputsCount() == 5, "Expected 2 or 5 args");
const auto countType = AS_TYPE(TDataType, callable.GetInput(1));
MKQL_ENSURE(countType->GetSchemeType() == NUdf::TDataType<ui64>::Id, "Expected ui64");
const auto list = LocateNode(ctx.NodeLocator, callable, 0);
const auto count = LocateNode(ctx.NodeLocator, callable, 1);
NUdf::TSourcePosition pos;
if (callable.GetInputsCount() == 5) {
const TStringBuf file = AS_VALUE(TDataLiteral, callable.GetInput(2))->AsValue().AsStringRef();
const ui32 row = AS_VALUE(TDataLiteral, callable.GetInput(3))->AsValue().Get<ui32>();
const ui32 column = AS_VALUE(TDataLiteral, callable.GetInput(4))->AsValue().Get<ui32>();
pos = NUdf::TSourcePosition(row, column, file);
}
return new TReplicateWrapper(ctx.Mutables, list, count, pos);
}
}
}
|