-
Notifications
You must be signed in to change notification settings - Fork 36
/
postgres_index.cpp
79 lines (68 loc) · 3.09 KB
/
postgres_index.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
#include "storage/postgres_catalog.hpp"
#include "storage/postgres_index.hpp"
#include "duckdb/parser/statement/create_statement.hpp"
#include "duckdb/planner/operator/logical_extension_operator.hpp"
#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
#include "duckdb/parser/parsed_data/drop_info.hpp"
namespace duckdb {
PostgresCreateIndex::PostgresCreateIndex(unique_ptr<CreateIndexInfo> info, TableCatalogEntry &table)
: PhysicalOperator(PhysicalOperatorType::EXTENSION, {LogicalType::BIGINT}, 1), info(std::move(info)), table(table) {
}
//===--------------------------------------------------------------------===//
// Source
//===--------------------------------------------------------------------===//
SourceResultType PostgresCreateIndex::GetData(ExecutionContext &context, DataChunk &chunk,
OperatorSourceInput &input) const {
auto &catalog = table.catalog;
auto &schema = table.schema;
auto existing =
schema.GetEntry(catalog.GetCatalogTransaction(context.client), CatalogType::INDEX_ENTRY, info->index_name);
if (existing) {
switch (info->on_conflict) {
case OnCreateConflict::IGNORE_ON_CONFLICT:
return SourceResultType::FINISHED;
case OnCreateConflict::ERROR_ON_CONFLICT:
throw BinderException("Index with name \"%s\" already exists in schema \"%s\"", info->index_name,
table.schema.name);
case OnCreateConflict::REPLACE_ON_CONFLICT: {
DropInfo drop_info;
drop_info.type = CatalogType::INDEX_ENTRY;
drop_info.schema = info->schema;
drop_info.name = info->index_name;
schema.DropEntry(context.client, drop_info);
break;
}
default:
throw InternalException("Unsupported on create conflict");
}
}
schema.CreateIndex(context.client, *info, table);
return SourceResultType::FINISHED;
}
//===--------------------------------------------------------------------===//
// Logical Operator
//===--------------------------------------------------------------------===//
class LogicalPostgresCreateIndex : public LogicalExtensionOperator {
public:
LogicalPostgresCreateIndex(unique_ptr<CreateIndexInfo> info_p, TableCatalogEntry &table)
: info(std::move(info_p)), table(table) {
}
unique_ptr<CreateIndexInfo> info;
TableCatalogEntry &table;
unique_ptr<PhysicalOperator> CreatePlan(ClientContext &context, PhysicalPlanGenerator &generator) override {
return make_uniq<PostgresCreateIndex>(std::move(info), table);
}
void Serialize(Serializer &serializer) const override {
throw NotImplementedException("Cannot serialize Postgres Create index");
}
void ResolveTypes() override {
types = {LogicalType::BIGINT};
}
};
unique_ptr<LogicalOperator> PostgresCatalog::BindCreateIndex(Binder &binder, CreateStatement &stmt,
TableCatalogEntry &table,
unique_ptr<LogicalOperator> plan) {
return make_uniq<LogicalPostgresCreateIndex>(unique_ptr_cast<CreateInfo, CreateIndexInfo>(std::move(stmt.info)),
table);
}
} // namespace duckdb