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
|
#include "kqp_yql.h"
#include <ydb/library/yql/core/yql_expr_type_annotation.h>
namespace NYql {
using namespace NKikimr;
using namespace NKikimr::NKqp;
using namespace NNodes;
static EPhysicalQueryType GetPhysicalQueryType(const TStringBuf& value) {
if (value == "data_query") {
return EPhysicalQueryType::Data;
} else if (value == "scan_query") {
return EPhysicalQueryType::Scan;
} else {
YQL_ENSURE(false, "Unknown physical query type: " << value);
}
}
static TStringBuf PhysicalQueryTypeToString(EPhysicalQueryType type) {
switch (type) {
case EPhysicalQueryType::Unspecified:
break;
case EPhysicalQueryType::Data:
return "data_query";
case EPhysicalQueryType::Scan:
return "scan_query";
}
YQL_ENSURE(false, "Unexpected physical query type: " << type);
}
TKqpPhyQuerySettings TKqpPhyQuerySettings::Parse(const TKqpPhysicalQuery& node) {
TKqpPhyQuerySettings settings;
for (const auto& tuple : node.Settings()) {
auto name = tuple.Name().Value();
if (name == TypeSettingName) {
YQL_ENSURE(tuple.Value().Maybe<TCoAtom>());
settings.Type = GetPhysicalQueryType(tuple.Value().Cast<TCoAtom>().Value());
}
}
return settings;
}
NNodes::TCoNameValueTupleList TKqpPhyQuerySettings::BuildNode(TExprContext& ctx, TPositionHandle pos) const {
TVector<TCoNameValueTuple> settings;
if (Type) {
settings.push_back(Build<TCoNameValueTuple>(ctx, pos)
.Name().Build(TypeSettingName)
.Value<TCoAtom>().Build(PhysicalQueryTypeToString(*Type))
.Done());
}
return Build<TCoNameValueTupleList>(ctx, pos)
.Add(settings)
.Done();
}
static EPhysicalTxType GetPhysicalTxType(const TStringBuf& value) {
if (value == "compute") {
return EPhysicalTxType::Compute;
} else if (value == "data") {
return EPhysicalTxType::Data;
} else if (value == "scan") {
return EPhysicalTxType::Scan;
} else {
YQL_ENSURE(false, "Unknown physical tx type: " << value);
}
}
static TStringBuf PhysicalTxTypeToString(EPhysicalTxType type) {
switch (type) {
case EPhysicalTxType::Unspecified:
break;
case EPhysicalTxType::Compute:
return "compute";
case EPhysicalTxType::Data:
return "data";
case EPhysicalTxType::Scan:
return "scan";
}
YQL_ENSURE(false, "Unexpected physical tx type: " << type);
}
TKqpPhyTxSettings TKqpPhyTxSettings::Parse(const TKqpPhysicalTx& node) {
TKqpPhyTxSettings settings;
for (const auto& tuple : node.Settings()) {
auto name = tuple.Name().Value();
if (name == TypeSettingName) {
YQL_ENSURE(tuple.Value().Maybe<TCoAtom>());
settings.Type = GetPhysicalTxType(tuple.Value().Cast<TCoAtom>().Value());
} else if (name == WithEffectsSettingName) {
settings.WithEffects = true;
}
}
return settings;
}
NNodes::TCoNameValueTupleList TKqpPhyTxSettings::BuildNode(TExprContext& ctx, TPositionHandle pos) const {
TVector<TCoNameValueTuple> settings;
settings.reserve(2);
if (Type) {
settings.emplace_back(Build<TCoNameValueTuple>(ctx, pos)
.Name().Build(TypeSettingName)
.Value<TCoAtom>().Build(PhysicalTxTypeToString(*Type))
.Done());
}
if (WithEffects) {
settings.emplace_back(Build<TCoNameValueTuple>(ctx, pos)
.Name().Build(WithEffectsSettingName)
.Done());
}
return Build<TCoNameValueTupleList>(ctx, pos)
.Add(settings)
.Done();
}
namespace {
template <typename TKqlReadOperation>
TKqpReadTableSettings ParseInternal(const TKqlReadOperation& node) {
TKqpReadTableSettings settings;
for (const auto& tuple : node.Settings()) {
TStringBuf name = tuple.Name().Value();
if (name == TKqpReadTableSettings::SkipNullKeysSettingName) {
YQL_ENSURE(tuple.Value().template Maybe<TCoAtomList>());
for (const auto& key : tuple.Value().template Cast<TCoAtomList>()) {
settings.SkipNullKeys.emplace_back(TString(key.Value()));
}
} else if (name == TKqpReadTableSettings::ItemsLimitSettingName) {
YQL_ENSURE(tuple.Value().IsValid());
settings.ItemsLimit = tuple.Value().Cast().Ptr();
} else if (name == TKqpReadTableSettings::ReverseSettingName) {
YQL_ENSURE(tuple.Ref().ChildrenSize() == 1);
settings.Reverse = true;
} else {
YQL_ENSURE(false, "Unknown KqpReadTable setting name '" << name << "'");
}
}
return settings;
}
} // anonymous namespace end
TKqpReadTableSettings TKqpReadTableSettings::Parse(const TKqlReadTableBase& node) {
return ParseInternal(node);
}
TKqpReadTableSettings TKqpReadTableSettings::Parse(const TKqlReadTableRangesBase& node) {
return ParseInternal(node);
}
NNodes::TCoNameValueTupleList TKqpReadTableSettings::BuildNode(TExprContext& ctx, TPositionHandle pos) const {
TVector<TCoNameValueTuple> settings;
settings.reserve(3);
if (!SkipNullKeys.empty()) {
TVector<TExprNodePtr> keys;
keys.reserve(SkipNullKeys.size());
for (auto& key: SkipNullKeys) {
keys.emplace_back(ctx.NewAtom(pos, key));
}
settings.emplace_back(
Build<TCoNameValueTuple>(ctx, pos)
.Name()
.Build(SkipNullKeysSettingName)
.Value<TCoAtomList>()
.Add(keys)
.Build()
.Done());
}
if (ItemsLimit) {
settings.emplace_back(
Build<TCoNameValueTuple>(ctx, pos)
.Name()
.Build(ItemsLimitSettingName)
.Value(ItemsLimit)
.Done());
}
if (Reverse) {
settings.emplace_back(
Build<TCoNameValueTuple>(ctx, pos)
.Name()
.Build(ReverseSettingName)
.Done());
}
return Build<TCoNameValueTupleList>(ctx, pos)
.Add(settings)
.Done();
}
void TKqpReadTableSettings::AddSkipNullKey(const TString& key) {
for (auto& k : SkipNullKeys) {
if (k == key) {
return;
}
}
SkipNullKeys.emplace_back(key);
}
TKqpUpsertRowsSettings TKqpUpsertRowsSettings::Parse(const TKqpUpsertRows& node) {
TKqpUpsertRowsSettings settings;
for (const auto& tuple : node.Settings()) {
TStringBuf name = tuple.Name().Value();
if (name == TKqpUpsertRowsSettings::InplaceSettingName) {
YQL_ENSURE(tuple.Ref().ChildrenSize() == 1);
settings.Inplace = true;
} else {
YQL_ENSURE(false, "Unknown KqpUpsertRows setting name '" << name << "'");
}
}
return settings;
}
NNodes::TCoNameValueTupleList TKqpUpsertRowsSettings::BuildNode(TExprContext& ctx, TPositionHandle pos) const {
TVector<TCoNameValueTuple> settings;
settings.reserve(1);
if (Inplace) {
settings.emplace_back(
Build<TCoNameValueTuple>(ctx, pos)
.Name().Build(InplaceSettingName)
.Done());
}
return Build<TCoNameValueTupleList>(ctx, pos)
.Add(settings)
.Done();
}
TCoNameValueTupleList TKqpReadTableExplainPrompt::BuildNode(TExprContext& ctx, TPositionHandle pos) const {
TVector<TCoNameValueTuple> prompt;
prompt.reserve(2);
TVector<TExprNodePtr> keys;
keys.reserve(UsedKeyColumns.size());
for (auto& key: UsedKeyColumns) {
keys.emplace_back(ctx.NewAtom(pos, key));
}
prompt.emplace_back(
Build<TCoNameValueTuple>(ctx, pos)
.Name()
.Build(UsedKeyColumnsName)
.Value<TCoAtomList>()
.Add(keys)
.Build()
.Done()
);
if (!ExpectedMaxRanges.empty()) {
prompt.emplace_back(
Build<TCoNameValueTuple>(ctx, pos)
.Name()
.Build(ExpectedMaxRangesName)
.Value<TCoAtom>()
.Build(ExpectedMaxRanges)
.Done()
);
}
return Build<TCoNameValueTupleList>(ctx, pos)
.Add(prompt)
.Done();
}
TKqpReadTableExplainPrompt TKqpReadTableExplainPrompt::Parse(const NNodes::TKqlReadTableRangesBase& node) {
TKqpReadTableExplainPrompt prompt;
for (const auto& tuple : node.ExplainPrompt()) {
TStringBuf name = tuple.Name().Value();
if (name == TKqpReadTableExplainPrompt::UsedKeyColumnsName) {
for (const auto& key : tuple.Value().template Cast<TCoAtomList>()) {
prompt.UsedKeyColumns.emplace_back(TString(key.Value()));
}
continue;
}
if (name == TKqpReadTableExplainPrompt::ExpectedMaxRangesName) {
prompt.ExpectedMaxRanges = TString(tuple.Value().template Cast<TCoAtom>());
continue;
}
YQL_ENSURE(false, "Unknown KqpReadTableRanges explain prompt name '" << name << "'");
}
return prompt;
}
TString KqpExprToPrettyString(const TExprNode& expr, TExprContext& ctx) {
try {
TConvertToAstSettings settings;
settings.NoInlineFunc = [] (const TExprNode& exprNode) {
TExprBase node(&exprNode);
if (node.Maybe<TDqStageBase>()) {
return true;
}
if (node.Maybe<TDqConnection>()) {
return true;
}
if (node.Maybe<TKqlReadTableBase>()) {
return true;
}
if (node.Maybe<TKqlReadTableRangesBase>()) {
return true;
}
return false;
};
auto ast = ConvertToAst(expr, ctx, settings);
TStringStream exprStream;
YQL_ENSURE(ast.Root);
ast.Root->PrettyPrintTo(exprStream, NYql::TAstPrintFlags::PerLine | NYql::TAstPrintFlags::ShortQuote);
TString exprText = exprStream.Str();
return exprText;
} catch (const std::exception& e) {
return TStringBuilder() << "Failed to render expression to pretty string: " << e.what();
}
}
TString KqpExprToPrettyString(const TExprBase& expr, TExprContext& ctx) {
return KqpExprToPrettyString(expr.Ref(), ctx);
}
TString PrintKqpStageOnly(const TDqStageBase& stage, TExprContext& ctx) {
if (stage.Inputs().Empty()) {
return KqpExprToPrettyString(stage, ctx);
}
TNodeOnNodeOwnedMap replaces;
for (ui64 i = 0; i < stage.Inputs().Size(); ++i) {
auto input = stage.Inputs().Item(i);
auto param = Build<TCoParameter>(ctx, input.Pos())
.Name().Build(TStringBuilder() << "stage_input_" << i)
.Type(ExpandType(input.Pos(), *input.Ref().GetTypeAnn(), ctx))
.Done();
replaces[input.Raw()] = param.Ptr();
}
auto newStage = ctx.ReplaceNodes(stage.Ptr(), replaces);
return KqpExprToPrettyString(TExprBase(newStage), ctx);
}
} // namespace NYql
|