-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscratch.c
212 lines (160 loc) · 4.77 KB
/
scratch.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
static PlannedStmt * planQuery(char *queryString);
typedef struct
{
Portal portal;
Tuplestorestate *tstore;
} run_select_context;
/* makes sure everything is building correctly */
PG_FUNCTION_INFO_V1(return_one);
Datum return_one(PG_FUNCTION_ARGS)
{
PG_RETURN_INT16(1);
}
PG_FUNCTION_INFO_V1(return_variadic);
Datum return_variadic(PG_FUNCTION_ARGS)
{
int arg = PG_GETARG_INT32(0);
HeapTuple resultTuple;
Datum resultDatum;
Datum *values;
bool *isnull;
/* figure out what we're supposed to return */
TupleDesc tupleDesc;
TypeFuncClass typeClass = get_call_result_type(fcinfo, NULL, &tupleDesc);
if (typeClass == TYPEFUNC_SCALAR)
{
/* we've only been asked to return one value */
PG_RETURN_INT32(arg);
}
if (typeClass != TYPEFUNC_COMPOSITE)
{
ereport(ERROR, (errmsg("called in context that cannot accept type record")));
}
tupleDesc = BlessTupleDesc(tupleDesc);
/* return the requested number of t */
values = palloc(sizeof(Datum) * tupleDesc->natts);
isnull = palloc(sizeof(bool) * tupleDesc->natts);
// todo: also check that it expects ints...
for(int i = 0; i < tupleDesc->natts; i++)
{
values[i] = Int32GetDatum(arg);
isnull[i] = false;
}
resultTuple = heap_form_tuple(tupleDesc, values, isnull);
resultDatum = HeapTupleGetDatum(resultTuple);
PG_RETURN_DATUM(resultDatum);
}
PG_FUNCTION_INFO_V1(return_set);
Datum return_set(PG_FUNCTION_ARGS)
{
int arg = PG_GETARG_INT32(0);
FuncCallContext *funcctx;
if (SRF_IS_FIRSTCALL())
{
MemoryContext oldcontext;
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
MemoryContextSwitchTo(oldcontext);
}
funcctx = SRF_PERCALL_SETUP();
if (funcctx->call_cntr < arg)
{
SRF_RETURN_NEXT(funcctx, Int32GetDatum(arg));
}
else
{
SRF_RETURN_DONE(funcctx);
}
}
/*
* Improvements:
* - This does not check that the TupleDesc of its return type matches that of the query
* - Using a tuplestore to store a single tuple feels silly, make your own receiver?
*
* Tests:
* - Does this work inside a transaction?
* - Does this work inside a larger query?
*
* Resources:
* - pg_cursor does something crazy with tuplestore, maybe duplicate that?
*/
PG_FUNCTION_INFO_V1(run_select_old);
Datum run_select_old(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
run_select_context *ctx;
Portal portal;
text *t = PG_GETARG_TEXT_P(0);
char *queryString = text_to_cstring(t);
Tuplestorestate *tstore;
DestReceiver *receiver;
TupleTableSlot *slot;
Datum result;
uint64 count;
TupleDesc tupleDesc;
TypeFuncClass typeClass = get_call_result_type(fcinfo, NULL, &tupleDesc);
if (typeClass != TYPEFUNC_COMPOSITE)
{
ereport(ERROR, (errmsg("called in context that cannot accept type record")));
}
tupleDesc = BlessTupleDesc(tupleDesc);
if (SRF_IS_FIRSTCALL())
{
MemoryContext oldcontext;
PlannedStmt *plan = planQuery(queryString);
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
{
ctx = (run_select_context *) palloc(sizeof(run_select_context));
portal = CreateNewPortal();
portal->visible = false; /* hide the portal from pg_cursors */
ctx->portal = portal;
funcctx->user_fctx = ctx;
}
MemoryContextSwitchTo(oldcontext);
queryString = MemoryContextStrdup(PortalGetHeapMemory(portal), queryString);
PortalDefineQuery(portal, NULL, queryString, "SELECT", list_make1(plan), NULL);
PortalStart(portal, NULL, 0, InvalidSnapshot);
}
funcctx = SRF_PERCALL_SETUP();
ctx = funcctx->user_fctx;
portal = ctx->portal;
tstore = tuplestore_begin_heap(false, false, work_mem);
receiver = CreateDestReceiver(DestTuplestore);
SetTuplestoreDestReceiverParams(receiver, tstore, CurrentMemoryContext, true);
count = PortalRunFetch(portal, FETCH_FORWARD, 1, receiver);
if (count == 0)
{
PortalDrop(portal, false);
SRF_RETURN_DONE(funcctx);
}
slot = MakeSingleTupleTableSlot(tupleDesc);
tuplestore_gettupleslot(tstore, true, true, slot); /* makes a copy for us */
result = ExecFetchSlotTupleDatum(slot); /* makes another copy for us */
(*receiver->rDestroy) (receiver);
SRF_RETURN_NEXT(funcctx, result);
}
static PlannedStmt *
planQuery(char *queryString)
{
List *parseTreeList = pg_parse_query(queryString);
uint32 treeCount = list_length(parseTreeList);
Node *parsedTree;
List *queryTreeList;
Query *selectQuery;
PlannedStmt *plan;
if (treeCount != 1)
{
ereport(ERROR, (errmsg("argument must include a single command")));
}
parsedTree = (Node *) linitial(parseTreeList);
if (!IsA(parsedTree, SelectStmt))
{
ereport(ERROR, (errmsg("only SELECT statements are supported")));
}
queryTreeList = pg_analyze_and_rewrite(parsedTree, queryString, NULL, 0);
selectQuery = (Query *) linitial(queryTreeList);
plan = standard_planner(selectQuery, 0, NULL);
pprint(plan);
return plan;
}