From 41d735fd56287f77a5123635d551a49de6594187 Mon Sep 17 00:00:00 2001 From: kssenii Date: Fri, 20 Aug 2021 11:38:50 +0300 Subject: [PATCH] Fix context --- programs/local/LocalServer.cpp | 31 +++++++------------------------ programs/local/LocalServer.h | 4 +--- src/Client/LocalConnection.cpp | 16 ++++++++++------ src/Client/LocalConnection.h | 3 +++ 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/programs/local/LocalServer.cpp b/programs/local/LocalServer.cpp index 370fa6176ab..e0fe9b9885b 100644 --- a/programs/local/LocalServer.cpp +++ b/programs/local/LocalServer.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include namespace fs = std::filesystem; @@ -401,6 +402,11 @@ try processConfig(); applyCmdSettings(global_context); + + connection_parameters = ConnectionParameters(config()); + /// Using query context withcmd settings. + connection = std::make_unique(global_context); + // query_context->makeSessionContext(); // query_context->authenticate("default", "", Poco::Net::SocketAddress{}); @@ -471,24 +477,6 @@ void LocalServer::processConfig() shared_context = Context::createShared(); global_context = Context::createGlobal(shared_context.get()); - const auto & settings = global_context->getSettingsRef(); - - std::vector queries; - auto parse_res = splitMultipartQuery(queries_str, queries, settings.max_query_size, settings.max_parser_depth); - - if (!parse_res.second) - throw Exception("Cannot parse and execute the following part of query: " + String(parse_res.first), ErrorCodes::SYNTAX_ERROR); - - /// Authenticate and create a context to execute queries. - Session session{global_context, ClientInfo::Interface::TCP}; - session.authenticate("default", "", Poco::Net::SocketAddress{}); - - /// Use the same context for all queries. - auto context = session.makeQueryContext(); - context->makeSessionContext(); /// initial_create_query requires a session context to be set. - context->setCurrentQueryId(""); - applyCmdSettings(context); - global_context->makeGlobalContext(); global_context->setApplicationType(Context::ApplicationType::LOCAL); @@ -500,12 +488,7 @@ void LocalServer::processConfig() if (config().has("macros")) global_context->setMacros(std::make_unique(config(), "macros", log)); - is_default_format = !config().has("vertical") && !config().has("format"); - if (config().has("vertical")) - format = config().getString("format", "Vertical"); - else - format = config().getString("format", is_interactive ? "PrettyCompact" : "TabSeparated"); - + format = config().getString("output-format", config().getString("format", is_interactive ? "PrettyCompact" : "TSV")); insert_format = "Values"; /// Setting value from cmd arg overrides one from config if (global_context->getSettingsRef().max_insert_block_size.changed) diff --git a/programs/local/LocalServer.h b/programs/local/LocalServer.h index 88fccd9f732..c05223b78a4 100644 --- a/programs/local/LocalServer.h +++ b/programs/local/LocalServer.h @@ -30,6 +30,7 @@ public: ~LocalServer() override { + connection.reset(); if (global_context) global_context->shutdown(); /// required for properly exception handling } @@ -39,9 +40,6 @@ protected: void connect() override { - connection_parameters = ConnectionParameters(config()); - /// Using query context withcmd settings. - connection = std::make_unique(global_context); } void processError(const String & query) const override; diff --git a/src/Client/LocalConnection.cpp b/src/Client/LocalConnection.cpp index ddea3c0431e..8b8227ae0e1 100644 --- a/src/Client/LocalConnection.cpp +++ b/src/Client/LocalConnection.cpp @@ -2,7 +2,6 @@ #include #include - namespace DB { @@ -15,7 +14,10 @@ namespace ErrorCodes LocalConnection::LocalConnection(ContextPtr context_) : WithContext(context_) + , session(getContext(), ClientInfo::Interface::TCP) { + /// Authenticate and create a context to execute queries. + session.authenticate("default", "", Poco::Net::SocketAddress{}); } void LocalConnection::setDefaultDatabase(const String & database) @@ -67,12 +69,8 @@ void LocalConnection::sendQuery( const ClientInfo *, bool) { - query_context = Context::createCopy(getContext()); - query_context->makeQueryContext(); - // query_context->setProgressCallback([this] (const Progress & value) { return this->updateProgress(value); }); - query_context->setCurrentQueryId(""); + /// Use the same context for all queries. // applyCmdSettings(query_context); - CurrentThread::QueryScope query_scope_holder(query_context); /// query_context->setCurrentDatabase(default_database); @@ -82,6 +80,11 @@ void LocalConnection::sendQuery( state->query_id = query_id_; state->query = query_; + query_context = session.makeQueryContext(); + query_context->makeSessionContext(); /// initial_create_query requires a session context to be set. + query_context->setCurrentQueryId(""); + CurrentThread::QueryScope query_scope_holder(query_context); + try { state->io = executeQuery(state->query, query_context, false, state->stage, true); @@ -184,6 +187,7 @@ void LocalConnection::finishQuery() } state->io.onFinish(); + state.reset(); query_context.reset(); } diff --git a/src/Client/LocalConnection.h b/src/Client/LocalConnection.h index aef93e201d2..4fed62e2d99 100644 --- a/src/Client/LocalConnection.h +++ b/src/Client/LocalConnection.h @@ -4,11 +4,13 @@ #include #include #include +#include namespace DB { + /// State of query processing. struct LocalQueryState { @@ -116,6 +118,7 @@ public: private: ContextMutablePtr query_context; + Session session; String description;