forked from duckdb/duckdb-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres_table_set.cpp
394 lines (362 loc) · 14.9 KB
/
postgres_table_set.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "storage/postgres_table_set.hpp"
#include "storage/postgres_transaction.hpp"
#include "duckdb/parser/parsed_data/create_table_info.hpp"
#include "duckdb/parser/constraints/not_null_constraint.hpp"
#include "duckdb/parser/constraints/unique_constraint.hpp"
#include "duckdb/parser/expression/constant_expression.hpp"
#include "duckdb/planner/parsed_data/bound_create_table_info.hpp"
#include "duckdb/catalog/dependency_list.hpp"
#include "duckdb/parser/parsed_data/create_table_info.hpp"
#include "duckdb/parser/constraints/list.hpp"
#include "storage/postgres_schema_entry.hpp"
#include "duckdb/parser/parser.hpp"
#include "duckdb/common/string_util.hpp"
#include "postgres_conversion.hpp"
namespace duckdb {
PostgresTableSet::PostgresTableSet(PostgresSchemaEntry &schema, unique_ptr<PostgresResultSlice> table_result_p)
: PostgresInSchemaSet(schema, !table_result_p), table_result(std::move(table_result_p)) {
}
string PostgresTableSet::GetInitializeQuery(const string &schema, const string &table) {
string base_query = R"(
SELECT pg_namespace.oid AS namespace_id, relname, relpages, attname,
pg_type.typname type_name, atttypmod type_modifier, pg_attribute.attndims ndim,
attnum, pg_attribute.attnotnull AS notnull, NULL constraint_id,
NULL constraint_type, NULL constraint_key
FROM pg_class
JOIN pg_namespace ON relnamespace = pg_namespace.oid
JOIN pg_attribute ON pg_class.oid=pg_attribute.attrelid
JOIN pg_type ON atttypid=pg_type.oid
WHERE attnum > 0 AND relkind IN ('r', 'v', 'm', 'f', 'p') ${CONDITION}
UNION ALL
SELECT pg_namespace.oid AS namespace_id, relname, NULL relpages, NULL attname, NULL type_name,
NULL type_modifier, NULL ndim, NULL attnum, NULL AS notnull,
pg_constraint.oid AS constraint_id, contype AS constraint_type,
conkey AS constraint_key
FROM pg_class
JOIN pg_namespace ON relnamespace = pg_namespace.oid
JOIN pg_constraint ON (pg_class.oid=pg_constraint.conrelid)
WHERE relkind IN ('r', 'v', 'm', 'f', 'p') AND contype IN ('p', 'u') ${CONDITION}
ORDER BY namespace_id, relname, attnum, constraint_id;
)";
string condition;
if (!schema.empty()) {
condition += "AND pg_namespace.nspname=" + KeywordHelper::WriteQuoted(schema);
}
if (!table.empty()) {
condition += "AND relname=" + KeywordHelper::WriteQuoted(table);
}
return StringUtil::Replace(base_query, "${CONDITION}", condition);
}
void PostgresTableSet::AddColumn(optional_ptr<PostgresTransaction> transaction,
optional_ptr<PostgresSchemaEntry> schema, PostgresResult &result, idx_t row,
PostgresTableInfo &table_info) {
PostgresTypeData type_info;
idx_t column_index = 3;
auto column_name = result.GetString(row, column_index);
type_info.type_name = result.GetString(row, column_index + 1);
type_info.type_modifier = result.GetInt64(row, column_index + 2);
type_info.array_dimensions = result.GetInt64(row, column_index + 3);
bool is_not_null = result.GetBool(row, column_index + 5);
string default_value;
PostgresType postgres_type;
auto column_type = PostgresUtils::TypeToLogicalType(transaction, schema, type_info, postgres_type);
table_info.postgres_types.push_back(std::move(postgres_type));
table_info.postgres_names.push_back(column_name);
ColumnDefinition column(std::move(column_name), std::move(column_type));
if (!default_value.empty()) {
auto expressions = Parser::ParseExpressionList(default_value);
if (expressions.empty()) {
throw InternalException("Expression list is empty");
}
column.SetDefaultValue(std::move(expressions[0]));
}
auto &create_info = *table_info.create_info;
if (is_not_null) {
create_info.constraints.push_back(
make_uniq<NotNullConstraint>(LogicalIndex(create_info.columns.PhysicalColumnCount())));
}
create_info.columns.AddColumn(std::move(column));
}
void PostgresTableSet::AddConstraint(PostgresResult &result, idx_t row, PostgresTableInfo &table_info) {
idx_t column_index = 9;
auto constraint_type = result.GetString(row, column_index + 1);
auto constraint_key = result.GetString(row, column_index + 2);
if (constraint_key.empty() || constraint_key.front() != '{' || constraint_key.back() != '}') {
// invalid constraint key
D_ASSERT(0);
return;
}
auto &create_info = *table_info.create_info;
auto splits = StringUtil::Split(constraint_key.substr(1, constraint_key.size() - 2), ",");
vector<string> columns;
for (auto &split : splits) {
auto index = std::stoull(split);
if (index <= 0 || index > create_info.columns.LogicalColumnCount()) {
return;
}
columns.push_back(create_info.columns.GetColumn(LogicalIndex(index - 1)).Name());
}
create_info.constraints.push_back(make_uniq<UniqueConstraint>(std::move(columns), constraint_type == "p"));
}
void PostgresTableSet::AddColumnOrConstraint(optional_ptr<PostgresTransaction> transaction,
optional_ptr<PostgresSchemaEntry> schema, PostgresResult &result,
idx_t row, PostgresTableInfo &table_info) {
if (result.IsNull(row, 3)) {
// constraint
AddConstraint(result, row, table_info);
} else {
AddColumn(transaction, schema, result, row, table_info);
}
}
void PostgresTableSet::CreateEntries(PostgresTransaction &transaction, PostgresResult &result, idx_t start, idx_t end) {
vector<unique_ptr<PostgresTableInfo>> tables;
unique_ptr<PostgresTableInfo> info;
for (idx_t row = start; row < end; row++) {
auto table_name = result.GetString(row, 1);
if (!info || info->GetTableName() != table_name) {
if (info) {
tables.push_back(std::move(info));
}
auto approx_num_pages = result.IsNull(row, 2) ? 0 : result.GetInt64(row, 2);
info = make_uniq<PostgresTableInfo>(schema, table_name);
info->approx_num_pages = approx_num_pages;
}
AddColumnOrConstraint(&transaction, &schema, result, row, *info);
}
if (info) {
tables.push_back(std::move(info));
}
for (auto &tbl_info : tables) {
auto table_entry = make_uniq<PostgresTableEntry>(catalog, schema, *tbl_info);
CreateEntry(std::move(table_entry));
}
}
void PostgresTableSet::LoadEntries(ClientContext &context) {
auto &transaction = PostgresTransaction::Get(context, catalog);
if (table_result) {
CreateEntries(transaction, table_result->GetResult(), table_result->start, table_result->end);
table_result.reset();
} else {
auto query = GetInitializeQuery(schema.name);
auto result = transaction.Query(query);
auto rows = result->Count();
CreateEntries(transaction, *result, 0, rows);
}
}
unique_ptr<PostgresTableInfo> PostgresTableSet::GetTableInfo(PostgresTransaction &transaction,
PostgresSchemaEntry &schema, const string &table_name) {
auto query = PostgresTableSet::GetInitializeQuery(schema.name, table_name);
auto result = transaction.Query(query);
auto rows = result->Count();
if (rows == 0) {
return nullptr;
}
auto table_info = make_uniq<PostgresTableInfo>(schema, table_name);
for (idx_t row = 0; row < rows; row++) {
AddColumnOrConstraint(&transaction, &schema, *result, row, *table_info);
}
table_info->approx_num_pages = result->GetInt64(0, 2);
return table_info;
}
unique_ptr<PostgresTableInfo> PostgresTableSet::GetTableInfo(PostgresConnection &connection, const string &schema_name,
const string &table_name) {
auto query = PostgresTableSet::GetInitializeQuery(schema_name, table_name);
auto result = connection.Query(query);
auto rows = result->Count();
if (rows == 0) {
throw InvalidInputException("Table %s does not contain any columns.", table_name);
}
auto table_info = make_uniq<PostgresTableInfo>(schema_name, table_name);
for (idx_t row = 0; row < rows; row++) {
AddColumnOrConstraint(nullptr, nullptr, *result, row, *table_info);
}
table_info->approx_num_pages = result->GetInt64(0, 2);
return table_info;
}
optional_ptr<CatalogEntry> PostgresTableSet::ReloadEntry(ClientContext &context, const string &table_name) {
auto &transaction = PostgresTransaction::Get(context, catalog);
auto table_info = GetTableInfo(transaction, schema, table_name);
if (!table_info) {
return nullptr;
}
auto table_entry = make_uniq<PostgresTableEntry>(catalog, schema, *table_info);
auto table_ptr = table_entry.get();
CreateEntry(std::move(table_entry));
return table_ptr;
}
// FIXME - this is almost entirely copied from TableCatalogEntry::ColumnsToSQL - should be unified
string PostgresColumnsToSQL(const ColumnList &columns, const vector<unique_ptr<Constraint>> &constraints) {
std::stringstream ss;
ss << "(";
// find all columns that have NOT NULL specified, but are NOT primary key
// columns
logical_index_set_t not_null_columns;
logical_index_set_t unique_columns;
logical_index_set_t pk_columns;
unordered_set<string> multi_key_pks;
vector<string> extra_constraints;
for (auto &constraint : constraints) {
if (constraint->type == ConstraintType::NOT_NULL) {
auto ¬_null = constraint->Cast<NotNullConstraint>();
not_null_columns.insert(not_null.index);
} else if (constraint->type == ConstraintType::UNIQUE) {
auto &pk = constraint->Cast<UniqueConstraint>();
vector<string> constraint_columns = pk.columns;
if (pk.index.index != DConstants::INVALID_INDEX) {
// no columns specified: single column constraint
if (pk.is_primary_key) {
pk_columns.insert(pk.index);
} else {
unique_columns.insert(pk.index);
}
} else {
// multi-column constraint, this constraint needs to go at the end after
// all columns
if (pk.is_primary_key) {
// multi key pk column: insert set of columns into multi_key_pks
for (auto &col : pk.columns) {
multi_key_pks.insert(col);
}
}
extra_constraints.push_back(constraint->ToString());
}
} else if (constraint->type == ConstraintType::FOREIGN_KEY) {
auto &fk = constraint->Cast<ForeignKeyConstraint>();
if (fk.info.type == ForeignKeyType::FK_TYPE_FOREIGN_KEY_TABLE ||
fk.info.type == ForeignKeyType::FK_TYPE_SELF_REFERENCE_TABLE) {
extra_constraints.push_back(constraint->ToString());
}
} else {
extra_constraints.push_back(constraint->ToString());
}
}
for (auto &column : columns.Logical()) {
if (column.Oid() > 0) {
ss << ", ";
}
ss << KeywordHelper::WriteOptionallyQuoted(column.Name()) << " ";
ss << PostgresUtils::TypeToString(column.Type());
bool not_null = not_null_columns.find(column.Logical()) != not_null_columns.end();
bool is_single_key_pk = pk_columns.find(column.Logical()) != pk_columns.end();
bool is_multi_key_pk = multi_key_pks.find(column.Name()) != multi_key_pks.end();
bool is_unique = unique_columns.find(column.Logical()) != unique_columns.end();
if (not_null && !is_single_key_pk && !is_multi_key_pk) {
// NOT NULL but not a primary key column
ss << " NOT NULL";
}
if (is_single_key_pk) {
// single column pk: insert constraint here
ss << " PRIMARY KEY";
}
if (is_unique) {
// single column unique: insert constraint here
ss << " UNIQUE";
}
if (column.Generated()) {
ss << " GENERATED ALWAYS AS(" << column.GeneratedExpression().ToString() << ")";
} else if (column.HasDefaultValue()) {
ss << " DEFAULT(" << column.DefaultValue().ToString() << ")";
}
}
// print any extra constraints that still need to be printed
for (auto &extra_constraint : extra_constraints) {
ss << ", ";
ss << extra_constraint;
}
ss << ")";
return ss.str();
}
string GetPostgresCreateTable(CreateTableInfo &info) {
for (idx_t i = 0; i < info.columns.LogicalColumnCount(); i++) {
auto &col = info.columns.GetColumnMutable(LogicalIndex(i));
col.SetType(PostgresUtils::ToPostgresType(col.GetType()));
}
std::stringstream ss;
ss << "CREATE TABLE ";
if (info.on_conflict == OnCreateConflict::IGNORE_ON_CONFLICT) {
ss << "IF NOT EXISTS ";
}
if (!info.schema.empty()) {
ss << KeywordHelper::WriteQuoted(info.schema, '"');
ss << ".";
}
ss << KeywordHelper::WriteQuoted(info.table, '"');
ss << PostgresColumnsToSQL(info.columns, info.constraints);
ss << ";";
return ss.str();
}
optional_ptr<CatalogEntry> PostgresTableSet::CreateTable(ClientContext &context, BoundCreateTableInfo &info) {
auto &transaction = PostgresTransaction::Get(context, catalog);
auto create_sql = GetPostgresCreateTable(info.Base());
transaction.Query(create_sql);
auto tbl_entry = make_uniq<PostgresTableEntry>(catalog, schema, info.Base());
return CreateEntry(std::move(tbl_entry));
}
void PostgresTableSet::AlterTable(ClientContext &context, RenameTableInfo &info) {
auto &transaction = PostgresTransaction::Get(context, catalog);
string sql = "ALTER TABLE ";
sql += KeywordHelper::WriteOptionallyQuoted(schema.name) + ".";
sql += KeywordHelper::WriteOptionallyQuoted(info.name);
sql += " RENAME TO ";
sql += KeywordHelper::WriteOptionallyQuoted(info.new_table_name);
transaction.Query(sql);
}
void PostgresTableSet::AlterTable(ClientContext &context, RenameColumnInfo &info) {
auto &transaction = PostgresTransaction::Get(context, catalog);
string sql = "ALTER TABLE ";
sql += KeywordHelper::WriteOptionallyQuoted(schema.name) + ".";
sql += KeywordHelper::WriteOptionallyQuoted(info.name);
sql += " RENAME COLUMN ";
sql += KeywordHelper::WriteOptionallyQuoted(info.old_name);
sql += " TO ";
sql += KeywordHelper::WriteOptionallyQuoted(info.new_name);
transaction.Query(sql);
}
void PostgresTableSet::AlterTable(ClientContext &context, AddColumnInfo &info) {
auto &transaction = PostgresTransaction::Get(context, catalog);
string sql = "ALTER TABLE ";
sql += KeywordHelper::WriteOptionallyQuoted(schema.name) + ".";
sql += KeywordHelper::WriteOptionallyQuoted(info.name);
sql += " ADD COLUMN ";
if (info.if_column_not_exists) {
sql += "IF NOT EXISTS ";
}
sql += KeywordHelper::WriteOptionallyQuoted(info.new_column.Name());
sql += " ";
sql += info.new_column.Type().ToString();
transaction.Query(sql);
}
void PostgresTableSet::AlterTable(ClientContext &context, RemoveColumnInfo &info) {
auto &transaction = PostgresTransaction::Get(context, catalog);
string sql = "ALTER TABLE ";
sql += KeywordHelper::WriteOptionallyQuoted(schema.name) + ".";
sql += KeywordHelper::WriteOptionallyQuoted(info.name);
sql += " DROP COLUMN ";
if (info.if_column_exists) {
sql += "IF EXISTS ";
}
sql += KeywordHelper::WriteOptionallyQuoted(info.removed_column);
transaction.Query(sql);
}
void PostgresTableSet::AlterTable(ClientContext &context, AlterTableInfo &alter) {
switch (alter.alter_table_type) {
case AlterTableType::RENAME_TABLE:
AlterTable(context, alter.Cast<RenameTableInfo>());
break;
case AlterTableType::RENAME_COLUMN:
AlterTable(context, alter.Cast<RenameColumnInfo>());
break;
case AlterTableType::ADD_COLUMN:
AlterTable(context, alter.Cast<AddColumnInfo>());
break;
case AlterTableType::REMOVE_COLUMN:
AlterTable(context, alter.Cast<RemoveColumnInfo>());
break;
default:
throw BinderException("Unsupported ALTER TABLE type - Postgres tables only "
"support RENAME TABLE, RENAME COLUMN, "
"ADD COLUMN and DROP COLUMN");
}
ClearEntries();
}
} // namespace duckdb