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
|
#pragma once
#include <yql/essentials/core/credentials/yql_credentials.h>
#include <yql/essentials/core/file_storage/file_storage.h>
#include <yql/essentials/core/services/yql_plan.h>
#include <yql/essentials/core/services/yql_transform_pipeline.h>
#include <yql/essentials/core/url_lister/interface/url_lister_manager.h>
#include <yql/essentials/core/url_preprocessing/interface/url_preprocessing.h>
#include <yql/essentials/core/yql_type_annotation.h>
#include <yql/essentials/core/yql_user_data.h>
#include <yql/essentials/core/qplayer/storage/interface/yql_qstorage.h>
#include <yql/essentials/providers/config/yql_config_provider.h>
#include <yql/essentials/providers/result/provider/yql_result_provider.h>
#include <yql/essentials/providers/common/proto/gateways_config.pb.h>
#include <yql/essentials/public/issue/yql_issue.h>
#include <yql/essentials/sql/sql.h>
#include <library/cpp/random_provider/random_provider.h>
#include <library/cpp/time_provider/time_provider.h>
#include <library/cpp/threading/future/future.h>
#include <util/system/file.h>
#include <util/generic/ptr.h>
#include <functional>
namespace NKikimr {
namespace NMiniKQL {
class IFunctionRegistry;
}
}
namespace NYql {
class TProgram;
using TProgramPtr = TIntrusivePtr<TProgram>;
class TProgramFactory;
using TProgramFactoryPtr = TIntrusivePtr<TProgramFactory>;
///////////////////////////////////////////////////////////////////////////////
// TProgramFactory
///////////////////////////////////////////////////////////////////////////////
class TProgramFactory: public TThrRefBase, private TMoveOnly
{
public:
TProgramFactory(
bool useRepeatableRandomAndTimeProviders,
const NKikimr::NMiniKQL::IFunctionRegistry* functionRegistry,
ui64 nextUniqueId,
const TVector<TDataProviderInitializer>& dataProvidersInit,
const TString& runner);
void AddUserDataTable(const TUserDataTable& userDataTable);
void SetCredentials(TCredentials::TPtr credentials);
void SetGatewaysConfig(const TGatewaysConfig* gatewaysConfig);
void SetModules(IModuleResolver::TPtr modules);
void SetUrlListerManager(IUrlListerManagerPtr urlListerManager);
void SetUdfResolver(IUdfResolver::TPtr udfResolver);
void SetUdfIndex(TUdfIndex::TPtr udfIndex, TUdfIndexPackageSet::TPtr udfIndexPackageSet);
void SetFileStorage(TFileStoragePtr fileStorage);
void SetUrlPreprocessing(IUrlPreprocessing::TPtr urlPreprocessing);
void EnableRangeComputeFor();
void SetArrowResolver(IArrowResolver::TPtr arrowResolver);
TProgramPtr Create(
const TFile& file,
const TString& sessionId = TString(),
const TQContext& qContext = {});
TProgramPtr Create(
const TString& filename,
const TString& sourceCode,
const TString& sessionId = TString(),
EHiddenMode hiddenMode = EHiddenMode::Disable,
const TQContext& qContext = {});
void UnrepeatableRandom();
private:
const bool UseRepeatableRandomAndTimeProviders_;
bool UseUnrepeatableRandom = false;
const NKikimr::NMiniKQL::IFunctionRegistry* FunctionRegistry_;
const ui64 NextUniqueId_;
TVector<TDataProviderInitializer> DataProvidersInit_;
TUserDataTable UserDataTable_;
TCredentials::TPtr Credentials_;
const TGatewaysConfig* GatewaysConfig_;
IModuleResolver::TPtr Modules_;
IUrlListerManagerPtr UrlListerManager_;
IUdfResolver::TPtr UdfResolver_;
TUdfIndex::TPtr UdfIndex_;
TUdfIndexPackageSet::TPtr UdfIndexPackageSet_;
TFileStoragePtr FileStorage_;
IUrlPreprocessing::TPtr UrlPreprocessing_;
TString Runner_;
bool EnableRangeComputeFor_ = false;
IArrowResolver::TPtr ArrowResolver_;
};
///////////////////////////////////////////////////////////////////////////////
// TProgram
///////////////////////////////////////////////////////////////////////////////
class TProgram: public TThrRefBase, private TNonCopyable
{
public:
friend TProgramFactory;
using TStatus = IGraphTransformer::TStatus;
using TFutureStatus = NThreading::TFuture<TStatus>;
public:
~TProgram();
void AddCredentials(const TVector<std::pair<TString, TCredential>>& credentials);
void ClearCredentials();
void AddUserDataTable(const TUserDataTable& userDataTable);
bool ParseYql();
bool ParseSql();
bool ParseSql(const NSQLTranslation::TTranslationSettings& settings);
bool Compile(const TString& username, bool skipLibraries = false);
TStatus Discover(const TString& username);
TFutureStatus DiscoverAsync(const TString& username);
TStatus Lineage(const TString& username, IOutputStream* traceOut = nullptr, IOutputStream* exprOut = nullptr, bool withTypes = false);
TFutureStatus LineageAsync(const TString& username, IOutputStream* traceOut = nullptr, IOutputStream* exprOut = nullptr, bool withTypes = false);
TStatus Validate(const TString& username, IOutputStream* exprOut = nullptr, bool withTypes = false);
TFutureStatus ValidateAsync(const TString& username, IOutputStream* exprOut = nullptr, bool withTypes = false);
TStatus Optimize(
const TString& username,
IOutputStream* traceOut = nullptr,
IOutputStream* tracePlan = nullptr,
IOutputStream* exprOut = nullptr,
bool withTypes = false);
TFutureStatus OptimizeAsync(
const TString& username,
IOutputStream* traceOut = nullptr,
IOutputStream* tracePlan = nullptr,
IOutputStream* exprOut = nullptr,
bool withTypes = false);
TStatus Run(
const TString& username,
IOutputStream* traceOut = nullptr,
IOutputStream* tracePlan = nullptr,
IOutputStream* exprOut = nullptr,
bool withTypes = false);
TFutureStatus RunAsync(
const TString& username,
IOutputStream* traceOut = nullptr,
IOutputStream* tracePlan = nullptr,
IOutputStream* exprOut = nullptr,
bool withTypes = false);
TStatus LineageWithConfig(
const TString& username,
const IPipelineConfigurator& pipelineConf);
TFutureStatus LineageAsyncWithConfig(
const TString& username,
const IPipelineConfigurator& pipelineConf);
TStatus OptimizeWithConfig(
const TString& username,
const IPipelineConfigurator& pipelineConf);
TFutureStatus OptimizeAsyncWithConfig(
const TString& username,
const IPipelineConfigurator& pipelineConf);
TStatus RunWithConfig(
const TString& username,
const IPipelineConfigurator& pipelineConf);
TFutureStatus RunAsyncWithConfig(
const TString& username,
const IPipelineConfigurator& pipelineConf);
TFutureStatus ContinueAsync();
bool HasActiveProcesses();
bool NeedWaitForActiveProcesses();
[[nodiscard]]
NThreading::TFuture<void> Abort();
TIssues Issues() const;
TIssues CompletedIssues() const;
void FinalizeIssues();
void Print(IOutputStream* exprOut, IOutputStream* planOut, bool cleanPlan = false);
inline void PrintErrorsTo(IOutputStream& out) const {
Issues().PrintWithProgramTo(out, Filename_, SourceCode_);
}
inline const TAstNode* AstRoot() const {
return AstRoot_;
}
inline const TExprNode::TPtr& ExprRoot() const {
return ExprRoot_;
}
inline TExprContext& ExprCtx() const {
return *ExprCtx_;
}
inline bool HasResults() const {
return ResultProviderConfig_ &&
!ResultProviderConfig_->CommittedResults.empty();
}
inline const TVector<TString>& Results() const {
return ResultProviderConfig_->CommittedResults;
}
TMaybe<TString> GetQueryAst(TMaybe<size_t> memoryLimit = {});
TMaybe<TString> GetQueryPlan(const TPlanSettings& settings = {});
void SetDiagnosticFormat(NYson::EYsonFormat format) {
DiagnosticFormat_ = format;
}
void SetResultType(IDataProvider::EResultFormat type) {
ResultType_ = type;
}
TMaybe<TString> GetDiagnostics();
IGraphTransformer::TStatistics GetRawDiagnostics();
TMaybe<TString> GetTasksInfo();
TMaybe<TString> GetStatistics(bool totalOnly = false, THashMap<TString, TStringBuf> extraYsons = {});
TMaybe<TString> GetDiscoveredData();
TMaybe<TString> GetLineage();
TString ResultsAsString() const;
void ConfigureYsonResultFormat(NYson::EYsonFormat format);
inline IOutputStream* ExprStream() const { return ExprStream_; }
inline IOutputStream* PlanStream() const { return PlanStream_; }
NYson::EYsonFormat GetResultFormat() const { return ResultFormat_; }
NYson::EYsonFormat GetOutputFormat() const { return OutputFormat_; }
void SetValidateOptions(NUdf::EValidateMode validateMode);
void SetDisableNativeUdfSupport(bool disable);
void SetUseTableMetaFromGraph(bool use);
void SetProgressWriter(TOperationProgressWriter writer) {
Y_ENSURE(!TypeCtx_, "TypeCtx_ already created");
ProgressWriter_ = ThreadSafeProgressWriter(writer);
}
void SetAuthenticatedUser(const TString& user) {
Y_ENSURE(!TypeCtx_, "TypeCtx_ already created");
OperationOptions_.AuthenticatedUser = user;
}
void SetOperationId(const TString& id) {
Y_ENSURE(!TypeCtx_, "TypeCtx_ already created");
OperationOptions_.Id = id;
}
void SetSharedOperationId(const TString& id) {
Y_ENSURE(!TypeCtx_, "TypeCtx_ already created");
OperationOptions_.SharedId = id;
}
void SetOperationTitle(const TString& title) {
Y_ENSURE(!TypeCtx_, "TypeCtx_ already created");
if (!title.Contains("YQL")) {
ythrow yexception() << "Please mention YQL in the title '" << title << "'";
}
OperationOptions_.Title = title;
}
void SetOperationUrl(const TString& url) {
Y_ENSURE(!TypeCtx_, "TypeCtx_ already created");
OperationOptions_.Url = url;
}
void SetQueryName(const TString& name) {
Y_ENSURE(!TypeCtx_, "TypeCtx_ already created");
OperationOptions_.QueryName = name;
}
void SetOperationAttrsYson(const TString& attrs) {
Y_ENSURE(!TypeCtx_, "TypeCtx_ already created");
OperationOptions_.AttrsYson = attrs;
}
void SetParametersYson(const TString& parameters);
// should be used after Compile phase
bool ExtractQueryParametersMetadata();
const TString& GetExtractedQueryParametersMetadataYson() const {
return ExtractedQueryParametersMetadataYson_;
}
void EnableResultPosition() {
SupportsResultPosition_ = true;
}
IPlanBuilder& GetPlanBuilder();
void SetAbortHidden(std::function<void()>&& func) {
AbortHidden_ = std::move(func);
}
TMaybe<TSet<TString>> GetUsedClusters() {
CollectUsedClusters();
return UsedClusters_;
}
private:
TProgram(
const NKikimr::NMiniKQL::IFunctionRegistry* functionRegistry,
const TIntrusivePtr<IRandomProvider> randomProvider,
const TIntrusivePtr<ITimeProvider> timeProvider,
ui64 nextUniqueId,
const TVector<TDataProviderInitializer>& dataProvidersInit,
const TUserDataTable& userDataTable,
const TCredentials::TPtr& credentials,
const IModuleResolver::TPtr& modules,
const IUrlListerManagerPtr& urlListerManager,
const IUdfResolver::TPtr& udfResolver,
const TUdfIndex::TPtr& udfIndex,
const TUdfIndexPackageSet::TPtr& udfIndexPackageSet,
const TFileStoragePtr& fileStorage,
const IUrlPreprocessing::TPtr& urlPreprocessing,
const TGatewaysConfig* gatewaysConfig,
const TString& filename,
const TString& sourceCode,
const TString& sessionId,
const TString& runner,
bool enableRangeComputeFor,
const IArrowResolver::TPtr& arrowResolver,
EHiddenMode hiddenMode,
const TQContext& qContext);
TTypeAnnotationContextPtr BuildTypeAnnotationContext(const TString& username);
TTypeAnnotationContextPtr GetAnnotationContext() const;
TTypeAnnotationContextPtr ProvideAnnotationContext(const TString& username);
bool CollectUsedClusters();
NThreading::TFuture<void> OpenSession(const TString& username);
[[nodiscard]]
NThreading::TFuture<void> CleanupLastSession();
[[nodiscard]]
NThreading::TFuture<void> CloseLastSession();
TFutureStatus RemoteKikimrValidate(const TString& cluster);
TFutureStatus RemoteKikimrOptimize(const TString& cluster, const IPipelineConfigurator* pipelineConf);
TFutureStatus RemoteKikimrRun(const TString& cluster, const IPipelineConfigurator* pipelineConf);
bool FillParseResult(NYql::TAstParseResult&& astRes, NYql::TWarningRules* warningRules = nullptr);
TString GetSessionId() const;
NThreading::TFuture<IGraphTransformer::TStatus> AsyncTransformWithFallback(bool applyAsyncChanges);
void SaveExprRoot();
private:
std::optional<bool> CheckFallbackIssues(const TIssues& issues);
void HandleSourceCode(TString& sourceCode);
void HandleTranslationSettings(NSQLTranslation::TTranslationSettings& loadedSettings,
const NSQLTranslation::TTranslationSettings*& currentSettings);
const NKikimr::NMiniKQL::IFunctionRegistry* FunctionRegistry_;
const TIntrusivePtr<IRandomProvider> RandomProvider_;
const TIntrusivePtr<ITimeProvider> TimeProvider_;
const ui64 NextUniqueId_;
TAstNode* AstRoot_;
std::unique_ptr<TMemoryPool> AstPool_;
TAutoPtr<TExprContext> ExprCtx_;
TTypeAnnotationContextPtr TypeCtx_;
const IModuleResolver::TPtr Modules_;
TVector<TDataProviderInitializer> DataProvidersInit_;
TAdaptiveLock DataProvidersLock_;
TVector<TDataProviderInfo> DataProviders_;
TYqlOperationOptions OperationOptions_;
TCredentials::TPtr Credentials_;
const IUrlListerManagerPtr UrlListerManager_;
IUdfResolver::TPtr UdfResolver_;
const TUdfIndex::TPtr UdfIndex_;
const TUdfIndexPackageSet::TPtr UdfIndexPackageSet_;
const TFileStoragePtr FileStorage_;
TUserDataTable SavedUserDataTable_;
TUserDataStorage::TPtr UserDataStorage_;
const TGatewaysConfig* GatewaysConfig_;
TGatewaysConfig LoadedGatewaysConfig_;
TString Filename_;
TString SourceCode_;
ESourceSyntax SourceSyntax_;
ui16 SyntaxVersion_;
TExprNode::TPtr ExprRoot_;
TExprNode::TPtr SavedExprRoot_;
mutable TAdaptiveLock SessionIdLock_;
TString SessionId_;
NThreading::TFuture<void> CloseLastSessionFuture_;
TAutoPtr<IPlanBuilder> PlanBuilder_;
TAutoPtr<IGraphTransformer> Transformer_;
TIntrusivePtr<TResultProviderConfig> ResultProviderConfig_;
bool SupportsResultPosition_ = false;
IDataProvider::EResultFormat ResultType_;
NYson::EYsonFormat ResultFormat_;
NYson::EYsonFormat OutputFormat_;
TMaybe<NYson::EYsonFormat> DiagnosticFormat_;
NUdf::EValidateMode ValidateMode_ = NUdf::EValidateMode::None;
bool DisableNativeUdfSupport_ = false;
bool UseTableMetaFromGraph_ = false;
TMaybe<TSet<TString>> UsedClusters_;
TMaybe<TSet<TString>> UsedProviders_;
TMaybe<TString> ExternalQueryAst_;
TMaybe<TString> ExternalQueryPlan_;
TMaybe<TString> ExternalDiagnostics_;
IOutputStream* ExprStream_ = nullptr;
IOutputStream* PlanStream_ = nullptr;
TOperationProgressWriter ProgressWriter_ = [](const TOperationProgress&) {};
TString ExtractedQueryParametersMetadataYson_;
const bool EnableRangeComputeFor_;
const IArrowResolver::TPtr ArrowResolver_;
i64 FallbackCounter_ = 0;
const EHiddenMode HiddenMode_ = EHiddenMode::Disable;
THiddenQueryAborter AbortHidden_ = [](){};
TMaybe<TString> LineageStr_;
TQContext QContext_;
TIssues FinalIssues_;
};
void UpdateSqlFlagsFromQContext(const TQContext& qContext, THashSet<TString>& flags);
} // namspace NYql
|