2014-01-03 08:20:13 +00:00
|
|
|
|
#include <DB/Common/ProfileEvents.h>
|
|
|
|
|
|
2011-11-06 20:47:07 +00:00
|
|
|
|
#include <DB/Parsers/formatAST.h>
|
|
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
|
#include <DB/DataStreams/BlockIO.h>
|
2011-10-30 11:30:52 +00:00
|
|
|
|
#include <DB/Interpreters/executeQuery.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2012-12-26 20:29:28 +00:00
|
|
|
|
static void checkLimits(const IAST & ast, const Limits & limits)
|
|
|
|
|
{
|
|
|
|
|
if (limits.max_ast_depth)
|
|
|
|
|
ast.checkDepth(limits.max_ast_depth);
|
|
|
|
|
if (limits.max_ast_elements)
|
|
|
|
|
ast.checkSize(limits.max_ast_elements);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
|
void executeQuery(
|
|
|
|
|
ReadBuffer & istr,
|
|
|
|
|
WriteBuffer & ostr,
|
|
|
|
|
Context & context,
|
2012-05-22 18:32:45 +00:00
|
|
|
|
BlockInputStreamPtr & query_plan,
|
2013-09-03 23:58:05 +00:00
|
|
|
|
bool internal,
|
2012-05-22 18:32:45 +00:00
|
|
|
|
QueryProcessingStage::Enum stage)
|
2011-10-30 11:30:52 +00:00
|
|
|
|
{
|
2014-01-03 08:20:13 +00:00
|
|
|
|
ProfileEvents::increment(ProfileEvents::Query);
|
|
|
|
|
|
2012-03-26 04:17:17 +00:00
|
|
|
|
ParserQuery parser;
|
|
|
|
|
ASTPtr ast;
|
2014-03-10 12:25:37 +00:00
|
|
|
|
const char * expected = "";
|
2011-10-30 11:30:52 +00:00
|
|
|
|
|
2014-04-02 18:38:17 +00:00
|
|
|
|
PODArray<char> parse_buf;
|
2011-10-30 11:30:52 +00:00
|
|
|
|
const char * begin;
|
|
|
|
|
const char * end;
|
|
|
|
|
|
|
|
|
|
/// Если в istr ещё ничего нет, то считываем кусок данных
|
|
|
|
|
if (istr.buffer().size() == 0)
|
|
|
|
|
istr.next();
|
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
size_t max_query_size = context.getSettings().max_query_size;
|
|
|
|
|
|
|
|
|
|
if (istr.buffer().end() - istr.position() >= static_cast<ssize_t>(max_query_size))
|
2011-10-30 11:30:52 +00:00
|
|
|
|
{
|
|
|
|
|
/// Если оставшийся размер буфера istr достаточен, чтобы распарсить запрос до max_query_size, то парсим прямо в нём
|
|
|
|
|
begin = istr.position();
|
|
|
|
|
end = istr.buffer().end();
|
|
|
|
|
istr.position() += end - begin;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/// Если нет - считываем достаточное количество данных в parse_buf
|
2012-08-02 17:33:31 +00:00
|
|
|
|
parse_buf.resize(max_query_size);
|
|
|
|
|
parse_buf.resize(istr.read(&parse_buf[0], max_query_size));
|
2011-10-30 11:30:52 +00:00
|
|
|
|
begin = &parse_buf[0];
|
|
|
|
|
end = begin + parse_buf.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char * pos = begin;
|
|
|
|
|
|
|
|
|
|
bool parse_res = parser.parse(pos, end, ast, expected);
|
|
|
|
|
|
2013-11-28 20:09:38 +00:00
|
|
|
|
if (pos == begin && (end == begin || *pos == ';'))
|
2013-10-26 03:53:51 +00:00
|
|
|
|
throw Exception("Empty query", ErrorCodes::EMPTY_QUERY);
|
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
|
/// Распарсенный запрос должен заканчиваться на конец входных данных или на точку с запятой.
|
|
|
|
|
if (!parse_res || (pos != end && *pos != ';'))
|
2014-04-13 08:52:50 +00:00
|
|
|
|
throw Exception(getSyntaxErrorMessage(parse_res, begin, end, pos, expected),
|
2012-03-26 04:17:17 +00:00
|
|
|
|
ErrorCodes::SYNTAX_ERROR);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
|
2014-03-02 20:37:11 +00:00
|
|
|
|
/// Засунем запрос в строку. Она выводится в лог и в processlist. Если запрос INSERT, то не будем включать данные для вставки.
|
|
|
|
|
auto insert = dynamic_cast<const ASTInsertQuery *>(&*ast);
|
2014-04-04 02:27:01 +00:00
|
|
|
|
size_t query_size = (insert && insert->data) ? (insert->data - begin) : (pos - begin);
|
|
|
|
|
|
|
|
|
|
if (query_size > max_query_size)
|
|
|
|
|
throw Exception("Query is too large (" + toString(query_size) + ")."
|
|
|
|
|
" max_query_size = " + toString(max_query_size), ErrorCodes::QUERY_IS_TOO_LARGE);
|
|
|
|
|
|
|
|
|
|
String query(begin, query_size);
|
2013-09-03 20:21:28 +00:00
|
|
|
|
|
|
|
|
|
LOG_DEBUG(&Logger::get("executeQuery"), query);
|
2013-09-03 20:31:27 +00:00
|
|
|
|
|
|
|
|
|
/// Положим запрос в список процессов. Но запрос SHOW PROCESSLIST класть не будем.
|
|
|
|
|
ProcessList::EntryPtr process_list_entry;
|
2014-04-08 07:47:51 +00:00
|
|
|
|
if (!internal && nullptr == dynamic_cast<const ASTShowProcesslistQuery *>(&*ast))
|
2013-11-03 05:32:42 +00:00
|
|
|
|
{
|
|
|
|
|
process_list_entry = context.getProcessList().insert(
|
2014-05-03 19:52:35 +00:00
|
|
|
|
query, context.getUser(), context.getCurrentQueryId(), context.getIPAddress(),
|
2014-05-03 22:57:43 +00:00
|
|
|
|
context.getSettingsRef().limits.max_memory_usage,
|
2014-05-03 19:52:35 +00:00
|
|
|
|
context.getSettingsRef().queue_max_wait_ms.totalMilliseconds(),
|
|
|
|
|
context.getSettingsRef().replace_running_query);
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
2013-11-03 05:32:42 +00:00
|
|
|
|
context.setProcessListElement(&process_list_entry->get());
|
|
|
|
|
}
|
2013-06-15 06:44:22 +00:00
|
|
|
|
|
2012-12-26 20:29:28 +00:00
|
|
|
|
/// Проверка ограничений.
|
|
|
|
|
checkLimits(*ast, context.getSettingsRef().limits);
|
|
|
|
|
|
2013-08-12 00:36:18 +00:00
|
|
|
|
QuotaForIntervals & quota = context.getQuota();
|
|
|
|
|
time_t current_time = time(0);
|
|
|
|
|
|
|
|
|
|
quota.checkExceeded(current_time);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
InterpreterQuery interpreter(ast, context, stage);
|
|
|
|
|
interpreter.execute(ostr, &istr, query_plan);
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
quota.addError(current_time);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quota.addQuery(current_time);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
|
BlockIO executeQuery(
|
|
|
|
|
const String & query,
|
2012-05-22 18:32:45 +00:00
|
|
|
|
Context & context,
|
2013-09-03 23:58:05 +00:00
|
|
|
|
bool internal,
|
2012-05-22 18:32:45 +00:00
|
|
|
|
QueryProcessingStage::Enum stage)
|
2012-03-11 08:52:56 +00:00
|
|
|
|
{
|
2014-01-03 08:20:13 +00:00
|
|
|
|
ProfileEvents::increment(ProfileEvents::Query);
|
|
|
|
|
|
2012-03-26 04:17:17 +00:00
|
|
|
|
ParserQuery parser;
|
|
|
|
|
ASTPtr ast;
|
2014-03-10 12:25:37 +00:00
|
|
|
|
const char * expected = "";
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
|
|
|
|
const char * begin = query.data();
|
|
|
|
|
const char * end = begin + query.size();
|
|
|
|
|
const char * pos = begin;
|
|
|
|
|
|
|
|
|
|
bool parse_res = parser.parse(pos, end, ast, expected);
|
|
|
|
|
|
2013-11-28 20:09:38 +00:00
|
|
|
|
if (pos == begin && (end == begin || *pos == ';'))
|
2013-10-26 03:53:51 +00:00
|
|
|
|
throw Exception("Empty query", ErrorCodes::EMPTY_QUERY);
|
|
|
|
|
|
2012-03-11 08:52:56 +00:00
|
|
|
|
/// Распарсенный запрос должен заканчиваться на конец входных данных или на точку с запятой.
|
|
|
|
|
if (!parse_res || (pos != end && *pos != ';'))
|
2014-04-13 08:52:50 +00:00
|
|
|
|
throw Exception(getSyntaxErrorMessage(parse_res, begin, end, pos, expected),
|
2012-03-26 04:17:17 +00:00
|
|
|
|
ErrorCodes::SYNTAX_ERROR);
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
2012-12-26 20:29:28 +00:00
|
|
|
|
/// Проверка ограничений.
|
|
|
|
|
checkLimits(*ast, context.getSettingsRef().limits);
|
|
|
|
|
|
2013-08-12 00:36:18 +00:00
|
|
|
|
QuotaForIntervals & quota = context.getQuota();
|
|
|
|
|
time_t current_time = time(0);
|
|
|
|
|
|
|
|
|
|
quota.checkExceeded(current_time);
|
|
|
|
|
|
2013-09-03 23:58:05 +00:00
|
|
|
|
BlockIO res;
|
2013-09-07 04:54:59 +00:00
|
|
|
|
|
|
|
|
|
/// Положим запрос в список процессов. Но запрос SHOW PROCESSLIST класть не будем.
|
|
|
|
|
ProcessList::EntryPtr process_list_entry;
|
2014-04-08 07:47:51 +00:00
|
|
|
|
if (!internal && nullptr == dynamic_cast<const ASTShowProcesslistQuery *>(&*ast))
|
2013-11-03 05:32:42 +00:00
|
|
|
|
{
|
|
|
|
|
process_list_entry = context.getProcessList().insert(
|
2014-05-03 19:52:35 +00:00
|
|
|
|
query, context.getUser(), context.getCurrentQueryId(), context.getIPAddress(),
|
2014-05-03 22:57:43 +00:00
|
|
|
|
context.getSettingsRef().limits.max_memory_usage,
|
2014-05-03 19:52:35 +00:00
|
|
|
|
context.getSettingsRef().queue_max_wait_ms.totalMilliseconds(),
|
|
|
|
|
context.getSettingsRef().replace_running_query);
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
2013-11-03 05:32:42 +00:00
|
|
|
|
context.setProcessListElement(&process_list_entry->get());
|
|
|
|
|
}
|
2013-09-03 23:58:05 +00:00
|
|
|
|
|
2013-08-12 00:36:18 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
InterpreterQuery interpreter(ast, context, stage);
|
|
|
|
|
res = interpreter.execute();
|
2013-09-07 04:54:59 +00:00
|
|
|
|
|
|
|
|
|
/// Держим элемент списка процессов до конца обработки запроса.
|
|
|
|
|
res.process_list_entry = process_list_entry;
|
2013-08-12 00:36:18 +00:00
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
quota.addError(current_time);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quota.addQuery(current_time);
|
|
|
|
|
return res;
|
2012-03-11 08:52:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
|
}
|