Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nooblose committed May 24, 2024
1 parent 7ddf7ec commit 8b78d9b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
25 changes: 10 additions & 15 deletions src/Interpreters/executeQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<PODArray<char>>();
parse_buf = std::make_unique<PODArray<char>>();
const char * begin;
const char * end;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<ASTPtr, BlockIO> executeQuery(const String & query, ContextMutablePtr context, QueryFlags flags, QueryProcessingStage::Enum stage)
{
auto istr = std::make_unique<ReadBufferFromString>(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);

Expand All @@ -1478,6 +1469,10 @@ std::pair<ASTPtr, BlockIO> 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,
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 16 additions & 10 deletions src/Interpreters/executeQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReadBuffer> istr;
std::unique_ptr<PODArray<char>> parse_buf;
QueryData(
ReadBuffer & istr,
ContextMutablePtr context,
QueryFlags flags_ = {},
const QueryProcessingStage::Enum stage_ = QueryProcessingStage::Enum::Complete);

QueryData(ASTPtr ast_);

ASTPtr ast;
std::unique_ptr<ReadBuffer> 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<PODArray<char>> parse_buf;
};

/// Parse and execute a query.
void executeQuery(
Expand Down
20 changes: 13 additions & 7 deletions src/Server/HTTPHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ASTSelectQuery>())
Expand All @@ -621,8 +620,6 @@ QueryData combineQueryWithParams(ReadBuffer & in, HTMLForm & params, ContextMuta
if (auto * child_select_query = child->as<ASTSelectQuery>())
combineSelectQuery(child_select_query, http_query_ast);
}

return query_data;
}


Expand Down Expand Up @@ -1024,17 +1021,26 @@ void HTTPHandler::processQuery(

customizeContext(request, context, *in_post_maybe_compressed);

std::shared_ptr<QueryData> query_data;

auto query = getQuery(request, params, context);
std::unique_ptr<ReadBuffer> in_param = std::make_unique<ReadBufferFromString>(query);

std::unique_ptr<ReadBuffer> in;

in = has_external_data ? std::move(in_param)
: std::make_unique<ConcatReadBuffer>(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<QueryData>(*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,
Expand Down
8 changes: 2 additions & 6 deletions src/Server/TabularHandler.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <memory>
#include <optional>
#include <string>

Expand All @@ -12,15 +13,10 @@ namespace DB

class TabularHandler : public HTTPHandler
{
private:
LoggerPtr log;

std::optional<std::string> where;

public:
TabularHandler(IServer & server_, const std::optional<String> & content_type_override_ = std::nullopt);

std::string getQuery(HTTPServerRequest & request, HTMLForm & params, ContextMutablePtr context) override;
std::shared_ptr<QueryData> getQueryAST(HTTPServerRequest & request, HTMLForm & params, ContextMutablePtr context) override;

bool customizeQueryParam(ContextMutablePtr context, const std::string & key, const std::string & value) override;
};
Expand Down

0 comments on commit 8b78d9b

Please sign in to comment.