aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/jsonpath/binary.cpp
blob: 8d75a6d3b90e05c32bf1b7335264c4fc9fdbb5a0 (plain) (blame)
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include "binary.h"

#include <yql/essentials/utils/yql_panic.h>

namespace NYql::NJsonPath {

bool TArraySubscriptOffsets::IsRange() const {
    return ToOffset > 0;
}

const TStringBuf TJsonPathItem::GetString() const {
    return std::get<TStringBuf>(Data);
}

const TVector<TArraySubscriptOffsets>& TJsonPathItem::GetSubscripts() const {
    return std::get<TVector<TArraySubscriptOffsets>>(Data);
}

const TBinaryOpArgumentsOffset& TJsonPathItem::GetBinaryOpArguments() const {
    return std::get<TBinaryOpArgumentsOffset>(Data);
}

double TJsonPathItem::GetNumber() const {
    return std::get<double>(Data);
}

bool TJsonPathItem::GetBoolean() const {
    return std::get<bool>(Data);
}

TFilterPredicateOffset TJsonPathItem::GetFilterPredicateOffset() const {
    return std::get<TFilterPredicateOffset>(Data);
}

TStartsWithPrefixOffset TJsonPathItem::GetStartsWithPrefixOffset() const {
    return std::get<TStartsWithPrefixOffset>(Data);
}

const NReWrapper::IRePtr& TJsonPathItem::GetRegex() const {
    return std::get<NReWrapper::IRePtr>(Data);
}

TJsonPathReader::TJsonPathReader(const TJsonPathPtr path)
    : Path(path)
    , InitialPos(0)
    , Mode(ReadMode(InitialPos))
{
}

const TJsonPathItem& TJsonPathReader::ReadFirst() {
    return ReadFromPos(InitialPos);
}

const TJsonPathItem& TJsonPathReader::ReadInput(const TJsonPathItem& item) {
    YQL_ENSURE(item.InputItemOffset.Defined());
    return ReadFromPos(*item.InputItemOffset);
}

const TJsonPathItem& TJsonPathReader::ReadFromSubscript(const TArraySubscriptOffsets& subscript) {
    return ReadFromPos(subscript.FromOffset);
}

const TJsonPathItem& TJsonPathReader::ReadToSubscript(const TArraySubscriptOffsets& subscript) {
    YQL_ENSURE(subscript.IsRange());
    return ReadFromPos(subscript.ToOffset);
}

const TJsonPathItem& TJsonPathReader::ReadLeftOperand(const TJsonPathItem& node) {
    return ReadFromPos(node.GetBinaryOpArguments().LeftOffset);
}

const TJsonPathItem& TJsonPathReader::ReadRightOperand(const TJsonPathItem& node) {
    return ReadFromPos(node.GetBinaryOpArguments().RightOffset);
}

const TJsonPathItem& TJsonPathReader::ReadFilterPredicate(const TJsonPathItem& node) {
    return ReadFromPos(node.GetFilterPredicateOffset().Offset);
}

const TJsonPathItem& TJsonPathReader::ReadPrefix(const TJsonPathItem& node) {
    return ReadFromPos(node.GetStartsWithPrefixOffset().Offset);
}

EJsonPathMode TJsonPathReader::GetMode() const {
    return Mode;
}

const TJsonPathItem& TJsonPathReader::ReadFromPos(TUint pos) {
    YQL_ENSURE(pos < Path->Size());

    const auto it = ItemCache.find(pos);
    if (it != ItemCache.end()) {
        return it->second;
    }

    TJsonPathItem& result = ItemCache[pos];
    result.Type = ReadType(pos);

    const auto row = ReadUint(pos);
    const auto column = ReadUint(pos);
    result.Pos = TPosition(column, row, "jsonpath");

    switch (result.Type) {
        // Items without input
        case EJsonPathItemType::FilterObject:
        case EJsonPathItemType::NullLiteral:
        case EJsonPathItemType::ContextObject:
        case EJsonPathItemType::LastArrayIndex:
            break;

        case EJsonPathItemType::Variable:
        case EJsonPathItemType::StringLiteral:
            result.Data = ReadString(pos);
            break;

        case EJsonPathItemType::NumberLiteral:
            result.Data = ReadDouble(pos);
            break;

        case EJsonPathItemType::BooleanLiteral:
            result.Data = ReadBool(pos);
            break;

        // Items with single input
        case EJsonPathItemType::TypeMethod:
        case EJsonPathItemType::SizeMethod:
        case EJsonPathItemType::KeyValueMethod:
        case EJsonPathItemType::AbsMethod:
        case EJsonPathItemType::FloorMethod:
        case EJsonPathItemType::CeilingMethod:
        case EJsonPathItemType::DoubleMethod:
        case EJsonPathItemType::WildcardArrayAccess:
        case EJsonPathItemType::WildcardMemberAccess:
        case EJsonPathItemType::UnaryMinus:
        case EJsonPathItemType::UnaryPlus:
        case EJsonPathItemType::UnaryNot:
        case EJsonPathItemType::IsUnknownPredicate:
        case EJsonPathItemType::ExistsPredicate:
            result.InputItemOffset = ReadUint(pos);
            break;

        case EJsonPathItemType::MemberAccess:
            result.Data = ReadString(pos);
            result.InputItemOffset = ReadUint(pos);
            break;

        case EJsonPathItemType::ArrayAccess:
            result.Data = ReadSubscripts(pos);
            result.InputItemOffset = ReadUint(pos);
            break;

        case EJsonPathItemType::FilterPredicate:
            result.Data = TFilterPredicateOffset{ReadUint(pos)};
            result.InputItemOffset = ReadUint(pos);
            break;

        case EJsonPathItemType::StartsWithPredicate:
            result.Data = TStartsWithPrefixOffset{ReadUint(pos)};
            result.InputItemOffset = ReadUint(pos);
            break;

        case EJsonPathItemType::LikeRegexPredicate: {
            const auto serializedRegex = ReadString(pos);

            auto regex = NReWrapper::NDispatcher::Deserialize(serializedRegex);
            result.Data = std::move(regex);
            result.InputItemOffset = ReadUint(pos);
            break;
        }

        // Items with 2 inputs
        case EJsonPathItemType::BinaryAdd:
        case EJsonPathItemType::BinarySubstract:
        case EJsonPathItemType::BinaryMultiply:
        case EJsonPathItemType::BinaryDivide:
        case EJsonPathItemType::BinaryModulo:
        case EJsonPathItemType::BinaryLess:
        case EJsonPathItemType::BinaryLessEqual:
        case EJsonPathItemType::BinaryGreater:
        case EJsonPathItemType::BinaryGreaterEqual:
        case EJsonPathItemType::BinaryEqual:
        case EJsonPathItemType::BinaryNotEqual:
        case EJsonPathItemType::BinaryAnd:
        case EJsonPathItemType::BinaryOr:
            TBinaryOpArgumentsOffset data;
            data.LeftOffset = ReadUint(pos);
            data.RightOffset = ReadUint(pos);
            result.Data = data;
            break;
    }

    return result;
}

TUint TJsonPathReader::ReadUint(TUint& pos) {
    return ReadPOD<TUint>(pos);
}

double TJsonPathReader::ReadDouble(TUint& pos) {
    return ReadPOD<double>(pos);
}

bool TJsonPathReader::ReadBool(TUint& pos) {
    return ReadPOD<bool>(pos);
}

EJsonPathItemType TJsonPathReader::ReadType(TUint& pos) {
    return static_cast<EJsonPathItemType>(ReadUint(pos));
}

EJsonPathMode TJsonPathReader::ReadMode(TUint& pos) {
    return static_cast<EJsonPathMode>(ReadUint(pos));
}

const TStringBuf TJsonPathReader::ReadString(TUint& pos) {
    TUint length = ReadUint(pos);
    TStringBuf result(Path->Begin() + pos, length);
    pos += length;
    return result;
}

TVector<TArraySubscriptOffsets> TJsonPathReader::ReadSubscripts(TUint& pos) {
    const auto count = ReadUint(pos);
    TVector<TArraySubscriptOffsets> result(count);

    for (size_t i = 0; i < count; i++) {
        result[i].FromOffset = ReadUint(pos);
        result[i].ToOffset = ReadUint(pos);
    }
    return result;
}

void TJsonPathBuilder::VisitRoot(const TRootNode& node) {
    // Block structure:
    // <(1) TUint>
    // Components:
    // (1) Must be casted to EJsonPathMode. Jsonpath execution mode
    WriteMode(node.GetMode());
    node.GetExpr()->Accept(*this);
}

void TJsonPathBuilder::VisitContextObject(const TContextObjectNode& node) {
    WriteZeroInputItem(EJsonPathItemType::ContextObject, node);
}

void TJsonPathBuilder::VisitVariable(const TVariableNode& node) {
    WriteZeroInputItem(EJsonPathItemType::Variable, node);
    WriteString(node.GetName());
}

void TJsonPathBuilder::VisitLastArrayIndex(const TLastArrayIndexNode& node) {
    WriteZeroInputItem(EJsonPathItemType::LastArrayIndex, node);
}

void TJsonPathBuilder::VisitNumberLiteral(const TNumberLiteralNode& node) {
    WriteZeroInputItem(EJsonPathItemType::NumberLiteral, node);
    WriteDouble(node.GetValue());
}

void TJsonPathBuilder::VisitMemberAccess(const TMemberAccessNode& node) {
    // Block structure:
    // <(1) TUint> <(2) TUint> <(3) TUint> <(4) TUint> <(5) char[]> <(6) TUint>
    // Components:
    // (1) Must be casted to EJsonPathItemType. Member access item type
    // (2) Row of the position in the source jsonpath
    // (3) Column of the position in the source jsonpath
    // (4) Length of member name string
    // (5) Member name string
    // (6) Offset of the input item
    WriteType(EJsonPathItemType::MemberAccess);
    WritePos(node);
    WriteString(node.GetMember());

    WriteNextPosition();
    node.GetInput()->Accept(*this);
}

void TJsonPathBuilder::VisitWildcardMemberAccess(const TWildcardMemberAccessNode& node) {
    WriteSingleInputItem(EJsonPathItemType::WildcardMemberAccess, node, node.GetInput());
}

void TJsonPathBuilder::VisitArrayAccess(const TArrayAccessNode& node) {
    // Block structure:
    // <(1) TUint> <(2) TUint> <(3) TUint> <(4) TUint> <(5) pair<TUint, TUint>[]> <(6) TUint> <(7) items>
    // Components:
    // (1) Must be casted to EJsonPathItemType. Array access item type
    // (2) Row of the position in the source jsonpath
    // (3) Column of the position in the source jsonpath
    // (4) Count of subscripts stored
    // (5) Array of pairs with offsets to subscript items. If subscript is a single index, only first element
    //     is set to it's offset and second is zero. If subscript is a range, both pair elements are valid offsets
    //     to the elements of range (lower and upper bound).
    // (6) Offset of the input item
    // (7) Array of subcsripts. For details about encoding see VisitArraySubscript
    WriteType(EJsonPathItemType::ArrayAccess);
    WritePos(node);

    // (4) Write count of subscripts stored
    const auto& subscripts = node.GetSubscripts();
    const auto count = subscripts.size();
    WriteUint(count);

    // (5) We do not know sizes of each subscript. Write array of zeros for offsets
    const auto indexStart = CurrentEndPos();
    TVector<TUint> offsets(2 * count);
    WriteUintSequence(offsets);

    // (6) Reserve space for input offset to rewrite it later
    const auto inputStart = CurrentEndPos();
    WriteFinishPosition();

    // (7) Write all subscripts and record offset for each of them
    for (size_t i = 0; i < count; i++) {
        offsets[2 * i] = CurrentEndPos();
        subscripts[i].From->Accept(*this);

        if (subscripts[i].To) {
            offsets[2 * i + 1] = CurrentEndPos();
            subscripts[i].To->Accept(*this);
        }
    }

    // (5) Rewrite offsets with correct values
    RewriteUintSequence(offsets, indexStart);

    // (6) Rewrite input offset
    RewriteUint(CurrentEndPos(), inputStart);
    node.GetInput()->Accept(*this);
}

void TJsonPathBuilder::VisitWildcardArrayAccess(const TWildcardArrayAccessNode& node) {
    WriteSingleInputItem(EJsonPathItemType::WildcardArrayAccess, node, node.GetInput());
}

void TJsonPathBuilder::VisitUnaryOperation(const TUnaryOperationNode& node) {
    EJsonPathItemType type;
    switch (node.GetOp()) {
        case EUnaryOperation::Plus:
            type = EJsonPathItemType::UnaryPlus;
            break;
        case EUnaryOperation::Minus:
            type = EJsonPathItemType::UnaryMinus;
            break;
        case EUnaryOperation::Not:
            type = EJsonPathItemType::UnaryNot;
            break;
    }

    WriteSingleInputItem(type, node, node.GetExpr());
}

void TJsonPathBuilder::VisitBinaryOperation(const TBinaryOperationNode& node) {
    EJsonPathItemType type;
    switch (node.GetOp()) {
        case EBinaryOperation::Add:
            type = EJsonPathItemType::BinaryAdd;
            break;
        case EBinaryOperation::Substract:
            type = EJsonPathItemType::BinarySubstract;
            break;
        case EBinaryOperation::Multiply:
            type = EJsonPathItemType::BinaryMultiply;
            break;
        case EBinaryOperation::Divide:
            type = EJsonPathItemType::BinaryDivide;
            break;
        case EBinaryOperation::Modulo:
            type = EJsonPathItemType::BinaryModulo;
            break;
        case EBinaryOperation::Less:
            type = EJsonPathItemType::BinaryLess;
            break;
        case EBinaryOperation::LessEqual:
            type = EJsonPathItemType::BinaryLessEqual;
            break;
        case EBinaryOperation::Greater:
            type = EJsonPathItemType::BinaryGreater;
            break;
        case EBinaryOperation::GreaterEqual:
            type = EJsonPathItemType::BinaryGreaterEqual;
            break;
        case EBinaryOperation::Equal:
            type = EJsonPathItemType::BinaryEqual;
            break;
        case EBinaryOperation::NotEqual:
            type = EJsonPathItemType::BinaryNotEqual;
            break;
        case EBinaryOperation::And:
            type = EJsonPathItemType::BinaryAnd;
            break;
        case EBinaryOperation::Or:
            type = EJsonPathItemType::BinaryOr;
            break;
    }

    WriteTwoInputsItem(type, node, node.GetLeftExpr(), node.GetRightExpr());
}

void TJsonPathBuilder::VisitBooleanLiteral(const TBooleanLiteralNode& node) {
    WriteZeroInputItem(EJsonPathItemType::BooleanLiteral, node);
    WriteBool(node.GetValue());
}

void TJsonPathBuilder::VisitNullLiteral(const TNullLiteralNode& node) {
    WriteZeroInputItem(EJsonPathItemType::NullLiteral, node);
}

void TJsonPathBuilder::VisitStringLiteral(const TStringLiteralNode& node) {
    WriteZeroInputItem(EJsonPathItemType::StringLiteral, node);
    WriteString(node.GetValue());
}

void TJsonPathBuilder::VisitFilterObject(const TFilterObjectNode& node) {
    WriteZeroInputItem(EJsonPathItemType::FilterObject, node);
}

void TJsonPathBuilder::VisitFilterPredicate(const TFilterPredicateNode& node) {
    WriteTwoInputsItem(EJsonPathItemType::FilterPredicate, node, node.GetPredicate(), node.GetInput());
}

void TJsonPathBuilder::VisitMethodCall(const TMethodCallNode& node) {
    EJsonPathItemType type;
    switch (node.GetType()) {
        case EMethodType::Abs:
            type = EJsonPathItemType::AbsMethod;
            break;
        case EMethodType::Floor:
            type = EJsonPathItemType::FloorMethod;
            break;
        case EMethodType::Ceiling:
            type = EJsonPathItemType::CeilingMethod;
            break;
        case EMethodType::Double:
            type = EJsonPathItemType::DoubleMethod;
            break;
        case EMethodType::Type:
            type = EJsonPathItemType::TypeMethod;
            break;
        case EMethodType::Size:
            type = EJsonPathItemType::SizeMethod;
            break;
        case EMethodType::KeyValue:
            type = EJsonPathItemType::KeyValueMethod;
            break;
    }

    WriteSingleInputItem(type, node, node.GetInput());
}

TJsonPathPtr TJsonPathBuilder::ShrinkAndGetResult() {
    Result->ShrinkToFit();
    return Result;
}

void TJsonPathBuilder::VisitStartsWithPredicate(const TStartsWithPredicateNode& node) {
    WriteTwoInputsItem(EJsonPathItemType::StartsWithPredicate, node, node.GetPrefix(), node.GetInput());
}

void TJsonPathBuilder::VisitExistsPredicate(const TExistsPredicateNode& node) {
    WriteSingleInputItem(EJsonPathItemType::ExistsPredicate, node, node.GetInput());
}

void TJsonPathBuilder::VisitIsUnknownPredicate(const TIsUnknownPredicateNode& node) {
    WriteSingleInputItem(EJsonPathItemType::IsUnknownPredicate, node, node.GetInput());
}

void TJsonPathBuilder::VisitLikeRegexPredicate(const TLikeRegexPredicateNode& node) {
    // Block structure:
    // <(1) TUint> <(2) TUint> <(3) TUint> <(4) TUint> <(5) char[]> <(6) TUint>
    // Components:
    // (1) Must be casted to EJsonPathItemType. Member access item type
    // (2) Row of the position in the source jsonpath
    // (3) Column of the position in the source jsonpath
    // (4) Length of serialized Hyperscan database
    // (5) Serialized Hyperscan database
    // (6) Offset of the input item
    WriteType(EJsonPathItemType::LikeRegexPredicate);
    WritePos(node);

    const TString serializedRegex = node.GetRegex()->Serialize();
    WriteString(serializedRegex);

    WriteNextPosition();
    node.GetInput()->Accept(*this);
}

void TJsonPathBuilder::WriteZeroInputItem(EJsonPathItemType type, const TAstNode& node) {
    // Block structure:
    // <(1) TUint> <(2) TUint> <(3) TUint>
    // Components:
    // (1) Item type
    // (2) Row of the position in the source jsonpath
    // (3) Column of the position in the source jsonpath
    WriteType(type);
    WritePos(node);
}

void TJsonPathBuilder::WriteSingleInputItem(EJsonPathItemType type, const TAstNode& node, const TAstNodePtr input) {
    // Block structure:
    // <(1) TUint> <(2) TUint> <(3) TUint> <(4) TUint> <(5) item>
    // Components:
    // (1) Item type
    // (2) Row of the position in the source jsonpath
    // (3) Column of the position in the source jsonpath
    // (4) Offset of the input item
    // (5) Input item
    WriteZeroInputItem(type, node);

    WriteNextPosition();
    input->Accept(*this);
}

void TJsonPathBuilder::WriteTwoInputsItem(EJsonPathItemType type, const TAstNode& node, const TAstNodePtr firstInput, const TAstNodePtr secondInput) {
    // Block structure:
    // <(1) TUint> <(2) TUint> <(3) TUint> <(4) TUint> <(5) TUint> <(6) item> <(7) item>
    // Components:
    // (1) Item type
    // (2) Row of the position in the source jsonpath
    // (3) Column of the position in the source jsonpath
    // (4) Offset of the first input
    // (5) Offset of the second input
    // (6) JsonPath item representing first input
    // (7) JsonPath item representing right input
    WriteZeroInputItem(type, node);

    // (4) and (5) Fill offsets with zeros
    const auto indexStart = CurrentEndPos();
    WriteUint(0);
    WriteUint(0);

    // (6) Write first input and record it's offset
    const auto firstInputStart = CurrentEndPos();
    firstInput->Accept(*this);

    // (7) Write second input and record it's offset
    const auto secondInputStart = CurrentEndPos();
    secondInput->Accept(*this);

    // (4) and (5) Rewrite offsets with correct values
    RewriteUintSequence({firstInputStart, secondInputStart}, indexStart);
}

void TJsonPathBuilder::WritePos(const TAstNode& node) {
    WriteUint(node.GetPos().Row);
    WriteUint(node.GetPos().Column);
}

void TJsonPathBuilder::WriteType(EJsonPathItemType type) {
    WriteUint(static_cast<TUint>(type));
}

void TJsonPathBuilder::WriteMode(EJsonPathMode mode) {
    WriteUint(static_cast<TUint>(mode));
}

void TJsonPathBuilder::WriteNextPosition() {
    WriteUint(CurrentEndPos() + sizeof(TUint));
}

void TJsonPathBuilder::WriteFinishPosition() {
    WriteUint(0);
}

void TJsonPathBuilder::WriteString(TStringBuf value) {
    WriteUint(value.size());
    Result->Append(value.data(), value.size());
}

void TJsonPathBuilder::RewriteUintSequence(const TVector<TUint>& sequence, TUint offset) {
    const auto length = sequence.size() * sizeof(TUint);
    Y_ASSERT(offset + length < CurrentEndPos());

    MemCopy(Result->Data() + offset, reinterpret_cast<const char*>(sequence.data()), length);
}

void TJsonPathBuilder::WriteUintSequence(const TVector<TUint>& sequence) {
    const auto length = sequence.size() * sizeof(TUint);
    Result->Append(reinterpret_cast<const char*>(sequence.data()), length);
}

void TJsonPathBuilder::RewriteUint(TUint value, TUint offset) {
    Y_ASSERT(offset + sizeof(TUint) < CurrentEndPos());

    MemCopy(Result->Data() + offset, reinterpret_cast<const char*>(&value), sizeof(TUint));
}

void TJsonPathBuilder::WriteUint(TUint value) {
    WritePOD(value);
}

void TJsonPathBuilder::WriteDouble(double value) {
    WritePOD(value);
}

void TJsonPathBuilder::WriteBool(bool value) {
    WritePOD(value);
}

TUint TJsonPathBuilder::CurrentEndPos() const {
    return Result->Size();
}


}