1#include <Parsers/ASTIdentifier.h>
2#include <Parsers/ASTRenameQuery.h>
3
4#include <Parsers/CommonParsers.h>
5#include <Parsers/ParserRenameQuery.h>
6
7
8namespace DB
9{
10
11
12/// Parse database.table or table.
13static bool parseDatabaseAndTable(
14 ASTRenameQuery::Table & db_and_table, IParser::Pos & pos, Expected & expected)
15{
16 ParserIdentifier name_p;
17 ParserToken s_dot(TokenType::Dot);
18
19 ASTPtr database;
20 ASTPtr table;
21
22 if (!name_p.parse(pos, table, expected))
23 return false;
24
25 if (s_dot.ignore(pos, expected))
26 {
27 database = table;
28 if (!name_p.parse(pos, table, expected))
29 return false;
30 }
31
32 db_and_table.database.clear();
33 tryGetIdentifierNameInto(database, db_and_table.database);
34 tryGetIdentifierNameInto(table, db_and_table.table);
35
36 return true;
37}
38
39
40bool ParserRenameQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
41{
42 ParserKeyword s_rename_table("RENAME TABLE");
43 ParserKeyword s_to("TO");
44 ParserToken s_comma(TokenType::Comma);
45
46 if (!s_rename_table.ignore(pos, expected))
47 return false;
48
49 ASTRenameQuery::Elements elements;
50
51 while (true)
52 {
53 if (!elements.empty() && !s_comma.ignore(pos))
54 break;
55
56 elements.push_back(ASTRenameQuery::Element());
57
58 if (!parseDatabaseAndTable(elements.back().from, pos, expected)
59 || !s_to.ignore(pos)
60 || !parseDatabaseAndTable(elements.back().to, pos, expected))
61 return false;
62 }
63
64 String cluster_str;
65 if (ParserKeyword{"ON"}.ignore(pos, expected))
66 {
67 if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
68 return false;
69 }
70
71 auto query = std::make_shared<ASTRenameQuery>();
72 query->cluster = cluster_str;
73 node = query;
74
75 query->elements = elements;
76 return true;
77}
78
79
80}
81