ClickHouse/dbms/src/Interpreters/executeQuery.cpp

96 lines
2.8 KiB
C++
Raw Normal View History

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
{
void executeQuery(
ReadBuffer & istr,
WriteBuffer & ostr,
Context & context,
2012-03-05 00:09:41 +00:00
BlockInputStreamPtr & query_plan)
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-03-05 00:09:41 +00:00
if (istr.buffer().end() - istr.position() >= static_cast<ssize_t>(context.settings.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-03-05 00:09:41 +00:00
parse_buf.resize(context.settings.max_query_size);
parse_buf.resize(istr.read(&parse_buf[0], context.settings.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 "
2011-10-30 11:30:52 +00:00
+ Poco::NumberFormatter::format(pos - begin) + ": "
+ 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
2011-11-06 20:47:07 +00:00
formatAST(*ast, std::cerr);
std::cerr << std::endl;
2012-03-05 00:09:41 +00:00
InterpreterQuery interpreter(ast, context);
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,
Context & context)
{
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 "
2012-03-11 08:52:56 +00:00
+ Poco::NumberFormatter::format(pos - begin) + ": "
+ 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
formatAST(*ast, std::cerr);
std::cerr << std::endl;
InterpreterQuery interpreter(ast, context);
return interpreter.execute();
}
2011-10-30 11:30:52 +00:00
}