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
|
#include "match_recognize.h"
#include "source.h"
#include "context.h"
namespace NSQLTranslationV1 {
namespace {
const auto VarDataName = "data";
const auto VarMatchedVarsName = "vars";
const auto VarLastRowIndexName = "lri";
} //namespace {
class TMatchRecognize: public TAstListNode {
public:
TMatchRecognize(
TPosition pos,
ISource* source,
const TString& inputTable,
std::pair<TPosition, TVector<TNamedFunction>>&& partitioners,
std::pair<TPosition, TVector<TSortSpecificationPtr>>&& sortSpecs,
std::pair<TPosition, TVector<TNamedFunction>>&& measures,
std::pair<TPosition, ERowsPerMatch>&& rowsPerMatch,
std::pair<TPosition, NYql::NMatchRecognize::TAfterMatchSkipTo>&& skipTo,
std::pair<TPosition, NYql::NMatchRecognize::TRowPattern>&& pattern,
std::pair<TPosition, TNodePtr>&& subset,
std::pair<TPosition, TVector<TNamedFunction>>&& definitions
): TAstListNode(pos, {BuildAtom(pos, "block")})
{
Add(BuildBlockStatements(
pos,
source,
inputTable,
std::move(partitioners),
std::move(sortSpecs),
std::move(measures),
std::move(rowsPerMatch),
std::move(skipTo),
std::move(pattern),
std::move(subset),
std::move(definitions)
));
}
private:
TMatchRecognize(const TMatchRecognize& other)
: TAstListNode(other.Pos)
{
Nodes = CloneContainer(other.Nodes);
}
TNodePtr BuildBlockStatements(
TPosition pos,
ISource* source,
const TString& inputTable,
std::pair<TPosition, TVector<TNamedFunction>>&& partitioners,
std::pair<TPosition, TVector<TSortSpecificationPtr>>&& sortSpecs,
std::pair<TPosition, TVector<TNamedFunction>>&& measures,
std::pair<TPosition, ERowsPerMatch>&& rowsPerMatch,
std::pair<TPosition, NYql::NMatchRecognize::TAfterMatchSkipTo>&& skipTo,
std::pair<TPosition, NYql::NMatchRecognize::TRowPattern>&& pattern,
std::pair<TPosition, TNodePtr>&& subset,
std::pair<TPosition, TVector<TNamedFunction>>&& definitions
) {
Y_UNUSED(pos);
auto inputRowType = Y("ListItemType",Y("TypeOf", inputTable));
auto patternNode = Pattern(pattern.first, pattern.second);
auto partitionColumns = Y();
for (const auto& p: partitioners.second){
partitionColumns->Add(BuildQuotedAtom(p.callable->GetPos(), p.name));
}
partitionColumns = Q(partitionColumns);
auto partitionKeySelector = Y();
for (const auto& p: partitioners.second){
partitionKeySelector->Add(p.callable);
}
partitionKeySelector = BuildLambda(partitioners.first, Y("row"), Q(partitionKeySelector));
auto measureNames = Y();
for (const auto& m: measures.second){
measureNames->Add(BuildQuotedAtom(m.callable->GetPos(), m.name));
}
TNodePtr measuresNode = Y("MatchRecognizeMeasures", inputRowType, patternNode, Q(measureNames));
for (const auto& m: measures.second){
measuresNode->Add(BuildLambda(m.callable->GetPos(), Y(VarDataName, VarMatchedVarsName), m.callable));
}
auto defineNames = Y();
for (const auto& d: definitions.second) {
defineNames->Add(BuildQuotedAtom(d.callable->GetPos(), d.name));
}
TNodePtr defineNode = Y("MatchRecognizeDefines", inputRowType, patternNode, Q(defineNames));
for (const auto& d: definitions.second) {
defineNode->Add(BuildLambda(d.callable->GetPos(), Y(VarDataName, VarMatchedVarsName, VarLastRowIndexName), d.callable));
}
return Q(Y(
Y("let", "input", inputTable),
Y("let", "partitionKeySelector", partitionKeySelector),
Y("let", "partitionColumns", partitionColumns),
Y("let", "sortTraits", sortSpecs.second.empty()? Y("Void") : source->BuildSortSpec(sortSpecs.second, inputTable, true, false)),
Y("let", "measures", measuresNode),
Y("let", "rowsPerMatch", BuildQuotedAtom(rowsPerMatch.first, "RowsPerMatch_" + ToString(rowsPerMatch.second))),
Y("let", "skipTo", BuildTuple(skipTo.first, {Q("AfterMatchSkip_" + ToString(skipTo.second.To)), Q(ToString(skipTo.second.Var))})),
Y("let", "pattern", patternNode),
Y("let", "subset", subset.second ? subset.second : Q("")),
Y("let", "define", defineNode),
Y("let", "res", Y("MatchRecognize",
"input",
"partitionKeySelector",
"partitionColumns",
"sortTraits",
Y("MatchRecognizeParams",
"measures",
"rowsPerMatch",
"skipTo",
"pattern",
"define"
)
)),
Y("return", "res")
));
}
TPtr PatternFactor(const TPosition& pos, const NYql::NMatchRecognize::TRowPatternFactor& factor) {
return BuildTuple(pos, {
factor.Primary.index() == 0 ?
BuildQuotedAtom(pos, std::get<0>(factor.Primary)) :
Pattern(pos, std::get<1>(factor.Primary)),
BuildQuotedAtom(pos, ToString(factor.QuantityMin)),
BuildQuotedAtom(pos, ToString(factor.QuantityMax)),
BuildQuotedAtom(pos, ToString(factor.Greedy)),
BuildQuotedAtom(pos, ToString(factor.Output)),
BuildQuotedAtom(pos, ToString(factor.Unused))
});
}
TPtr PatternTerm(const TPosition& pos, const NYql::NMatchRecognize::TRowPatternTerm& term) {
auto factors = Y();
for (const auto& f: term)
factors->Add(PatternFactor(pos, f));
return Q(std::move(factors));
}
TPtr Pattern(const TPosition& pos, const NYql::NMatchRecognize::TRowPattern& pattern) {
TNodePtr patternNode = Y("MatchRecognizePattern");
for (const auto& t: pattern) {
patternNode->Add(PatternTerm(pos, t));
}
return patternNode;
}
TPtr DoClone() const final{
return new TMatchRecognize(*this);
}
};
TNodePtr TMatchRecognizeBuilder::Build(TContext& ctx, TString&& inputTable, ISource* source){
TNodePtr node = new TMatchRecognize(
Pos,
source,
std::move(inputTable),
std::move(Partitioners),
std::move(SortSpecs),
std::move(Measures),
std::move(RowsPerMatch),
std::move(SkipTo),
std::move(Pattern),
std::move(Subset),
std::move(Definitions)
);
if (!node->Init(ctx, source))
return nullptr;
return node;
}
namespace {
const auto DefaultNavigatingFunction = "MatchRecognizeDefaultNavigating";
}
bool TMatchRecognizeVarAccessNode::DoInit(TContext& ctx, ISource* src) {
//If referenced var is the var that is currently being defined
//then it's a reference to the last row in a partition
Node = new TMatchRecognizeNavigate(ctx.Pos(), DefaultNavigatingFunction, TVector<TNodePtr>{this->Clone()});
return Node->Init(ctx, src);
}
bool TMatchRecognizeNavigate::DoInit(TContext& ctx, ISource* src) {
Y_UNUSED(src);
if (Args.size() != 1) {
ctx.Error(Pos) << "Exactly one argument is required in MATCH_RECOGNIZE navigation function";
return false;
}
const auto varColumn = dynamic_cast<TMatchRecognizeVarAccessNode *>(Args[0].Get());
if (not varColumn) {
ctx.Error(Pos) << "Row pattern navigation operations are applicable to row pattern variable only";
return false;
}
const auto varData = BuildAtom(ctx.Pos(), VarDataName);
const auto varMatchedVars = BuildAtom(ctx.Pos(), VarMatchedVarsName);
const auto varLastRowIndex = BuildAtom(ctx.Pos(), VarLastRowIndexName);
const auto matchedRanges = Y("Member", varMatchedVars, Q(varColumn->GetVar()));
TNodePtr navigatedRowIndex;
if (DefaultNavigatingFunction == Name) {
if (not varColumn->IsTheSameVar()) {
ctx.Error(Pos) << "Row pattern navigation function is required";
return false;
}
navigatedRowIndex = varLastRowIndex;
}
else if ("PREV" == Name) {
if (not varColumn->IsTheSameVar()) {
ctx.Error(Pos) << "PREV relative to matched vars is not implemented yet";
return false;
}
navigatedRowIndex = Y(
"-",
varLastRowIndex,
Y("Uint64", Q("1"))
);
} else if ("FIRST" == Name) {
navigatedRowIndex = Y(
"Member",
Y("Head", matchedRanges),
Q("From")
);
} else if ("LAST" == Name) {
navigatedRowIndex = Y(
"Member",
Y("Last", matchedRanges),
Q("To")
);
} else {
ctx.Error(Pos) << "Internal logic error";
return false;
}
Add("Member");
Add(
Y(
"Lookup",
Y("ToIndexDict", varData),
navigatedRowIndex
)
),
Add(Q(varColumn->GetColumn()));
return true;
}
} // namespace NSQLTranslationV1
|