Client.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. #include <boost/algorithm/string/join.hpp>
  2. #include <cstdlib>
  3. #include <fcntl.h>
  4. #include <map>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <memory>
  8. #include <optional>
  9. #include <Common/ThreadStatus.h>
  10. #include <Common/scope_guard_safe.h>
  11. #include <boost/program_options.hpp>
  12. #include <boost/algorithm/string/replace.hpp>
  13. #include <filesystem>
  14. #include <string>
  15. #include "Client.h"
  16. #include "Client/ConnectionString.h"
  17. #include "Core/Protocol.h"
  18. #include "Parsers/formatAST.h"
  19. #include <base/find_symbols.h>
  20. #include <Access/AccessControl.h>
  21. #include "config_version.h"
  22. #include <Common/Exception.h>
  23. #include <Common/formatReadable.h>
  24. #include <Common/TerminalSize.h>
  25. #include <Common/Config/configReadClient.h>
  26. #include <Core/QueryProcessingStage.h>
  27. #include <Columns/ColumnString.h>
  28. #include <Poco/Util/Application.h>
  29. #include <IO/ReadBufferFromString.h>
  30. #include <IO/ReadHelpers.h>
  31. #include <IO/UseSSL.h>
  32. #include <IO/WriteBufferFromOStream.h>
  33. #include <IO/WriteHelpers.h>
  34. #include <IO/copyData.h>
  35. #include <Parsers/ASTCreateQuery.h>
  36. #include <Parsers/ASTDropQuery.h>
  37. #include <Parsers/ASTSetQuery.h>
  38. #include <Parsers/ASTUseQuery.h>
  39. #include <Parsers/ASTInsertQuery.h>
  40. #include <Parsers/ASTSelectQuery.h>
  41. #include <Processors/Transforms/getSourceFromASTInsertQuery.h>
  42. #include <Interpreters/InterpreterSetQuery.h>
  43. #include <Functions/registerFunctions.h>
  44. #include <AggregateFunctions/registerAggregateFunctions.h>
  45. #include <Formats/registerFormats.h>
  46. #ifndef __clang__
  47. #pragma GCC optimize("-fno-var-tracking-assignments")
  48. #endif
  49. namespace fs = std::filesystem;
  50. using namespace std::literals;
  51. namespace DB
  52. {
  53. namespace ErrorCodes
  54. {
  55. extern const int BAD_ARGUMENTS;
  56. extern const int UNKNOWN_PACKET_FROM_SERVER;
  57. extern const int SYNTAX_ERROR;
  58. extern const int TOO_DEEP_RECURSION;
  59. extern const int NETWORK_ERROR;
  60. extern const int AUTHENTICATION_FAILED;
  61. extern const int NO_ELEMENTS_IN_CONFIG;
  62. }
  63. void Client::processError(const String & query) const
  64. {
  65. if (server_exception)
  66. {
  67. fmt::print(stderr, "Received exception from server (version {}):\n{}\n",
  68. server_version,
  69. getExceptionMessage(*server_exception, print_stack_trace, true));
  70. if (is_interactive)
  71. {
  72. fmt::print(stderr, "\n");
  73. }
  74. else
  75. {
  76. fmt::print(stderr, "(query: {})\n", query);
  77. }
  78. }
  79. if (client_exception)
  80. {
  81. fmt::print(stderr, "Error on processing query: {}\n", client_exception->message());
  82. if (is_interactive)
  83. {
  84. fmt::print(stderr, "\n");
  85. }
  86. else
  87. {
  88. fmt::print(stderr, "(query: {})\n", query);
  89. }
  90. }
  91. // A debug check -- at least some exception must be set, if the error
  92. // flag is set, and vice versa.
  93. assert(have_error == (client_exception || server_exception));
  94. }
  95. void Client::showWarnings()
  96. {
  97. try
  98. {
  99. std::vector<String> messages = loadWarningMessages();
  100. if (!messages.empty())
  101. {
  102. std::cout << "Warnings:" << std::endl;
  103. for (const auto & message : messages)
  104. std::cout << " * " << message << std::endl;
  105. std::cout << std::endl;
  106. }
  107. }
  108. catch (...)
  109. {
  110. /// Ignore exception
  111. }
  112. }
  113. void Client::parseConnectionsCredentials()
  114. {
  115. /// It is not possible to correctly handle multiple --host --port options.
  116. if (hosts_and_ports.size() >= 2)
  117. return;
  118. std::optional<String> host;
  119. if (hosts_and_ports.empty())
  120. {
  121. if (config().has("host"))
  122. host = config().getString("host");
  123. }
  124. else
  125. {
  126. host = hosts_and_ports.front().host;
  127. }
  128. String connection;
  129. if (config().has("connection"))
  130. connection = config().getString("connection");
  131. else
  132. connection = host.value_or("localhost");
  133. Strings keys;
  134. config().keys("connections_credentials", keys);
  135. bool connection_found = false;
  136. for (const auto & key : keys)
  137. {
  138. const String & prefix = "connections_credentials." + key;
  139. const String & connection_name = config().getString(prefix + ".name", "");
  140. if (connection_name != connection)
  141. continue;
  142. connection_found = true;
  143. String connection_hostname;
  144. if (config().has(prefix + ".hostname"))
  145. connection_hostname = config().getString(prefix + ".hostname");
  146. else
  147. connection_hostname = connection_name;
  148. if (hosts_and_ports.empty())
  149. config().setString("host", connection_hostname);
  150. if (config().has(prefix + ".port") && hosts_and_ports.empty())
  151. config().setInt("port", config().getInt(prefix + ".port"));
  152. if (config().has(prefix + ".secure") && !config().has("secure"))
  153. config().setBool("secure", config().getBool(prefix + ".secure"));
  154. if (config().has(prefix + ".user") && !config().has("user"))
  155. config().setString("user", config().getString(prefix + ".user"));
  156. if (config().has(prefix + ".password") && !config().has("password"))
  157. config().setString("password", config().getString(prefix + ".password"));
  158. if (config().has(prefix + ".database") && !config().has("database"))
  159. config().setString("database", config().getString(prefix + ".database"));
  160. if (config().has(prefix + ".history_file") && !config().has("history_file"))
  161. {
  162. String history_file = config().getString(prefix + ".history_file");
  163. if (history_file.starts_with("~") && !home_path.empty())
  164. history_file = home_path + "/" + history_file.substr(1);
  165. config().setString("history_file", history_file);
  166. }
  167. }
  168. if (config().has("connection") && !connection_found)
  169. throw Exception(ErrorCodes::NO_ELEMENTS_IN_CONFIG, "No such connection '{}' in connections_credentials", connection);
  170. }
  171. /// Make query to get all server warnings
  172. std::vector<String> Client::loadWarningMessages()
  173. {
  174. /// Older server versions cannot execute the query loading warnings.
  175. constexpr UInt64 min_server_revision_to_load_warnings = DBMS_MIN_PROTOCOL_VERSION_WITH_VIEW_IF_PERMITTED;
  176. if (server_revision < min_server_revision_to_load_warnings)
  177. return {};
  178. std::vector<String> messages;
  179. connection->sendQuery(connection_parameters.timeouts,
  180. "SELECT * FROM viewIfPermitted(SELECT message FROM system.warnings ELSE null('message String'))",
  181. {} /* query_parameters */,
  182. "" /* query_id */,
  183. QueryProcessingStage::Complete,
  184. &global_context->getSettingsRef(),
  185. &global_context->getClientInfo(), false, {});
  186. while (true)
  187. {
  188. Packet packet = connection->receivePacket();
  189. switch (packet.type)
  190. {
  191. case Protocol::Server::Data:
  192. if (packet.block)
  193. {
  194. const ColumnString & column = typeid_cast<const ColumnString &>(*packet.block.getByPosition(0).column);
  195. size_t rows = packet.block.rows();
  196. for (size_t i = 0; i < rows; ++i)
  197. messages.emplace_back(column[i].get<String>());
  198. }
  199. continue;
  200. case Protocol::Server::Progress:
  201. case Protocol::Server::ProfileInfo:
  202. case Protocol::Server::Totals:
  203. case Protocol::Server::Extremes:
  204. case Protocol::Server::Log:
  205. continue;
  206. case Protocol::Server::Exception:
  207. packet.exception->rethrow();
  208. return messages;
  209. case Protocol::Server::EndOfStream:
  210. return messages;
  211. case Protocol::Server::ProfileEvents:
  212. continue;
  213. default:
  214. throw Exception(ErrorCodes::UNKNOWN_PACKET_FROM_SERVER, "Unknown packet {} from server {}",
  215. packet.type, connection->getDescription());
  216. }
  217. }
  218. }
  219. void Client::initialize(Poco::Util::Application & self)
  220. {
  221. Poco::Util::Application::initialize(self);
  222. const char * home_path_cstr = getenv("HOME"); // NOLINT(concurrency-mt-unsafe)
  223. if (home_path_cstr)
  224. home_path = home_path_cstr;
  225. configReadClient(config(), home_path);
  226. /** getenv is thread-safe in Linux glibc and in all sane libc implementations.
  227. * But the standard does not guarantee that subsequent calls will not rewrite the value by returned pointer.
  228. *
  229. * man getenv:
  230. *
  231. * As typically implemented, getenv() returns a pointer to a string within the environment list.
  232. * The caller must take care not to modify this string, since that would change the environment of
  233. * the process.
  234. *
  235. * The implementation of getenv() is not required to be reentrant. The string pointed to by the return value of getenv()
  236. * may be statically allocated, and can be modified by a subsequent call to getenv(), putenv(3), setenv(3), or unsetenv(3).
  237. */
  238. const char * env_user = getenv("CLICKHOUSE_USER"); // NOLINT(concurrency-mt-unsafe)
  239. if (env_user && !config().has("user"))
  240. config().setString("user", env_user);
  241. const char * env_password = getenv("CLICKHOUSE_PASSWORD"); // NOLINT(concurrency-mt-unsafe)
  242. if (env_password && !config().has("password"))
  243. config().setString("password", env_password);
  244. parseConnectionsCredentials();
  245. // global_context->setApplicationType(Context::ApplicationType::CLIENT);
  246. global_context->setQueryParameters(query_parameters);
  247. /// settings and limits could be specified in config file, but passed settings has higher priority
  248. for (const auto & setting : global_context->getSettingsRef().allUnchanged())
  249. {
  250. const auto & name = setting.getName();
  251. if (config().has(name))
  252. global_context->setSetting(name, config().getString(name));
  253. }
  254. /// Set path for format schema files
  255. if (config().has("format_schema_path"))
  256. global_context->setFormatSchemaPath(fs::weakly_canonical(config().getString("format_schema_path")));
  257. }
  258. int Client::main(const std::vector<std::string> & /*args*/)
  259. try
  260. {
  261. UseSSL use_ssl;
  262. auto & thread_status = MainThreadStatus::getInstance();
  263. setupSignalHandler();
  264. std::cout << std::fixed << std::setprecision(3);
  265. std::cerr << std::fixed << std::setprecision(3);
  266. registerFormats();
  267. registerFunctions();
  268. registerAggregateFunctions();
  269. processConfig();
  270. initTtyBuffer(toProgressOption(config().getString("progress", "default")));
  271. {
  272. // All that just to set DB::CurrentThread::get().getGlobalContext()
  273. // which is required for client timezone (pushed from server) to work.
  274. auto thread_group = std::make_shared<ThreadGroup>();
  275. const_cast<ContextWeakPtr&>(thread_group->global_context) = global_context;
  276. thread_status.attachToGroup(thread_group, false);
  277. }
  278. /// Includes delayed_interactive.
  279. if (is_interactive)
  280. {
  281. clearTerminal();
  282. showClientVersion();
  283. }
  284. try
  285. {
  286. connect();
  287. }
  288. catch (const Exception & e)
  289. {
  290. if (e.code() != DB::ErrorCodes::AUTHENTICATION_FAILED ||
  291. config().has("password") ||
  292. config().getBool("ask-password", false) ||
  293. !is_interactive)
  294. throw;
  295. config().setBool("ask-password", true);
  296. connect();
  297. }
  298. /// Show warnings at the beginning of connection.
  299. if (is_interactive && !config().has("no-warnings"))
  300. showWarnings();
  301. /// Set user password complexity rules
  302. auto & access_control = global_context->getAccessControl();
  303. access_control.setPasswordComplexityRules(connection->getPasswordComplexityRules());
  304. if (is_interactive && !delayed_interactive)
  305. {
  306. runInteractive();
  307. }
  308. else
  309. {
  310. connection->setDefaultDatabase(connection_parameters.default_database);
  311. runNonInteractive();
  312. // If exception code isn't zero, we should return non-zero return
  313. // code anyway.
  314. const auto * exception = server_exception ? server_exception.get() : client_exception.get();
  315. if (exception)
  316. {
  317. return exception->code() != 0 ? exception->code() : -1;
  318. }
  319. if (have_error)
  320. {
  321. // Shouldn't be set without an exception, but check it just in
  322. // case so that at least we don't lose an error.
  323. return -1;
  324. }
  325. if (delayed_interactive)
  326. runInteractive();
  327. }
  328. return 0;
  329. }
  330. catch (const Exception & e)
  331. {
  332. bool need_print_stack_trace = config().getBool("stacktrace", false) && e.code() != ErrorCodes::NETWORK_ERROR;
  333. std::cerr << getExceptionMessage(e, need_print_stack_trace, true) << std::endl << std::endl;
  334. /// If exception code isn't zero, we should return non-zero return code anyway.
  335. return e.code() ? e.code() : -1;
  336. }
  337. catch (...)
  338. {
  339. std::cerr << getCurrentExceptionMessage(false) << std::endl;
  340. return getCurrentExceptionCode();
  341. }
  342. void Client::connect()
  343. {
  344. String server_name;
  345. UInt64 server_version_major = 0;
  346. UInt64 server_version_minor = 0;
  347. UInt64 server_version_patch = 0;
  348. if (hosts_and_ports.empty())
  349. {
  350. String host = config().getString("host", "localhost");
  351. UInt16 port = ConnectionParameters::getPortFromConfig(config());
  352. hosts_and_ports.emplace_back(HostAndPort{host, port});
  353. }
  354. for (size_t attempted_address_index = 0; attempted_address_index < hosts_and_ports.size(); ++attempted_address_index)
  355. {
  356. try
  357. {
  358. connection_parameters = ConnectionParameters(
  359. config(), hosts_and_ports[attempted_address_index].host, hosts_and_ports[attempted_address_index].port);
  360. if (is_interactive)
  361. std::cout << "Connecting to "
  362. << (!connection_parameters.default_database.empty() ? "database " + connection_parameters.default_database + " at "
  363. : "")
  364. << connection_parameters.host << ":" << connection_parameters.port
  365. << (!connection_parameters.user.empty() ? " as user " + connection_parameters.user : "") << "." << std::endl;
  366. connection = Connection::createConnection(connection_parameters, global_context);
  367. if (max_client_network_bandwidth)
  368. {
  369. ThrottlerPtr throttler = std::make_shared<Throttler>(max_client_network_bandwidth, 0, "");
  370. connection->setThrottler(throttler);
  371. }
  372. connection->getServerVersion(
  373. connection_parameters.timeouts, server_name, server_version_major, server_version_minor, server_version_patch, server_revision);
  374. config().setString("host", connection_parameters.host);
  375. config().setInt("port", connection_parameters.port);
  376. break;
  377. }
  378. catch (const Exception & e)
  379. {
  380. if (e.code() == DB::ErrorCodes::AUTHENTICATION_FAILED)
  381. {
  382. /// This problem can't be fixed with reconnection so it is not attempted
  383. throw;
  384. }
  385. else
  386. {
  387. if (attempted_address_index == hosts_and_ports.size() - 1)
  388. throw;
  389. if (is_interactive)
  390. {
  391. std::cerr << "Connection attempt to database at "
  392. << connection_parameters.host << ":" << connection_parameters.port
  393. << " resulted in failure"
  394. << std::endl
  395. << getExceptionMessage(e, false)
  396. << std::endl
  397. << "Attempting connection to the next provided address"
  398. << std::endl;
  399. }
  400. }
  401. }
  402. }
  403. server_version = toString(server_version_major) + "." + toString(server_version_minor) + "." + toString(server_version_patch);
  404. load_suggestions = is_interactive && (server_revision >= Suggest::MIN_SERVER_REVISION) && !config().getBool("disable_suggestion", false);
  405. if (server_display_name = connection->getServerDisplayName(connection_parameters.timeouts); server_display_name.empty())
  406. server_display_name = config().getString("host", "localhost");
  407. if (is_interactive)
  408. {
  409. std::cout << "Connected to " << server_name << " server version " << server_version << " revision " << server_revision << "."
  410. << std::endl << std::endl;
  411. auto client_version_tuple = std::make_tuple(VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
  412. auto server_version_tuple = std::make_tuple(server_version_major, server_version_minor, server_version_patch);
  413. if (client_version_tuple < server_version_tuple)
  414. {
  415. std::cout << "ClickHouse client version is older than ClickHouse server. "
  416. << "It may lack support for new features." << std::endl
  417. << std::endl;
  418. }
  419. else if (client_version_tuple > server_version_tuple)
  420. {
  421. std::cout << "ClickHouse server version is older than ClickHouse client. "
  422. << "It may indicate that the server is out of date and can be upgraded." << std::endl
  423. << std::endl;
  424. }
  425. }
  426. if (!global_context->getSettingsRef().use_client_time_zone)
  427. {
  428. const auto & time_zone = connection->getServerTimezone(connection_parameters.timeouts);
  429. if (!time_zone.empty())
  430. {
  431. try
  432. {
  433. DateLUT::setDefaultTimezone(time_zone);
  434. }
  435. catch (...)
  436. {
  437. std::cerr << "Warning: could not switch to server time zone: " << time_zone
  438. << ", reason: " << getCurrentExceptionMessage(/* with_stacktrace = */ false) << std::endl
  439. << "Proceeding with local time zone." << std::endl
  440. << std::endl;
  441. }
  442. }
  443. else
  444. {
  445. std::cerr << "Warning: could not determine server time zone. "
  446. << "Proceeding with local time zone." << std::endl
  447. << std::endl;
  448. }
  449. }
  450. prompt_by_server_display_name = config().getRawString("prompt_by_server_display_name.default", "{display_name} :) ");
  451. Strings keys;
  452. config().keys("prompt_by_server_display_name", keys);
  453. for (const String & key : keys)
  454. {
  455. if (key != "default" && server_display_name.find(key) != std::string::npos)
  456. {
  457. prompt_by_server_display_name = config().getRawString("prompt_by_server_display_name." + key);
  458. break;
  459. }
  460. }
  461. /// Prompt may contain escape sequences including \e[ or \x1b[ sequences to set terminal color.
  462. {
  463. String unescaped_prompt_by_server_display_name;
  464. ReadBufferFromString in(prompt_by_server_display_name);
  465. readEscapedString(unescaped_prompt_by_server_display_name, in);
  466. prompt_by_server_display_name = std::move(unescaped_prompt_by_server_display_name);
  467. }
  468. /// Prompt may contain the following substitutions in a form of {name}.
  469. std::map<String, String> prompt_substitutions{
  470. {"host", connection_parameters.host},
  471. {"port", toString(connection_parameters.port)},
  472. {"user", connection_parameters.user},
  473. {"display_name", server_display_name},
  474. };
  475. /// Quite suboptimal.
  476. for (const auto & [key, value] : prompt_substitutions)
  477. boost::replace_all(prompt_by_server_display_name, "{" + key + "}", value);
  478. }
  479. // Prints changed settings to stderr. Useful for debugging fuzzing failures.
  480. void Client::printChangedSettings() const
  481. {
  482. auto print_changes = [](const auto & changes, std::string_view settings_name)
  483. {
  484. if (!changes.empty())
  485. {
  486. fmt::print(stderr, "Changed {}: ", settings_name);
  487. for (size_t i = 0; i < changes.size(); ++i)
  488. {
  489. if (i)
  490. fmt::print(stderr, ", ");
  491. fmt::print(stderr, "{} = '{}'", changes[i].name, toString(changes[i].value));
  492. }
  493. fmt::print(stderr, "\n");
  494. }
  495. else
  496. {
  497. fmt::print(stderr, "No changed {}.\n", settings_name);
  498. }
  499. };
  500. print_changes(global_context->getSettingsRef().changes(), "settings");
  501. print_changes(cmd_merge_tree_settings.changes(), "MergeTree settings");
  502. }
  503. static bool queryHasWithClause(const IAST & ast)
  504. {
  505. if (const auto * select = dynamic_cast<const ASTSelectQuery *>(&ast); select && select->with())
  506. {
  507. return true;
  508. }
  509. // This full recursive walk is somewhat excessive, because most of the
  510. // children are not queries, but on the other hand it will let us to avoid
  511. // breakage when the AST structure changes and some new variant of query
  512. // nesting is added. This function is used in fuzzer, so it's better to be
  513. // defensive and avoid weird unexpected errors.
  514. for (const auto & child : ast.children)
  515. {
  516. if (queryHasWithClause(*child))
  517. {
  518. return true;
  519. }
  520. }
  521. return false;
  522. }
  523. std::optional<bool> Client::processFuzzingStep(const String & query_to_execute, const ASTPtr & parsed_query)
  524. {
  525. processParsedSingleQuery(query_to_execute, query_to_execute, parsed_query);
  526. const auto * exception = server_exception ? server_exception.get() : client_exception.get();
  527. // Sometimes you may get TOO_DEEP_RECURSION from the server,
  528. // and TOO_DEEP_RECURSION should not fail the fuzzer check.
  529. if (have_error && exception->code() == ErrorCodes::TOO_DEEP_RECURSION)
  530. {
  531. have_error = false;
  532. server_exception.reset();
  533. client_exception.reset();
  534. return true;
  535. }
  536. if (have_error)
  537. {
  538. fmt::print(stderr, "Error on processing query '{}': {}\n", parsed_query->formatForErrorMessage(), exception->message());
  539. // Try to reconnect after errors, for two reasons:
  540. // 1. We might not have realized that the server died, e.g. if
  541. // it sent us a <Fatal> trace and closed connection properly.
  542. // 2. The connection might have gotten into a wrong state and
  543. // the next query will get false positive about
  544. // "Unknown packet from server".
  545. try
  546. {
  547. connection->forceConnected(connection_parameters.timeouts);
  548. }
  549. catch (...)
  550. {
  551. // Just report it, we'll terminate below.
  552. fmt::print(stderr,
  553. "Error while reconnecting to the server: {}\n",
  554. getCurrentExceptionMessage(true));
  555. // The reconnection might fail, but we'll still be connected
  556. // in the sense of `connection->isConnected() = true`,
  557. // in case when the requested database doesn't exist.
  558. // Disconnect manually now, so that the following code doesn't
  559. // have any doubts, and the connection state is predictable.
  560. connection->disconnect();
  561. }
  562. }
  563. if (!connection->isConnected())
  564. {
  565. // Probably the server is dead because we found an assertion
  566. // failure. Fail fast.
  567. fmt::print(stderr, "Lost connection to the server.\n");
  568. // Print the changed settings because they might be needed to
  569. // reproduce the error.
  570. printChangedSettings();
  571. return false;
  572. }
  573. return std::nullopt;
  574. }
  575. /// Returns false when server is not available.
  576. bool Client::processWithFuzzing(const String & full_query)
  577. {
  578. ASTPtr orig_ast;
  579. try
  580. {
  581. const char * begin = full_query.data();
  582. orig_ast = parseQuery(begin, begin + full_query.size(), true);
  583. }
  584. catch (const Exception & e)
  585. {
  586. if (e.code() != ErrorCodes::SYNTAX_ERROR &&
  587. e.code() != ErrorCodes::TOO_DEEP_RECURSION)
  588. throw;
  589. }
  590. if (!orig_ast)
  591. {
  592. // Can't continue after a parsing error
  593. return true;
  594. }
  595. // `USE db` should not be executed
  596. // since this will break every query after `DROP db`
  597. if (orig_ast->as<ASTUseQuery>())
  598. {
  599. return true;
  600. }
  601. // Don't repeat:
  602. // - INSERT -- Because the tables may grow too big.
  603. // - CREATE -- Because first we run the unmodified query, it will succeed,
  604. // and the subsequent queries will fail.
  605. // When we run out of fuzzer errors, it may be interesting to
  606. // add fuzzing of create queries that wraps columns into
  607. // LowCardinality or Nullable.
  608. // Also there are other kinds of create queries such as CREATE
  609. // DICTIONARY, we could fuzz them as well.
  610. // - DROP -- No point in this (by the same reasons).
  611. // - SET -- The time to fuzz the settings has not yet come
  612. // (see comments in Client/QueryFuzzer.cpp)
  613. size_t this_query_runs = query_fuzzer_runs;
  614. ASTs queries_for_fuzzed_tables;
  615. if (orig_ast->as<ASTSetQuery>())
  616. {
  617. this_query_runs = 1;
  618. }
  619. else if (const auto * create = orig_ast->as<ASTCreateQuery>())
  620. {
  621. if (QueryFuzzer::isSuitableForFuzzing(*create))
  622. this_query_runs = create_query_fuzzer_runs;
  623. else
  624. this_query_runs = 1;
  625. }
  626. else if (const auto * insert = orig_ast->as<ASTInsertQuery>())
  627. {
  628. this_query_runs = 1;
  629. queries_for_fuzzed_tables = fuzzer.getInsertQueriesForFuzzedTables(full_query);
  630. }
  631. else if (const auto * drop = orig_ast->as<ASTDropQuery>())
  632. {
  633. this_query_runs = 1;
  634. queries_for_fuzzed_tables = fuzzer.getDropQueriesForFuzzedTables(*drop);
  635. }
  636. String query_to_execute;
  637. ASTPtr fuzz_base = orig_ast;
  638. for (size_t fuzz_step = 0; fuzz_step < this_query_runs; ++fuzz_step)
  639. {
  640. fmt::print(stderr, "Fuzzing step {} out of {}\n", fuzz_step, this_query_runs);
  641. ASTPtr ast_to_process;
  642. try
  643. {
  644. WriteBufferFromOwnString dump_before_fuzz;
  645. fuzz_base->dumpTree(dump_before_fuzz);
  646. auto base_before_fuzz = fuzz_base->formatForErrorMessage();
  647. ast_to_process = fuzz_base->clone();
  648. WriteBufferFromOwnString dump_of_cloned_ast;
  649. ast_to_process->dumpTree(dump_of_cloned_ast);
  650. // Run the original query as well.
  651. if (fuzz_step > 0)
  652. {
  653. fuzzer.fuzzMain(ast_to_process);
  654. }
  655. auto base_after_fuzz = fuzz_base->formatForErrorMessage();
  656. // Check that the source AST didn't change after fuzzing. This
  657. // helps debug AST cloning errors, where the cloned AST doesn't
  658. // clone all its children, and erroneously points to some source
  659. // child elements.
  660. if (base_before_fuzz != base_after_fuzz)
  661. {
  662. printChangedSettings();
  663. fmt::print(
  664. stderr,
  665. "Base before fuzz: {}\n"
  666. "Base after fuzz: {}\n",
  667. base_before_fuzz,
  668. base_after_fuzz);
  669. fmt::print(stderr, "Dump before fuzz:\n{}\n", dump_before_fuzz.str());
  670. fmt::print(stderr, "Dump of cloned AST:\n{}\n", dump_of_cloned_ast.str());
  671. fmt::print(stderr, "Dump after fuzz:\n");
  672. WriteBufferFromOStream cerr_buf(std::cerr, 4096);
  673. fuzz_base->dumpTree(cerr_buf);
  674. cerr_buf.finalize();
  675. fmt::print(
  676. stderr,
  677. "Found error: IAST::clone() is broken for some AST node. This is a bug. The original AST ('dump before fuzz') and its cloned copy ('dump of cloned AST') refer to the same nodes, which must never happen. This means that their parent node doesn't implement clone() correctly.");
  678. _exit(1);
  679. }
  680. auto fuzzed_text = ast_to_process->formatForErrorMessage();
  681. if (fuzz_step > 0 && fuzzed_text == base_before_fuzz)
  682. {
  683. fmt::print(stderr, "Got boring AST\n");
  684. continue;
  685. }
  686. query_to_execute = ast_to_process->formatForErrorMessage();
  687. if (auto res = processFuzzingStep(query_to_execute, ast_to_process))
  688. return *res;
  689. }
  690. catch (...)
  691. {
  692. if (!ast_to_process)
  693. fmt::print(stderr,
  694. "Error while forming new query: {}\n",
  695. getCurrentExceptionMessage(true));
  696. // Some functions (e.g. protocol parsers) don't throw, but
  697. // set last_exception instead, so we'll also do it here for
  698. // uniformity.
  699. // Surprisingly, this is a client exception, because we get the
  700. // server exception w/o throwing (see onReceiveException()).
  701. client_exception = std::make_unique<Exception>(getCurrentExceptionMessageAndPattern(print_stack_trace), getCurrentExceptionCode());
  702. have_error = true;
  703. }
  704. // Check that after the query is formatted, we can parse it back,
  705. // format again and get the same result. Unfortunately, we can't
  706. // compare the ASTs, which would be more sensitive to errors. This
  707. // double formatting check doesn't catch all errors, e.g. we can
  708. // format query incorrectly, but to a valid SQL that we can then
  709. // parse and format into the same SQL.
  710. // There are some complicated cases where we can generate the SQL
  711. // which we can't parse:
  712. // * first argument of lambda() replaced by fuzzer with
  713. // something else, leading to constructs such as
  714. // arrayMap((min(x) + 3) -> x + 1, ....)
  715. // * internals of Enum replaced, leading to:
  716. // Enum(equals(someFunction(y), 3)).
  717. // And there are even the cases when we can parse the query, but
  718. // it's logically incorrect and its formatting is a mess, such as
  719. // when `lambda()` function gets substituted into a wrong place.
  720. // To avoid dealing with these cases, run the check only for the
  721. // queries we were able to successfully execute.
  722. // Another caveat is that sometimes WITH queries are not executed,
  723. // if they are not referenced by the main SELECT, so they can still
  724. // have the aforementioned problems. Disable this check for such
  725. // queries, for lack of a better solution.
  726. // There is also a problem that fuzzer substitutes positive Int64
  727. // literals or Decimal literals, which are then parsed back as
  728. // UInt64, and suddenly duplicate alias substitution starts or stops
  729. // working (ASTWithAlias::formatImpl) or something like that.
  730. // So we compare not even the first and second formatting of the
  731. // query, but second and third.
  732. // If you have to add any more workarounds to this check, just remove
  733. // it altogether, it's not so useful.
  734. if (ast_to_process && !have_error && !queryHasWithClause(*ast_to_process))
  735. {
  736. ASTPtr ast_2;
  737. try
  738. {
  739. const auto * tmp_pos = query_to_execute.c_str();
  740. ast_2 = parseQuery(tmp_pos, tmp_pos + query_to_execute.size(), false /* allow_multi_statements */);
  741. }
  742. catch (Exception & e)
  743. {
  744. if (e.code() != ErrorCodes::SYNTAX_ERROR &&
  745. e.code() != ErrorCodes::TOO_DEEP_RECURSION)
  746. throw;
  747. }
  748. if (ast_2)
  749. {
  750. const auto text_2 = ast_2->formatForErrorMessage();
  751. const auto * tmp_pos = text_2.c_str();
  752. const auto ast_3 = parseQuery(tmp_pos, tmp_pos + text_2.size(),
  753. false /* allow_multi_statements */);
  754. const auto text_3 = ast_3 ? ast_3->formatForErrorMessage() : "";
  755. if (text_3 != text_2)
  756. {
  757. fmt::print(stderr, "Found error: The query formatting is broken.\n");
  758. printChangedSettings();
  759. fmt::print(stderr,
  760. "Got the following (different) text after formatting the fuzzed query and parsing it back:\n'{}'\n, expected:\n'{}'\n",
  761. text_3, text_2);
  762. fmt::print(stderr, "In more detail:\n");
  763. fmt::print(stderr, "AST-1 (generated by fuzzer):\n'{}'\n", ast_to_process->dumpTree());
  764. fmt::print(stderr, "Text-1 (AST-1 formatted):\n'{}'\n", query_to_execute);
  765. fmt::print(stderr, "AST-2 (Text-1 parsed):\n'{}'\n", ast_2->dumpTree());
  766. fmt::print(stderr, "Text-2 (AST-2 formatted):\n'{}'\n", text_2);
  767. fmt::print(stderr, "AST-3 (Text-2 parsed):\n'{}'\n", ast_3 ? ast_3->dumpTree() : "");
  768. fmt::print(stderr, "Text-3 (AST-3 formatted):\n'{}'\n", text_3);
  769. fmt::print(stderr, "Text-3 must be equal to Text-2, but it is not.\n");
  770. _exit(1);
  771. }
  772. }
  773. }
  774. // The server is still alive so we're going to continue fuzzing.
  775. // Determine what we're going to use as the starting AST.
  776. if (have_error)
  777. {
  778. // Query completed with error, keep the previous starting AST.
  779. // Also discard the exception that we now know to be non-fatal,
  780. // so that it doesn't influence the exit code.
  781. server_exception.reset();
  782. client_exception.reset();
  783. fuzzer.notifyQueryFailed(ast_to_process);
  784. have_error = false;
  785. }
  786. else if (ast_to_process->formatForErrorMessage().size() > 500)
  787. {
  788. // ast too long, start from original ast
  789. fmt::print(stderr, "Current AST is too long, discarding it and using the original AST as a start\n");
  790. fuzz_base = orig_ast;
  791. }
  792. else
  793. {
  794. // fuzz starting from this successful query
  795. fmt::print(stderr, "Query succeeded, using this AST as a start\n");
  796. fuzz_base = ast_to_process;
  797. }
  798. }
  799. for (const auto & query : queries_for_fuzzed_tables)
  800. {
  801. std::cout << std::endl;
  802. WriteBufferFromOStream ast_buf(std::cout, 4096);
  803. formatAST(*query, ast_buf, false /*highlight*/);
  804. ast_buf.finalize();
  805. if (const auto * insert = query->as<ASTInsertQuery>())
  806. {
  807. /// For inserts with data it's really useful to have the data itself available in the logs, as formatAST doesn't print it
  808. if (insert->hasInlinedData())
  809. {
  810. String bytes;
  811. {
  812. auto read_buf = getReadBufferFromASTInsertQuery(query);
  813. WriteBufferFromString write_buf(bytes);
  814. copyData(*read_buf, write_buf);
  815. }
  816. std::cout << std::endl << bytes;
  817. }
  818. }
  819. std::cout << std::endl << std::endl;
  820. try
  821. {
  822. query_to_execute = query->formatForErrorMessage();
  823. if (auto res = processFuzzingStep(query_to_execute, query))
  824. return *res;
  825. }
  826. catch (...)
  827. {
  828. client_exception = std::make_unique<Exception>(getCurrentExceptionMessageAndPattern(print_stack_trace), getCurrentExceptionCode());
  829. have_error = true;
  830. }
  831. if (have_error)
  832. {
  833. server_exception.reset();
  834. client_exception.reset();
  835. fuzzer.notifyQueryFailed(query);
  836. have_error = false;
  837. }
  838. }
  839. return true;
  840. }
  841. void Client::printHelpMessage(const OptionsDescription & options_description)
  842. {
  843. std::cout << options_description.main_description.value() << "\n";
  844. std::cout << options_description.external_description.value() << "\n";
  845. std::cout << options_description.hosts_and_ports_description.value() << "\n";
  846. std::cout << "In addition, --param_name=value can be specified for substitution of parameters for parametrized queries.\n";
  847. }
  848. void Client::addOptions(OptionsDescription & options_description)
  849. {
  850. /// Main commandline options related to client functionality and all parameters from Settings.
  851. options_description.main_description->add_options()
  852. ("config,c", po::value<std::string>(), "config-file path (another shorthand)")
  853. ("connection", po::value<std::string>(), "connection to use (from the client config), by default connection name is hostname")
  854. ("secure,s", "Use TLS connection")
  855. ("user,u", po::value<std::string>()->default_value("default"), "user")
  856. ("password", po::value<std::string>(), "password")
  857. ("ask-password", "ask-password")
  858. ("quota_key", po::value<std::string>(), "A string to differentiate quotas when the user have keyed quotas configured on server")
  859. ("max_client_network_bandwidth", po::value<int>(), "the maximum speed of data exchange over the network for the client in bytes per second.")
  860. ("compression", po::value<bool>(), "enable or disable compression (enabled by default for remote communication and disabled for localhost communication).")
  861. ("query-fuzzer-runs", po::value<int>()->default_value(0), "After executing every SELECT query, do random mutations in it and run again specified number of times. This is used for testing to discover unexpected corner cases.")
  862. ("create-query-fuzzer-runs", po::value<int>()->default_value(0), "")
  863. ("interleave-queries-file", po::value<std::vector<std::string>>()->multitoken(),
  864. "file path with queries to execute before every file from 'queries-file'; multiple files can be specified (--queries-file file1 file2...); this is needed to enable more aggressive fuzzing of newly added tests (see 'query-fuzzer-runs' option)")
  865. ("opentelemetry-traceparent", po::value<std::string>(), "OpenTelemetry traceparent header as described by W3C Trace Context recommendation")
  866. ("opentelemetry-tracestate", po::value<std::string>(), "OpenTelemetry tracestate header as described by W3C Trace Context recommendation")
  867. ("no-warnings", "disable warnings when client connects to server")
  868. ("fake-drop", "Ignore all DROP queries, should be used only for testing")
  869. ("accept-invalid-certificate", "Ignore certificate verification errors, equal to config parameters openSSL.client.invalidCertificateHandler.name=AcceptCertificateHandler and openSSL.client.verificationMode=none")
  870. ;
  871. /// Commandline options related to external tables.
  872. options_description.external_description.emplace(createOptionsDescription("External tables options", terminal_width));
  873. options_description.external_description->add_options()
  874. (
  875. "file", po::value<std::string>(), "data file or - for stdin"
  876. )
  877. (
  878. "name", po::value<std::string>()->default_value("_data"), "name of the table"
  879. )
  880. (
  881. "format", po::value<std::string>()->default_value("TabSeparated"), "data format"
  882. )
  883. (
  884. "structure", po::value<std::string>(), "structure"
  885. )
  886. (
  887. "types", po::value<std::string>(), "types"
  888. );
  889. /// Commandline options related to hosts and ports.
  890. options_description.hosts_and_ports_description.emplace(createOptionsDescription("Hosts and ports options", terminal_width));
  891. options_description.hosts_and_ports_description->add_options()
  892. ("host,h", po::value<String>()->default_value("localhost"),
  893. "Server hostname. Multiple hosts can be passed via multiple arguments"
  894. "Example of usage: '--host host1 --host host2 --port port2 --host host3 ...'"
  895. "Each '--port port' will be attached to the last seen host that doesn't have a port yet,"
  896. "if there is no such host, the port will be attached to the next first host or to default host.")
  897. ("port", po::value<UInt16>(), "server ports")
  898. ;
  899. }
  900. void Client::processOptions(const OptionsDescription & options_description,
  901. const CommandLineOptions & options,
  902. const std::vector<Arguments> & external_tables_arguments,
  903. const std::vector<Arguments> & hosts_and_ports_arguments)
  904. {
  905. namespace po = boost::program_options;
  906. size_t number_of_external_tables_with_stdin_source = 0;
  907. for (size_t i = 0; i < external_tables_arguments.size(); ++i)
  908. {
  909. /// Parse commandline options related to external tables.
  910. po::parsed_options parsed_tables = po::command_line_parser(external_tables_arguments[i]).options(
  911. options_description.external_description.value()).run();
  912. po::variables_map external_options;
  913. po::store(parsed_tables, external_options);
  914. try
  915. {
  916. external_tables.emplace_back(external_options);
  917. if (external_tables.back().file == "-")
  918. ++number_of_external_tables_with_stdin_source;
  919. if (number_of_external_tables_with_stdin_source > 1)
  920. throw Exception(ErrorCodes::BAD_ARGUMENTS, "Two or more external tables has stdin (-) set as --file field");
  921. }
  922. catch (const Exception & e)
  923. {
  924. std::cerr << getExceptionMessage(e, false) << std::endl;
  925. std::cerr << "Table №" << i << std::endl << std::endl;
  926. /// Avoid the case when error exit code can possibly overflow to normal (zero).
  927. auto exit_code = e.code() % 256;
  928. if (exit_code == 0)
  929. exit_code = 255;
  930. _exit(exit_code);
  931. }
  932. }
  933. for (const auto & hosts_and_ports_argument : hosts_and_ports_arguments)
  934. {
  935. /// Parse commandline options related to external tables.
  936. po::parsed_options parsed_hosts_and_ports
  937. = po::command_line_parser(hosts_and_ports_argument).options(options_description.hosts_and_ports_description.value()).run();
  938. po::variables_map host_and_port_options;
  939. po::store(parsed_hosts_and_ports, host_and_port_options);
  940. std::string host = host_and_port_options["host"].as<std::string>();
  941. std::optional<UInt16> port = !host_and_port_options["port"].empty()
  942. ? std::make_optional(host_and_port_options["port"].as<UInt16>())
  943. : std::nullopt;
  944. hosts_and_ports.emplace_back(HostAndPort{host, port});
  945. }
  946. send_external_tables = true;
  947. shared_context = Context::createShared();
  948. global_context = Context::createGlobal(shared_context.get());
  949. global_context->makeGlobalContext();
  950. global_context->setApplicationType(Context::ApplicationType::CLIENT);
  951. global_context->setSettings(cmd_settings);
  952. /// Copy settings-related program options to config.
  953. /// TODO: Is this code necessary?
  954. for (const auto & setting : global_context->getSettingsRef().all())
  955. {
  956. const auto & name = setting.getName();
  957. if (options.count(name))
  958. {
  959. if (allow_repeated_settings)
  960. config().setString(name, options[name].as<Strings>().back());
  961. else
  962. config().setString(name, options[name].as<String>());
  963. }
  964. }
  965. if (options.count("config-file") && options.count("config"))
  966. throw Exception(ErrorCodes::BAD_ARGUMENTS, "Two or more configuration files referenced in arguments");
  967. if (options.count("config"))
  968. config().setString("config-file", options["config"].as<std::string>());
  969. if (options.count("connection"))
  970. config().setString("connection", options["connection"].as<std::string>());
  971. if (options.count("interleave-queries-file"))
  972. interleave_queries_files = options["interleave-queries-file"].as<std::vector<std::string>>();
  973. if (options.count("secure"))
  974. config().setBool("secure", true);
  975. if (options.count("user") && !options["user"].defaulted())
  976. config().setString("user", options["user"].as<std::string>());
  977. if (options.count("password"))
  978. config().setString("password", options["password"].as<std::string>());
  979. if (options.count("ask-password"))
  980. config().setBool("ask-password", true);
  981. if (options.count("quota_key"))
  982. config().setString("quota_key", options["quota_key"].as<std::string>());
  983. if (options.count("max_client_network_bandwidth"))
  984. max_client_network_bandwidth = options["max_client_network_bandwidth"].as<int>();
  985. if (options.count("compression"))
  986. config().setBool("compression", options["compression"].as<bool>());
  987. if (options.count("no-warnings"))
  988. config().setBool("no-warnings", true);
  989. if (options.count("fake-drop"))
  990. fake_drop = true;
  991. if (options.count("accept-invalid-certificate"))
  992. {
  993. config().setString("openSSL.client.invalidCertificateHandler.name", "AcceptCertificateHandler");
  994. config().setString("openSSL.client.verificationMode", "none");
  995. }
  996. else
  997. config().setString("openSSL.client.invalidCertificateHandler.name", "RejectCertificateHandler");
  998. if ((query_fuzzer_runs = options["query-fuzzer-runs"].as<int>()))
  999. {
  1000. // Fuzzer implies multiquery.
  1001. config().setBool("multiquery", true);
  1002. // Ignore errors in parsing queries.
  1003. config().setBool("ignore-error", true);
  1004. ignore_error = true;
  1005. }
  1006. if ((create_query_fuzzer_runs = options["create-query-fuzzer-runs"].as<int>()))
  1007. {
  1008. // Fuzzer implies multiquery.
  1009. config().setBool("multiquery", true);
  1010. // Ignore errors in parsing queries.
  1011. config().setBool("ignore-error", true);
  1012. global_context->setSetting("allow_suspicious_low_cardinality_types", true);
  1013. ignore_error = true;
  1014. }
  1015. if (options.count("opentelemetry-traceparent"))
  1016. {
  1017. String traceparent = options["opentelemetry-traceparent"].as<std::string>();
  1018. String error;
  1019. if (!global_context->getClientTraceContext().parseTraceparentHeader(traceparent, error))
  1020. throw Exception(ErrorCodes::BAD_ARGUMENTS, "Cannot parse OpenTelemetry traceparent '{}': {}", traceparent, error);
  1021. }
  1022. if (options.count("opentelemetry-tracestate"))
  1023. global_context->getClientTraceContext().tracestate = options["opentelemetry-tracestate"].as<std::string>();
  1024. }
  1025. void Client::processConfig()
  1026. {
  1027. if (config().has("query") && config().has("queries-file"))
  1028. throw Exception(ErrorCodes::BAD_ARGUMENTS, "Options '--query' and '--queries-file' cannot be specified at the same time");
  1029. /// Batch mode is enabled if one of the following is true:
  1030. /// - -q (--query) command line option is present.
  1031. /// The value of the option is used as the text of query (or of multiple queries).
  1032. /// If stdin is not a terminal, INSERT data for the first query is read from it.
  1033. /// - stdin is not a terminal. In this case queries are read from it.
  1034. /// - --queries-file command line option is present.
  1035. /// The value of the option is used as file with query (or of multiple queries) to execute.
  1036. delayed_interactive = config().has("interactive") && (config().has("query") || config().has("queries-file"));
  1037. if (stdin_is_a_tty
  1038. && (delayed_interactive || (!config().has("query") && queries_files.empty())))
  1039. {
  1040. is_interactive = true;
  1041. }
  1042. else
  1043. {
  1044. echo_queries = config().getBool("echo", false);
  1045. ignore_error = config().getBool("ignore-error", false);
  1046. auto query_id = config().getString("query_id", "");
  1047. if (!query_id.empty())
  1048. global_context->setCurrentQueryId(query_id);
  1049. }
  1050. print_stack_trace = config().getBool("stacktrace", false);
  1051. logging_initialized = true;
  1052. if (config().has("multiquery"))
  1053. is_multiquery = true;
  1054. is_default_format = !config().has("vertical") && !config().has("format");
  1055. if (config().has("vertical"))
  1056. format = config().getString("format", "Vertical");
  1057. else
  1058. format = config().getString("format", is_interactive ? "PrettyCompact" : "TabSeparated");
  1059. format_max_block_size = config().getUInt64("format_max_block_size",
  1060. global_context->getSettingsRef().max_block_size);
  1061. insert_format = "Values";
  1062. /// Setting value from cmd arg overrides one from config
  1063. if (global_context->getSettingsRef().max_insert_block_size.changed)
  1064. {
  1065. insert_format_max_block_size = global_context->getSettingsRef().max_insert_block_size;
  1066. }
  1067. else
  1068. {
  1069. insert_format_max_block_size = config().getUInt64("insert_format_max_block_size",
  1070. global_context->getSettingsRef().max_insert_block_size);
  1071. }
  1072. global_context->setClientName(std::string(DEFAULT_CLIENT_NAME));
  1073. global_context->setQueryKindInitial();
  1074. global_context->setQuotaClientKey(config().getString("quota_key", ""));
  1075. global_context->setQueryKind(query_kind);
  1076. }
  1077. void Client::readArguments(
  1078. int argc,
  1079. char ** argv,
  1080. Arguments & common_arguments,
  1081. std::vector<Arguments> & external_tables_arguments,
  1082. std::vector<Arguments> & hosts_and_ports_arguments)
  1083. {
  1084. bool has_connection_string = argc >= 2 && tryParseConnectionString(std::string_view(argv[1]), common_arguments, hosts_and_ports_arguments);
  1085. int start_argument_index = has_connection_string ? 2 : 1;
  1086. /** We allow different groups of arguments:
  1087. * - common arguments;
  1088. * - arguments for any number of external tables each in form "--external args...",
  1089. * where possible args are file, name, format, structure, types;
  1090. * - param arguments for prepared statements.
  1091. * Split these groups before processing.
  1092. */
  1093. bool in_external_group = false;
  1094. std::string prev_host_arg;
  1095. std::string prev_port_arg;
  1096. for (int arg_num = start_argument_index; arg_num < argc; ++arg_num)
  1097. {
  1098. std::string_view arg = argv[arg_num];
  1099. if (has_connection_string)
  1100. checkIfCmdLineOptionCanBeUsedWithConnectionString(arg);
  1101. if (arg == "--external")
  1102. {
  1103. in_external_group = true;
  1104. external_tables_arguments.emplace_back(Arguments{""});
  1105. }
  1106. /// Options with value after equal sign.
  1107. else if (
  1108. in_external_group
  1109. && (arg.starts_with("--file=") || arg.starts_with("--name=") || arg.starts_with("--format=") || arg.starts_with("--structure=")
  1110. || arg.starts_with("--types=")))
  1111. {
  1112. external_tables_arguments.back().emplace_back(arg);
  1113. }
  1114. /// Options with value after whitespace.
  1115. else if (in_external_group && (arg == "--file" || arg == "--name" || arg == "--format" || arg == "--structure" || arg == "--types"))
  1116. {
  1117. if (arg_num + 1 < argc)
  1118. {
  1119. external_tables_arguments.back().emplace_back(arg);
  1120. ++arg_num;
  1121. arg = argv[arg_num];
  1122. external_tables_arguments.back().emplace_back(arg);
  1123. }
  1124. else
  1125. break;
  1126. }
  1127. else
  1128. {
  1129. in_external_group = false;
  1130. if (arg == "--file"sv || arg == "--name"sv || arg == "--structure"sv || arg == "--types"sv)
  1131. throw Exception(ErrorCodes::BAD_ARGUMENTS, "Parameter must be in external group, try add --external before {}", arg);
  1132. /// Parameter arg after underline.
  1133. if (arg.starts_with("--param_"))
  1134. {
  1135. auto param_continuation = arg.substr(strlen("--param_"));
  1136. auto equal_pos = param_continuation.find_first_of('=');
  1137. if (equal_pos == std::string::npos)
  1138. {
  1139. /// param_name value
  1140. ++arg_num;
  1141. if (arg_num >= argc)
  1142. throw Exception(ErrorCodes::BAD_ARGUMENTS, "Parameter requires value");
  1143. arg = argv[arg_num];
  1144. query_parameters.emplace(String(param_continuation), String(arg));
  1145. }
  1146. else
  1147. {
  1148. if (equal_pos == 0)
  1149. throw Exception(ErrorCodes::BAD_ARGUMENTS, "Parameter name cannot be empty");
  1150. /// param_name=value
  1151. query_parameters.emplace(param_continuation.substr(0, equal_pos), param_continuation.substr(equal_pos + 1));
  1152. }
  1153. }
  1154. else if (arg.starts_with("--host") || arg.starts_with("-h"))
  1155. {
  1156. std::string host_arg;
  1157. /// --host host
  1158. if (arg == "--host" || arg == "-h")
  1159. {
  1160. ++arg_num;
  1161. if (arg_num >= argc)
  1162. throw Exception(ErrorCodes::BAD_ARGUMENTS, "Host argument requires value");
  1163. arg = argv[arg_num];
  1164. host_arg = "--host=";
  1165. host_arg.append(arg);
  1166. }
  1167. else
  1168. host_arg = arg;
  1169. /// --port port1 --host host1
  1170. if (!prev_port_arg.empty())
  1171. {
  1172. hosts_and_ports_arguments.push_back({host_arg, prev_port_arg});
  1173. prev_port_arg.clear();
  1174. }
  1175. else
  1176. {
  1177. /// --host host1 --host host2
  1178. if (!prev_host_arg.empty())
  1179. hosts_and_ports_arguments.push_back({prev_host_arg});
  1180. prev_host_arg = host_arg;
  1181. }
  1182. }
  1183. else if (arg.starts_with("--port"))
  1184. {
  1185. auto port_arg = String{arg};
  1186. /// --port port
  1187. if (arg == "--port")
  1188. {
  1189. port_arg.push_back('=');
  1190. ++arg_num;
  1191. if (arg_num >= argc)
  1192. throw Exception(ErrorCodes::BAD_ARGUMENTS, "Port argument requires value");
  1193. arg = argv[arg_num];
  1194. port_arg.append(arg);
  1195. }
  1196. /// --host host1 --port port1
  1197. if (!prev_host_arg.empty())
  1198. {
  1199. hosts_and_ports_arguments.push_back({port_arg, prev_host_arg});
  1200. prev_host_arg.clear();
  1201. }
  1202. else
  1203. {
  1204. /// --port port1 --port port2
  1205. if (!prev_port_arg.empty())
  1206. hosts_and_ports_arguments.push_back({prev_port_arg});
  1207. prev_port_arg = port_arg;
  1208. }
  1209. }
  1210. else if (arg == "--allow_repeated_settings")
  1211. allow_repeated_settings = true;
  1212. else if (arg == "--allow_merge_tree_settings")
  1213. allow_merge_tree_settings = true;
  1214. else if (arg == "--multiquery" && (arg_num + 1) < argc && !std::string_view(argv[arg_num + 1]).starts_with('-'))
  1215. {
  1216. /// Transform the abbreviated syntax '--multiquery <SQL>' into the full syntax '--multiquery -q <SQL>'
  1217. ++arg_num;
  1218. arg = argv[arg_num];
  1219. addMultiquery(arg, common_arguments);
  1220. }
  1221. else if (arg == "--password" && ((arg_num + 1) >= argc || std::string_view(argv[arg_num + 1]).starts_with('-')))
  1222. {
  1223. common_arguments.emplace_back(arg);
  1224. /// if the value of --password is omitted, the password will be asked before
  1225. /// connection start
  1226. common_arguments.emplace_back(ConnectionParameters::ASK_PASSWORD);
  1227. }
  1228. else
  1229. common_arguments.emplace_back(arg);
  1230. }
  1231. }
  1232. if (!prev_host_arg.empty())
  1233. hosts_and_ports_arguments.push_back({prev_host_arg});
  1234. if (!prev_port_arg.empty())
  1235. hosts_and_ports_arguments.push_back({prev_port_arg});
  1236. }
  1237. }
  1238. #pragma GCC diagnostic ignored "-Wunused-function"
  1239. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  1240. int mainEntryClickHouseClient(int argc, char ** argv)
  1241. {
  1242. try
  1243. {
  1244. DB::Client client;
  1245. client.init(argc, argv);
  1246. return client.run();
  1247. }
  1248. catch (const DB::Exception & e)
  1249. {
  1250. std::cerr << DB::getExceptionMessage(e, false) << std::endl;
  1251. return 1;
  1252. }
  1253. catch (const boost::program_options::error & e)
  1254. {
  1255. std::cerr << "Bad arguments: " << e.what() << std::endl;
  1256. return DB::ErrorCodes::BAD_ARGUMENTS;
  1257. }
  1258. catch (...)
  1259. {
  1260. std::cerr << DB::getCurrentExceptionMessage(true) << std::endl;
  1261. return 1;
  1262. }
  1263. }