2015-04-11 03:10:23 +00:00
|
|
|
|
#include <DB/Parsers/parseQuery.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Из позиции в (возможно многострочном) запросе получить номер строки и номер столбца в строке.
|
|
|
|
|
* Используется в сообщении об ошибках.
|
|
|
|
|
*/
|
|
|
|
|
static inline std::pair<size_t, size_t> getLineAndCol(IParser::Pos begin, IParser::Pos pos)
|
|
|
|
|
{
|
|
|
|
|
size_t line = 0;
|
|
|
|
|
|
|
|
|
|
IParser::Pos nl;
|
|
|
|
|
while (nullptr != (nl = reinterpret_cast<IParser::Pos>(memchr(begin, '\n', pos - begin))))
|
|
|
|
|
{
|
|
|
|
|
++line;
|
|
|
|
|
begin = nl + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Нумеруются с единицы.
|
|
|
|
|
return { line + 1, pos - begin + 1 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static std::string getSyntaxErrorMessage(
|
|
|
|
|
IParser::Pos begin,
|
|
|
|
|
IParser::Pos end,
|
|
|
|
|
IParser::Pos max_parsed_pos,
|
|
|
|
|
Expected expected,
|
2015-04-11 04:15:14 +00:00
|
|
|
|
bool hilite,
|
2015-04-11 03:10:23 +00:00
|
|
|
|
const std::string & description)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream message;
|
|
|
|
|
|
|
|
|
|
message << "Syntax error";
|
|
|
|
|
|
|
|
|
|
if (!description.empty())
|
|
|
|
|
message << " (" << description << ")";
|
|
|
|
|
|
2015-04-11 04:15:14 +00:00
|
|
|
|
if (max_parsed_pos == end || *max_parsed_pos == ';')
|
2015-04-11 03:10:23 +00:00
|
|
|
|
{
|
2015-04-14 01:39:30 +00:00
|
|
|
|
message << ": failed at end of query.\n";
|
|
|
|
|
|
|
|
|
|
if (expected && *expected && *expected != '.')
|
|
|
|
|
message << "Expected " << expected;
|
2015-04-11 04:15:14 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message << ": failed at position " << (max_parsed_pos - begin + 1);
|
|
|
|
|
|
|
|
|
|
/// Если запрос многострочный.
|
|
|
|
|
IParser::Pos nl = reinterpret_cast<IParser::Pos>(memchr(begin, '\n', end - begin));
|
|
|
|
|
if (nullptr != nl && nl + 1 != end)
|
|
|
|
|
{
|
|
|
|
|
size_t line = 0;
|
|
|
|
|
size_t col = 0;
|
|
|
|
|
std::tie(line, col) = getLineAndCol(begin, max_parsed_pos);
|
|
|
|
|
|
|
|
|
|
message << " (line " << line << ", col " << col << ")";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hilite)
|
|
|
|
|
{
|
|
|
|
|
message << ":\n\n";
|
|
|
|
|
message.write(begin, max_parsed_pos - begin);
|
|
|
|
|
message << "\033[41;1m" << *max_parsed_pos << "\033[0m"; /// Ярко-красный фон.
|
|
|
|
|
message.write(max_parsed_pos + 1, end - max_parsed_pos - 1);
|
|
|
|
|
message << "\n\n";
|
|
|
|
|
|
|
|
|
|
if (expected && *expected && *expected != '.')
|
|
|
|
|
message << "Expected " << expected;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message << ": " << std::string(max_parsed_pos, std::min(SHOW_CHARS_ON_SYNTAX_ERROR, end - max_parsed_pos));
|
|
|
|
|
|
|
|
|
|
if (expected && *expected && *expected != '.')
|
|
|
|
|
message << ", expected " << expected;
|
|
|
|
|
}
|
2015-04-11 03:10:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return message.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-11 04:15:14 +00:00
|
|
|
|
ASTPtr tryParseQuery(
|
|
|
|
|
IParser & parser,
|
2015-04-14 20:46:34 +00:00
|
|
|
|
IParser::Pos & pos,
|
2015-04-11 04:15:14 +00:00
|
|
|
|
IParser::Pos end,
|
|
|
|
|
std::string & out_error_message,
|
|
|
|
|
bool hilite,
|
|
|
|
|
const std::string & description)
|
2015-04-11 03:10:23 +00:00
|
|
|
|
{
|
2015-04-14 20:46:34 +00:00
|
|
|
|
if (pos == end || *pos == ';')
|
2015-04-11 03:10:23 +00:00
|
|
|
|
{
|
|
|
|
|
out_error_message = "Empty query";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Expected expected = "";
|
2015-04-14 20:46:34 +00:00
|
|
|
|
IParser::Pos begin = pos;
|
2015-04-11 03:10:23 +00:00
|
|
|
|
IParser::Pos max_parsed_pos = pos;
|
|
|
|
|
|
|
|
|
|
ASTPtr res;
|
|
|
|
|
bool parse_res = parser.parse(pos, end, res, max_parsed_pos, expected);
|
|
|
|
|
|
|
|
|
|
/// Распарсенный запрос должен заканчиваться на конец входных данных или на точку с запятой.
|
|
|
|
|
if (!parse_res || (pos != end && *pos != ';'))
|
|
|
|
|
{
|
2015-04-11 04:15:14 +00:00
|
|
|
|
out_error_message = getSyntaxErrorMessage(begin, end, max_parsed_pos, expected, hilite, description);
|
2015-04-11 03:10:23 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-04-14 20:46:34 +00:00
|
|
|
|
ASTPtr parseQueryAndMovePosition(
|
2015-04-11 04:15:14 +00:00
|
|
|
|
IParser & parser,
|
2015-04-14 20:46:34 +00:00
|
|
|
|
IParser::Pos & pos,
|
2015-04-11 04:15:14 +00:00
|
|
|
|
IParser::Pos end,
|
|
|
|
|
const std::string & description)
|
2015-04-11 03:10:23 +00:00
|
|
|
|
{
|
|
|
|
|
std::string error_message;
|
2015-04-14 20:46:34 +00:00
|
|
|
|
ASTPtr res = tryParseQuery(parser, pos, end, error_message, false, description);
|
2015-04-11 03:10:23 +00:00
|
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
throw Exception(error_message, ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-14 20:46:34 +00:00
|
|
|
|
|
|
|
|
|
ASTPtr parseQuery(
|
|
|
|
|
IParser & parser,
|
|
|
|
|
IParser::Pos begin,
|
|
|
|
|
IParser::Pos end,
|
|
|
|
|
const std::string & description)
|
|
|
|
|
{
|
|
|
|
|
auto pos = begin;
|
|
|
|
|
return parseQueryAndMovePosition(parser, pos, end, description);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
|
}
|