forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtest_alter_command_parser.cpp
264 lines (215 loc) · 14.8 KB
/
gtest_alter_command_parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <gtest/gtest.h>
#include <Parsers/parseQuery.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier_fwd.h>
#include <Parsers/MySQL/ASTAlterCommand.h>
#include <Parsers/MySQL/ASTDeclareOption.h>
using namespace DB;
using namespace DB::MySQLParser;
static inline ASTPtr tryParserQuery(IParser & parser, const String & query) // -V1071
{
return parseQuery(parser, query.data(), query.data() + query.size(), "", 0, 0);
}
TEST(ParserAlterCommand, AddAlterCommand)
{
ParserAlterCommand alter_p;
ASTPtr ast = tryParserQuery(alter_p, "ADD column_name INT");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::ADD_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "column_name");
ast = tryParserQuery(alter_p, "ADD COLUMN column_name INT");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::ADD_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "column_name");
ast = tryParserQuery(alter_p, "ADD (column_name INT)");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::ADD_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "column_name");
ast = tryParserQuery(alter_p, "ADD COLUMN (column_name INT, column_name_1 INT)");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::ADD_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 2);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[1]->as<ASTDeclareColumn>()->name, "column_name_1");
ast = tryParserQuery(alter_p, "ADD INDEX (col_01, col_02(100), col_03 DESC) KEY_BLOCK_SIZE 3");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::ADD_INDEX);
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_decl->index_columns->children.size(), 3);
EXPECT_EQ(getIdentifierName(ast->as<ASTAlterCommand>()->index_decl->index_columns->children[0]), "col_01");
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_decl->index_columns->children[1]->as<ASTFunction>()->name, "col_02");
EXPECT_EQ(getIdentifierName(ast->as<ASTAlterCommand>()->index_decl->index_columns->children[2]), "col_03");
}
TEST(ParserAlterCommand, DropAlterCommand)
{
ParserAlterCommand alter_p;
ASTPtr ast = tryParserQuery(alter_p, "DROP CHECK constraint_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_CHECK);
EXPECT_EQ(ast->as<ASTAlterCommand>()->constraint_name, "constraint_name");
ast = tryParserQuery(alter_p, "DROP CONSTRAINT constraint_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_CHECK);
EXPECT_EQ(ast->as<ASTAlterCommand>()->constraint_name, "constraint_name");
ast = tryParserQuery(alter_p, "DROP KEY index_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_INDEX);
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_type, "KEY");
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_name, "index_name");
ast = tryParserQuery(alter_p, "DROP INDEX index_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_INDEX);
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_type, "KEY");
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_name, "index_name");
ast = tryParserQuery(alter_p, "DROP PRIMARY KEY");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_INDEX);
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_type, "PRIMARY_KEY");
ast = tryParserQuery(alter_p, "DROP FOREIGN KEY fk_symbol");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_INDEX);
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_name, "fk_symbol");
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_type, "FOREIGN");
ast = tryParserQuery(alter_p, "DROP column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "column_name");
ast = tryParserQuery(alter_p, "DROP COLUMN column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "column_name");
}
TEST(ParserAlterCommand, AlterAlterCommand)
{
ParserAlterCommand alter_p;
ASTPtr ast = tryParserQuery(alter_p, "ALTER CHECK constraint_name NOT ENFORCED");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_CHECK);
EXPECT_TRUE(ast->as<ASTAlterCommand>()->not_check_enforced);
EXPECT_EQ(ast->as<ASTAlterCommand>()->constraint_name, "constraint_name");
ast = tryParserQuery(alter_p, "ALTER CHECK constraint_name ENFORCED");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_CHECK);
EXPECT_FALSE(ast->as<ASTAlterCommand>()->not_check_enforced);
EXPECT_EQ(ast->as<ASTAlterCommand>()->constraint_name, "constraint_name");
ast = tryParserQuery(alter_p, "ALTER CONSTRAINT constraint_name NOT ENFORCED");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_CHECK);
EXPECT_TRUE(ast->as<ASTAlterCommand>()->not_check_enforced);
EXPECT_EQ(ast->as<ASTAlterCommand>()->constraint_name, "constraint_name");
ast = tryParserQuery(alter_p, "ALTER CONSTRAINT constraint_name ENFORCED");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_CHECK);
EXPECT_FALSE(ast->as<ASTAlterCommand>()->not_check_enforced);
EXPECT_EQ(ast->as<ASTAlterCommand>()->constraint_name, "constraint_name");
ast = tryParserQuery(alter_p, "ALTER INDEX index_name VISIBLE");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_INDEX_VISIBLE);
EXPECT_TRUE(ast->as<ASTAlterCommand>()->index_visible);
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_name, "index_name");
ast = tryParserQuery(alter_p, "ALTER INDEX index_name INVISIBLE");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_INDEX_VISIBLE);
EXPECT_FALSE(ast->as<ASTAlterCommand>()->index_visible);
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_name, "index_name");
ast = tryParserQuery(alter_p, "ALTER column_name SET DEFAULT other_column");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN_DEFAULT);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "column_name");
EXPECT_EQ(getIdentifierName(ast->as<ASTAlterCommand>()->default_expression), "other_column");
ast = tryParserQuery(alter_p, "ALTER column_name DROP DEFAULT");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_COLUMN_DEFAULT);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "column_name");
ast = tryParserQuery(alter_p, "ALTER COLUMN column_name SET DEFAULT other_column");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN_DEFAULT);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "column_name");
EXPECT_EQ(getIdentifierName(ast->as<ASTAlterCommand>()->default_expression), "other_column");
ast = tryParserQuery(alter_p, "ALTER COLUMN column_name DROP DEFAULT");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::DROP_COLUMN_DEFAULT);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "column_name");
}
TEST(ParserAlterCommand, RenameAlterCommand)
{
ParserAlterCommand alter_p;
ASTPtr ast = tryParserQuery(alter_p, "RENAME COLUMN old_column_name TO new_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::RENAME_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "old_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "new_column_name");
ast = tryParserQuery(alter_p, "RENAME KEY old_index_name TO new_index_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::RENAME_INDEX);
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "old_index_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_name, "new_index_name");
ast = tryParserQuery(alter_p, "RENAME INDEX old_index_name TO new_index_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::RENAME_INDEX);
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "old_index_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->index_name, "new_index_name");
ast = tryParserQuery(alter_p, "RENAME TO new_table_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::RENAME_TABLE);
EXPECT_EQ(ast->as<ASTAlterCommand>()->new_table_name, "new_table_name");
ast = tryParserQuery(alter_p, "RENAME TO new_database_name.new_table_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::RENAME_TABLE);
EXPECT_EQ(ast->as<ASTAlterCommand>()->new_table_name, "new_table_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->new_database_name, "new_database_name");
}
TEST(ParserAlterCommand, ModifyAlterCommand)
{
ParserAlterCommand alter_p;
ASTPtr ast = tryParserQuery(alter_p, "MODIFY column_name INT");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "");
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "");
EXPECT_FALSE(ast->as<ASTAlterCommand>()->first);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "column_name");
ast = tryParserQuery(alter_p, "MODIFY column_name INT FIRST");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "");
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "");
EXPECT_TRUE(ast->as<ASTAlterCommand>()->first);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "column_name");
ast = tryParserQuery(alter_p, "MODIFY column_name INT AFTER other_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "");
EXPECT_FALSE(ast->as<ASTAlterCommand>()->first);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "other_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "column_name");
ast = tryParserQuery(alter_p, "MODIFY COLUMN column_name INT AFTER other_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN);
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "");
EXPECT_FALSE(ast->as<ASTAlterCommand>()->first);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "other_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "column_name");
}
TEST(ParserAlterCommand, ChangeAlterCommand)
{
ParserAlterCommand alter_p;
ASTPtr ast = tryParserQuery(alter_p, "CHANGE old_column_name new_column_name INT");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN);
EXPECT_FALSE(ast->as<ASTAlterCommand>()->first);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "");
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "old_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "new_column_name");
ast = tryParserQuery(alter_p, "CHANGE old_column_name new_column_name INT FIRST");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN);
EXPECT_TRUE(ast->as<ASTAlterCommand>()->first);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "");
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "old_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "new_column_name");
ast = tryParserQuery(alter_p, "CHANGE old_column_name new_column_name INT AFTER other_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN);
EXPECT_FALSE(ast->as<ASTAlterCommand>()->first);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "other_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "old_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "new_column_name");
ast = tryParserQuery(alter_p, "CHANGE COLUMN old_column_name new_column_name INT AFTER other_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_COLUMN);
EXPECT_FALSE(ast->as<ASTAlterCommand>()->first);
EXPECT_EQ(ast->as<ASTAlterCommand>()->column_name, "other_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->old_name, "old_column_name");
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children.size(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->additional_columns->children[0]->as<ASTDeclareColumn>()->name, "new_column_name");
}
TEST(ParserAlterCommand, AlterOptionsCommand)
{
ParserAlterCommand alter_p;
ASTPtr ast = tryParserQuery(alter_p, "ALGORITHM DEFAULT");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_PROPERTIES);
EXPECT_EQ(ast->as<ASTAlterCommand>()->properties->as<ASTDeclareOptions>()->changes.size(), 1);
EXPECT_EQ(getIdentifierName(ast->as<ASTAlterCommand>()->properties->as<ASTDeclareOptions>()->changes["algorithm"]), "DEFAULT");
ast = tryParserQuery(alter_p, "AUTO_INCREMENT 1 CHECKSUM 1");
EXPECT_EQ(ast->as<ASTAlterCommand>()->type, ASTAlterCommand::MODIFY_PROPERTIES);
EXPECT_EQ(ast->as<ASTAlterCommand>()->properties->as<ASTDeclareOptions>()->changes.size(), 2);
EXPECT_EQ(ast->as<ASTAlterCommand>()->properties->as<ASTDeclareOptions>()->changes["checksum"]->as<ASTLiteral>()->value.safeGet<UInt64>(), 1);
EXPECT_EQ(ast->as<ASTAlterCommand>()->properties->as<ASTDeclareOptions>()->changes["auto_increment"]->as<ASTLiteral>()->value.safeGet<UInt64>(), 1);
EXPECT_THROW(tryParserQuery(alter_p, "FORCE ALGORITHM DEFAULT"), Exception);
EXPECT_THROW(tryParserQuery(alter_p, "ALGORITHM DEFAULT AUTO_INCREMENT 1"), Exception);
}