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
|
#include "mkql_wide_map.h"
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/utils/cast.h>
namespace NKikimr {
namespace NMiniKQL {
using NYql::EnsureDynamicCast;
namespace {
class TExpandMapWrapper : public TStatelessWideFlowCodegeneratorNode<TExpandMapWrapper> {
using TBaseComputation = TStatelessWideFlowCodegeneratorNode<TExpandMapWrapper>;
public:
TExpandMapWrapper(IComputationNode* flow, IComputationExternalNode* item, TComputationNodePtrVector&& newItems)
: TBaseComputation(flow), Flow(flow), Item(item), NewItems(std::move(newItems))
{}
EFetchResult DoCalculate(TComputationContext& ctx, NUdf::TUnboxedValue*const* output) const {
if (auto item = Flow->GetValue(ctx); item.IsSpecial()) {
return item.IsYield() ? EFetchResult::Yield : EFetchResult::Finish;
} else {
Item->SetValue(ctx, std::move(item));
}
for (const auto item : NewItems)
if (const auto out = *output++)
*out = item->GetValue(ctx);
return EFetchResult::One;
}
#ifndef MKQL_DISABLE_CODEGEN
TGenerateResult DoGenGetValues(const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto codegenItem = dynamic_cast<ICodegeneratorExternalNode*>(Item);
MKQL_ENSURE(codegenItem, "Item must be codegenerator node.");
const auto item = GetNodeValue(Flow, ctx, block);
const auto resultType = Type::getInt32Ty(context);
const auto outres = SelectInst::Create(IsYield(item, block), ConstantInt::get(resultType, 0), ConstantInt::get(resultType, -1), "outres", block);
const auto work = BasicBlock::Create(context, "work", ctx.Func);
const auto pass = BasicBlock::Create(context, "pass", ctx.Func);
const auto result = PHINode::Create(outres->getType(), 2, "result", pass);
result->addIncoming(outres, block);
BranchInst::Create(pass, work, IsSpecial(item, block), block);
block = work;
codegenItem->CreateSetValue(ctx, block, item);
result->addIncoming(ConstantInt::get(resultType, 1), block);
BranchInst::Create(pass, block);
block = pass;
TGettersList getters;
getters.reserve(NewItems.size());
std::transform(NewItems.cbegin(), NewItems.cend(), std::back_inserter(getters), [&](IComputationNode* node) {
return [node](const TCodegenContext& ctx, BasicBlock*& block){ return GetNodeValue(node, ctx, block); };
});
return {result, std::move(getters)};
}
#endif
private:
void RegisterDependencies() const final {
if (const auto flow = FlowDependsOn(Flow)) {
Own(flow, Item);
std::for_each(NewItems.cbegin(), NewItems.cend(), std::bind(&TExpandMapWrapper::DependsOn, flow, std::placeholders::_1));
}
}
IComputationNode* const Flow;
IComputationExternalNode *const Item;
const TComputationNodePtrVector NewItems;
};
class TWideMapWrapper : public TStatelessWideFlowCodegeneratorNode<TWideMapWrapper> {
using TBaseComputation = TStatelessWideFlowCodegeneratorNode<TWideMapWrapper>;
public:
TWideMapWrapper(TComputationMutables& mutables, IComputationWideFlowNode* flow, TComputationExternalNodePtrVector&& items, TComputationNodePtrVector&& newItems)
: TBaseComputation(flow)
, Flow(flow)
, Items(std::move(items))
, NewItems(std::move(newItems))
, PasstroughtMap(GetPasstroughtMapOneToOne(Items, NewItems))
, ReversePasstroughtMap(GetPasstroughtMapOneToOne(NewItems, Items))
, WideFieldsIndex(mutables.IncrementWideFieldsIndex(Items.size()))
{}
EFetchResult DoCalculate(TComputationContext& ctx, NUdf::TUnboxedValue*const* output) const {
auto** fields = ctx.WideFields.data() + WideFieldsIndex;
for (auto i = 0U; i < Items.size(); ++i)
if (const auto& map = PasstroughtMap[i]; map && !Items[i]->GetDependencesCount()) {
if (const auto out = output[*map])
fields[i] = out;
} else
fields[i] = &Items[i]->RefValue(ctx);
if (const auto result = Flow->FetchValues(ctx, fields); EFetchResult::One != result)
return result;
for (auto i = 0U; i < NewItems.size(); ++i) {
if (const auto out = output[i]) {
if (const auto& map = ReversePasstroughtMap[i]) {
if (const auto from = *map; !Items[from]->GetDependencesCount()) {
if (const auto first = *PasstroughtMap[from]; first != i)
*out = *output[first];
continue;
}
}
*out = NewItems[i]->GetValue(ctx);
}
}
return EFetchResult::One;
}
#ifndef MKQL_DISABLE_CODEGEN
TGenerateResult DoGenGetValues(const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto result = GetNodeValues(Flow, ctx, block);
const auto good = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, result.first, ConstantInt::get(result.first->getType(), 0), "good", block);
const auto work = BasicBlock::Create(context, "work", ctx.Func);
const auto pass = BasicBlock::Create(context, "pass", ctx.Func);
BranchInst::Create(work, pass, good, block);
block = work;
for (auto i = 0U; i < Items.size(); ++i)
if (Items[i]->GetDependencesCount() > 0U || !PasstroughtMap[i])
EnsureDynamicCast<ICodegeneratorExternalNode*>(Items[i])->CreateSetValue(ctx, block, result.second[i](ctx, block));
BranchInst::Create(pass, block);
block = pass;
TGettersList getters;
getters.reserve(NewItems.size());
for (auto i = 0U; i < NewItems.size(); ++i) {
if (const auto map = ReversePasstroughtMap[i])
getters.emplace_back(result.second[*map]);
else
getters.emplace_back([node=NewItems[i]](const TCodegenContext& ctx, BasicBlock*& block){ return GetNodeValue(node, ctx, block); });
};
return {result.first, std::move(getters)};
}
#endif
private:
void RegisterDependencies() const final {
if (const auto flow = FlowDependsOn(Flow)) {
std::for_each(Items.cbegin(), Items.cend(), std::bind(&TWideMapWrapper::Own, flow, std::placeholders::_1));
std::for_each(NewItems.cbegin(), NewItems.cend(), std::bind(&TWideMapWrapper::DependsOn, flow, std::placeholders::_1));
}
}
IComputationWideFlowNode* const Flow;
const TComputationExternalNodePtrVector Items;
const TComputationNodePtrVector NewItems;
const TPasstroughtMap PasstroughtMap, ReversePasstroughtMap;
const ui32 WideFieldsIndex;
};
class TNarrowMapWrapper : public TStatelessFlowCodegeneratorNode<TNarrowMapWrapper> {
using TBaseComputation = TStatelessFlowCodegeneratorNode<TNarrowMapWrapper>;
public:
TNarrowMapWrapper(TComputationMutables& mutables, EValueRepresentation kind, IComputationWideFlowNode* flow, TComputationExternalNodePtrVector&& items, IComputationNode* newItem)
: TBaseComputation(flow, kind)
, Flow(flow)
, Items(std::move(items))
, NewItem(newItem)
, PasstroughItem(GetPasstroughtMap(TComputationNodePtrVector{NewItem}, Items).front())
, WideFieldsIndex(mutables.IncrementWideFieldsIndex(Items.size()))
{}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
auto** fields = ctx.WideFields.data() + WideFieldsIndex;
for (auto i = 0U; i < Items.size(); ++i) {
if (NewItem == Items[i] || Items[i]->GetDependencesCount() > 0U)
fields[i] = &Items[i]->RefValue(ctx);
}
switch (const auto result = Flow->FetchValues(ctx, fields)) {
case EFetchResult::Finish:
return NUdf::TUnboxedValuePod::MakeFinish();
case EFetchResult::Yield:
return NUdf::TUnboxedValuePod::MakeYield();
case EFetchResult::One:
return NewItem->GetValue(ctx).Release();
}
}
#ifndef MKQL_DISABLE_CODEGEN
Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto getres = GetNodeValues(Flow, ctx, block);
const auto yield = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, getres.first, ConstantInt::get(getres.first->getType(), 0), "yield", block);
const auto good = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, getres.first, ConstantInt::get(getres.first->getType(), 0), "good", block);
const auto outres = SelectInst::Create(yield, GetYield(context), GetFinish(context), "outres", block);
const auto work = BasicBlock::Create(context, "work", ctx.Func);
const auto pass = BasicBlock::Create(context, "pass", ctx.Func);
const auto result = PHINode::Create(outres->getType(), 2, "result", pass);
result->addIncoming(outres, block);
BranchInst::Create(work, pass, good, block);
block = work;
if (const auto passtrough = PasstroughItem) {
result->addIncoming(getres.second[*passtrough](ctx, block), block);
} else {
for (auto i = 0U; i < Items.size(); ++i)
if (Items[i]->GetDependencesCount() > 0U)
EnsureDynamicCast<ICodegeneratorExternalNode*>(Items[i])->CreateSetValue(ctx, block, getres.second[i](ctx, block));
result->addIncoming(GetNodeValue(NewItem, ctx, block), block);
}
BranchInst::Create(pass, block);
block = pass;
return result;
}
#endif
private:
void RegisterDependencies() const final {
if (const auto flow = FlowDependsOn(Flow)) {
std::for_each(Items.cbegin(), Items.cend(), std::bind(&TNarrowMapWrapper::Own, flow, std::placeholders::_1));
DependsOn(flow, NewItem);
}
}
IComputationWideFlowNode* const Flow;
const TComputationExternalNodePtrVector Items;
IComputationNode* const NewItem;
const std::optional<size_t> PasstroughItem;
const ui32 WideFieldsIndex;
};
}
IComputationNode* WrapExpandMap(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
const auto width = GetWideComponentsCount(AS_TYPE(TFlowType, callable.GetType()->GetReturnType()));
MKQL_ENSURE(callable.GetInputsCount() == width + 2U, "Expected two or more args.");
const auto flow = LocateNode(ctx.NodeLocator, callable, 0U);
TComputationNodePtrVector newItems(width, nullptr);
ui32 index = 1U;
std::generate(newItems.begin(), newItems.end(), [&](){ return LocateNode(ctx.NodeLocator, callable, ++index); });
const auto itemArg = LocateExternalNode(ctx.NodeLocator, callable, 1U);
return new TExpandMapWrapper(flow, itemArg, std::move(newItems));
}
IComputationNode* WrapWideMap(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() > 0U, "Expected argument.");
const auto inputWidth = GetWideComponentsCount(AS_TYPE(TFlowType, callable.GetInput(0U).GetStaticType()));
const auto outputWidth = GetWideComponentsCount(AS_TYPE(TFlowType, callable.GetType()->GetReturnType()));
MKQL_ENSURE(callable.GetInputsCount() == inputWidth + outputWidth + 1U, "Wrong signature.");
const auto flow = LocateNode(ctx.NodeLocator, callable, 0U);
if (const auto wide = dynamic_cast<IComputationWideFlowNode*>(flow)) {
TComputationNodePtrVector newItems(outputWidth, nullptr);
ui32 index = inputWidth;
std::generate(newItems.begin(), newItems.end(), [&](){ return LocateNode(ctx.NodeLocator, callable, ++index); });
TComputationExternalNodePtrVector args(inputWidth, nullptr);
index = 0U;
std::generate(args.begin(), args.end(), [&](){ return LocateExternalNode(ctx.NodeLocator, callable, ++index); });
return new TWideMapWrapper(ctx.Mutables, wide, std::move(args), std::move(newItems));
}
THROW yexception() << "Expected wide flow.";
}
IComputationNode* WrapNarrowMap(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() > 1U, "Expected two or more args.");
const auto width = GetWideComponentsCount(AS_TYPE(TFlowType, callable.GetInput(0U).GetStaticType()));
MKQL_ENSURE(callable.GetInputsCount() == width + 2U, "Wrong signature.");
const auto flow = LocateNode(ctx.NodeLocator, callable, 0U);
if (const auto wide = dynamic_cast<IComputationWideFlowNode*>(flow)) {
const auto newItem = LocateNode(ctx.NodeLocator, callable, callable.GetInputsCount() - 1U);
TComputationExternalNodePtrVector args(width, nullptr);
ui32 index = 0U;
std::generate(args.begin(), args.end(), [&](){ return LocateExternalNode(ctx.NodeLocator, callable, ++index); });
return new TNarrowMapWrapper(ctx.Mutables, GetValueRepresentation(callable.GetType()->GetReturnType()), wide, std::move(args), newItem);
}
THROW yexception() << "Expected wide flow.";
}
}
}
|