2021-07-11 11:36:27 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-09-29 19:17:26 +00:00
|
|
|
#include <Common/ProgressIndication.h>
|
|
|
|
#include <Common/InterruptListener.h>
|
|
|
|
#include <Common/ShellCommand.h>
|
|
|
|
#include <Core/ExternalTable.h>
|
2021-07-11 11:36:27 +00:00
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2021-07-11 20:35:29 +00:00
|
|
|
#include <Client/Suggest.h>
|
2021-07-11 23:17:14 +00:00
|
|
|
#include <Client/QueryFuzzer.h>
|
2021-09-29 19:17:26 +00:00
|
|
|
#include <boost/program_options.hpp>
|
2021-07-11 11:36:27 +00:00
|
|
|
|
2021-07-29 12:48:07 +00:00
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-08-20 14:10:59 +00:00
|
|
|
|
2021-07-29 12:48:07 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
}
|
2021-07-11 11:36:27 +00:00
|
|
|
|
2021-08-21 10:55:54 +00:00
|
|
|
enum MultiQueryProcessingStage
|
|
|
|
{
|
|
|
|
QUERIES_END,
|
|
|
|
PARSING_EXCEPTION,
|
|
|
|
CONTINUE_PARSING,
|
|
|
|
EXECUTE_QUERY,
|
|
|
|
PARSING_FAILED,
|
|
|
|
};
|
|
|
|
|
2021-10-01 17:31:00 +00:00
|
|
|
void interruptSignalHandler(int signum);
|
2021-10-01 13:47:39 +00:00
|
|
|
|
2021-10-08 14:03:54 +00:00
|
|
|
class InternalTextLogs;
|
|
|
|
|
2021-07-22 21:27:26 +00:00
|
|
|
class ClientBase : public Poco::Util::Application
|
2021-07-11 11:36:27 +00:00
|
|
|
{
|
2021-07-11 23:17:14 +00:00
|
|
|
|
2021-07-11 11:36:27 +00:00
|
|
|
public:
|
2021-07-22 21:27:26 +00:00
|
|
|
using Arguments = std::vector<String>;
|
2021-07-11 11:36:27 +00:00
|
|
|
|
2021-10-08 14:03:54 +00:00
|
|
|
ClientBase();
|
|
|
|
~ClientBase() override;
|
|
|
|
|
2021-07-11 11:36:27 +00:00
|
|
|
void init(int argc, char ** argv);
|
2021-07-23 20:54:49 +00:00
|
|
|
|
2021-07-11 11:36:27 +00:00
|
|
|
protected:
|
2021-08-18 21:01:17 +00:00
|
|
|
void runInteractive();
|
2021-08-19 11:07:47 +00:00
|
|
|
void runNonInteractive();
|
2021-08-18 21:01:17 +00:00
|
|
|
|
2021-08-20 14:10:59 +00:00
|
|
|
virtual bool processWithFuzzing(const String &)
|
|
|
|
{
|
|
|
|
throw Exception("Query processing with fuzzing is not implemented", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
2021-08-19 11:07:47 +00:00
|
|
|
|
2021-09-04 18:19:01 +00:00
|
|
|
virtual bool executeMultiQuery(const String & all_queries_text) = 0;
|
|
|
|
virtual void connect() = 0;
|
2021-08-21 10:55:54 +00:00
|
|
|
virtual void processError(const String & query) const = 0;
|
2021-09-19 21:42:28 +00:00
|
|
|
virtual String getName() const = 0;
|
2021-08-18 21:01:17 +00:00
|
|
|
|
2021-08-17 19:59:51 +00:00
|
|
|
void processOrdinaryQuery(const String & query_to_execute, ASTPtr parsed_query);
|
2021-08-18 14:39:04 +00:00
|
|
|
void processInsertQuery(const String & query_to_execute, ASTPtr parsed_query);
|
2021-08-20 14:10:59 +00:00
|
|
|
|
2021-08-21 10:55:54 +00:00
|
|
|
void processTextAsSingleQuery(const String & full_query);
|
2021-08-20 14:10:59 +00:00
|
|
|
void processParsedSingleQuery(const String & full_query, const String & query_to_execute,
|
|
|
|
ASTPtr parsed_query, std::optional<bool> echo_query_ = {}, bool report_error = false);
|
2021-08-18 14:39:04 +00:00
|
|
|
|
2021-08-21 10:55:54 +00:00
|
|
|
static void adjustQueryEnd(const char *& this_query_end, const char * all_queries_end, int max_parser_depth);
|
|
|
|
ASTPtr parseQuery(const char *& pos, const char * end, bool allow_multi_statements) const;
|
2021-10-03 09:11:59 +00:00
|
|
|
static void setupSignalHandler();
|
2021-08-20 14:10:59 +00:00
|
|
|
|
|
|
|
MultiQueryProcessingStage analyzeMultiQueryText(
|
|
|
|
const char *& this_query_begin, const char *& this_query_end, const char * all_queries_end,
|
|
|
|
String & query_to_execute, ASTPtr & parsed_query, const String & all_queries_text,
|
|
|
|
std::optional<Exception> & current_exception);
|
2021-07-29 09:45:09 +00:00
|
|
|
|
2021-07-28 12:56:11 +00:00
|
|
|
/// For non-interactive multi-query mode get queries text prefix.
|
|
|
|
virtual String getQueryTextPrefix() { return ""; }
|
2021-07-23 20:56:47 +00:00
|
|
|
|
2021-09-22 21:35:29 +00:00
|
|
|
static void clearTerminal();
|
2021-09-04 18:19:01 +00:00
|
|
|
void showClientVersion();
|
|
|
|
|
2021-07-28 12:56:11 +00:00
|
|
|
using ProgramOptionsDescription = boost::program_options::options_description;
|
|
|
|
using CommandLineOptions = boost::program_options::variables_map;
|
2021-07-12 09:30:16 +00:00
|
|
|
|
2021-07-28 12:56:11 +00:00
|
|
|
struct OptionsDescription
|
|
|
|
{
|
|
|
|
std::optional<ProgramOptionsDescription> main_description;
|
|
|
|
std::optional<ProgramOptionsDescription> external_description;
|
|
|
|
};
|
2021-07-11 20:35:29 +00:00
|
|
|
|
2021-07-28 12:56:11 +00:00
|
|
|
virtual void printHelpMessage(const OptionsDescription & options_description) = 0;
|
2021-07-29 12:48:07 +00:00
|
|
|
virtual void addAndCheckOptions(OptionsDescription & options_description, po::variables_map & options, Arguments & arguments) = 0;
|
2021-07-28 12:56:11 +00:00
|
|
|
virtual void processOptions(const OptionsDescription & options_description,
|
|
|
|
const CommandLineOptions & options,
|
|
|
|
const std::vector<Arguments> & external_tables_arguments) = 0;
|
|
|
|
virtual void processConfig() = 0;
|
2021-07-12 14:51:09 +00:00
|
|
|
|
2021-07-11 20:35:29 +00:00
|
|
|
private:
|
2021-08-19 11:07:47 +00:00
|
|
|
bool processQueryText(const String & text);
|
|
|
|
|
|
|
|
void receiveResult(ASTPtr parsed_query);
|
|
|
|
bool receiveAndProcessPacket(ASTPtr parsed_query, bool cancelled);
|
|
|
|
void receiveLogs(ASTPtr parsed_query);
|
|
|
|
bool receiveSampleBlock(Block & out, ColumnsDescription & columns_description, ASTPtr parsed_query);
|
|
|
|
bool receiveEndOfQuery();
|
|
|
|
|
|
|
|
void onProgress(const Progress & value);
|
|
|
|
void onData(Block & block, ASTPtr parsed_query);
|
|
|
|
void onLogData(Block & block);
|
|
|
|
void onTotals(Block & block, ASTPtr parsed_query);
|
|
|
|
void onExtremes(Block & block, ASTPtr parsed_query);
|
|
|
|
void onReceiveExceptionFromServer(std::unique_ptr<Exception> && e);
|
|
|
|
void onProfileInfo(const BlockStreamProfileInfo & profile_info);
|
|
|
|
void onEndOfStream();
|
2021-09-14 11:06:00 +00:00
|
|
|
void onProfileEvents(Block & block);
|
2021-08-19 11:07:47 +00:00
|
|
|
|
|
|
|
void sendData(Block & sample, const ColumnsDescription & columns_description, ASTPtr parsed_query);
|
|
|
|
void sendDataFrom(ReadBuffer & buf, Block & sample,
|
|
|
|
const ColumnsDescription & columns_description, ASTPtr parsed_query);
|
2021-08-21 10:55:54 +00:00
|
|
|
void sendExternalTables(ASTPtr parsed_query);
|
2021-08-19 11:07:47 +00:00
|
|
|
|
|
|
|
void initBlockOutputStream(const Block & block, ASTPtr parsed_query);
|
|
|
|
void initLogsOutputStream();
|
|
|
|
|
2021-07-11 20:35:29 +00:00
|
|
|
inline String prompt() const
|
|
|
|
{
|
|
|
|
return boost::replace_all_copy(prompt_by_server_display_name, "{database}", config().getString("database", "default"));
|
|
|
|
}
|
2021-07-12 12:25:17 +00:00
|
|
|
|
2021-08-21 10:55:54 +00:00
|
|
|
void resetOutput();
|
2021-07-12 12:25:17 +00:00
|
|
|
void outputQueryInfo(bool echo_query_);
|
2021-09-11 11:49:42 +00:00
|
|
|
void readArguments(int argc, char ** argv, Arguments & common_arguments, std::vector<Arguments> & external_tables_arguments);
|
2021-07-22 21:27:26 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool is_interactive = false; /// Use either interactive line editing interface or batch mode.
|
|
|
|
bool is_multiquery = false;
|
|
|
|
|
|
|
|
bool echo_queries = false; /// Print queries before execution in batch mode.
|
|
|
|
bool ignore_error = false; /// In case of errors, don't print error message, continue to next query. Only applicable for non-interactive mode.
|
|
|
|
bool print_time_to_stderr = false; /// Output execution time to stderr in batch mode.
|
2021-09-11 11:34:22 +00:00
|
|
|
bool load_suggestions = false;
|
2021-07-22 21:27:26 +00:00
|
|
|
|
|
|
|
std::vector<String> queries_files; /// If not empty, queries will be read from these files
|
|
|
|
std::vector<String> interleave_queries_files; /// If not empty, run queries from these files before processing every file from 'queries_files'.
|
|
|
|
|
|
|
|
bool stdin_is_a_tty = false; /// stdin is a terminal.
|
|
|
|
bool stdout_is_a_tty = false; /// stdout is a terminal.
|
|
|
|
uint64_t terminal_width = 0;
|
|
|
|
|
2021-09-11 11:34:22 +00:00
|
|
|
ServerConnectionPtr connection;
|
2021-09-04 18:19:01 +00:00
|
|
|
ConnectionParameters connection_parameters;
|
|
|
|
|
|
|
|
String format; /// Query results output format.
|
|
|
|
bool is_default_format = true; /// false, if format is set in the config or command line.
|
|
|
|
size_t format_max_block_size = 0; /// Max block size for console output.
|
|
|
|
String insert_format; /// Format of INSERT data that is read from stdin in batch mode.
|
|
|
|
size_t insert_format_max_block_size = 0; /// Max block size when reading INSERT data.
|
|
|
|
size_t max_client_network_bandwidth = 0; /// The maximum speed of data exchange over the network for the client in bytes per second.
|
|
|
|
|
|
|
|
bool has_vertical_output_suffix = false; /// Is \G present at the end of the query string?
|
|
|
|
|
|
|
|
/// We will format query_id in interactive mode in various ways, the default is just to print Query id: ...
|
|
|
|
std::vector<std::pair<String, String>> query_id_formats;
|
|
|
|
|
2021-07-22 21:27:26 +00:00
|
|
|
/// Settings specified via command line args
|
|
|
|
Settings cmd_settings;
|
|
|
|
|
|
|
|
SharedContextHolder shared_context;
|
|
|
|
ContextMutablePtr global_context;
|
|
|
|
|
|
|
|
/// Buffer that reads from stdin in batch mode.
|
|
|
|
ReadBufferFromFileDescriptor std_in{STDIN_FILENO};
|
|
|
|
/// Console output.
|
|
|
|
WriteBufferFromFileDescriptor std_out{STDOUT_FILENO};
|
|
|
|
std::unique_ptr<ShellCommand> pager_cmd;
|
2021-08-07 19:21:52 +00:00
|
|
|
|
2021-07-22 21:27:26 +00:00
|
|
|
/// The user can specify to redirect query output to a file.
|
2021-08-07 19:21:52 +00:00
|
|
|
std::unique_ptr<WriteBuffer> out_file_buf;
|
2021-07-22 21:27:26 +00:00
|
|
|
BlockOutputStreamPtr block_out_stream;
|
2021-08-07 19:21:52 +00:00
|
|
|
|
2021-07-22 21:27:26 +00:00
|
|
|
/// The user could specify special file for server logs (stderr by default)
|
|
|
|
std::unique_ptr<WriteBuffer> out_logs_buf;
|
|
|
|
String server_logs_file;
|
2021-10-08 14:03:54 +00:00
|
|
|
std::unique_ptr<InternalTextLogs> logs_out_stream;
|
2021-07-22 21:27:26 +00:00
|
|
|
|
2021-09-04 18:19:01 +00:00
|
|
|
String home_path;
|
|
|
|
String history_file; /// Path to a file containing command history.
|
2021-08-17 19:59:51 +00:00
|
|
|
|
2021-09-04 18:19:01 +00:00
|
|
|
String current_profile;
|
2021-08-17 19:59:51 +00:00
|
|
|
|
2021-09-04 18:19:01 +00:00
|
|
|
UInt64 server_revision = 0;
|
|
|
|
String server_version;
|
|
|
|
String prompt_by_server_display_name;
|
|
|
|
String server_display_name;
|
2021-08-17 19:59:51 +00:00
|
|
|
|
2021-09-04 18:19:01 +00:00
|
|
|
ProgressIndication progress_indication;
|
|
|
|
bool need_render_progress = true;
|
|
|
|
bool written_first_block = false;
|
|
|
|
size_t processed_rows = 0; /// How many rows have been read or written.
|
2021-08-17 19:59:51 +00:00
|
|
|
|
|
|
|
/// The last exception that was received from the server. Is used for the
|
|
|
|
/// return code in batch mode.
|
|
|
|
std::unique_ptr<Exception> server_exception;
|
|
|
|
/// Likewise, the last exception that occurred on the client.
|
|
|
|
std::unique_ptr<Exception> client_exception;
|
|
|
|
|
2021-09-04 18:19:01 +00:00
|
|
|
/// If the last query resulted in exception. `server_exception` or
|
|
|
|
/// `client_exception` must be set.
|
|
|
|
bool have_error = false;
|
2021-08-17 19:59:51 +00:00
|
|
|
|
2021-09-04 18:19:01 +00:00
|
|
|
std::list<ExternalTable> external_tables; /// External tables info.
|
2021-09-24 08:29:01 +00:00
|
|
|
bool send_external_tables = false;
|
2021-09-04 18:19:01 +00:00
|
|
|
NameToNameMap query_parameters; /// Dictionary with query parameters for prepared statements.
|
2021-08-18 14:39:04 +00:00
|
|
|
|
2021-09-04 18:19:01 +00:00
|
|
|
QueryFuzzer fuzzer;
|
|
|
|
int query_fuzzer_runs = 0;
|
2021-08-19 11:07:47 +00:00
|
|
|
|
2021-09-04 18:19:01 +00:00
|
|
|
QueryProcessingStage::Enum query_processing_stage;
|
2021-07-11 11:36:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|