ClickHouse/src/Parsers/ParserInsertQuery.cpp

290 lines
9.2 KiB
C++
Raw Normal View History

#include <Parsers/ASTIdentifier_fwd.h>
#include <Parsers/ASTInsertQuery.h>
#include <Parsers/ASTSelectWithUnionQuery.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>
#include <Parsers/ParserWatchQuery.h>
#include <Parsers/ParserInsertQuery.h>
#include <Parsers/ParserSetQuery.h>
#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"
2011-10-30 05:19:41 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int SYNTAX_ERROR;
}
2011-10-30 05:19:41 +00:00
bool ParserInsertQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
2011-10-30 05:19:41 +00:00
{
/// Create parsers
ParserKeyword s_insert_into("INSERT INTO");
2021-08-13 16:30:28 +00:00
ParserKeyword s_from_infile("FROM INFILE");
ParserKeyword s_compression("COMPRESSION");
ParserKeyword s_table("TABLE");
ParserKeyword s_function("FUNCTION");
ParserToken s_dot(TokenType::Dot);
ParserKeyword s_values("VALUES");
ParserKeyword s_format("FORMAT");
ParserKeyword s_settings("SETTINGS");
ParserKeyword s_select("SELECT");
ParserKeyword s_watch("WATCH");
2021-04-14 00:10:14 +00:00
ParserKeyword s_partition_by("PARTITION BY");
ParserKeyword s_with("WITH");
ParserToken s_lparen(TokenType::OpeningRoundBracket);
ParserToken s_rparen(TokenType::ClosingRoundBracket);
2021-10-12 23:51:11 +00:00
ParserIdentifier name_p(true);
2020-09-03 17:51:16 +00:00
ParserList columns_p(std::make_unique<ParserInsertElement>(), std::make_unique<ParserToken>(TokenType::Comma), false);
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);
/// 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.
ASTPtr database;
ASTPtr table;
2021-08-13 16:30:28 +00:00
ASTPtr infile;
ASTPtr columns;
ASTPtr format;
ASTPtr select;
ASTPtr watch;
ASTPtr table_function;
ASTPtr settings_ast;
2021-04-14 00:10:14 +00:00
ASTPtr partition_by_expr;
ASTPtr compression;
2021-04-14 00:10:14 +00:00
2017-04-02 17:37:49 +00:00
/// Insertion data
const char * data = nullptr;
/// Check for key words `INSERT INTO`. If it isn't found, the query can't be parsed as insert query.
if (!s_insert_into.ignore(pos, expected))
return false;
/// try to find 'TABLE'
s_table.ignore(pos, expected);
/// 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.
if (s_function.ignore(pos, expected))
{
/// Read function name
if (!table_function_p.parse(pos, table_function, expected))
return false;
2021-04-14 00:10:14 +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;
}
}
else
{
/// Read one word. It can be table or database name.
if (!name_p.parse(pos, table, expected))
return false;
2021-09-10 16:58:15 +00:00
/// If there is a dot, previous name was database name,
/// so read table name after dot.
if (s_dot.ignore(pos, expected))
{
database = table;
if (!name_p.parse(pos, table, expected))
return false;
}
}
2017-04-02 17:37:49 +00:00
/// Is there a list of columns
if (s_lparen.ignore(pos, expected))
{
if (!columns_p.parse(pos, columns, expected))
return false;
if (!s_rparen.ignore(pos, expected))
return false;
}
Pos before_values = pos;
String format_str;
/// VALUES or FROM INFILE or FORMAT or SELECT
2021-08-16 13:28:39 +00:00
if (s_from_infile.ignore(pos, expected))
{
/// Read file name to process it later
2021-08-16 13:28:39 +00:00
if (!infile_name_p.parse(pos, infile, expected))
return false;
/// 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-14 11:15:32 +00:00
/// Read format name
if (!s_format.ignore(pos, expected) || !name_p.parse(pos, format, expected))
return false;
tryGetIdentifierNameInto(format, format_str);
}
else if (s_values.ignore(pos, expected))
{
/// If VALUES is defined in query, everything except setting will be parsed as data
data = pos->begin;
2021-10-20 14:17:20 +00:00
format_str = "Values";
}
else if (s_format.ignore(pos, expected))
{
/// If FORMAT is defined, read format name
if (!name_p.parse(pos, format, expected))
return false;
2021-10-20 14:17:20 +00:00
tryGetIdentifierNameInto(format, format_str);
}
else if (s_select.ignore(pos, expected) || s_with.ignore(pos,expected))
{
/// 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.
pos = before_values;
ParserSelectWithUnionQuery select_p;
select_p.parse(pos, select, expected);
2019-05-28 18:30:10 +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;
2021-10-20 14:17:20 +00:00
tryGetIdentifierNameInto(format, format_str);
}
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.
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;
}
else if (infile) {} /// Support syntax INSERT INTO ... FROM INFILE ... without FORMAT statement
else
{
/// If all previous conditions were false, query is incorrect
return false;
}
/// Read SETTINGS if they are defined
if (s_settings.ignore(pos, expected))
{
/// Settings are written like SET query, so parse them with ParserSetQuery
ParserSetQuery parser_settings(true);
if (!parser_settings.parse(pos, settings_ast, expected))
return false;
}
if (select)
{
/// Copy SETTINGS from the INSERT ... SELECT ... SETTINGS
InsertQuerySettingsPushDownVisitor::Data visitor_data{settings_ast};
InsertQuerySettingsPushDownVisitor(visitor_data).visit(select);
}
/// In case of defined format, data follows it.
2021-08-16 13:28:39 +00:00
if (format && !infile)
{
Pos last_token = pos;
--last_token;
data = last_token->end;
/// If format name is followed by ';' (end of query symbol) there is no data to insert.
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);
while (data < end && (*data == ' ' || *data == '\t' || *data == '\f'))
++data;
/// Data starts after the first newline, if there is one, or after all the whitespace characters, otherwise.
if (data < end && *data == '\r')
++data;
if (data < end && *data == '\n')
++data;
}
2011-10-31 06:37:12 +00:00
/// Create query and fill its fields.
2018-02-26 03:37:08 +00:00
auto query = std::make_shared<ASTInsertQuery>();
node = query;
2011-10-30 05:19:41 +00:00
2021-08-13 16:30:28 +00:00
if (infile)
{
2021-08-13 16:30:28 +00:00
query->infile = infile;
if (compression)
query->compression = compression;
}
2021-08-13 16:30:28 +00:00
if (table_function)
{
query->table_function = table_function;
2021-04-14 08:00:17 +00:00
query->partition_by = partition_by_expr;
}
else
{
2021-10-12 23:51:11 +00:00
query->database = database;
query->table = table;
2021-10-13 07:00:00 +00:00
if (database)
query->children.push_back(database);
if (table)
query->children.push_back(table);
}
2011-10-31 06:37:12 +00:00
query->columns = columns;
2021-10-20 14:17:20 +00:00
query->format = std::move(format_str);
query->select = select;
query->watch = watch;
query->settings_ast = settings_ast;
query->data = data != end ? data : nullptr;
query->end = end;
2011-10-30 05:19:41 +00:00
if (columns)
query->children.push_back(columns);
if (select)
query->children.push_back(select);
if (watch)
query->children.push_back(watch);
if (settings_ast)
query->children.push_back(settings_ast);
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
}