2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ASTInsertQuery.h>
|
|
|
|
|
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/ParserSelectQuery.h>
|
|
|
|
#include <Parsers/ParserInsertQuery.h>
|
|
|
|
|
|
|
|
#include <Common/typeid_cast.h>
|
2016-11-20 12:43:20 +00:00
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
bool ParserInsertQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
2011-10-30 05:19:41 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos begin = pos;
|
|
|
|
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_insert_into("INSERT INTO");
|
2017-07-12 20:58:54 +00:00
|
|
|
ParserToken s_dot(TokenType::Dot);
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_values("VALUES");
|
|
|
|
ParserKeyword s_format("FORMAT");
|
|
|
|
ParserKeyword s_select("SELECT");
|
2017-07-12 20:58:54 +00:00
|
|
|
ParserToken s_lparen(TokenType::OpeningRoundBracket);
|
|
|
|
ParserToken s_rparen(TokenType::ClosingRoundBracket);
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserIdentifier name_p;
|
2017-07-10 03:28:12 +00:00
|
|
|
ParserList columns_p(std::make_unique<ParserCompoundIdentifier>(), std::make_unique<ParserToken>(TokenType::Comma), false);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ASTPtr database;
|
|
|
|
ASTPtr table;
|
|
|
|
ASTPtr columns;
|
|
|
|
ASTPtr format;
|
|
|
|
ASTPtr select;
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Insertion data
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * data = nullptr;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_insert_into.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, table, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_dot.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
database = table;
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, table, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Is there a list of columns
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_lparen.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!columns_p.parse(pos, columns, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!s_rparen.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pos before_select = pos;
|
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// VALUES or FORMAT or SELECT
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_values.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-12 01:49:20 +00:00
|
|
|
data = pos->begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (s_format.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-08-01 17:02:41 +00:00
|
|
|
auto name_pos = pos;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, format, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (pos->type == TokenType::Semicolon)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("You have excessive ';' symbol before data for INSERT.\n"
|
|
|
|
"Example:\n\n"
|
|
|
|
"INSERT INTO t (x, y) FORMAT TabSeparated\n"
|
|
|
|
"1\tHello\n"
|
|
|
|
"2\tWorld\n"
|
|
|
|
"\n"
|
|
|
|
"Note that there is no ';' in first line.", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
2017-07-12 01:49:20 +00:00
|
|
|
/// Data starts after the first newline, if there is one, or after all the whitespace characters, otherwise.
|
2017-08-01 17:02:41 +00:00
|
|
|
|
|
|
|
data = name_pos->end;
|
2017-07-12 01:49:20 +00:00
|
|
|
|
|
|
|
while (data < end && (*data == ' ' || *data == '\t' || *data == '\f'))
|
|
|
|
++data;
|
|
|
|
|
|
|
|
if (data < end && *data == '\r')
|
|
|
|
++data;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-12 01:49:20 +00:00
|
|
|
if (data < end && *data == '\n')
|
|
|
|
++data;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-07-10 03:28:12 +00:00
|
|
|
else if (s_select.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
pos = before_select;
|
|
|
|
ParserSelectQuery select_p;
|
2017-07-10 03:28:12 +00:00
|
|
|
select_p.parse(pos, select, expected);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-31 06:37:12 +00:00
|
|
|
|
2017-07-12 01:49:20 +00:00
|
|
|
auto query = std::make_shared<ASTInsertQuery>(StringRange(begin, pos));
|
2017-04-01 07:20:54 +00:00
|
|
|
node = query;
|
2011-10-30 05:19:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (database)
|
|
|
|
query->database = typeid_cast<ASTIdentifier &>(*database).name;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
query->table = typeid_cast<ASTIdentifier &>(*table).name;
|
2011-10-31 06:37:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (format)
|
|
|
|
query->format = typeid_cast<ASTIdentifier &>(*format).name;
|
|
|
|
|
|
|
|
query->columns = columns;
|
|
|
|
query->select = select;
|
2017-07-12 01:49:20 +00:00
|
|
|
query->data = data != end ? data : nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
query->end = end;
|
2011-10-30 05:19:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (columns)
|
|
|
|
query->children.push_back(columns);
|
|
|
|
if (select)
|
|
|
|
query->children.push_back(select);
|
|
|
|
|
|
|
|
return true;
|
2011-10-30 05:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|