-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathoption.c
More file actions
286 lines (248 loc) · 7.11 KB
/
Copy pathoption.c
File metadata and controls
286 lines (248 loc) · 7.11 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
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
/*-------------------------------------------------------------------------
*
* option.c
* FDW option handling for clickhouse_fdw
*
* Portions Copyright (c) 2012-2019, PostgreSQL Global Development Group
* Portions Copyright (c) 2019-2022, Adjust GmbH
* Copyright (c) 2025, ClickHouse, Inc.
*
* IDENTIFICATION
* github.com/clickhouse/clickhouse_fdw/src/option.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "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 "commands/extension.h"
#include "utils/builtins.h"
#include "utils/varlena.h"
static char *DEFAULT_DBNAME = "default";
/*
* Describes the valid options for objects that this wrapper uses.
*/
typedef struct ChFdwOption
{
const char *keyword;
Oid optcontext; /* OID of catalog in which option may appear */
bool is_ch_opt; /* true if it's used in clickhouseclient */
char dispchar[10];
} ChFdwOption;
/*
* Valid options for clickhouse_fdw.
* Allocated and filled in InitChFdwOptions.
*/
static ChFdwOption *clickhouse_fdw_options;
/*
* Valid options for clickhouse client.
* Allocated and filled in InitChFdwOptions.
*/
static const ChFdwOption ch_options[] =
{
{"host", 0, false},
{"port", 0, false},
{"dbname", 0, false},
{"user", 0, false},
{"password", 0, false},
{NULL}
};
/*
* Helper functions
*/
static void InitChFdwOptions(void);
static bool is_valid_option(const char *keyword, Oid context);
static bool is_ch_option(const char *keyword);
/*
* Validate the generic options given to a FOREIGN DATA WRAPPER, SERVER,
* USER MAPPING or FOREIGN TABLE that uses clickhouse_fdw.
*
* Raise an ERROR if the option or its value is considered invalid.
*/
PG_FUNCTION_INFO_V1(clickhouse_fdw_validator);
Datum
clickhouse_fdw_validator(PG_FUNCTION_ARGS)
{
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
Oid catalog = PG_GETARG_OID(1);
ListCell *cell;
/* Build our options lists if we didn't yet. */
InitChFdwOptions();
/*
* Check that only options supported by clickhouse_fdw, and allowed for the
* current object type, are given.
*/
foreach (cell, options_list)
{
DefElem *def = (DefElem *) lfirst(cell);
if (!is_valid_option(def->defname, catalog))
{
/*
* Unknown option specified, complain about it. Provide a hint
* with list of valid options for the object.
*/
ChFdwOption *opt;
StringInfoData buf;
initStringInfo(&buf);
for (opt = clickhouse_fdw_options; opt->keyword; opt++)
{
if (catalog == opt->optcontext)
appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
opt->keyword);
}
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
errmsg("invalid option \"%s\"", def->defname),
errhint("Valid options in this context are: %s",
buf.data)));
}
}
PG_RETURN_VOID();
}
/*
* Initialize option lists.
*/
static void
InitChFdwOptions(void)
{
int num_ch_opts;
const ChFdwOption *lopt;
ChFdwOption *popt;
/* non-clickhouseclient FDW-specific FDW options */
static const ChFdwOption non_ch_options[] =
{
{"database", ForeignTableRelationId, false},
{"table_name", ForeignTableRelationId, false},
{"engine", ForeignTableRelationId, false},
{"driver", ForeignServerRelationId, false},
{"aggregatefunction", AttributeRelationId, false},
{"simpleaggregatefunction", AttributeRelationId, false},
{"column_name", AttributeRelationId, false},
{"arrays", AttributeRelationId, false},
{NULL, InvalidOid, false}
};
/* Prevent redundant initialization. */
if (clickhouse_fdw_options)
{
return;
}
/* Count how many clickhouseclient options are available. */
num_ch_opts = 0;
for (lopt = ch_options; lopt->keyword; lopt++)
{
num_ch_opts++;
}
/*
* We use plain malloc here to allocate clickhouse_fdw_options because it
* lives as long as the backend process does. Besides, keeping
* ch_options in memory allows us to avoid copying every keyword
* string.
*/
clickhouse_fdw_options = (ChFdwOption *) malloc(sizeof(
ChFdwOption) * num_ch_opts + sizeof(non_ch_options));
if (clickhouse_fdw_options == NULL)
ereport(ERROR,
(errcode(ERRCODE_FDW_OUT_OF_MEMORY),
errmsg("out of memory")));
popt = clickhouse_fdw_options;
for (lopt = ch_options; lopt->keyword; lopt++)
{
/* We don't have to copy keyword string, as described above. */
popt->keyword = lopt->keyword;
/*
* "user" and any secret options are allowed only on user mappings.
* Everything else is a server option.
*/
if (strcmp(lopt->keyword, "user") == 0 ||
strcmp(lopt->keyword, "password") == 0 ||
strchr(lopt->dispchar, '*'))
{
popt->optcontext = UserMappingRelationId;
}
else
{
popt->optcontext = ForeignServerRelationId;
}
popt->is_ch_opt = true;
popt++;
}
/* Append FDW-specific options and dummy terminator. */
memcpy(popt, non_ch_options, sizeof(non_ch_options));
popt->is_ch_opt = true;
popt++;
}
/*
* Check whether the given option is one of the valid clickhouse_fdw options.
* context is the Oid of the catalog holding the object the option is for.
*/
static bool
is_valid_option(const char *keyword, Oid context)
{
ChFdwOption *opt;
Assert(clickhouse_fdw_options); /* must be initialized already */
for (opt = clickhouse_fdw_options; opt->keyword; opt++)
{
if (context == opt->optcontext && strcmp(opt->keyword, keyword) == 0)
{
return true;
}
}
return false;
}
/*
* Check whether the given option is one of the valid clickhouseclient options.
*/
static bool
is_ch_option(const char *keyword)
{
ChFdwOption *opt;
Assert(clickhouse_fdw_options); /* must be initialized already */
for (opt = clickhouse_fdw_options; opt->keyword; opt++)
{
if (opt->is_ch_opt && strcmp(opt->keyword, keyword) == 0)
{
return true;
}
}
return false;
}
/*
* Generate key-value arrays which include only clickhouseclient options from the
* given list (which can contain any kind of options). Caller must have
* allocated large-enough arrays. Returns number of options found.
*/
void
chfdw_extract_options(List *defelems, char **driver, char **host, int *port,
char **dbname, char **username, char **password)
{
ListCell *lc;
/* Build our options lists if we didn't yet. */
InitChFdwOptions();
foreach (lc, defelems)
{
DefElem *def = (DefElem *) lfirst(lc);
if (driver && strcmp(def->defname, "driver") == 0)
*driver = defGetString(def);
if (is_ch_option(def->defname))
{
if (host && strcmp(def->defname, "host") == 0)
*host = defGetString(def);
else if (port && strcmp(def->defname, "port") == 0)
*port = atoi(defGetString(def));
else if (username && strcmp(def->defname, "user") == 0)
*username = defGetString(def);
else if (password && strcmp(def->defname, "password") == 0)
*password = defGetString(def);
else if (dbname && strcmp(def->defname, "dbname") == 0)
{
*dbname = defGetString(def);
if (*dbname[0] == '\0')
*dbname = DEFAULT_DBNAME;
}
}
}
}