Fix context

This commit is contained in:
kssenii 2021-08-20 11:38:50 +03:00
parent 39e80a47a3
commit 41d735fd56
4 changed files with 21 additions and 33 deletions

View File

@ -44,6 +44,7 @@
#include <common/argsToConfig.h>
#include <Common/TerminalSize.h>
#include <Common/randomSeed.h>
#include <Interpreters/Session.h>
#include <filesystem>
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<LocalConnection>(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<String> 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<Macros>(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)

View File

@ -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<LocalConnection>(global_context);
}
void processError(const String & query) const override;

View File

@ -2,7 +2,6 @@
#include <Interpreters/executeQuery.h>
#include <Storages/IStorage.h>
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();
}

View File

@ -4,11 +4,13 @@
#include <DataStreams/AsynchronousBlockInputStream.h>
#include <Processors/Executors/PullingAsyncPipelineExecutor.h>
#include <IO/TimeoutSetter.h>
#include <Interpreters/Session.h>
namespace DB
{
/// State of query processing.
struct LocalQueryState
{
@ -116,6 +118,7 @@ public:
private:
ContextMutablePtr query_context;
Session session;
String description;