-
Notifications
You must be signed in to change notification settings - Fork 15
/
option.c
402 lines (338 loc) · 10.2 KB
/
option.c
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
395
396
397
398
399
400
401
402
/*-------------------------------------------------------------------------
*
* InfluxDB Foreign Data Wrapper for PostgreSQL
*
* Portions Copyright (c) 2018, TOSHIBA CORPORATION
*
* IDENTIFICATION
* option.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "influxdb_fdw.h"
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <regex.h>
#include "funcapi.h"
#include "access/reloptions.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_user_mapping.h"
#include "catalog/pg_type.h"
#include "catalog/pg_collation.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "miscadmin.h"
#include "mb/pg_wchar.h"
#include "storage/fd.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/rel.h"
#include "utils/lsyscache.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/planmain.h"
#include "utils/varlena.h"
#include "utils/formatting.h"
#ifdef CXX_CLIENT
#include "query_cxx.h"
#endif
/*
* Describes the valid options for objects that use this wrapper.
*/
struct InfluxDBFdwOption
{
const char *optname;
Oid optcontext; /* Oid of catalog in which option may appear */
};
/*
* Valid options for influxdb_fdw.
*/
static struct InfluxDBFdwOption valid_options[] =
{
{"host", ForeignServerRelationId},
{"port", ForeignServerRelationId},
{"dbname", ForeignServerRelationId},
#ifdef CXX_CLIENT
{"version", ForeignServerRelationId},
{"retention_policy", ForeignServerRelationId},
{"auth_token", UserMappingRelationId},
#endif
{"user", UserMappingRelationId},
{"password", UserMappingRelationId},
{"table", ForeignTableRelationId},
{"column_name", AttributeRelationId},
{"tags", ForeignTableRelationId},
{"schemaless", ForeignTableRelationId},
#if (PG_VERSION_NUM >= 140000)
/* batch_size is available on both server and table */
{"batch_size", ForeignServerRelationId},
{"batch_size", ForeignTableRelationId},
#endif
{"tags", AttributeRelationId},
{"fields", AttributeRelationId},
/* Sentinel */
{NULL, InvalidOid}
};
static List *influxdbExtractTagsList(char *in_string);
extern Datum influxdb_fdw_validator(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(influxdb_fdw_validator);
bool influxdb_is_valid_option(const char *option, Oid context);
/*
* Validate the generic options given to a FOREIGN DATA WRAPPER, SERVER,
* USER MAPPING or FOREIGN TABLE that uses influxdb_fdw.
*
* Raise an ERROR if the option or its value is considered invalid.
*/
Datum
influxdb_fdw_validator(PG_FUNCTION_ARGS)
{
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
Oid catalog = PG_GETARG_OID(1);
ListCell *cell;
/*
* Check that only options supported by influxdb_fdw, and allowed for the
* current object type, are given.
*/
foreach(cell, options_list)
{
DefElem *def = (DefElem *) lfirst(cell);
if (!influxdb_is_valid_option(def->defname, catalog))
{
struct InfluxDBFdwOption *opt;
StringInfoData buf;
#if PG_VERSIION_NUM < 160000
/*
* Unknown option specified, complain about it. Provide a hint
* with list of valid options for the object.
*/
initStringInfo(&buf);
for (opt = valid_options; opt->optname; opt++)
{
if (catalog == opt->optcontext)
appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
opt->optname);
}
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
errmsg("invalid option \"%s\"", def->defname),
buf.len > 0
? errhint("Valid options in this context are: %s",
buf.data)
: errhint("There are no valid options in this context.")));
#else
/*
* Unknown option specified, complain about it. Provide a hint
* with list of valid options for the object.
*/
const char *closest_match;
ClosestMatchState match_state;
bool has_valid_options = false;
initClosestMatch(&match_state, def->defname, 4);
for (opt = valid_options; opt->optname; opt++)
{
if (catalog == opt->optcontext)
{
has_valid_options = true;
updateClosestMatch(&match_state, opt->optname);
}
}
closest_match = getClosestMatch(&match_state);
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_OPTION_NAME),
errmsg("invalid option \"%s\"", def->defname),
has_valid_options ? closest_match ?
errhint("Perhaps you meant the option \"%s\".",
closest_match) : 0 :
errhint("There are no valid options in this context.")));
#endif
}
/*
* Validate option value, when we can do so without any context.
*/
#if (PG_VERSION_NUM >= 140000)
if (strcmp(def->defname, "batch_size") == 0)
{
char *value;
int int_val;
bool is_parsed;
value = defGetString(def);
is_parsed = parse_int(value, &int_val, 0, NULL);
if (!is_parsed)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid value for integer option \"%s\": %s",
def->defname, value)));
if (int_val <= 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("\"%s\" must be an integer value greater than zero",
def->defname)));
}
#endif
#ifdef CXX_CLIENT
if(strcmp(def->defname, "version") == 0)
{
int int_val;
int_val = atoi(defGetString(def));
if(int_val != INFLUXDB_VERSION_1 && int_val !=INFLUXDB_VERSION_2)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("InfluxDB only support versions from v1.x to 2.x. \"%s\" must be 1 or 2.", def->defname)));
}
/* Validate host address if it is set */
if (strcmp(def->defname, "host") == 0)
{
regex_t regex;
int ret;
char *host = str_tolower(defGetString(def), strlen(defGetString(def)), C_COLLATION_OID);
/* Must start with either http:// or https:// */
ret = regcomp(®ex, "^((http|https)://)", REG_EXTENDED);
Assert(ret == 0); /*for debug invalid regex pattern */
ret = regexec(®ex, host, 0, NULL, 0);
if (ret == REG_NOMATCH)
{
regfree(®ex);
elog(ERROR, "influxdb_fdw: Host address must start with either http:// or https://");
}
regfree(®ex);
}
#endif
}
PG_RETURN_VOID();
}
/*
* Check if the provided option is one of the valid options.
* context is the Oid of the catalog holding the object the option is for.
*/
bool
influxdb_is_valid_option(const char *option, Oid context)
{
struct InfluxDBFdwOption *opt;
for (opt = valid_options; opt->optname; opt++)
{
if (context == opt->optcontext && strcmp(opt->optname, option) == 0)
return true;
}
return false;
}
/*
* Fetch the options for a influxdb_fdw foreign table.
*/
influxdb_opt *
influxdb_get_options(Oid foreignoid, Oid userid)
{
ForeignTable *f_table = NULL;
ForeignServer *f_server = NULL;
UserMapping *f_mapping;
List *options;
ListCell *lc;
influxdb_opt *opt;
opt = (influxdb_opt *) palloc(sizeof(influxdb_opt));
memset(opt, 0, sizeof(influxdb_opt));
/*
* Extract options from FDW objects.
*/
PG_TRY();
{
f_table = GetForeignTable(foreignoid);
f_server = GetForeignServer(f_table->serverid);
}
PG_CATCH();
{
f_table = NULL;
f_server = GetForeignServer(foreignoid);
}
PG_END_TRY();
f_mapping = GetUserMapping(userid, f_server->serverid);
options = NIL;
if (f_table)
options = list_concat(options, f_table->options);
options = list_concat(options, f_server->options);
options = list_concat(options, f_mapping->options);
/* Loop through the options, and get the server/port */
foreach(lc, options)
{
DefElem *def = (DefElem *) lfirst(lc);
if (strcmp(def->defname, "table") == 0)
opt->svr_table = defGetString(def);
if (strcmp(def->defname, "host") == 0)
opt->svr_address = defGetString(def);
if (strcmp(def->defname, "port") == 0)
opt->svr_port = atoi(defGetString(def));
if (strcmp(def->defname, "user") == 0)
opt->svr_username = defGetString(def);
if (strcmp(def->defname, "password") == 0)
opt->svr_password = defGetString(def);
if (strcmp(def->defname, "dbname") == 0)
opt->svr_database = defGetString(def);
if (strcmp(def->defname, "table_name") == 0)
opt->svr_table = defGetString(def);
if (strcmp(def->defname, "tags") == 0)
opt->tags_list = influxdbExtractTagsList(defGetString(def));
if (strcmp(def->defname, "schemaless") == 0)
opt->schemaless = defGetBoolean(def);
#ifdef CXX_CLIENT
if (strcmp(def->defname, "auth_token") == 0)
opt->svr_token = defGetString(def);
if (strcmp(def->defname, "version") == 0)
opt->svr_version = atoi(defGetString(def));
if (strcmp(def->defname, "retention_policy") == 0)
opt->svr_retention_policy = defGetString(def);
#endif
}
if (!opt->svr_table && f_table)
opt->svr_table = get_rel_name(foreignoid);
#ifdef CXX_CLIENT
/* When using the influxdb-cxx API client, c++ not allow std::string(NULL). */
if (opt->svr_address == NULL)
elog(ERROR, "influxdb_fdw: Server Host not specified");
if (opt->svr_database == NULL || strcmp(opt->svr_database, "") == 0)
elog(ERROR, "influxdb_fdw: Database not specified");
if (opt->svr_username == NULL)
opt->svr_username = "";
if (opt->svr_password == NULL)
opt->svr_password = "";
if (opt->svr_token == NULL)
opt->svr_token = "";
if (opt->svr_retention_policy == NULL)
opt->svr_retention_policy = "";
#endif
return opt;
}
#ifdef CXX_CLIENT
/*
* Fetch the version options for a influxdb_fdw foreign table.
*/
int
influxdb_get_version_option(influxdb_opt *opt)
{
/* determine which version is connected to */
return check_connected_influxdb_version(opt->svr_address, opt->svr_port, opt->svr_username, opt->svr_password, opt->svr_database,
opt->svr_token, opt->svr_retention_policy);
}
#endif
/*
* Parse a comma-separated string and return a list of tag keys of a foreign table.
*/
static List *
influxdbExtractTagsList(char *in_string)
{
List *tags_list = NIL;
/* SplitIdentifierString scribbles on its input, so pstrdup first */
if (!SplitIdentifierString(pstrdup(in_string), ',', &tags_list))
{
/* Syntax error in tags list */
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("parameter \"%s\" must be a list of tag keys",
"tags")));
}
return tags_list;
}