1 | #pragma once |
2 | |
3 | #include <Parsers/IAST.h> |
4 | #include <Parsers/ASTQueryWithTableAndOutput.h> |
5 | #include <Parsers/ASTQueryWithOnCluster.h> |
6 | #include <Parsers/ASTTTLElement.h> |
7 | |
8 | |
9 | namespace DB |
10 | { |
11 | |
12 | /** ALTER query: |
13 | * ALTER TABLE [db.]name_type |
14 | * ADD COLUMN col_name type [AFTER col_after], |
15 | * DROP COLUMN col_drop [FROM PARTITION partition], |
16 | * MODIFY COLUMN col_name type, |
17 | * DROP PARTITION partition, |
18 | * COMMENT_COLUMN col_name 'comment', |
19 | * ALTER LIVE VIEW [db.]name_type |
20 | * REFRESH |
21 | */ |
22 | |
23 | class ASTAlterCommand : public IAST |
24 | { |
25 | public: |
26 | enum Type |
27 | { |
28 | ADD_COLUMN, |
29 | DROP_COLUMN, |
30 | MODIFY_COLUMN, |
31 | COMMENT_COLUMN, |
32 | MODIFY_ORDER_BY, |
33 | MODIFY_TTL, |
34 | MODIFY_SETTING, |
35 | |
36 | ADD_INDEX, |
37 | DROP_INDEX, |
38 | MATERIALIZE_INDEX, |
39 | |
40 | ADD_CONSTRAINT, |
41 | DROP_CONSTRAINT, |
42 | |
43 | DROP_PARTITION, |
44 | DROP_DETACHED_PARTITION, |
45 | ATTACH_PARTITION, |
46 | MOVE_PARTITION, |
47 | REPLACE_PARTITION, |
48 | FETCH_PARTITION, |
49 | FREEZE_PARTITION, |
50 | FREEZE_ALL, |
51 | |
52 | DELETE, |
53 | UPDATE, |
54 | |
55 | NO_TYPE, |
56 | |
57 | LIVE_VIEW_REFRESH, |
58 | }; |
59 | |
60 | Type type = NO_TYPE; |
61 | |
62 | /** The ADD COLUMN query stores the name and type of the column to add |
63 | * This field is not used in the DROP query |
64 | * In MODIFY query, the column name and the new type are stored here |
65 | */ |
66 | ASTPtr col_decl; |
67 | |
68 | /** The ADD COLUMN query here optionally stores the name of the column following AFTER |
69 | * The DROP query stores the column name for deletion here |
70 | */ |
71 | ASTPtr column; |
72 | |
73 | /** For MODIFY ORDER BY |
74 | */ |
75 | ASTPtr order_by; |
76 | |
77 | /** The ADD INDEX query stores the IndexDeclaration there. |
78 | */ |
79 | ASTPtr index_decl; |
80 | |
81 | /** The ADD INDEX query stores the name of the index following AFTER. |
82 | * The DROP INDEX query stores the name for deletion. |
83 | * The MATERIALIZE INDEX query stores the name of the index to materialize. |
84 | * The CLEAR INDEX query stores the name of the index to clear. |
85 | */ |
86 | ASTPtr index; |
87 | |
88 | /** The ADD CONSTRAINT query stores the ConstraintDeclaration there. |
89 | */ |
90 | ASTPtr constraint_decl; |
91 | |
92 | /** The DROP CONSTRAINT query stores the name for deletion. |
93 | */ |
94 | ASTPtr constraint; |
95 | |
96 | /** Used in DROP PARTITION and ATTACH PARTITION FROM queries. |
97 | * The value or ID of the partition is stored here. |
98 | */ |
99 | ASTPtr partition; |
100 | |
101 | /// For DELETE/UPDATE WHERE: the predicate that filters the rows to delete/update. |
102 | ASTPtr predicate; |
103 | |
104 | /// A list of expressions of the form `column = expr` for the UPDATE command. |
105 | ASTPtr update_assignments; |
106 | |
107 | /// A column comment |
108 | ASTPtr comment; |
109 | |
110 | /// For MODIFY TTL query |
111 | ASTPtr ttl; |
112 | |
113 | /// FOR MODIFY_SETTING |
114 | ASTPtr settings_changes; |
115 | |
116 | /** In ALTER CHANNEL, ADD, DROP, SUSPEND, RESUME, REFRESH, MODIFY queries, the list of live views is stored here |
117 | */ |
118 | ASTPtr values; |
119 | |
120 | bool detach = false; /// true for DETACH PARTITION |
121 | |
122 | bool part = false; /// true for ATTACH PART, DROP DETACHED PART and MOVE |
123 | |
124 | bool clear_column = false; /// for CLEAR COLUMN (do not drop column from metadata) |
125 | |
126 | bool clear_index = false; /// for CLEAR INDEX (do not drop index from metadata) |
127 | |
128 | bool if_not_exists = false; /// option for ADD_COLUMN |
129 | |
130 | bool if_exists = false; /// option for DROP_COLUMN, MODIFY_COLUMN, COMMENT_COLUMN |
131 | |
132 | PartDestinationType move_destination_type; /// option for MOVE PART/PARTITION |
133 | |
134 | String move_destination_name; /// option for MOVE PART/PARTITION |
135 | |
136 | /** For FETCH PARTITION - the path in ZK to the shard, from which to download the partition. |
137 | */ |
138 | String from; |
139 | |
140 | /** For FREEZE PARTITION - place local backup to directory with specified name. |
141 | */ |
142 | String with_name; |
143 | |
144 | /// REPLACE(ATTACH) PARTITION partition FROM db.table |
145 | String from_database; |
146 | String from_table; |
147 | /// To distinguish REPLACE and ATTACH PARTITION partition FROM db.table |
148 | bool replace = true; |
149 | |
150 | String getID(char delim) const override { return "AlterCommand" + (delim + std::to_string(static_cast<int>(type))); } |
151 | |
152 | ASTPtr clone() const override; |
153 | |
154 | protected: |
155 | void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override; |
156 | }; |
157 | |
158 | class ASTAlterCommandList : public IAST |
159 | { |
160 | public: |
161 | std::vector<ASTAlterCommand *> commands; |
162 | |
163 | void add(const ASTPtr & command) |
164 | { |
165 | commands.push_back(command->as<ASTAlterCommand>()); |
166 | children.push_back(command); |
167 | } |
168 | |
169 | String getID(char) const override { return "AlterCommandList" ; } |
170 | |
171 | ASTPtr clone() const override; |
172 | |
173 | protected: |
174 | void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override; |
175 | }; |
176 | |
177 | class ASTAlterQuery : public ASTQueryWithTableAndOutput, public ASTQueryWithOnCluster |
178 | { |
179 | public: |
180 | bool is_live_view{false}; /// true for ALTER LIVE VIEW |
181 | |
182 | ASTAlterCommandList * command_list = nullptr; |
183 | |
184 | String getID(char) const override; |
185 | |
186 | ASTPtr clone() const override; |
187 | |
188 | ASTPtr getRewrittenASTWithoutOnCluster(const std::string & new_database) const override |
189 | { |
190 | return removeOnCluster<ASTAlterQuery>(clone(), new_database); |
191 | } |
192 | |
193 | protected: |
194 | void formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override; |
195 | }; |
196 | |
197 | } |
198 | |