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
|
#include "yql_pure_provider.h"
#include <library/cpp/testing/unittest/registar.h>
#include <yql/essentials/core/facade/yql_facade.h>
#include <yql/essentials/minikql/invoke_builtins/mkql_builtins.h>
#include <yql/essentials/minikql/mkql_function_registry.h>
#include <yql/essentials/public/result_format/yql_result_format_response.h>
#include <library/cpp/yson/node/node_io.h>
#include <util/system/user.h>
#include <util/string/strip.h>
namespace NYql {
namespace {
struct TSettings {
bool SExpr = false;
bool Pretty = false;
};
TString Run(const TString& query, TSettings settings = {}) {
auto functionRegistry = NKikimr::NMiniKQL::CreateFunctionRegistry(NKikimr::NMiniKQL::CreateBuiltinRegistry());
TVector<TDataProviderInitializer> dataProvidersInit;
dataProvidersInit.push_back(GetPureDataProviderInitializer());
TProgramFactory factory(true, functionRegistry.Get(), 0ULL, dataProvidersInit, "ut");
TProgramPtr program = factory.Create("-stdin-", query);
program->ConfigureYsonResultFormat(settings.Pretty ? NYson::EYsonFormat::Pretty : NYson::EYsonFormat::Text);
bool parseRes;
if (settings.SExpr) {
parseRes = program->ParseYql();
} else {
parseRes = program->ParseSql();
}
if (!parseRes) {
TStringStream err;
program->PrintErrorsTo(err);
UNIT_FAIL(err.Str());
}
if (!program->Compile(GetUsername())) {
TStringStream err;
program->PrintErrorsTo(err);
UNIT_FAIL(err.Str());
}
TProgram::TStatus status = program->Run(GetUsername());
if (status == TProgram::TStatus::Error) {
TStringStream err;
program->PrintErrorsTo(err);
UNIT_FAIL(err.Str());
}
return program->ResultsAsString();
}
}
Y_UNIT_TEST_SUITE(TPureProviderTests) {
Y_UNIT_TEST(SExpr) {
const auto s = R"(
(
(let result_sink (DataSink 'result))
(let output (Int32 '1))
(let world (Write! world result_sink (Key) output '('('type))))
(return (Commit! world result_sink))
)
)";
const auto expectedRes = R"(
[
{
"Write" = [
{
"Type" = [
"DataType";
"Int32"
];
"Data" = "1"
}
]
}
]
)";
auto res = Run(s, TSettings{.SExpr = true, .Pretty = true});
UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
}
Y_UNIT_TEST(Sql0Rows) {
const auto s = "select * from (select 1 as x) limit 0";
const auto expectedRes = R"(
[
{
"Write" = [
{
"Type" = [
"ListType";
[
"StructType";
[
[
"x";
[
"DataType";
"Int32"
]
]
]
]
];
"Data" = []
}
]
}
]
)";
auto res = Run(s, TSettings{.Pretty = true});
UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
}
void Sql1RowImpl(const TString& query) {
const auto expectedRes = R"(
[
{
"Write" = [
{
"Type" = [
"ListType";
[
"StructType";
[
[
"x";
[
"DataType";
"Int32"
]
]
]
]
];
"Data" = [
[
"1"
]
]
}
]
}
]
)";
auto res = Run(query, TSettings{.Pretty = true});
UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
}
Y_UNIT_TEST(Sql1Row_LLVM_On) {
const auto s = "pragma config.flags(\"LLVM\",\"--dump-stats\");select 1 as x";
Sql1RowImpl(s);
}
Y_UNIT_TEST(Sql1Row_LLVM_Off) {
const auto s = "pragma config.flags(\"LLVM\",\"OFF\");select 1 as x";
Sql1RowImpl(s);
}
Y_UNIT_TEST(Sql2Rows) {
const auto s = "select 1 as x union all select 2 as x order by x";
const auto expectedRes = R"(
[
{
"Write" = [
{
"Type" = [
"ListType";
[
"StructType";
[
[
"x";
[
"DataType";
"Int32"
]
]
]
]
];
"Data" = [
[
"1"
];
[
"2"
]
]
}
]
}
]
)";
auto res = Run(s, TSettings{.Pretty = true});
UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
}
Y_UNIT_TEST(TruncateRows) {
const auto s = "select x from (select ListFromRange(1,2000) as x) flatten by x";
auto res = Run(s);
auto respList = NResult::ParseResponse(NYT::NodeFromYsonString(res));
UNIT_ASSERT_VALUES_EQUAL(respList.size(), 1);
UNIT_ASSERT_VALUES_EQUAL(respList[0].Writes.size(), 1);
UNIT_ASSERT(respList[0].Writes[0].IsTruncated);
}
Y_UNIT_TEST(TruncateBytes) {
const auto s = "select '" + TString(1000000, 'a') + "' as x, 1 as y union all select '' as x, 2 as y order by y";
auto res = Run(s);
auto respList = NResult::ParseResponse(NYT::NodeFromYsonString(res));
UNIT_ASSERT_VALUES_EQUAL(respList.size(), 1);
UNIT_ASSERT_VALUES_EQUAL(respList[0].Writes.size(), 1);
UNIT_ASSERT(respList[0].Writes[0].IsTruncated);
}
Y_UNIT_TEST(ColumnOrder) {
const auto s = "pragma OrderedColumns;select 1 as y, 2 as x";
const auto expectedRes = R"(
[
{
"Write" = [
{
"Type" = [
"ListType";
[
"StructType";
[
[
"y";
[
"DataType";
"Int32"
]
];
[
"x";
[
"DataType";
"Int32"
]
]
]
]
];
"Data" = [
[
"1";
"2"
]
]
}
]
}
]
)";
auto res = Run(s, TSettings{.Pretty = true});
UNIT_ASSERT_NO_DIFF(res, Strip(expectedRes));
}
}
} // namespace NYql
|