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,
|
|
|
|
|
QueryProcessingStage::Enum stage)
|
2011-10-30 11:30:52 +00:00
|
|
|
|
{
|
2012-03-26 04:17:17 +00:00
|
|
|
|
ParserQuery parser;
|
|
|
|
|
ASTPtr ast;
|
2011-10-30 11:30:52 +00:00
|
|
|
|
std::string expected;
|
|
|
|
|
|
|
|
|
|
std::vector<char> parse_buf;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
/// Распарсенный запрос должен заканчиваться на конец входных данных или на точку с запятой.
|
|
|
|
|
if (!parse_res || (pos != end && *pos != ';'))
|
2012-03-26 04:17:17 +00:00
|
|
|
|
throw Exception("Syntax error: failed at position "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(pos - begin) + ": "
|
2011-10-30 11:30:52 +00:00
|
|
|
|
+ std::string(pos, std::min(SHOW_CHARS_ON_SYNTAX_ERROR, end - pos))
|
|
|
|
|
+ ", expected " + (parse_res ? "end of query" : expected) + ".",
|
2012-03-26 04:17:17 +00:00
|
|
|
|
ErrorCodes::SYNTAX_ERROR);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
|
2013-06-15 06:44:22 +00:00
|
|
|
|
LOG_DEBUG(&Logger::get("executeQuery"), std::string(begin, pos - begin));
|
|
|
|
|
|
2012-12-26 20:29:28 +00:00
|
|
|
|
/// Проверка ограничений.
|
|
|
|
|
checkLimits(*ast, context.getSettingsRef().limits);
|
|
|
|
|
|
2012-05-22 18:32:45 +00:00
|
|
|
|
InterpreterQuery interpreter(ast, context, stage);
|
2012-01-09 19:20:48 +00:00
|
|
|
|
interpreter.execute(ostr, &istr, query_plan);
|
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,
|
|
|
|
|
QueryProcessingStage::Enum stage)
|
2012-03-11 08:52:56 +00:00
|
|
|
|
{
|
2012-03-26 04:17:17 +00:00
|
|
|
|
ParserQuery parser;
|
|
|
|
|
ASTPtr ast;
|
2012-03-11 08:52:56 +00:00
|
|
|
|
std::string expected;
|
|
|
|
|
|
|
|
|
|
const char * begin = query.data();
|
|
|
|
|
const char * end = begin + query.size();
|
|
|
|
|
const char * pos = begin;
|
|
|
|
|
|
|
|
|
|
bool parse_res = parser.parse(pos, end, ast, expected);
|
|
|
|
|
|
|
|
|
|
/// Распарсенный запрос должен заканчиваться на конец входных данных или на точку с запятой.
|
|
|
|
|
if (!parse_res || (pos != end && *pos != ';'))
|
2012-03-26 04:17:17 +00:00
|
|
|
|
throw Exception("Syntax error: failed at position "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(pos - begin) + ": "
|
2012-03-11 08:52:56 +00:00
|
|
|
|
+ std::string(pos, std::min(SHOW_CHARS_ON_SYNTAX_ERROR, end - pos))
|
|
|
|
|
+ ", expected " + (parse_res ? "end of query" : expected) + ".",
|
2012-03-26 04:17:17 +00:00
|
|
|
|
ErrorCodes::SYNTAX_ERROR);
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
2012-06-25 03:56:45 +00:00
|
|
|
|
// formatAST(*ast, std::cerr);
|
|
|
|
|
// std::cerr << std::endl;
|
2012-03-11 08:52:56 +00:00
|
|
|
|
|
2012-12-26 20:29:28 +00:00
|
|
|
|
/// Проверка ограничений.
|
|
|
|
|
checkLimits(*ast, context.getSettingsRef().limits);
|
|
|
|
|
|
2012-05-22 18:32:45 +00:00
|
|
|
|
InterpreterQuery interpreter(ast, context, stage);
|
2012-03-11 08:52:56 +00:00
|
|
|
|
return interpreter.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
|
}
|