2022-05-08 17:01:47 +00:00
# include <cstdlib>
2012-03-26 02:48:08 +00:00
# include <fcntl.h>
2018-02-26 13:24:06 +00:00
# include <map>
2012-03-25 03:47:13 +00:00
# include <iostream>
2012-03-26 02:48:08 +00:00
# include <iomanip>
2023-04-11 11:03:03 +00:00
# include <memory>
2017-11-20 04:15:43 +00:00
# include <optional>
2023-04-11 11:03:03 +00:00
# include <Common/ThreadStatus.h>
2014-03-05 13:48:45 +00:00
# include <boost/program_options.hpp>
2018-02-26 13:24:06 +00:00
# include <boost/algorithm/string/replace.hpp>
2021-07-18 14:42:41 +00:00
# include <filesystem>
2021-08-16 13:40:44 +00:00
# include <string>
2021-07-23 20:56:47 +00:00
# include "Client.h"
2023-06-07 03:16:29 +00:00
# include "Client/ConnectionString.h"
2021-08-30 11:04:59 +00:00
# include "Core/Protocol.h"
2022-08-11 02:34:10 +00:00
# include "Parsers/formatAST.h"
2021-07-23 20:56:47 +00:00
2022-11-22 02:41:23 +00:00
# include <Access/AccessControl.h>
2023-10-23 14:48:30 +00:00
# include <Common/Config/ConfigProcessor.h>
# include <Common/Config/getClientConfigPath.h>
2024-08-08 02:41:22 +00:00
# include <Common/CurrentThread.h>
2024-04-02 12:45:48 +00:00
# include <Common/Exception.h>
# include <Common/TerminalSize.h>
# include <Common/config_version.h>
# include <Common/formatReadable.h>
2024-07-11 12:00:05 +00:00
# include <Core/Settings.h>
2021-07-23 20:56:47 +00:00
# include <Columns/ColumnString.h>
2021-07-18 14:42:41 +00:00
2018-03-23 16:55:35 +00:00
# include <IO/ReadBufferFromString.h>
2017-04-01 09:19:00 +00:00
# include <IO/ReadHelpers.h>
2021-09-04 18:19:01 +00:00
# include <IO/UseSSL.h>
2022-11-22 12:10:53 +00:00
# include <IO/WriteBufferFromOStream.h>
# include <IO/WriteHelpers.h>
# include <IO/copyData.h>
2021-07-23 20:56:47 +00:00
2020-08-06 15:39:09 +00:00
# include <Parsers/ASTCreateQuery.h>
2021-01-06 00:58:31 +00:00
# include <Parsers/ASTDropQuery.h>
2017-04-01 09:19:00 +00:00
# include <Parsers/ASTSetQuery.h>
# include <Parsers/ASTUseQuery.h>
# include <Parsers/ASTInsertQuery.h>
2021-03-17 15:55:53 +00:00
# include <Parsers/ASTSelectQuery.h>
2021-07-18 14:42:41 +00:00
2022-11-22 12:10:53 +00:00
# include <Processors/Transforms/getSourceFromASTInsertQuery.h>
2017-07-13 00:04:06 +00:00
# include <Functions/registerFunctions.h>
# include <AggregateFunctions/registerAggregateFunctions.h>
2020-10-29 03:39:43 +00:00
# include <Formats/registerFormats.h>
2024-03-07 12:29:42 +00:00
# include <Formats/FormatFactory.h>
2018-08-25 13:55:18 +00:00
2024-07-11 12:00:05 +00:00
# include <Poco/Util/Application.h>
2021-05-16 22:06:09 +00:00
namespace fs = std : : filesystem ;
2022-03-01 09:22:12 +00:00
using namespace std : : literals ;
2014-08-15 00:08:15 +00:00
2021-07-23 20:56:47 +00:00
2012-03-25 03:47:13 +00:00
namespace DB
{
2024-09-18 12:20:53 +00:00
namespace Setting
{
extern const SettingsDialect dialect ;
extern const SettingsBool use_client_time_zone ;
}
2016-01-12 02:21:15 +00:00
namespace ErrorCodes
{
2017-04-01 07:20:54 +00:00
extern const int BAD_ARGUMENTS ;
extern const int UNKNOWN_PACKET_FROM_SERVER ;
2021-01-05 21:20:39 +00:00
extern const int SYNTAX_ERROR ;
2021-06-19 18:33:49 +00:00
extern const int TOO_DEEP_RECURSION ;
2021-07-27 09:29:35 +00:00
extern const int NETWORK_ERROR ;
2021-08-20 08:08:21 +00:00
extern const int AUTHENTICATION_FAILED ;
2023-02-16 12:38:25 +00:00
extern const int NO_ELEMENTS_IN_CONFIG ;
2024-05-06 02:37:11 +00:00
extern const int USER_EXPIRED ;
2016-01-12 02:21:15 +00:00
}
2021-08-21 10:55:54 +00:00
void Client : : processError ( const String & query ) const
2021-03-17 15:55:53 +00:00
{
2021-08-21 10:55:54 +00:00
if ( server_exception )
2021-03-17 15:55:53 +00:00
{
2021-09-11 14:56:11 +00:00
fmt : : print ( stderr , " Received exception from server (version {}): \n {} \n " ,
server_version ,
getExceptionMessage ( * server_exception , print_stack_trace , true ) ) ;
2024-05-06 02:37:11 +00:00
if ( server_exception - > code ( ) = = ErrorCodes : : USER_EXPIRED )
{
server_exception - > rethrow ( ) ;
}
2021-08-21 10:55:54 +00:00
if ( is_interactive )
2021-09-11 14:56:11 +00:00
{
fmt : : print ( stderr , " \n " ) ;
}
else
{
fmt : : print ( stderr , " (query: {}) \n " , query ) ;
}
2021-03-17 15:55:53 +00:00
}
2021-08-21 10:55:54 +00:00
if ( client_exception )
2021-03-17 15:55:53 +00:00
{
2021-09-11 14:56:11 +00:00
fmt : : print ( stderr , " Error on processing query: {} \n " , client_exception - > message ( ) ) ;
2021-08-21 10:55:54 +00:00
if ( is_interactive )
2021-09-11 14:56:11 +00:00
{
2021-08-21 10:55:54 +00:00
fmt : : print ( stderr , " \n " ) ;
2021-09-11 14:56:11 +00:00
}
else
{
fmt : : print ( stderr , " (query: {}) \n " , query ) ;
}
2021-08-21 10:55:54 +00:00
}
// A debug check -- at least some exception must be set, if the error
// flag is set, and vice versa.
assert ( have_error = = ( client_exception | | server_exception ) ) ;
}
2022-07-20 09:51:55 +00:00
void Client : : showWarnings ( )
{
try
{
std : : vector < String > messages = loadWarningMessages ( ) ;
if ( ! messages . empty ( ) )
{
std : : cout < < " Warnings: " < < std : : endl ;
for ( const auto & message : messages )
std : : cout < < " * " < < message < < std : : endl ;
std : : cout < < std : : endl ;
}
}
2023-09-25 20:19:09 +00:00
catch ( . . . ) // NOLINT(bugprone-empty-catch)
2022-07-20 09:51:55 +00:00
{
/// Ignore exception
}
}
2023-10-23 14:48:30 +00:00
void Client : : parseConnectionsCredentials ( Poco : : Util : : AbstractConfiguration & config , const std : : string & connection_name )
2023-01-27 20:41:59 +00:00
{
2023-10-23 14:48:30 +00:00
std : : optional < String > default_connection_name ;
2023-01-27 20:41:59 +00:00
if ( hosts_and_ports . empty ( ) )
{
2023-10-23 14:48:30 +00:00
if ( config . has ( " host " ) )
default_connection_name = config . getString ( " host " ) ;
2023-01-27 20:41:59 +00:00
}
else
{
2023-10-23 14:48:30 +00:00
default_connection_name = hosts_and_ports . front ( ) . host ;
2023-01-27 20:41:59 +00:00
}
2023-02-16 12:38:25 +00:00
String connection ;
2023-10-23 14:48:30 +00:00
if ( ! connection_name . empty ( ) )
connection = connection_name ;
2023-02-16 12:38:25 +00:00
else
2023-10-23 14:48:30 +00:00
connection = default_connection_name . value_or ( " localhost " ) ;
2023-02-16 12:38:25 +00:00
2023-01-27 20:41:59 +00:00
Strings keys ;
2023-10-23 14:48:30 +00:00
config . keys ( " connections_credentials " , keys ) ;
2023-02-16 12:38:25 +00:00
bool connection_found = false ;
for ( const auto & key : keys )
2023-01-27 20:41:59 +00:00
{
2023-02-16 12:38:25 +00:00
const String & prefix = " connections_credentials. " + key ;
2023-01-27 20:41:59 +00:00
2023-10-23 14:48:30 +00:00
const String & name = config . getString ( prefix + " .name " , " " ) ;
if ( name ! = connection )
2023-01-27 20:41:59 +00:00
continue ;
2023-02-16 12:38:25 +00:00
connection_found = true ;
2023-01-27 20:41:59 +00:00
String connection_hostname ;
2023-10-23 14:48:30 +00:00
if ( config . has ( prefix + " .hostname " ) )
connection_hostname = config . getString ( prefix + " .hostname " ) ;
2023-01-27 20:41:59 +00:00
else
2023-10-23 14:48:30 +00:00
connection_hostname = name ;
config . setString ( " host " , connection_hostname ) ;
if ( config . has ( prefix + " .port " ) )
config . setInt ( " port " , config . getInt ( prefix + " .port " ) ) ;
if ( config . has ( prefix + " .secure " ) )
config . setBool ( " secure " , config . getBool ( prefix + " .secure " ) ) ;
if ( config . has ( prefix + " .user " ) )
config . setString ( " user " , config . getString ( prefix + " .user " ) ) ;
if ( config . has ( prefix + " .password " ) )
config . setString ( " password " , config . getString ( prefix + " .password " ) ) ;
if ( config . has ( prefix + " .database " ) )
config . setString ( " database " , config . getString ( prefix + " .database " ) ) ;
if ( config . has ( prefix + " .history_file " ) )
2023-01-27 20:41:59 +00:00
{
2023-10-23 14:48:30 +00:00
String history_file = config . getString ( prefix + " .history_file " ) ;
2023-01-27 20:41:59 +00:00
if ( history_file . starts_with ( " ~ " ) & & ! home_path . empty ( ) )
history_file = home_path + " / " + history_file . substr ( 1 ) ;
2023-10-23 14:48:30 +00:00
config . setString ( " history_file " , history_file ) ;
2023-01-27 20:41:59 +00:00
}
2024-07-01 06:02:41 +00:00
if ( config . has ( prefix + " .accept-invalid-certificate " ) )
config . setBool ( " accept-invalid-certificate " , config . getBool ( prefix + " .accept-invalid-certificate " ) ) ;
2023-01-27 20:41:59 +00:00
}
2023-02-16 12:38:25 +00:00
2023-10-23 14:48:30 +00:00
if ( ! connection_name . empty ( ) & & ! connection_found )
2023-02-16 12:38:25 +00:00
throw Exception ( ErrorCodes : : NO_ELEMENTS_IN_CONFIG , " No such connection '{}' in connections_credentials " , connection ) ;
2023-01-27 20:41:59 +00:00
}
2021-07-22 19:38:37 +00:00
/// Make query to get all server warnings
std : : vector < String > Client : : loadWarningMessages ( )
{
2022-07-20 09:51:55 +00:00
/// Older server versions cannot execute the query loading warnings.
2022-07-21 08:48:41 +00:00
constexpr UInt64 min_server_revision_to_load_warnings = DBMS_MIN_PROTOCOL_VERSION_WITH_VIEW_IF_PERMITTED ;
2022-07-20 09:51:55 +00:00
2022-07-21 08:48:41 +00:00
if ( server_revision < min_server_revision_to_load_warnings )
2022-07-20 09:51:55 +00:00
return { } ;
2021-07-22 19:38:37 +00:00
std : : vector < String > messages ;
2022-07-09 15:51:59 +00:00
connection - > sendQuery ( connection_parameters . timeouts ,
" SELECT * FROM viewIfPermitted(SELECT message FROM system.warnings ELSE null('message String')) " ,
2022-08-12 12:28:35 +00:00
{ } /* query_parameters */ ,
2022-07-09 15:51:59 +00:00
" " /* query_id */ ,
2021-11-10 15:45:15 +00:00
QueryProcessingStage : : Complete ,
2024-07-25 14:36:32 +00:00
& client_context - > getSettingsRef ( ) ,
& client_context - > getClientInfo ( ) , false , { } ) ;
2021-07-22 19:38:37 +00:00
while ( true )
{
Packet packet = connection - > receivePacket ( ) ;
switch ( packet . type )
{
case Protocol : : Server : : Data :
if ( packet . block )
{
const ColumnString & column = typeid_cast < const ColumnString & > ( * packet . block . getByPosition ( 0 ) . column ) ;
size_t rows = packet . block . rows ( ) ;
for ( size_t i = 0 ; i < rows ; + + i )
2024-08-10 13:01:55 +00:00
messages . emplace_back ( column [ i ] . safeGet < String > ( ) ) ;
2021-07-22 19:38:37 +00:00
}
continue ;
case Protocol : : Server : : Progress :
case Protocol : : Server : : ProfileInfo :
case Protocol : : Server : : Totals :
case Protocol : : Server : : Extremes :
case Protocol : : Server : : Log :
continue ;
case Protocol : : Server : : Exception :
packet . exception - > rethrow ( ) ;
return messages ;
case Protocol : : Server : : EndOfStream :
return messages ;
2021-08-30 11:04:59 +00:00
case Protocol : : Server : : ProfileEvents :
continue ;
2021-07-22 19:38:37 +00:00
default :
throw Exception ( ErrorCodes : : UNKNOWN_PACKET_FROM_SERVER , " Unknown packet {} from server {} " ,
packet . type , connection - > getDescription ( ) ) ;
}
}
}
2024-06-25 14:23:37 +00:00
Poco : : Util : : LayeredConfiguration & Client : : getClientConfiguration ( )
{
return config ( ) ;
}
2021-07-22 19:38:37 +00:00
2021-07-23 20:56:47 +00:00
void Client : : initialize ( Poco : : Util : : Application & self )
2012-03-25 03:47:13 +00:00
{
2021-07-23 20:56:47 +00:00
Poco : : Util : : Application : : initialize ( self ) ;
2022-08-21 18:24:17 +00:00
const char * home_path_cstr = getenv ( " HOME " ) ; // NOLINT(concurrency-mt-unsafe)
2021-07-18 14:42:41 +00:00
if ( home_path_cstr )
home_path = home_path_cstr ;
2023-10-23 14:48:30 +00:00
std : : optional < std : : string > config_path ;
if ( config ( ) . has ( " config-file " ) )
config_path . emplace ( config ( ) . getString ( " config-file " ) ) ;
else
config_path = getClientConfigPath ( home_path ) ;
if ( config_path . has_value ( ) )
{
ConfigProcessor config_processor ( * config_path ) ;
auto loaded_config = config_processor . loadConfig ( ) ;
parseConnectionsCredentials ( * loaded_config . configuration , config ( ) . getString ( " connection " , " " ) ) ;
config ( ) . add ( loaded_config . configuration ) ;
}
else if ( config ( ) . has ( " connection " ) )
2024-05-17 08:23:32 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " --connection was specified, but config does not exist " ) ;
2014-01-08 16:33:28 +00:00
2024-07-01 06:02:41 +00:00
if ( config ( ) . has ( " accept-invalid-certificate " ) )
{
config ( ) . setString ( " openSSL.client.invalidCertificateHandler.name " , " AcceptCertificateHandler " ) ;
config ( ) . setString ( " openSSL.client.verificationMode " , " none " ) ;
}
2022-04-05 16:35:23 +00:00
/** getenv is thread-safe in Linux glibc and in all sane libc implementations.
* But the standard does not guarantee that subsequent calls will not rewrite the value by returned pointer .
*
* man getenv :
*
* As typically implemented , getenv ( ) returns a pointer to a string within the environment list .
* The caller must take care not to modify this string , since that would change the environment of
* the process .
*
* The implementation of getenv ( ) is not required to be reentrant . The string pointed to by the return value of getenv ( )
* may be statically allocated , and can be modified by a subsequent call to getenv ( ) , putenv ( 3 ) , setenv ( 3 ) , or unsetenv ( 3 ) .
*/
2022-08-21 18:24:17 +00:00
const char * env_user = getenv ( " CLICKHOUSE_USER " ) ; // NOLINT(concurrency-mt-unsafe)
2023-04-05 19:40:43 +00:00
if ( env_user & & ! config ( ) . has ( " user " ) )
2022-02-28 05:33:14 +00:00
config ( ) . setString ( " user " , env_user ) ;
2022-04-05 16:35:23 +00:00
2022-08-21 18:24:17 +00:00
const char * env_password = getenv ( " CLICKHOUSE_PASSWORD " ) ; // NOLINT(concurrency-mt-unsafe)
2023-04-05 19:40:43 +00:00
if ( env_password & & ! config ( ) . has ( " password " ) )
2022-02-28 05:33:14 +00:00
config ( ) . setString ( " password " , env_password ) ;
2021-07-18 14:42:41 +00:00
/// settings and limits could be specified in config file, but passed settings has higher priority
2024-09-18 12:20:53 +00:00
for ( const auto & setting : global_context - > getSettingsRef ( ) . getUnchangedNames ( ) )
2021-07-18 14:42:41 +00:00
{
2024-09-18 12:20:53 +00:00
String name { setting } ;
2021-07-18 14:42:41 +00:00
if ( config ( ) . has ( name ) )
global_context - > setSetting ( name , config ( ) . getString ( name ) ) ;
}
2012-03-25 03:47:13 +00:00
2021-07-18 14:42:41 +00:00
/// Set path for format schema files
if ( config ( ) . has ( " format_schema_path " ) )
global_context - > setFormatSchemaPath ( fs : : weakly_canonical ( config ( ) . getString ( " format_schema_path " ) ) ) ;
2023-11-15 13:55:43 +00:00
/// Set the path for google proto files
if ( config ( ) . has ( " google_protos_path " ) )
global_context - > setGoogleProtosPath ( fs : : weakly_canonical ( config ( ) . getString ( " google_protos_path " ) ) ) ;
2021-07-18 14:42:41 +00:00
}
2012-03-25 03:47:13 +00:00
2017-03-13 17:35:17 +00:00
2021-09-04 18:19:01 +00:00
int Client : : main ( const std : : vector < std : : string > & /*args*/ )
try
2021-07-18 14:42:41 +00:00
{
2021-09-04 18:19:01 +00:00
UseSSL use_ssl ;
2024-08-08 02:41:22 +00:00
auto & thread_status = MainThreadStatus : : getInstance ( ) ;
2021-10-02 08:10:34 +00:00
setupSignalHandler ( ) ;
2021-09-04 18:19:01 +00:00
std : : cout < < std : : fixed < < std : : setprecision ( 3 ) ;
std : : cerr < < std : : fixed < < std : : setprecision ( 3 ) ;
registerFormats ( ) ;
registerFunctions ( ) ;
registerAggregateFunctions ( ) ;
2021-08-04 16:56:15 +00:00
2021-09-04 18:19:01 +00:00
processConfig ( ) ;
2023-10-16 20:24:23 +00:00
adjustSettings ( ) ;
2023-11-23 17:13:12 +00:00
initTTYBuffer ( toProgressOption ( config ( ) . getString ( " progress " , " default " ) ) ) ;
2024-02-22 18:17:15 +00:00
ASTAlterCommand : : setFormatAlterCommandsWithParentheses ( true ) ;
2021-08-04 16:56:15 +00:00
2024-08-08 02:41:22 +00:00
{
// All that just to set DB::CurrentThread::get().getGlobalContext()
// which is required for client timezone (pushed from server) to work.
auto thread_group = std : : make_shared < ThreadGroup > ( ) ;
const_cast < ContextWeakPtr & > ( thread_group - > global_context ) = global_context ;
thread_status . attachToGroup ( thread_group , false ) ;
}
2021-11-14 07:19:59 +00:00
/// Includes delayed_interactive.
2021-09-04 18:19:01 +00:00
if ( is_interactive )
{
clearTerminal ( ) ;
showClientVersion ( ) ;
}
2017-04-01 07:20:54 +00:00
2023-02-22 14:58:06 +00:00
try
{
connect ( ) ;
}
catch ( const Exception & e )
{
2023-02-22 15:51:36 +00:00
if ( e . code ( ) ! = DB : : ErrorCodes : : AUTHENTICATION_FAILED | |
config ( ) . has ( " password " ) | |
config ( ) . getBool ( " ask-password " , false ) | |
! is_interactive )
throw ;
2023-02-22 14:58:06 +00:00
2023-02-22 15:51:36 +00:00
config ( ) . setBool ( " ask-password " , true ) ;
connect ( ) ;
2023-02-22 14:58:06 +00:00
}
2017-04-01 07:20:54 +00:00
2022-07-20 09:51:55 +00:00
/// Show warnings at the beginning of connection.
2021-11-14 07:19:59 +00:00
if ( is_interactive & & ! config ( ) . has ( " no-warnings " ) )
2022-07-20 09:51:55 +00:00
showWarnings ( ) ;
2021-07-28 12:56:11 +00:00
2022-12-11 23:47:43 +00:00
/// Set user password complexity rules
2024-07-25 14:36:32 +00:00
auto & access_control = client_context - > getAccessControl ( ) ;
2022-12-11 23:47:43 +00:00
access_control . setPasswordComplexityRules ( connection - > getPasswordComplexityRules ( ) ) ;
2022-11-22 02:41:23 +00:00
2021-10-29 12:04:08 +00:00
if ( is_interactive & & ! delayed_interactive )
2021-09-04 18:19:01 +00:00
{
runInteractive ( ) ;
}
else
{
connection - > setDefaultDatabase ( connection_parameters . default_database ) ;
2017-04-01 07:20:54 +00:00
2021-09-04 18:19:01 +00:00
runNonInteractive ( ) ;
2021-07-23 20:56:47 +00:00
2021-09-04 18:19:01 +00:00
// If exception code isn't zero, we should return non-zero return
// code anyway.
const auto * exception = server_exception ? server_exception . get ( ) : client_exception . get ( ) ;
2021-07-23 20:56:47 +00:00
2021-09-04 18:19:01 +00:00
if ( exception )
{
return exception - > code ( ) ! = 0 ? exception - > code ( ) : - 1 ;
}
if ( have_error )
{
// Shouldn't be set without an exception, but check it just in
// case so that at least we don't lose an error.
return - 1 ;
2017-04-01 07:20:54 +00:00
}
2021-10-29 12:04:08 +00:00
if ( delayed_interactive )
runInteractive ( ) ;
2021-07-27 09:29:35 +00:00
}
2017-04-01 07:20:54 +00:00
2021-07-18 14:42:41 +00:00
return 0 ;
}
2021-09-04 18:19:01 +00:00
catch ( const Exception & e )
{
2021-10-25 07:47:22 +00:00
bool need_print_stack_trace = config ( ) . getBool ( " stacktrace " , false ) & & e . code ( ) ! = ErrorCodes : : NETWORK_ERROR ;
std : : cerr < < getExceptionMessage ( e , need_print_stack_trace , true ) < < std : : endl < < std : : endl ;
2021-09-04 18:19:01 +00:00
/// If exception code isn't zero, we should return non-zero return code anyway.
return e . code ( ) ? e . code ( ) : - 1 ;
}
catch ( . . . )
{
std : : cerr < < getCurrentExceptionMessage ( false ) < < std : : endl ;
return getCurrentExceptionCode ( ) ;
}
2021-07-18 14:42:41 +00:00
void Client : : connect ( )
{
String server_name ;
UInt64 server_version_major = 0 ;
UInt64 server_version_minor = 0 ;
UInt64 server_version_patch = 0 ;
2022-02-21 12:08:23 +00:00
if ( hosts_and_ports . empty ( ) )
{
String host = config ( ) . getString ( " host " , " localhost " ) ;
2023-11-13 02:31:56 +00:00
UInt16 port = ConnectionParameters : : getPortFromConfig ( config ( ) , host ) ;
2022-02-21 12:08:23 +00:00
hosts_and_ports . emplace_back ( HostAndPort { host , port } ) ;
}
2022-02-10 09:43:08 +00:00
for ( size_t attempted_address_index = 0 ; attempted_address_index < hosts_and_ports . size ( ) ; + + attempted_address_index )
2021-07-18 14:42:41 +00:00
{
2021-12-13 14:35:27 +00:00
try
2021-08-21 15:22:53 +00:00
{
2022-02-21 12:08:23 +00:00
connection_parameters = ConnectionParameters (
config ( ) , hosts_and_ports [ attempted_address_index ] . host , hosts_and_ports [ attempted_address_index ] . port ) ;
2021-08-21 15:22:53 +00:00
2022-02-10 09:43:08 +00:00
if ( is_interactive )
std : : cout < < " Connecting to "
< < ( ! connection_parameters . default_database . empty ( ) ? " database " + connection_parameters . default_database + " at "
: " " )
< < connection_parameters . host < < " : " < < connection_parameters . port
< < ( ! connection_parameters . user . empty ( ) ? " as user " + connection_parameters . user : " " ) < < " . " < < std : : endl ;
2024-07-25 14:36:32 +00:00
connection = Connection : : createConnection ( connection_parameters , client_context ) ;
2021-08-21 15:22:53 +00:00
2021-12-13 14:35:27 +00:00
if ( max_client_network_bandwidth )
{
ThrottlerPtr throttler = std : : make_shared < Throttler > ( max_client_network_bandwidth , 0 , " " ) ;
connection - > setThrottler ( throttler ) ;
}
connection - > getServerVersion (
connection_parameters . timeouts , server_name , server_version_major , server_version_minor , server_version_patch , server_revision ) ;
2022-01-17 22:57:05 +00:00
config ( ) . setString ( " host " , connection_parameters . host ) ;
config ( ) . setInt ( " port " , connection_parameters . port ) ;
2021-12-13 14:35:27 +00:00
break ;
2021-08-20 08:08:21 +00:00
}
2021-12-13 14:35:27 +00:00
catch ( const Exception & e )
2021-08-20 08:08:21 +00:00
{
2022-11-22 18:34:38 +00:00
if ( e . code ( ) = = DB : : ErrorCodes : : AUTHENTICATION_FAILED )
2021-12-13 14:35:27 +00:00
{
2022-11-22 18:34:38 +00:00
/// This problem can't be fixed with reconnection so it is not attempted
2021-12-13 14:35:27 +00:00
throw ;
}
else
{
2022-02-10 09:43:08 +00:00
if ( attempted_address_index = = hosts_and_ports . size ( ) - 1 )
2021-12-13 14:35:27 +00:00
throw ;
2018-03-05 14:47:10 +00:00
2022-01-25 14:30:05 +00:00
if ( is_interactive )
{
2022-01-17 22:57:05 +00:00
std : : cerr < < " Connection attempt to database at "
< < connection_parameters . host < < " : " < < connection_parameters . port
< < " resulted in failure "
< < std : : endl
< < getExceptionMessage ( e , false )
< < std : : endl
< < " Attempting connection to the next provided address "
< < std : : endl ;
}
2021-12-13 14:35:27 +00:00
}
2021-08-20 08:08:21 +00:00
}
2021-08-21 15:22:53 +00:00
}
2019-09-10 19:28:33 +00:00
2021-07-18 14:42:41 +00:00
server_version = toString ( server_version_major ) + " . " + toString ( server_version_minor ) + " . " + toString ( server_version_patch ) ;
2022-07-20 09:51:55 +00:00
load_suggestions = is_interactive & & ( server_revision > = Suggest : : MIN_SERVER_REVISION ) & & ! config ( ) . getBool ( " disable_suggestion " , false ) ;
2024-04-08 18:54:28 +00:00
wait_for_suggestions_to_load = config ( ) . getBool ( " wait_for_suggestions_to_load " , false ) ;
2020-05-02 17:19:43 +00:00
2021-07-18 14:42:41 +00:00
if ( server_display_name = connection - > getServerDisplayName ( connection_parameters . timeouts ) ; server_display_name . empty ( ) )
server_display_name = config ( ) . getString ( " host " , " localhost " ) ;
if ( is_interactive )
{
2023-12-02 15:32:34 +00:00
std : : cout < < " Connected to " < < server_name < < " server version " < < server_version < < " . " < < std : : endl < < std : : endl ;
2021-07-18 14:42:41 +00:00
auto client_version_tuple = std : : make_tuple ( VERSION_MAJOR , VERSION_MINOR , VERSION_PATCH ) ;
auto server_version_tuple = std : : make_tuple ( server_version_major , server_version_minor , server_version_patch ) ;
if ( client_version_tuple < server_version_tuple )
{
std : : cout < < " ClickHouse client version is older than ClickHouse server. "
< < " It may lack support for new features. " < < std : : endl
< < std : : endl ;
}
2024-02-06 17:37:14 +00:00
else if ( client_version_tuple > server_version_tuple & & server_display_name ! = " clickhouse-cloud " )
2021-07-18 14:42:41 +00:00
{
std : : cout < < " ClickHouse server version is older than ClickHouse client. "
< < " It may indicate that the server is out of date and can be upgraded. " < < std : : endl
< < std : : endl ;
2017-04-01 07:20:54 +00:00
}
2021-07-18 14:42:41 +00:00
}
2021-06-15 01:43:35 +00:00
2024-09-18 12:20:53 +00:00
if ( ! client_context - > getSettingsRef ( ) [ Setting : : use_client_time_zone ] )
2021-07-18 14:42:41 +00:00
{
const auto & time_zone = connection - > getServerTimezone ( connection_parameters . timeouts ) ;
if ( ! time_zone . empty ( ) )
2021-06-15 01:43:35 +00:00
{
2021-07-18 14:42:41 +00:00
try
2021-06-15 01:43:35 +00:00
{
2021-07-18 14:42:41 +00:00
DateLUT : : setDefaultTimezone ( time_zone ) ;
2021-06-15 01:43:35 +00:00
}
2021-07-18 14:42:41 +00:00
catch ( . . . )
2021-06-15 01:43:35 +00:00
{
2021-07-18 14:42:41 +00:00
std : : cerr < < " Warning: could not switch to server time zone: " < < time_zone
< < " , reason: " < < getCurrentExceptionMessage ( /* with_stacktrace = */ false ) < < std : : endl
< < " Proceeding with local time zone. " < < std : : endl
< < std : : endl ;
2021-06-15 01:43:35 +00:00
}
}
2021-07-18 14:42:41 +00:00
else
2021-06-15 01:43:35 +00:00
{
2021-07-18 14:42:41 +00:00
std : : cerr < < " Warning: could not determine server time zone. "
< < " Proceeding with local time zone. " < < std : : endl
< < std : : endl ;
2021-06-15 01:43:35 +00:00
}
2021-07-18 14:42:41 +00:00
}
prompt_by_server_display_name = config ( ) . getRawString ( " prompt_by_server_display_name.default " , " {display_name} :) " ) ;
2021-06-15 01:43:35 +00:00
2021-07-18 14:42:41 +00:00
Strings keys ;
config ( ) . keys ( " prompt_by_server_display_name " , keys ) ;
for ( const String & key : keys )
{
if ( key ! = " default " & & server_display_name . find ( key ) ! = std : : string : : npos )
2021-06-15 01:43:35 +00:00
{
2021-07-18 14:42:41 +00:00
prompt_by_server_display_name = config ( ) . getRawString ( " prompt_by_server_display_name. " + key ) ;
break ;
2021-06-15 01:43:35 +00:00
}
2017-04-01 07:20:54 +00:00
}
2021-07-18 14:42:41 +00:00
/// Prompt may contain escape sequences including \e[ or \x1b[ sequences to set terminal color.
2017-04-01 07:20:54 +00:00
{
2021-07-18 14:42:41 +00:00
String unescaped_prompt_by_server_display_name ;
ReadBufferFromString in ( prompt_by_server_display_name ) ;
readEscapedString ( unescaped_prompt_by_server_display_name , in ) ;
prompt_by_server_display_name = std : : move ( unescaped_prompt_by_server_display_name ) ;
}
2017-12-19 19:46:02 +00:00
2021-07-18 14:42:41 +00:00
/// Prompt may contain the following substitutions in a form of {name}.
std : : map < String , String > prompt_substitutions {
{ " host " , connection_parameters . host } ,
{ " port " , toString ( connection_parameters . port ) } ,
{ " user " , connection_parameters . user } ,
{ " display_name " , server_display_name } ,
} ;
/// Quite suboptimal.
for ( const auto & [ key , value ] : prompt_substitutions )
boost : : replace_all ( prompt_by_server_display_name , " { " + key + " } " , value ) ;
}
2021-01-16 15:03:32 +00:00
2021-07-18 14:42:41 +00:00
// Prints changed settings to stderr. Useful for debugging fuzzing failures.
void Client : : printChangedSettings ( ) const
{
2022-07-13 20:57:24 +00:00
auto print_changes = [ ] ( const auto & changes , std : : string_view settings_name )
2021-07-18 14:42:41 +00:00
{
2022-07-13 20:57:24 +00:00
if ( ! changes . empty ( ) )
2021-01-18 22:40:30 +00:00
{
2022-07-13 20:57:24 +00:00
fmt : : print ( stderr , " Changed {}: " , settings_name ) ;
for ( size_t i = 0 ; i < changes . size ( ) ; + + i )
2021-01-28 20:16:35 +00:00
{
2022-07-13 20:57:24 +00:00
if ( i )
fmt : : print ( stderr , " , " ) ;
fmt : : print ( stderr , " {} = '{}' " , changes [ i ] . name , toString ( changes [ i ] . value ) ) ;
2021-01-28 20:16:35 +00:00
}
2022-07-13 20:57:24 +00:00
fmt : : print ( stderr , " \n " ) ;
2021-01-18 22:40:30 +00:00
}
2022-07-13 20:57:24 +00:00
else
{
fmt : : print ( stderr , " No changed {}. \n " , settings_name ) ;
}
} ;
2024-07-25 14:36:32 +00:00
print_changes ( client_context - > getSettingsRef ( ) . changes ( ) , " settings " ) ;
2022-07-13 20:57:24 +00:00
print_changes ( cmd_merge_tree_settings . changes ( ) , " MergeTree settings " ) ;
2021-07-18 14:42:41 +00:00
}
2021-01-18 22:40:30 +00:00
2021-07-18 14:42:41 +00:00
2022-04-20 09:26:44 +00:00
static bool queryHasWithClause ( const IAST & ast )
2021-08-19 11:07:47 +00:00
{
2022-04-20 09:26:44 +00:00
if ( const auto * select = dynamic_cast < const ASTSelectQuery * > ( & ast ) ; select & & select - > with ( ) )
2021-08-19 11:07:47 +00:00
{
2021-08-21 10:55:54 +00:00
return true ;
2021-08-19 11:07:47 +00:00
}
2021-08-21 10:55:54 +00:00
// This full recursive walk is somewhat excessive, because most of the
// children are not queries, but on the other hand it will let us to avoid
// breakage when the AST structure changes and some new variant of query
// nesting is added. This function is used in fuzzer, so it's better to be
// defensive and avoid weird unexpected errors.
2022-04-20 09:26:44 +00:00
for ( const auto & child : ast . children )
2021-08-19 11:07:47 +00:00
{
2022-04-20 09:26:44 +00:00
if ( queryHasWithClause ( * child ) )
2021-08-19 11:07:47 +00:00
{
2021-08-21 10:55:54 +00:00
return true ;
2021-08-19 11:07:47 +00:00
}
}
2021-08-21 10:55:54 +00:00
return false ;
2021-08-19 11:07:47 +00:00
}
2022-08-11 02:34:10 +00:00
std : : optional < bool > Client : : processFuzzingStep ( const String & query_to_execute , const ASTPtr & parsed_query )
{
processParsedSingleQuery ( query_to_execute , query_to_execute , parsed_query ) ;
const auto * exception = server_exception ? server_exception . get ( ) : client_exception . get ( ) ;
// Sometimes you may get TOO_DEEP_RECURSION from the server,
// and TOO_DEEP_RECURSION should not fail the fuzzer check.
if ( have_error & & exception - > code ( ) = = ErrorCodes : : TOO_DEEP_RECURSION )
{
have_error = false ;
server_exception . reset ( ) ;
client_exception . reset ( ) ;
return true ;
}
if ( have_error )
{
fmt : : print ( stderr , " Error on processing query '{}': {} \n " , parsed_query - > formatForErrorMessage ( ) , exception - > message ( ) ) ;
// Try to reconnect after errors, for two reasons:
// 1. We might not have realized that the server died, e.g. if
// it sent us a <Fatal> trace and closed connection properly.
// 2. The connection might have gotten into a wrong state and
// the next query will get false positive about
// "Unknown packet from server".
try
{
connection - > forceConnected ( connection_parameters . timeouts ) ;
}
catch ( . . . )
{
// Just report it, we'll terminate below.
fmt : : print ( stderr ,
" Error while reconnecting to the server: {} \n " ,
getCurrentExceptionMessage ( true ) ) ;
// The reconnection might fail, but we'll still be connected
// in the sense of `connection->isConnected() = true`,
// in case when the requested database doesn't exist.
// Disconnect manually now, so that the following code doesn't
// have any doubts, and the connection state is predictable.
connection - > disconnect ( ) ;
}
}
if ( ! connection - > isConnected ( ) )
{
// Probably the server is dead because we found an assertion
// failure. Fail fast.
fmt : : print ( stderr , " Lost connection to the server. \n " ) ;
// Print the changed settings because they might be needed to
// reproduce the error.
printChangedSettings ( ) ;
return false ;
}
return std : : nullopt ;
}
2021-08-19 11:07:47 +00:00
2021-07-18 14:42:41 +00:00
/// Returns false when server is not available.
2021-07-28 12:56:11 +00:00
bool Client : : processWithFuzzing ( const String & full_query )
2021-07-18 14:42:41 +00:00
{
ASTPtr orig_ast ;
try
{
2021-07-28 12:56:11 +00:00
const char * begin = full_query . data ( ) ;
2024-03-26 09:53:08 +00:00
orig_ast = parseQuery ( begin , begin + full_query . size ( ) ,
2024-07-25 14:36:32 +00:00
client_context - > getSettingsRef ( ) ,
2024-06-25 14:23:37 +00:00
/*allow_multi_statements=*/ true ) ;
2021-07-18 14:42:41 +00:00
}
catch ( const Exception & e )
{
if ( e . code ( ) ! = ErrorCodes : : SYNTAX_ERROR & &
e . code ( ) ! = ErrorCodes : : TOO_DEEP_RECURSION )
throw ;
2021-01-18 22:40:30 +00:00
}
2021-12-07 16:09:27 +00:00
if ( ! orig_ast )
2021-10-02 08:15:34 +00:00
{
2021-12-07 16:09:27 +00:00
// Can't continue after a parsing error
2021-10-02 08:15:34 +00:00
return true ;
}
2021-12-07 16:09:27 +00:00
// `USE db` should not be executed
// since this will break every query after `DROP db`
if ( orig_ast - > as < ASTUseQuery > ( ) )
2021-03-12 20:51:53 +00:00
{
2021-07-18 14:42:41 +00:00
return true ;
}
2023-10-11 06:50:58 +00:00
// Kusto is not a subject for fuzzing (yet)
2024-09-18 12:20:53 +00:00
if ( client_context - > getSettingsRef ( ) [ Setting : : dialect ] = = DB : : Dialect : : kusto )
2023-08-26 14:35:14 +00:00
{
2023-10-11 06:50:58 +00:00
return true ;
2023-08-26 14:35:14 +00:00
}
2023-05-30 01:32:53 +00:00
if ( auto * q = orig_ast - > as < ASTSetQuery > ( ) )
2023-05-23 02:41:45 +00:00
{
2024-06-13 16:40:44 +00:00
if ( auto * set_dialect = q - > changes . tryGet ( " dialect " ) ; set_dialect & & set_dialect - > safeGet < String > ( ) = = " kusto " )
2023-10-11 06:50:58 +00:00
return true ;
2023-09-25 20:43:01 +00:00
}
2023-05-23 02:41:45 +00:00
2021-10-02 08:09:20 +00:00
// Don't repeat:
// - INSERT -- Because the tables may grow too big.
// - CREATE -- Because first we run the unmodified query, it will succeed,
// and the subsequent queries will fail.
// When we run out of fuzzer errors, it may be interesting to
// add fuzzing of create queries that wraps columns into
// LowCardinality or Nullable.
// Also there are other kinds of create queries such as CREATE
// DICTIONARY, we could fuzz them as well.
// - DROP -- No point in this (by the same reasons).
// - SET -- The time to fuzz the settings has not yet come
// (see comments in Client/QueryFuzzer.cpp)
2021-07-18 14:42:41 +00:00
size_t this_query_runs = query_fuzzer_runs ;
2022-08-12 00:32:21 +00:00
ASTs queries_for_fuzzed_tables ;
2022-08-11 02:34:10 +00:00
2022-08-12 00:32:21 +00:00
if ( orig_ast - > as < ASTSetQuery > ( ) )
2021-07-18 14:42:41 +00:00
{
this_query_runs = 1 ;
2021-03-12 20:51:53 +00:00
}
2022-08-11 02:34:10 +00:00
else if ( const auto * create = orig_ast - > as < ASTCreateQuery > ( ) )
{
2022-09-28 14:52:10 +00:00
if ( QueryFuzzer : : isSuitableForFuzzing ( * create ) )
2022-08-11 02:34:10 +00:00
this_query_runs = create_query_fuzzer_runs ;
else
this_query_runs = 1 ;
}
else if ( const auto * insert = orig_ast - > as < ASTInsertQuery > ( ) )
2021-07-18 14:42:41 +00:00
{
this_query_runs = 1 ;
2022-08-12 00:32:21 +00:00
queries_for_fuzzed_tables = fuzzer . getInsertQueriesForFuzzedTables ( full_query ) ;
}
else if ( const auto * drop = orig_ast - > as < ASTDropQuery > ( ) )
{
this_query_runs = 1 ;
queries_for_fuzzed_tables = fuzzer . getDropQueriesForFuzzedTables ( * drop ) ;
2021-03-12 20:51:53 +00:00
}
2017-04-01 07:20:54 +00:00
2021-07-28 12:56:11 +00:00
String query_to_execute ;
2021-07-18 14:42:41 +00:00
ASTPtr fuzz_base = orig_ast ;
2022-08-11 02:34:10 +00:00
2021-07-18 14:42:41 +00:00
for ( size_t fuzz_step = 0 ; fuzz_step < this_query_runs ; + + fuzz_step )
2020-06-26 05:44:42 +00:00
{
2021-07-18 14:42:41 +00:00
fmt : : print ( stderr , " Fuzzing step {} out of {} \n " , fuzz_step , this_query_runs ) ;
2020-06-26 05:44:42 +00:00
2021-07-18 14:42:41 +00:00
ASTPtr ast_to_process ;
2021-01-05 21:34:08 +00:00
try
2020-06-26 05:44:42 +00:00
{
2021-07-18 14:42:41 +00:00
WriteBufferFromOwnString dump_before_fuzz ;
fuzz_base - > dumpTree ( dump_before_fuzz ) ;
auto base_before_fuzz = fuzz_base - > formatForErrorMessage ( ) ;
ast_to_process = fuzz_base - > clone ( ) ;
WriteBufferFromOwnString dump_of_cloned_ast ;
ast_to_process - > dumpTree ( dump_of_cloned_ast ) ;
// Run the original query as well.
if ( fuzz_step > 0 )
{
fuzzer . fuzzMain ( ast_to_process ) ;
}
auto base_after_fuzz = fuzz_base - > formatForErrorMessage ( ) ;
// Check that the source AST didn't change after fuzzing. This
// helps debug AST cloning errors, where the cloned AST doesn't
// clone all its children, and erroneously points to some source
// child elements.
if ( base_before_fuzz ! = base_after_fuzz )
{
printChangedSettings ( ) ;
fmt : : print (
stderr ,
" Base before fuzz: {} \n "
" Base after fuzz: {} \n " ,
base_before_fuzz ,
base_after_fuzz ) ;
fmt : : print ( stderr , " Dump before fuzz: \n {} \n " , dump_before_fuzz . str ( ) ) ;
fmt : : print ( stderr , " Dump of cloned AST: \n {} \n " , dump_of_cloned_ast . str ( ) ) ;
fmt : : print ( stderr , " Dump after fuzz: \n " ) ;
WriteBufferFromOStream cerr_buf ( std : : cerr , 4096 ) ;
fuzz_base - > dumpTree ( cerr_buf ) ;
2023-06-26 14:03:47 +00:00
cerr_buf . finalize ( ) ;
2021-07-18 14:42:41 +00:00
fmt : : print (
stderr ,
" 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. " ) ;
2022-08-20 15:09:20 +00:00
_exit ( 1 ) ;
2021-07-18 14:42:41 +00:00
}
auto fuzzed_text = ast_to_process - > formatForErrorMessage ( ) ;
if ( fuzz_step > 0 & & fuzzed_text = = base_before_fuzz )
{
fmt : : print ( stderr , " Got boring AST \n " ) ;
continue ;
}
2022-08-11 02:34:10 +00:00
query_to_execute = ast_to_process - > formatForErrorMessage ( ) ;
2023-08-26 14:35:14 +00:00
if ( auto res = processFuzzingStep ( query_to_execute , ast_to_process ) )
return * res ;
2021-01-05 21:34:08 +00:00
}
2021-07-18 14:42:41 +00:00
catch ( . . . )
2021-01-05 21:34:08 +00:00
{
2023-05-30 01:28:16 +00:00
if ( ! ast_to_process )
fmt : : print ( stderr ,
" Error while forming new query: {} \n " ,
getCurrentExceptionMessage ( true ) ) ;
2021-07-18 14:42:41 +00:00
// Some functions (e.g. protocol parsers) don't throw, but
// set last_exception instead, so we'll also do it here for
// uniformity.
// Surprisingly, this is a client exception, because we get the
// server exception w/o throwing (see onReceiveException()).
2023-01-23 13:16:14 +00:00
client_exception = std : : make_unique < Exception > ( getCurrentExceptionMessageAndPattern ( print_stack_trace ) , getCurrentExceptionCode ( ) ) ;
2021-07-18 14:42:41 +00:00
have_error = true ;
2021-01-05 21:34:08 +00:00
}
2020-06-26 05:44:42 +00:00
2024-02-17 01:24:21 +00:00
// The server is still alive, so we're going to continue fuzzing.
2021-07-18 14:42:41 +00:00
// Determine what we're going to use as the starting AST.
if ( have_error )
{
// Query completed with error, keep the previous starting AST.
// Also discard the exception that we now know to be non-fatal,
// so that it doesn't influence the exit code.
server_exception . reset ( ) ;
client_exception . reset ( ) ;
2022-09-28 15:55:39 +00:00
fuzzer . notifyQueryFailed ( ast_to_process ) ;
2021-07-18 14:42:41 +00:00
have_error = false ;
}
else if ( ast_to_process - > formatForErrorMessage ( ) . size ( ) > 500 )
{
// ast too long, start from original ast
fmt : : print ( stderr , " Current AST is too long, discarding it and using the original AST as a start \n " ) ;
fuzz_base = orig_ast ;
}
else
{
// fuzz starting from this successful query
fmt : : print ( stderr , " Query succeeded, using this AST as a start \n " ) ;
fuzz_base = ast_to_process ;
}
}
2021-02-10 11:18:11 +00:00
2022-09-28 14:52:10 +00:00
for ( const auto & query : queries_for_fuzzed_tables )
2022-08-11 02:34:10 +00:00
{
std : : cout < < std : : endl ;
WriteBufferFromOStream ast_buf ( std : : cout , 4096 ) ;
2022-09-28 14:52:10 +00:00
formatAST ( * query , ast_buf , false /*highlight*/ ) ;
2023-06-26 14:03:47 +00:00
ast_buf . finalize ( ) ;
2022-11-22 12:10:53 +00:00
if ( const auto * insert = query - > as < ASTInsertQuery > ( ) )
{
/// For inserts with data it's really useful to have the data itself available in the logs, as formatAST doesn't print it
if ( insert - > hasInlinedData ( ) )
{
String bytes ;
{
auto read_buf = getReadBufferFromASTInsertQuery ( query ) ;
WriteBufferFromString write_buf ( bytes ) ;
copyData ( * read_buf , write_buf ) ;
}
std : : cout < < std : : endl < < bytes ;
}
}
2022-08-11 02:34:10 +00:00
std : : cout < < std : : endl < < std : : endl ;
try
{
2022-09-28 14:52:10 +00:00
query_to_execute = query - > formatForErrorMessage ( ) ;
if ( auto res = processFuzzingStep ( query_to_execute , query ) )
2022-08-11 02:34:10 +00:00
return * res ;
}
catch ( . . . )
{
2023-01-23 13:16:14 +00:00
client_exception = std : : make_unique < Exception > ( getCurrentExceptionMessageAndPattern ( print_stack_trace ) , getCurrentExceptionCode ( ) ) ;
2022-08-11 02:34:10 +00:00
have_error = true ;
}
if ( have_error )
{
server_exception . reset ( ) ;
client_exception . reset ( ) ;
2022-09-28 21:47:10 +00:00
fuzzer . notifyQueryFailed ( query ) ;
2022-08-11 02:34:10 +00:00
have_error = false ;
}
}
2021-07-18 14:42:41 +00:00
return true ;
}
2021-02-10 11:18:11 +00:00
2020-06-26 05:44:42 +00:00
2024-04-26 11:19:22 +00:00
void Client : : printHelpMessage ( const OptionsDescription & options_description , bool verbose )
2021-07-18 14:42:41 +00:00
{
std : : cout < < options_description . main_description . value ( ) < < " \n " ;
std : : cout < < options_description . external_description . value ( ) < < " \n " ;
2022-03-15 18:36:13 +00:00
std : : cout < < options_description . hosts_and_ports_description . value ( ) < < " \n " ;
2024-04-26 11:19:22 +00:00
if ( verbose )
std : : cout < < " All settings are documented at https://clickhouse.com/docs/en/operations/settings/settings. \n \n " ;
2021-07-18 14:42:41 +00:00
std : : cout < < " In addition, --param_name=value can be specified for substitution of parameters for parametrized queries. \n " ;
2024-02-11 19:00:37 +00:00
std : : cout < < " \n See also: https://clickhouse.com/docs/en/integrations/sql-clients/cli \n " ;
2021-07-18 14:42:41 +00:00
}
2017-03-13 17:35:17 +00:00
2018-04-20 19:31:19 +00:00
2021-10-14 13:34:05 +00:00
void Client : : addOptions ( OptionsDescription & options_description )
2021-07-18 14:42:41 +00:00
{
/// Main commandline options related to client functionality and all parameters from Settings.
options_description . main_description - > add_options ( )
( " config,c " , po : : value < std : : string > ( ) , " config-file path (another shorthand) " )
2023-02-16 12:38:25 +00:00
( " connection " , po : : value < std : : string > ( ) , " connection to use (from the client config), by default connection name is hostname " )
2021-07-18 14:42:41 +00:00
( " secure,s " , " Use TLS connection " )
2024-03-18 14:22:50 +00:00
( " no-secure " , " Don't use TLS connection " )
2021-07-18 14:42:41 +00:00
( " user,u " , po : : value < std : : string > ( ) - > default_value ( " default " ) , " user " )
2023-06-14 01:37:58 +00:00
( " password " , po : : value < std : : string > ( ) , " password " )
2021-07-18 14:42:41 +00:00
( " ask-password " , " ask-password " )
2024-04-04 20:58:35 +00:00
( " ssh-key-file " , po : : value < std : : string > ( ) , " File containing the SSH private key for authenticate with the server. " )
( " ssh-key-passphrase " , po : : value < std : : string > ( ) , " Passphrase for the SSH private key specified by --ssh-key-file. " )
2021-07-18 14:42:41 +00:00
( " quota_key " , po : : value < std : : string > ( ) , " A string to differentiate quotas when the user have keyed quotas configured on server " )
2024-04-22 01:36:34 +00:00
( " jwt " , po : : value < std : : string > ( ) , " Use JWT for authentication " )
2021-08-20 21:19:06 +00:00
2021-07-18 14:42:41 +00:00
( " max_client_network_bandwidth " , po : : value < int > ( ) , " the maximum speed of data exchange over the network for the client in bytes per second. " )
2022-03-30 11:45:30 +00:00
( " compression " , po : : value < bool > ( ) , " enable or disable compression (enabled by default for remote communication and disabled for localhost communication). " )
2021-08-20 21:19:06 +00:00
2021-07-18 14:42:41 +00:00
( " 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. " )
2022-08-11 02:34:10 +00:00
( " create-query-fuzzer-runs " , po : : value < int > ( ) - > default_value ( 0 ) , " " )
2021-07-18 14:42:41 +00:00
( " interleave-queries-file " , po : : value < std : : vector < std : : string > > ( ) - > multitoken ( ) ,
" 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) " )
2021-08-20 21:19:06 +00:00
2021-07-18 14:42:41 +00:00
( " opentelemetry-traceparent " , po : : value < std : : string > ( ) , " OpenTelemetry traceparent header as described by W3C Trace Context recommendation " )
( " opentelemetry-tracestate " , po : : value < std : : string > ( ) , " OpenTelemetry tracestate header as described by W3C Trace Context recommendation " )
2021-08-20 21:19:06 +00:00
2021-07-22 19:38:37 +00:00
( " no-warnings " , " disable warnings when client connects to server " )
2024-03-18 16:26:05 +00:00
/// TODO: Left for compatibility as it's used in upgrade check, remove after next release and use server setting ignore_drop_queries_probability
2022-02-18 13:54:21 +00:00
( " fake-drop " , " Ignore all DROP queries, should be used only for testing " )
2022-09-26 00:54:04 +00:00
( " accept-invalid-certificate " , " Ignore certificate verification errors, equal to config parameters openSSL.client.invalidCertificateHandler.name=AcceptCertificateHandler and openSSL.client.verificationMode=none " )
2021-07-18 14:42:41 +00:00
;
/// Commandline options related to external tables.
options_description . external_description . emplace ( createOptionsDescription ( " External tables options " , terminal_width ) ) ;
options_description . external_description - > add_options ( )
(
" file " , po : : value < std : : string > ( ) , " data file or - for stdin "
)
(
" name " , po : : value < std : : string > ( ) - > default_value ( " _data " ) , " name of the table "
)
(
" format " , po : : value < std : : string > ( ) - > default_value ( " TabSeparated " ) , " data format "
)
(
" structure " , po : : value < std : : string > ( ) , " structure "
)
(
" types " , po : : value < std : : string > ( ) , " types "
) ;
2022-02-10 09:43:08 +00:00
/// Commandline options related to hosts and ports.
options_description . hosts_and_ports_description . emplace ( createOptionsDescription ( " Hosts and ports options " , terminal_width ) ) ;
options_description . hosts_and_ports_description - > add_options ( )
( " host,h " , po : : value < String > ( ) - > default_value ( " localhost " ) ,
2022-02-10 11:07:21 +00:00
" Server hostname. Multiple hosts can be passed via multiple arguments "
2022-02-15 12:12:21 +00:00
" Example of usage: '--host host1 --host host2 --port port2 --host host3 ...' "
" Each '--port port' will be attached to the last seen host that doesn't have a port yet, "
" if there is no such host, the port will be attached to the next first host or to default host. " )
2022-03-10 19:41:03 +00:00
( " port " , po : : value < UInt16 > ( ) , " server ports " )
2022-02-10 09:43:08 +00:00
;
2021-07-18 14:42:41 +00:00
}
2018-05-23 16:12:46 +00:00
2017-04-01 07:20:54 +00:00
2021-07-18 14:42:41 +00:00
void Client : : processOptions ( const OptionsDescription & options_description ,
const CommandLineOptions & options ,
2022-02-10 09:43:08 +00:00
const std : : vector < Arguments > & external_tables_arguments ,
const std : : vector < Arguments > & hosts_and_ports_arguments )
2021-07-18 14:42:41 +00:00
{
namespace po = boost : : program_options ;
size_t number_of_external_tables_with_stdin_source = 0 ;
for ( size_t i = 0 ; i < external_tables_arguments . size ( ) ; + + i )
2021-07-11 11:36:27 +00:00
{
2021-07-18 14:42:41 +00:00
/// Parse commandline options related to external tables.
po : : parsed_options parsed_tables = po : : command_line_parser ( external_tables_arguments [ i ] ) . options (
options_description . external_description . value ( ) ) . run ( ) ;
po : : variables_map external_options ;
po : : store ( parsed_tables , external_options ) ;
2018-06-06 20:57:07 +00:00
2021-07-18 14:42:41 +00:00
try
2017-04-01 07:20:54 +00:00
{
2021-07-18 14:42:41 +00:00
external_tables . emplace_back ( external_options ) ;
if ( external_tables . back ( ) . file = = " - " )
+ + number_of_external_tables_with_stdin_source ;
if ( number_of_external_tables_with_stdin_source > 1 )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " Two or more external tables has stdin (-) set as - - file field " ) ;
2017-04-01 07:20:54 +00:00
}
2021-07-18 14:42:41 +00:00
catch ( const Exception & e )
2019-04-25 14:08:20 +00:00
{
2021-07-22 19:25:50 +00:00
std : : cerr < < getExceptionMessage ( e , false ) < < std : : endl ;
2021-07-18 14:42:41 +00:00
std : : cerr < < " Table № " < < i < < std : : endl < < std : : endl ;
/// Avoid the case when error exit code can possibly overflow to normal (zero).
auto exit_code = e . code ( ) % 256 ;
if ( exit_code = = 0 )
exit_code = 255 ;
2022-08-20 15:09:20 +00:00
_exit ( exit_code ) ;
2018-11-06 11:23:19 +00:00
}
2021-07-18 14:42:41 +00:00
}
2022-02-10 09:43:08 +00:00
2022-02-21 12:08:23 +00:00
for ( const auto & hosts_and_ports_argument : hosts_and_ports_arguments )
2022-02-10 09:43:08 +00:00
{
2022-02-21 12:08:23 +00:00
/// Parse commandline options related to external tables.
po : : parsed_options parsed_hosts_and_ports
= po : : command_line_parser ( hosts_and_ports_argument ) . options ( options_description . hosts_and_ports_description . value ( ) ) . run ( ) ;
po : : variables_map host_and_port_options ;
po : : store ( parsed_hosts_and_ports , host_and_port_options ) ;
2022-03-10 19:41:03 +00:00
std : : string host = host_and_port_options [ " host " ] . as < std : : string > ( ) ;
std : : optional < UInt16 > port = ! host_and_port_options [ " port " ] . empty ( )
? std : : make_optional ( host_and_port_options [ " port " ] . as < UInt16 > ( ) )
: std : : nullopt ;
hosts_and_ports . emplace_back ( HostAndPort { host , port } ) ;
2022-02-10 09:43:08 +00:00
}
2021-09-24 08:29:01 +00:00
send_external_tables = true ;
2017-03-13 17:35:17 +00:00
2021-07-18 14:42:41 +00:00
shared_context = Context : : createShared ( ) ;
global_context = Context : : createGlobal ( shared_context . get ( ) ) ;
2020-06-30 09:25:23 +00:00
2021-07-18 14:42:41 +00:00
global_context - > makeGlobalContext ( ) ;
global_context - > setApplicationType ( Context : : ApplicationType : : CLIENT ) ;
2020-06-26 05:44:42 +00:00
2021-07-18 14:42:41 +00:00
global_context - > setSettings ( cmd_settings ) ;
/// Copy settings-related program options to config.
/// TODO: Is this code necessary?
2024-09-18 12:20:53 +00:00
global_context - > getSettingsRef ( ) . addToClientOptions ( config ( ) , options , allow_repeated_settings ) ;
2021-07-18 14:42:41 +00:00
if ( options . count ( " config-file " ) & & options . count ( " config " ) )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " Two or more configuration files referenced in arguments " ) ;
2021-07-18 14:42:41 +00:00
if ( options . count ( " config " ) )
config ( ) . setString ( " config-file " , options [ " config " ] . as < std : : string > ( ) ) ;
2023-02-16 12:38:25 +00:00
if ( options . count ( " connection " ) )
config ( ) . setString ( " connection " , options [ " connection " ] . as < std : : string > ( ) ) ;
2021-07-18 14:42:41 +00:00
if ( options . count ( " interleave-queries-file " ) )
interleave_queries_files = options [ " interleave-queries-file " ] . as < std : : vector < std : : string > > ( ) ;
if ( options . count ( " secure " ) )
config ( ) . setBool ( " secure " , true ) ;
2024-01-22 16:00:56 +00:00
if ( options . count ( " no-secure " ) )
config ( ) . setBool ( " no-secure " , true ) ;
2021-07-18 14:42:41 +00:00
if ( options . count ( " user " ) & & ! options [ " user " ] . defaulted ( ) )
config ( ) . setString ( " user " , options [ " user " ] . as < std : : string > ( ) ) ;
if ( options . count ( " password " ) )
config ( ) . setString ( " password " , options [ " password " ] . as < std : : string > ( ) ) ;
if ( options . count ( " ask-password " ) )
config ( ) . setBool ( " ask-password " , true ) ;
2023-09-26 15:50:19 +00:00
if ( options . count ( " ssh-key-file " ) )
config ( ) . setString ( " ssh-key-file " , options [ " ssh-key-file " ] . as < std : : string > ( ) ) ;
if ( options . count ( " ssh-key-passphrase " ) )
config ( ) . setString ( " ssh-key-passphrase " , options [ " ssh-key-passphrase " ] . as < std : : string > ( ) ) ;
2021-07-18 14:42:41 +00:00
if ( options . count ( " quota_key " ) )
config ( ) . setString ( " quota_key " , options [ " quota_key " ] . as < std : : string > ( ) ) ;
if ( options . count ( " max_client_network_bandwidth " ) )
max_client_network_bandwidth = options [ " max_client_network_bandwidth " ] . as < int > ( ) ;
if ( options . count ( " compression " ) )
config ( ) . setBool ( " compression " , options [ " compression " ] . as < bool > ( ) ) ;
2021-07-22 19:38:37 +00:00
if ( options . count ( " no-warnings " ) )
config ( ) . setBool ( " no-warnings " , true ) ;
2021-08-20 12:17:51 +00:00
if ( options . count ( " fake-drop " ) )
2024-03-18 16:26:05 +00:00
config ( ) . setString ( " ignore_drop_queries_probability " , " 1 " ) ;
2024-04-22 01:36:34 +00:00
if ( options . count ( " jwt " ) )
{
2024-06-19 00:15:18 +00:00
if ( ! options [ " user " ] . defaulted ( ) )
2024-06-14 13:47:37 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " User and JWT flags can't be specified together " ) ;
2024-04-22 01:36:34 +00:00
config ( ) . setString ( " jwt " , options [ " jwt " ] . as < std : : string > ( ) ) ;
2024-07-03 21:48:18 +00:00
config ( ) . setString ( " user " , " " ) ;
2024-04-22 01:36:34 +00:00
}
2022-09-26 00:54:04 +00:00
if ( options . count ( " accept-invalid-certificate " ) )
2022-09-24 04:46:19 +00:00
{
config ( ) . setString ( " openSSL.client.invalidCertificateHandler.name " , " AcceptCertificateHandler " ) ;
config ( ) . setString ( " openSSL.client.verificationMode " , " none " ) ;
}
else
config ( ) . setString ( " openSSL.client.invalidCertificateHandler.name " , " RejectCertificateHandler " ) ;
2021-07-18 14:42:41 +00:00
if ( ( query_fuzzer_runs = options [ " query-fuzzer-runs " ] . as < int > ( ) ) )
{
// Ignore errors in parsing queries.
config ( ) . setBool ( " ignore-error " , true ) ;
ignore_error = true ;
}
2020-08-28 23:25:30 +00:00
2022-08-11 02:34:10 +00:00
if ( ( create_query_fuzzer_runs = options [ " create-query-fuzzer-runs " ] . as < int > ( ) ) )
{
// Ignore errors in parsing queries.
config ( ) . setBool ( " ignore-error " , true ) ;
global_context - > setSetting ( " allow_suspicious_low_cardinality_types " , true ) ;
ignore_error = true ;
}
2021-07-18 14:42:41 +00:00
if ( options . count ( " opentelemetry-traceparent " ) )
{
2021-07-22 21:27:26 +00:00
String traceparent = options [ " opentelemetry-traceparent " ] . as < std : : string > ( ) ;
String error ;
2023-07-07 10:49:50 +00:00
if ( ! global_context - > getClientTraceContext ( ) . parseTraceparentHeader ( traceparent , error ) )
2021-07-18 14:42:41 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " Cannot parse OpenTelemetry traceparent '{}': {} " , traceparent , error ) ;
}
2021-07-11 20:35:29 +00:00
2021-07-18 14:42:41 +00:00
if ( options . count ( " opentelemetry-tracestate " ) )
2023-07-07 10:49:50 +00:00
global_context - > getClientTraceContext ( ) . tracestate = options [ " opentelemetry-tracestate " ] . as < std : : string > ( ) ;
2024-07-27 04:32:26 +00:00
/// In case of clickhouse-client the `client_context` can be just an alias for the `global_context`.
/// (There is no need to copy the context because clickhouse-client has no background tasks so it won't use that context in parallel.)
client_context = global_context ;
initClientContext ( ) ;
2021-07-22 21:27:26 +00:00
}
void Client : : processConfig ( )
{
2023-09-04 14:48:38 +00:00
if ( ! queries . empty ( ) & & config ( ) . has ( " queries-file " ) )
2023-05-28 22:25:40 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " Options '--query' and '--queries-file' cannot be specified at the same time " ) ;
2021-07-23 20:54:49 +00:00
/// Batch mode is enabled if one of the following is true:
2023-05-17 03:43:35 +00:00
/// - -q (--query) command line option is present.
2021-07-23 20:54:49 +00:00
/// The value of the option is used as the text of query (or of multiple queries).
/// If stdin is not a terminal, INSERT data for the first query is read from it.
/// - stdin is not a terminal. In this case queries are read from it.
2022-06-20 22:09:55 +00:00
/// - --queries-file command line option is present.
2021-07-23 20:54:49 +00:00
/// The value of the option is used as file with query (or of multiple queries) to execute.
2023-09-04 14:48:38 +00:00
delayed_interactive = config ( ) . has ( " interactive " ) & & ( ! queries . empty ( ) | | config ( ) . has ( " queries-file " ) ) ;
2021-10-29 12:04:08 +00:00
if ( stdin_is_a_tty
2023-09-04 14:48:38 +00:00
& & ( delayed_interactive | | ( queries . empty ( ) & & queries_files . empty ( ) ) ) )
2021-10-29 12:04:08 +00:00
{
2021-07-23 20:54:49 +00:00
is_interactive = true ;
}
else
{
echo_queries = config ( ) . getBool ( " echo " , false ) ;
ignore_error = config ( ) . getBool ( " ignore-error " , false ) ;
2021-07-23 20:56:47 +00:00
auto query_id = config ( ) . getString ( " query_id " , " " ) ;
if ( ! query_id . empty ( ) )
global_context - > setCurrentQueryId ( query_id ) ;
2021-07-23 20:54:49 +00:00
}
2021-10-16 11:28:57 +00:00
print_stack_trace = config ( ) . getBool ( " stacktrace " , false ) ;
2021-07-23 20:54:49 +00:00
2023-10-16 20:26:49 +00:00
pager = config ( ) . getString ( " pager " , " " ) ;
2024-05-13 07:21:01 +00:00
setDefaultFormatsAndCompressionFromConfiguration ( ) ;
2021-07-18 14:42:41 +00:00
}
2021-07-11 20:35:29 +00:00
2022-03-23 03:03:25 +00:00
2022-03-01 09:22:12 +00:00
void Client : : readArguments (
int argc ,
char * * argv ,
Arguments & common_arguments ,
std : : vector < Arguments > & external_tables_arguments ,
std : : vector < Arguments > & hosts_and_ports_arguments )
{
2023-06-07 03:16:29 +00:00
bool has_connection_string = argc > = 2 & & tryParseConnectionString ( std : : string_view ( argv [ 1 ] ) , common_arguments , hosts_and_ports_arguments ) ;
int start_argument_index = has_connection_string ? 2 : 1 ;
2022-03-01 09:22:12 +00:00
/** We allow different groups of arguments:
* - common arguments ;
* - arguments for any number of external tables each in form " --external args... " ,
* where possible args are file , name , format , structure , types ;
* - param arguments for prepared statements .
* Split these groups before processing .
*/
bool in_external_group = false ;
std : : string prev_host_arg ;
std : : string prev_port_arg ;
2023-06-07 03:16:29 +00:00
for ( int arg_num = start_argument_index ; arg_num < argc ; + + arg_num )
2022-03-01 09:22:12 +00:00
{
2022-03-23 03:03:25 +00:00
std : : string_view arg = argv [ arg_num ] ;
2022-03-01 09:22:12 +00:00
2023-06-09 06:40:57 +00:00
if ( has_connection_string )
2023-06-14 01:15:45 +00:00
checkIfCmdLineOptionCanBeUsedWithConnectionString ( arg ) ;
2023-06-09 06:40:57 +00:00
2022-03-23 03:03:25 +00:00
if ( arg = = " --external " )
2022-03-01 09:22:12 +00:00
{
in_external_group = true ;
external_tables_arguments . emplace_back ( Arguments { " " } ) ;
}
/// Options with value after equal sign.
2022-03-23 03:03:25 +00:00
else if (
in_external_group
& & ( arg . starts_with ( " --file= " ) | | arg . starts_with ( " --name= " ) | | arg . starts_with ( " --format= " ) | | arg . starts_with ( " --structure= " )
| | arg . starts_with ( " --types= " ) ) )
2022-03-01 09:22:12 +00:00
{
external_tables_arguments . back ( ) . emplace_back ( arg ) ;
}
/// Options with value after whitespace.
2022-03-23 03:03:25 +00:00
else if ( in_external_group & & ( arg = = " --file " | | arg = = " --name " | | arg = = " --format " | | arg = = " --structure " | | arg = = " --types " ) )
2022-03-01 09:22:12 +00:00
{
if ( arg_num + 1 < argc )
{
external_tables_arguments . back ( ) . emplace_back ( arg ) ;
+ + arg_num ;
arg = argv [ arg_num ] ;
external_tables_arguments . back ( ) . emplace_back ( arg ) ;
}
else
break ;
}
else
{
in_external_group = false ;
2022-03-01 11:29:59 +00:00
if ( arg = = " --file " sv | | arg = = " --name " sv | | arg = = " --structure " sv | | arg = = " --types " sv )
2022-03-01 09:22:12 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " Parameter must be in external group, try add --external before {} " , arg ) ;
/// Parameter arg after underline.
2022-03-23 03:03:25 +00:00
if ( arg . starts_with ( " --param_ " ) )
2022-03-01 09:22:12 +00:00
{
2022-03-23 03:03:25 +00:00
auto param_continuation = arg . substr ( strlen ( " --param_ " ) ) ;
auto equal_pos = param_continuation . find_first_of ( ' = ' ) ;
2022-03-01 09:22:12 +00:00
2022-03-23 03:03:25 +00:00
if ( equal_pos = = std : : string : : npos )
2022-03-01 09:22:12 +00:00
{
/// param_name value
+ + arg_num ;
if ( arg_num > = argc )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " Parameter requires value " ) ;
2022-03-01 09:22:12 +00:00
arg = argv [ arg_num ] ;
query_parameters . emplace ( String ( param_continuation ) , String ( arg ) ) ;
}
2022-03-23 03:03:25 +00:00
else
{
if ( equal_pos = = 0 )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " Parameter name cannot be empty " ) ;
2022-03-23 03:03:25 +00:00
/// param_name=value
query_parameters . emplace ( param_continuation . substr ( 0 , equal_pos ) , param_continuation . substr ( equal_pos + 1 ) ) ;
}
2022-03-01 09:22:12 +00:00
}
2022-03-23 03:03:25 +00:00
else if ( arg . starts_with ( " --host " ) | | arg . starts_with ( " -h " ) )
2022-03-01 09:22:12 +00:00
{
std : : string host_arg ;
/// --host host
2022-03-23 03:03:25 +00:00
if ( arg = = " --host " | | arg = = " -h " )
2022-03-01 09:22:12 +00:00
{
+ + arg_num ;
if ( arg_num > = argc )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " Host argument requires value " ) ;
2022-03-01 09:22:12 +00:00
arg = argv [ arg_num ] ;
host_arg = " --host= " ;
host_arg . append ( arg ) ;
}
else
host_arg = arg ;
/// --port port1 --host host1
if ( ! prev_port_arg . empty ( ) )
{
hosts_and_ports_arguments . push_back ( { host_arg , prev_port_arg } ) ;
prev_port_arg . clear ( ) ;
}
else
{
/// --host host1 --host host2
if ( ! prev_host_arg . empty ( ) )
hosts_and_ports_arguments . push_back ( { prev_host_arg } ) ;
prev_host_arg = host_arg ;
}
}
2022-03-23 03:03:25 +00:00
else if ( arg . starts_with ( " --port " ) )
2022-03-01 09:22:12 +00:00
{
2022-03-23 03:03:25 +00:00
auto port_arg = String { arg } ;
2022-03-01 09:22:12 +00:00
/// --port port
2022-03-23 03:03:25 +00:00
if ( arg = = " --port " )
2022-03-01 09:22:12 +00:00
{
port_arg . push_back ( ' = ' ) ;
+ + arg_num ;
if ( arg_num > = argc )
2023-01-23 21:13:58 +00:00
throw Exception ( ErrorCodes : : BAD_ARGUMENTS , " Port argument requires value " ) ;
2022-03-01 09:22:12 +00:00
arg = argv [ arg_num ] ;
port_arg . append ( arg ) ;
}
/// --host host1 --port port1
if ( ! prev_host_arg . empty ( ) )
{
hosts_and_ports_arguments . push_back ( { port_arg , prev_host_arg } ) ;
prev_host_arg . clear ( ) ;
}
else
{
/// --port port1 --port port2
if ( ! prev_port_arg . empty ( ) )
hosts_and_ports_arguments . push_back ( { prev_port_arg } ) ;
prev_port_arg = port_arg ;
}
}
2022-03-23 03:03:25 +00:00
else if ( arg = = " --allow_repeated_settings " )
2022-03-08 03:29:00 +00:00
allow_repeated_settings = true ;
2022-07-07 22:16:01 +00:00
else if ( arg = = " --allow_merge_tree_settings " )
allow_merge_tree_settings = true ;
2023-06-14 01:37:58 +00:00
else if ( arg = = " --password " & & ( ( arg_num + 1 ) > = argc | | std : : string_view ( argv [ arg_num + 1 ] ) . starts_with ( ' - ' ) ) )
{
common_arguments . emplace_back ( arg ) ;
2023-06-19 03:57:38 +00:00
/// if the value of --password is omitted, the password will be asked before
/// connection start
common_arguments . emplace_back ( ConnectionParameters : : ASK_PASSWORD ) ;
2023-06-14 01:37:58 +00:00
}
2022-03-01 09:22:12 +00:00
else
common_arguments . emplace_back ( arg ) ;
}
}
if ( ! prev_host_arg . empty ( ) )
hosts_and_ports_arguments . push_back ( { prev_host_arg } ) ;
if ( ! prev_port_arg . empty ( ) )
hosts_and_ports_arguments . push_back ( { prev_port_arg } ) ;
}
2017-03-13 17:35:17 +00:00
}
2021-07-11 11:36:27 +00:00
2024-03-11 12:54:34 +00:00
# pragma clang diagnostic ignored "-Wunused-function"
# pragma clang diagnostic ignored "-Wmissing-declarations"
2020-06-30 09:25:23 +00:00
2017-03-24 15:05:54 +00:00
int mainEntryClickHouseClient ( int argc , char * * argv )
2017-03-13 17:35:17 +00:00
{
2017-04-01 07:20:54 +00:00
try
{
2019-02-02 13:49:24 +00:00
DB : : Client client ;
2023-10-23 14:48:30 +00:00
// Initialize command line options
2017-04-01 07:20:54 +00:00
client . init ( argc , argv ) ;
2019-02-02 13:49:24 +00:00
return client . run ( ) ;
2017-04-01 07:20:54 +00:00
}
2020-06-24 16:34:49 +00:00
catch ( const DB : : Exception & e )
{
2021-07-22 19:25:50 +00:00
std : : cerr < < DB : : getExceptionMessage ( e , false ) < < std : : endl ;
2020-06-24 16:34:49 +00:00
return 1 ;
}
2021-10-15 15:29:27 +00:00
catch ( const boost : : program_options : : error & e )
{
std : : cerr < < " Bad arguments: " < < e . what ( ) < < std : : endl ;
return DB : : ErrorCodes : : BAD_ARGUMENTS ;
}
2018-06-06 20:57:07 +00:00
catch ( . . . )
{
std : : cerr < < DB : : getCurrentExceptionMessage ( true ) < < std : : endl ;
return 1 ;
}
2017-03-13 17:35:17 +00:00
}