-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathoption.c
More file actions
148 lines (128 loc) · 3.46 KB
/
option.c
File metadata and controls
148 lines (128 loc) · 3.46 KB
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
/*-------------------------------------------------------------------------
*
* DuckDB Foreign Data Wrapper for PostgreSQL
*
* IDENTIFICATION
* option.c
*
*------------------------------------------------------------------------*/
#include "postgres.h"
#include "duckdb_fdw.h"
#include "access/reloptions.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_user_mapping.h"
#include "commands/defrem.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
/*
* Option context structure
*/
struct DuckDBFdwOption
{
const char *defname;
Oid optcontext;
};
/*
* Valid options
*/
static struct DuckDBFdwOption valid_options[] =
{
/* Connection options */
{"database", ForeignServerRelationId},
/* S3 / Cloud Credentials */
{"s3_region", ForeignServerRelationId},
{"s3_access_key_id", ForeignServerRelationId},
{"s3_secret_access_key", ForeignServerRelationId},
{"s3_endpoint", ForeignServerRelationId},
{"s3_endpoint_type", ForeignServerRelationId}, /* e.g. 's3_tables' */
{"s3_use_ssl", ForeignServerRelationId},
/* Catalogs */
{"attach_catalogs", ForeignServerRelationId}, /* format: 'name=uri;type iceberg' */
/* Extensions */
{"extensions", ForeignServerRelationId}, /* e.g., 'httpfs,spatial,iceberg' */
/* Table options */
{"table", ForeignTableRelationId},
{"read_parquet", ForeignTableRelationId}, /* Path to parquet file */
/* Execution options */
{"use_remote_estimate", ForeignServerRelationId},
{NULL, InvalidOid}
};
/*
* Check if the option is valid
*/
static bool
duckdb_is_valid_option(const char *option, Oid context)
{
struct DuckDBFdwOption *opt;
for (opt = valid_options; opt->defname; opt++)
{
if (context == opt->optcontext && strcmp(opt->defname, option) == 0)
return true;
}
return false;
}
/*
* FDW Option Validator
*/
PG_FUNCTION_INFO_V1(duckdb_fdw_validator);
Datum
duckdb_fdw_validator(PG_FUNCTION_ARGS)
{
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
Oid catalog = PG_GETARG_OID(1);
ListCell *lc;
foreach(lc, options_list)
{
DefElem *def = (DefElem *) lfirst(lc);
if (!duckdb_is_valid_option(def->defname, catalog))
{
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
errmsg("invalid option \"%s\"", def->defname)));
}
}
PG_RETURN_VOID();
}
/*
* Extract FDW options into a struct
*/
duckdb_opt *
duckdb_get_options(Oid foreignoid)
{
ForeignTable *f_table = NULL;
ForeignServer *f_server = NULL;
List *options;
ListCell *lc;
duckdb_opt *opt;
opt = (duckdb_opt *) palloc0(sizeof(duckdb_opt));
PG_TRY();
{
f_table = GetForeignTable(foreignoid);
f_server = GetForeignServer(f_table->serverid);
}
PG_CATCH();
{
FlushErrorState();
f_table = NULL;
f_server = GetForeignServer(foreignoid);
}
PG_END_TRY();
options = NIL;
if (f_table)
options = list_concat(options, f_table->options);
options = list_concat(options, f_server->options);
foreach(lc, options)
{
DefElem *def = (DefElem *) lfirst(lc);
if (strcmp(def->defname, "database") == 0)
opt->svr_database = defGetString(def);
else if (strcmp(def->defname, "table") == 0)
opt->svr_table = defGetString(def);
else if (strcmp(def->defname, "use_remote_estimate") == 0)
opt->use_remote_estimate = defGetBoolean(def);
}
/* If table name is not specified, use Postgres relation name */
if (!opt->svr_table && f_table)
opt->svr_table = get_rel_name(foreignoid);
return opt;
}