source: trunk/FACT++/src/rootifysql.cc@ 19411

Last change on this file since 19411 was 19403, checked in by tbretz, 7 years ago
include 'random' was missing for Peppermint 9 (gcc 7)
File size: 38.4 KB
Line 
1#include "Database.h"
2
3#include <random>
4
5#include <boost/regex.hpp>
6#include <boost/tokenizer.hpp>
7#include <boost/algorithm/string.hpp>
8#include <boost/algorithm/string/join.hpp>
9
10#include "tools.h"
11#include "Time.h"
12#include "Configuration.h"
13
14#include <TROOT.h>
15#include <TSystem.h>
16#include <TFile.h>
17#include <TTree.h>
18
19using namespace std;
20
21// ------------------------------------------------------------------------
22
23void SetupConfiguration(Configuration &conf)
24{
25 po::options_description control("Database options");
26 control.add_options()
27 ("uri,u", var<string>()->required(), "Database link as in\n\tuser:password@server[:port]/database[?compress=0|1].")
28 ("query,q", var<string>(""), "MySQL query (overwrites --file)")
29 ("file", var<string>("rootify.sql"), "An ASCII file with the MySQL query (overwrites --query)")
30 ("ignore-null,i", po_switch(), "Do not skip rows containing any NULL field")
31 ("display,d", po_switch(), "Displays contents on the screen (most usefull in combination with mysql statements as SHOW or EXPLAIN)")
32 ("explain", po_switch(), "Requests an EXPLAIN from the server (shows the server optimized query)\nsee also https://dev.mysql.com/doc/refman/explain-output.html")
33 ("profiling", po_switch(), "Turn on profiling and print profile")
34 ("var.*", var<string>(), "Predefined SQL user variables (@VAR)")
35 ("env.*", vars<string>(), "Predefined environment for substitutions in the query ($ENV)")
36 ("list.*", var<string>(), "Predefined environment for substitutions in the query ($ENV). The list is read from the given file (one list entry per line)")
37 ("print-connection", po_switch(), "Print database connection information")
38 ("verbose,v", var<uint16_t>(1), "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
39 ;
40
41 po::options_description ascii("ASCII output");
42 ascii.add_options()
43 ("write,w", var<string>(""), "Write output to an ascii file")
44 ("delimiter", var<string>(), "The delimiter used if contents are displayed with --display (default=\\t)")
45 ("copy-shabang", po_switch(), "Copy the sha-bang line if exists to the output file")
46 ("copy-header", po_switch(), "Copy the header (all line starting with '#' up to the first non-comment line to the output file")
47 ("copy-query", po_switch(), "Copy the query to the ascii output file")
48 ("copy-comments", po_switch(), "Copy all lines starting with '#' to the output file which are not part of header")
49 ("copy-all", po_switch(), "An alias for --copy-header --copy-query --copy-comments")
50 ;
51
52 po::options_description root("Root file options");
53 root.add_options()
54 ("out,o", var<string>("rootify.root"), "Output root file name")
55 ("force,f", po_switch(), "Force overwriting an existing root file ('RECREATE')")
56 ("update", po_switch(), "Update an existing root file with the new tree ('UPDATE')")
57 ("compression,c", var<uint16_t>(1), "zlib compression level for the root file")
58 ("tree,t", var<string>("Result"), "Name of the root tree")
59 ("ignore", vars<string>(), "Ignore the given columns")
60 ("null,n", po_switch(), "Redirect the output file to /dev/null (mainly for debugging purposes, e.g. performance studies)")
61 ("no-fill", po_switch(), "Do not fill events into the root file (mainly for debugging purposes, e.g. performance studies)")
62 ;
63
64 po::options_description split("Splitting options");
65 split.add_options()
66 ("split-sequence,S", vars<uint16_t>(), "Split data sequentially into several trees/files (e.g. 1, 1, 2)")
67 ("split-quantile,Q", vars<double>(), "Split data randomly into several trees/files (e.g. 0.5, 1)")
68 ("seed", var<uint64_t>(mt19937_64::default_seed), "Seed value in case of random split")
69 ;
70
71 po::positional_options_description p;
72 p.add("file", 1); // The 1st positional options (n=1)
73 p.add("out", 1); // The 2nd positional options (n=1)
74
75 conf.AddOptions(control);
76 conf.AddOptions(ascii);
77 conf.AddOptions(root);
78 conf.AddOptions(split);
79 conf.SetArgumentPositions(p);
80}
81
82void PrintUsage()
83{
84 cout <<
85 "rootifysql - Converts the result of a mysql query into a root file\n"
86 "\n"
87 "For convenience, this documentation uses the extended version of the options, "
88 "refer to the output below to get the abbreviations.\n"
89 "\n"
90 "Writes the result of a mysql query into a root file. For each column, a branch is "
91 "created of type double with the field name as name. This is usually the column name "
92 "if not specified otherwise by the AS mysql directive.\n"
93 "\n"
94 "Columns with CHAR or VARCHAR as field type are ignored. DATETIME, DATE and TIME "
95 "columns are converted to unix time (time_t). Rows containing any file which is "
96 "NULL are skipped if not suppressed by the --ignore-null option. Ideally, the query "
97 "is compiled in a way that no NULL field is returned. With the --display option the "
98 "result of the request is printed on the screen (NULL skipping still in action). "
99 "This can be useful to create an ascii file or to show results as 'SHOW DATABASES' "
100 "or 'EXPLAIN table'. To redirect the contents into an ascii file, the option -v0 "
101 "is useful. To suppress writing to an output file --null can be used.\n"
102 "\n"
103 "The default is to read the query from a file called rootify.sql. Except if a different "
104 "filename is specified by the --file option or a query is given with --query.\n"
105 "\n"
106 "As a trick, the rootify.sql file can be made excutable (chmod u+x rootify.sql). "
107 "If the first line contains '#!rootifysql', the script can be executed directly.\n"
108 "\n"
109 "Columns whose name start with @ are skipped. If you want them in your output file "
110 "give them a name using AS, e.g. 'SELECT @A:=5 AS A'.\n"
111 "\n"
112 "You can use variables in your sql query like @MyVar and define them on the "
113 "command line. In this example with --var.MyVar=5\n"
114 "\n"
115 "You can use environment definitions for substitutions in your SQL query. "
116 "For example --env.TEST=5 would replace $TEST or ${TEST} in your query by 5."
117 "If you specify one environment variable more than once, a list is created. "
118 "For example --env.TEST=1 --env.TEST=2 --env.TEST=3 would substitute "
119 "$TEST or ${TEST} by '1, 2, 3'. This is useful for the SQL `IN` keyword. "
120 "You can also read the values for an enviroment substitution from a file "
121 "(one element per line), e.g. --env.TEST=file.txt. Empty lines and lines "
122 "starting with a # are skipped.\n"
123 "\n"
124 "Comments in the query-file can be placed according to the SQL standard inline "
125 "/*comment*/ or introduced with # (shell script style) or -- (SQL style).\n"
126 "\n"
127 "For several purposes, it might be convenient to split the output to several "
128 "different root-trees or ascii files. This can be done using the --split-sequence (-S) "
129 "and the --split-quantile (-Q) options. If a split sequence is defined as "
130 "-S 1 -S 2 -S 1 the events are split by 1:2:1 in this sequence order. If "
131 "quantiled are given as -Q 0.5 -Q 0.6, the first tree will contain 50% of "
132 "the second one 10% and the third one 40%. The corresponding seed value can "
133 "be set with --seed.\n"
134 "\n"
135 "In case of success, 0 is returned, a value>0 otherwise.\n"
136 "\n"
137 "Usage: rootifysql [rootify.sql [rootify.root]] [-u URI] [-q query|-f file] [-i] [-o out] [-f] [-cN] [-t tree] [-vN]\n"
138 "\n"
139 ;
140 cout << endl;
141}
142
143struct ExplainParser
144{
145 string sql;
146
147 vector<string> vec;
148
149 string substitute(string _str, const boost::regex &expr)
150 {
151 boost::smatch match;
152 while (boost::regex_search(_str, match, expr, boost::regex_constants::format_first_only))
153 {
154 const auto &len = match.length();
155 const auto &pos = match.position();
156 const auto &str = match.str();
157
158 const auto it = find(vec.cbegin(), vec.cend(), str);
159 const size_t id = it==vec.cend() ? vec.size() : it-vec.cbegin();
160
161 _str.replace(pos, len, "{"+to_string(id)+"}");
162
163 if (it==vec.cend())
164 vec.push_back(str);//.substr(1, str.size()-2));
165 }
166
167 return _str;
168 }
169
170 string substitute(const string &str, const string &expr)
171 {
172 return substitute(str, boost::regex(expr));
173 }
174
175 vector<string> queries;
176
177 string resub(string str)
178 {
179 // search for "KEYWORD expression"
180 boost::regex reg("\\{[0-9]+\\}");
181
182 boost::smatch match;
183 while (boost::regex_search(str, match, reg, boost::regex_constants::format_first_only))
184 {
185 const auto &len = match.length();
186 const auto &pos = match.position();
187 const auto &arg = match.str(); // Argument
188
189 const auto idx = atoi(arg.c_str()+1);
190
191 str.replace(pos, len, resub(vec[idx]));
192 }
193
194 return str;
195 }
196
197 void expression(string expr, size_t indent=0)
198 {
199 if (expr[0]=='{')
200 {
201 const auto idx = atoi(expr.c_str()+1);
202
203 // This is a subquery
204 if (vec[idx].substr(0,3)=="(/*")
205 {
206 cout << setw(indent) << ' ' << "(\n";
207 find_tokens(vec[idx], indent+4);
208 cout << setw(indent) << ' ' << ") ";
209 }
210 else
211 // This is just something to substitute back
212 if (vec[idx].substr(0,2)=="({")
213 {
214 cout << setw(indent) << ' ' << "(" << resub(vec[idx]) << ") ";
215 }
216 else
217 {
218 if (indent>0)
219 cout << setw(indent) << ' ';
220 cout << resub(vec[idx]);
221 }
222 }
223 else
224 {
225 if (indent>0)
226 cout << setw(indent) << ' ';
227 cout << resub(expr);
228 }
229 }
230
231 void find_tokens(string str, size_t indent=0)
232 {
233 // ( COMMENT )?( TOKEN )?(( {NNN} | NNN )( AS|ON ( {NNN}) ))?(,)?)
234 //regex reg("(\\/\\*\\ select\\#[0-9]+\\ \\*\\/\\ *)?([a-zA-Z ]+)?((\\{[0-9]+\\}|[0-9]+)(\\ ?([Aa][Ss]|[Oo][Nn])\\ ?(\\{[0-9]+\\}))?(,)?)");
235
236 const string _com = "\\/\\*\\ select\\#[0-9]+\\ \\*\\/\\ *";
237
238 const string _tok = "[a-zA-Z_ ]+";
239
240 const string _nnn = "\\{[0-9]+\\}|[0-9]+";
241
242 const string _as = "\\ ?([Aa][Ss])\\ ?";
243
244 // ( _nnn ) ( _as ( _nnn ))?(,)? // can also match noting in between two {NNN}
245 const string _exp = "("+_nnn+")" + "("+_as+"("+_nnn+"))?(,)?";
246
247 // Matche: ( _com )? ( ( _tok )? ( _exp ) | ( _tok ) )
248 boost::regex reg("("+_com+")?" + "(" + "("+_tok+")?"+"("+_exp+")" + "|" + "("+_tok+")" + ")");
249
250 boost::smatch match;
251 while (boost::regex_search(str, match, reg, boost::regex_constants::format_first_only))
252 {
253
254 const auto &com = match.str(1); // comment
255 const auto &tok1 = Tools::Trim(match.str(3)); // token with expression
256 const auto &arg1 = match.str(5); // argument 1
257 const auto &as = match.str(7); // as
258 const auto &arg2 = match.str(8); // argument 2
259 const auto &comma = match.str(9); // comma
260 const auto &tok2 = Tools::Trim(match.str(10)); // token without expression
261
262 if (!com.empty())
263 cout << setw(indent) << ' ' << "\033[34m" << com << "\033[0m" << '\n';
264
265 if (!tok1.empty())
266 cout << setw(indent) << ' ' << "\033[32m" << tok1 << "\033[0m" << '\n';
267 if (!tok2.empty())
268 cout << setw(indent) << ' ' << "\033[32m" << tok2 << "\033[0m" << '\n';
269
270 if (!arg1.empty())
271 {
272 expression(arg1, indent+4);
273
274 if (!as.empty())
275 cout << " \033[33m" << as << "\033[0m ";
276
277 if (!arg2.empty())
278 expression(arg2);
279
280 if (!comma.empty())
281 cout << ',';
282
283 cout << '\n';
284 }
285
286 str = str.substr(match.position()+match.length());
287 }
288 }
289
290
291 ExplainParser(const string &_sql) : sql(_sql)
292 {
293 // substitute all strings
294 sql = substitute(sql, "'[^']*'");
295
296 // substitute all escaped sequences (`something`.`something-else`)
297 sql = substitute(sql, "`[^`]*`(\\.`[^`]*`)*");
298
299 // substitute all paranthesis
300 sql = substitute(sql, "[a-zA-Z0-9_]*\\([^\\(\\)]*\\)");
301
302 //cout << sql << "\n\n";
303 find_tokens(sql);
304 cout << endl;
305 }
306};
307
308// Remove queries...
309void format(string sql)
310{
311 ExplainParser p(sql);
312
313 /*
314
315 SELECT
316 [ALL | DISTINCT | DISTINCTROW ]
317 [HIGH_PRIORITY]
318 [STRAIGHT_JOIN]
319 [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
320 [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
321 select_expr [, select_expr ...]
322 [FROM table_references
323 [PARTITION partition_list]
324 [WHERE where_condition]
325 [GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
326 [HAVING where_condition]
327 [WINDOW window_name AS (window_spec)
328 [, window_name AS (window_spec)] ...]
329 [ORDER BY {col_name | expr | position}
330 [ASC | DESC], ... [WITH ROLLUP]]
331 [LIMIT {[offset,] row_count | row_count OFFSET offset}]
332 [INTO OUTFILE 'file_name'
333 [CHARACTER SET charset_name]
334 export_options
335 | INTO DUMPFILE 'file_name'
336 | INTO var_name [, var_name]]
337 [FOR {UPDATE | SHARE} [OF tbl_name [, tbl_name] ...] [NOWAIT | SKIP LOCKED]
338 | LOCK IN SHARE MODE]]
339 */
340
341 /*
342table_references:
343 escaped_table_reference [, escaped_table_reference] ...
344
345escaped_table_reference:
346 table_reference
347 | { OJ table_reference }
348
349table_reference:
350 table_factor
351 | join_table
352
353table_factor:
354 tbl_name [PARTITION (partition_names)]
355 [[AS] alias] [index_hint_list]
356 | table_subquery [AS] alias [(col_list)]
357 | ( table_references )
358
359join_table:
360 table_reference [INNER | CROSS] JOIN table_factor [join_condition]
361 | table_reference STRAIGHT_JOIN table_factor
362 | table_reference STRAIGHT_JOIN table_factor ON conditional_expr
363 | table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition
364 | table_reference NATURAL [INNER | {LEFT|RIGHT} [OUTER]] JOIN table_factor
365
366join_condition:
367 ON conditional_expr
368 | USING (column_list)
369
370index_hint_list:
371 index_hint [, index_hint] ...
372
373index_hint:
374 USE {INDEX|KEY}
375 [FOR {JOIN|ORDER BY|GROUP BY}] ([index_list])
376 | IGNORE {INDEX|KEY}
377 [FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
378 | FORCE {INDEX|KEY}
379 [FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
380
381index_list:
382 index_name [, index_name] ...
383 */
384
385}
386
387int finish(Database &connection, const uint16_t &verbose, const bool &profiling, const bool &print_connection)
388{
389 if (verbose>0)
390 {
391 try
392 {
393 const auto resw =
394 connection.query("SHOW WARNINGS").store();
395
396 if (resw.num_rows()>0)
397 cout << "\n" << resw.num_rows() << " Warning(s) issued:\n\n";
398
399 for (size_t i=0; i<resw.num_rows(); i++)
400 {
401 const mysqlpp::Row &roww = resw[i];
402
403 cout << roww["Level"] << '[' << roww["Code"] << "]: ";
404 cout << roww["Message"] << '\n';
405 }
406 cout << endl;
407
408 }
409 catch (const exception &e)
410 {
411 cerr << "\nSHOW WARNINGS\n\n";
412 cerr << "SQL query failed:\n" << e.what() << endl;
413 return 1;
414 }
415 }
416
417 if (profiling)
418 {
419 try
420 {
421 const auto N =
422 connection.query("SHOW PROFILES").store().num_rows();
423
424 const auto resp =
425 connection.query("SHOW PROFILE ALL FOR QUERY "+to_string(verbose?N-1:N)).store();
426
427 cout << '\n';
428 cout << left;
429 cout << setw(26) << "Status" << ' ';
430 cout << right;
431 cout << setw(11) << "Duration" << ' ';
432 cout << setw(11) << "CPU User" << ' ';
433 cout << setw(11) << "CPU System" << '\n';
434 cout << "--------------------------------------------------------------\n";
435 for (size_t i=0; i<resp.num_rows(); i++)
436 {
437 const mysqlpp::Row &rowp = resp[i];
438
439 cout << left;
440 cout << setw(26) << rowp["Status"] << ' ';
441 cout << right;
442 cout << setw(11) << rowp["Duration"] << ' ';
443 cout << setw(11) << rowp["CPU_user"] << ' ';
444 cout << setw(11) << rowp["CPU_system"] << '\n';
445 }
446 cout << "--------------------------------------------------------------\n";
447 cout << endl;
448 }
449 catch (const exception &e)
450 {
451 cerr << "\nSHOW PROFILE ALL\n\n";
452 cerr << "SQL query failed:\n" << e.what() << '\n' <<endl;
453 return 2;
454 }
455 }
456
457 if (print_connection)
458 {
459 try
460 {
461 // Exchange _send and _received as it is the view of the server
462 const auto &res1 = connection.query("SHOW STATUS LIKE 'Bytes_%'").store();
463 cout << left << setw(16) << res1[1]["Variable_name"] << ' ' << Tools::Scientific(res1[0]["Value"]) << endl;
464 cout << left << setw(16) << res1[0]["Variable_name"] << ' ' << Tools::Scientific(res1[1]["Value"]) << endl;
465 cout << endl;
466 }
467 catch (const exception &e)
468 {
469 cerr << "\nSHOW STATUS LIKE 'Bytes_%'\n\n";
470 cerr << "SQL query failed:\n" << e.what() << endl;
471 return 3;
472 }
473 }
474
475 if (verbose>0)
476 cout << "Success!\n" << endl;
477 return 0;
478
479}
480
481int main(int argc, const char* argv[])
482{
483 Time start;
484
485 gROOT->SetBatch();
486
487 Configuration conf(argv[0]);
488 conf.SetPrintUsage(PrintUsage);
489 SetupConfiguration(conf);
490
491 if (!conf.DoParse(argc, argv))
492 return 127;
493
494 // ----------------------------- Evaluate options --------------------------
495 const string uri = conf.Get<string>("uri");
496 const string out = conf.Get<string>("out");
497 const string file = conf.Get<string>("file");
498 const string tree = conf.Get<string>("tree");
499 const bool force = conf.Get<bool>("force");
500 const bool ignorenull = conf.Get<bool>("ignore-null");
501 const bool update = conf.Get<bool>("update");
502 const bool display = conf.Get<bool>("display");
503 const string write = conf.Get<string>("write");
504 const bool noout = conf.Get<bool>("null");
505 const bool nofill = conf.Get<bool>("no-fill");
506 const bool explain = conf.Get<bool>("explain");
507 const bool profiling = conf.Get<bool>("profiling");
508 const uint16_t verbose = conf.Get<uint16_t>("verbose");
509 const uint16_t compression = conf.Get<uint16_t>("compression");
510 const string delimiter = conf.Has("delimiter") ? conf.Get<string>("delimiter") : "\t";
511
512 const bool copy_all = conf.Get<bool>("copy-all");
513 const bool copy_shabang = conf.Get<bool>("copy-shabang");
514 const bool copy_header = copy_all || conf.Get<bool>("copy-header");
515 const bool copy_query = copy_all || conf.Get<bool>("copy-query");
516 const bool copy_comments = copy_all || conf.Get<bool>("copy-comments");
517
518 const vector<string> _ignore = conf.Vec<string>("ignore");
519 const bool print_connection = conf.Get<bool>("print-connection");
520 //const vector<Map> mymap = conf.Vec<Map>("map");
521
522 // ----------------------- Setup splitting ---------------------------------
523
524 vector<uint16_t> split_seq = conf.Vec<uint16_t>("split-sequence");
525 vector<double> split_quant = conf.Vec<double>("split-quantile");
526
527 if (!split_seq.empty() && !split_quant.empty())
528 throw runtime_error("Only splitting by --split-sequence or --split-quantile is allowed.");
529
530 const size_t num_split = ::max(split_seq.size(), split_quant.size()+1);
531
532 map<size_t, size_t> split_lut;
533 for (size_t i=0; i<split_seq.size(); i++)
534 {
535 const size_t sz = split_lut.size();
536 for (size_t j=0; j<split_seq[i]; j++)
537 split_lut.emplace(j+sz, i);
538 }
539
540 for (size_t i=0; i<split_quant.size(); i++)
541 if (split_quant[i]<0 || split_quant[i]>=1)
542 throw runtime_error("Splitting quantiles must be in the range [0;1)");
543
544 for (size_t i=1; i<split_quant.size(); i++)
545 {
546 if (split_quant[i]<=split_quant[i-1])
547 throw runtime_error("Splitting quantiles must be in increasing order.");
548 }
549
550 // -------------------------------------------------------------------------
551
552 const auto vars = conf.GetWildcardOptions("var.*");
553
554 vector<string> variables;
555 for (auto var=vars.cbegin(); var!=vars.cend(); var++)
556 variables.emplace_back('@'+var->substr(4)+":="+Tools::Trim(conf.Get<string>(*var)));
557
558 // -------------------------------------------------------------------------
559
560 if (verbose>0)
561 {
562 cout << "\n------------------------ Rootify SQL -------------------------" << endl;
563 cout << "Start Time: " << Time::sql << Time(Time::local) << endl;
564 }
565
566 string query = conf.Get<string>("query");
567 if (query.empty())
568 {
569 if (verbose>0)
570 cout << "Reading query from file '" << file << "'." << endl;
571
572 ifstream fin(file);
573 if (!fin)
574 {
575 cerr << "Could not open query in '" << file << "': " << strerror(errno) << endl;
576 return 4;
577 }
578
579 getline(fin, query, (char)fin.eof());
580 }
581
582 if (query.empty())
583 {
584 cerr << "No query specified." << endl;
585 return 5;
586 }
587
588 // -------------------------------------------------------------------------
589
590 map<string, vector<string>> envs;
591
592 const auto &envs1 = conf.GetWildcardOptions("env.*");
593 for (auto env=envs1.cbegin(); env!=envs1.cend(); env++)
594 envs[env->substr(4)] = conf.Vec<string>(*env);
595
596 const auto &envs2 = conf.GetWildcardOptions("list.*");
597 for (auto env=envs2.cbegin(); env!=envs2.cend(); env++)
598 {
599 const string fname = conf.Get<string>(*env);
600 const string &ident = env->substr(5);
601
602 ifstream fin(fname);
603 if (!fin)
604 {
605 cerr << "Could not open environment in '" << fname << "' for ${" << ident << "}: " << strerror(errno) << endl;
606 return 6;
607 }
608 for (string line; getline(fin, line); )
609 {
610 const auto &l = Tools::Trim(line);
611 if (!l.empty() && l[0]!='#')
612 envs[ident].push_back(line);
613 }
614
615 if (verbose>0)
616 cout << "Found " << envs[ident].size() << " list element(s) for ${" << ident << "}" << endl;
617 }
618
619 for (auto env=envs.cbegin(); env!=envs.cend(); env++)
620 {
621 boost::regex rexpr("\\$(\\{"+env->first+"\\}|"+env->first+"\\b)");
622 query = boost::regex_replace(query, rexpr, boost::join(env->second, ", "));
623 }
624
625 // -------------------------- Check for file permssion ---------------------
626 // Strictly speaking, checking for write permission and existance is not necessary,
627 // but it is convenient that the user does not find out that it failed after
628 // waiting long for the query result
629 //
630
631 // I am using root here instead of boost to be
632 // consistent with the access pattern by TFile
633 TString path(noout?"/dev/null":out.c_str());
634 gSystem->ExpandPathName(path);
635
636 if (!noout)
637 {
638 FileStat_t stat;
639 const Int_t exist = !gSystem->GetPathInfo(path, stat);
640 const Bool_t _write = !gSystem->AccessPathName(path, kWritePermission) && R_ISREG(stat.fMode);
641
642 if ((update && !exist) || (update && exist && !_write) || (force && exist && !_write))
643 {
644 cerr << "File '" << path << "' is not writable." << endl;
645 return 7;
646 }
647
648 if (!update && !force && exist)
649 {
650 cerr << "File '" << path << "' already exists." << endl;
651 return 8;
652 }
653 }
654
655 Time start2;
656
657 // --------------------------- Connect to database -------------------------------------------------
658
659 if (*query.rbegin()!='\n')
660 query += '\n';
661
662 if (verbose>0)
663 {
664 cout << "Connecting to database...\n";
665 cout << "Client Version: " << mysqlpp::Connection().client_version() << endl;
666 }
667
668 Database connection(uri); // Keep alive while fetching rows
669
670 if (verbose>0)
671 cout << "Server Version: " << connection.server_version() << endl;
672
673 if (print_connection)
674 {
675 try
676 {
677 const auto &res1 = connection.query("SHOW STATUS LIKE 'Compression'").store();
678 cout << "Compression of database connection is " << string(res1[0][1]) << endl;
679
680 const auto &res2 = connection.query("SHOW STATUS LIKE 'Ssl_cipher'").store();
681 cout << "Connection to databases is " << (string(res2[0][1]).empty()?"UNENCRYPTED":"ENCRYPTED ("+string(res2[0][1])+")") << endl;
682 }
683 catch (const exception &e)
684 {
685 cerr << "\nSHOW STATUS LIKE 'Compression'\n\n";
686 cerr << "SQL query failed:\n" << e.what() << endl;
687 return 9;
688 }
689 }
690
691 try
692 {
693 if (profiling)
694 connection.query("SET PROFILING=1").execute();
695 }
696 catch (const exception &e)
697 {
698 cerr << "\nSET profiling=1\n\n";
699 cerr << "SQL query failed:\n" << e.what() << endl;
700 return 10;
701 }
702
703 // -------------------------- Set user defined variables -------------------
704 if (variables.size()>0)
705 {
706 if (verbose>0)
707 cout << "Setting user defined variables..." << endl;
708
709 const string varset =
710 "SET\n "+boost::algorithm::join(variables, ",\n ");
711
712 try
713 {
714 connection.query(varset).execute();
715 }
716 catch (const exception &e)
717 {
718 cerr << '\n' << varset << "\n\n";
719 cerr << "SQL query failed:\n" << e.what() << endl;
720 return 11;
721 }
722
723 if (verbose>2)
724 cout << '\n' << varset << '\n' << endl;
725 }
726
727 // ------------------------- Explain query if requested --------------------
728
729 if (explain)
730 {
731 try
732 {
733 const auto res0 =
734 connection.query("EXPLAIN FORMAT=JSON "+query).store();
735
736 cout << res0[0][0] << endl;
737 cout << endl;
738
739 const mysqlpp::StoreQueryResult res1 =
740 connection.query("EXPLAIN "+query).store();
741
742 for (size_t i=0; i<res1.num_rows(); i++)
743 {
744 const mysqlpp::Row &row = res1[i];
745
746 cout << "\nid : " << row["id"];
747 cout << "\nselect type : " << row["select_type"];
748
749 if (!row["table"].is_null())
750 cout << "\ntable : " << row["table"];
751
752 if (!row["partitions"].is_null())
753 cout << "\npartitions : " << row["partitions"];
754
755 if (!row["key"].is_null())
756 cout << "\nselected key : " << row["key"] << " [len=" << row["key_len"] << "] out of (" << row["possible_keys"] << ")";
757
758 if (!row["type"].is_null())
759 cout << "\njoin type : " << row["type"];
760
761 //if (!row["possible_keys"].is_null())
762 // cout << "\npossible_keys: " << row["possible_keys"];
763
764 //if (!row["key_len"].is_null())
765 // cout << "\nkey_len : " << row["key_len"];
766
767 if (!row["ref"].is_null())
768 cout << "\nref : (" << row["ref"] << ") compared to the index";
769
770 if (!row["rows"].is_null())
771 cout << "\nrows : " << row["rows"];
772
773 if (!row["filtered"].is_null())
774 cout << "\nfiltered : " << row["filtered"];
775
776 if (!row["extra"].is_null())
777 cout << "\nExtra : " << row["extra"];
778
779 cout << endl;
780 }
781
782 cout << endl;
783
784 const mysqlpp::StoreQueryResult res2 =
785 connection.query("SHOW WARNINGS").store();
786
787 for (size_t i=0; i<res2.num_rows(); i++)
788 {
789 const mysqlpp::Row &row = res2[i];
790
791 // 1003 //
792 cout << row["Level"] << '[' << row["Code"] << "]:\n";
793 if (uint32_t(row["Code"])==1003)
794 format(row["Message"].c_str());
795 else
796 cout << row["Message"] << '\n' << endl;
797
798 }
799
800 }
801 catch (const exception &e)
802 {
803 cerr << '\n' << query << "\n\n";
804 cerr << "SQL query failed:\n" << e.what() << endl;
805 return 12;
806 }
807
808 return 0;
809 }
810
811 // -------------------------- Request data from database -------------------
812 if (verbose>0)
813 cout << "Requesting data... please be patient!" << endl;
814
815 if (verbose>2)
816 cout << '\n' << query << endl;
817
818 const mysqlpp::UseQueryResult res =
819 connection.query(query).use();
820
821 // -------------------------------------------------------------------------
822
823 if (verbose>0)
824 {
825 cout << "Opening file '" << path << "' [compression=" << compression << "]...\n";
826 cout << "Writing data to tree '" << tree << "'" << (nofill?" (--skipped--)":"") << endl;
827 if (num_split)
828 {
829 cout << "Splitting configured " << (split_seq.empty()?"randomly":"in sequence") << " into " << num_split << " branches." << endl;
830 if (!split_quant.empty())
831 cout << "Seed value configured as " << conf.Get<uint64_t>("seed") << "." << endl;
832 }
833 }
834
835 // ----------------------------- Open output file --------------------------
836 TFile tfile(path, update?"UPDATE":(force?"RECREATE":"CREATE"), "Rootify SQL", compression);
837 if (tfile.IsZombie())
838 return 13;
839
840 // -------------------------------------------------------------------------
841
842 // get the first row to get the field description
843 mysqlpp::Row row = res.fetch_row();
844 if (!row)
845 {
846 cerr << "Empty set returned... nothing to write." << endl;
847 return finish(connection, verbose, profiling, print_connection)+20;
848 }
849
850 if (verbose>0)
851 cout << "Trying to setup " << row.size() << " branches..." << endl;
852
853 if (verbose>1)
854 cout << endl;
855
856 const mysqlpp::FieldNames &l = *row.field_list().list;
857
858 vector<double> buf(l.size());
859 vector<uint8_t> typ(l.size(),'n'); // n=number [double], d is used for DateTime
860
861 UInt_t cols = 0;
862
863 // IMPLEMENT FILE SPLITTING!
864 // OpenFile(tree, query)
865 // SetupColumns
866 // WriteRow
867 // CloseFile
868
869 // Ratio[3]: 50%, 20%, 30%
870 // File[x3]: root, cout, fout
871
872
873 // -------------------- Configure branches of TTree ------------------------
874 vector<TTree*> ttree;
875
876 if (num_split==0)
877 ttree.emplace_back(new TTree(tree.c_str(), query.c_str()));
878 else
879 for (size_t i=0; i<num_split; i++)
880 ttree.emplace_back(new TTree((tree+"["+to_string(i)+"]").c_str(), query.c_str()));
881
882 size_t skipat = 0;
883 size_t skipreg = 0;
884 for (size_t i=0; i<l.size(); i++)
885 {
886 const string t = row[i].type().sql_name();
887
888 if (t.find("DATETIME")!=string::npos)
889 typ[i] = 'd';
890 else
891 if (t.find("DATE")!=string::npos)
892 typ[i] = 'D';
893 else
894 if (t.find("TIME")!=string::npos)
895 typ[i] = 'T';
896 else
897 if (t.find("VARCHAR")!=string::npos)
898 typ[i] = 'V';
899 else
900 if (t.find("CHAR")!=string::npos)
901 typ[i] = 'C';
902
903 bool found = false;
904 for (auto pattern=_ignore.cbegin(); pattern!=_ignore.cend(); pattern++)
905 {
906 if (boost::regex_match(l[i], boost::regex(*pattern)))
907 {
908 found = true;
909 typ[i] = '-';
910 skipreg++;
911 break;
912 }
913 }
914
915 if (l[i][0]=='@')
916 {
917 typ[i] = '@';
918 skipat++;
919 }
920
921 const bool use = l[i][0]!='@' && typ[i]!='V' && typ[i]!='C' && !found;
922
923 if (verbose>1)
924 cout << (use?" + ":" - ") << l[i].c_str() << " [" << t << "] {" << typ[i] << "}\n";
925
926 if (use)
927 {
928 // string name = l[i];
929 // for (const auto &m: mymap)
930 // name = boost::regex_replace(l[i], boost::regex(m.first), m.second);
931
932 for (auto it=ttree.begin(); it!=ttree.end(); it++)
933 it[0]->Branch(l[i].c_str(), buf.data()+i);
934 cols++;
935 }
936 }
937 // -------------------------------------------------------------------------
938
939 if (verbose>1)
940 cout << endl;
941 if (verbose>0)
942 {
943 if (skipreg)
944 cout << skipreg << " branches skipped due to ignore list." << endl;
945 if (skipat)
946 cout << skipat << " branches skipped due to name starting with @." << endl;
947 cout << "Configured " << cols << " branches.\nFilling branches..." << endl;
948 }
949
950 // ------------------------- Open the ascii files --------------------------
951
952 vector<ofstream> fout;
953 if (!write.empty())
954 {
955 vector<string> names;
956 if (num_split==0)
957 names.emplace_back(write);
958 else
959 for (size_t i=0; i<num_split; i++)
960 names.emplace_back(write+"-"+to_string(i));
961
962 for (auto it=names.cbegin(); it!=names.cend(); it++)
963 {
964 fout.emplace_back(*it);
965 if (!*fout.rbegin())
966 cout << "WARNING: Writing to '" << write << "' failed: " << strerror(errno) << endl;
967 }
968 }
969
970 // ----------------------- Prepare the ascii comment -----------------------
971
972 string contents;
973
974 istringstream istr(query);
975 size_t line = 0;
976 bool header = true;
977 while (istr)
978 {
979 string ibuf;
980 getline(istr, ibuf);
981 const string sbuf = Tools::Trim(ibuf);
982
983 const bool shabang = line==0 && ibuf[0]=='#' && ibuf[1]=='!';
984 const bool comment = sbuf[0]=='#' && !shabang;
985 const bool isquery = !shabang && !comment;
986 if (isquery)
987 header = false;
988
989 line++;
990
991 if ((copy_shabang && shabang) ||
992 (copy_header && comment && header) ||
993 (copy_query && isquery) ||
994 (copy_comments && comment && !header))
995 contents += '#' + ibuf + '\n';
996 }
997
998 // ----------------------- Write the ascii headers -------------------------
999
1000 ostringstream htxt;
1001 if (display || !fout.empty())
1002 htxt << row.field_list(delimiter.c_str());
1003
1004 if (display)
1005 {
1006 cout << endl;
1007 cout << contents << endl;
1008 cout << "# " << htxt.str() << endl;
1009 }
1010 for (auto ff=fout.begin(); ff!=fout.end(); ff++)
1011 {
1012 *ff << contents;
1013 *ff << "# " << htxt.str() << endl;
1014 }
1015
1016 // ---------------------- Fill TTree with DB data --------------------------
1017
1018 const uniform_real_distribution<double> distribution(0,1);
1019 mt19937_64 generator;
1020 generator.seed(conf.Get<uint64_t>("seed"));
1021 auto rndm = bind(distribution, generator);
1022
1023 size_t count = 0;
1024 size_t skip = 0;
1025 do
1026 {
1027 size_t index = 0;
1028 if (!split_lut.empty())
1029 index = split_lut[count % split_lut.size()];
1030 if (!split_quant.empty())
1031 {
1032 const float r = rndm();
1033 for (; r>=split_quant[index]; index++)
1034 if (index==split_quant.size())
1035 break;
1036 }
1037
1038 count++;
1039
1040 ostringstream rtxt;
1041 if (display || !fout.empty())
1042 rtxt << row.value_list(delimiter.c_str(), mysqlpp::do_nothing);
1043
1044 if (display)
1045 cout << rtxt.str() << '\n';
1046 if (!fout.empty())
1047 fout[index] << rtxt.str() << '\n';
1048
1049 size_t idx=0;
1050 for (auto col=row.begin(); col!=row.end(); col++, idx++)
1051 {
1052 if (!ignorenull && col->is_null())
1053 {
1054 skip++;
1055 break;
1056 }
1057
1058 switch (typ[idx])
1059 {
1060 case 'd':
1061 buf[idx] = time_t((mysqlpp::DateTime)(*col));
1062 break;
1063
1064 case 'D':
1065 buf[idx] = time_t((mysqlpp::Date)(*col));
1066 break;
1067
1068 case 'T':
1069 buf[idx] = time_t((mysqlpp::Time)(*col));
1070 break;
1071
1072 case 'V':
1073 case 'C':
1074 case '-':
1075 case '@':
1076 break;
1077
1078 default:
1079 buf[idx] = atof(col->c_str());
1080 }
1081 }
1082
1083 if (idx==row.size() && !nofill)
1084 ttree[index]->Fill();
1085
1086 row = res.fetch_row();
1087
1088
1089 } while (row);
1090
1091 // -------------------------------------------------------------------------
1092
1093 if (display)
1094 cout << '\n' << endl;
1095
1096 if (verbose>0)
1097 {
1098 cout << count << " rows fetched." << endl;
1099 if (skip>0)
1100 cout << skip << " rows skipped due to NULL field." << endl;
1101
1102 for (size_t i=0; i<ttree.size(); i++)
1103 cout << ttree[i]->GetEntries() << " rows filled into tree #" << i << "." << endl;
1104 }
1105
1106 for (auto it=ttree.begin(); it!=ttree.end(); it++)
1107 (*it)->Write();
1108 tfile.Close();
1109
1110 if (verbose>0)
1111 {
1112 const auto sec = Time().UnixTime()-start.UnixTime();
1113
1114 cout << Tools::Scientific(tfile.GetSize()) << "B written to disk.\n";
1115 cout << "File closed.\n";
1116 cout << "Execution time: " << sec << "s ";
1117 cout << "(" << Tools::Fractional(sec/count) << "s/row)\n";
1118 cout << "--------------------------------------------------------------" << endl;
1119 }
1120
1121 return finish(connection, verbose, profiling, print_connection);
1122}
Note: See TracBrowser for help on using the repository browser.