2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTDropQuery.h>
|
2011-11-05 23:31:19 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/CommonParsers.h>
|
|
|
|
#include <Parsers/ParserDropQuery.h>
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-03-18 00:57:00 +00:00
|
|
|
namespace
|
2018-05-05 16:14:06 +00:00
|
|
|
{
|
2018-04-21 00:35:20 +00:00
|
|
|
|
2020-07-22 05:00:02 +00:00
|
|
|
bool parseDropQuery(IParser::Pos & pos, ASTPtr & node, Expected & expected, bool optional_table_keyword = false)
|
2018-04-21 00:35:20 +00:00
|
|
|
{
|
2018-01-27 16:17:05 +00:00
|
|
|
ParserKeyword s_temporary("TEMPORARY");
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_table("TABLE");
|
2019-10-08 11:10:29 +00:00
|
|
|
ParserKeyword s_dictionary("DICTIONARY");
|
2020-03-23 22:28:30 +00:00
|
|
|
ParserKeyword s_view("VIEW");
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_database("DATABASE");
|
2017-07-12 20:58:54 +00:00
|
|
|
ParserToken s_dot(TokenType::Dot);
|
2017-06-18 03:07:03 +00:00
|
|
|
ParserKeyword s_if_exists("IF EXISTS");
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserIdentifier name_p;
|
2020-03-20 00:07:52 +00:00
|
|
|
ParserKeyword s_no_delay("NO DELAY");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ASTPtr database;
|
|
|
|
ASTPtr table;
|
2017-04-21 12:39:28 +00:00
|
|
|
String cluster_str;
|
2017-04-01 07:20:54 +00:00
|
|
|
bool if_exists = false;
|
2018-01-27 16:17:05 +00:00
|
|
|
bool temporary = false;
|
2019-10-08 11:10:29 +00:00
|
|
|
bool is_dictionary = false;
|
2020-03-23 22:28:30 +00:00
|
|
|
bool is_view = false;
|
2020-03-20 00:07:52 +00:00
|
|
|
bool no_delay = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_database.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_if_exists.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
if_exists = true;
|
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!name_p.parse(pos, database, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2017-05-30 11:49:17 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword{"ON"}.ignore(pos, expected))
|
2017-05-30 11:49:17 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
|
2017-05-30 11:49:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-23 22:28:30 +00:00
|
|
|
if (s_view.ignore(pos, expected))
|
|
|
|
is_view = true;
|
|
|
|
else if (s_dictionary.ignore(pos, expected))
|
|
|
|
is_dictionary = true;
|
|
|
|
else if (s_temporary.ignore(pos, expected))
|
2018-01-27 16:17:05 +00:00
|
|
|
temporary = true;
|
|
|
|
|
2020-07-22 06:00:45 +00:00
|
|
|
if (!is_view && !is_dictionary && (!s_table.ignore(pos, expected) && !optional_table_keyword))
|
2019-10-08 11:10:29 +00:00
|
|
|
{
|
2020-03-23 22:28:30 +00:00
|
|
|
return false;
|
2019-10-08 11:10:29 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (s_if_exists.ignore(pos, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
if_exists = true;
|
|
|
|
|
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-13 16:12:56 +00:00
|
|
|
|
2017-07-10 03:28:12 +00:00
|
|
|
if (ParserKeyword{"ON"}.ignore(pos, expected))
|
2017-04-13 16:12:56 +00:00
|
|
|
{
|
2017-07-10 03:28:12 +00:00
|
|
|
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
|
2017-04-13 16:12:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-20 00:07:52 +00:00
|
|
|
|
|
|
|
if (s_no_delay.ignore(pos, expected))
|
|
|
|
no_delay = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 03:37:08 +00:00
|
|
|
auto query = std::make_shared<ASTDropQuery>();
|
2017-04-01 07:20:54 +00:00
|
|
|
node = query;
|
|
|
|
|
2018-04-21 00:35:20 +00:00
|
|
|
query->kind = ASTDropQuery::Kind::Drop;
|
2017-04-01 07:20:54 +00:00
|
|
|
query->if_exists = if_exists;
|
2018-01-27 16:17:05 +00:00
|
|
|
query->temporary = temporary;
|
2019-10-08 11:10:29 +00:00
|
|
|
query->is_dictionary = is_dictionary;
|
2020-03-23 22:28:30 +00:00
|
|
|
query->is_view = is_view;
|
2020-03-20 00:07:52 +00:00
|
|
|
query->no_delay = no_delay;
|
2019-01-14 18:15:04 +00:00
|
|
|
|
2019-08-08 20:02:30 +00:00
|
|
|
tryGetIdentifierNameInto(database, query->database);
|
|
|
|
tryGetIdentifierNameInto(table, query->table);
|
2019-01-14 18:15:04 +00:00
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
query->cluster = cluster_str;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return true;
|
2011-11-05 23:31:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 00:57:00 +00:00
|
|
|
bool parseDetachQuery(IParser::Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
if (parseDropQuery(pos, node, expected))
|
|
|
|
{
|
|
|
|
auto * drop_query = node->as<ASTDropQuery>();
|
|
|
|
drop_query->kind = ASTDropQuery::Kind::Detach;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseTruncateQuery(IParser::Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
2020-07-22 05:00:02 +00:00
|
|
|
if (parseDropQuery(pos, node, expected, true))
|
2020-03-18 00:57:00 +00:00
|
|
|
{
|
|
|
|
auto * drop_query = node->as<ASTDropQuery>();
|
|
|
|
drop_query->kind = ASTDropQuery::Kind::Truncate;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParserDropQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
|
|
|
|
{
|
|
|
|
ParserKeyword s_drop("DROP");
|
|
|
|
ParserKeyword s_detach("DETACH");
|
|
|
|
ParserKeyword s_truncate("TRUNCATE");
|
|
|
|
|
|
|
|
if (s_drop.ignore(pos, expected))
|
|
|
|
return parseDropQuery(pos, node, expected);
|
|
|
|
else if (s_detach.ignore(pos, expected))
|
|
|
|
return parseDetachQuery(pos, node, expected);
|
|
|
|
else if (s_truncate.ignore(pos, expected))
|
|
|
|
return parseTruncateQuery(pos, node, expected);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
}
|