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
|
#include "match_recognize.h"
#include "source.h"
#include "context.h"
#include <util/generic/overloaded.h>
namespace NSQLTranslationV1 {
namespace {
constexpr auto VarDataName = "data";
constexpr auto VarMatchedVarsName = "vars";
constexpr auto VarLastRowIndexName = "lri";
class TMatchRecognizeColumnAccessNode final : public TAstListNode {
public:
TMatchRecognizeColumnAccessNode(TPosition pos, TString var, TString column)
: TAstListNode(pos)
, Var_(std::move(var))
, Column_(std::move(column)) {
}
const TString* GetColumnName() const override {
return std::addressof(Column_);
}
bool DoInit(TContext& ctx, ISource* /* src */) override {
switch (ctx.GetColumnReferenceState()) {
case EColumnRefState::MatchRecognizeMeasures:
if (!ctx.SetMatchRecognizeAggrVar(Var_)) {
return false;
}
Add(
"Member",
BuildAtom(Pos_, "row"),
Q(Column_)
);
break;
case EColumnRefState::MatchRecognizeDefine:
if (ctx.GetMatchRecognizeDefineVar() != Var_) {
ctx.Error() << "Row pattern navigation function is required";
return false;
}
BuildLookup(VarLastRowIndexName);
break;
case EColumnRefState::MatchRecognizeDefineAggregate:
if (!ctx.SetMatchRecognizeAggrVar(Var_)) {
return false;
}
BuildLookup("index");
break;
default:
ctx.Error(Pos_) << "Unexpected column reference state";
return false;
}
return true;
}
TNodePtr DoClone() const override {
return MakeIntrusive<TMatchRecognizeColumnAccessNode>(Pos_, Var_, Column_);
}
private:
void BuildLookup(TString varKeyName) {
Add(
"Member",
Y(
"Lookup",
Y(
"ToIndexDict",
BuildAtom(Pos_, VarDataName)
),
BuildAtom(Pos_, std::move(varKeyName))
),
Q(Column_)
);
}
private:
TString Var_;
TString Column_;
};
class TMatchRecognizeDefineAggregate final : public TAstListNode {
public:
TMatchRecognizeDefineAggregate(TPosition pos, TString name, TVector<TNodePtr> args)
: TAstListNode(pos)
, Name_(std::move(name))
, Args_(std::move(args)) {
}
bool DoInit(TContext& ctx, ISource* src) override {
if (EColumnRefState::MatchRecognizeDefine != ctx.GetColumnReferenceState()) {
ctx.Error(Pos_) << "Unexpected column reference state";
return false;
}
TColumnRefScope scope(ctx, EColumnRefState::MatchRecognizeDefineAggregate, false, ctx.GetMatchRecognizeDefineVar());
if (Args_.size() != 1) {
ctx.Error() << "Exactly one argument is required in MATCH_RECOGNIZE navigation function";
return false;
}
const auto arg = Args_[0];
if (!arg || !arg->Init(ctx, src)) {
return false;
}
const auto body = [&]() -> TNodePtr {
if ("first" == Name_) {
return Y("Member", Y("Head", "item"), Q("From"));
} else if ("last" == Name_) {
return Y("Member", Y("Last", "item"), Q("To"));
} else {
ctx.Error() << "Unknown row pattern navigation function: " << Name_;
return {};
}
}();
if (!body) {
return false;
}
Add("Apply", BuildLambda(Pos_, Y("index"), arg), body);
return true;
}
TNodePtr DoClone() const override {
return MakeIntrusive<TMatchRecognizeDefineAggregate>(Pos_, Name_, Args_);
}
private:
TString Name_;
TVector<TNodePtr> Args_;
};
class TMatchRecognizeVarAccessNode final : public INode {
public:
TMatchRecognizeVarAccessNode(TPosition pos, TNodePtr aggr)
: INode(pos)
, Aggr_(std::move(aggr)) {
}
bool DoInit(TContext& ctx, ISource* src) override {
if (!Aggr_ || !Aggr_->Init(ctx, src)) {
return false;
}
auto var = ctx.ExtractMatchRecognizeAggrVar();
Expr_ = [&]() -> TNodePtr {
switch (ctx.GetColumnReferenceState()) {
case EColumnRefState::MatchRecognizeMeasures: {
ctx.GetMatchRecognizeAggregations().emplace_back(std::move(var), Aggr_->GetAggregation());
return Aggr_;
}
case EColumnRefState::MatchRecognizeDefine:
return Y(
"Apply",
BuildLambda(Pos_, Y("item"), Aggr_),
Y(
"Member",
BuildAtom(ctx.Pos(), VarMatchedVarsName),
Q(std::move(var))
)
);
default:
ctx.Error(Pos_) << "Unexpected column reference state";
return {};
}
}();
return Expr_ && Expr_->Init(ctx, src);
}
TNodePtr DoClone() const override {
return MakeIntrusive<TMatchRecognizeVarAccessNode>(Pos_, Aggr_);
}
TAstNode* Translate(TContext& ctx) const override {
return Expr_->Translate(ctx);
}
private:
TNodePtr Aggr_;
TNodePtr Expr_;
};
class TMatchRecognize final : public TAstListNode {
public:
TMatchRecognize(
TPosition pos,
TString label,
TNodePtr partitionKeySelector,
TNodePtr partitionColumns,
TVector<TSortSpecificationPtr> sortSpecs,
TVector<TNamedFunction> measures,
TNodePtr rowsPerMatch,
TNodePtr skipTo,
TNodePtr pattern,
TNodePtr patternVars,
TNodePtr subset,
TVector<TNamedFunction> definitions)
: TAstListNode(pos)
, Label_(std::move(label))
, PartitionKeySelector_(std::move(partitionKeySelector))
, PartitionColumns_(std::move(partitionColumns))
, SortSpecs_(std::move(sortSpecs))
, Measures_(std::move(measures))
, RowsPerMatch_(std::move(rowsPerMatch))
, SkipTo_(std::move(skipTo))
, Pattern_(std::move(pattern))
, PatternVars_(std::move(patternVars))
, Subset_(std::move(subset))
, Definitions_(std::move(definitions)) {
}
private:
bool DoInit(TContext& ctx, ISource* src) override {
auto inputRowType = Y("ListItemType", Y("TypeOf", Label_));
if (!PartitionKeySelector_ || !PartitionKeySelector_->Init(ctx, src)) {
return false;
}
if (!PartitionColumns_ || !PartitionColumns_->Init(ctx, src)) {
return false;
}
const auto sortTraits = SortSpecs_.empty() ? Y("Void") : src->BuildSortSpec(SortSpecs_, Label_, true, false);
if (!sortTraits || !sortTraits->Init(ctx, src)) {
return false;
}
auto measureNames = Y();
auto measuresCallables = Y();
for (auto& m: Measures_) {
TColumnRefScope scope(ctx, EColumnRefState::MatchRecognizeMeasures);
if (!m.Callable || !m.Callable->Init(ctx, src)) {
return false;
}
const auto pos = m.Callable->GetPos();
measureNames = L(measureNames, BuildQuotedAtom(m.Callable->GetPos(), std::move(m.Name)));
auto measuresVars = Y();
auto measuresAggregates = Y();
for (auto& [var, aggr] : ctx.GetMatchRecognizeAggregations()) {
if (!aggr) {
return false;
}
auto [traits, result] = aggr->AggregationTraits(Y("TypeOf", Label_), false, false, false, ctx);
if (!result) {
return false;
}
measuresVars = L(measuresVars, BuildQuotedAtom(pos, std::move(var)));
measuresAggregates = L(measuresAggregates, std::move(traits));
}
ctx.GetMatchRecognizeAggregations().clear();
measuresCallables = L(
measuresCallables,
Y(
"MatchRecognizeMeasuresCallable",
BuildLambda(pos, Y("row"), std::move(m.Callable)),
Q(measuresVars),
Q(measuresAggregates)
)
);
}
auto measuresNode = Y("MatchRecognizeMeasuresCallables", inputRowType, Q(PatternVars_), Q(measureNames), Q(measuresCallables));
if (!RowsPerMatch_ || !RowsPerMatch_->Init(ctx, src)) {
return false;
}
if (!SkipTo_ || !SkipTo_->Init(ctx, src)) {
return false;
}
if (!Pattern_ || !Pattern_->Init(ctx, src)) {
return false;
}
if (!PatternVars_ || !PatternVars_->Init(ctx, src)) {
return false;
}
auto defineNames = Y();
for (auto& d: Definitions_) {
defineNames = L(defineNames, BuildQuotedAtom(d.Callable->GetPos(), d.Name));
}
auto defineNode = Y("MatchRecognizeDefines", inputRowType, Q(PatternVars_), Q(defineNames));
for (auto& d: Definitions_) {
TColumnRefScope scope(ctx, EColumnRefState::MatchRecognizeDefine, true, d.Name);
if (!d.Callable || !d.Callable->Init(ctx, src)) {
return false;
}
const auto pos = d.Callable->GetPos();
defineNode = L(defineNode, BuildLambda(pos, Y(VarDataName, VarMatchedVarsName, VarLastRowIndexName), std::move(d.Callable)));
}
Add(
"block",
Q(Y(
Y("let", "input", Label_),
Y("let", "partitionKeySelector", PartitionKeySelector_),
Y("let", "partitionColumns", PartitionColumns_),
Y("let", "sortTraits", sortTraits),
Y("let", "measures", measuresNode),
Y("let", "rowsPerMatch", RowsPerMatch_),
Y("let", "skipTo", SkipTo_),
Y("let", "pattern", Pattern_),
Y("let", "subset", Subset_ ? Subset_ : Q("")),
Y("let", "define", defineNode),
Y("let", "res", Y("MatchRecognize",
"input",
"partitionKeySelector",
"partitionColumns",
"sortTraits",
Y("MatchRecognizeParams",
"measures",
"rowsPerMatch",
"skipTo",
"pattern",
"define"
)
)),
Y("return", "res")
))
);
return true;
}
TNodePtr DoClone() const override {
return MakeIntrusive<TMatchRecognize>(
Pos_,
Label_,
PartitionKeySelector_,
PartitionColumns_,
SortSpecs_,
Measures_,
RowsPerMatch_,
SkipTo_,
Pattern_,
PatternVars_,
Subset_,
Definitions_
);
}
private:
TString Label_;
TNodePtr PartitionKeySelector_;
TNodePtr PartitionColumns_;
TVector<TSortSpecificationPtr> SortSpecs_;
TVector<TNamedFunction> Measures_;
TNodePtr RowsPerMatch_;
TNodePtr SkipTo_;
TNodePtr Pattern_;
TNodePtr PatternVars_;
TNodePtr Subset_;
TVector<TNamedFunction> Definitions_;
};
} // anonymous namespace
TNodePtr TMatchRecognizeBuilder::Build(TContext& ctx, TString label, ISource* src) {
const auto node = MakeIntrusive<TMatchRecognize>(
Pos_,
std::move(label),
std::move(PartitionKeySelector_),
std::move(PartitionColumns_),
std::move(SortSpecs_),
std::move(Measures_),
std::move(RowsPerMatch_),
std::move(SkipTo_),
std::move(Pattern_),
std::move(PatternVars_),
std::move(Subset_),
std::move(Definitions_)
);
if (!node->Init(ctx, src)) {
return {};
}
return node;
}
TNodePtr BuildMatchRecognizeColumnAccess(TPosition pos, TString var, TString column) {
return MakeIntrusive<TMatchRecognizeColumnAccessNode>(pos, std::move(var), std::move(column));
}
TNodePtr BuildMatchRecognizeDefineAggregate(TPosition pos, TString name, TVector<TNodePtr> args) {
const auto result = MakeIntrusive<TMatchRecognizeDefineAggregate>(pos, std::move(name), std::move(args));
return BuildMatchRecognizeVarAccess(pos, std::move(result));
}
TNodePtr BuildMatchRecognizeVarAccess(TPosition pos, TNodePtr aggr) {
return MakeIntrusive<TMatchRecognizeVarAccessNode>(pos, std::move(aggr));
}
} // namespace NSQLTranslationV1
|