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
|
#include <contrib/libs/fmt/include/fmt/format.h>
#include <yql/essentials/ast/yql_expr.h>
#include <yql/essentials/providers/common/provider/yql_provider_names.h>
#include <yql/essentials/sql/sql.h>
#include <yql/essentials/parser/pg_catalog/catalog.h>
#include <yql/essentials/parser/pg_wrapper/interface/config.h>
enum class EDebugOutput {
None,
ToCerr,
};
inline TString Err2Str(NYql::TAstParseResult& res, EDebugOutput debug = EDebugOutput::None) {
TStringStream s;
res.Issues.PrintTo(s);
if (debug == EDebugOutput::ToCerr) {
Cerr << s.Str() << Endl;
}
return s.Str();
}
class TTestAutoParamBuilder : public NYql::IAutoParamBuilder {
public:
TString GetParamValue(const TString& param) const {
auto ptr = State.FindPtr(param);
Y_ENSURE(ptr);
TStringBuilder res;
if (ListParams.contains(param)) {
res << '[';
bool first = true;
for (const auto& t : ptr->first) {
if (!first) {
res << ',';
} else {
first = false;
}
res << t;
}
res << ':';
first = true;
for (const auto& v : ptr->second) {
if (!first) {
res << ',';
} else {
first = false;
}
res << v.GetOrElse("#");
}
res << ']';
} else {
res << ptr->first.front();
res << ':';
res << ptr->second.front().GetOrElse("#");
}
return res;
}
ui32 Size() const final {
return State.size();
}
bool Contains(const TString& name) const final {
return State.contains(name);
}
NYql::IAutoParamTypeBuilder& Add(const TString& name) final {
CurrentParam = name;
return Type;
}
class TTypeProxy : public NYql::IAutoParamTypeBuilder {
public:
TTypeProxy(TTestAutoParamBuilder& owner)
: Owner(owner)
{}
void Pg(const TString& name) {
Owner.State[Owner.CurrentParam].first.push_back(name);
}
void BeginList() final {
Owner.ListParams.insert(Owner.CurrentParam);
}
void EndList() final {
}
void BeginTuple() final {
}
void EndTuple() final {
}
void BeforeItem() final {
}
void AfterItem() final {
}
NYql::IAutoParamDataBuilder& FinishType() final {
return Owner.Data;
}
TTestAutoParamBuilder& Owner;
};
class TDataProxy : public NYql::IAutoParamDataBuilder {
public:
TDataProxy(TTestAutoParamBuilder& owner)
: Owner(owner)
{}
void Pg(const TMaybe<TString>& value) final {
Owner.State[Owner.CurrentParam].second.push_back(value);
}
void BeginList() final {
}
void EndList() final {
}
void BeginTuple() final {
}
void EndTuple() final {
}
void BeforeItem() final {
}
void AfterItem() final {
}
NYql::IAutoParamBuilder& FinishData() final {
return Owner;
}
TTestAutoParamBuilder& Owner;
};
TMap<TString, std::pair<TVector<TString>, TVector<TMaybe<TString>>>> State;
THashSet<TString> ListParams;
TTypeProxy Type;
TDataProxy Data;
TString CurrentParam;
TTestAutoParamBuilder()
: Type(*this)
, Data(*this)
{}
};
class TTestAutoParamBuilderFactory : public NYql::IAutoParamBuilderFactory {
public:
NYql::IAutoParamBuilderPtr MakeBuilder() final {
return MakeIntrusive<TTestAutoParamBuilder>();
}
};
inline NYql::TAstParseResult SqlToYqlWithMode(const TString& query, NSQLTranslation::ESqlMode mode = NSQLTranslation::ESqlMode::QUERY, size_t maxErrors = 10, const TString& provider = {},
EDebugOutput debug = EDebugOutput::None, bool ansiLexer = false, NSQLTranslation::TTranslationSettings settings = {})
{
google::protobuf::Arena arena;
const auto service = provider ? provider : TString(NYql::YtProviderName);
const TString cluster = "plato";
settings.ClusterMapping[cluster] = service;
settings.ClusterMapping["hahn"] = NYql::YtProviderName;
settings.ClusterMapping["mon"] = NYql::SolomonProviderName;
settings.ClusterMapping[""] = NYql::KikimrProviderName;
settings.MaxErrors = maxErrors;
settings.Mode = mode;
settings.Arena = &arena;
settings.AnsiLexer = ansiLexer;
settings.SyntaxVersion = 1;
settings.PgParser = true;
TTestAutoParamBuilderFactory autoParamFactory;
settings.AutoParamBuilderFactory = &autoParamFactory;
auto res = SqlToYql(query, settings);
if (debug == EDebugOutput::ToCerr) {
Err2Str(res, debug);
}
return res;
}
inline NYql::TAstParseResult PgSqlToYql(const TString& query, size_t maxErrors = 10, const TString& provider = {}, EDebugOutput debug = EDebugOutput::None) {
return SqlToYqlWithMode(query, NSQLTranslation::ESqlMode::QUERY, maxErrors, provider, debug);
}
using TAstNodeVisitFunc = std::function<void(const NYql::TAstNode& root)>;
inline void VisitAstNodes(const NYql::TAstNode& root, const TAstNodeVisitFunc& visitFunc) {
visitFunc(root);
if (!root.IsList()) {
return;
}
for (size_t childIdx = 0; childIdx < root.GetChildrenCount(); ++childIdx) {
VisitAstNodes(*root.GetChild(childIdx), visitFunc);
}
}
inline TMaybe<const NYql::TAstNode*> MaybeGetQuotedValue(const NYql::TAstNode& node) {
const bool isQuotedList =
node.IsListOfSize(2) && node.GetChild(0)->IsAtom()
&& node.GetChild(0)->GetContent() == "quote";
if (isQuotedList) {
return node.GetChild(1);
}
return {};
}
|