11#include " src/kqp_runner.h"
22
3+ #include < cstdio>
4+
35#include < contrib/libs/protobuf/src/google/protobuf/text_format.h>
46
57#include < library/cpp/colorizer/colors.h>
@@ -22,7 +24,7 @@ struct TExecutionOptions {
2224 TString ScriptTraceId = " kqprun" ;
2325
2426 bool HasResults () const {
25- return ScriptQuery && ScriptQueryAction == NKikimrKqp::QUERY_ACTION_EXECUTE && !ClearExecution ;
27+ return ScriptQuery && ScriptQueryAction == NKikimrKqp::QUERY_ACTION_EXECUTE;
2628 }
2729};
2830
@@ -46,6 +48,9 @@ void RunScript(const TExecutionOptions& executionOptions, const NKqpRun::TRunner
4648 if (!runner.ExecuteScript (executionOptions.ScriptQuery , executionOptions.ScriptQueryAction , executionOptions.ScriptTraceId )) {
4749 ythrow yexception () << " Script execution failed" ;
4850 }
51+ if (!runner.FetchScriptResults ()) {
52+ ythrow yexception () << " Fetch script results failed" ;
53+ }
4954 } else {
5055 if (!runner.ExecuteQuery (executionOptions.ScriptQuery , executionOptions.ScriptQueryAction , executionOptions.ScriptTraceId )) {
5156 ythrow yexception () << " Query execution failed" ;
@@ -54,10 +59,7 @@ void RunScript(const TExecutionOptions& executionOptions, const NKqpRun::TRunner
5459 }
5560
5661 if (executionOptions.HasResults ()) {
57- Cout << colors.Yellow () << " Writing script results..." << colors.Default () << Endl;
58- if (!runner.WriteScriptResults ()) {
59- ythrow yexception () << " Writing script results failed" ;
60- }
62+ runner.PrintScriptResults ();
6163 }
6264}
6365
@@ -74,6 +76,20 @@ THolder<TFileOutput> SetupDefaultFileOutput(const TString& filePath, IOutputStre
7476}
7577
7678
79+ TIntrusivePtr<NKikimr::NMiniKQL::IMutableFunctionRegistry> CreateFunctionRegistry (const TString& udfsDirectory, TVector<TString> udfsPaths) {
80+ if (!udfsDirectory.empty () || !udfsPaths.empty ()) {
81+ NColorizer::TColors colors = NColorizer::AutoColors (Cout);
82+ Cout << colors.Yellow () << " Fetching udfs..." << colors.Default () << Endl;
83+ }
84+
85+ NKikimr::NMiniKQL::FindUdfsInDir (udfsDirectory, &udfsPaths);
86+ auto functionRegistry = NKikimr::NMiniKQL::CreateFunctionRegistry (&NYql::NBacktrace::KikimrBackTrace, NKikimr::NMiniKQL::CreateBuiltinRegistry (), false , udfsPaths)->Clone ();
87+ NKikimr::NMiniKQL::FillStaticModules (*functionRegistry);
88+
89+ return functionRegistry;
90+ }
91+
92+
7793void RunMain (int argc, const char * argv[]) {
7894 TExecutionOptions executionOptions;
7995 NKqpRun::TRunnerOptions runnerOptions;
@@ -87,9 +103,11 @@ void RunMain(int argc, const char* argv[]) {
87103 TString logFile = " -" ;
88104 TString appConfigFile = " ./configuration/app_config.conf" ;
89105
106+ TString traceOptType = " disabled" ;
90107 TString scriptQueryAction = " execute" ;
91108 TString planOutputFormat = " pretty" ;
92109 TString resultOutputFormat = " rows" ;
110+ i64 resultsRowsLimit = 1000 ;
93111
94112 TVector<TString> udfsPaths;
95113 TString udfsDirectory;
@@ -103,7 +121,7 @@ void RunMain(int argc, const char* argv[]) {
103121 .Optional ()
104122 .RequiredArgument (" FILE" )
105123 .StoreResult (&schemeQueryFile);
106- options.AddLongOption (" app-config" , " File with app config (TAppConfig)" )
124+ options.AddLongOption (' c ' , " app-config" , " File with app config (TAppConfig)" )
107125 .Optional ()
108126 .RequiredArgument (" FILE" )
109127 .DefaultValue (appConfigFile)
@@ -135,33 +153,33 @@ void RunMain(int argc, const char* argv[]) {
135153 .NoArgument ()
136154 .DefaultValue (executionOptions.ClearExecution )
137155 .SetFlag (&executionOptions.ClearExecution );
138- options.AddLongOption (" trace-opt" , " print AST in the begin of each transformation" )
156+ options.AddLongOption (' T ' , " trace-opt" , " print AST in the begin of each transformation, one of { scheme | script | all } " )
139157 .Optional ()
140- .NoArgument ( )
141- .DefaultValue (runnerOptions. YdbSettings . TraceOpt )
142- .SetFlag (&runnerOptions. YdbSettings . TraceOpt );
143- options.AddLongOption (" script-action" , " Script query execute action, one of { execute | explain }" )
158+ .RequiredArgument ( " STR " )
159+ .DefaultValue (traceOptType )
160+ .StoreResult (&traceOptType );
161+ options.AddLongOption (' A ' , " script-action" , " Script query execute action, one of { execute | explain }" )
144162 .Optional ()
145163 .RequiredArgument (" STR" )
146164 .DefaultValue (scriptQueryAction)
147165 .StoreResult (&scriptQueryAction);
148- options.AddLongOption (" plan-format" , " Script query plan format, one of { pretty | table | json }" )
166+ options.AddLongOption (' P ' , " plan-format" , " Script query plan format, one of { pretty | table | json }" )
149167 .Optional ()
150168 .RequiredArgument (" STR" )
151169 .DefaultValue (planOutputFormat)
152170 .StoreResult (&planOutputFormat);
153- options.AddLongOption (" result-format" , " Script query result format, one of { rows | full }" )
171+ options.AddLongOption (' R ' , " result-format" , " Script query result format, one of { rows | full }" )
154172 .Optional ()
155173 .RequiredArgument (" STR" )
156174 .DefaultValue (resultOutputFormat)
157175 .StoreResult (&resultOutputFormat);
158- options.AddLongOption (" result-rows-limit" , " Rows limit for script execution results" )
176+ options.AddLongOption (' L ' , " result-rows-limit" , " Rows limit for script execution results" )
159177 .Optional ()
160178 .RequiredArgument (" INT" )
161- .DefaultValue (runnerOptions. ResultsRowsLimit )
162- .StoreResult (&runnerOptions. ResultsRowsLimit );
179+ .DefaultValue (resultsRowsLimit )
180+ .StoreResult (&resultsRowsLimit );
163181
164- options.AddLongOption (" udf" , " Load shared library with UDF by given path" )
182+ options.AddLongOption (' u ' , " udf" , " Load shared library with UDF by given path" )
165183 .Optional ()
166184 .RequiredArgument (" FILE" )
167185 .AppendTo (&udfsPaths);
@@ -191,15 +209,19 @@ void RunMain(int argc, const char* argv[]) {
191209
192210 // Runner options
193211
194- if (runnerOptions.ResultsRowsLimit < 0 ) {
195- ythrow yexception () << " Results rows limit less than zero" ;
196- }
197-
198212 THolder<TFileOutput> resultFileHolder = SetupDefaultFileOutput (resultOutputFile, runnerOptions.ResultOutput );
199213 THolder<TFileOutput> schemeQueryAstFileHolder = SetupDefaultFileOutput (schemeQueryAstFile, runnerOptions.SchemeQueryAstOutput );
200214 THolder<TFileOutput> scriptQueryAstFileHolder = SetupDefaultFileOutput (scriptQueryAstFile, runnerOptions.ScriptQueryAstOutput );
201215 THolder<TFileOutput> scriptQueryPlanFileHolder = SetupDefaultFileOutput (scriptQueryPlanFile, runnerOptions.ScriptQueryPlanOutput );
202216
217+ runnerOptions.TraceOptType =
218+ (traceOptType == TStringBuf (" all" )) ? NKqpRun::TRunnerOptions::ETraceOptType::All
219+ : (traceOptType == TStringBuf (" scheme" )) ? NKqpRun::TRunnerOptions::ETraceOptType::Scheme
220+ : (traceOptType == TStringBuf (" script" )) ? NKqpRun::TRunnerOptions::ETraceOptType::Script
221+ : (traceOptType == TStringBuf (" disabled" )) ? NKqpRun::TRunnerOptions::ETraceOptType::Disabled
222+ : NKqpRun::TRunnerOptions::ETraceOptType::All;
223+ runnerOptions.YdbSettings .TraceOptEnabled = runnerOptions.TraceOptType != NKqpRun::TRunnerOptions::ETraceOptType::Disabled;
224+
203225 runnerOptions.ResultOutputFormat =
204226 (resultOutputFormat == TStringBuf (" rows" )) ? NKqpRun::TRunnerOptions::EResultOutputFormat::RowsJson
205227 : (resultOutputFormat == TStringBuf (" full" )) ? NKqpRun::TRunnerOptions::EResultOutputFormat::FullJson
@@ -215,20 +237,22 @@ void RunMain(int argc, const char* argv[]) {
215237
216238 if (logFile != " -" ) {
217239 runnerOptions.YdbSettings .LogOutputFile = logFile;
240+ std::remove (logFile.c_str ());
218241 }
219242
220243 runnerOptions.YdbSettings .YqlToken = GetEnv (" YQL_TOKEN" );
221-
222- NKikimr::NMiniKQL::FindUdfsInDir (udfsDirectory, &udfsPaths);
223- auto functionRegistry = NKikimr::NMiniKQL::CreateFunctionRegistry (&NYql::NBacktrace::KikimrBackTrace, NKikimr::NMiniKQL::CreateBuiltinRegistry (), false , udfsPaths)->Clone ();
224- NKikimr::NMiniKQL::FillStaticModules (*functionRegistry);
225- runnerOptions.YdbSettings .FunctionRegistry = functionRegistry.Get ();
244+ runnerOptions.YdbSettings .FunctionRegistry = CreateFunctionRegistry (udfsDirectory, udfsPaths).Get ();
226245
227246 TString appConfigData = TFileInput (appConfigFile).ReadAll ();
228247 if (!google::protobuf::TextFormat::ParseFromString (appConfigData, &runnerOptions.YdbSettings .AppConfig )) {
229248 ythrow yexception () << " Bad format of app configuration" ;
230249 }
231250
251+ if (resultsRowsLimit < 0 ) {
252+ ythrow yexception () << " Results rows limit less than zero" ;
253+ }
254+ runnerOptions.YdbSettings .AppConfig .MutableQueryServiceConfig ()->SetScriptResultRowsLimit (resultsRowsLimit);
255+
232256 RunScript (executionOptions, runnerOptions);
233257}
234258
0 commit comments