2016-10-25 12:14:27 +00:00
|
|
|
#include "LocalServer.h"
|
2017-03-14 18:39:06 +00:00
|
|
|
|
2016-10-25 12:14:27 +00:00
|
|
|
#include <Poco/Util/XMLConfiguration.h>
|
2016-10-31 14:31:26 +00:00
|
|
|
#include <Poco/Util/HelpFormatter.h>
|
|
|
|
#include <Poco/Util/OptionCallback.h>
|
2016-12-13 18:51:19 +00:00
|
|
|
#include <Poco/String.h>
|
2018-01-15 14:58:21 +00:00
|
|
|
#include <Poco/Logger.h>
|
|
|
|
#include <Poco/NullChannel.h>
|
2018-03-23 20:46:43 +00:00
|
|
|
#include <Databases/DatabaseMemory.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/System/attachSystemTables.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/ProcessList.h>
|
|
|
|
#include <Interpreters/executeQuery.h>
|
|
|
|
#include <Interpreters/loadMetadata.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/Macros.h>
|
2018-02-28 20:34:25 +00:00
|
|
|
#include <Common/Config/ConfigProcessor.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
2018-04-20 19:31:19 +00:00
|
|
|
#include <Common/ClickHouseRevision.h>
|
2018-07-18 09:48:45 +00:00
|
|
|
#include <Common/config_version.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromString.h>
|
2017-07-31 21:39:24 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBufferFromFileDescriptor.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <Parsers/IAST.h>
|
2016-10-25 12:14:27 +00:00
|
|
|
#include <common/ErrorHandlers.h>
|
2018-06-05 20:09:51 +00:00
|
|
|
#include <Common/StatusFile.h>
|
2018-06-20 15:21:42 +00:00
|
|
|
#include <Common/ThreadStatus.h>
|
2017-04-21 17:47:27 +00:00
|
|
|
#include <Functions/registerFunctions.h>
|
2017-05-05 20:39:25 +00:00
|
|
|
#include <AggregateFunctions/registerAggregateFunctions.h>
|
2017-06-10 09:04:31 +00:00
|
|
|
#include <TableFunctions/registerTableFunctions.h>
|
2017-12-30 00:36:06 +00:00
|
|
|
#include <Storages/registerStorages.h>
|
2018-04-20 19:31:19 +00:00
|
|
|
#include <boost/program_options/options_description.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
2016-12-13 18:51:19 +00:00
|
|
|
|
2016-10-25 12:14:27 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-11-11 17:01:02 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
extern const int CANNOT_LOAD_CONFIG;
|
2016-11-11 17:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LocalServer::LocalServer() = default;
|
|
|
|
|
2016-12-13 18:51:19 +00:00
|
|
|
LocalServer::~LocalServer()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (context)
|
|
|
|
context->shutdown(); /// required for properly exception handling
|
2016-12-13 18:51:19 +00:00
|
|
|
}
|
2016-11-11 17:01:02 +00:00
|
|
|
|
|
|
|
|
2016-10-25 12:14:27 +00:00
|
|
|
void LocalServer::initialize(Poco::Util::Application & self)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Util::Application::initialize(self);
|
2018-01-15 14:58:21 +00:00
|
|
|
|
|
|
|
// Turn off server logging to stderr
|
2018-04-20 19:31:19 +00:00
|
|
|
if (!config().has("verbose"))
|
2018-01-15 14:58:21 +00:00
|
|
|
{
|
|
|
|
Poco::Logger::root().setLevel("none");
|
|
|
|
Poco::Logger::root().setChannel(Poco::AutoPtr<Poco::NullChannel>(new Poco::NullChannel()));
|
|
|
|
}
|
2016-10-25 12:14:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 15:32:40 +00:00
|
|
|
void LocalServer::applyCmdSettings(Context & context)
|
|
|
|
{
|
2017-11-30 11:50:02 +00:00
|
|
|
#define EXTRACT_SETTING(TYPE, NAME, DEFAULT, DESCRIPTION) \
|
2018-04-20 19:31:19 +00:00
|
|
|
if (cmd_settings.NAME.changed) \
|
|
|
|
context.getSettingsRef().NAME = cmd_settings.NAME;
|
2017-04-01 07:20:54 +00:00
|
|
|
APPLY_FOR_SETTINGS(EXTRACT_SETTING)
|
2016-10-31 19:54:49 +00:00
|
|
|
#undef EXTRACT_SETTING
|
2016-10-31 14:31:26 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:51:19 +00:00
|
|
|
/// If path is specified and not empty, will try to setup server environment and load existing metadata
|
|
|
|
void LocalServer::tryInitPath()
|
|
|
|
{
|
2018-04-20 19:31:19 +00:00
|
|
|
std::string path = config().getString("path", "");
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::trimInPlace(path);
|
2016-12-13 18:51:19 +00:00
|
|
|
|
2018-04-20 15:32:40 +00:00
|
|
|
if (!path.empty())
|
|
|
|
{
|
|
|
|
if (path.back() != '/')
|
|
|
|
path += '/';
|
|
|
|
|
|
|
|
context->setPath(path);
|
|
|
|
return;
|
|
|
|
}
|
2016-12-13 18:51:19 +00:00
|
|
|
|
2018-04-20 15:32:40 +00:00
|
|
|
/// In case of empty path set paths to helpful directories
|
|
|
|
std::string cd = Poco::Path::current();
|
|
|
|
context->setTemporaryPath(cd + "tmp");
|
|
|
|
context->setFlagsPath(cd + "flags");
|
|
|
|
context->setUserFilesPath(""); // user's files are everywhere
|
2016-12-13 18:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
int LocalServer::main(const std::vector<std::string> & /*args*/)
|
2016-12-13 18:51:19 +00:00
|
|
|
try
|
2016-10-25 12:14:27 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Logger * log = &logger();
|
|
|
|
|
|
|
|
if (!config().has("query") && !config().has("table-structure")) /// Nothing to process
|
|
|
|
{
|
2018-08-23 00:14:26 +00:00
|
|
|
if (config().hasOption("verbose"))
|
|
|
|
std::cerr << "There are no queries to process." << '\n';
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return Application::EXIT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load config files if exists
|
|
|
|
if (config().has("config-file") || Poco::File("config.xml").exists())
|
|
|
|
{
|
2017-11-21 16:54:25 +00:00
|
|
|
ConfigProcessor config_processor(config().getString("config-file", "config.xml"), false, true);
|
|
|
|
auto loaded_config = config_processor.loadConfig();
|
|
|
|
config_processor.savePreprocessedConfig(loaded_config);
|
|
|
|
config().add(loaded_config.configuration.duplicate(), PRIO_DEFAULT, false);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 20:31:23 +00:00
|
|
|
context = std::make_unique<Context>(Context::createGlobal());
|
2017-04-01 07:20:54 +00:00
|
|
|
context->setGlobalContext(*context);
|
2017-07-11 20:12:15 +00:00
|
|
|
context->setApplicationType(Context::ApplicationType::LOCAL);
|
2017-04-01 07:20:54 +00:00
|
|
|
tryInitPath();
|
|
|
|
|
2018-04-20 15:32:40 +00:00
|
|
|
std::optional<StatusFile> status;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Skip temp path installation
|
|
|
|
|
|
|
|
/// We will terminate process on error
|
|
|
|
static KillingErrorHandler error_handler;
|
|
|
|
Poco::ErrorHandler::set(&error_handler);
|
|
|
|
|
|
|
|
/// Don't initilaize DateLUT
|
|
|
|
|
2017-04-21 17:47:27 +00:00
|
|
|
registerFunctions();
|
2017-05-05 20:39:25 +00:00
|
|
|
registerAggregateFunctions();
|
2017-06-10 09:04:31 +00:00
|
|
|
registerTableFunctions();
|
2017-12-30 00:36:06 +00:00
|
|
|
registerStorages();
|
2017-04-21 17:47:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Maybe useless
|
|
|
|
if (config().has("macros"))
|
2018-03-13 23:44:23 +00:00
|
|
|
context->setMacros(std::make_unique<Macros>(config(), "macros"));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Skip networking
|
|
|
|
|
|
|
|
setupUsers();
|
|
|
|
|
|
|
|
/// Limit on total number of concurrently executing queries.
|
|
|
|
/// Threre are no need for concurrent threads, override max_concurrent_queries.
|
|
|
|
context->getProcessList().setMaxSize(0);
|
|
|
|
|
|
|
|
/// Size of cache for uncompressed blocks. Zero means disabled.
|
2017-04-12 16:37:19 +00:00
|
|
|
size_t uncompressed_cache_size = config().getUInt64("uncompressed_cache_size", 0);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (uncompressed_cache_size)
|
|
|
|
context->setUncompressedCache(uncompressed_cache_size);
|
|
|
|
|
|
|
|
/// Size of cache for marks (index of MergeTree family of tables). It is necessary.
|
|
|
|
/// Specify default value for mark_cache_size explicitly!
|
2017-04-12 16:37:19 +00:00
|
|
|
size_t mark_cache_size = config().getUInt64("mark_cache_size", 5368709120);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (mark_cache_size)
|
|
|
|
context->setMarkCache(mark_cache_size);
|
|
|
|
|
2018-02-01 13:52:29 +00:00
|
|
|
/// Load global settings from default_profile and system_profile.
|
|
|
|
context->setDefaultProfiles(config());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/** Init dummy default DB
|
|
|
|
* NOTE: We force using isolated default database to avoid conflicts with default database from server enviroment
|
|
|
|
* Otherwise, metadata of temporary File(format, EXPLICIT_PATH) tables will pollute metadata/ directory;
|
2018-01-15 14:58:21 +00:00
|
|
|
* if such tables will not be dropped, clickhouse-server will not be able to load them due to security reasons.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2018-04-20 19:31:19 +00:00
|
|
|
std::string default_database = config().getString("default_database", "_local");
|
2017-04-01 07:20:54 +00:00
|
|
|
context->addDatabase(default_database, std::make_shared<DatabaseMemory>(default_database));
|
|
|
|
context->setCurrentDatabase(default_database);
|
2018-05-11 14:35:32 +00:00
|
|
|
applyCmdOptions(*context);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-04-20 19:31:19 +00:00
|
|
|
if (!context->getPath().empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-04-20 15:32:40 +00:00
|
|
|
/// Lock path directory before read
|
|
|
|
status.emplace(context->getPath() + "status");
|
|
|
|
|
2018-04-20 19:31:19 +00:00
|
|
|
LOG_DEBUG(log, "Loading metadata from " << context->getPath());
|
2017-06-18 05:43:29 +00:00
|
|
|
loadMetadataSystem(*context);
|
|
|
|
attachSystemTables();
|
2017-04-01 07:20:54 +00:00
|
|
|
loadMetadata(*context);
|
|
|
|
LOG_DEBUG(log, "Loaded metadata.");
|
|
|
|
}
|
2017-06-15 20:08:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
attachSystemTables();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
processQueries();
|
|
|
|
|
|
|
|
context->shutdown();
|
|
|
|
context.reset();
|
|
|
|
|
|
|
|
return Application::EXIT_OK;
|
2016-10-25 12:14:27 +00:00
|
|
|
}
|
2016-12-13 18:51:19 +00:00
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
2018-08-23 00:14:26 +00:00
|
|
|
std::cerr << getCurrentExceptionMessage(config().hasOption("stacktrace")) << '\n';
|
2016-12-13 18:51:19 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// If exception code isn't zero, we should return non-zero return code anyway.
|
|
|
|
return e.code() ? e.code() : -1;
|
2016-12-13 18:51:19 +00:00
|
|
|
}
|
2016-10-25 12:14:27 +00:00
|
|
|
|
2016-10-28 17:38:32 +00:00
|
|
|
|
2016-11-11 17:01:02 +00:00
|
|
|
inline String getQuotedString(const String & s)
|
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
WriteBufferFromOwnString buf;
|
2017-04-01 07:20:54 +00:00
|
|
|
writeQuotedString(s, buf);
|
2017-07-31 21:39:24 +00:00
|
|
|
return buf.str();
|
2016-11-11 17:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-28 17:38:32 +00:00
|
|
|
std::string LocalServer::getInitialCreateTableQuery()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!config().has("table-structure"))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto table_name = backQuoteIfNeed(config().getString("table-name", "table"));
|
|
|
|
auto table_structure = config().getString("table-structure");
|
|
|
|
auto data_format = backQuoteIfNeed(config().getString("table-data-format", "TSV"));
|
|
|
|
String table_file;
|
|
|
|
if (!config().has("table-file") || config().getString("table-file") == "-") /// Use Unix tools stdin naming convention
|
|
|
|
table_file = "stdin";
|
|
|
|
else /// Use regular file
|
|
|
|
table_file = getQuotedString(config().getString("table-file"));
|
|
|
|
|
|
|
|
return
|
|
|
|
"CREATE TABLE " + table_name +
|
|
|
|
" (" + table_structure + ") " +
|
|
|
|
"ENGINE = "
|
|
|
|
"File(" + data_format + ", " + table_file + ")"
|
|
|
|
"; ";
|
2016-10-28 17:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LocalServer::attachSystemTables()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
DatabasePtr system_database = context->tryGetDatabase("system");
|
|
|
|
if (!system_database)
|
|
|
|
{
|
|
|
|
/// TODO: add attachTableDelayed into DatabaseMemory to speedup loading
|
|
|
|
system_database = std::make_shared<DatabaseMemory>("system");
|
|
|
|
context->addDatabase("system", system_database);
|
|
|
|
}
|
|
|
|
|
2017-06-18 05:43:29 +00:00
|
|
|
attachSystemTablesLocal(*system_database);
|
2016-10-28 17:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-25 12:14:27 +00:00
|
|
|
void LocalServer::processQueries()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
String initial_create_query = getInitialCreateTableQuery();
|
|
|
|
String queries_str = initial_create_query + config().getString("query");
|
2016-10-31 14:31:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<String> queries;
|
|
|
|
auto parse_res = splitMultipartQuery(queries_str, queries);
|
2016-10-25 12:14:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!parse_res.second)
|
|
|
|
throw Exception("Cannot parse and execute the following part of query: " + String(parse_res.first), ErrorCodes::SYNTAX_ERROR);
|
2016-11-11 17:01:02 +00:00
|
|
|
|
2018-04-20 15:32:40 +00:00
|
|
|
context->setSessionContext(*context);
|
|
|
|
context->setQueryContext(*context);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
context->setUser("default", "", Poco::Net::SocketAddress{}, "");
|
2018-03-09 23:50:20 +00:00
|
|
|
context->setCurrentQueryId("");
|
2018-04-20 15:32:40 +00:00
|
|
|
applyCmdSettings(*context);
|
2016-10-25 12:14:27 +00:00
|
|
|
|
2018-06-20 15:21:42 +00:00
|
|
|
/// Use the same query_id (and thread group) for all queries
|
|
|
|
CurrentThread::QueryScope query_scope_holder(*context);
|
|
|
|
|
2018-04-20 19:31:19 +00:00
|
|
|
bool echo_query = config().hasOption("echo") || config().hasOption("verbose");
|
|
|
|
std::exception_ptr exception;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & query : queries)
|
|
|
|
{
|
|
|
|
ReadBufferFromString read_buf(query);
|
|
|
|
WriteBufferFromFileDescriptor write_buf(STDOUT_FILENO);
|
2016-10-25 12:14:27 +00:00
|
|
|
|
2018-04-20 19:31:19 +00:00
|
|
|
if (echo_query)
|
2018-08-23 00:14:26 +00:00
|
|
|
std::cerr << query << '\n';
|
2016-10-31 14:31:26 +00:00
|
|
|
|
2018-04-20 19:31:19 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
executeQuery(read_buf, write_buf, /* allow_into_outfile = */ true, *context, {});
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
if (!config().hasOption("ignore-error"))
|
|
|
|
throw;
|
|
|
|
|
|
|
|
if (!exception)
|
|
|
|
exception = std::current_exception();
|
|
|
|
|
2018-08-23 00:14:26 +00:00
|
|
|
std::cerr << getCurrentExceptionMessage(config().hasOption("stacktrace")) << '\n';
|
2018-04-20 19:31:19 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-04-20 19:31:19 +00:00
|
|
|
|
|
|
|
if (exception)
|
|
|
|
std::rethrow_exception(exception);
|
2016-10-25 12:14:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:01:02 +00:00
|
|
|
static const char * minimal_default_user_xml =
|
|
|
|
"<yandex>"
|
2017-04-01 07:20:54 +00:00
|
|
|
" <profiles>"
|
|
|
|
" <default></default>"
|
|
|
|
" </profiles>"
|
|
|
|
" <users>"
|
|
|
|
" <default>"
|
|
|
|
" <password></password>"
|
|
|
|
" <networks>"
|
|
|
|
" <ip>::/0</ip>"
|
|
|
|
" </networks>"
|
|
|
|
" <profile>default</profile>"
|
|
|
|
" <quota>default</quota>"
|
|
|
|
" </default>"
|
|
|
|
" </users>"
|
|
|
|
" <quotas>"
|
|
|
|
" <default></default>"
|
|
|
|
" </quotas>"
|
2016-11-11 17:01:02 +00:00
|
|
|
"</yandex>";
|
|
|
|
|
|
|
|
|
2018-01-25 12:18:27 +00:00
|
|
|
static ConfigurationPtr getConfigurationFromXMLString(const char * xml_data)
|
2017-10-13 19:13:41 +00:00
|
|
|
{
|
2018-01-25 12:18:27 +00:00
|
|
|
std::stringstream ss{std::string{xml_data}};
|
|
|
|
Poco::XML::InputSource input_source{ss};
|
|
|
|
return {new Poco::Util::XMLConfiguration{&input_source}};
|
2017-10-13 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-25 12:14:27 +00:00
|
|
|
void LocalServer::setupUsers()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ConfigurationPtr users_config;
|
|
|
|
|
|
|
|
if (config().has("users_config") || config().has("config-file") || Poco::File("config.xml").exists())
|
|
|
|
{
|
2017-11-22 08:06:09 +00:00
|
|
|
const auto users_config_path = config().getString("users_config", config().getString("config-file", "config.xml"));
|
|
|
|
ConfigProcessor config_processor(users_config_path);
|
|
|
|
const auto loaded_config = config_processor.loadConfig();
|
|
|
|
config_processor.savePreprocessedConfig(loaded_config);
|
|
|
|
users_config = loaded_config.configuration;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-13 19:13:41 +00:00
|
|
|
users_config = getConfigurationFromXMLString(minimal_default_user_xml);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (users_config)
|
|
|
|
context->setUsersConfig(users_config);
|
|
|
|
else
|
|
|
|
throw Exception("Can't load config for users", ErrorCodes::CANNOT_LOAD_CONFIG);
|
2016-10-31 14:31:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 19:31:19 +00:00
|
|
|
static void showClientVersion()
|
|
|
|
{
|
2018-08-23 00:14:26 +00:00
|
|
|
std::cout << DBMS_NAME << " client version " << VERSION_STRING << "." << '\n';
|
2018-04-20 19:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string LocalServer::getHelpHeader() const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
"usage: clickhouse-local [initial table definition] [--query <query>]\n"
|
|
|
|
|
|
|
|
"clickhouse-local allows to execute SQL queries on your data files via single command line call."
|
|
|
|
" To do so, initially you need to define your data source and its format."
|
|
|
|
" After you can execute your SQL queries in usual manner.\n"
|
|
|
|
|
|
|
|
"There are two ways to define initial table keeping your data."
|
|
|
|
" Either just in first query like this:\n"
|
|
|
|
" CREATE TABLE <table> (<structure>) ENGINE = File(<input-format>, <file>);\n"
|
|
|
|
"Either through corresponding command line parameters --table --structure --input-format and --file.";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LocalServer::getHelpFooter() const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
"Example printing memory used by each Unix user:\n"
|
|
|
|
"ps aux | tail -n +2 | awk '{ printf(\"%s\\t%s\\n\", $1, $4) }' | "
|
|
|
|
"clickhouse-local -S \"user String, mem Float64\" -q"
|
|
|
|
" \"SELECT user, round(sum(mem), 2) as mem_total FROM table GROUP BY user ORDER"
|
|
|
|
" BY mem_total DESC FORMAT PrettyCompact\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalServer::init(int argc, char ** argv)
|
|
|
|
{
|
2018-05-23 16:12:46 +00:00
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2018-04-20 19:31:19 +00:00
|
|
|
/// Don't parse options with Poco library, we prefer neat boost::program_options
|
|
|
|
stopOptionsProcessing();
|
|
|
|
|
2018-05-23 16:12:46 +00:00
|
|
|
unsigned line_length = po::options_description::m_default_line_length;
|
|
|
|
unsigned min_description_length = line_length / 2;
|
|
|
|
if (isatty(STDIN_FILENO))
|
|
|
|
{
|
|
|
|
winsize terminal_size{};
|
|
|
|
ioctl(0, TIOCGWINSZ, &terminal_size);
|
|
|
|
line_length = std::max(3U, static_cast<unsigned>(terminal_size.ws_col));
|
|
|
|
min_description_length = std::min(min_description_length, line_length - 2);
|
|
|
|
}
|
2018-04-20 19:31:19 +00:00
|
|
|
|
|
|
|
#define DECLARE_SETTING(TYPE, NAME, DEFAULT, DESCRIPTION) (#NAME, po::value<std::string> (), DESCRIPTION)
|
2018-05-23 16:12:46 +00:00
|
|
|
po::options_description description("Main options", line_length, min_description_length);
|
2018-04-20 19:31:19 +00:00
|
|
|
description.add_options()
|
|
|
|
("help", "produce help message")
|
|
|
|
("config-file,c", po::value<std::string>(), "config-file path")
|
|
|
|
("query,q", po::value<std::string>(), "query")
|
|
|
|
("database,d", po::value<std::string>(), "database")
|
|
|
|
|
|
|
|
("table,N", po::value<std::string>(), "name of the initial table")
|
|
|
|
/// If structure argument is omitted then initial query is not generated
|
|
|
|
("structure,S", po::value<std::string>(), "structure of the initial table (list of column and type names)")
|
|
|
|
("file,f", po::value<std::string>(), "path to file with data of the initial table (stdin if not specified)")
|
|
|
|
("input-format", po::value<std::string>(), "input format of the initial table data")
|
|
|
|
("format,f", po::value<std::string>(), "default output format (clickhouse-client compatibility)")
|
|
|
|
("output-format", po::value<std::string>(), "default output format")
|
|
|
|
|
|
|
|
("stacktrace", "print stack traces of exceptions")
|
|
|
|
("echo", "print query before execution")
|
|
|
|
("verbose", "print query and other debugging info")
|
|
|
|
("ignore-error", "do not stop processing if a query failed")
|
|
|
|
("version,V", "print version information and exit")
|
|
|
|
APPLY_FOR_SETTINGS(DECLARE_SETTING);
|
|
|
|
#undef DECLARE_SETTING
|
|
|
|
|
|
|
|
/// Parse main commandline options.
|
|
|
|
po::parsed_options parsed = po::command_line_parser(argc, argv).options(description).run();
|
|
|
|
po::variables_map options;
|
|
|
|
po::store(parsed, options);
|
|
|
|
|
|
|
|
if (options.count("version") || options.count("V"))
|
|
|
|
{
|
|
|
|
showClientVersion();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.count("help"))
|
|
|
|
{
|
|
|
|
std::cout << getHelpHeader() << "\n";
|
|
|
|
std::cout << description << "\n";
|
|
|
|
std::cout << getHelpFooter() << "\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract settings and limits from the options.
|
|
|
|
#define EXTRACT_SETTING(TYPE, NAME, DEFAULT, DESCRIPTION) \
|
|
|
|
if (options.count(#NAME)) \
|
|
|
|
cmd_settings.set(#NAME, options[#NAME].as<std::string>());
|
|
|
|
APPLY_FOR_SETTINGS(EXTRACT_SETTING)
|
|
|
|
#undef EXTRACT_SETTING
|
|
|
|
|
|
|
|
/// Save received data into the internal config.
|
|
|
|
if (options.count("config-file"))
|
|
|
|
config().setString("config-file", options["config-file"].as<std::string>());
|
|
|
|
if (options.count("query"))
|
|
|
|
config().setString("query", options["query"].as<std::string>());
|
|
|
|
if (options.count("database"))
|
|
|
|
config().setString("default_database", options["database"].as<std::string>());
|
|
|
|
|
|
|
|
if (options.count("table"))
|
|
|
|
config().setString("table-name", options["table"].as<std::string>());
|
|
|
|
if (options.count("file"))
|
|
|
|
config().setString("table-file", options["file"].as<std::string>());
|
|
|
|
if (options.count("structure"))
|
|
|
|
config().setString("table-structure", options["structure"].as<std::string>());
|
|
|
|
if (options.count("input-format"))
|
|
|
|
config().setString("table-data-format", options["input-format"].as<std::string>());
|
|
|
|
if (options.count("format"))
|
|
|
|
config().setString("format", options["format"].as<std::string>());
|
|
|
|
if (options.count("output-format"))
|
|
|
|
config().setString("output-format", options["output-format"].as<std::string>());
|
|
|
|
|
|
|
|
if (options.count("stacktrace"))
|
|
|
|
config().setBool("stacktrace", true);
|
|
|
|
if (options.count("echo"))
|
|
|
|
config().setBool("echo", true);
|
|
|
|
if (options.count("verbose"))
|
|
|
|
config().setBool("verbose", true);
|
|
|
|
if (options.count("ignore-error"))
|
|
|
|
config().setBool("ignore-error", true);
|
|
|
|
}
|
|
|
|
|
2018-05-11 14:35:32 +00:00
|
|
|
void LocalServer::applyCmdOptions(Context & context)
|
|
|
|
{
|
|
|
|
context.setDefaultFormat(config().getString("output-format", config().getString("format", "TSV")));
|
|
|
|
applyCmdSettings(context);
|
|
|
|
}
|
|
|
|
|
2016-10-25 12:14:27 +00:00
|
|
|
}
|
2016-10-31 19:54:49 +00:00
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
int mainEntryClickHouseLocal(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
DB::LocalServer app;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
app.init(argc, argv);
|
|
|
|
return app.run();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2018-08-23 00:14:26 +00:00
|
|
|
std::cerr << DB::getCurrentExceptionMessage(true) << '\n';
|
2017-12-02 02:47:12 +00:00
|
|
|
auto code = DB::getCurrentExceptionCode();
|
|
|
|
return code ? code : 1;
|
|
|
|
}
|
|
|
|
}
|