1#include "duckdb/parser/parsed_data/alter_table_info.hpp"
2
3#include "duckdb/common/field_writer.hpp"
4#include "duckdb/parser/constraint.hpp"
5
6namespace duckdb {
7
8//===--------------------------------------------------------------------===//
9// ChangeOwnershipInfo
10//===--------------------------------------------------------------------===//
11ChangeOwnershipInfo::ChangeOwnershipInfo(CatalogType entry_catalog_type, string entry_catalog_p, string entry_schema_p,
12 string entry_name_p, string owner_schema_p, string owner_name_p,
13 OnEntryNotFound if_not_found)
14 : AlterInfo(AlterType::CHANGE_OWNERSHIP, std::move(entry_catalog_p), std::move(entry_schema_p),
15 std::move(entry_name_p), if_not_found),
16 entry_catalog_type(entry_catalog_type), owner_schema(std::move(owner_schema_p)),
17 owner_name(std::move(owner_name_p)) {
18}
19
20CatalogType ChangeOwnershipInfo::GetCatalogType() const {
21 return entry_catalog_type;
22}
23
24unique_ptr<AlterInfo> ChangeOwnershipInfo::Copy() const {
25 return make_uniq_base<AlterInfo, ChangeOwnershipInfo>(args: entry_catalog_type, args: catalog, args: schema, args: name, args: owner_schema,
26 args: owner_name, args: if_not_found);
27}
28
29void ChangeOwnershipInfo::Serialize(FieldWriter &writer) const {
30 throw InternalException("ChangeOwnershipInfo cannot be serialized");
31}
32
33//===--------------------------------------------------------------------===//
34// AlterTableInfo
35//===--------------------------------------------------------------------===//
36AlterTableInfo::AlterTableInfo(AlterTableType type, AlterEntryData data)
37 : AlterInfo(AlterType::ALTER_TABLE, std::move(data.catalog), std::move(data.schema), std::move(data.name),
38 data.if_not_found),
39 alter_table_type(type) {
40}
41AlterTableInfo::~AlterTableInfo() {
42}
43
44CatalogType AlterTableInfo::GetCatalogType() const {
45 return CatalogType::TABLE_ENTRY;
46}
47
48void AlterTableInfo::Serialize(FieldWriter &writer) const {
49 writer.WriteField<AlterTableType>(element: alter_table_type);
50 writer.WriteString(val: catalog);
51 writer.WriteString(val: schema);
52 writer.WriteString(val: name);
53 writer.WriteField(element: if_not_found);
54 SerializeAlterTable(writer);
55}
56
57unique_ptr<AlterInfo> AlterTableInfo::Deserialize(FieldReader &reader) {
58 auto type = reader.ReadRequired<AlterTableType>();
59 AlterEntryData data;
60 data.catalog = reader.ReadRequired<string>();
61 data.schema = reader.ReadRequired<string>();
62 data.name = reader.ReadRequired<string>();
63 data.if_not_found = reader.ReadRequired<OnEntryNotFound>();
64
65 unique_ptr<AlterTableInfo> info;
66 switch (type) {
67 case AlterTableType::RENAME_COLUMN:
68 return RenameColumnInfo::Deserialize(reader, data: std::move(data));
69 case AlterTableType::RENAME_TABLE:
70 return RenameTableInfo::Deserialize(reader, data: std::move(data));
71 case AlterTableType::ADD_COLUMN:
72 return AddColumnInfo::Deserialize(reader, data: std::move(data));
73 case AlterTableType::REMOVE_COLUMN:
74 return RemoveColumnInfo::Deserialize(reader, data: std::move(data));
75 case AlterTableType::ALTER_COLUMN_TYPE:
76 return ChangeColumnTypeInfo::Deserialize(reader, data: std::move(data));
77 case AlterTableType::SET_DEFAULT:
78 return SetDefaultInfo::Deserialize(reader, data: std::move(data));
79 case AlterTableType::FOREIGN_KEY_CONSTRAINT:
80 return AlterForeignKeyInfo::Deserialize(reader, data: std::move(data));
81 case AlterTableType::SET_NOT_NULL:
82 return SetNotNullInfo::Deserialize(reader, data: std::move(data));
83 case AlterTableType::DROP_NOT_NULL:
84 return DropNotNullInfo::Deserialize(reader, data: std::move(data));
85 default:
86 throw SerializationException("Unknown alter table type for deserialization!");
87 }
88}
89
90//===--------------------------------------------------------------------===//
91// RenameColumnInfo
92//===--------------------------------------------------------------------===//
93RenameColumnInfo::RenameColumnInfo(AlterEntryData data, string old_name_p, string new_name_p)
94 : AlterTableInfo(AlterTableType::RENAME_COLUMN, std::move(data)), old_name(std::move(old_name_p)),
95 new_name(std::move(new_name_p)) {
96}
97RenameColumnInfo::~RenameColumnInfo() {
98}
99
100unique_ptr<AlterInfo> RenameColumnInfo::Copy() const {
101 return make_uniq_base<AlterInfo, RenameColumnInfo>(args: GetAlterEntryData(), args: old_name, args: new_name);
102}
103
104void RenameColumnInfo::SerializeAlterTable(FieldWriter &writer) const {
105 writer.WriteString(val: old_name);
106 writer.WriteString(val: new_name);
107}
108
109unique_ptr<AlterInfo> RenameColumnInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
110 auto old_name = reader.ReadRequired<string>();
111 auto new_name = reader.ReadRequired<string>();
112 return make_uniq<RenameColumnInfo>(args: std::move(data), args&: old_name, args&: new_name);
113}
114
115//===--------------------------------------------------------------------===//
116// RenameTableInfo
117//===--------------------------------------------------------------------===//
118RenameTableInfo::RenameTableInfo(AlterEntryData data, string new_name_p)
119 : AlterTableInfo(AlterTableType::RENAME_TABLE, std::move(data)), new_table_name(std::move(new_name_p)) {
120}
121RenameTableInfo::~RenameTableInfo() {
122}
123
124unique_ptr<AlterInfo> RenameTableInfo::Copy() const {
125 return make_uniq_base<AlterInfo, RenameTableInfo>(args: GetAlterEntryData(), args: new_table_name);
126}
127
128void RenameTableInfo::SerializeAlterTable(FieldWriter &writer) const {
129 writer.WriteString(val: new_table_name);
130}
131
132unique_ptr<AlterInfo> RenameTableInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
133 auto new_name = reader.ReadRequired<string>();
134 return make_uniq<RenameTableInfo>(args: std::move(data), args&: new_name);
135}
136
137//===--------------------------------------------------------------------===//
138// AddColumnInfo
139//===--------------------------------------------------------------------===//
140AddColumnInfo::AddColumnInfo(AlterEntryData data, ColumnDefinition new_column, bool if_column_not_exists)
141 : AlterTableInfo(AlterTableType::ADD_COLUMN, std::move(data)), new_column(std::move(new_column)),
142 if_column_not_exists(if_column_not_exists) {
143}
144
145AddColumnInfo::~AddColumnInfo() {
146}
147
148unique_ptr<AlterInfo> AddColumnInfo::Copy() const {
149 return make_uniq_base<AlterInfo, AddColumnInfo>(args: GetAlterEntryData(), args: new_column.Copy(), args: if_column_not_exists);
150}
151
152void AddColumnInfo::SerializeAlterTable(FieldWriter &writer) const {
153 writer.WriteSerializable(element: new_column);
154 writer.WriteField<bool>(element: if_column_not_exists);
155}
156
157unique_ptr<AlterInfo> AddColumnInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
158 auto new_column = reader.ReadRequiredSerializable<ColumnDefinition, ColumnDefinition>();
159 auto if_column_not_exists = reader.ReadRequired<bool>();
160 return make_uniq<AddColumnInfo>(args: std::move(data), args: std::move(new_column), args&: if_column_not_exists);
161}
162
163//===--------------------------------------------------------------------===//
164// RemoveColumnInfo
165//===--------------------------------------------------------------------===//
166RemoveColumnInfo::RemoveColumnInfo(AlterEntryData data, string removed_column, bool if_column_exists, bool cascade)
167 : AlterTableInfo(AlterTableType::REMOVE_COLUMN, std::move(data)), removed_column(std::move(removed_column)),
168 if_column_exists(if_column_exists), cascade(cascade) {
169}
170RemoveColumnInfo::~RemoveColumnInfo() {
171}
172
173unique_ptr<AlterInfo> RemoveColumnInfo::Copy() const {
174 return make_uniq_base<AlterInfo, RemoveColumnInfo>(args: GetAlterEntryData(), args: removed_column, args: if_column_exists, args: cascade);
175}
176
177void RemoveColumnInfo::SerializeAlterTable(FieldWriter &writer) const {
178 writer.WriteString(val: removed_column);
179 writer.WriteField<bool>(element: if_column_exists);
180 writer.WriteField<bool>(element: cascade);
181}
182
183unique_ptr<AlterInfo> RemoveColumnInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
184 auto new_name = reader.ReadRequired<string>();
185 auto if_column_exists = reader.ReadRequired<bool>();
186 auto cascade = reader.ReadRequired<bool>();
187 return make_uniq<RemoveColumnInfo>(args: std::move(data), args: std::move(new_name), args&: if_column_exists, args&: cascade);
188}
189
190//===--------------------------------------------------------------------===//
191// ChangeColumnTypeInfo
192//===--------------------------------------------------------------------===//
193ChangeColumnTypeInfo::ChangeColumnTypeInfo(AlterEntryData data, string column_name, LogicalType target_type,
194 unique_ptr<ParsedExpression> expression)
195 : AlterTableInfo(AlterTableType::ALTER_COLUMN_TYPE, std::move(data)), column_name(std::move(column_name)),
196 target_type(std::move(target_type)), expression(std::move(expression)) {
197}
198ChangeColumnTypeInfo::~ChangeColumnTypeInfo() {
199}
200
201unique_ptr<AlterInfo> ChangeColumnTypeInfo::Copy() const {
202 return make_uniq_base<AlterInfo, ChangeColumnTypeInfo>(args: GetAlterEntryData(), args: column_name, args: target_type,
203 args: expression->Copy());
204}
205
206void ChangeColumnTypeInfo::SerializeAlterTable(FieldWriter &writer) const {
207 writer.WriteString(val: column_name);
208 writer.WriteSerializable(element: target_type);
209 writer.WriteOptional(element: expression);
210}
211
212unique_ptr<AlterInfo> ChangeColumnTypeInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
213 auto column_name = reader.ReadRequired<string>();
214 auto target_type = reader.ReadRequiredSerializable<LogicalType, LogicalType>();
215 auto expression = reader.ReadOptional<ParsedExpression>(default_value: nullptr);
216 return make_uniq<ChangeColumnTypeInfo>(args: std::move(data), args: std::move(column_name), args: std::move(target_type),
217 args: std::move(expression));
218}
219
220//===--------------------------------------------------------------------===//
221// SetDefaultInfo
222//===--------------------------------------------------------------------===//
223SetDefaultInfo::SetDefaultInfo(AlterEntryData data, string column_name_p, unique_ptr<ParsedExpression> new_default)
224 : AlterTableInfo(AlterTableType::SET_DEFAULT, std::move(data)), column_name(std::move(column_name_p)),
225 expression(std::move(new_default)) {
226}
227SetDefaultInfo::~SetDefaultInfo() {
228}
229
230unique_ptr<AlterInfo> SetDefaultInfo::Copy() const {
231 return make_uniq_base<AlterInfo, SetDefaultInfo>(args: GetAlterEntryData(), args: column_name,
232 args: expression ? expression->Copy() : nullptr);
233}
234
235void SetDefaultInfo::SerializeAlterTable(FieldWriter &writer) const {
236 writer.WriteString(val: column_name);
237 writer.WriteOptional(element: expression);
238}
239
240unique_ptr<AlterInfo> SetDefaultInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
241 auto column_name = reader.ReadRequired<string>();
242 auto new_default = reader.ReadOptional<ParsedExpression>(default_value: nullptr);
243 return make_uniq<SetDefaultInfo>(args: std::move(data), args: std::move(column_name), args: std::move(new_default));
244}
245
246//===--------------------------------------------------------------------===//
247// SetNotNullInfo
248//===--------------------------------------------------------------------===//
249SetNotNullInfo::SetNotNullInfo(AlterEntryData data, string column_name_p)
250 : AlterTableInfo(AlterTableType::SET_NOT_NULL, std::move(data)), column_name(std::move(column_name_p)) {
251}
252SetNotNullInfo::~SetNotNullInfo() {
253}
254
255unique_ptr<AlterInfo> SetNotNullInfo::Copy() const {
256 return make_uniq_base<AlterInfo, SetNotNullInfo>(args: GetAlterEntryData(), args: column_name);
257}
258
259void SetNotNullInfo::SerializeAlterTable(FieldWriter &writer) const {
260 writer.WriteString(val: column_name);
261}
262
263unique_ptr<AlterInfo> SetNotNullInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
264 auto column_name = reader.ReadRequired<string>();
265 return make_uniq<SetNotNullInfo>(args: std::move(data), args: std::move(column_name));
266}
267
268//===--------------------------------------------------------------------===//
269// DropNotNullInfo
270//===--------------------------------------------------------------------===//
271DropNotNullInfo::DropNotNullInfo(AlterEntryData data, string column_name_p)
272 : AlterTableInfo(AlterTableType::DROP_NOT_NULL, std::move(data)), column_name(std::move(column_name_p)) {
273}
274DropNotNullInfo::~DropNotNullInfo() {
275}
276
277unique_ptr<AlterInfo> DropNotNullInfo::Copy() const {
278 return make_uniq_base<AlterInfo, DropNotNullInfo>(args: GetAlterEntryData(), args: column_name);
279}
280
281void DropNotNullInfo::SerializeAlterTable(FieldWriter &writer) const {
282 writer.WriteString(val: column_name);
283}
284
285unique_ptr<AlterInfo> DropNotNullInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
286 auto column_name = reader.ReadRequired<string>();
287 return make_uniq<DropNotNullInfo>(args: std::move(data), args: std::move(column_name));
288}
289
290//===--------------------------------------------------------------------===//
291// AlterForeignKeyInfo
292//===--------------------------------------------------------------------===//
293AlterForeignKeyInfo::AlterForeignKeyInfo(AlterEntryData data, string fk_table, vector<string> pk_columns,
294 vector<string> fk_columns, vector<PhysicalIndex> pk_keys,
295 vector<PhysicalIndex> fk_keys, AlterForeignKeyType type_p)
296 : AlterTableInfo(AlterTableType::FOREIGN_KEY_CONSTRAINT, std::move(data)), fk_table(std::move(fk_table)),
297 pk_columns(std::move(pk_columns)), fk_columns(std::move(fk_columns)), pk_keys(std::move(pk_keys)),
298 fk_keys(std::move(fk_keys)), type(type_p) {
299}
300AlterForeignKeyInfo::~AlterForeignKeyInfo() {
301}
302
303unique_ptr<AlterInfo> AlterForeignKeyInfo::Copy() const {
304 return make_uniq_base<AlterInfo, AlterForeignKeyInfo>(args: GetAlterEntryData(), args: fk_table, args: pk_columns, args: fk_columns,
305 args: pk_keys, args: fk_keys, args: type);
306}
307
308void AlterForeignKeyInfo::SerializeAlterTable(FieldWriter &writer) const {
309 writer.WriteString(val: fk_table);
310 writer.WriteList<string>(elements: pk_columns);
311 writer.WriteList<string>(elements: fk_columns);
312 writer.WriteIndexList<PhysicalIndex>(elements: pk_keys);
313 writer.WriteIndexList<PhysicalIndex>(elements: fk_keys);
314 writer.WriteField<AlterForeignKeyType>(element: type);
315}
316
317unique_ptr<AlterInfo> AlterForeignKeyInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
318 auto fk_table = reader.ReadRequired<string>();
319 auto pk_columns = reader.ReadRequiredList<string>();
320 auto fk_columns = reader.ReadRequiredList<string>();
321 auto pk_keys = reader.ReadRequiredIndexList<PhysicalIndex>();
322 auto fk_keys = reader.ReadRequiredIndexList<PhysicalIndex>();
323 auto type = reader.ReadRequired<AlterForeignKeyType>();
324 return make_uniq<AlterForeignKeyInfo>(args: std::move(data), args: std::move(fk_table), args: std::move(pk_columns),
325 args: std::move(fk_columns), args: std::move(pk_keys), args: std::move(fk_keys), args&: type);
326}
327
328//===--------------------------------------------------------------------===//
329// Alter View
330//===--------------------------------------------------------------------===//
331AlterViewInfo::AlterViewInfo(AlterViewType type, AlterEntryData data)
332 : AlterInfo(AlterType::ALTER_VIEW, std::move(data.catalog), std::move(data.schema), std::move(data.name),
333 data.if_not_found),
334 alter_view_type(type) {
335}
336AlterViewInfo::~AlterViewInfo() {
337}
338
339CatalogType AlterViewInfo::GetCatalogType() const {
340 return CatalogType::VIEW_ENTRY;
341}
342
343void AlterViewInfo::Serialize(FieldWriter &writer) const {
344 writer.WriteField<AlterViewType>(element: alter_view_type);
345 writer.WriteString(val: catalog);
346 writer.WriteString(val: schema);
347 writer.WriteString(val: name);
348 writer.WriteField<OnEntryNotFound>(element: if_not_found);
349 SerializeAlterView(writer);
350}
351
352unique_ptr<AlterInfo> AlterViewInfo::Deserialize(FieldReader &reader) {
353 auto type = reader.ReadRequired<AlterViewType>();
354 AlterEntryData data;
355 data.catalog = reader.ReadRequired<string>();
356 data.schema = reader.ReadRequired<string>();
357 data.name = reader.ReadRequired<string>();
358 data.if_not_found = reader.ReadRequired<OnEntryNotFound>();
359 unique_ptr<AlterViewInfo> info;
360 switch (type) {
361 case AlterViewType::RENAME_VIEW:
362 return RenameViewInfo::Deserialize(reader, data: std::move(data));
363 default:
364 throw SerializationException("Unknown alter view type for deserialization!");
365 }
366}
367
368//===--------------------------------------------------------------------===//
369// RenameViewInfo
370//===--------------------------------------------------------------------===//
371RenameViewInfo::RenameViewInfo(AlterEntryData data, string new_name_p)
372 : AlterViewInfo(AlterViewType::RENAME_VIEW, std::move(data)), new_view_name(std::move(new_name_p)) {
373}
374RenameViewInfo::~RenameViewInfo() {
375}
376
377unique_ptr<AlterInfo> RenameViewInfo::Copy() const {
378 return make_uniq_base<AlterInfo, RenameViewInfo>(args: GetAlterEntryData(), args: new_view_name);
379}
380
381void RenameViewInfo::SerializeAlterView(FieldWriter &writer) const {
382 writer.WriteString(val: new_view_name);
383}
384
385unique_ptr<AlterInfo> RenameViewInfo::Deserialize(FieldReader &reader, AlterEntryData data) {
386 auto new_name = reader.ReadRequired<string>();
387 return make_uniq<RenameViewInfo>(args: std::move(data), args&: new_name);
388}
389} // namespace duckdb
390