22
22
#include < yql/essentials/public/udf/udf_static_registry.h>
23
23
24
24
25
+ namespace NKqpRun {
26
+
27
+ namespace {
28
+
25
29
struct TExecutionOptions {
26
30
enum class EExecutionCase {
27
31
GenericScript,
@@ -77,7 +81,7 @@ struct TExecutionOptions {
77
81
return GetValue (index, ScriptQueryActions, NKikimrKqp::EQueryAction::QUERY_ACTION_EXECUTE);
78
82
}
79
83
80
- NKqpRun:: TRequestOptions GetSchemeQueryOptions () const {
84
+ TRequestOptions GetSchemeQueryOptions () const {
81
85
TString sql = SchemeQuery;
82
86
if (UseTemplates) {
83
87
ReplaceYqlTokenTemplate (sql);
@@ -94,7 +98,7 @@ struct TExecutionOptions {
94
98
};
95
99
}
96
100
97
- NKqpRun:: TRequestOptions GetScriptQueryOptions (size_t index, size_t queryId, TInstant startTime) const {
101
+ TRequestOptions GetScriptQueryOptions (size_t index, size_t queryId, TInstant startTime) const {
98
102
Y_ABORT_UNLESS (index < ScriptQueries.size ());
99
103
100
104
TString sql = ScriptQueries[index];
@@ -110,11 +114,12 @@ struct TExecutionOptions {
110
114
.PoolId = GetValue (index, PoolIds, TString ()),
111
115
.UserSID = GetValue (index, UserSIDs, TString (BUILTIN_ACL_ROOT)),
112
116
.Database = GetValue (index, Databases, TString ()),
113
- .Timeout = GetValue (index, Timeouts, TDuration::Zero ())
117
+ .Timeout = GetValue (index, Timeouts, TDuration::Zero ()),
118
+ .QueryId = queryId
114
119
};
115
120
}
116
121
117
- void Validate (const NKqpRun:: TRunnerOptions& runnerOptions) const {
122
+ void Validate (const TRunnerOptions& runnerOptions) const {
118
123
if (!SchemeQuery && ScriptQueries.empty () && !runnerOptions.YdbSettings .MonitoringEnabled && !runnerOptions.YdbSettings .GrpcEnabled ) {
119
124
ythrow yexception () << " Nothing to execute and is not running as daemon" ;
120
125
}
@@ -123,7 +128,7 @@ struct TExecutionOptions {
123
128
ValidateSchemeQueryOptions (runnerOptions);
124
129
ValidateScriptExecutionOptions (runnerOptions);
125
130
ValidateAsyncOptions (runnerOptions.YdbSettings .AsyncQueriesSettings );
126
- ValidateTraceOpt (runnerOptions. TraceOptType );
131
+ ValidateTraceOpt (runnerOptions);
127
132
}
128
133
129
134
private:
@@ -143,7 +148,7 @@ struct TExecutionOptions {
143
148
checker (Timeouts.size (), " timeouts" );
144
149
}
145
150
146
- void ValidateSchemeQueryOptions (const NKqpRun:: TRunnerOptions& runnerOptions) const {
151
+ void ValidateSchemeQueryOptions (const TRunnerOptions& runnerOptions) const {
147
152
if (SchemeQuery) {
148
153
return ;
149
154
}
@@ -152,7 +157,7 @@ struct TExecutionOptions {
152
157
}
153
158
}
154
159
155
- void ValidateScriptExecutionOptions (const NKqpRun:: TRunnerOptions& runnerOptions) const {
160
+ void ValidateScriptExecutionOptions (const TRunnerOptions& runnerOptions) const {
156
161
if (runnerOptions.YdbSettings .SameSession && HasExecutionCase (EExecutionCase::AsyncQuery)) {
157
162
ythrow yexception () << " Same session can not be used with async quries" ;
158
163
}
@@ -194,7 +199,7 @@ struct TExecutionOptions {
194
199
}
195
200
}
196
201
197
- void ValidateAsyncOptions (const NKqpRun:: TAsyncQueriesSettings& asyncQueriesSettings) const {
202
+ void ValidateAsyncOptions (const TAsyncQueriesSettings& asyncQueriesSettings) const {
198
203
if (asyncQueriesSettings.InFlightLimit && !HasExecutionCase (EExecutionCase::AsyncQuery)) {
199
204
ythrow yexception () << " In flight limit can not be used without async queries" ;
200
205
}
@@ -205,28 +210,43 @@ struct TExecutionOptions {
205
210
}
206
211
}
207
212
208
- void ValidateTraceOpt (NKqpRun::TRunnerOptions::ETraceOptType traceOptType) const {
209
- switch (traceOptType) {
210
- case NKqpRun::TRunnerOptions::ETraceOptType::Scheme: {
213
+ void ValidateTraceOpt (const TRunnerOptions& runnerOptions) const {
214
+ NColorizer::TColors colors = NColorizer::AutoColors (Cout);
215
+ switch (runnerOptions.TraceOptType ) {
216
+ case TRunnerOptions::ETraceOptType::Scheme: {
211
217
if (!SchemeQuery) {
212
218
ythrow yexception () << " Trace opt type scheme cannot be used without scheme query" ;
213
219
}
214
220
break ;
215
221
}
216
- case NKqpRun:: TRunnerOptions::ETraceOptType::Script: {
222
+ case TRunnerOptions::ETraceOptType::Script: {
217
223
if (ScriptQueries.empty ()) {
218
224
ythrow yexception () << " Trace opt type script cannot be used without script queries" ;
219
225
}
220
226
}
221
- case NKqpRun:: TRunnerOptions::ETraceOptType::All: {
227
+ case TRunnerOptions::ETraceOptType::All: {
222
228
if (!SchemeQuery && ScriptQueries.empty ()) {
223
229
ythrow yexception () << " Trace opt type all cannot be used without any queries" ;
224
230
}
225
231
}
226
- case NKqpRun:: TRunnerOptions::ETraceOptType::Disabled: {
232
+ case TRunnerOptions::ETraceOptType::Disabled: {
227
233
break ;
228
234
}
229
235
}
236
+
237
+ if (const auto traceOptId = runnerOptions.TraceOptScriptId ) {
238
+ if (runnerOptions.TraceOptType != TRunnerOptions::ETraceOptType::Script) {
239
+ ythrow yexception () << " Trace opt id allowed only for trace opt type script (used " << runnerOptions.TraceOptType << " )" ;
240
+ }
241
+
242
+ const ui64 numberScripts = ScriptQueries.size () * LoopCount;
243
+ if (*traceOptId >= numberScripts) {
244
+ ythrow yexception () << " Invalid trace opt id " << *traceOptId << " , it should be less than number of scipt queries " << numberScripts;
245
+ }
246
+ if (numberScripts == 1 ) {
247
+ Cout << colors.Red () << " Warning: trace opt id is not necessary for single script mode, use -T script" << Endl;
248
+ }
249
+ }
230
250
}
231
251
232
252
private:
@@ -239,8 +259,8 @@ struct TExecutionOptions {
239
259
}
240
260
241
261
static void ReplaceYqlTokenTemplate (TString& sql) {
242
- const TString variableName = TStringBuilder () << " ${" << NKqpRun:: YQL_TOKEN_VARIABLE << " }" ;
243
- if (const TString& yqlToken = GetEnv (NKqpRun:: YQL_TOKEN_VARIABLE)) {
262
+ const TString variableName = TStringBuilder () << " ${" << YQL_TOKEN_VARIABLE << " }" ;
263
+ if (const TString& yqlToken = GetEnv (YQL_TOKEN_VARIABLE)) {
244
264
SubstGlobal (sql, variableName, yqlToken);
245
265
} else if (sql.Contains (variableName)) {
246
266
ythrow yexception () << " Failed to replace ${YQL_TOKEN} template, please specify YQL_TOKEN environment variable\n " ;
@@ -249,7 +269,7 @@ struct TExecutionOptions {
249
269
};
250
270
251
271
252
- void RunArgumentQuery (size_t index, size_t queryId, TInstant startTime, const TExecutionOptions& executionOptions, NKqpRun:: TKqpRunner& runner) {
272
+ void RunArgumentQuery (size_t index, size_t queryId, TInstant startTime, const TExecutionOptions& executionOptions, TKqpRunner& runner) {
253
273
NColorizer::TColors colors = NColorizer::AutoColors (Cout);
254
274
255
275
switch (executionOptions.GetExecutionCase (index)) {
@@ -292,7 +312,7 @@ void RunArgumentQuery(size_t index, size_t queryId, TInstant startTime, const TE
292
312
}
293
313
294
314
295
- void RunArgumentQueries (const TExecutionOptions& executionOptions, NKqpRun:: TKqpRunner& runner) {
315
+ void RunArgumentQueries (const TExecutionOptions& executionOptions, TKqpRunner& runner) {
296
316
NColorizer::TColors colors = NColorizer::AutoColors (Cout);
297
317
298
318
if (executionOptions.SchemeQuery ) {
@@ -354,11 +374,11 @@ void RunAsDaemon() {
354
374
}
355
375
356
376
357
- void RunScript (const TExecutionOptions& executionOptions, const NKqpRun:: TRunnerOptions& runnerOptions) {
377
+ void RunScript (const TExecutionOptions& executionOptions, const TRunnerOptions& runnerOptions) {
358
378
NColorizer::TColors colors = NColorizer::AutoColors (Cout);
359
379
360
380
Cout << colors.Yellow () << TInstant::Now ().ToIsoStringLocal () << " Initialization of kqp runner..." << colors.Default () << Endl;
361
- NKqpRun:: TKqpRunner runner (runnerOptions);
381
+ TKqpRunner runner (runnerOptions);
362
382
363
383
try {
364
384
RunArgumentQueries (executionOptions, runner);
@@ -403,11 +423,11 @@ TIntrusivePtr<NKikimr::NMiniKQL::IMutableFunctionRegistry> CreateFunctionRegistr
403
423
404
424
405
425
class TMain : public TMainClassArgs {
406
- inline static const TString YqlToken = GetEnv(NKqpRun:: YQL_TOKEN_VARIABLE);
426
+ inline static const TString YqlToken = GetEnv(YQL_TOKEN_VARIABLE);
407
427
inline static std::vector<std::unique_ptr<TFileOutput>> FileHolders;
408
428
409
429
TExecutionOptions ExecutionOptions;
410
- NKqpRun:: TRunnerOptions RunnerOptions;
430
+ TRunnerOptions RunnerOptions;
411
431
412
432
THashMap<TString, TString> TablesMapping;
413
433
TVector<TString> UdfsPaths;
@@ -524,13 +544,13 @@ class TMain : public TMainClassArgs {
524
544
std::remove (file.c_str ());
525
545
}
526
546
});
527
- TChoices<NKqpRun:: TRunnerOptions::ETraceOptType> traceOpt ({
528
- {" all" , NKqpRun:: TRunnerOptions::ETraceOptType::All},
529
- {" scheme" , NKqpRun:: TRunnerOptions::ETraceOptType::Scheme},
530
- {" script" , NKqpRun:: TRunnerOptions::ETraceOptType::Script},
531
- {" disabled" , NKqpRun:: TRunnerOptions::ETraceOptType::Disabled}
547
+ TChoices<TRunnerOptions::ETraceOptType> traceOpt ({
548
+ {" all" , TRunnerOptions::ETraceOptType::All},
549
+ {" scheme" , TRunnerOptions::ETraceOptType::Scheme},
550
+ {" script" , TRunnerOptions::ETraceOptType::Script},
551
+ {" disabled" , TRunnerOptions::ETraceOptType::Disabled}
532
552
});
533
- options.AddLongOption (' T' , " trace-opt" , " print AST in the begin of each transformation" )
553
+ options.AddLongOption (' T' , " trace-opt" , " print AST in the begin of each transformation (use script@<query id> for tracing one -p query) " )
534
554
.RequiredArgument (" trace-opt-query" )
535
555
.DefaultValue (" disabled" )
536
556
.Choices (traceOpt.GetChoices ())
@@ -539,6 +559,9 @@ class TMain : public TMainClassArgs {
539
559
RunnerOptions.YdbSettings .TraceOptEnabled = traceOptType != NKqpRun::TRunnerOptions::ETraceOptType::Disabled;
540
560
return traceOptType;
541
561
});
562
+ options.AddLongOption (' I' , " trace-opt-index" , " index of -p query to use --trace-opt, starts from zero" )
563
+ .RequiredArgument (" uint" )
564
+ .StoreResult (&RunnerOptions.TraceOptScriptId );
542
565
options.AddLongOption (" trace-id" , " Trace id for -p queries" )
543
566
.RequiredArgument (" id" )
544
567
.EmplaceTo (&ExecutionOptions.TraceIds );
@@ -551,10 +574,10 @@ class TMain : public TMainClassArgs {
551
574
.RequiredArgument (" uint" )
552
575
.DefaultValue (0 )
553
576
.StoreResult (&ExecutionOptions.ResultsRowsLimit );
554
- TChoices<NKqpRun:: TRunnerOptions::EResultOutputFormat> resultFormat ({
555
- {" rows" , NKqpRun:: TRunnerOptions::EResultOutputFormat::RowsJson},
556
- {" full-json" , NKqpRun:: TRunnerOptions::EResultOutputFormat::FullJson},
557
- {" full-proto" , NKqpRun:: TRunnerOptions::EResultOutputFormat::FullProto}
577
+ TChoices<TRunnerOptions::EResultOutputFormat> resultFormat ({
578
+ {" rows" , TRunnerOptions::EResultOutputFormat::RowsJson},
579
+ {" full-json" , TRunnerOptions::EResultOutputFormat::FullJson},
580
+ {" full-proto" , TRunnerOptions::EResultOutputFormat::FullProto}
558
581
});
559
582
options.AddLongOption (' R' , " result-format" , " Script query result format" )
560
583
.RequiredArgument (" result-format" )
@@ -620,9 +643,9 @@ class TMain : public TMainClassArgs {
620
643
.RequiredArgument (" uint" )
621
644
.DefaultValue (0 )
622
645
.StoreResult (&RunnerOptions.YdbSettings .AsyncQueriesSettings .InFlightLimit );
623
- TChoices<NKqpRun:: TAsyncQueriesSettings::EVerbose> verbose ({
624
- {" each-query" , NKqpRun:: TAsyncQueriesSettings::EVerbose::EachQuery},
625
- {" final" , NKqpRun:: TAsyncQueriesSettings::EVerbose::Final}
646
+ TChoices<TAsyncQueriesSettings::EVerbose> verbose ({
647
+ {" each-query" , TAsyncQueriesSettings::EVerbose::EachQuery},
648
+ {" final" , TAsyncQueriesSettings::EVerbose::Final}
626
649
});
627
650
options.AddLongOption (" async-verbose" , " Verbose type for async queries" )
628
651
.RequiredArgument (" type" )
@@ -814,13 +837,16 @@ void SegmentationFaultHandler(int) {
814
837
abort ();
815
838
}
816
839
840
+ } // anonymous namespace
841
+
842
+ } // namespace NKqpRun
817
843
818
844
int main (int argc, const char * argv[]) {
819
- std::set_terminate (KqprunTerminateHandler);
820
- signal (SIGSEGV, &SegmentationFaultHandler);
845
+ std::set_terminate (NKqpRun:: KqprunTerminateHandler);
846
+ signal (SIGSEGV, &NKqpRun:: SegmentationFaultHandler);
821
847
822
848
try {
823
- TMain ().Run (argc, argv);
849
+ NKqpRun:: TMain ().Run (argc, argv);
824
850
} catch (...) {
825
851
NColorizer::TColors colors = NColorizer::AutoColors (Cerr);
826
852
0 commit comments