/
plr.h
executable file
·369 lines (323 loc) · 10.1 KB
/
plr.h
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
/*
* PL/R - PostgreSQL support for R as a
* procedural language (PL)
*
* Copyright (c) 2003 by Joseph E. Conway
* ALL RIGHTS RESERVED
*
* Joe Conway <mail@joeconway.com>
*
* Based on pltcl by Jan Wieck
* and inspired by REmbeddedPostgres by
* Duncan Temple Lang <duncan@research.bell-labs.com>
* http://www.omegahat.org/RSPostgres/
*
* License: GPL version 2 or newer. http://www.gnu.org/copyleft/gpl.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* plr.h
*/
#ifndef PLR_H
#define PLR_H
#include <unistd.h>
#include <fcntl.h>
#include <setjmp.h>
#include <sys/stat.h>
#define ELOG_H
#include "postgres.h"
#include "access/heapam.h"
#include "catalog/catversion.h"
#include "catalog/pg_language.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "lib/stringinfo.h"
#include "nodes/makefuncs.h"
#include "parser/parse_type.h"
#include "tcop/tcopprot.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "fmgr.h"
#include "R.h"
#include "Rinternals.h"
#include "Rdefines.h"
#ifdef ERROR
#undef ERROR
#endif
#ifdef WARNING
#undef WARNING
#endif
#undef ELOG_H
#include "utils/elog.h"
/* working with postgres 7.3 compatible sources */
#if (CATALOG_VERSION_NO < 200303091)
#define PG_VERSION_73_COMPAT
#endif
#ifdef DEBUGPROTECT
#undef PROTECT
#define PROTECT(s) \
do { \
elog(NOTICE, "\tPROTECT\t1\t%s\t%d", __FILE__, __LINE__); \
protect(s); \
} while (0)
#undef UNPROTECT
#define UNPROTECT(n) \
do { \
elog(NOTICE, "\tUNPROTECT\t%d\t%s\t%d", n, __FILE__, __LINE__); \
unprotect(n); \
} while (0)
#endif /* DEBUGPROTECT */
#define xpfree(var_) \
do { \
if (var_ != NULL) \
{ \
pfree(var_); \
var_ = NULL; \
} \
} while (0)
#define freeStringInfo(mystr_) \
do { \
xpfree((mystr_)->data); \
xpfree(mystr_); \
} while (0)
#define resetStringInfo(mystr_) \
do { \
xpfree((mystr_)->data); \
initStringInfo(mystr_); \
} while (0)
#define NEXT_STR_ELEMENT " %s"
/*
* See the no-exported header file ${R_HOME}/src/include/Parse.h
*/
extern SEXP R_ParseVector(SEXP, int, int *);
#define PARSE_NULL 0
#define PARSE_OK 1
#define PARSE_INCOMPLETE 2
#define PARSE_ERROR 3
#define PARSE_EOF 4
/* convert C string to text pointer */
#define PG_TEXT_GET_STR(textp_) \
DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp_)))
#define PG_STR_GET_TEXT(str_) \
DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(str_)))
#define PG_REPLACE_STR(str_, substr_, replacestr_) \
PG_TEXT_GET_STR(DirectFunctionCall3(replace_text, \
PG_STR_GET_TEXT(str_), \
PG_STR_GET_TEXT(substr_), \
PG_STR_GET_TEXT(replacestr_)))
#define CHECK_RETURN_PSEUDOTYPES \
do { \
if (typeStruct->typtype == 'p') \
{ \
if (procStruct->prorettype == VOIDOID || \
procStruct->prorettype == RECORDOID) \
/* okay */ ; \
else if (procStruct->prorettype == TRIGGEROID) \
{ \
xpfree(prodesc->proname); \
xpfree(prodesc); \
elog(ERROR, "plr functions cannot return type %s" \
"\n\texcept when used as triggers", \
format_type_be(procStruct->prorettype)); \
} \
else \
{ \
xpfree(prodesc->proname); \
xpfree(prodesc); \
elog(ERROR, "plr functions cannot return type %s", \
format_type_be(procStruct->prorettype)); \
} \
} \
} while (0)
#define CHECK_ARG_PSEUDOTYPES \
do { \
if (typeStruct->typtype == 'p') \
{ \
xpfree(prodesc->proname); \
xpfree(prodesc); \
elog(ERROR, "plr functions cannot take type %s", \
format_type_be(arg_typid[i])); \
} \
} while (0)
#ifdef PG_VERSION_73_COMPAT
/*************************************************************************
* working with postgres 7.3 compatible sources
*************************************************************************/
#define INIT_FINFO_FUNCEXPR
#define TUPLESTORE_BEGIN_HEAP tuplestore_begin_heap(true, SortMem)
#define GET_PRORETTYPE(prorettype_) \
do { \
prorettype_ = procStruct->prorettype; \
} while (0)
#define GET_PROARGS(pronargs_, proargtypes_) \
do { \
int i; \
pronargs_ = procStruct->pronargs; \
for (i = 0; i < pronargs_; i++) \
proargtypes_[i] = procStruct->proargtypes[i]; \
} while (0)
#define CHECK_POLYMORPHIC_TYPES
#define INIT_AUX_FMGR_ATTS \
do { \
finfo->fn_mcxt = QueryContext; \
} while (0)
#define palloc0(sz_) palloc(sz_)
#else
/*************************************************************************
* working with postgres 7.4 compatible sources
*************************************************************************/
#define TUPLESTORE_BEGIN_HEAP tuplestore_begin_heap(true, false, SortMem)
#define GET_PRORETTYPE(prorettype_) \
do { \
if (procStruct->prorettype == ANYARRAYOID || \
procStruct->prorettype == ANYELEMENTOID) \
{ \
prorettype_ = get_fn_expr_rettype(fcinfo->flinfo); \
if (prorettype_ == InvalidOid) \
prorettype_ = procStruct->prorettype; \
} \
else \
prorettype_ = procStruct->prorettype; \
} while (0)
#define GET_PROARGS(pronargs_, proargtypes_) \
do { \
int i; \
pronargs_ = procStruct->pronargs; \
for (i = 0; i < pronargs_; i++) \
{ \
if (procStruct->proargtypes[i] == ANYARRAYOID || \
procStruct->proargtypes[i] == ANYELEMENTOID) \
{ \
proargtypes_[i] = get_fn_expr_argtype(fcinfo->flinfo, i); \
if (proargtypes_[i] == InvalidOid) \
proargtypes_[i] = procStruct->proargtypes[i]; \
} \
else \
proargtypes_[i] = procStruct->proargtypes[i]; \
} \
} while (0)
#define CHECK_POLYMORPHIC_TYPES \
do { \
if (uptodate) \
uptodate = (result_typid == prodesc->result_typid); \
if (uptodate) \
{ \
int i; \
for (i = 0; i < nargs; i++) \
{ \
if (arg_typid[i] != prodesc->arg_typid[i]) \
{ \
uptodate = false; \
break; \
} \
} \
} \
} while (0)
#define INIT_AUX_FMGR_ATTS \
do { \
finfo->fn_mcxt = QueryContext; \
finfo->fn_expr = (Node *) NULL; \
} while (0)
#endif /* PG_VERSION_73_COMPAT */
/*
* structs
*/
/* The information we cache about loaded procedures */
typedef struct plr_proc_desc
{
char *proname;
TransactionId fn_xmin;
CommandId fn_cmin;
bool lanpltrusted;
Oid result_typid;
bool result_istuple;
FmgrInfo result_in_func;
Oid result_elem;
FmgrInfo result_elem_in_func;
int result_elem_typlen;
bool result_elem_typbyval;
char result_elem_typalign;
int nargs;
Oid arg_typid[FUNC_MAX_ARGS];
FmgrInfo arg_out_func[FUNC_MAX_ARGS];
Oid arg_elem[FUNC_MAX_ARGS];
FmgrInfo arg_elem_out_func[FUNC_MAX_ARGS];
int arg_elem_typlen[FUNC_MAX_ARGS];
bool arg_elem_typbyval[FUNC_MAX_ARGS];
char arg_elem_typalign[FUNC_MAX_ARGS];
int arg_is_rel[FUNC_MAX_ARGS];
SEXP fun; /* compiled R function */
} plr_proc_desc;
/*
* external declarations
*/
/* libR interpreter initialization */
extern int Rf_initEmbeddedR(int argc, char **argv);
/* PL/R language handler */
extern Datum plr_call_handler(PG_FUNCTION_ARGS);
extern void start_interp(void);
extern void plr_init_load_modules(MemoryContext plr_SPI_context);
extern void load_r_cmd(const char *cmd);
extern SEXP call_r_func(SEXP fun, SEXP rargs);
/* argument and return value conversion functions */
extern SEXP pg_scalar_get_r(Datum dvalue, Oid arg_typid, FmgrInfo arg_out_func);
extern SEXP pg_array_get_r(Datum dvalue, FmgrInfo out_func, int typlen, bool typbyval, char typalign);
extern SEXP pg_tuple_get_r_frame(int ntuples, HeapTuple *tuples, TupleDesc tupdesc);
extern Datum r_get_pg(SEXP rval, plr_proc_desc *prodesc, FunctionCallInfo fcinfo);
extern Datum get_scalar_datum(SEXP rval, FmgrInfo result_in_func, Oid result_elem, bool *isnull);
/* Postgres support functions installed into the R interpreter */
extern void throw_pg_notice(const char **msg);
extern void throw_pg_error(const char **msg);
extern SEXP plr_quote_literal(SEXP rawstr);
extern SEXP plr_quote_ident(SEXP rawstr);
extern SEXP plr_SPI_exec(SEXP rsql);
extern SEXP plr_SPI_prepare(SEXP rsql, SEXP rargtypes);
extern SEXP plr_SPI_execp(SEXP rsaved_plan, SEXP rargvalues);
extern SEXP plr_SPI_lastoid(void);
/* Postgres callable functions useful in conjunction with PL/R */
extern Datum reload_plr_modules(PG_FUNCTION_ARGS);
extern Datum install_rcmd(PG_FUNCTION_ARGS);
extern Datum plr_array_push(PG_FUNCTION_ARGS);
extern Datum plr_array(PG_FUNCTION_ARGS);
extern Datum plr_array_accum(PG_FUNCTION_ARGS);
extern Datum plr_environ(PG_FUNCTION_ARGS);
/* Postgres backend support functions */
extern bool isArrayTypeName(const char *typeName);
extern char *get_load_self_ref_cmd(Oid funcid);
extern void perm_fmgr_info(Oid functionId, FmgrInfo *finfo);
extern void system_cache_lookup(Oid element_type, bool input, int *typlen,
bool *typbyval, char *typdelim, Oid *typelem,
Oid *proc, char *typalign);
extern Oid get_typelem(Oid element_type);
/*
* See the no-exported header file ${R_HOME}/src/include/Defn.h
*/
extern void R_PreserveObject(SEXP);
extern void R_ReleaseObject(SEXP);
#ifdef PG_VERSION_73_COMPAT
/*************************************************************************
* working with postgres 7.3 compatible sources
*************************************************************************/
/* these are in the backend in 7.4 sources */
extern ArrayType *construct_md_array(Datum *elems, int ndims, int *dims,
int *lbs, Oid elmtype, int elmlen,
bool elmbyval, char elmalign);
extern Oid get_fn_expr_rettype(FunctionCallInfo fcinfo);
#endif /* PG_VERSION_73_COMPAT */
#endif /* PLR_H */