2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2018-02-25 06:34:20 +00:00
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTInsertQuery.h>
|
|
|
|
|
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
2018-02-25 06:34:20 +00:00
|
|
|
#include <Parsers/ParserSelectWithUnionQuery.h>
|
2020-04-25 11:33:47 +00:00
|
|
|
#include <Parsers/ParserWatchQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ParserInsertQuery.h>
|
2019-02-19 19:26:41 +00:00
|
|
|
#include <Parsers/ParserSetQuery.h>
|
2020-11-25 18:32:52 +00:00
|
|
|
#include <Parsers/InsertQuerySettingsPushDownVisitor.h>
|
2019-05-28 18:30:10 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +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-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_insert_into("INSERT INTO");
|
2017-11-03 15:14:19 +00:00
|
|
|
ParserKeyword s_table("TABLE");
|
|
|
|
ParserKeyword s_function("FUNCTION");
|
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");
|
2019-02-19 19:26:41 +00:00
|
|
|
ParserKeyword s_settings("SETTINGS");
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_select("SELECT");
|
2020-04-25 11:33:47 +00:00
|
|
|
ParserKeyword s_watch("WATCH");
|
2017-12-24 09:24:10 +00:00
|
|
|
ParserKeyword s_with("WITH");
|
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;
|
2020-09-03 17:51:16 +00:00
|
|
|
ParserList columns_p(std::make_unique<ParserInsertElement>(), std::make_unique<ParserToken>(TokenType::Comma), false);
|
2019-11-18 16:49:23 +00:00
|
|
|
ParserFunction table_function_p{false};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ASTPtr database;
|
|
|
|
ASTPtr table;
|
|
|
|
ASTPtr columns;
|
|
|
|
ASTPtr format;
|
|
|
|
ASTPtr select;
|
2020-04-25 11:33:47 +00:00
|
|
|
ASTPtr watch;
|
2017-11-02 14:01:11 +00:00
|
|
|
ASTPtr table_function;
|
2019-02-19 19:26:41 +00:00
|
|
|
ASTPtr settings_ast;
|
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-12-01 13:23:10 +00:00
|
|
|
s_table.ignore(pos, expected);
|
|
|
|
|
|
|
|
if (s_function.ignore(pos, expected))
|
2017-11-02 14:01:11 +00:00
|
|
|
{
|
2017-11-03 15:14:19 +00:00
|
|
|
if (!table_function_p.parse(pos, table_function, expected))
|
|
|
|
return false;
|
2017-11-02 14:01:11 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
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-11-02 14:01:11 +00:00
|
|
|
|
|
|
|
if (s_dot.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
database = table;
|
|
|
|
if (!name_p.parse(pos, table, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-25 11:33:47 +00:00
|
|
|
Pos before_values = pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, format, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2019-02-19 19:26:41 +00:00
|
|
|
}
|
|
|
|
else if (s_select.ignore(pos, expected) || s_with.ignore(pos,expected))
|
|
|
|
{
|
2020-04-25 11:33:47 +00:00
|
|
|
pos = before_values;
|
2019-02-19 19:26:41 +00:00
|
|
|
ParserSelectWithUnionQuery select_p;
|
|
|
|
select_p.parse(pos, select, expected);
|
2019-05-28 18:30:10 +00:00
|
|
|
|
2019-05-30 20:12:44 +00:00
|
|
|
/// FORMAT section is expected if we have input() in SELECT part
|
|
|
|
if (s_format.ignore(pos, expected) && !name_p.parse(pos, format, expected))
|
|
|
|
return false;
|
2019-02-19 19:26:41 +00:00
|
|
|
}
|
2020-04-25 11:33:47 +00:00
|
|
|
else if (s_watch.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
pos = before_values;
|
|
|
|
ParserWatchQuery watch_p;
|
|
|
|
watch_p.parse(pos, watch, expected);
|
|
|
|
|
|
|
|
/// FORMAT section is expected if we have input() in SELECT part
|
|
|
|
if (s_format.ignore(pos, expected) && !name_p.parse(pos, format, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-19 19:26:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-02-19 19:26:41 +00:00
|
|
|
if (s_settings.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
ParserSetQuery parser_settings(true);
|
|
|
|
if (!parser_settings.parse(pos, settings_ast, expected))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:32:52 +00:00
|
|
|
if (select)
|
|
|
|
{
|
|
|
|
/// Copy SETTINGS from the INSERT ... SELECT ... SETTINGS
|
|
|
|
InsertQuerySettingsPushDownVisitor::Data visitor_data{settings_ast};
|
|
|
|
InsertQuerySettingsPushDownVisitor(visitor_data).visit(select);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-19 19:26:41 +00:00
|
|
|
if (format)
|
|
|
|
{
|
|
|
|
Pos last_token = pos;
|
|
|
|
--last_token;
|
|
|
|
data = last_token->end;
|
2017-07-12 01:49:20 +00:00
|
|
|
|
2017-08-22 19:51:03 +00:00
|
|
|
if (data < end && *data == ';')
|
|
|
|
throw Exception("You have excessive ';' symbol before data for INSERT.\n"
|
|
|
|
"Example:\n\n"
|
|
|
|
"INSERT INTO t (x, y) FORMAT TabSeparated\n"
|
|
|
|
";\tHello\n"
|
|
|
|
"2\tWorld\n"
|
|
|
|
"\n"
|
|
|
|
"Note that there is no ';' just after format name, "
|
|
|
|
"you need to put at least one whitespace symbol before the data.", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
2017-07-12 01:49:20 +00:00
|
|
|
while (data < end && (*data == ' ' || *data == '\t' || *data == '\f'))
|
|
|
|
++data;
|
|
|
|
|
2017-08-22 19:51:03 +00:00
|
|
|
/// Data starts after the first newline, if there is one, or after all the whitespace characters, otherwise.
|
|
|
|
|
2017-07-12 01:49:20 +00:00
|
|
|
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
|
|
|
}
|
2011-10-31 06:37:12 +00:00
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto query = std::make_shared<ASTInsertQuery>();
|
2017-04-01 07:20:54 +00:00
|
|
|
node = query;
|
2011-10-30 05:19:41 +00:00
|
|
|
|
2017-11-02 14:01:11 +00:00
|
|
|
if (table_function)
|
|
|
|
{
|
|
|
|
query->table_function = table_function;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-02 20:23:58 +00:00
|
|
|
tryGetIdentifierNameInto(database, query->table_id.database_name);
|
|
|
|
tryGetIdentifierNameInto(table, query->table_id.table_name);
|
2017-11-02 14:01:11 +00:00
|
|
|
}
|
2011-10-31 06:37:12 +00:00
|
|
|
|
2019-08-08 20:02:30 +00:00
|
|
|
tryGetIdentifierNameInto(format, query->format);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
query->columns = columns;
|
|
|
|
query->select = select;
|
2020-04-25 11:33:47 +00:00
|
|
|
query->watch = watch;
|
2019-02-19 19:26:41 +00:00
|
|
|
query->settings_ast = settings_ast;
|
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);
|
2020-04-25 11:33:47 +00:00
|
|
|
if (watch)
|
|
|
|
query->children.push_back(watch);
|
2019-02-19 19:26:41 +00:00
|
|
|
if (settings_ast)
|
|
|
|
query->children.push_back(settings_ast);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return true;
|
2011-10-30 05:19:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 17:51:16 +00:00
|
|
|
bool ParserInsertElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
return ParserColumnsMatcher().parse(pos, node, expected)
|
|
|
|
|| ParserQualifiedAsterisk().parse(pos, node, expected)
|
|
|
|
|| ParserAsterisk().parse(pos, node, expected)
|
|
|
|
|| ParserCompoundIdentifier().parse(pos, node, expected);
|
|
|
|
}
|
2011-10-30 05:19:41 +00:00
|
|
|
|
|
|
|
}
|