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
|
#pragma once
#include "binary.h"
#include "value.h"
#include <yql/essentials/public/issue/yql_issue.h>
#include <yql/essentials/utils/yql_panic.h>
#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/public/udf/udf_value_builder.h>
#include <yql/essentials/public/udf/udf_allocator.h>
#include <library/cpp/json/json_value.h>
#include <library/cpp/containers/stack_vector/stack_vec.h>
#include <util/generic/ptr.h>
#include <util/generic/stack.h>
#include <util/generic/hash.h>
#include <util/generic/maybe.h>
#include <variant>
namespace NYql::NJsonPath {
using TJsonNodes = TSmallVec<TValue>;
class TResult {
public:
TResult(TJsonNodes&& nodes);
TResult(const TJsonNodes& nodes);
TResult(TIssue&& issue);
const TJsonNodes& GetNodes() const;
TJsonNodes& GetNodes();
const TIssue& GetError() const;
bool IsError() const;
private:
std::variant<TJsonNodes, TIssue> Result;
};
class TArraySubscript {
public:
TArraySubscript(i64 from, TPosition fromPos)
: From(from)
, FromPos(fromPos)
, HasTo(false)
{
}
TArraySubscript(i64 from, TPosition fromPos, i64 to, TPosition toPos)
: From(from)
, FromPos(fromPos)
, To(to)
, ToPos(toPos)
, HasTo(true)
{
}
i64 GetFrom() const {
return From;
}
TPosition GetFromPos() const {
return FromPos;
}
i64 GetTo() const {
YQL_ENSURE(IsRange());
return To;
}
TPosition GetToPos() const {
return ToPos;
}
bool IsRange() const {
return HasTo;
}
private:
i64 From = 0;
TPosition FromPos;
i64 To = 0;
TPosition ToPos;
bool HasTo;
};
using TVariablesMap = THashMap<TString, TValue>;
class TExecutor {
public:
TExecutor(
const TJsonPathPtr path,
const TJsonNodes& input,
const TVariablesMap& variables,
const NUdf::IValueBuilder* valueBuilder);
TResult Execute();
private:
constexpr static double EPSILON = 1e-20;
static bool IsZero(double value);
static bool IsEqual(double a, double b);
static bool IsLess(double a, double b);
static bool IsGreater(double a, double b);
bool IsStrict() const;
bool IsLax() const;
TResult Execute(const TJsonPathItem& item);
TResult ContextObject();
TResult Variable(const TJsonPathItem& item);
TResult LastArrayIndex(const TJsonPathItem& item);
TResult NumberLiteral(const TJsonPathItem& item);
TResult MemberAccess(const TJsonPathItem& item);
TResult WildcardMemberAccess(const TJsonPathItem& item);
TMaybe<TIssue> EnsureSingleSubscript(TPosition pos, const TJsonNodes& index, i64& result);
TMaybe<TIssue> EnsureArraySubscripts(const TJsonPathItem& item, TVector<TArraySubscript>& result);
TResult ArrayAccess(const TJsonPathItem& item);
TResult WildcardArrayAccess(const TJsonPathItem& item);
TResult UnaryArithmeticOp(const TJsonPathItem& item);
TMaybe<TIssue> EnsureBinaryArithmeticOpArgument(TPosition pos, const TJsonNodes& nodes, double& result);
TResult BinaryArithmeticOp(const TJsonPathItem& item);
TMaybe<TIssue> EnsureLogicalOpArgument(TPosition pos, const TJsonNodes& nodes, TMaybe<bool>& result);
TResult BinaryLogicalOp(const TJsonPathItem& item);
TResult UnaryLogicalOp(const TJsonPathItem& item);
TResult BooleanLiteral(const TJsonPathItem& item);
TResult NullLiteral();
TResult StringLiteral(const TJsonPathItem& item);
TMaybe<bool> CompareValues(const TValue& left, const TValue& right, EJsonPathItemType operation);
TResult CompareOp(const TJsonPathItem& item);
TResult FilterObject(const TJsonPathItem& item);
TResult FilterPredicate(const TJsonPathItem& item);
TResult NumericMethod(const TJsonPathItem& item);
TResult DoubleMethod(const TJsonPathItem& item);
TResult TypeMethod(const TJsonPathItem& item);
TResult SizeMethod(const TJsonPathItem& item);
TResult KeyValueMethod(const TJsonPathItem& item);
TResult StartsWithPredicate(const TJsonPathItem& item);
TResult IsUnknownPredicate(const TJsonPathItem& item);
TResult ExistsPredicate(const TJsonPathItem& item);
TResult LikeRegexPredicate(const TJsonPathItem& item);
TJsonNodes OptionalUnwrapArrays(const TJsonNodes& input);
TJsonNodes OptionalArrayWrapNodes(const TJsonNodes& input);
TStack<TValue> ArraySubscriptSource;
TStack<TValue> CurrentFilterObject;
TJsonPathReader Reader;
TJsonNodes Input;
const TVariablesMap& Variables;
const NUdf::IValueBuilder* ValueBuilder;
};
}
|