ClickHouse coverage report · #104431
Current view: top level - programs - main.cpp (source / functions) Coverage Total Hit
Current: branch=revert-104359-revert-104251-parquet-single-file-parallelism, current_commit=fe78f354148f7dd6d5cedb9bccbc2ddd9f6bfb6e Lines: 94.3 % 123 116
Current Date: 2026-05-20 23:21:29 Functions: 85.7 % 14 12
Baseline: base_branch=master, baseline_commit=8ace6bb6ff2b46357aa2d9787c8a95cbd1250913 Branches: 84.0 % 94 79
Baseline Date:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed

             Branch data     Line data    Source code
       1                 :             : #include <base/phdr_cache.h>
       2                 :             : #include <base/scope_guard.h>
       3                 :             : #include <base/defines.h>
       4                 :             : 
       5                 :             : #include <Common/EnvironmentChecks.h>
       6                 :             : #include <Common/Exception.h>
       7                 :             : #include <Common/StringUtils.h>
       8                 :             : #include <Common/getHashOfLoadedBinary.h>
       9                 :             : #include <Common/Crypto/OpenSSLInitializer.h>
      10                 :             : 
      11                 :             : 
      12                 :             : #include "config.h"
      13                 :             : #include "config_tools.h"
      14                 :             : 
      15                 :             : #include <unistd.h>
      16                 :             : 
      17                 :             : #include <filesystem>
      18                 :             : #include <iostream>
      19                 :             : #include <new>
      20                 :             : #include <string>
      21                 :             : #include <string_view>
      22                 :             : #include <utility> /// pair
      23                 :             : #include <vector>
      24                 :             : 
      25                 :             : #ifdef SANITIZER
      26                 :             : #pragma clang diagnostic push
      27                 :             : #pragma clang diagnostic ignored "-Wreserved-identifier"
      28                 :             : extern "C" {
      29                 :             : #ifdef ADDRESS_SANITIZER
      30                 :             : const char * __asan_default_options()
      31                 :             : {
      32                 :             :     return "halt_on_error=1 abort_on_error=1";
      33                 :             : }
      34                 :             : const char * __lsan_default_options()
      35                 :             : {
      36                 :             :     return "max_allocation_size_mb=32768";
      37                 :             : }
      38                 :             : const char * __lsan_default_suppressions()
      39                 :             : {
      40                 :             :     /// OpenSSL intentionally does not free all global state at exit.
      41                 :             :     /// These are known false positives from OpenSSL provider and EVP initialization.
      42                 :             :     return "leak:ossl_provider_new\n"
      43                 :             :            "leak:OSSL_PROVIDER_try_load_ex\n"
      44                 :             :            "leak:ossl_rand_ctx_new\n"
      45                 :             :            "leak:OSSL_LIB_CTX_new\n"
      46                 :             :            "leak:ossl_legacy_provider_init\n"
      47                 :             :            /// OpenSSL EVP method objects are cached globally and never freed at exit.
      48                 :             :            /// Triggered when S3 client initializes HMAC (AWS SDK -> OpenSSL HMAC_Init_ex).
      49                 :             :            "leak:evp_md_new\n"
      50                 :             :            "leak:construct_evp_method\n"
      51                 :             :            "leak:CRYPTO_THREAD_lock_new\n";
      52                 :             : }
      53                 :             : #endif
      54                 :             : 
      55                 :             : #ifdef MEMORY_SANITIZER
      56                 :             : const char * __msan_default_options()
      57                 :             : {
      58                 :             :     return "abort_on_error=1 poison_in_dtor=1 max_allocation_size_mb=32768";
      59                 :             : }
      60                 :             : #endif
      61                 :             : 
      62                 :             : #ifdef THREAD_SANITIZER
      63                 :             : const char * __tsan_default_options()
      64                 :             : {
      65                 :             :     return "halt_on_error=1 abort_on_error=1 history_size=7 second_deadlock_stack=1 max_allocation_size_mb=32768";
      66                 :             : }
      67                 :             : #endif
      68                 :             : 
      69                 :             : #ifdef UNDEFINED_BEHAVIOR_SANITIZER
      70                 :             : const char * __ubsan_default_options()
      71                 :             : {
      72                 :             :     return "print_stacktrace=1 max_allocation_size_mb=32768";
      73                 :             : }
      74                 :             : #endif
      75                 :             : }
      76                 :             : #pragma clang diagnostic pop
      77                 :             : #endif
      78                 :             : 
      79                 :             : /// Universal executable for various clickhouse applications
      80                 :             : int mainEntryClickHouseBenchmark(int argc, char ** argv);
      81                 :             : int mainEntryClickHouseCheckMarks(int argc, char ** argv);
      82                 :             : int mainEntryClickHouseChecksumForCompressedBlock(int, char **);
      83                 :             : int mainEntryClickHouseClient(int argc, char ** argv);
      84                 :             : int mainEntryClickHouseCompressor(int argc, char ** argv);
      85                 :             : int mainEntryClickHouseDisks(int argc, char ** argv);
      86                 :             : int mainEntryClickHouseExtractFromConfig(int argc, char ** argv);
      87                 :             : int mainEntryClickHouseFormat(int argc, char ** argv);
      88                 :             : int mainEntryClickHouseFstDumpTree(int argc, char ** argv);
      89                 :             : int mainEntryClickHouseGitImport(int argc, char ** argv);
      90                 :             : int mainEntryClickHouseLocal(int argc, char ** argv);
      91                 :             : int mainEntryClickHouseObfuscator(int argc, char ** argv);
      92                 :             : int mainEntryClickHouseSU(int argc, char ** argv);
      93                 :             : int mainEntryClickHouseDockerInit(int argc, char ** argv);
      94                 :             : int mainEntryClickHouseServer(int argc, char ** argv);
      95                 :             : int mainEntryClickHouseStaticFilesDiskUploader(int argc, char ** argv);
      96                 :             : int mainEntryClickHouseZooKeeperDumpTree(int argc, char ** argv);
      97                 :             : int mainEntryClickHouseZooKeeperRemoveByList(int argc, char ** argv);
      98                 :             : 
      99                 :             : int mainEntryClickHouseHashBinary(int argc, char ** argv)
     100                 :           4 : {
     101 [ +  - ][ +  - ]:           4 :     if (argc > 1 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0))
                 [ #  # ]
     102                 :           4 :     {
     103                 :           4 :         std::cout << "Usage: clickhouse hash-binary\n"
     104                 :           4 :                      "Prints hash of ClickHouse binary.\n"
     105                 :           4 :                      "  -h, --help   Print this message\n"
     106                 :           4 :                      "Result is intentionally without newline. So you can run:\n"
     107                 :           4 :                      "objcopy --add-section .clickhouse.hash=<(./clickhouse hash-binary) clickhouse\n\n"
     108                 :           4 :                      "Current binary hash: ";
     109                 :           4 :     }
     110                 :           4 :     std::cout << getHashOfLoadedBinaryHex();
     111                 :           4 :     return 0;
     112                 :           4 : }
     113                 :             : 
     114                 :             : #if ENABLE_CLICKHOUSE_KEEPER
     115                 :             : int mainEntryClickHouseKeeper(int argc, char ** argv);
     116                 :             : #endif
     117                 :             : #if ENABLE_CLICKHOUSE_KEEPER_CONVERTER
     118                 :             : int mainEntryClickHouseKeeperConverter(int argc, char ** argv);
     119                 :             : #endif
     120                 :             : #if ENABLE_CLICKHOUSE_KEEPER_CLIENT
     121                 :             : int mainEntryClickHouseKeeperClient(int argc, char ** argv);
     122                 :             : #endif
     123                 :             : #if USE_RAPIDJSON && USE_NURAFT
     124                 :             : int mainEntryClickHouseKeeperBench(int argc, char ** argv);
     125                 :             : #endif
     126                 :             : #if USE_NURAFT
     127                 :             : int mainEntryClickHouseKeeperDataDumper(int argc, char ** argv);
     128                 :             : int mainEntryClickHouseKeeperUtils(int argc, char ** argv);
     129                 :             : #endif
     130                 :             : 
     131                 :             : #if USE_CHDIG
     132                 :             : extern "C" int chdig_main(int argc, char ** argv);
     133                 :             : int mainEntryClickHouseChdig(int argc, char ** argv)
     134                 :           4 : {
     135                 :           4 :     return chdig_main(argc, argv);
     136                 :           4 : }
     137                 :             : #endif
     138                 :             : 
     139                 :             : // install
     140                 :             : int mainEntryClickHouseInstall(int argc, char ** argv);
     141                 :             : int mainEntryClickHouseStart(int argc, char ** argv);
     142                 :             : int mainEntryClickHouseStop(int argc, char ** argv);
     143                 :             : int mainEntryClickHouseStatus(int argc, char ** argv);
     144                 :             : int mainEntryClickHouseRestart(int argc, char ** argv);
     145                 :             : 
     146                 :             : /// Private-only programs
     147                 :             : #if CLICKHOUSE_CLOUD
     148                 :             : int mainEntryClickHouseSharedCatalogUtil(int argc, char ** argv);
     149                 :             : #if ENABLE_DISTRIBUTED_CACHE
     150                 :             : int mainEntryClickHouseDistributedCache(int argc, char ** argv);
     151                 :             : #endif
     152                 :             : int mainEntryClickHouseSharedMergeTreeGarbageCleaner(int argc, char ** argv);
     153                 :             : int mainEntryClickHouseClearZooKeeperLocks(int argc, char ** argv);
     154                 :             : int mainEntryClickHousePackedIO(int argc, char ** argv);
     155                 :             : int mainEntryClickHouseMangler(int argc, char ** argv);
     156                 :             : #endif
     157                 :             : 
     158                 :             : namespace
     159                 :             : {
     160                 :             : 
     161                 :             : using MainFunc = int (*)(int, char**);
     162                 :             : 
     163                 :             : /// Forward declaration, since clickhouse_applications is defined after this function.
     164                 :             : void printHelp(std::ostream & out);
     165                 :             : 
     166                 :             : int mainEntryHelp(int, char **)
     167                 :          28 : {
     168                 :          28 :     printHelp(std::cout);
     169                 :          28 :     return 0;
     170                 :          28 : }
     171                 :             : 
     172                 :             : int printHelpOnError(int, char **)
     173                 :          12 : {
     174                 :          12 :     printHelp(std::cerr);
     175                 :          12 :     return -1;
     176                 :          12 : }
     177                 :             : 
     178                 :             : /// Add an item here to register new application.
     179                 :             : /// This list has a "priority" - e.g. we need to disambiguate clickhouse --format being
     180                 :             : /// either clickouse-format or clickhouse-{local, client} --format.
     181                 :             : /// Currently we will prefer the latter option.
     182                 :             : std::pair<std::string_view, MainFunc> clickhouse_applications[] =
     183                 :             : {
     184                 :             :     {"local", mainEntryClickHouseLocal},
     185                 :             :     {"client", mainEntryClickHouseClient},
     186                 :             : #if USE_CHDIG
     187                 :             :     {"chdig", mainEntryClickHouseChdig},
     188                 :             :     {"dig", mainEntryClickHouseChdig},
     189                 :             : #endif
     190                 :             :     {"benchmark", mainEntryClickHouseBenchmark},
     191                 :             :     {"server", mainEntryClickHouseServer},
     192                 :             :     {"extract-from-config", mainEntryClickHouseExtractFromConfig},
     193                 :             :     {"compressor", mainEntryClickHouseCompressor},
     194                 :             :     {"format", mainEntryClickHouseFormat},
     195                 :             :     {"obfuscator", mainEntryClickHouseObfuscator},
     196                 :             :     {"git-import", mainEntryClickHouseGitImport},
     197                 :             :     {"static-files-disk-uploader", mainEntryClickHouseStaticFilesDiskUploader},
     198                 :             :     {"su", mainEntryClickHouseSU},
     199                 :             :     {"docker-init", mainEntryClickHouseDockerInit},
     200                 :             :     {"hash-binary", mainEntryClickHouseHashBinary},
     201                 :             :     {"disks", mainEntryClickHouseDisks},
     202                 :             :     {"check-marks", mainEntryClickHouseCheckMarks},
     203                 :             :     {"checksum-for-compressed-block", mainEntryClickHouseChecksumForCompressedBlock},
     204                 :             :     {"zookeeper-dump-tree", mainEntryClickHouseZooKeeperDumpTree},
     205                 :             :     {"zookeeper-remove-by-list", mainEntryClickHouseZooKeeperRemoveByList},
     206                 :             : 
     207                 :             :     // keeper
     208                 :             : #if ENABLE_CLICKHOUSE_KEEPER
     209                 :             :     {"keeper", mainEntryClickHouseKeeper},
     210                 :             : #endif
     211                 :             : #if ENABLE_CLICKHOUSE_KEEPER_CONVERTER
     212                 :             :     {"keeper-converter", mainEntryClickHouseKeeperConverter},
     213                 :             : #endif
     214                 :             : #if ENABLE_CLICKHOUSE_KEEPER_CLIENT
     215                 :             :     {"keeper-client", mainEntryClickHouseKeeperClient},
     216                 :             : #endif
     217                 :             : #if USE_RAPIDJSON && USE_NURAFT
     218                 :             :     {"keeper-bench", mainEntryClickHouseKeeperBench},
     219                 :             : #endif
     220                 :             : #if USE_NURAFT
     221                 :             :     {"keeper-data-dumper", mainEntryClickHouseKeeperDataDumper},
     222                 :             :     {"keeper-utils", mainEntryClickHouseKeeperUtils},
     223                 :             : #endif
     224                 :             :     // install
     225                 :             :     {"install", mainEntryClickHouseInstall},
     226                 :             :     {"start", mainEntryClickHouseStart},
     227                 :             :     {"stop", mainEntryClickHouseStop},
     228                 :             :     {"status", mainEntryClickHouseStatus},
     229                 :             :     {"restart", mainEntryClickHouseRestart},
     230                 :             :     // help
     231                 :             :     {"help", mainEntryHelp},
     232                 :             : 
     233                 :             : /// Private-only programs
     234                 :             : #if CLICKHOUSE_CLOUD
     235                 :             :     {"shared-merge-tree-garbage-cleaner", mainEntryClickHouseSharedMergeTreeGarbageCleaner},
     236                 :             :     {"clear-zookeeper-locks", mainEntryClickHouseClearZooKeeperLocks},
     237                 :             :     {"shared-catalog-util", mainEntryClickHouseSharedCatalogUtil},
     238                 :             :     {"packed-io", mainEntryClickHousePackedIO},
     239                 :             :     {"mangler", mainEntryClickHouseMangler},
     240                 :             : #if ENABLE_DISTRIBUTED_CACHE
     241                 :             :     {"distributed-cache", mainEntryClickHouseDistributedCache}
     242                 :             : #endif
     243                 :             : #endif
     244                 :             : };
     245                 :             : 
     246                 :             : void printHelp(std::ostream & out)
     247                 :          40 : {
     248                 :          40 :     out << "Use one of the following commands:" << std::endl;
     249         [ +  + ]:          40 :     for (const auto & application : clickhouse_applications)
     250                 :        1280 :         out << "clickhouse " << application.first << " [args] " << std::endl;
     251                 :          40 : }
     252                 :             : 
     253                 :             : /// Add an item here to register a new short name
     254                 :             : std::pair<std::string_view, std::string_view> clickhouse_short_names[] =
     255                 :             : {
     256                 :             :     {"chl", "local"},
     257                 :             :     {"chc", "client"},
     258                 :             : #if USE_CHDIG
     259                 :             :     {"chdig", "chdig"},
     260                 :             : #endif
     261                 :             : };
     262                 :             : 
     263                 :             : }
     264                 :             : 
     265                 :             : bool isClickhouseApp(std::string_view app_suffix, std::vector<char *> & argv)
     266                 :      892757 : {
     267         [ +  + ]:      892757 :     for (const auto & [alias, name] : clickhouse_short_names)
     268         [ +  + ]:     2678271 :         if (app_suffix == name
     269 [ +  - ][ -  + ]:     2678271 :             && !argv.empty() && (alias == argv[0] || endsWith(argv[0], "/" + std::string(alias))))
                 [ -  + ]
     270                 :           0 :             return true;
     271                 :             : 
     272                 :             :     /// Use app if the first arg 'app' is passed (the arg should be quietly removed)
     273         [ +  + ]:      892757 :     if (argv.size() >= 2)
     274                 :      890399 :     {
     275                 :      890399 :         auto first_arg = argv.begin() + 1;
     276                 :             : 
     277                 :             :         /// 'clickhouse --client ...' and 'clickhouse client ...' are Ok
     278         [ +  + ]:      890399 :         if (*first_arg == app_suffix
     279 [ +  + ][ +  + ]:      890399 :             || (std::string_view(*first_arg).starts_with("--") && std::string_view(*first_arg).substr(2) == app_suffix))
     280                 :       91208 :         {
     281                 :       91208 :             argv.erase(first_arg);
     282                 :       91208 :             return true;
     283                 :       91208 :         }
     284                 :      890399 :     }
     285                 :             : 
     286                 :             :     /// Use app if clickhouse binary is run through symbolic link with name clickhouse-app
     287                 :      801549 :     std::string app_name = "clickhouse-" + std::string(app_suffix);
     288 [ +  - ][ +  + ]:      801549 :     return !argv.empty() && (app_name == argv[0] || endsWith(argv[0], "/" + app_name));
                 [ +  + ]
     289                 :      892757 : }
     290                 :             : 
     291                 :             : /// Don't allow dlopen in the main ClickHouse binary, because it is harmful and insecure.
     292                 :             : /// We don't use it. But it can be used by some libraries for implementation of "plugins".
     293                 :             : /// We absolutely discourage the ancient technique of loading
     294                 :             : /// 3rd-party uncontrolled dangerous libraries into the process address space,
     295                 :             : /// because it is insane.
     296                 :             : ///
     297                 :             : /// We do allow `dlopen()` in case of OpenSSL FIPS build,
     298                 :             : /// because it requires a FIPS provider (i.e. fips.so), which is loaded dynamically.
     299                 :             : #if !(defined(USE_MUSL) || USE_OPENSSL_FIPS)
     300                 :             : extern "C"
     301                 :             : {
     302                 :             :     void * dlopen(const char *, int)
     303                 :        4355 :     {
     304                 :        4355 :         return nullptr;
     305                 :        4355 :     }
     306                 :             : 
     307                 :             :     void * dlmopen(long, const char *, int) // NOLINT
     308                 :           0 :     {
     309                 :           0 :         return nullptr;
     310                 :           0 :     }
     311                 :             : 
     312                 :             :     int dlclose(void *)
     313                 :           0 :     {
     314                 :           0 :         return 0;
     315                 :           0 :     }
     316                 :             : 
     317                 :             :     const char * dlerror()
     318                 :          44 :     {
     319                 :          44 :         return "ClickHouse does not allow dynamic library loading";
     320                 :          44 :     }
     321                 :             : }
     322                 :             : #endif
     323                 :             : 
     324                 :             : /// Prevent messages from JeMalloc in the release build.
     325                 :             : /// Some of these messages are non-actionable for the users, such as:
     326                 :             : /// <jemalloc>: Number of CPUs detected is not deterministic. Per-CPU arena disabled.
     327                 :             : #if USE_JEMALLOC && defined(NDEBUG) && !defined(SANITIZER)
     328                 :             : extern "C" void (*je_malloc_message)(void *, const char *s);
     329                 :      204312 : __attribute__((constructor(0))) void init_je_malloc_message() { je_malloc_message = [](void *, const char *){}; }
     330                 :             : #elif USE_JEMALLOC
     331                 :             : #include <unordered_set>
     332                 :             : /// Ignore messages which can be safely ignored, e.g. EAGAIN on pthread_create
     333                 :             : extern "C" void (*je_malloc_message)(void *, const char * s);
     334                 :             : __attribute__((constructor(0))) void init_je_malloc_message()
     335                 :             : {
     336                 :             :     je_malloc_message = [](void *, const char * str)
     337                 :             :     {
     338                 :             :         using namespace std::literals;
     339                 :             :         static const std::unordered_set<std::string_view> ignore_messages{
     340                 :             :             "<jemalloc>: background thread creation failed (11)\n"sv};
     341                 :             : 
     342                 :             :         std::string_view message_view{str};
     343                 :             :         if (ignore_messages.contains(message_view))
     344                 :             :             return;
     345                 :             : 
     346                 :             : #    if defined(SYS_write)
     347                 :             :         syscall(SYS_write, 2 /*stderr*/, message_view.data(), message_view.size());
     348                 :             : #    else
     349                 :             :         write(STDERR_FILENO, message_view.data(), message_view.size());
     350                 :             : #    endif
     351                 :             :     };
     352                 :             : }
     353                 :             : #endif
     354                 :             : 
     355                 :             : /// OpenSSL early initialization.
     356                 :             : /// See also EnvironmentChecks.cpp for other static initializers.
     357                 :             : /// Must be ran after EnvironmentChecks.cpp, as OpenSSL uses SSE4.1 and POPCNT.
     358                 :             : __attribute__((constructor(202))) void init_ssl()
     359                 :      204312 : {
     360                 :      204312 :     DB::OpenSSLInitializer::instance();
     361                 :      204312 : }
     362                 :             : 
     363                 :             : /// This allows to implement assert to forbid initialization of a class in static constructors.
     364                 :             : /// Usage:
     365                 :             : ///
     366                 :             : /// extern bool inside_main;
     367                 :             : /// class C { C() { assert(inside_main); } };
     368                 :             : bool inside_main = false;
     369                 :             : 
     370                 :             : int main(int argc_, char ** argv_)
     371                 :      204312 : {
     372                 :      204312 :     inside_main = true;
     373                 :      204312 :     SCOPE_EXIT({ inside_main = false; });
     374                 :             : 
     375                 :             :     /// PHDR cache is required for query profiler to work reliably
     376                 :             :     /// It also speed up exception handling, but exceptions from dynamically loaded libraries (dlopen)
     377                 :             :     ///  will work only after additional call of this function.
     378                 :             :     /// Note: we forbid dlopen in our code.
     379                 :      204312 :     updatePHDRCache();
     380                 :             : 
     381                 :      204312 : #if !defined(USE_MUSL)
     382                 :      204312 :     checkHarmfulEnvironmentVariables(argv_);
     383                 :      204312 : #endif
     384                 :             : 
     385                 :             :     /// This is used for testing. For example,
     386                 :             :     /// clickhouse-local should be able to run a simple query without throw/catch.
     387         [ +  + ]:      204312 :     if (getenv("CLICKHOUSE_TERMINATE_ON_ANY_EXCEPTION")) // NOLINT(concurrency-mt-unsafe)
     388                 :          36 :         DB::terminate_on_any_exception = true;
     389                 :             : 
     390                 :             :     /// Reset new handler to default (that throws std::bad_alloc)
     391                 :             :     /// It is needed because LLVM library clobbers it.
     392                 :      204312 :     std::set_new_handler(nullptr);
     393                 :             : 
     394                 :      204312 :     std::vector<char *> argv(argv_, argv_ + argc_);
     395                 :             : 
     396                 :             :     /// Print a basic help if nothing was matched
     397                 :      204312 :     MainFunc main_func = printHelpOnError;
     398                 :             : 
     399         [ +  + ]:      204312 :     for (auto & application : clickhouse_applications)
     400                 :      892757 :     {
     401         [ +  + ]:      892757 :         if (isClickhouseApp(application.first, argv))
     402                 :      204183 :         {
     403                 :      204183 :             main_func = application.second;
     404                 :      204183 :             break;
     405                 :      204183 :         }
     406                 :      892757 :     }
     407                 :             : 
     408                 :             :     /// Top-level --help / -h / -? (as the sole argument) should show the dispatcher
     409                 :             :     /// help listing all subcommands and exit with code 0. Without this carve-out,
     410                 :             :     /// `--help` would match the `startsWith(argv[i], "-h")` rule below and be routed
     411                 :             :     /// into clickhouse-client, which treats anything starting with "-h" as a --host
     412                 :             :     /// specification and fails.
     413 [ +  + ][ +  + ]:      204312 :     if (main_func == printHelpOnError && argv.size() == 2)
     414                 :          77 :     {
     415                 :          77 :         std::string_view arg(argv[1]);
     416 [ -  + ][ +  + ]:          77 :         if (arg == "--help" || arg == "-h" || arg == "-?")
                 [ +  + ]
     417                 :           8 :             main_func = mainEntryHelp;
     418                 :          77 :     }
     419                 :             : 
     420                 :             :     /// If host/port arguments are passed to clickhouse/ch shortcuts,
     421                 :             :     /// interpret it as clickhouse-client invocation for usability.
     422 [ +  + ][ +  + ]:      204312 :     if (main_func == printHelpOnError && argv.size() >= 2)
     423                 :         117 :     {
     424         [ +  + ]:         290 :         for (size_t i = 1, num_args = argv.size(); i < num_args; ++i)
     425                 :         181 :         {
     426 [ +  + ][ +  + ]:         181 :             if ((i + 1 < num_args && argv[i] == std::string_view("--host")) || startsWith(argv[i], "--host=")
         [ +  + ][ -  + ]
     427 [ +  + ][ -  + ]:         181 :                 || (i + 1 < num_args && argv[i] == std::string_view("--port")) || startsWith(argv[i], "--port=")
                 [ -  + ]
     428         [ +  + ]:         181 :                 || startsWith(argv[i], "-h"))
     429                 :           8 :             {
     430                 :           8 :                 main_func = mainEntryClickHouseClient;
     431                 :           8 :                 break;
     432                 :           8 :             }
     433                 :         181 :         }
     434                 :         117 :     }
     435                 :             : 
     436                 :             :     /// Interpret binary without argument or with arguments starts with dash
     437                 :             :     /// ('-') as clickhouse-local for better usability:
     438                 :             :     ///
     439                 :             :     ///     clickhouse help # dumps help
     440                 :             :     ///     clickhouse -q 'select 1' # use local
     441                 :             :     ///     clickhouse # spawn local
     442                 :             :     ///     clickhouse local # spawn local
     443                 :             :     ///     clickhouse "select ..." # spawn local
     444                 :             :     ///     clickhouse /tmp/repro --enable-analyzer
     445                 :             :     ///
     446                 :      204312 :     std::error_code ec;
     447 [ +  + ][ +  - ]:      204312 :     if (main_func == printHelpOnError && !argv.empty()
     448 [ +  + ][ +  - ]:      204312 :         && (argv.size() < 2 || argv[1] != std::string_view("--help"))
     449 [ +  + ][ +  + ]:      204312 :         && (argv.size() == 1 || argv[1][0] == '-' || std::string_view(argv[1]).contains(' ')
                 [ +  + ]
     450         [ +  + ]:         113 :             || std::filesystem::is_regular_file(std::filesystem::path{argv[1]}, ec)))
     451                 :          93 :     {
     452                 :          93 :         main_func = mainEntryClickHouseLocal;
     453                 :          93 :     }
     454                 :             : 
     455                 :             :     /// If the argument looks like a file path but doesn't exist, provide a helpful error
     456                 :             :     /// instead of the generic "Use one of the following commands" message.
     457                 :             :     /// The check above routes existing files to clickhouse-local, but when the file
     458                 :             :     /// doesn't exist, we fall through to `printHelp` which is confusing:
     459                 :             :     ///     $ clickhouse tests/queries/0_stateless/my_test.sql
     460                 :             :     ///     Use one of the following commands: ...
     461                 :             :     /// We detect file-like arguments by the presence of `/` (path separator)
     462                 :             :     /// or `.` (file extension), which distinguishes them from mistyped subcommand
     463                 :             :     /// names like "clickhouse sever" where the generic help is appropriate.
     464 [ +  + ][ +  - ]:      204312 :     if (main_func == printHelpOnError && argv.size() >= 2)
     465                 :          20 :     {
     466                 :          20 :         std::string_view arg(argv[1]);
     467 [ +  + ][ +  + ]:          20 :         if (arg.contains('/') || arg.contains('.'))
     468                 :           8 :         {
     469                 :           8 :             std::cerr << "Error: no such file: " << arg << std::endl;
     470                 :           8 :             std::cerr << "If you intended to run a script, please check the path." << std::endl;
     471                 :           8 :             return 1;
     472                 :           8 :         }
     473                 :          20 :     }
     474                 :             : 
     475                 :      204304 :     int exit_code = main_func(static_cast<int>(argv.size()), argv.data());
     476                 :             : 
     477                 :      204304 :     return exit_code;
     478                 :      204312 : }
        

Generated by: LCOV version 2.4-0