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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
#include "ast_builder.h"
#include "ast_nodes.h"
#include "parse_double.h"
#include <yql/essentials/core/issue/protos/issue_id.pb.h>
#include <yql/essentials/minikql/jsonpath/rewrapper/proto/serialization.pb.h>
#include <yql/essentials/ast/yql_ast_escaping.h>
#include <util/generic/singleton.h>
#include <util/system/compiler.h>
#include <util/string/cast.h>
#include <util/string/builder.h>
#include <util/charset/utf8.h>
#include <util/system/cpu_id.h>
#include <cmath>
using namespace NYql;
using namespace NYql::NJsonPath;
using namespace NJsonPathGenerated;
using namespace NReWrapper;
namespace {
constexpr ui32 RegexpLibId = NReWrapper::TSerialization::YDB_REWRAPPER_LIB_ID;
TPosition GetPos(const TToken& token) {
return TPosition(token.GetColumn(), token.GetLine());
}
bool TryStringContent(const TString& str, TString& result, TString& error, bool onlyDoubleQuoted = true) {
result.clear();
error.clear();
const bool doubleQuoted = str.StartsWith('"') && str.EndsWith('"');
const bool singleQuoted = str.StartsWith('\'') && str.EndsWith('\'');
if (!doubleQuoted && !singleQuoted) {
error = "String must be quoted";
return false;
}
if (singleQuoted && onlyDoubleQuoted) {
error = "Only double quoted strings allowed";
return false;
}
char quoteChar = doubleQuoted ? '"' : '\'';
size_t readBytes = 0;
TStringBuf atom(str);
atom.Skip(1);
TStringOutput sout(result);
result.reserve(str.size());
auto unescapeResult = UnescapeArbitraryAtom(atom, quoteChar, &sout, &readBytes);
if (unescapeResult == EUnescapeResult::OK) {
return true;
} else {
error = UnescapeResultToString(unescapeResult);
return false;
}
}
}
TAstBuilder::TAstBuilder(TIssues& issues)
: Issues(issues)
{
}
void TAstBuilder::Error(TPosition pos, const TStringBuf message) {
Issues.AddIssue(pos, message);
Issues.back().SetCode(TIssuesIds::JSONPATH_PARSE_ERROR, TSeverityIds::S_ERROR);
}
TArrayAccessNode::TSubscript TAstBuilder::BuildArraySubscript(const TRule_array_subscript& node) {
TAstNodePtr from = BuildExpr(node.GetRule_expr1());
TAstNodePtr to = nullptr;
if (node.HasBlock2()) {
to = BuildExpr(node.GetBlock2().GetRule_expr2());
}
return {from, to};
}
TAstNodePtr TAstBuilder::BuildArrayAccessor(const TRule_array_accessor& node, TAstNodePtr input) {
TVector<TArrayAccessNode::TSubscript> subscripts;
subscripts.reserve(1 + node.Block3Size());
subscripts.push_back(BuildArraySubscript(node.GetRule_array_subscript2()));
for (size_t i = 0; i < node.Block3Size(); i++) {
subscripts.push_back(BuildArraySubscript(node.GetBlock3(i).GetRule_array_subscript2()));
}
return new TArrayAccessNode(GetPos(node.GetToken1()), subscripts, input);
}
TAstNodePtr TAstBuilder::BuildWildcardArrayAccessor(const TRule_wildcard_array_accessor& node, TAstNodePtr input) {
return new TWildcardArrayAccessNode(GetPos(node.GetToken1()), input);
}
TString TAstBuilder::BuildIdentifier(const TRule_identifier& node) {
switch (node.GetAltCase()) {
case TRule_identifier::kAltIdentifier1:
return node.GetAlt_identifier1().GetToken1().GetValue();
case TRule_identifier::kAltIdentifier2:
return node.GetAlt_identifier2().GetRule_keyword1().GetToken1().GetValue();
case TRule_identifier::ALT_NOT_SET:
Y_ABORT("Alternative for 'identifier' rule is not set");
}
}
TAstNodePtr TAstBuilder::BuildMemberAccessor(const TRule_member_accessor& node, TAstNodePtr input) {
TString name;
const auto& nameBlock = node.GetBlock2();
switch (nameBlock.GetAltCase()) {
case TRule_member_accessor_TBlock2::kAlt1:
name = BuildIdentifier(nameBlock.GetAlt1().GetRule_identifier1());
break;
case TRule_member_accessor_TBlock2::kAlt2: {
const auto& token = nameBlock.GetAlt2().GetToken1();
TString error;
if (!TryStringContent(token.GetValue(), name, error, /* onlyDoubleQuoted */ false)) {
Error(GetPos(token), error);
return nullptr;
}
break;
}
case TRule_member_accessor_TBlock2::ALT_NOT_SET:
Y_ABORT("Alternative for 'member_accessor' rule is not set");
}
return new TMemberAccessNode(GetPos(node.GetToken1()), name, input);
}
TAstNodePtr TAstBuilder::BuildWildcardMemberAccessor(const TRule_wildcard_member_accessor& node, TAstNodePtr input) {
const auto& token = node.GetToken2();
return new TWildcardMemberAccessNode(GetPos(token), input);
}
TAstNodePtr TAstBuilder::BuildFilter(const TRule_filter& node, TAstNodePtr input) {
const auto predicate = BuildExpr(node.GetRule_expr3());
return new TFilterPredicateNode(GetPos(node.GetToken2()), predicate, input);
}
TAstNodePtr TAstBuilder::BuildMethod(const TRule_method& node, TAstNodePtr input) {
const auto& token = node.GetToken2();
const auto pos = GetPos(token);
const auto& value = token.GetValue();
auto type = EMethodType::Double;
if (value == "abs") {
type = EMethodType::Abs;
} else if (value == "floor") {
type = EMethodType::Floor;
} else if (value == "ceiling") {
type = EMethodType::Ceiling;
} else if (value == "type") {
type = EMethodType::Type;
} else if (value == "size") {
type = EMethodType::Size;
} else if (value == "keyvalue") {
type = EMethodType::KeyValue;
}
return new TMethodCallNode(pos, type, input);
}
TAstNodePtr TAstBuilder::BuildAccessorOp(const TRule_accessor_op& node, TAstNodePtr input) {
switch (node.GetAltCase()) {
case TRule_accessor_op::kAltAccessorOp1:
return BuildMemberAccessor(node.GetAlt_accessor_op1().GetRule_member_accessor1(), input);
case TRule_accessor_op::kAltAccessorOp2:
return BuildWildcardMemberAccessor(node.GetAlt_accessor_op2().GetRule_wildcard_member_accessor1(), input);
case TRule_accessor_op::kAltAccessorOp3:
return BuildArrayAccessor(node.GetAlt_accessor_op3().GetRule_array_accessor1(), input);
case TRule_accessor_op::kAltAccessorOp4:
return BuildWildcardArrayAccessor(node.GetAlt_accessor_op4().GetRule_wildcard_array_accessor1(), input);
case TRule_accessor_op::kAltAccessorOp5:
return BuildFilter(node.GetAlt_accessor_op5().GetRule_filter1(), input);
case TRule_accessor_op::kAltAccessorOp6:
return BuildMethod(node.GetAlt_accessor_op6().GetRule_method1(), input);
case TRule_accessor_op::ALT_NOT_SET:
Y_ABORT("Alternative for 'accessor_op' rule is not set");
}
}
TAstNodePtr TAstBuilder::BuildPrimary(const TRule_primary& node) {
switch (node.GetAltCase()) {
case TRule_primary::kAltPrimary1: {
const auto& token = node.GetAlt_primary1().GetToken1();
const auto& numberString = token.GetValue();
const double parsedValue = ParseDouble(numberString);
if (Y_UNLIKELY(std::isnan(parsedValue))) {
Y_ABORT("Invalid number was allowed by JsonPath grammar");
}
if (Y_UNLIKELY(std::isinf(parsedValue))) {
Error(GetPos(token), "Number literal is infinity");
return nullptr;
}
return new TNumberLiteralNode(GetPos(token), parsedValue);
}
case TRule_primary::kAltPrimary2: {
const auto& token = node.GetAlt_primary2().GetToken1();
return new TContextObjectNode(GetPos(token));
}
case TRule_primary::kAltPrimary3: {
const auto& token = node.GetAlt_primary3().GetToken1();
return new TLastArrayIndexNode(GetPos(token));
}
case TRule_primary::kAltPrimary4: {
const auto& primary = node.GetAlt_primary4().GetBlock1();
const auto input = BuildExpr(primary.GetRule_expr2());
if (primary.HasBlock4()) {
const auto& token = primary.GetBlock4().GetToken1();
return new TIsUnknownPredicateNode(GetPos(token), input);
}
return input;
}
case TRule_primary::kAltPrimary5: {
const auto& token = node.GetAlt_primary5().GetToken1();
return new TVariableNode(GetPos(token), token.GetValue().substr(1));
}
case TRule_primary::kAltPrimary6: {
const auto& token = node.GetAlt_primary6().GetToken1();
return new TBooleanLiteralNode(GetPos(token), true);
}
case TRule_primary::kAltPrimary7: {
const auto& token = node.GetAlt_primary7().GetToken1();
return new TBooleanLiteralNode(GetPos(token), false);
}
case TRule_primary::kAltPrimary8: {
const auto& token = node.GetAlt_primary8().GetToken1();
return new TNullLiteralNode(GetPos(token));
}
case TRule_primary::kAltPrimary9: {
const auto& token = node.GetAlt_primary9().GetToken1();
TString value;
TString error;
if (!TryStringContent(token.GetValue(), value, error)) {
Error(GetPos(token), error);
return nullptr;
}
return new TStringLiteralNode(GetPos(token), value);
}
case TRule_primary::kAltPrimary10: {
const auto& token = node.GetAlt_primary10().GetToken1();
return new TFilterObjectNode(GetPos(token));
}
case TRule_primary::ALT_NOT_SET:
Y_ABORT("Alternative for 'primary' rule is not set");
}
}
TAstNodePtr TAstBuilder::BuildAccessorExpr(const TRule_accessor_expr& node) {
TAstNodePtr input = BuildPrimary(node.GetRule_primary1());
for (size_t i = 0; i < node.Block2Size(); i++) {
input = BuildAccessorOp(node.GetBlock2(i).GetRule_accessor_op1(), input);
}
return input;
}
TAstNodePtr TAstBuilder::BuildPlainExpr(const TRule_plain_expr& node) {
return BuildAccessorExpr(node.GetRule_accessor_expr1());
}
TAstNodePtr TAstBuilder::BuildLikeRegexExpr(const TRule_like_regex_expr& node, TAstNodePtr input) {
const auto& regexToken = node.GetToken2();
TString regex;
TString error;
if (!TryStringContent(regexToken.GetValue(), regex, error)) {
Error(GetPos(regexToken), error);
return nullptr;
}
ui32 parsedFlags = 0;
if (node.HasBlock3()) {
TString flags;
const auto& flagsToken = node.GetBlock3().GetToken2();
if (!TryStringContent(flagsToken.GetValue(), flags, error)) {
Error(GetPos(flagsToken), error);
return nullptr;
}
for (char flag : flags) {
switch (flag) {
case 'i':
parsedFlags |= FLAGS_CASELESS;
break;
default:
Error(GetPos(flagsToken), TStringBuilder() << "Unsupported regex flag '" << flag << "'");
break;
}
}
}
IRePtr compiledRegex;
try {
compiledRegex = NDispatcher::Compile(regex, parsedFlags, RegexpLibId);
} catch (const NReWrapper::TCompileException& e) {
Error(GetPos(regexToken), e.AsStrBuf());
return nullptr;
}
return new TLikeRegexPredicateNode(GetPos(node.GetToken1()), input, std::move(compiledRegex));
}
TAstNodePtr TAstBuilder::BuildPredicateExpr(const TRule_predicate_expr& node) {
switch (node.GetAltCase()) {
case TRule_predicate_expr::kAltPredicateExpr1: {
const auto& predicate = node.GetAlt_predicate_expr1().GetBlock1();
const auto input = BuildPlainExpr(predicate.GetRule_plain_expr1());
if (!predicate.HasBlock2()) {
return input;
}
const auto& block = predicate.GetBlock2();
switch (block.GetAltCase()) {
case TRule_predicate_expr_TAlt1_TBlock1_TBlock2::kAlt1: {
const auto& innerBlock = block.GetAlt1().GetRule_starts_with_expr1();
const auto& prefix = BuildPlainExpr(innerBlock.GetRule_plain_expr3());
return new TStartsWithPredicateNode(GetPos(innerBlock.GetToken1()), input, prefix);
}
case TRule_predicate_expr_TAlt1_TBlock1_TBlock2::kAlt2: {
return BuildLikeRegexExpr(block.GetAlt2().GetRule_like_regex_expr1(), input);
}
case TRule_predicate_expr_TAlt1_TBlock1_TBlock2::ALT_NOT_SET:
Y_ABORT("Alternative for inner block of 'predicate_expr' rule is not set");
}
Y_UNREACHABLE();
}
case TRule_predicate_expr::kAltPredicateExpr2: {
const auto& predicate = node.GetAlt_predicate_expr2().GetBlock1();
const auto input = BuildExpr(predicate.GetRule_expr3());
return new TExistsPredicateNode(GetPos(predicate.GetToken1()), input);
}
case TRule_predicate_expr::ALT_NOT_SET:
Y_ABORT("Alternative for 'predicate' rule is not set");
}
Y_UNREACHABLE();
}
TAstNodePtr TAstBuilder::BuildUnaryExpr(const TRule_unary_expr& node) {
const auto predicateExpr = BuildPredicateExpr(node.GetRule_predicate_expr2());
if (!node.HasBlock1()) {
return predicateExpr;
}
const auto& opToken = node.GetBlock1().GetToken1();
const auto& opValue = opToken.GetValue();
auto operation = EUnaryOperation::Plus;
if (opValue == "-") {
operation = EUnaryOperation::Minus;
} else if (opValue == "!") {
operation = EUnaryOperation::Not;
}
return new TUnaryOperationNode(GetPos(opToken), operation, predicateExpr);
}
TAstNodePtr TAstBuilder::BuildMulExpr(const TRule_mul_expr& node) {
TAstNodePtr result = BuildUnaryExpr(node.GetRule_unary_expr1());
for (size_t i = 0; i < node.Block2Size(); i++) {
const auto& block = node.GetBlock2(i);
const auto& opToken = block.GetToken1();
const auto& opValue = opToken.GetValue();
auto operation = EBinaryOperation::Multiply;
if (opValue == "/") {
operation = EBinaryOperation::Divide;
} else if (opValue == "%") {
operation = EBinaryOperation::Modulo;
}
const auto rightOperand = BuildUnaryExpr(block.GetRule_unary_expr2());
result = new TBinaryOperationNode(GetPos(opToken), operation, result, rightOperand);
}
return result;
}
TAstNodePtr TAstBuilder::BuildAddExpr(const TRule_add_expr& node) {
TAstNodePtr result = BuildMulExpr(node.GetRule_mul_expr1());
for (size_t i = 0; i < node.Block2Size(); i++) {
const auto& block = node.GetBlock2(i);
const auto& opToken = block.GetToken1();
auto operation = EBinaryOperation::Add;
if (opToken.GetValue() == "-") {
operation = EBinaryOperation::Substract;
}
const auto rightOperand = BuildMulExpr(block.GetRule_mul_expr2());
result = new TBinaryOperationNode(GetPos(opToken), operation, result, rightOperand);
}
return result;
}
TAstNodePtr TAstBuilder::BuildCompareExpr(const TRule_compare_expr& node) {
TAstNodePtr result = BuildAddExpr(node.GetRule_add_expr1());
if (node.HasBlock2()) {
const auto& block = node.GetBlock2();
const auto& opToken = block.GetToken1();
const auto& opValue = opToken.GetValue();
auto operation = EBinaryOperation::Less;
if (opValue == "<=") {
operation = EBinaryOperation::LessEqual;
} else if (opValue == ">") {
operation = EBinaryOperation::Greater;
} else if (opValue == ">=") {
operation = EBinaryOperation::GreaterEqual;
}
const auto rightOperand = BuildAddExpr(block.GetRule_add_expr2());
result = new TBinaryOperationNode(GetPos(opToken), operation, result, rightOperand);
}
return result;
}
TAstNodePtr TAstBuilder::BuildEqualExpr(const TRule_equal_expr& node) {
TAstNodePtr result = BuildCompareExpr(node.GetRule_compare_expr1());
if (node.HasBlock2()) {
const auto& block = node.GetBlock2();
const auto& opToken = block.GetToken1();
const auto& opValue = opToken.GetValue();
auto operation = EBinaryOperation::Equal;
if (opValue == "<>" || opValue == "!=") {
operation = EBinaryOperation::NotEqual;
}
const auto rightOperand = BuildCompareExpr(block.GetRule_compare_expr2());
result = new TBinaryOperationNode(GetPos(opToken), operation, result, rightOperand);
}
return result;
}
TAstNodePtr TAstBuilder::BuildAndExpr(const TRule_and_expr& node) {
TAstNodePtr result = BuildEqualExpr(node.GetRule_equal_expr1());
for (size_t i = 0; i < node.Block2Size(); i++) {
const auto& block = node.GetBlock2(i);
const auto& opToken = block.GetToken1();
const auto rightOperand = BuildEqualExpr(block.GetRule_equal_expr2());
result = new TBinaryOperationNode(GetPos(opToken), EBinaryOperation::And, result, rightOperand);
}
return result;
}
TAstNodePtr TAstBuilder::BuildOrExpr(const TRule_or_expr& node) {
TAstNodePtr result = BuildAndExpr(node.GetRule_and_expr1());
for (size_t i = 0; i < node.Block2Size(); i++) {
const auto& block = node.GetBlock2(i);
const auto& opToken = block.GetToken1();
const auto rightOperand = BuildAndExpr(block.GetRule_and_expr2());
result = new TBinaryOperationNode(GetPos(opToken), EBinaryOperation::Or, result, rightOperand);
}
return result;
}
TAstNodePtr TAstBuilder::BuildExpr(const TRule_expr& node) {
return BuildOrExpr(node.GetRule_or_expr1());
}
TAstNodePtr TAstBuilder::BuildJsonPath(const TRule_jsonpath& node) {
TPosition pos;
auto mode = EJsonPathMode::Lax;
if (node.HasBlock1()) {
const auto& modeToken = node.GetBlock1().GetToken1();
pos = GetPos(modeToken);
if (modeToken.GetValue() == "strict") {
mode = EJsonPathMode::Strict;
}
}
const auto expr = BuildExpr(node.GetRule_expr2());
return new TRootNode(pos, expr, mode);
}
TAstNodePtr TAstBuilder::Build(const TJsonPathParserAST& ast) {
return BuildJsonPath(ast.GetRule_jsonpath());
}
namespace NYql::NJsonPath {
ui32 GetReLibId() {
return RegexpLibId;
}
}
|