-
Notifications
You must be signed in to change notification settings - Fork 28
/
parquet_s3_fdw_server_option.c
160 lines (136 loc) · 4.06 KB
/
parquet_s3_fdw_server_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
/*-------------------------------------------------------------------------
*
* parquet_s3_fdw_server_option.c
* Server option management for parquet_s3_fdw
*
* Portions Copyright (c) 2020, TOSHIBA CORPORATION
*
* IDENTIFICATION
* contrib/parquet_s3_fdw/parquet_s3_fdw_server_option.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "parquet_s3_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 "foreign/foreign.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
/*
* Check if the provided option is one of the valid options.
*/
bool
parquet_s3_is_valid_server_option(DefElem *def)
{
if (strcmp(def->defname, SERVER_OPTION_USE_MINIO) == 0 ||
strcmp(def->defname, SERVER_OPTION_KEEP_CONNECTIONS) == 0)
{
/* Check that bool value is valid */
bool check_bool_valid;
if (!parse_bool(defGetString(def), &check_bool_valid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("parquet_s3_fdw: invalid value for boolean option \"%s\": %s",
def->defname, defGetString(def))));
return true;
}
if (strcmp(def->defname, SERVER_OPTION_REGION) == 0 ||
strcmp(def->defname, SERVER_OPTION_ENDPOINT) == 0)
{
return true;
}
return false;
}
/*
* Extract listed option information into parquet_s3_server_opt structure.
*/
static void
parquet_s3_extract_options(List *options, parquet_s3_server_opt * opt)
{
ListCell *lc;
/* Loop through the options. */
foreach(lc, options)
{
DefElem *def = (DefElem *) lfirst(lc);
if (strcmp(def->defname, SERVER_OPTION_USE_MINIO) == 0)
opt->use_minio = defGetBoolean(def);
else if (strcmp(def->defname, SERVER_OPTION_KEEP_CONNECTIONS) == 0)
opt->keep_connections = defGetBoolean(def);
else if (strcmp(def->defname, SERVER_OPTION_REGION) == 0)
opt->region = defGetString(def);
else if (strcmp(def->defname, SERVER_OPTION_ENDPOINT) == 0)
opt->endpoint = defGetString(def);
}
}
/*
* Fetch the options for a parquet_s3_fdw foreign table.
*/
parquet_s3_server_opt *
parquet_s3_get_options(Oid foreignoid, Oid userid)
{
ForeignTable *f_table = NULL;
ForeignServer *f_server = NULL;
UserMapping *f_mapping;
List *options;
parquet_s3_server_opt *opt;
opt = (parquet_s3_server_opt *) palloc(sizeof(parquet_s3_server_opt));
memset(opt, 0, sizeof(parquet_s3_server_opt));
/* Set default value. */
opt->use_minio = false;
/* By default, all the connections to any foreign servers are kept open. */
opt->keep_connections = true;
opt->region = "ap-northeast-1";
opt->endpoint = "127.0.0.1:9000";
/*
* 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);
/* Store option information into the structure. */
parquet_s3_extract_options(options, opt);
return opt;
}
/*
* Fetch the options for a parquet_s3_fdw foreign server.
*/
parquet_s3_server_opt *
parquet_s3_get_server_options(Oid serverid)
{
ForeignServer *f_server = NULL;
List *options;
parquet_s3_server_opt *opt;
opt = (parquet_s3_server_opt *) palloc(sizeof(parquet_s3_server_opt));
memset(opt, 0, sizeof(parquet_s3_server_opt));
/* Set default value. */
opt->use_minio = false;
/* By default, all the connections to any foreign servers are kept open. */
opt->keep_connections = true;
opt->region = "ap-northeast-1";
opt->endpoint = "127.0.0.1:9000";
/* Get server options. */
f_server = GetForeignServer(serverid);
options = f_server->options;
/* Store option information into the structure. */
parquet_s3_extract_options(options, opt);
return opt;
}