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
|
#include "list_builtin.h"
using namespace NYql;
namespace NSQLTranslationV0 {
TAstNode* TListBuiltin::Translate(TContext& ctx) const {
Y_UNUSED(ctx);
Y_DEBUG_ABORT_UNLESS(Node);
return Node->Translate(ctx);
}
TNodePtr TListBuiltin::GetIdentityLambda() {
return BuildLambda(Pos, Y("arg"), Y(), "arg");
}
TNodePtr TListBuiltin::SkipEmpty(TNodePtr arg) {
auto sameArgLambda = BuildLambda(Pos, Y(), AstNode("item"));
auto handleNotSkippableType = BuildLambda(Pos, Y(), Y("Just", "item"));
auto checkOptional = Y("MatchType", "item", Q("Optional"),
sameArgLambda, handleNotSkippableType);
auto checkOptionalLambda = BuildLambda(Pos, Y(), checkOptional);
auto checkList = Y("MatchType", "item", Q("List"),
sameArgLambda, checkOptionalLambda);
return Y("OrderedFlatMap", arg, BuildLambda(Pos, Y("item"), checkList));
}
bool TListSortBuiltin::DoInit(TContext& ctx, ISource* src) {
if (Args.size() < 1 || Args.size() > 2) {
ctx.Error(Pos) << "List" << OpName
<< " requires one or two parameters";
return false;
}
if (!Args[0]->Init(ctx, src)) {
return false;
}
if (Args.size() == 2) {
if (!Args[1]->Init(ctx, src)) {
return false;
}
} else {
Args.push_back(GetIdentityLambda());
}
Node = Y(OpName, SkipEmpty(Args[0]), Y("Bool", Q(Asc ? "true" : "false")), Args[1]);
return true;
}
bool TListExtractBuiltin::DoInit(TContext& ctx, ISource* src) {
if (Args.size() != 2) {
ctx.Error(Pos) << "List" << OpName
<< " requires exactly two parameters";
return false;
}
if (!Args[0]->Init(ctx, src)) {
return false;
}
if (!Args[1]->Init(ctx, src)) {
return false;
}
Args[1] = MakeAtomFromExpression(ctx, Args[1]).Build();
Node = Y(OpName, SkipEmpty(Args[0]), Args[1]);
return true;
}
bool TListProcessBuiltin::CheckArgs(TContext& ctx, ISource* src) {
if (Args.size() < 2 ) {
ctx.Error(Pos) << "List" << OpName
<< " requires at least two parameters";
return false;
}
for (const auto& arg : Args) {
if (!arg->Init(ctx, src)) {
return false;
}
}
OpLiteral = Args[1]->GetLiteral("String");
return true;
}
TNodePtr TListProcessBuiltin::PrepareResult() {
TNodePtr result;
if (OpLiteral) {
size_t modulePos = OpLiteral->find("::");
if (modulePos != TString::npos) {
const TString& module = OpLiteral->substr(0, modulePos);
const TString& function = OpLiteral->substr(modulePos + 2);
auto udf = Y("Udf", Q(module + "." + function));
result = Y("Apply", udf, "item");
} else {
result = Y(*OpLiteral, "item");
}
} else {
result = Y("Apply", Args[1], "item");
}
for (size_t i = 0; i < Args.size(); ++i) {
if (i > 1) {
result->Add(Args[i]);
}
}
return result;
}
bool TListMapBuiltin::DoInit(TContext& ctx, ISource* src) {
if (!CheckArgs(ctx, src)) {
return false;
};
auto prepare = PrepareResult();
auto just = BuildLambda(Pos, Y(), Y("Just", prepare));
auto sameArgLambda = BuildLambda(Pos, Y(), prepare);
auto match = Y("MatchType", prepare, Q("Data"), just, sameArgLambda);
auto lambda = Flat ? BuildLambda(Pos, Y("item"), match) : GetMapLambda();
Node = Y(OpName,
Flat ? SkipEmpty(Args[0]) : Args[0],
lambda
);
return true;
}
TNodePtr TListMapBuiltin::GetMapLambda() {
return BuildLambda(Pos, Y("item"), PrepareResult());
}
bool TListFilterBuiltin::DoInit(TContext& ctx, ISource* src) {
if (!CheckArgs(ctx, src)) {
return false;
};
Node = Y("OrderedFlatMap",
SkipEmpty(Args[0]),
GetFilterLambda()
);
return true;
}
TNodePtr TListFilterBuiltin::GetFilterLambda() {
return BuildLambda(Pos, Y("item"), Y("OptionalIf", Y("Coalesce", PrepareResult(), Y("Bool", Q("false"))), "item"));
}
bool TListFoldBuiltin::DoInit(TContext& ctx, ISource* src) {
if (Args.size() != ArgCount) {
ctx.Error(Pos) << "Folding list with " << OpName << "requires exactly " << ArgCount << " parameter";
return false;
}
for (const auto& arg : Args) {
if (!arg->Init(ctx, src)) {
return false;
}
}
Node = Y("Fold",
SkipEmpty(Args[0]),
GetInitialState(),
GetUpdateLambda()
);
return true;
}
TNodePtr TListFoldBuiltin::GetInitialState() {
return Y(StateType, Q(StateValue));
}
TNodePtr TListFoldBuiltin::GetUpdateLambda() {
return BuildLambda(Pos, Y("item", "state"), Y(OpName, "item", "state"));
}
TNodePtr TListCountBuiltin::GetUpdateLambda() {
return BuildLambda(Pos, Y("item", "state"), Y(OpName, "state"));
}
bool TListAvgBuiltin::DoInit(TContext& ctx, ISource* src) {
if (TListFoldBuiltin::DoInit(ctx, src)) {
auto foldResult = Node;
Node = Y("Div", Y("Nth", foldResult, Q("1")), Y("Nth", foldResult, Q("0")));
return true;
} else {
return false;
}
}
TNodePtr TListAvgBuiltin::GetInitialState() {
return Q(Y(Y("Uint64", Q("0")), Y("Double", Q("0"))));
}
TNodePtr TListAvgBuiltin::GetUpdateLambda() {
auto count = Y("Inc", Y("Nth", "state", Q("0")));
auto sum = Y("Add", "item", Y("Nth", "state", Q("1")));
return BuildLambda(Pos, Y("item", "state"), Q(Y(count, sum)));
}
TNodePtr TListHasBuiltin::GetUpdateLambda() {
return BuildLambda(Pos, Y("item", "state"), Y("Or", "state",
Y("Coalesce", Y(OpName, "item", Args[1]), Y("Bool", Q("false")))));
}
bool TListFold1Builtin::DoInit(TContext& ctx, ISource* src) {
if (Args.size() != 1) {
ctx.Error(Pos) << "Folding list with " << OpName << " requires only one parameter";
return false;
}
if (!Args[0]->Init(ctx, src)) {
return false;
}
Node = Y("Fold1",
SkipEmpty(Args[0]),
GetInitLambda(),
GetUpdateLambda()
);
return true;
}
TNodePtr TListFold1Builtin::GetInitLambda() {
return GetIdentityLambda();
}
TNodePtr TListFold1Builtin::GetUpdateLambda() {
return BuildLambda(Pos, Y("item", "state"), Y(OpName, "state", "item"));
}
bool TListUniqBuiltin::DoInit(TContext& ctx, ISource* src) {
if (Args.size() != 1) {
ctx.Error(Pos) << OpName << " requires only one parameter";
return false;
}
if (!Args[0]->Init(ctx, src)) {
return false;
}
Node = Y("DictKeys",
Y("ToDict", Args[0], GetIdentityLambda(), BuildLambda(Pos, Y("item"), Y("Void")), Q(Y(Q("Hashed"), Q("One"))))
);
return true;
}
bool TListCreateBuiltin::DoInit(TContext& ctx, ISource* src) {
if (Args.size() != 1) {
ctx.Error(Pos) << OpName << " requires only one parameter";
return false;
}
if (!Args[0]->Init(ctx, src)) {
return false;
}
auto literal = Args[0]->GetLiteral("String");
if (literal) {
Node = Y("List",
Y("ListType",
Y("ParseType", Q(*literal))));
} else {
Node = Y("List",
Y("ListType", Args[0]));
}
return true;
}
bool TDictCreateBuiltin::DoInit(TContext& ctx, ISource* src) {
if (Args.size() != 2) {
ctx.Error(Pos) << OpName << " requires two parameters";
return false;
}
TNodePtr types[2];
for (ui32 i = 0; i < 2; ++i) {
if (!Args[i]->Init(ctx, src)) {
return false;
}
auto literal = Args[i]->GetLiteral("String");
if (literal) {
types[i] = Y("ParseType", Q(*literal));
} else {
types[i] = Args[i];
}
}
Node = Y("Dict",
Y("DictType", types[0], types[1]));
return true;
}
} // namespace NSQLTranslationV0
|