-
Notifications
You must be signed in to change notification settings - Fork 3
/
foreign_table_exposer.c
236 lines (210 loc) · 6.99 KB
/
foreign_table_exposer.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
#include "postgres.h"
#include <math.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/hash.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "parser/analyze.h"
#include "parser/parsetree.h"
#include "parser/scanner.h"
#include "pgstat.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/spin.h"
#include "tcop/utility.h"
#include "utils/rel.h"
#include "utils/relcache.h"
#include "utils/syscache.h"
#include "utils/elog.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_type.h"
#include "access/htup_details.h"
#include "nodes/nodes.h"
#include "nodes/pg_list.h"
#include "nodes/nodeFuncs.h"
#include "nodes/makefuncs.h"
#define PG_CATALOG "pg_catalog"
#define PG_CLASS "pg_class"
#define COL_RELKIND "relkind"
PG_MODULE_MAGIC;
/* Saved hook values in case of unload */
static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
/*---- Function declarations ----*/
void _PG_init(void);
void _PG_fini(void);
static void fte_post_parse_analyse(ParseState *pstate, Query *query);
static void rewrite_query(Query *query);
static bool walker(Node *node, List *relkinds);
/*
* Module load callback
*/
void
_PG_init(void)
{
/*
* Install hooks.
*/
prev_post_parse_analyze_hook = post_parse_analyze_hook;
post_parse_analyze_hook = fte_post_parse_analyse;
}
/*
* Module unload callback
*/
void
_PG_fini(void)
{
/* Uninstall hooks. */
post_parse_analyze_hook = prev_post_parse_analyze_hook;
}
/*
* Post-parse-analysis hook: mark query with a queryId
*/
static void
fte_post_parse_analyse(ParseState *pstate, Query *query)
{
if (prev_post_parse_analyze_hook)
{
prev_post_parse_analyze_hook(pstate, query);
}
rewrite_query(query);
}
typedef struct PgClassRelKindPos
{
Index varno;
AttrNumber varattno;
} PgClassRelKindPos;
static void
rewrite_query(Query *query)
{
int varno_index;
ListCell *lc_rtable;
List *relkinds = NIL;
if (query->type != T_Query || query->commandType != CMD_SELECT || query->querySource != QSRC_ORIGINAL)
{
return;
}
varno_index = 1;
foreach(lc_rtable, query->rtable)
{
bool is_pg_class = false;
Relation rd;
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc_rtable);
rd = RelationIdGetRelation(rte->relid);
if (rd != NULL && rd->rd_rel != NULL)
{
HeapTuple tp = SearchSysCache1(NAMESPACEOID, ObjectIdGetDatum(rd->rd_rel->relnamespace));
if (HeapTupleIsValid(tp))
{
Form_pg_namespace nsptup = (Form_pg_namespace) GETSTRUCT(tp);
if (nsptup != NULL)
{
if (strcmp(NameStr(nsptup->nspname), PG_CATALOG) == 0 &&
strcmp(NameStr(rd->rd_rel->relname), PG_CLASS) == 0)
{
ereport(DEBUG1, (errmsg("Found pg_catalog.pg_class")));
is_pg_class = true;
}
}
ReleaseSysCache(tp);
}
RelationClose(rd);
}
if (is_pg_class && rte->eref != NULL) {
int varattrno_index = 1;
ListCell *lc_colname;
foreach(lc_colname, rte->eref->colnames)
{
char *colname = strVal(lfirst(lc_colname));
if (strcmp(colname, COL_RELKIND) == 0)
{
ereport(DEBUG1, (errmsg("Found pg_catalog.pg_class.relkind: [varno:%d, varattno:%d]", varno_index, varattrno_index)));
PgClassRelKindPos *relkindPos = (PgClassRelKindPos*) palloc(sizeof(PgClassRelKindPos));
relkindPos->varno = varno_index;
relkindPos->varattno = varattrno_index;
relkinds = lappend(relkinds, relkindPos);
}
varattrno_index++;
}
}
/* TODO: Take care of subquery properly */
if (rte->subquery != NULL)
{
rewrite_query(rte->subquery);
}
varno_index++;
}
if (relkinds != NIL && relkinds->length > 0 && query->jointree != NULL)
{
FromExpr *fromExpr = query->jointree;
expression_tree_walker(fromExpr->quals, walker, relkinds);
}
}
static
bool walker(Node *node, List *relkinds)
{
if (node == NULL)
{
return false;
}
if (IsA(node, OpExpr))
{
/* TODO */
}
else if (IsA(node, ScalarArrayOpExpr))
{
ScalarArrayOpExpr *arrayOpExpr = (ScalarArrayOpExpr*) node;
if (arrayOpExpr->args->length == 2 && IsA(list_nth(arrayOpExpr->args, 0), Var) && IsA(list_nth(arrayOpExpr->args, 1), ArrayExpr))
{
ListCell *lc_relkind;
Var *var = (Var*) list_nth(arrayOpExpr->args, 0);
ArrayExpr *array = (ArrayExpr*) list_nth(arrayOpExpr->args, 1);
foreach(lc_relkind, relkinds)
{
PgClassRelKindPos *relkindPos = lfirst(lc_relkind);
if (var->varno == relkindPos->varno && var->varattno == relkindPos->varattno)
{
bool regularTableExists = false;
bool foreignTableExists = false;
ListCell *lc_elm;
foreach(lc_elm, array->elements)
{
Node *elm = (Node*)lfirst(lc_elm);
if (IsA(elm, Const))
{
Const *c = (Const*)elm;
if (c->constbyval && c->constlen == 1 && c->consttype == CHAROID)
{
switch (DatumGetChar(c->constvalue))
{
case RELKIND_RELATION:
regularTableExists = true;
break;
case RELKIND_FOREIGN_TABLE:
foreignTableExists = true;
break;
default:
break;
}
}
}
}
ereport(DEBUG1, (errmsg("regularTableExists=%d, foreignTableExists=%d", regularTableExists, foreignTableExists)));
if (regularTableExists && !foreignTableExists)
{
array->elements =
lappend(array->elements,
makeConst(CHAROID, -1, InvalidOid, 1, CharGetDatum(RELKIND_FOREIGN_TABLE), false, true));
}
}
}
}
else
{
ereport(WARNING, (errmsg("arrayOpExpr->args has unexpected values: %s", nodeToString(arrayOpExpr->args))));
}
}
return expression_tree_walker(node, walker, (void *) relkinds);
}