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
|
#include "src/kqp_runner.h"
#include <contrib/libs/protobuf/src/google/protobuf/text_format.h>
#include <library/cpp/colorizer/colors.h>
#include <library/cpp/getopt/last_getopt.h>
#include <util/stream/file.h>
#include <util/system/env.h>
#include <ydb/library/yql/minikql/invoke_builtins/mkql_builtins.h>
#include <ydb/library/yql/utils/backtrace/backtrace.h>
struct TExecutionOptions {
TString ScriptQuery;
TString SchemeQuery;
NKikimrKqp::EQueryAction ScriptQueryAction = NKikimrKqp::QUERY_ACTION_EXECUTE;
};
void RunScript(const TExecutionOptions& executionOptions, const NKqpRun::TRunnerOptions& runnerOptions) {
NColorizer::TColors colors = NColorizer::AutoColors(Cout);
Cout << colors.Yellow() << "Initialization of kqp runner..." << colors.Default() << Endl;
NKqpRun::TKqpRunner runner(runnerOptions);
if (executionOptions.SchemeQuery) {
Cout << colors.Yellow() << "Executing scheme query..." << colors.Default() << Endl;
if (!runner.ExecuteSchemeQuery(executionOptions.SchemeQuery)) {
ythrow yexception() << "Scheme query execution failed";
}
}
if (executionOptions.ScriptQuery) {
Cout << colors.Yellow() << "Executing script..." << colors.Default() << Endl;
if (!runner.ExecuteScript(executionOptions.ScriptQuery, executionOptions.ScriptQueryAction)) {
ythrow yexception() << "Script execution failed";
}
}
if (executionOptions.ScriptQueryAction == NKikimrKqp::QUERY_ACTION_EXECUTE) {
Cout << colors.Yellow() << "Writing script results..." << colors.Default() << Endl;
if (!runner.WriteScriptResults()) {
ythrow yexception() << "Writing script results failed";
}
}
}
THolder<TFileOutput> SetupDefaultFileOutput(const TString& filePath, IOutputStream*& stream) {
THolder<TFileOutput> fileHolder;
if (filePath == "-") {
stream = &Cout;
} else if (filePath) {
fileHolder.Reset(new TFileOutput(filePath));
stream = fileHolder.Get();
}
return fileHolder;
}
void RunMain(int argc, const char* argv[]) {
TString scriptQueryFile;
TString schemeQueryFile;
TString resultOutputFile = "-";
TString schemeQueryAstFile;
TString scriptQueryAstFile;
TString scriptQueryPlanFile;
TString logFile = "-";
TString scriptQueryAction = "execute";
TString planOutputFormat = "pretty";
i64 resultsRowsLimit = 1000;
TVector<TString> udfsPaths;
TString udfsDirectory;
NLastGetopt::TOpts options = NLastGetopt::TOpts::Default();
options.AddLongOption('p', "script-query", "Script query to execute")
.Optional()
.RequiredArgument("FILE")
.StoreResult(&scriptQueryFile);
options.AddLongOption('s', "scheme-query", "Scheme query to execute")
.Optional()
.RequiredArgument("FILE")
.StoreResult(&schemeQueryFile);
options.AddLongOption("log-file", "File with execution logs (use '-' to write in stderr)")
.Optional()
.RequiredArgument("FILE")
.StoreResult(&logFile);
options.AddLongOption("result-file", "File with script execution results (use '-' to write in stdout)")
.Optional()
.RequiredArgument("FILE")
.StoreResult(&resultOutputFile);
options.AddLongOption("scheme-ast-file", "File with scheme query ast (use '-' to write in stdout)")
.Optional()
.RequiredArgument("FILE")
.StoreResult(&schemeQueryAstFile);
options.AddLongOption("script-ast-file", "File with script query ast (use '-' to write in stdout)")
.Optional()
.RequiredArgument("FILE")
.StoreResult(&scriptQueryAstFile);
options.AddLongOption("script-plan-file", "File with script query plan (use '-' to write in stdout)")
.Optional()
.RequiredArgument("FILE")
.StoreResult(&scriptQueryPlanFile);
options.AddLongOption("script-action", "Script query execute action, one of { execute | explain }")
.Optional()
.RequiredArgument("STR")
.DefaultValue(scriptQueryAction)
.StoreResult(&scriptQueryAction);
options.AddLongOption("plan-format", "Script query plan format, one of { pretty | table | json }")
.Optional()
.RequiredArgument("STR")
.DefaultValue(planOutputFormat)
.StoreResult(&planOutputFormat);
options.AddLongOption("results-limit", "Rows limit for script execution results")
.Optional()
.RequiredArgument("INT")
.DefaultValue(resultsRowsLimit)
.StoreResult(&resultsRowsLimit);
options.AddLongOption("udf", "Load shared library with UDF by given path")
.Optional()
.RequiredArgument("FILE")
.AppendTo(&udfsPaths);
options.AddLongOption("udfs-dir", "Load all shared libraries with UDFs found in given directory")
.Optional()
.RequiredArgument("PATH")
.StoreResult(&udfsDirectory);
NLastGetopt::TOptsParseResult parsedOptions(&options, argc, argv);
// Execution options
TExecutionOptions executionOptions;
if (!schemeQueryFile && !scriptQueryFile) {
ythrow yexception() << "Nothing to execute";
}
if (schemeQueryFile) {
executionOptions.SchemeQuery = TFileInput(schemeQueryFile).ReadAll();
}
if (scriptQueryFile) {
executionOptions.ScriptQuery = TFileInput(scriptQueryFile).ReadAll();
}
executionOptions.ScriptQueryAction =
(scriptQueryAction == TStringBuf("execute")) ? NKikimrKqp::QUERY_ACTION_EXECUTE
: (scriptQueryAction == TStringBuf("explain")) ? NKikimrKqp::QUERY_ACTION_EXPLAIN
: NKikimrKqp::QUERY_ACTION_EXECUTE;
// Runner options
NKqpRun::TRunnerOptions runnerOptions;
if (resultsRowsLimit >= 0) {
runnerOptions.ResultsRowsLimit = resultsRowsLimit;
} else {
ythrow yexception() << "Results rows limit less than zero";
}
THolder<TFileOutput> resultFileHolder = SetupDefaultFileOutput(resultOutputFile, runnerOptions.ResultOutput);
THolder<TFileOutput> schemeQueryAstFileHolder = SetupDefaultFileOutput(schemeQueryAstFile, runnerOptions.SchemeQueryAstOutput);
THolder<TFileOutput> scriptQueryAstFileHolder = SetupDefaultFileOutput(scriptQueryAstFile, runnerOptions.ScriptQueryAstOutput);
THolder<TFileOutput> scriptQueryPlanFileHolder = SetupDefaultFileOutput(scriptQueryPlanFile, runnerOptions.ScriptQueryPlanOutput);
runnerOptions.PlanOutputFormat =
(planOutputFormat == TStringBuf("pretty")) ? NYdb::NConsoleClient::EOutputFormat::Pretty
: (planOutputFormat == TStringBuf("table")) ? NYdb::NConsoleClient::EOutputFormat::PrettyTable
: (planOutputFormat == TStringBuf("json")) ? NYdb::NConsoleClient::EOutputFormat::JsonUnicode
: NYdb::NConsoleClient::EOutputFormat::Default;
// Ydb settings
if (logFile != "-") {
runnerOptions.YdbSettings.LogOutputFile = logFile;
}
runnerOptions.YdbSettings.YqlToken = GetEnv("YQL_TOKEN");
NKikimr::NMiniKQL::FindUdfsInDir(udfsDirectory, &udfsPaths);
auto functionRegistry = NKikimr::NMiniKQL::CreateFunctionRegistry(&NYql::NBacktrace::KikimrBackTrace, NKikimr::NMiniKQL::CreateBuiltinRegistry(), false, udfsPaths)->Clone();
NKikimr::NMiniKQL::FillStaticModules(*functionRegistry);
runnerOptions.YdbSettings.FunctionRegistry = functionRegistry.Get();
TString appConfigData = TFileInput("./configuration/app_config.conf").ReadAll();
if (!google::protobuf::TextFormat::ParseFromString(appConfigData, &runnerOptions.YdbSettings.AppConfig)) {
ythrow yexception() << "Bad format of app configuration";
}
RunScript(executionOptions, runnerOptions);
}
int main(int argc, const char* argv[]) {
try {
RunMain(argc, argv);
} catch (...) {
NColorizer::TColors colors = NColorizer::AutoColors(Cerr);
Cerr << colors.Red() << CurrentExceptionMessage() << colors.Default() << Endl;
return 1;
}
return 0;
}
|