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
|
#include "yql_graph_transformer.h"
#include <yql/essentials/ast/yql_expr.h>
#include <yql/essentials/utils/yql_panic.h>
#include <yql/essentials/public/issue/yql_issue_manager.h>
namespace NYql {
namespace {
class TSharedTransformerProxy : public IGraphTransformer {
public:
TSharedTransformerProxy(const std::shared_ptr<IGraphTransformer>& inner)
: Inner_(inner)
{}
TStatus Transform(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) {
return Inner_->Transform(input, output, ctx);
}
NThreading::TFuture<void> GetAsyncFuture(const TExprNode& input) {
return Inner_->GetAsyncFuture(input);
}
TStatus ApplyAsyncChanges(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) {
return Inner_->ApplyAsyncChanges(input, output, ctx);
}
void Rewind() final {
return Inner_->Rewind();
}
TStatistics GetStatistics() const final {
return Inner_->GetStatistics();
}
private:
const std::shared_ptr<IGraphTransformer> Inner_;
};
class TCompositeGraphTransformer : public TGraphTransformerBase {
public:
TCompositeGraphTransformer(const TVector<TTransformStage>& stages, bool useIssueScopes, bool doCheckArguments)
: Stages_(stages)
, UseIssueScopes_(useIssueScopes)
, DoCheckArguments_(doCheckArguments)
{
if (UseIssueScopes_) {
for (const auto& stage : Stages_) {
YQL_ENSURE(!stage.Name.empty());
}
}
}
void Rewind() override {
for (auto& stage : Stages_) {
stage.GetTransformer().Rewind();
}
Index_ = 0;
CheckArgumentsCount_ = 0;
}
TStatus DoTransform(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) override {
//#define TRACE_NODES
#ifdef TRACE_NODES
static ui64 TransformsCount = 0;
++TransformsCount;
if ((TransformsCount % 100) == 0) {
Cout << "\r#transforms: " << TransformsCount << ", #nodes: " << ctx.NextUniqueId;
}
#endif
if (Index_ >= Stages_.size()) {
return TStatus::Ok;
}
auto status = WithScope(ctx, [&]() {
return Stages_[Index_].GetTransformer().Transform(input, output, ctx);
});
#ifndef NDEBUG
if (DoCheckArguments_ && output && output != input) {
try {
CheckArguments(*output);
++CheckArgumentsCount_;
} catch (yexception& e) {
e << "at CheckArguments() pass #" << CheckArgumentsCount_
<< ", stage '" << Stages_[Index_].Name << "'";
throw;
}
}
#else
Y_UNUSED(DoCheckArguments_);
Y_UNUSED(CheckArgumentsCount_);
#endif
status = HandleStatus(status);
return status;
}
NThreading::TFuture<void> DoGetAsyncFuture(const TExprNode& input) override {
YQL_ENSURE(Index_ < Stages_.size());
return Stages_[Index_].GetTransformer().GetAsyncFuture(input);
}
TStatus DoApplyAsyncChanges(TExprNode::TPtr input, TExprNode::TPtr& output, TExprContext& ctx) override {
YQL_ENSURE(Index_ < Stages_.size());
auto status = WithScope(ctx, [&]() {
return Stages_[Index_].GetTransformer().ApplyAsyncChanges(input, output, ctx);
});
status = HandleStatus(status);
return status;
}
TStatistics GetStatistics() const final {
if (Statistics_.Stages.empty()) {
Statistics_.Stages.resize(Stages_.size());
}
YQL_ENSURE(Stages_.size() == Statistics_.Stages.size());
for (size_t i = 0; i < Stages_.size(); ++i) {
auto& stagePair = Statistics_.Stages[i];
stagePair.first = Stages_[i].Name;
stagePair.second = Stages_[i].GetTransformer().GetStatistics();
}
return Statistics_;
}
private:
virtual TStatus HandleStatus(TStatus status) {
if (status.Level == IGraphTransformer::TStatus::Error) {
return status;
}
if (status.HasRestart) {
// ignore Async status in this case
Index_ = 0;
status = IGraphTransformer::TStatus(IGraphTransformer::TStatus::Repeat, true);
} else if (status.Level == IGraphTransformer::TStatus::Ok) {
status = IGraphTransformer::TStatus::Repeat;
++Index_;
}
return status;
}
template <typename TFunc>
TStatus WithScope(TExprContext& ctx, TFunc func) {
if (UseIssueScopes_) {
TIssueScopeGuard guard(ctx.IssueManager, [&]() {
const auto scopeIssueCode = Stages_[Index_].IssueCode;
const auto scopeIssueMessage = Stages_[Index_].IssueMessage;
auto issue = MakeIntrusive<TIssue>(TPosition(), scopeIssueMessage ? scopeIssueMessage : IssueCodeToString(scopeIssueCode));
issue->SetCode(scopeIssueCode, GetSeverity(scopeIssueCode));
return issue;
});
return func();
} else {
return func();
}
}
protected:
TVector<TTransformStage> Stages_;
const bool UseIssueScopes_;
const bool DoCheckArguments_;
size_t Index_ = 0;
ui64 CheckArgumentsCount_ = 0;
};
void AddTooManyTransformationsError(TPositionHandle pos, const TStringBuf& where, TExprContext& ctx) {
ctx.AddError(TIssue(ctx.GetPosition(pos),
TStringBuilder() << "YQL: Internal core error! " << where << " takes too much iterations: "
<< ctx.RepeatTransformLimit
<< ". You may set RepeatTransformLimit as flags for config provider."));
}
}
TAutoPtr<IGraphTransformer> CreateCompositeGraphTransformer(const TVector<TTransformStage>& stages, bool useIssueScopes) {
return new TCompositeGraphTransformer(stages, useIssueScopes, /* doCheckArguments = */ true);
}
TAutoPtr<IGraphTransformer> CreateCompositeGraphTransformerWithNoArgChecks(const TVector<TTransformStage>& stages, bool useIssueScopes) {
return new TCompositeGraphTransformer(stages, useIssueScopes, /* doCheckArguments = */ false);
}
namespace {
class TChoiceGraphTransformer : public TCompositeGraphTransformer {
public:
TChoiceGraphTransformer(
const std::function<bool(const TExprNode::TPtr& input, TExprContext& ctx)>& condition,
const TTransformStage& left,
const TTransformStage& right)
: TCompositeGraphTransformer(
{WrapCondition(condition), left, right},
/* useIssueScopes = */ false,
/* doCheckArgumentstrue = */ true)
{ }
private:
void Rewind() override {
Condition_.Clear();
TCompositeGraphTransformer::Rewind();
}
TStatus HandleStatus(TStatus status) override {
if (status.Level == IGraphTransformer::TStatus::Error) {
return status;
}
if (status.HasRestart) {
// ignore Async status in this case
Index_ = 0;
status = IGraphTransformer::TStatus(IGraphTransformer::TStatus::Repeat, true);
} else if (status.Level == IGraphTransformer::TStatus::Ok) {
status = IGraphTransformer::TStatus::Repeat;
YQL_ENSURE(!Condition_.Empty(), "Condition must be set");
if (Index_ == 0 && *Condition_) {
Index_ = 1; // left
} else if (Index_ == 0) {
Index_ = 2; // right
} else {
Index_ = 3; // end
}
}
return status;
}
TTransformStage WrapCondition(const std::function<bool(const TExprNode::TPtr& input, TExprContext& ctx)>& condition)
{
auto transformer = CreateFunctorTransformer([this, condition](const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) {
output = input;
if (Condition_.Empty()) {
Condition_ = condition(input, ctx);
}
return TStatus::Ok;
});
return TTransformStage(transformer, "Condition", TIssuesIds::DEFAULT_ERROR);
}
TMaybe<bool> Condition_;
};
} // namespace
TAutoPtr<IGraphTransformer> MakeSharedTransformerProxy(const std::shared_ptr<IGraphTransformer>& inner) {
return new TSharedTransformerProxy(inner);
}
TAutoPtr<IGraphTransformer> CreateChoiceGraphTransformer(
const std::function<bool(const TExprNode::TPtr& input, TExprContext& ctx)>& condition,
const TTransformStage& left, const TTransformStage& right)
{
return new TChoiceGraphTransformer(condition, left, right);
}
IGraphTransformer::TStatus SyncTransform(IGraphTransformer& transformer, TExprNode::TPtr& root, TExprContext& ctx) {
try {
for (; ctx.RepeatTransformCounter < ctx.RepeatTransformLimit; ++ctx.RepeatTransformCounter) {
TExprNode::TPtr newRoot;
auto status = transformer.Transform(root, newRoot, ctx);
if (newRoot) {
root = newRoot;
}
switch (status.Level) {
case IGraphTransformer::TStatus::Ok:
case IGraphTransformer::TStatus::Error:
return status;
case IGraphTransformer::TStatus::Repeat:
continue;
case IGraphTransformer::TStatus::Async:
break;
default:
YQL_ENSURE(false, "Unknown status");
}
auto future = transformer.GetAsyncFuture(*root);
future.Wait();
HandleFutureException(future);
status = transformer.ApplyAsyncChanges(root, newRoot, ctx);
if (newRoot) {
root = newRoot;
}
switch (status.Level) {
case IGraphTransformer::TStatus::Ok:
case IGraphTransformer::TStatus::Error:
return status;
case IGraphTransformer::TStatus::Repeat:
break;
case IGraphTransformer::TStatus::Async:
YQL_ENSURE(false, "Async status is forbidden for ApplyAsyncChanges");
break;
default:
YQL_ENSURE(false, "Unknown status");
}
}
AddTooManyTransformationsError(root->Pos(), "SyncTransform", ctx);
}
catch (const std::exception& e) {
ctx.AddError(ExceptionToIssue(e));
}
return IGraphTransformer::TStatus::Error;
}
IGraphTransformer::TStatus AsyncTransformStepImpl(IGraphTransformer& transformer, TExprNode::TPtr& root,
TExprContext& ctx, bool applyAsyncChanges, bool breakOnRestart,
const TStringBuf& name)
{
try {
if (applyAsyncChanges) {
TExprNode::TPtr newRoot;
auto status = transformer.ApplyAsyncChanges(root, newRoot, ctx);
if (newRoot) {
root = newRoot;
}
switch (status.Level) {
case IGraphTransformer::TStatus::Ok:
case IGraphTransformer::TStatus::Error:
break;
case IGraphTransformer::TStatus::Repeat:
if (breakOnRestart && status.HasRestart) {
return status;
}
return AsyncTransformStepImpl(transformer, root, ctx, false /* no async changes */, breakOnRestart, name);
case IGraphTransformer::TStatus::Async:
YQL_ENSURE(false, "Async status is forbidden for ApplyAsyncChanges");
break;
default:
YQL_ENSURE(false, "Unknown status");
break;
}
return status;
}
for (; ctx.RepeatTransformCounter < ctx.RepeatTransformLimit; ++ctx.RepeatTransformCounter) {
TExprNode::TPtr newRoot;
auto status = transformer.Transform(root, newRoot, ctx);
if (newRoot) {
root = newRoot;
}
switch (status.Level) {
case IGraphTransformer::TStatus::Ok:
case IGraphTransformer::TStatus::Error:
return status;
case IGraphTransformer::TStatus::Repeat:
if (breakOnRestart && status.HasRestart) {
return status;
}
// if (currentTime - startTime >= threshold) return NThreading::MakeFuture(IGraphTransformer::TStatus::Yield);
continue;
case IGraphTransformer::TStatus::Async:
break;
default:
YQL_ENSURE(false, "Unknown status");
}
break;
}
if (ctx.RepeatTransformCounter >= ctx.RepeatTransformLimit) {
AddTooManyTransformationsError(root->Pos(), name, ctx);
return IGraphTransformer::TStatus::Error;
}
}
catch (const std::exception& e) {
ctx.AddError(ExceptionToIssue(e));
return IGraphTransformer::TStatus::Error;
}
return IGraphTransformer::TStatus::Async;
}
IGraphTransformer::TStatus InstantTransform(IGraphTransformer& transformer, TExprNode::TPtr& root, TExprContext& ctx, bool breakOnRestart) {
IGraphTransformer::TStatus status = AsyncTransformStepImpl(transformer, root, ctx, false, breakOnRestart, "InstantTransform");
if (status.Level == IGraphTransformer::TStatus::Async) {
ctx.AddError(TIssue(ctx.GetPosition(root->Pos()), "Instant transform can not be delayed"));
return IGraphTransformer::TStatus::Error;
}
return status;
}
IGraphTransformer::TStatus AsyncTransformStep(IGraphTransformer& transformer, TExprNode::TPtr& root,
TExprContext& ctx, bool applyAsyncChanges)
{
return AsyncTransformStepImpl(transformer, root, ctx, applyAsyncChanges, false, "AsyncTransformStep");
}
NThreading::TFuture<IGraphTransformer::TStatus> AsyncTransform(IGraphTransformer& transformer, TExprNode::TPtr& root, TExprContext& ctx,
bool applyAsyncChanges) {
IGraphTransformer::TStatus status = AsyncTransformStepImpl(transformer, root, ctx, applyAsyncChanges, false, "AsyncTransform");
if (status.Level != IGraphTransformer::TStatus::Async) {
return NThreading::MakeFuture(status);
}
return transformer.GetAsyncFuture(*root).Apply(
[] (const NThreading::TFuture<void>&) mutable -> NThreading::TFuture<IGraphTransformer::TStatus> {
return NThreading::MakeFuture(IGraphTransformer::TStatus(IGraphTransformer::TStatus::Async));
});
}
void AsyncTransform(IGraphTransformer& transformer, TExprNode::TPtr& root, TExprContext& ctx, bool applyAsyncChanges,
std::function<void(const IGraphTransformer::TStatus&)> asyncCallback) {
NThreading::TFuture<IGraphTransformer::TStatus> status = AsyncTransform(transformer, root, ctx, applyAsyncChanges);
status.Subscribe(
[asyncCallback](const NThreading::TFuture<IGraphTransformer::TStatus>& status) mutable -> void {
HandleFutureException(status);
asyncCallback(status.GetValue());
});
}
}
template<>
void Out<NYql::IGraphTransformer::TStatus::ELevel>(class IOutputStream &o, NYql::IGraphTransformer::TStatus::ELevel x) {
#define YQL_GT_STATUS_MAP_TO_STRING_IMPL(name, ...) \
case NYql::IGraphTransformer::TStatus::name: \
o << #name; \
return;
switch (x) {
YQL_GT_STATUS_MAP(YQL_GT_STATUS_MAP_TO_STRING_IMPL)
default:
o << static_cast<int>(x);
return;
}
}
|