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>
|
2021-08-13 16:30:28 +00:00
|
|
|
#include "Parsers/IAST_fwd.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
|
|
|
{
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Create parsers
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_insert_into("INSERT INTO");
|
2021-08-13 16:30:28 +00:00
|
|
|
ParserKeyword s_from_infile("FROM INFILE");
|
2021-09-10 13:59:22 +00:00
|
|
|
ParserKeyword s_compression("COMPRESSION");
|
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");
|
2021-04-14 00:10:14 +00:00
|
|
|
ParserKeyword s_partition_by("PARTITION BY");
|
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};
|
2021-08-13 16:30:28 +00:00
|
|
|
ParserStringLiteral infile_name_p;
|
2021-04-14 00:10:14 +00:00
|
|
|
ParserExpressionWithOptionalAlias exp_elem_p(false);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// create ASTPtr variables (result of parsing will be put in them).
|
2021-09-10 16:58:15 +00:00
|
|
|
/// They will be used to initialize ASTInsertQuery's fields.
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTPtr database;
|
|
|
|
ASTPtr table;
|
2021-08-13 16:30:28 +00:00
|
|
|
ASTPtr infile;
|
2017-04-01 07:20:54 +00:00
|
|
|
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;
|
2021-04-14 00:10:14 +00:00
|
|
|
ASTPtr partition_by_expr;
|
2021-09-10 13:59:22 +00:00
|
|
|
ASTPtr compression;
|
2021-04-14 00:10:14 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Insertion data
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * data = nullptr;
|
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Check for key words `INSERT INTO`. If it isn't found, the query can't be parsed as insert query.
|
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;
|
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// try to find 'TABLE'
|
2017-12-01 13:23:10 +00:00
|
|
|
s_table.ignore(pos, expected);
|
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Search for 'FUNCTION'. If this key word is in query, read fields for insertion into 'TABLE FUNCTION'.
|
|
|
|
/// Word table is optional for table functions. (for example, s3 table function)
|
|
|
|
/// Otherwise fill 'TABLE' fields.
|
2017-12-01 13:23:10 +00:00
|
|
|
if (s_function.ignore(pos, expected))
|
2017-11-02 14:01:11 +00:00
|
|
|
{
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Read function name
|
2017-11-03 15:14:19 +00:00
|
|
|
if (!table_function_p.parse(pos, table_function, expected))
|
|
|
|
return false;
|
2021-04-14 00:10:14 +00:00
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Support insertion values with partition by.
|
2021-04-14 00:10:14 +00:00
|
|
|
if (s_partition_by.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
if (!exp_elem_p.parse(pos, partition_by_expr, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-02 14:01:11 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Read one word. It can be table or database name.
|
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
|
|
|
|
2021-09-10 16:58:15 +00:00
|
|
|
/// If there is a dot, previous name was database name,
|
2021-09-10 13:59:22 +00:00
|
|
|
/// so read table name after dot.
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Check if file is a source of data.
|
2021-08-16 13:28:39 +00:00
|
|
|
if (s_from_infile.ignore(pos, expected))
|
|
|
|
{
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Read its name to process it later
|
2021-08-16 13:28:39 +00:00
|
|
|
if (!infile_name_p.parse(pos, infile, expected))
|
|
|
|
return false;
|
2021-09-10 13:59:22 +00:00
|
|
|
|
|
|
|
/// Check for 'COMPRESSION' parameter (optional)
|
|
|
|
if (s_compression.ignore(pos, expected))
|
|
|
|
{
|
|
|
|
/// Read compression name. Create parser for this purpose.
|
|
|
|
ParserStringLiteral compression_p;
|
|
|
|
if (!compression_p.parse(pos, compression, expected))
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-16 13:28:39 +00:00
|
|
|
}
|
2021-08-14 11:15:32 +00:00
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
Pos before_values = pos;
|
|
|
|
|
2021-08-13 16:30:28 +00:00
|
|
|
/// VALUES or FROM INFILE or FORMAT or SELECT
|
2021-08-16 13:28:39 +00:00
|
|
|
if (!infile && s_values.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2021-09-10 13:59:22 +00:00
|
|
|
/// If VALUES is defined in query, everything except setting will be parsed as data
|
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
|
|
|
{
|
2021-09-10 13:59:22 +00:00
|
|
|
/// If FORMAT is defined, read format name
|
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))
|
|
|
|
{
|
2021-09-10 13:59:22 +00:00
|
|
|
/// If SELECT is defined, return to position before select and parse
|
2021-09-10 16:58:15 +00:00
|
|
|
/// rest of query as SELECT query.
|
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))
|
|
|
|
{
|
2021-09-10 16:58:15 +00:00
|
|
|
/// If WATCH is defined, return to position before WATCH and parse
|
|
|
|
/// rest of query as WATCH query.
|
2020-04-25 11:33:47 +00:00
|
|
|
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
|
|
|
|
{
|
2021-09-10 13:59:22 +00:00
|
|
|
/// If all previous conditions were false, query is incorrect
|
2019-02-19 19:26:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Read SETTINGS if they are defined
|
2019-02-19 19:26:41 +00:00
|
|
|
if (s_settings.ignore(pos, expected))
|
|
|
|
{
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Settings are written like SET query, so parse them with ParserSetQuery
|
2019-02-19 19:26:41 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// In case of defined format, data follows it.
|
2021-08-16 13:28:39 +00:00
|
|
|
if (format && !infile)
|
2019-02-19 19:26:41 +00:00
|
|
|
{
|
|
|
|
Pos last_token = pos;
|
|
|
|
--last_token;
|
|
|
|
data = last_token->end;
|
2017-07-12 01:49:20 +00:00
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// If format name is followed by ';' (end of query symbol) there is no data to insert.
|
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
|
|
|
|
2021-09-10 13:59:22 +00:00
|
|
|
/// Create query and fill its fields.
|
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
|
|
|
|
2021-08-13 16:30:28 +00:00
|
|
|
if (infile)
|
2021-09-10 13:59:22 +00:00
|
|
|
{
|
2021-08-13 16:30:28 +00:00
|
|
|
query->infile = infile;
|
2021-09-10 13:59:22 +00:00
|
|
|
if (compression)
|
|
|
|
query->compression = compression;
|
|
|
|
}
|
2021-08-13 16:30:28 +00:00
|
|
|
|
2017-11-02 14:01:11 +00:00
|
|
|
if (table_function)
|
|
|
|
{
|
|
|
|
query->table_function = table_function;
|
2021-04-14 08:00:17 +00:00
|
|
|
query->partition_by = partition_by_expr;
|
2017-11-02 14:01:11 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
}
|