From 8b78d9b0d19c3d3515f2d46624cc0b2813ec2ee9 Mon Sep 17 00:00:00 2001 From: Vage Ogannisian Date: Fri, 24 May 2024 15:15:49 +0000 Subject: [PATCH] fixes --- src/Interpreters/executeQuery.cpp | 25 ++++++++++--------------- src/Interpreters/executeQuery.h | 26 ++++++++++++++++---------- src/Server/HTTPHandler.cpp | 20 +++++++++++++------- src/Server/TabularHandler.h | 8 ++------ 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index b77ccd607bd7..8a246aff6c88 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -839,7 +839,7 @@ executeQueryImpl(QueryData & query_data, ContextMutablePtr context, QueryFlags f if (insert_query && insert_query->select) { /// Prepare Input storage before executing interpreter if we already got a buffer with data. - if (query_data.istr) + if (query_data.input_buf) { ASTPtr input_function; insert_query->tryFindInputFunction(input_function); @@ -1253,9 +1253,9 @@ size_t getMaxQuerySize(ContextMutablePtr context, QueryFlags flags) return context->getSettingsRef().max_query_size; } -QueryData getQueryData(ReadBuffer & istr, ContextMutablePtr context, QueryFlags flags, const QueryProcessingStage::Enum stage) +QueryData::QueryData(ReadBuffer & istr, ContextMutablePtr context, QueryFlags flags, const QueryProcessingStage::Enum stage) { - auto parse_buf = std::make_unique>(); + parse_buf = std::make_unique>(); const char * begin; const char * end; @@ -1323,10 +1323,6 @@ QueryData getQueryData(ReadBuffer & istr, ContextMutablePtr context, QueryFlags assert(internal || CurrentThread::get().getQueryContext()); assert(internal || CurrentThread::get().getQueryContext()->getCurrentQueryId() == CurrentThread::getQueryId()); - ASTPtr ast; - String query; - String query_for_logging; - try { /// Parse the query from string. @@ -1452,18 +1448,13 @@ QueryData getQueryData(ReadBuffer & istr, ContextMutablePtr context, QueryFlags throw; } - return QueryData{ - .ast = ast, - .istr = wrapReadBufferReference(istr), - .parse_buf = std::move(parse_buf), - .query = query, - .query_for_logging = query_for_logging}; + input_buf = wrapReadBufferReference(istr); } std::pair executeQuery(const String & query, ContextMutablePtr context, QueryFlags flags, QueryProcessingStage::Enum stage) { auto istr = std::make_unique(query); - auto query_data = getQueryData(*istr, context, flags, stage); + auto query_data = QueryData(*istr, context, flags, stage); auto res = executeQueryImpl(query_data, context, flags, stage); @@ -1478,6 +1469,10 @@ std::pair executeQuery(const String & query, ContextMutablePtr return std::make_pair(query_data.ast, std::move(res)); } +QueryData::QueryData(ASTPtr ast_) : ast(ast_) +{ +} + void executeQuery( ReadBuffer & istr, WriteBuffer & ostr, @@ -1553,7 +1548,7 @@ void executeQuery( }; - auto query_data = getQueryData(istr, context, flags, QueryProcessingStage::Complete); + auto query_data = QueryData(istr, context, flags, QueryProcessingStage::Complete); auto ast = query_data.ast; BlockIO streams; diff --git a/src/Interpreters/executeQuery.h b/src/Interpreters/executeQuery.h index 4a69392fd98c..0292682a2bb1 100644 --- a/src/Interpreters/executeQuery.h +++ b/src/Interpreters/executeQuery.h @@ -36,22 +36,28 @@ struct QueryFlags bool distributed_backup_restore = false; /// If true, this query is a part of backup restore. }; -struct QueryData +class QueryData { - ASTPtr ast; +public: + QueryData() = delete; - std::unique_ptr istr; - std::unique_ptr> parse_buf; + QueryData( + ReadBuffer & istr, + ContextMutablePtr context, + QueryFlags flags_ = {}, + const QueryProcessingStage::Enum stage_ = QueryProcessingStage::Enum::Complete); + + QueryData(ASTPtr ast_); + + ASTPtr ast; + std::unique_ptr input_buf; std::string query; std::string query_for_logging; -}; -QueryData getQueryData( - ReadBuffer & istr, - ContextMutablePtr context, - QueryFlags flags = {}, - const QueryProcessingStage::Enum stage = QueryProcessingStage::Enum::Complete); +private: + std::unique_ptr> parse_buf; +}; /// Parse and execute a query. void executeQuery( diff --git a/src/Server/HTTPHandler.cpp b/src/Server/HTTPHandler.cpp index cbe91d400154..184685573865 100644 --- a/src/Server/HTTPHandler.cpp +++ b/src/Server/HTTPHandler.cpp @@ -606,9 +606,8 @@ void combineSelectQuery(ASTSelectQuery * select_query, HTTPQueryAST & http_query combineOrderExpressions(select_query, http_query_ast.order_expressions); } -QueryData combineQueryWithParams(ReadBuffer & in, HTMLForm & params, ContextMutablePtr context) +void combineQueryWithParams(QueryData& query_data, HTMLForm & params) { - auto query_data = getQueryData(in, context); auto http_query_ast = getHTTPQueryAST(params); if (auto * select_query = query_data.ast->as()) @@ -621,8 +620,6 @@ QueryData combineQueryWithParams(ReadBuffer & in, HTMLForm & params, ContextMuta if (auto * child_select_query = child->as()) combineSelectQuery(child_select_query, http_query_ast); } - - return query_data; } @@ -1024,17 +1021,26 @@ void HTTPHandler::processQuery( customizeContext(request, context, *in_post_maybe_compressed); + std::shared_ptr query_data; + auto query = getQuery(request, params, context); std::unique_ptr in_param = std::make_unique(query); - std::unique_ptr in; + in = has_external_data ? std::move(in_param) : std::make_unique(std::move(in_param), std::move(in_post_maybe_compressed)); - auto query_data = combineQueryWithParams(*in, params, context); + if (!in->eof()) + query_data = std::make_shared(*in, context); + else if (auto query_ast = getQueryAST(request, params, context)) + query_data = query_ast; + else + throw Exception(ErrorCodes::SYNTAX_ERROR, "Empty request"); + + combineQueryWithParams(*query_data, params); executeQuery( - query_data, + *query_data, *used_output.out_maybe_delayed_and_compressed, /* allow_into_outfile = */ false, context, diff --git a/src/Server/TabularHandler.h b/src/Server/TabularHandler.h index c484fc3c8ca2..3445e47f43d4 100644 --- a/src/Server/TabularHandler.h +++ b/src/Server/TabularHandler.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -12,15 +13,10 @@ namespace DB class TabularHandler : public HTTPHandler { -private: - LoggerPtr log; - - std::optional where; - public: TabularHandler(IServer & server_, const std::optional & content_type_override_ = std::nullopt); - std::string getQuery(HTTPServerRequest & request, HTMLForm & params, ContextMutablePtr context) override; + std::shared_ptr getQueryAST(HTTPServerRequest & request, HTMLForm & params, ContextMutablePtr context) override; bool customizeQueryParam(ContextMutablePtr context, const std::string & key, const std::string & value) override; };