/
plparrot.c
271 lines (230 loc) · 8.25 KB
/
plparrot.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
/* Parrot header files */
#include "parrot/embed.h"
#include "parrot/extend.h"
#include "parrot/imcc.h"
#include "parrot/extend_vtable.h"
#include "include/embed_string.h"
/* Postgres header files */
#include "postgres.h"
#include "access/heapam.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "commands/trigger.h"
#include "funcapi.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "parser/parse_type.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
#ifdef TextDatumGetCString
#define TextDatum2String(x) (pstrdup(TextDatumGetCString(x)))
#else
/* For PostgreSQL versions 8.3 and prior */
#define TextDatum2String(X) (pstrdup(DatumGetCString(DirectFunctionCall1(textout, (X)))))
#endif
/**********************************************************************
* The information we cache about loaded procedures
**********************************************************************/
typedef struct plparrot_proc_desc
{
char *proname; /* user name of procedure */
TransactionId fn_xmin;
ItemPointerData fn_tid;
bool fn_readonly;
bool lanpltrusted;
bool fn_retistuple; /* true, if function returns tuple */
bool fn_retisset; /* true, if function returns set */
bool fn_retisarray; /* true if function returns array */
Oid result_oid; /* Oid of result type */
FmgrInfo result_in_func; /* I/O function and arg for result type */
Oid result_typioparam;
int nargs;
FmgrInfo arg_out_func[FUNC_MAX_ARGS];
bool arg_is_rowtype[FUNC_MAX_ARGS];
/* SV *reference; */
} plparrot_proc_desc;
/*
* The information we cache for the duration of a single call to a
* function.
*/
typedef struct plparrot_call_data
{
plparrot_proc_desc *prodesc;
FunctionCallInfo fcinfo;
Tuplestorestate *tuple_store;
TupleDesc ret_tdesc;
AttInMetadata *attinmeta;
MemoryContext tmp_cxt;
} plparrot_call_data;
Parrot_Interp interp;
/* Helper functions */
Parrot_String create_string(Parrot_Interp interp, const char *name);
Parrot_PMC create_pmc(Parrot_Interp interp, const char *name);
void dump_pmc(Parrot_Interp interp, Parrot_PMC pmc);
/* this is saved and restored by plparrot_call_handler */
static plparrot_call_data *current_call_data = NULL;
/* Be sure we do initialization only once */
static bool inited = false;
void _PG_init(void);
void _PG_fini(void);
void
_PG_init(void)
{
if (inited)
return;
interp = Parrot_new(NULL);
imcc_initialize(interp);
if (!interp) {
elog(ERROR,"Could not create a Parrot interpreter!\n");
return;
}
inited = true;
}
/*
* Per PostgreSQL 9.0 documentation, _PG_fini only gets called when a module
* is un-loaded, which isn't yet supported. But I'm putting this here for good
* measure, anyway
*/
void
_PG_fini(void)
{
Parrot_destroy(interp);
inited = false;
}
Datum plparrot_call_handler(PG_FUNCTION_ARGS);
static Datum plparrot_func_handler(PG_FUNCTION_ARGS);
/* The PostgreSQL function+trigger managers call this function for execution
of PL/Parrot procedures. */
PG_FUNCTION_INFO_V1(plparrot_call_handler);
/*
* The PostgreSQL function+trigger managers call this function for execution of
* PL/Parrot procedures.
*/
static Datum
plparrot_func_handler(PG_FUNCTION_ARGS)
{
Parrot_PMC func_pmc, func_args, result;
Parrot_String err;
Datum retval, procsrc_datum, element;
Form_pg_proc procstruct;
HeapTuple proctup;
char *proc_src, *errmsg, *tmp;
Oid returntype, *argtypes;
int numargs, rc, i;
char **argnames, *argmodes;
bool isnull;
int16 typlen;
bool typbyval;
char typalign;
/* we actually need an array of element types, one for each arg */
Oid element_type = get_fn_expr_argtype(fcinfo->flinfo, 0);
if ((rc = SPI_connect()) != SPI_OK_CONNECT)
elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
retval = PG_GETARG_DATUM(0);
proctup = SearchSysCache(PROCOID, ObjectIdGetDatum(fcinfo->flinfo->fn_oid), 0, 0, 0);
if (!HeapTupleIsValid(proctup))
elog(ERROR, "Failed to look up procedure with OID %u", fcinfo->flinfo->fn_oid);
procstruct = (Form_pg_proc) GETSTRUCT(proctup);
returntype = procstruct->prorettype;
procsrc_datum = SysCacheGetAttr(PROCOID, proctup, Anum_pg_proc_prosrc, &isnull);
numargs = get_func_arg_info(proctup, &argtypes, &argnames, &argmodes);
if (numargs && !OidIsValid(element_type))
elog(ERROR, "could not determine data type of input");
if (numargs)
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
// elog(NOTICE,"element_type = %u", element_type);
if (isnull)
elog(ERROR, "Couldn't load function source for function with OID %u", fcinfo->flinfo->fn_oid);
/* procstruct probably isn't valid after this ReleaseSysCache call, so don't use it anymore */
ReleaseSysCache(proctup);
proc_src = TextDatum2String(procsrc_datum);
// elog(NOTICE,"about to compile a PIR string: %s", proc_src);
/* Our current plan of attack is the pass along a ResizablePMCArray to all stored procedures */
func_pmc = Parrot_compile_string(interp, create_string(interp, "PIR"), proc_src, &err);
func_args = create_pmc(interp,"ResizablePMCArray");
/* TODO: fill func_args with PG_FUNCTION_ARGS */
/* TODO: correctly convert between various int + float types */
for (i = 0; i < numargs; i++) {
if (element_type == TEXTOID) {
Parrot_PMC_push_string(interp, func_args, create_string(interp, PG_GETARG_DATUM(i)));
} else if (element_type == INT4OID) {
Parrot_PMC_push_integer(interp, func_args, (Parrot_Int) PG_GETARG_DATUM(i));
} else if (element_type == FLOAT8OID) {
Parrot_PMC_push_float(interp, func_args, (Parrot_Float) PG_GETARG_DATUM(i));
}
}
// elog(NOTICE,"compiled a PIR string");
if (!STRING_is_null(interp, err)) {
// elog(NOTICE,"got an error compiling PIR string");
tmp = Parrot_str_to_cstring(interp, err);
errmsg = pstrdup(tmp);
// elog(NOTICE,"about to free parrot cstring");
Parrot_str_free_cstring(tmp);
elog(ERROR, "Error compiling PIR function");
}
// elog(NOTICE,"about to call compiled PIR string with Parrot_ext_call");
/* See Parrot's src/extend.c for interpretations of the third argument */
/* Pf => PMC with :flat attribute */
/* Return value of the function call is stored in result */
Parrot_ext_call(interp, func_pmc, "Pf", func_args, &result);
// This just coredumps
//dump_pmc(interp,result);
if ((rc = SPI_finish()) != SPI_OK_FINISH)
elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
return retval;
}
Datum
plparrot_call_handler(PG_FUNCTION_ARGS)
{
Datum retval;
plparrot_call_data *save_call_data = current_call_data;
//elog(NOTICE,"enter plparrot_call_handler");
//elog(NOTICE,"entering PG_TRY");
PG_TRY();
{
if (CALLED_AS_TRIGGER(fcinfo)) {
TriggerData *tdata = (TriggerData *) fcinfo->context;
/* we need a trigger handler */
} else {
retval = plparrot_func_handler(fcinfo);
}
}
PG_CATCH();
{
current_call_data = save_call_data;
PG_RE_THROW();
}
PG_END_TRY();
current_call_data = save_call_data;
return retval;
}
Parrot_PMC create_pmc(Parrot_Interp interp, const char *name)
{
return Parrot_PMC_new(interp,Parrot_PMC_typenum(interp,name));
}
Parrot_String create_string(Parrot_Interp interp, const char *name)
{
return Parrot_new_string(interp, name, strlen(name), (const char *) NULL, 0);
}
/* It would be nice to have a function that dumps the contents of a ResizablePMCArray */
/* TODO: Make me work */
void
dump_pmc(Parrot_Interp interp, Parrot_PMC pmc)
{
Parrot_String str;
Parrot_PMC tmp_pmc;
char *string;
tmp_pmc = Parrot_PMC_get_pmc_keyed(interp,pmc,0);
string = Parrot_PMC_get_string(interp,tmp_pmc);
elog(NOTICE, "PMC = %s", string);
}