-
Notifications
You must be signed in to change notification settings - Fork 3
/
pg_jinx.c
521 lines (417 loc) · 18.8 KB
/
pg_jinx.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include <pg_jinx.h>
// extern void destroyJVM(int status, Datum dummy);
extern void initializeJVM(bool);
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(fdw_validator);
PG_FUNCTION_INFO_V1(fdw_handler);
PG_FUNCTION_INFO_V1(inline_handler);
extern Datum fdw_validator(PG_FUNCTION_ARGS);
extern Datum fdw_handler(PG_FUNCTION_ARGS);
extern Datum inline_handler(PG_FUNCTION_ARGS);
/* The only reason these are defined is to deal with upgrading from Transgres 2.3 to 2.4 -- when I changed the names
in pg_jinx. As I was the only user, it is unlikely that this needs to be retained long term
*/
PG_FUNCTION_INFO_V1(pg_jinx_fdw_validator);
PG_FUNCTION_INFO_V1(pg_jinx_fdw_handler);
PG_FUNCTION_INFO_V1(pg_jinx_inline_handler);
extern Datum pg_jinx_fdw_validator(PG_FUNCTION_ARGS);
extern Datum pg_jinx_fdw_handler(PG_FUNCTION_ARGS);
extern Datum pg_jinx_inline_handler(PG_FUNCTION_ARGS);
Datum pg_jinx_fdw_handler(PG_FUNCTION_ARGS) { return fdw_handler(fcinfo); }
Datum pg_jinx_fdw_validator(PG_FUNCTION_ARGS) { return fdw_validator(fcinfo); }
Datum pg_jinx_inline_handler(PG_FUNCTION_ARGS) { return inline_handler(fcinfo); }
// -----------------------------------------------------------------
// FOREIGN DATA WRAPPER, SERVER, USER MAPPING or FOREIGN TABLE options.
Datum fdw_validator(PG_FUNCTION_ARGS) {
// List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
Oid catalog = PG_GETARG_OID(1);
// char *jarfile = NULL;
// ListCell *cell;
// char *classname = NULL;
if (catalog == ForeignDataWrapperRelationId) {
PG_RETURN_VOID();
};
/* foreach(cell, options_list) {
DefElem *def = (DefElem *) lfirst(cell);
if (strcmp(def->defname, "javaclass") == 0) {
if (catalog == ForeignTableRelationId) {
ereport(ERROR, (errmsg("%s", "can only set the javaclass on the server")));
} else {
classname = (char *) defGetString(def);
}
} else if (strcmp(def->defname, "jarfile") == 0) {
if (catalog == ForeignTableRelationId) {
ereport(ERROR, (errmsg("%s", "can only set the jarfile on the server")));
} else {
jarfile = (char *) defGetString(def);
}
}
}
if (catalog == ForeignServerRelationId) {
if (classname == NULL || jarfile == NULL) {
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), (errmsg("%s", "jarfile and javaclass parameters are mandatory"))));
}
}
*/
PG_RETURN_VOID();
/* if (jarfile == NULL) {
ereport(ERROR, (errmsg("%s", "jarfile not set")));
}
*/
}
// boilerplate
Datum fdw_handler(PG_FUNCTION_ARGS) {
FdwRoutine *fdwroutine = makeNode(FdwRoutine);
#if (PG_VERSION_NUM < 90200)
fdwroutine->PlanForeignScan = javaPlanForeignScan;
#endif
#if (PG_VERSION_NUM >= 90200)
fdwroutine->GetForeignRelSize = javaGetForeignRelSize;
fdwroutine->GetForeignPaths = javaGetForeignPaths;
fdwroutine->GetForeignPlan = javaGetForeignPlan;
#endif
fdwroutine->ExplainForeignScan = javaExplainForeignScan;
fdwroutine->BeginForeignScan = javaBeginForeignScan;
fdwroutine->IterateForeignScan = javaIterateForeignScan;
fdwroutine->ReScanForeignScan = javaReScanForeignScan;
fdwroutine->EndForeignScan = javaEndForeignScan;
fdwroutine->AddForeignUpdateTargets = javaAddForeignUpdateTargets;
fdwroutine->PlanForeignModify = javaPlanForeignModify;
fdwroutine->BeginForeignModify = javaBeginForeignModify;
fdwroutine->ExecForeignInsert = javaExecForeignInsert;
fdwroutine->ExecForeignUpdate = javaExecForeignUpdate;
fdwroutine->ExecForeignDelete = javaExecForeignDelete;
fdwroutine->EndForeignModify = javaEndForeignModify;
PG_RETURN_POINTER(fdwroutine);
}
extern Datum inline_handler(PG_FUNCTION_ARGS);
Datum inline_handler(PG_FUNCTION_ARGS) {
InlineCodeBlock *codeblock = (InlineCodeBlock *) DatumGetPointer(PG_GETARG_DATUM(0));
int argc;
char * arguments[FUNC_MAX_ARGS + 2];
const char *rest;
char *tempfile;
HeapTuple procTup;
Form_pg_proc procStruct;
Oid foid;
jboolean multicall;
/*if(s_javaVM == NULL) */ initializeJVM(false); // trusted
/* foid = fcinfo->flinfo->fn_oid; */
jobject fn = (*env)->CallStaticObjectMethod(env, functionClass, functionConstructor, 0 );
/*
procTup = SearchSysCache(PROCOID, ObjectIdGetDatum(foid), 0, 0, 0);
if(!HeapTupleIsValid(procTup))
ereport(ERROR, (errmsg("cache lookup failed for function (PROCOID) %u", foid)));
procStruct = (Form_pg_proc)GETSTRUCT(procTup);
multicall = procStruct->proretset;
// rettype = get_fn_expr_rettype(fcinfo->flinfo);
Oid rettype = procStruct->prorettype;
ReleaseSysCache(procTup);
*/
jobject src = (*env)->NewStringUTF(env, codeblock->source_text);
jarray paramNames = (*env)->NewObjectArray(env, 0, stringClass, NULL);
jarray argt = (*env)->NewIntArray(env, 0);
// jobject result = (*env)->CallObjectMethod(env, fn, functionInvoke, name, paramNames, argt, rettype, args);
jobject result = (*env)->CallObjectMethod(env, fn, functionInvoke, src, paramNames, argt, NULL, paramNames);
check_exception("%s\ncalling inline method: %s", codeblock->source_text);
PG_RETURN_VOID();
}
// end boilerplate for FDW
/*static Datum internalCallHandler(bool trusted, PG_FUNCTION_ARGS) {
Function function;
initializeJavaVM(trusted);
function = Function_getFunction(fcinfo);
return (CALLED_AS_TRIGGER(fcinfo)) ? Function_invokeTrigger(function, fcinfo) // Called as a trigger
: Function_invoke(function, fcinfo);
}
*/
extern JavaVM *s_javaVM;
extern Datum internalCallHandler(bool trusted, PG_FUNCTION_ARGS);
HeapTuple handleRecordD(jarray result, TupleDesc tupdesc) {
AttInMetadata *attinmeta = TupleDescGetAttInMetadata(tupdesc);
int natts = tupdesc->natts;
int i;
// SIGINTInterruptCheckProcess(state);
// java_rowarray = (*env)->CallObjectMethod(env, state -> java_call, state-> iterateForeignScan);
// CHECK_EXCEPTION("%s\ncalling iterateForeignScan","unused");
if (result == NULL) return 0;
Datum *dvalues;
bool *nulls;
Oid *oids;
int colcount = objectArrayToDatumArray(result, &oids, &dvalues, &nulls);
if (colcount != natts) {
ereport(ERROR, (errcode(ERRCODE_TOO_MANY_COLUMNS), (errmsg("returned array had %d elements, should have been %d",colcount, natts))));
}
for (i = 0; i < natts; i++) {
if (tupdesc->attrs[i]->attisdropped) continue;
if (nulls[i]) continue;
Oid toid = oids[i];
switch(tupdesc->attrs[i]->atttypid) {
case INT4OID:
if (toid == INT4OID);
else ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE), (errmsg("expecting INT4OID did not receive java integer"))));
break;
case FLOAT8OID:
if (toid == FLOAT8OID);
else ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE), (errmsg("expecting FLOAT8OID did not receive java double"))));
break;
case FLOAT4OID:
if (toid == FLOAT4OID);
else ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE), (errmsg("expecting FLOAT4OID did not receive java float"))));
break;
case INT2OID:
if (toid == INT2OID);
else ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE), (errmsg("expecting INT2OID did not receive java short"))));
break;
case INT8OID:
if (toid == INT8OID);
else ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE), (errmsg("expecting INT8OID did not receive java long"))));
break;
case TEXTOID:
case VARCHAROID:
if (toid == TEXTOID);
else ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE), (errmsg("expecting TEXTOID did not receive java string"))));
// dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i], js, attinmeta->attioparams[i], attinmeta->atttypmods[i]);
break;
case BOOLOID:
if (toid == BOOLOID);
else ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE), (errmsg("expecting BOOLOID did not receive java boolean"))));
break;
case BYTEAOID:
if (toid == BOOLOID);
else ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE), (errmsg("expecting BYTEAOID did not receive java byte array"))));
break;
default:
if (toid != tupdesc->attrs[i]->atttypid)
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE), (errmsg("unknown conversion for java to datum"))));
}
}
HeapTuple tuple = heap_form_tuple(tupdesc, dvalues, nulls);
pfree(nulls);
pfree(dvalues);
pfree(oids);
// pfree(dvalues); ??
// ExecStoreTuple(tuple, slot, InvalidBuffer, false);
return tuple;
}
static Datum handleRecord(jarray result, PG_FUNCTION_ARGS) {
TupleDesc tupdesc;
/// should I check that rsinfo->returnMode == SFRM_ValuePerCall
// should I test the "first call" SRF_IS_FIRSTCALL() --> fcinfo->flinfo->fn_extra == NULL
// caller with procStruct->proretset== true means multi-call
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
TypeFuncClass z = get_call_result_type(fcinfo, NULL, &tupdesc);
switch(z) {
case TYPEFUNC_OTHER: break; // is this for trigger?
case TYPEFUNC_COMPOSITE: break;
case TYPEFUNC_RECORD:
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("looking for a TYPEFUNC_COMPOSITE")));
break;
default:
ereport(ERROR, (errmsg("unknown record type")));
break;
}
HeapTuple ht = handleRecordD(result, tupdesc);
PG_RETURN_DATUM (HeapTupleGetDatum(ht));
}
static Oid resolveDomain(Oid type) {
HeapTuple typeTup = SearchSysCache(TYPEOID, ObjectIdGetDatum(type), 0, 0, 0);
if(!HeapTupleIsValid(typeTup)) {
ereport(ERROR, (errmsg("cache lookup failed for type %u", type)));
}
Form_pg_type typeStruct = (Form_pg_type)GETSTRUCT(typeTup);
if(typeStruct->typelem != 0 && typeStruct->typlen == -1) {
// type = Type_getArrayType(Type_fromOid(typeStruct->typelem, typeMap), typeId);
// looks like an Array of typelem -- do I need the resolve?
// Oid res = resolveOid(typeStruct->typelem) --> then turn this into an array
// return arrayOf(res)
}
// For some reason, the anyarray is *not* an array with anyelement as the element type.
if(type == ANYARRAYOID) {
// Oid res = ANYELEMENTOID;
// return arrayOf(res);
}
if(typeStruct->typbasetype != 0) {
// Domain type, recurse using the base type (which in turn may also be a domain)
Oid tbt = typeStruct->typbasetype;
ReleaseSysCache(typeTup);
return resolveDomain(tbt);
}
// Composite and record types will not have a TypeObtainer registered
// if(typeStruct->typtype == 'c' || (typeStruct->typtype == 'p' && typeId == RECORDOID)) {
// type = Composite_obtain(typeId);
// return
// }
/* ce = (CacheEntry)HashMap_getByOid(s_obtainerByOid, typeId);
if(ce == 0)
// Default to String and standard textin/textout coersion.
type = String_obtain(typeId);
else
{
type = ce->type;
if(type == 0)
type = ce->obtainer(typeId);
}
*/
ReleaseSysCache(typeTup);
return type;
}
static jobject callJavaMethod(jobject fn, HeapTuple procTup, PG_FUNCTION_ARGS) {
Oid *types;
char **names, *modes;
// look at how this is done in plpy_procedure.c (plpython)
int total = get_func_arg_info(procTup, &types, &names, &modes);
char **onames = names;
if (onames == NULL) names = palloc( total * sizeof(char *));
jarray paramNames = (*env)->NewObjectArray(env, total, stringClass, NULL);
char buf[100];
for(int i=0;i<total;i++) {
if (onames == NULL || names[i] == NULL) {
sprintf(buf, "$%d", i);
names[i]=buf;
}
(*env)->SetObjectArrayElement(env, paramNames, i, makeJavaString(names[i]));
}
bool isNull = false;
char *cname;
if (onames == NULL) pfree(names);
Datum tmp = SysCacheGetAttr(PROCOID, procTup, Anum_pg_proc_prosrc, &isNull);
if(isNull) { ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("'AS' clause of Java function cannot be NULL"))); }
jobject name = (*env)->NewStringUTF(env, cname = DatumGetCString(DirectFunctionCall1(textout, tmp)));
Form_pg_proc procStruct = (Form_pg_proc)GETSTRUCT(procTup);
jint top = procStruct->pronargs;
int rettype = procStruct->prorettype;
Oid *paramOids;
jarray argt = (*env)->NewIntArray(env, top);
if(top > 0) {
int idx;
jint *at;
paramOids = procStruct->proargtypes.values;
at = (*env)->GetIntArrayElements(env, argt, NULL);
for(idx = 0; idx < top; ++idx) at[idx] = resolveDomain(paramOids[idx]);
(*env)->ReleaseIntArrayElements(env,argt,at,0);
}
jarray args = (*env)->NewObjectArray(env, top, objClass, NULL); //
if(top > 0) {
int idx;
jobject value;
for(idx = 0; idx < top; ++idx) {
if(PG_ARGISNULL(idx)) continue;
value = datumToObject( resolveDomain(paramOids[idx]), PG_GETARG_DATUM(idx));
(*env)->SetObjectArrayElement(env, args, idx, value);
}
}
jobject result = (*env)->CallObjectMethod(env, fn, functionInvoke, name, paramNames, argt, rettype, args);
check_exception("%s\ncalling method %s", cname);
// return (CALLED_AS_TRIGGER(fcinfo)) ? Function_invokeTrigger(function, fcinfo) : Function_invoke(function, fcinfo);
return result;
}
static Datum startSRF(jobject fn, HeapTuple procTup, PG_FUNCTION_ARGS) {
FuncCallContext *context = SRF_FIRSTCALL_INIT();
jobject result = callJavaMethod(fn, procTup, fcinfo);
if(result == NULL) {
// Invocation_assertDisconnect();
fcinfo->isnull = true;
SRF_RETURN_DONE(context);
}
context->user_fctx = (*env)->NewGlobalRef(env, result);
// RegisterExprContextCallback(((ReturnSetInfo*)fcinfo->resultinfo)->econtext, _endOfSetCB, PointerGetDatum(NULL));
return 0;
}
static Datum doSRF(Oid rettype, PG_FUNCTION_ARGS) {
FuncCallContext *context = SRF_PERCALL_SETUP();
jobject resultIter = (jobject)context->user_fctx;
jboolean hn = (*env)->CallBooleanMethod(env, resultIter, hasNext);
check_exception("%s\nchecking for next row", "unused");
if (!hn) {
(*env)->DeleteGlobalRef(env, resultIter);
SRF_RETURN_DONE(context);
}
jobject result = (*env)->CallObjectMethod(env, resultIter, next);
check_exception("%s\ngetting next row", "unused");
Datum row;
if (rettype == RECORDOID) { row = handleRecord(result, fcinfo); }
else row = objectToDatum(rettype, result);
SRF_RETURN_NEXT(context, row);
}
Datum internalCallHandler(bool trusted, PG_FUNCTION_ARGS) {
HeapTuple procTup;
Form_pg_proc procStruct;
Oid foid;
jboolean multicall;
/*if(s_javaVM == NULL) */ initializeJVM(trusted);
foid = fcinfo->flinfo->fn_oid;
jobject fn = (*env)->CallStaticObjectMethod(env, functionClass, functionConstructor, foid );
procTup = SearchSysCache(PROCOID, ObjectIdGetDatum(foid), 0, 0, 0);
if(!HeapTupleIsValid(procTup))
ereport(ERROR, (errmsg("cache lookup failed for function (PROCOID) %u", foid)));
procStruct = (Form_pg_proc)GETSTRUCT(procTup);
multicall = procStruct->proretset;
// rettype = get_fn_expr_rettype(fcinfo->flinfo);
Oid rettype = procStruct->prorettype;
ReleaseSysCache(procTup);
if (CALLED_AS_TRIGGER(fcinfo)) {
jbooleanArray triggerBits = (*env)->NewBooleanArray(env, 9);
TriggerData* trigd = (TriggerData *) fcinfo->context;
jboolean *temp = (jboolean *)(*env)->GetBooleanArrayElements(env, triggerBits, NULL);
temp[0] = (jboolean)TRIGGER_FIRED_AFTER(trigd->tg_event);
temp[1] = (jboolean)TRIGGER_FIRED_BEFORE(trigd->tg_event);
temp[2] = (jboolean)TRIGGER_FIRED_INSTEAD(trigd->tg_event);
temp[3] = (jboolean)TRIGGER_FIRED_FOR_ROW(trigd->tg_event);
temp[4] = (jboolean)TRIGGER_FIRED_FOR_STATEMENT(trigd->tg_event);
temp[5] = (jboolean)TRIGGER_FIRED_BY_INSERT(trigd->tg_event);
temp[6] = (jboolean)TRIGGER_FIRED_BY_DELETE(trigd->tg_event);
temp[7] = (jboolean)TRIGGER_FIRED_BY_UPDATE(trigd->tg_event);
temp[8] = (jboolean)TRIGGER_FIRED_BY_TRUNCATE(trigd->tg_event);
(*env)->ReleaseBooleanArrayElements(env, triggerBits, temp, 0);
// triggerBits is a boolean[] of bits
TupleDesc td = trigd->tg_relation->rd_att;
jobject newt = tupleToObject(td, trigd->tg_newtuple);
jobject oldt = tupleToObject(td, trigd->tg_trigtuple);
int n = trigd->tg_trigger->tgnargs; // this should be zero
bool isNull = false;
char *cname;
Datum tmp = SysCacheGetAttr(PROCOID, procTup, Anum_pg_proc_prosrc, &isNull);
if(isNull) { ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("'AS' clause of Java function cannot be NULL"))); }
jobject name = (*env)->NewStringUTF(env, cname = DatumGetCString(DirectFunctionCall1(textout, tmp)));
jstring tgname = makeJavaString(trigd->tg_trigger->tgname);
jobject result = (*env)->CallObjectMethod(env, fn, triggerInvoke, name, tgname, triggerBits, oldt, newt);
check_exception("%s\ncalling trigger", "unused");
(*env)->DeleteLocalRef(env, name);
return heap_copytuple(handleRecordD(result, td));
}
jobject result;
if (multicall) {
if (SRF_IS_FIRSTCALL()) {
startSRF(fn, procTup, fcinfo);
}
return doSRF(rettype, fcinfo);
}
else {
result = callJavaMethod(fn, procTup, fcinfo);
// FIXME:
// if this wasn't calling a VOID method, this seems to crash things
if (result == NULL) {
fcinfo->isnull = true;
return NULL;
}
if (rettype == RECORDOID) { return handleRecord(result, fcinfo); }
return objectToDatum(rettype, result);
}
}
// Now the boilerplate for Stored Procedures
extern Datum javau_call_handler(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(javau_call_handler);
// This is the entry point for all untrusted calls.
Datum javau_call_handler(PG_FUNCTION_ARGS) {
return internalCallHandler(false, fcinfo);
}
extern Datum javax_call_handler(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(javax_call_handler);
// This is the entry point for all trusted calls.
Datum javax_call_handler(PG_FUNCTION_ARGS) {
return internalCallHandler(true, fcinfo);
}
// Set up interrupt handler
bool InterruptFlag = false; // Used for checking for SIGINT interrupt
bool TerminationFlag = false;