/src/postgres/src/backend/executor/execExprInterp.c
Line | Count | Source |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * execExprInterp.c |
4 | | * Interpreted evaluation of an expression step list. |
5 | | * |
6 | | * This file provides either a "direct threaded" (for gcc, clang and |
7 | | * compatible) or a "switch threaded" (for all compilers) implementation of |
8 | | * expression evaluation. The former is amongst the fastest known methods |
9 | | * of interpreting programs without resorting to assembly level work, or |
10 | | * just-in-time compilation, but it requires support for computed gotos. |
11 | | * The latter is amongst the fastest approaches doable in standard C. |
12 | | * |
13 | | * In either case we use ExprEvalStep->opcode to dispatch to the code block |
14 | | * within ExecInterpExpr() that implements the specific opcode type. |
15 | | * |
16 | | * Switch-threading uses a plain switch() statement to perform the |
17 | | * dispatch. This has the advantages of being plain C and allowing the |
18 | | * compiler to warn if implementation of a specific opcode has been forgotten. |
19 | | * The disadvantage is that dispatches will, as commonly implemented by |
20 | | * compilers, happen from a single location, requiring more jumps and causing |
21 | | * bad branch prediction. |
22 | | * |
23 | | * In direct threading, we use gcc's label-as-values extension - also adopted |
24 | | * by some other compilers - to replace ExprEvalStep->opcode with the address |
25 | | * of the block implementing the instruction. Dispatch to the next instruction |
26 | | * is done by a "computed goto". This allows for better branch prediction |
27 | | * (as the jumps are happening from different locations) and fewer jumps |
28 | | * (as no preparatory jump to a common dispatch location is needed). |
29 | | * |
30 | | * When using direct threading, ExecReadyInterpretedExpr will replace |
31 | | * each step's opcode field with the address of the relevant code block and |
32 | | * ExprState->flags will contain EEO_FLAG_DIRECT_THREADED to remember that |
33 | | * that's been done. |
34 | | * |
35 | | * For very simple instructions the overhead of the full interpreter |
36 | | * "startup", as minimal as it is, is noticeable. Therefore |
37 | | * ExecReadyInterpretedExpr will choose to implement certain simple |
38 | | * opcode patterns using special fast-path routines (ExecJust*). |
39 | | * |
40 | | * Complex or uncommon instructions are not implemented in-line in |
41 | | * ExecInterpExpr(), rather we call out to a helper function appearing later |
42 | | * in this file. For one reason, there'd not be a noticeable performance |
43 | | * benefit, but more importantly those complex routines are intended to be |
44 | | * shared between different expression evaluation approaches. For instance |
45 | | * a JIT compiler would generate calls to them. (This is why they are |
46 | | * exported rather than being "static" in this file.) |
47 | | * |
48 | | * |
49 | | * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group |
50 | | * Portions Copyright (c) 1994, Regents of the University of California |
51 | | * |
52 | | * IDENTIFICATION |
53 | | * src/backend/executor/execExprInterp.c |
54 | | * |
55 | | *------------------------------------------------------------------------- |
56 | | */ |
57 | | #include "postgres.h" |
58 | | |
59 | | #include "access/heaptoast.h" |
60 | | #include "access/tupconvert.h" |
61 | | #include "catalog/pg_type.h" |
62 | | #include "commands/sequence.h" |
63 | | #include "executor/execExpr.h" |
64 | | #include "executor/nodeSubplan.h" |
65 | | #include "funcapi.h" |
66 | | #include "miscadmin.h" |
67 | | #include "nodes/miscnodes.h" |
68 | | #include "nodes/nodeFuncs.h" |
69 | | #include "pgstat.h" |
70 | | #include "utils/array.h" |
71 | | #include "utils/builtins.h" |
72 | | #include "utils/date.h" |
73 | | #include "utils/datum.h" |
74 | | #include "utils/expandedrecord.h" |
75 | | #include "utils/json.h" |
76 | | #include "utils/jsonfuncs.h" |
77 | | #include "utils/jsonpath.h" |
78 | | #include "utils/lsyscache.h" |
79 | | #include "utils/memutils.h" |
80 | | #include "utils/timestamp.h" |
81 | | #include "utils/tuplesort.h" |
82 | | #include "utils/typcache.h" |
83 | | #include "utils/xml.h" |
84 | | |
85 | | /* |
86 | | * Use computed-goto-based opcode dispatch when computed gotos are available. |
87 | | * But use a separate symbol so that it's easy to adjust locally in this file |
88 | | * for development and testing. |
89 | | */ |
90 | | #ifdef HAVE_COMPUTED_GOTO |
91 | | #define EEO_USE_COMPUTED_GOTO |
92 | | #endif /* HAVE_COMPUTED_GOTO */ |
93 | | |
94 | | /* |
95 | | * Macros for opcode dispatch. |
96 | | * |
97 | | * EEO_SWITCH - just hides the switch if not in use. |
98 | | * EEO_CASE - labels the implementation of named expression step type. |
99 | | * EEO_DISPATCH - jump to the implementation of the step type for 'op'. |
100 | | * EEO_OPCODE - compute opcode required by used expression evaluation method. |
101 | | * EEO_NEXT - increment 'op' and jump to correct next step type. |
102 | | * EEO_JUMP - jump to the specified step number within the current expression. |
103 | | */ |
104 | | #if defined(EEO_USE_COMPUTED_GOTO) |
105 | | |
106 | | /* struct for jump target -> opcode lookup table */ |
107 | | typedef struct ExprEvalOpLookup |
108 | | { |
109 | | const void *opcode; |
110 | | ExprEvalOp op; |
111 | | } ExprEvalOpLookup; |
112 | | |
113 | | /* to make dispatch_table accessible outside ExecInterpExpr() */ |
114 | | static const void **dispatch_table = NULL; |
115 | | |
116 | | /* jump target -> opcode lookup table */ |
117 | | static ExprEvalOpLookup reverse_dispatch_table[EEOP_LAST]; |
118 | | |
119 | | #define EEO_SWITCH() |
120 | 0 | #define EEO_CASE(name) CASE_##name: |
121 | 0 | #define EEO_DISPATCH() goto *((void *) op->opcode) |
122 | 0 | #define EEO_OPCODE(opcode) ((intptr_t) dispatch_table[opcode]) |
123 | | |
124 | | #else /* !EEO_USE_COMPUTED_GOTO */ |
125 | | |
126 | | #define EEO_SWITCH() starteval: switch ((ExprEvalOp) op->opcode) |
127 | | #define EEO_CASE(name) case name: |
128 | | #define EEO_DISPATCH() goto starteval |
129 | | #define EEO_OPCODE(opcode) (opcode) |
130 | | |
131 | | #endif /* EEO_USE_COMPUTED_GOTO */ |
132 | | |
133 | | #define EEO_NEXT() \ |
134 | 0 | do { \ |
135 | 0 | op++; \ |
136 | 0 | EEO_DISPATCH(); \ |
137 | 0 | } while (0) |
138 | | |
139 | | #define EEO_JUMP(stepno) \ |
140 | 0 | do { \ |
141 | 0 | op = &state->steps[stepno]; \ |
142 | 0 | EEO_DISPATCH(); \ |
143 | 0 | } while (0) |
144 | | |
145 | | |
146 | | static Datum ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull); |
147 | | static void ExecInitInterpreter(void); |
148 | | |
149 | | /* support functions */ |
150 | | static void CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype); |
151 | | static void CheckOpSlotCompatibility(ExprEvalStep *op, TupleTableSlot *slot); |
152 | | static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod, |
153 | | ExprEvalRowtypeCache *rowcache, |
154 | | bool *changed); |
155 | | static void ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op, |
156 | | ExprContext *econtext, bool checkisnull); |
157 | | |
158 | | /* fast-path evaluation functions */ |
159 | | static Datum ExecJustInnerVar(ExprState *state, ExprContext *econtext, bool *isnull); |
160 | | static Datum ExecJustOuterVar(ExprState *state, ExprContext *econtext, bool *isnull); |
161 | | static Datum ExecJustScanVar(ExprState *state, ExprContext *econtext, bool *isnull); |
162 | | static Datum ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull); |
163 | | static Datum ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull); |
164 | | static Datum ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull); |
165 | | static Datum ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull); |
166 | | static Datum ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull); |
167 | | static Datum ExecJustInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
168 | | static Datum ExecJustOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
169 | | static Datum ExecJustScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
170 | | static Datum ExecJustAssignInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
171 | | static Datum ExecJustAssignOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
172 | | static Datum ExecJustAssignScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
173 | | static Datum ExecJustHashInnerVarWithIV(ExprState *state, ExprContext *econtext, bool *isnull); |
174 | | static Datum ExecJustHashOuterVar(ExprState *state, ExprContext *econtext, bool *isnull); |
175 | | static Datum ExecJustHashInnerVar(ExprState *state, ExprContext *econtext, bool *isnull); |
176 | | static Datum ExecJustHashOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
177 | | static Datum ExecJustHashInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
178 | | static Datum ExecJustHashOuterVarStrict(ExprState *state, ExprContext *econtext, bool *isnull); |
179 | | |
180 | | /* execution helper functions */ |
181 | | static pg_always_inline void ExecEvalArrayCompareInternal(FunctionCallInfo fcinfo, |
182 | | ArrayType *arr, |
183 | | int16 typlen, |
184 | | bool typbyval, |
185 | | char typalign, |
186 | | bool useOr, |
187 | | Datum *result, |
188 | | bool *resultnull); |
189 | | static pg_always_inline void ExecAggPlainTransByVal(AggState *aggstate, |
190 | | AggStatePerTrans pertrans, |
191 | | AggStatePerGroup pergroup, |
192 | | ExprContext *aggcontext, |
193 | | int setno); |
194 | | static pg_always_inline void ExecAggPlainTransByRef(AggState *aggstate, |
195 | | AggStatePerTrans pertrans, |
196 | | AggStatePerGroup pergroup, |
197 | | ExprContext *aggcontext, |
198 | | int setno); |
199 | | static char *ExecGetJsonValueItemString(JsonbValue *item, bool *resnull); |
200 | | |
201 | | /* |
202 | | * ScalarArrayOpExprHashEntry |
203 | | * Hash table entry type used during EEOP_HASHED_SCALARARRAYOP |
204 | | */ |
205 | | typedef struct ScalarArrayOpExprHashEntry |
206 | | { |
207 | | Datum key; |
208 | | uint32 status; /* hash status */ |
209 | | uint32 hash; /* hash value (cached) */ |
210 | | } ScalarArrayOpExprHashEntry; |
211 | | |
212 | | #define SH_PREFIX saophash |
213 | | #define SH_ELEMENT_TYPE ScalarArrayOpExprHashEntry |
214 | | #define SH_KEY_TYPE Datum |
215 | | #define SH_SCOPE static inline |
216 | | #define SH_DECLARE |
217 | | #include "lib/simplehash.h" |
218 | | |
219 | | static bool saop_hash_element_match(struct saophash_hash *tb, Datum key1, |
220 | | Datum key2); |
221 | | static uint32 saop_element_hash(struct saophash_hash *tb, Datum key); |
222 | | |
223 | | /* |
224 | | * ScalarArrayOpExprHashTable |
225 | | * Hash table for EEOP_HASHED_SCALARARRAYOP |
226 | | */ |
227 | | typedef struct ScalarArrayOpExprHashTable |
228 | | { |
229 | | saophash_hash *hashtab; /* underlying hash table */ |
230 | | struct ExprEvalStep *op; |
231 | | FmgrInfo hash_finfo; /* function's lookup data */ |
232 | | FunctionCallInfoBaseData hash_fcinfo_data; /* arguments etc */ |
233 | | } ScalarArrayOpExprHashTable; |
234 | | |
235 | | /* Define parameters for ScalarArrayOpExpr hash table code generation. */ |
236 | | #define SH_PREFIX saophash |
237 | 0 | #define SH_ELEMENT_TYPE ScalarArrayOpExprHashEntry |
238 | | #define SH_KEY_TYPE Datum |
239 | 0 | #define SH_KEY key |
240 | 0 | #define SH_HASH_KEY(tb, key) saop_element_hash(tb, key) |
241 | 0 | #define SH_EQUAL(tb, a, b) saop_hash_element_match(tb, a, b) |
242 | | #define SH_SCOPE static inline |
243 | | #define SH_STORE_HASH |
244 | 0 | #define SH_GET_HASH(tb, a) a->hash |
245 | | #define SH_DEFINE |
246 | | #include "lib/simplehash.h" |
247 | | |
248 | | /* |
249 | | * Prepare ExprState for interpreted execution. |
250 | | */ |
251 | | void |
252 | | ExecReadyInterpretedExpr(ExprState *state) |
253 | 0 | { |
254 | | /* Ensure one-time interpreter setup has been done */ |
255 | 0 | ExecInitInterpreter(); |
256 | | |
257 | | /* Simple validity checks on expression */ |
258 | 0 | Assert(state->steps_len >= 1); |
259 | 0 | Assert(state->steps[state->steps_len - 1].opcode == EEOP_DONE_RETURN || |
260 | 0 | state->steps[state->steps_len - 1].opcode == EEOP_DONE_NO_RETURN); |
261 | | |
262 | | /* |
263 | | * Don't perform redundant initialization. This is unreachable in current |
264 | | * cases, but might be hit if there's additional expression evaluation |
265 | | * methods that rely on interpreted execution to work. |
266 | | */ |
267 | 0 | if (state->flags & EEO_FLAG_INTERPRETER_INITIALIZED) |
268 | 0 | return; |
269 | | |
270 | | /* |
271 | | * First time through, check whether attribute matches Var. Might not be |
272 | | * ok anymore, due to schema changes. We do that by setting up a callback |
273 | | * that does checking on the first call, which then sets the evalfunc |
274 | | * callback to the actual method of execution. |
275 | | */ |
276 | 0 | state->evalfunc = ExecInterpExprStillValid; |
277 | | |
278 | | /* DIRECT_THREADED should not already be set */ |
279 | 0 | Assert((state->flags & EEO_FLAG_DIRECT_THREADED) == 0); |
280 | | |
281 | | /* |
282 | | * There shouldn't be any errors before the expression is fully |
283 | | * initialized, and even if so, it'd lead to the expression being |
284 | | * abandoned. So we can set the flag now and save some code. |
285 | | */ |
286 | 0 | state->flags |= EEO_FLAG_INTERPRETER_INITIALIZED; |
287 | | |
288 | | /* |
289 | | * Select fast-path evalfuncs for very simple expressions. "Starting up" |
290 | | * the full interpreter is a measurable overhead for these, and these |
291 | | * patterns occur often enough to be worth optimizing. |
292 | | */ |
293 | 0 | if (state->steps_len == 5) |
294 | 0 | { |
295 | 0 | ExprEvalOp step0 = state->steps[0].opcode; |
296 | 0 | ExprEvalOp step1 = state->steps[1].opcode; |
297 | 0 | ExprEvalOp step2 = state->steps[2].opcode; |
298 | 0 | ExprEvalOp step3 = state->steps[3].opcode; |
299 | |
|
300 | 0 | if (step0 == EEOP_INNER_FETCHSOME && |
301 | 0 | step1 == EEOP_HASHDATUM_SET_INITVAL && |
302 | 0 | step2 == EEOP_INNER_VAR && |
303 | 0 | step3 == EEOP_HASHDATUM_NEXT32) |
304 | 0 | { |
305 | 0 | state->evalfunc_private = (void *) ExecJustHashInnerVarWithIV; |
306 | 0 | return; |
307 | 0 | } |
308 | 0 | } |
309 | 0 | else if (state->steps_len == 4) |
310 | 0 | { |
311 | 0 | ExprEvalOp step0 = state->steps[0].opcode; |
312 | 0 | ExprEvalOp step1 = state->steps[1].opcode; |
313 | 0 | ExprEvalOp step2 = state->steps[2].opcode; |
314 | |
|
315 | 0 | if (step0 == EEOP_OUTER_FETCHSOME && |
316 | 0 | step1 == EEOP_OUTER_VAR && |
317 | 0 | step2 == EEOP_HASHDATUM_FIRST) |
318 | 0 | { |
319 | 0 | state->evalfunc_private = (void *) ExecJustHashOuterVar; |
320 | 0 | return; |
321 | 0 | } |
322 | 0 | else if (step0 == EEOP_INNER_FETCHSOME && |
323 | 0 | step1 == EEOP_INNER_VAR && |
324 | 0 | step2 == EEOP_HASHDATUM_FIRST) |
325 | 0 | { |
326 | 0 | state->evalfunc_private = (void *) ExecJustHashInnerVar; |
327 | 0 | return; |
328 | 0 | } |
329 | 0 | else if (step0 == EEOP_OUTER_FETCHSOME && |
330 | 0 | step1 == EEOP_OUTER_VAR && |
331 | 0 | step2 == EEOP_HASHDATUM_FIRST_STRICT) |
332 | 0 | { |
333 | 0 | state->evalfunc_private = (void *) ExecJustHashOuterVarStrict; |
334 | 0 | return; |
335 | 0 | } |
336 | 0 | } |
337 | 0 | else if (state->steps_len == 3) |
338 | 0 | { |
339 | 0 | ExprEvalOp step0 = state->steps[0].opcode; |
340 | 0 | ExprEvalOp step1 = state->steps[1].opcode; |
341 | |
|
342 | 0 | if (step0 == EEOP_INNER_FETCHSOME && |
343 | 0 | step1 == EEOP_INNER_VAR) |
344 | 0 | { |
345 | 0 | state->evalfunc_private = ExecJustInnerVar; |
346 | 0 | return; |
347 | 0 | } |
348 | 0 | else if (step0 == EEOP_OUTER_FETCHSOME && |
349 | 0 | step1 == EEOP_OUTER_VAR) |
350 | 0 | { |
351 | 0 | state->evalfunc_private = ExecJustOuterVar; |
352 | 0 | return; |
353 | 0 | } |
354 | 0 | else if (step0 == EEOP_SCAN_FETCHSOME && |
355 | 0 | step1 == EEOP_SCAN_VAR) |
356 | 0 | { |
357 | 0 | state->evalfunc_private = ExecJustScanVar; |
358 | 0 | return; |
359 | 0 | } |
360 | 0 | else if (step0 == EEOP_INNER_FETCHSOME && |
361 | 0 | step1 == EEOP_ASSIGN_INNER_VAR) |
362 | 0 | { |
363 | 0 | state->evalfunc_private = ExecJustAssignInnerVar; |
364 | 0 | return; |
365 | 0 | } |
366 | 0 | else if (step0 == EEOP_OUTER_FETCHSOME && |
367 | 0 | step1 == EEOP_ASSIGN_OUTER_VAR) |
368 | 0 | { |
369 | 0 | state->evalfunc_private = ExecJustAssignOuterVar; |
370 | 0 | return; |
371 | 0 | } |
372 | 0 | else if (step0 == EEOP_SCAN_FETCHSOME && |
373 | 0 | step1 == EEOP_ASSIGN_SCAN_VAR) |
374 | 0 | { |
375 | 0 | state->evalfunc_private = ExecJustAssignScanVar; |
376 | 0 | return; |
377 | 0 | } |
378 | 0 | else if (step0 == EEOP_CASE_TESTVAL && |
379 | 0 | (step1 == EEOP_FUNCEXPR_STRICT || |
380 | 0 | step1 == EEOP_FUNCEXPR_STRICT_1 || |
381 | 0 | step1 == EEOP_FUNCEXPR_STRICT_2)) |
382 | 0 | { |
383 | 0 | state->evalfunc_private = ExecJustApplyFuncToCase; |
384 | 0 | return; |
385 | 0 | } |
386 | 0 | else if (step0 == EEOP_INNER_VAR && |
387 | 0 | step1 == EEOP_HASHDATUM_FIRST) |
388 | 0 | { |
389 | 0 | state->evalfunc_private = (void *) ExecJustHashInnerVarVirt; |
390 | 0 | return; |
391 | 0 | } |
392 | 0 | else if (step0 == EEOP_OUTER_VAR && |
393 | 0 | step1 == EEOP_HASHDATUM_FIRST) |
394 | 0 | { |
395 | 0 | state->evalfunc_private = (void *) ExecJustHashOuterVarVirt; |
396 | 0 | return; |
397 | 0 | } |
398 | 0 | } |
399 | 0 | else if (state->steps_len == 2) |
400 | 0 | { |
401 | 0 | ExprEvalOp step0 = state->steps[0].opcode; |
402 | |
|
403 | 0 | if (step0 == EEOP_CONST) |
404 | 0 | { |
405 | 0 | state->evalfunc_private = ExecJustConst; |
406 | 0 | return; |
407 | 0 | } |
408 | 0 | else if (step0 == EEOP_INNER_VAR) |
409 | 0 | { |
410 | 0 | state->evalfunc_private = ExecJustInnerVarVirt; |
411 | 0 | return; |
412 | 0 | } |
413 | 0 | else if (step0 == EEOP_OUTER_VAR) |
414 | 0 | { |
415 | 0 | state->evalfunc_private = ExecJustOuterVarVirt; |
416 | 0 | return; |
417 | 0 | } |
418 | 0 | else if (step0 == EEOP_SCAN_VAR) |
419 | 0 | { |
420 | 0 | state->evalfunc_private = ExecJustScanVarVirt; |
421 | 0 | return; |
422 | 0 | } |
423 | 0 | else if (step0 == EEOP_ASSIGN_INNER_VAR) |
424 | 0 | { |
425 | 0 | state->evalfunc_private = ExecJustAssignInnerVarVirt; |
426 | 0 | return; |
427 | 0 | } |
428 | 0 | else if (step0 == EEOP_ASSIGN_OUTER_VAR) |
429 | 0 | { |
430 | 0 | state->evalfunc_private = ExecJustAssignOuterVarVirt; |
431 | 0 | return; |
432 | 0 | } |
433 | 0 | else if (step0 == EEOP_ASSIGN_SCAN_VAR) |
434 | 0 | { |
435 | 0 | state->evalfunc_private = ExecJustAssignScanVarVirt; |
436 | 0 | return; |
437 | 0 | } |
438 | 0 | } |
439 | | |
440 | 0 | #if defined(EEO_USE_COMPUTED_GOTO) |
441 | | |
442 | | /* |
443 | | * In the direct-threaded implementation, replace each opcode with the |
444 | | * address to jump to. (Use ExecEvalStepOp() to get back the opcode.) |
445 | | */ |
446 | 0 | for (int off = 0; off < state->steps_len; off++) |
447 | 0 | { |
448 | 0 | ExprEvalStep *op = &state->steps[off]; |
449 | |
|
450 | 0 | op->opcode = EEO_OPCODE(op->opcode); |
451 | 0 | } |
452 | |
|
453 | 0 | state->flags |= EEO_FLAG_DIRECT_THREADED; |
454 | 0 | #endif /* EEO_USE_COMPUTED_GOTO */ |
455 | |
|
456 | 0 | state->evalfunc_private = ExecInterpExpr; |
457 | 0 | } |
458 | | |
459 | | |
460 | | /* |
461 | | * Evaluate expression identified by "state" in the execution context |
462 | | * given by "econtext". *isnull is set to the is-null flag for the result, |
463 | | * and the Datum value is the function result. |
464 | | * |
465 | | * As a special case, return the dispatch table's address if state is NULL. |
466 | | * This is used by ExecInitInterpreter to set up the dispatch_table global. |
467 | | * (Only applies when EEO_USE_COMPUTED_GOTO is defined.) |
468 | | */ |
469 | | static Datum |
470 | | ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) |
471 | 0 | { |
472 | 0 | ExprEvalStep *op; |
473 | 0 | TupleTableSlot *resultslot; |
474 | 0 | TupleTableSlot *innerslot; |
475 | 0 | TupleTableSlot *outerslot; |
476 | 0 | TupleTableSlot *scanslot; |
477 | 0 | TupleTableSlot *oldslot; |
478 | 0 | TupleTableSlot *newslot; |
479 | | |
480 | | /* |
481 | | * This array has to be in the same order as enum ExprEvalOp. |
482 | | */ |
483 | 0 | #if defined(EEO_USE_COMPUTED_GOTO) |
484 | 0 | static const void *const dispatch_table[] = { |
485 | 0 | &&CASE_EEOP_DONE_RETURN, |
486 | 0 | &&CASE_EEOP_DONE_NO_RETURN, |
487 | 0 | &&CASE_EEOP_INNER_FETCHSOME, |
488 | 0 | &&CASE_EEOP_OUTER_FETCHSOME, |
489 | 0 | &&CASE_EEOP_SCAN_FETCHSOME, |
490 | 0 | &&CASE_EEOP_OLD_FETCHSOME, |
491 | 0 | &&CASE_EEOP_NEW_FETCHSOME, |
492 | 0 | &&CASE_EEOP_INNER_VAR, |
493 | 0 | &&CASE_EEOP_OUTER_VAR, |
494 | 0 | &&CASE_EEOP_SCAN_VAR, |
495 | 0 | &&CASE_EEOP_OLD_VAR, |
496 | 0 | &&CASE_EEOP_NEW_VAR, |
497 | 0 | &&CASE_EEOP_INNER_SYSVAR, |
498 | 0 | &&CASE_EEOP_OUTER_SYSVAR, |
499 | 0 | &&CASE_EEOP_SCAN_SYSVAR, |
500 | 0 | &&CASE_EEOP_OLD_SYSVAR, |
501 | 0 | &&CASE_EEOP_NEW_SYSVAR, |
502 | 0 | &&CASE_EEOP_WHOLEROW, |
503 | 0 | &&CASE_EEOP_ASSIGN_INNER_VAR, |
504 | 0 | &&CASE_EEOP_ASSIGN_OUTER_VAR, |
505 | 0 | &&CASE_EEOP_ASSIGN_SCAN_VAR, |
506 | 0 | &&CASE_EEOP_ASSIGN_OLD_VAR, |
507 | 0 | &&CASE_EEOP_ASSIGN_NEW_VAR, |
508 | 0 | &&CASE_EEOP_ASSIGN_TMP, |
509 | 0 | &&CASE_EEOP_ASSIGN_TMP_MAKE_RO, |
510 | 0 | &&CASE_EEOP_CONST, |
511 | 0 | &&CASE_EEOP_FUNCEXPR, |
512 | 0 | &&CASE_EEOP_FUNCEXPR_STRICT, |
513 | 0 | &&CASE_EEOP_FUNCEXPR_STRICT_1, |
514 | 0 | &&CASE_EEOP_FUNCEXPR_STRICT_2, |
515 | 0 | &&CASE_EEOP_FUNCEXPR_FUSAGE, |
516 | 0 | &&CASE_EEOP_FUNCEXPR_STRICT_FUSAGE, |
517 | 0 | &&CASE_EEOP_BOOL_AND_STEP_FIRST, |
518 | 0 | &&CASE_EEOP_BOOL_AND_STEP, |
519 | 0 | &&CASE_EEOP_BOOL_AND_STEP_LAST, |
520 | 0 | &&CASE_EEOP_BOOL_OR_STEP_FIRST, |
521 | 0 | &&CASE_EEOP_BOOL_OR_STEP, |
522 | 0 | &&CASE_EEOP_BOOL_OR_STEP_LAST, |
523 | 0 | &&CASE_EEOP_BOOL_NOT_STEP, |
524 | 0 | &&CASE_EEOP_QUAL, |
525 | 0 | &&CASE_EEOP_JUMP, |
526 | 0 | &&CASE_EEOP_JUMP_IF_NULL, |
527 | 0 | &&CASE_EEOP_JUMP_IF_NOT_NULL, |
528 | 0 | &&CASE_EEOP_JUMP_IF_NOT_TRUE, |
529 | 0 | &&CASE_EEOP_NULLTEST_ISNULL, |
530 | 0 | &&CASE_EEOP_NULLTEST_ISNOTNULL, |
531 | 0 | &&CASE_EEOP_NULLTEST_ROWISNULL, |
532 | 0 | &&CASE_EEOP_NULLTEST_ROWISNOTNULL, |
533 | 0 | &&CASE_EEOP_BOOLTEST_IS_TRUE, |
534 | 0 | &&CASE_EEOP_BOOLTEST_IS_NOT_TRUE, |
535 | 0 | &&CASE_EEOP_BOOLTEST_IS_FALSE, |
536 | 0 | &&CASE_EEOP_BOOLTEST_IS_NOT_FALSE, |
537 | 0 | &&CASE_EEOP_PARAM_EXEC, |
538 | 0 | &&CASE_EEOP_PARAM_EXTERN, |
539 | 0 | &&CASE_EEOP_PARAM_CALLBACK, |
540 | 0 | &&CASE_EEOP_PARAM_SET, |
541 | 0 | &&CASE_EEOP_CASE_TESTVAL, |
542 | 0 | &&CASE_EEOP_CASE_TESTVAL_EXT, |
543 | 0 | &&CASE_EEOP_MAKE_READONLY, |
544 | 0 | &&CASE_EEOP_IOCOERCE, |
545 | 0 | &&CASE_EEOP_IOCOERCE_SAFE, |
546 | 0 | &&CASE_EEOP_DISTINCT, |
547 | 0 | &&CASE_EEOP_NOT_DISTINCT, |
548 | 0 | &&CASE_EEOP_NULLIF, |
549 | 0 | &&CASE_EEOP_SQLVALUEFUNCTION, |
550 | 0 | &&CASE_EEOP_CURRENTOFEXPR, |
551 | 0 | &&CASE_EEOP_NEXTVALUEEXPR, |
552 | 0 | &&CASE_EEOP_RETURNINGEXPR, |
553 | 0 | &&CASE_EEOP_ARRAYEXPR, |
554 | 0 | &&CASE_EEOP_ARRAYCOERCE, |
555 | 0 | &&CASE_EEOP_ROW, |
556 | 0 | &&CASE_EEOP_ROWCOMPARE_STEP, |
557 | 0 | &&CASE_EEOP_ROWCOMPARE_FINAL, |
558 | 0 | &&CASE_EEOP_MINMAX, |
559 | 0 | &&CASE_EEOP_FIELDSELECT, |
560 | 0 | &&CASE_EEOP_FIELDSTORE_DEFORM, |
561 | 0 | &&CASE_EEOP_FIELDSTORE_FORM, |
562 | 0 | &&CASE_EEOP_SBSREF_SUBSCRIPTS, |
563 | 0 | &&CASE_EEOP_SBSREF_OLD, |
564 | 0 | &&CASE_EEOP_SBSREF_ASSIGN, |
565 | 0 | &&CASE_EEOP_SBSREF_FETCH, |
566 | 0 | &&CASE_EEOP_DOMAIN_TESTVAL, |
567 | 0 | &&CASE_EEOP_DOMAIN_TESTVAL_EXT, |
568 | 0 | &&CASE_EEOP_DOMAIN_NOTNULL, |
569 | 0 | &&CASE_EEOP_DOMAIN_CHECK, |
570 | 0 | &&CASE_EEOP_HASHDATUM_SET_INITVAL, |
571 | 0 | &&CASE_EEOP_HASHDATUM_FIRST, |
572 | 0 | &&CASE_EEOP_HASHDATUM_FIRST_STRICT, |
573 | 0 | &&CASE_EEOP_HASHDATUM_NEXT32, |
574 | 0 | &&CASE_EEOP_HASHDATUM_NEXT32_STRICT, |
575 | 0 | &&CASE_EEOP_CONVERT_ROWTYPE, |
576 | 0 | &&CASE_EEOP_SCALARARRAYOP, |
577 | 0 | &&CASE_EEOP_HASHED_SCALARARRAYOP, |
578 | 0 | &&CASE_EEOP_XMLEXPR, |
579 | 0 | &&CASE_EEOP_JSON_CONSTRUCTOR, |
580 | 0 | &&CASE_EEOP_IS_JSON, |
581 | 0 | &&CASE_EEOP_JSONEXPR_PATH, |
582 | 0 | &&CASE_EEOP_JSONEXPR_COERCION, |
583 | 0 | &&CASE_EEOP_JSONEXPR_COERCION_FINISH, |
584 | 0 | &&CASE_EEOP_AGGREF, |
585 | 0 | &&CASE_EEOP_GROUPING_FUNC, |
586 | 0 | &&CASE_EEOP_WINDOW_FUNC, |
587 | 0 | &&CASE_EEOP_MERGE_SUPPORT_FUNC, |
588 | 0 | &&CASE_EEOP_SUBPLAN, |
589 | 0 | &&CASE_EEOP_AGG_STRICT_DESERIALIZE, |
590 | 0 | &&CASE_EEOP_AGG_DESERIALIZE, |
591 | 0 | &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS, |
592 | 0 | &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1, |
593 | 0 | &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_NULLS, |
594 | 0 | &&CASE_EEOP_AGG_PLAIN_PERGROUP_NULLCHECK, |
595 | 0 | &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL, |
596 | 0 | &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL, |
597 | 0 | &&CASE_EEOP_AGG_PLAIN_TRANS_BYVAL, |
598 | 0 | &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF, |
599 | 0 | &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYREF, |
600 | 0 | &&CASE_EEOP_AGG_PLAIN_TRANS_BYREF, |
601 | 0 | &&CASE_EEOP_AGG_PRESORTED_DISTINCT_SINGLE, |
602 | 0 | &&CASE_EEOP_AGG_PRESORTED_DISTINCT_MULTI, |
603 | 0 | &&CASE_EEOP_AGG_ORDERED_TRANS_DATUM, |
604 | 0 | &&CASE_EEOP_AGG_ORDERED_TRANS_TUPLE, |
605 | 0 | &&CASE_EEOP_LAST |
606 | 0 | }; |
607 | |
|
608 | 0 | StaticAssertDecl(lengthof(dispatch_table) == EEOP_LAST + 1, |
609 | 0 | "dispatch_table out of whack with ExprEvalOp"); |
610 | |
|
611 | 0 | if (unlikely(state == NULL)) |
612 | 0 | return PointerGetDatum(dispatch_table); |
613 | | #else |
614 | | Assert(state != NULL); |
615 | | #endif /* EEO_USE_COMPUTED_GOTO */ |
616 | | |
617 | | /* setup state */ |
618 | 0 | op = state->steps; |
619 | 0 | resultslot = state->resultslot; |
620 | 0 | innerslot = econtext->ecxt_innertuple; |
621 | 0 | outerslot = econtext->ecxt_outertuple; |
622 | 0 | scanslot = econtext->ecxt_scantuple; |
623 | 0 | oldslot = econtext->ecxt_oldtuple; |
624 | 0 | newslot = econtext->ecxt_newtuple; |
625 | |
|
626 | 0 | #if defined(EEO_USE_COMPUTED_GOTO) |
627 | 0 | EEO_DISPATCH(); |
628 | 0 | #endif |
629 | |
|
630 | 0 | EEO_SWITCH() |
631 | 0 | { |
632 | 0 | EEO_CASE(EEOP_DONE_RETURN) |
633 | 0 | { |
634 | 0 | *isnull = state->resnull; |
635 | 0 | return state->resvalue; |
636 | 0 | } |
637 | | |
638 | 0 | EEO_CASE(EEOP_DONE_NO_RETURN) |
639 | 0 | { |
640 | 0 | Assert(isnull == NULL); |
641 | 0 | return (Datum) 0; |
642 | 0 | } |
643 | | |
644 | 0 | EEO_CASE(EEOP_INNER_FETCHSOME) |
645 | 0 | { |
646 | 0 | CheckOpSlotCompatibility(op, innerslot); |
647 | |
|
648 | 0 | slot_getsomeattrs(innerslot, op->d.fetch.last_var); |
649 | |
|
650 | 0 | EEO_NEXT(); |
651 | 0 | } |
652 | |
|
653 | 0 | EEO_CASE(EEOP_OUTER_FETCHSOME) |
654 | 0 | { |
655 | 0 | CheckOpSlotCompatibility(op, outerslot); |
656 | |
|
657 | 0 | slot_getsomeattrs(outerslot, op->d.fetch.last_var); |
658 | |
|
659 | 0 | EEO_NEXT(); |
660 | 0 | } |
661 | |
|
662 | 0 | EEO_CASE(EEOP_SCAN_FETCHSOME) |
663 | 0 | { |
664 | 0 | CheckOpSlotCompatibility(op, scanslot); |
665 | |
|
666 | 0 | slot_getsomeattrs(scanslot, op->d.fetch.last_var); |
667 | |
|
668 | 0 | EEO_NEXT(); |
669 | 0 | } |
670 | |
|
671 | 0 | EEO_CASE(EEOP_OLD_FETCHSOME) |
672 | 0 | { |
673 | 0 | CheckOpSlotCompatibility(op, oldslot); |
674 | |
|
675 | 0 | slot_getsomeattrs(oldslot, op->d.fetch.last_var); |
676 | |
|
677 | 0 | EEO_NEXT(); |
678 | 0 | } |
679 | |
|
680 | 0 | EEO_CASE(EEOP_NEW_FETCHSOME) |
681 | 0 | { |
682 | 0 | CheckOpSlotCompatibility(op, newslot); |
683 | |
|
684 | 0 | slot_getsomeattrs(newslot, op->d.fetch.last_var); |
685 | |
|
686 | 0 | EEO_NEXT(); |
687 | 0 | } |
688 | |
|
689 | 0 | EEO_CASE(EEOP_INNER_VAR) |
690 | 0 | { |
691 | 0 | int attnum = op->d.var.attnum; |
692 | | |
693 | | /* |
694 | | * Since we already extracted all referenced columns from the |
695 | | * tuple with a FETCHSOME step, we can just grab the value |
696 | | * directly out of the slot's decomposed-data arrays. But let's |
697 | | * have an Assert to check that that did happen. |
698 | | */ |
699 | 0 | Assert(attnum >= 0 && attnum < innerslot->tts_nvalid); |
700 | 0 | *op->resvalue = innerslot->tts_values[attnum]; |
701 | 0 | *op->resnull = innerslot->tts_isnull[attnum]; |
702 | |
|
703 | 0 | EEO_NEXT(); |
704 | 0 | } |
705 | |
|
706 | 0 | EEO_CASE(EEOP_OUTER_VAR) |
707 | 0 | { |
708 | 0 | int attnum = op->d.var.attnum; |
709 | | |
710 | | /* See EEOP_INNER_VAR comments */ |
711 | |
|
712 | 0 | Assert(attnum >= 0 && attnum < outerslot->tts_nvalid); |
713 | 0 | *op->resvalue = outerslot->tts_values[attnum]; |
714 | 0 | *op->resnull = outerslot->tts_isnull[attnum]; |
715 | |
|
716 | 0 | EEO_NEXT(); |
717 | 0 | } |
718 | |
|
719 | 0 | EEO_CASE(EEOP_SCAN_VAR) |
720 | 0 | { |
721 | 0 | int attnum = op->d.var.attnum; |
722 | | |
723 | | /* See EEOP_INNER_VAR comments */ |
724 | |
|
725 | 0 | Assert(attnum >= 0 && attnum < scanslot->tts_nvalid); |
726 | 0 | *op->resvalue = scanslot->tts_values[attnum]; |
727 | 0 | *op->resnull = scanslot->tts_isnull[attnum]; |
728 | |
|
729 | 0 | EEO_NEXT(); |
730 | 0 | } |
731 | |
|
732 | 0 | EEO_CASE(EEOP_OLD_VAR) |
733 | 0 | { |
734 | 0 | int attnum = op->d.var.attnum; |
735 | | |
736 | | /* See EEOP_INNER_VAR comments */ |
737 | |
|
738 | 0 | Assert(attnum >= 0 && attnum < oldslot->tts_nvalid); |
739 | 0 | *op->resvalue = oldslot->tts_values[attnum]; |
740 | 0 | *op->resnull = oldslot->tts_isnull[attnum]; |
741 | |
|
742 | 0 | EEO_NEXT(); |
743 | 0 | } |
744 | |
|
745 | 0 | EEO_CASE(EEOP_NEW_VAR) |
746 | 0 | { |
747 | 0 | int attnum = op->d.var.attnum; |
748 | | |
749 | | /* See EEOP_INNER_VAR comments */ |
750 | |
|
751 | 0 | Assert(attnum >= 0 && attnum < newslot->tts_nvalid); |
752 | 0 | *op->resvalue = newslot->tts_values[attnum]; |
753 | 0 | *op->resnull = newslot->tts_isnull[attnum]; |
754 | |
|
755 | 0 | EEO_NEXT(); |
756 | 0 | } |
757 | |
|
758 | 0 | EEO_CASE(EEOP_INNER_SYSVAR) |
759 | 0 | { |
760 | 0 | ExecEvalSysVar(state, op, econtext, innerslot); |
761 | 0 | EEO_NEXT(); |
762 | 0 | } |
763 | |
|
764 | 0 | EEO_CASE(EEOP_OUTER_SYSVAR) |
765 | 0 | { |
766 | 0 | ExecEvalSysVar(state, op, econtext, outerslot); |
767 | 0 | EEO_NEXT(); |
768 | 0 | } |
769 | |
|
770 | 0 | EEO_CASE(EEOP_SCAN_SYSVAR) |
771 | 0 | { |
772 | 0 | ExecEvalSysVar(state, op, econtext, scanslot); |
773 | 0 | EEO_NEXT(); |
774 | 0 | } |
775 | |
|
776 | 0 | EEO_CASE(EEOP_OLD_SYSVAR) |
777 | 0 | { |
778 | 0 | ExecEvalSysVar(state, op, econtext, oldslot); |
779 | 0 | EEO_NEXT(); |
780 | 0 | } |
781 | |
|
782 | 0 | EEO_CASE(EEOP_NEW_SYSVAR) |
783 | 0 | { |
784 | 0 | ExecEvalSysVar(state, op, econtext, newslot); |
785 | 0 | EEO_NEXT(); |
786 | 0 | } |
787 | |
|
788 | 0 | EEO_CASE(EEOP_WHOLEROW) |
789 | 0 | { |
790 | | /* too complex for an inline implementation */ |
791 | 0 | ExecEvalWholeRowVar(state, op, econtext); |
792 | |
|
793 | 0 | EEO_NEXT(); |
794 | 0 | } |
795 | |
|
796 | 0 | EEO_CASE(EEOP_ASSIGN_INNER_VAR) |
797 | 0 | { |
798 | 0 | int resultnum = op->d.assign_var.resultnum; |
799 | 0 | int attnum = op->d.assign_var.attnum; |
800 | | |
801 | | /* |
802 | | * We do not need CheckVarSlotCompatibility here; that was taken |
803 | | * care of at compilation time. But see EEOP_INNER_VAR comments. |
804 | | */ |
805 | 0 | Assert(attnum >= 0 && attnum < innerslot->tts_nvalid); |
806 | 0 | Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
807 | 0 | resultslot->tts_values[resultnum] = innerslot->tts_values[attnum]; |
808 | 0 | resultslot->tts_isnull[resultnum] = innerslot->tts_isnull[attnum]; |
809 | |
|
810 | 0 | EEO_NEXT(); |
811 | 0 | } |
812 | |
|
813 | 0 | EEO_CASE(EEOP_ASSIGN_OUTER_VAR) |
814 | 0 | { |
815 | 0 | int resultnum = op->d.assign_var.resultnum; |
816 | 0 | int attnum = op->d.assign_var.attnum; |
817 | | |
818 | | /* |
819 | | * We do not need CheckVarSlotCompatibility here; that was taken |
820 | | * care of at compilation time. But see EEOP_INNER_VAR comments. |
821 | | */ |
822 | 0 | Assert(attnum >= 0 && attnum < outerslot->tts_nvalid); |
823 | 0 | Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
824 | 0 | resultslot->tts_values[resultnum] = outerslot->tts_values[attnum]; |
825 | 0 | resultslot->tts_isnull[resultnum] = outerslot->tts_isnull[attnum]; |
826 | |
|
827 | 0 | EEO_NEXT(); |
828 | 0 | } |
829 | |
|
830 | 0 | EEO_CASE(EEOP_ASSIGN_SCAN_VAR) |
831 | 0 | { |
832 | 0 | int resultnum = op->d.assign_var.resultnum; |
833 | 0 | int attnum = op->d.assign_var.attnum; |
834 | | |
835 | | /* |
836 | | * We do not need CheckVarSlotCompatibility here; that was taken |
837 | | * care of at compilation time. But see EEOP_INNER_VAR comments. |
838 | | */ |
839 | 0 | Assert(attnum >= 0 && attnum < scanslot->tts_nvalid); |
840 | 0 | Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
841 | 0 | resultslot->tts_values[resultnum] = scanslot->tts_values[attnum]; |
842 | 0 | resultslot->tts_isnull[resultnum] = scanslot->tts_isnull[attnum]; |
843 | |
|
844 | 0 | EEO_NEXT(); |
845 | 0 | } |
846 | |
|
847 | 0 | EEO_CASE(EEOP_ASSIGN_OLD_VAR) |
848 | 0 | { |
849 | 0 | int resultnum = op->d.assign_var.resultnum; |
850 | 0 | int attnum = op->d.assign_var.attnum; |
851 | | |
852 | | /* |
853 | | * We do not need CheckVarSlotCompatibility here; that was taken |
854 | | * care of at compilation time. But see EEOP_INNER_VAR comments. |
855 | | */ |
856 | 0 | Assert(attnum >= 0 && attnum < oldslot->tts_nvalid); |
857 | 0 | Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
858 | 0 | resultslot->tts_values[resultnum] = oldslot->tts_values[attnum]; |
859 | 0 | resultslot->tts_isnull[resultnum] = oldslot->tts_isnull[attnum]; |
860 | |
|
861 | 0 | EEO_NEXT(); |
862 | 0 | } |
863 | |
|
864 | 0 | EEO_CASE(EEOP_ASSIGN_NEW_VAR) |
865 | 0 | { |
866 | 0 | int resultnum = op->d.assign_var.resultnum; |
867 | 0 | int attnum = op->d.assign_var.attnum; |
868 | | |
869 | | /* |
870 | | * We do not need CheckVarSlotCompatibility here; that was taken |
871 | | * care of at compilation time. But see EEOP_INNER_VAR comments. |
872 | | */ |
873 | 0 | Assert(attnum >= 0 && attnum < newslot->tts_nvalid); |
874 | 0 | Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
875 | 0 | resultslot->tts_values[resultnum] = newslot->tts_values[attnum]; |
876 | 0 | resultslot->tts_isnull[resultnum] = newslot->tts_isnull[attnum]; |
877 | |
|
878 | 0 | EEO_NEXT(); |
879 | 0 | } |
880 | |
|
881 | 0 | EEO_CASE(EEOP_ASSIGN_TMP) |
882 | 0 | { |
883 | 0 | int resultnum = op->d.assign_tmp.resultnum; |
884 | |
|
885 | 0 | Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
886 | 0 | resultslot->tts_values[resultnum] = state->resvalue; |
887 | 0 | resultslot->tts_isnull[resultnum] = state->resnull; |
888 | |
|
889 | 0 | EEO_NEXT(); |
890 | 0 | } |
891 | |
|
892 | 0 | EEO_CASE(EEOP_ASSIGN_TMP_MAKE_RO) |
893 | 0 | { |
894 | 0 | int resultnum = op->d.assign_tmp.resultnum; |
895 | |
|
896 | 0 | Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
897 | 0 | resultslot->tts_isnull[resultnum] = state->resnull; |
898 | 0 | if (!resultslot->tts_isnull[resultnum]) |
899 | 0 | resultslot->tts_values[resultnum] = |
900 | 0 | MakeExpandedObjectReadOnlyInternal(state->resvalue); |
901 | 0 | else |
902 | 0 | resultslot->tts_values[resultnum] = state->resvalue; |
903 | |
|
904 | 0 | EEO_NEXT(); |
905 | 0 | } |
906 | |
|
907 | 0 | EEO_CASE(EEOP_CONST) |
908 | 0 | { |
909 | 0 | *op->resnull = op->d.constval.isnull; |
910 | 0 | *op->resvalue = op->d.constval.value; |
911 | |
|
912 | 0 | EEO_NEXT(); |
913 | 0 | } |
914 | | |
915 | | /* |
916 | | * Function-call implementations. Arguments have previously been |
917 | | * evaluated directly into fcinfo->args. |
918 | | * |
919 | | * As both STRICT checks and function-usage are noticeable performance |
920 | | * wise, and function calls are a very hot-path (they also back |
921 | | * operators!), it's worth having so many separate opcodes. |
922 | | * |
923 | | * Note: the reason for using a temporary variable "d", here and in |
924 | | * other places, is that some compilers think "*op->resvalue = f();" |
925 | | * requires them to evaluate op->resvalue into a register before |
926 | | * calling f(), just in case f() is able to modify op->resvalue |
927 | | * somehow. The extra line of code can save a useless register spill |
928 | | * and reload across the function call. |
929 | | */ |
930 | 0 | EEO_CASE(EEOP_FUNCEXPR) |
931 | 0 | { |
932 | 0 | FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
933 | 0 | Datum d; |
934 | |
|
935 | 0 | fcinfo->isnull = false; |
936 | 0 | d = op->d.func.fn_addr(fcinfo); |
937 | 0 | *op->resvalue = d; |
938 | 0 | *op->resnull = fcinfo->isnull; |
939 | |
|
940 | 0 | EEO_NEXT(); |
941 | 0 | } |
942 | | |
943 | | /* strict function call with more than two arguments */ |
944 | 0 | EEO_CASE(EEOP_FUNCEXPR_STRICT) |
945 | 0 | { |
946 | 0 | FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
947 | 0 | NullableDatum *args = fcinfo->args; |
948 | 0 | int nargs = op->d.func.nargs; |
949 | 0 | Datum d; |
950 | |
|
951 | 0 | Assert(nargs > 2); |
952 | | |
953 | | /* strict function, so check for NULL args */ |
954 | 0 | for (int argno = 0; argno < nargs; argno++) |
955 | 0 | { |
956 | 0 | if (args[argno].isnull) |
957 | 0 | { |
958 | 0 | *op->resnull = true; |
959 | 0 | goto strictfail; |
960 | 0 | } |
961 | 0 | } |
962 | 0 | fcinfo->isnull = false; |
963 | 0 | d = op->d.func.fn_addr(fcinfo); |
964 | 0 | *op->resvalue = d; |
965 | 0 | *op->resnull = fcinfo->isnull; |
966 | |
|
967 | 0 | strictfail: |
968 | 0 | EEO_NEXT(); |
969 | 0 | } |
970 | | |
971 | | /* strict function call with one argument */ |
972 | 0 | EEO_CASE(EEOP_FUNCEXPR_STRICT_1) |
973 | 0 | { |
974 | 0 | FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
975 | 0 | NullableDatum *args = fcinfo->args; |
976 | |
|
977 | 0 | Assert(op->d.func.nargs == 1); |
978 | | |
979 | | /* strict function, so check for NULL args */ |
980 | 0 | if (args[0].isnull) |
981 | 0 | *op->resnull = true; |
982 | 0 | else |
983 | 0 | { |
984 | 0 | Datum d; |
985 | |
|
986 | 0 | fcinfo->isnull = false; |
987 | 0 | d = op->d.func.fn_addr(fcinfo); |
988 | 0 | *op->resvalue = d; |
989 | 0 | *op->resnull = fcinfo->isnull; |
990 | 0 | } |
991 | |
|
992 | 0 | EEO_NEXT(); |
993 | 0 | } |
994 | | |
995 | | /* strict function call with two arguments */ |
996 | 0 | EEO_CASE(EEOP_FUNCEXPR_STRICT_2) |
997 | 0 | { |
998 | 0 | FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
999 | 0 | NullableDatum *args = fcinfo->args; |
1000 | |
|
1001 | 0 | Assert(op->d.func.nargs == 2); |
1002 | | |
1003 | | /* strict function, so check for NULL args */ |
1004 | 0 | if (args[0].isnull || args[1].isnull) |
1005 | 0 | *op->resnull = true; |
1006 | 0 | else |
1007 | 0 | { |
1008 | 0 | Datum d; |
1009 | |
|
1010 | 0 | fcinfo->isnull = false; |
1011 | 0 | d = op->d.func.fn_addr(fcinfo); |
1012 | 0 | *op->resvalue = d; |
1013 | 0 | *op->resnull = fcinfo->isnull; |
1014 | 0 | } |
1015 | |
|
1016 | 0 | EEO_NEXT(); |
1017 | 0 | } |
1018 | |
|
1019 | 0 | EEO_CASE(EEOP_FUNCEXPR_FUSAGE) |
1020 | 0 | { |
1021 | | /* not common enough to inline */ |
1022 | 0 | ExecEvalFuncExprFusage(state, op, econtext); |
1023 | |
|
1024 | 0 | EEO_NEXT(); |
1025 | 0 | } |
1026 | |
|
1027 | 0 | EEO_CASE(EEOP_FUNCEXPR_STRICT_FUSAGE) |
1028 | 0 | { |
1029 | | /* not common enough to inline */ |
1030 | 0 | ExecEvalFuncExprStrictFusage(state, op, econtext); |
1031 | |
|
1032 | 0 | EEO_NEXT(); |
1033 | 0 | } |
1034 | | |
1035 | | /* |
1036 | | * If any of its clauses is FALSE, an AND's result is FALSE regardless |
1037 | | * of the states of the rest of the clauses, so we can stop evaluating |
1038 | | * and return FALSE immediately. If none are FALSE and one or more is |
1039 | | * NULL, we return NULL; otherwise we return TRUE. This makes sense |
1040 | | * when you interpret NULL as "don't know": perhaps one of the "don't |
1041 | | * knows" would have been FALSE if we'd known its value. Only when |
1042 | | * all the inputs are known to be TRUE can we state confidently that |
1043 | | * the AND's result is TRUE. |
1044 | | */ |
1045 | 0 | EEO_CASE(EEOP_BOOL_AND_STEP_FIRST) |
1046 | 0 | { |
1047 | 0 | *op->d.boolexpr.anynull = false; |
1048 | | |
1049 | | /* |
1050 | | * EEOP_BOOL_AND_STEP_FIRST resets anynull, otherwise it's the |
1051 | | * same as EEOP_BOOL_AND_STEP - so fall through to that. |
1052 | | */ |
1053 | | |
1054 | | /* FALL THROUGH */ |
1055 | 0 | } |
1056 | |
|
1057 | 0 | EEO_CASE(EEOP_BOOL_AND_STEP) |
1058 | 0 | { |
1059 | 0 | if (*op->resnull) |
1060 | 0 | { |
1061 | 0 | *op->d.boolexpr.anynull = true; |
1062 | 0 | } |
1063 | 0 | else if (!DatumGetBool(*op->resvalue)) |
1064 | 0 | { |
1065 | | /* result is already set to FALSE, need not change it */ |
1066 | | /* bail out early */ |
1067 | 0 | EEO_JUMP(op->d.boolexpr.jumpdone); |
1068 | 0 | } |
1069 | |
|
1070 | 0 | EEO_NEXT(); |
1071 | 0 | } |
1072 | |
|
1073 | 0 | EEO_CASE(EEOP_BOOL_AND_STEP_LAST) |
1074 | 0 | { |
1075 | 0 | if (*op->resnull) |
1076 | 0 | { |
1077 | | /* result is already set to NULL, need not change it */ |
1078 | 0 | } |
1079 | 0 | else if (!DatumGetBool(*op->resvalue)) |
1080 | 0 | { |
1081 | | /* result is already set to FALSE, need not change it */ |
1082 | | |
1083 | | /* |
1084 | | * No point jumping early to jumpdone - would be same target |
1085 | | * (as this is the last argument to the AND expression), |
1086 | | * except more expensive. |
1087 | | */ |
1088 | 0 | } |
1089 | 0 | else if (*op->d.boolexpr.anynull) |
1090 | 0 | { |
1091 | 0 | *op->resvalue = (Datum) 0; |
1092 | 0 | *op->resnull = true; |
1093 | 0 | } |
1094 | 0 | else |
1095 | 0 | { |
1096 | | /* result is already set to TRUE, need not change it */ |
1097 | 0 | } |
1098 | |
|
1099 | 0 | EEO_NEXT(); |
1100 | 0 | } |
1101 | | |
1102 | | /* |
1103 | | * If any of its clauses is TRUE, an OR's result is TRUE regardless of |
1104 | | * the states of the rest of the clauses, so we can stop evaluating |
1105 | | * and return TRUE immediately. If none are TRUE and one or more is |
1106 | | * NULL, we return NULL; otherwise we return FALSE. This makes sense |
1107 | | * when you interpret NULL as "don't know": perhaps one of the "don't |
1108 | | * knows" would have been TRUE if we'd known its value. Only when all |
1109 | | * the inputs are known to be FALSE can we state confidently that the |
1110 | | * OR's result is FALSE. |
1111 | | */ |
1112 | 0 | EEO_CASE(EEOP_BOOL_OR_STEP_FIRST) |
1113 | 0 | { |
1114 | 0 | *op->d.boolexpr.anynull = false; |
1115 | | |
1116 | | /* |
1117 | | * EEOP_BOOL_OR_STEP_FIRST resets anynull, otherwise it's the same |
1118 | | * as EEOP_BOOL_OR_STEP - so fall through to that. |
1119 | | */ |
1120 | | |
1121 | | /* FALL THROUGH */ |
1122 | 0 | } |
1123 | |
|
1124 | 0 | EEO_CASE(EEOP_BOOL_OR_STEP) |
1125 | 0 | { |
1126 | 0 | if (*op->resnull) |
1127 | 0 | { |
1128 | 0 | *op->d.boolexpr.anynull = true; |
1129 | 0 | } |
1130 | 0 | else if (DatumGetBool(*op->resvalue)) |
1131 | 0 | { |
1132 | | /* result is already set to TRUE, need not change it */ |
1133 | | /* bail out early */ |
1134 | 0 | EEO_JUMP(op->d.boolexpr.jumpdone); |
1135 | 0 | } |
1136 | |
|
1137 | 0 | EEO_NEXT(); |
1138 | 0 | } |
1139 | |
|
1140 | 0 | EEO_CASE(EEOP_BOOL_OR_STEP_LAST) |
1141 | 0 | { |
1142 | 0 | if (*op->resnull) |
1143 | 0 | { |
1144 | | /* result is already set to NULL, need not change it */ |
1145 | 0 | } |
1146 | 0 | else if (DatumGetBool(*op->resvalue)) |
1147 | 0 | { |
1148 | | /* result is already set to TRUE, need not change it */ |
1149 | | |
1150 | | /* |
1151 | | * No point jumping to jumpdone - would be same target (as |
1152 | | * this is the last argument to the AND expression), except |
1153 | | * more expensive. |
1154 | | */ |
1155 | 0 | } |
1156 | 0 | else if (*op->d.boolexpr.anynull) |
1157 | 0 | { |
1158 | 0 | *op->resvalue = (Datum) 0; |
1159 | 0 | *op->resnull = true; |
1160 | 0 | } |
1161 | 0 | else |
1162 | 0 | { |
1163 | | /* result is already set to FALSE, need not change it */ |
1164 | 0 | } |
1165 | |
|
1166 | 0 | EEO_NEXT(); |
1167 | 0 | } |
1168 | |
|
1169 | 0 | EEO_CASE(EEOP_BOOL_NOT_STEP) |
1170 | 0 | { |
1171 | | /* |
1172 | | * Evaluation of 'not' is simple... if expr is false, then return |
1173 | | * 'true' and vice versa. It's safe to do this even on a |
1174 | | * nominally null value, so we ignore resnull; that means that |
1175 | | * NULL in produces NULL out, which is what we want. |
1176 | | */ |
1177 | 0 | *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue)); |
1178 | |
|
1179 | 0 | EEO_NEXT(); |
1180 | 0 | } |
1181 | |
|
1182 | 0 | EEO_CASE(EEOP_QUAL) |
1183 | 0 | { |
1184 | | /* simplified version of BOOL_AND_STEP for use by ExecQual() */ |
1185 | | |
1186 | | /* If argument (also result) is false or null ... */ |
1187 | 0 | if (*op->resnull || |
1188 | 0 | !DatumGetBool(*op->resvalue)) |
1189 | 0 | { |
1190 | | /* ... bail out early, returning FALSE */ |
1191 | 0 | *op->resnull = false; |
1192 | 0 | *op->resvalue = BoolGetDatum(false); |
1193 | 0 | EEO_JUMP(op->d.qualexpr.jumpdone); |
1194 | 0 | } |
1195 | | |
1196 | | /* |
1197 | | * Otherwise, leave the TRUE value in place, in case this is the |
1198 | | * last qual. Then, TRUE is the correct answer. |
1199 | | */ |
1200 | |
|
1201 | 0 | EEO_NEXT(); |
1202 | 0 | } |
1203 | |
|
1204 | 0 | EEO_CASE(EEOP_JUMP) |
1205 | 0 | { |
1206 | | /* Unconditionally jump to target step */ |
1207 | 0 | EEO_JUMP(op->d.jump.jumpdone); |
1208 | 0 | } |
1209 | |
|
1210 | 0 | EEO_CASE(EEOP_JUMP_IF_NULL) |
1211 | 0 | { |
1212 | | /* Transfer control if current result is null */ |
1213 | 0 | if (*op->resnull) |
1214 | 0 | EEO_JUMP(op->d.jump.jumpdone); |
1215 | |
|
1216 | 0 | EEO_NEXT(); |
1217 | 0 | } |
1218 | |
|
1219 | 0 | EEO_CASE(EEOP_JUMP_IF_NOT_NULL) |
1220 | 0 | { |
1221 | | /* Transfer control if current result is non-null */ |
1222 | 0 | if (!*op->resnull) |
1223 | 0 | EEO_JUMP(op->d.jump.jumpdone); |
1224 | |
|
1225 | 0 | EEO_NEXT(); |
1226 | 0 | } |
1227 | |
|
1228 | 0 | EEO_CASE(EEOP_JUMP_IF_NOT_TRUE) |
1229 | 0 | { |
1230 | | /* Transfer control if current result is null or false */ |
1231 | 0 | if (*op->resnull || !DatumGetBool(*op->resvalue)) |
1232 | 0 | EEO_JUMP(op->d.jump.jumpdone); |
1233 | |
|
1234 | 0 | EEO_NEXT(); |
1235 | 0 | } |
1236 | |
|
1237 | 0 | EEO_CASE(EEOP_NULLTEST_ISNULL) |
1238 | 0 | { |
1239 | 0 | *op->resvalue = BoolGetDatum(*op->resnull); |
1240 | 0 | *op->resnull = false; |
1241 | |
|
1242 | 0 | EEO_NEXT(); |
1243 | 0 | } |
1244 | |
|
1245 | 0 | EEO_CASE(EEOP_NULLTEST_ISNOTNULL) |
1246 | 0 | { |
1247 | 0 | *op->resvalue = BoolGetDatum(!*op->resnull); |
1248 | 0 | *op->resnull = false; |
1249 | |
|
1250 | 0 | EEO_NEXT(); |
1251 | 0 | } |
1252 | |
|
1253 | 0 | EEO_CASE(EEOP_NULLTEST_ROWISNULL) |
1254 | 0 | { |
1255 | | /* out of line implementation: too large */ |
1256 | 0 | ExecEvalRowNull(state, op, econtext); |
1257 | |
|
1258 | 0 | EEO_NEXT(); |
1259 | 0 | } |
1260 | |
|
1261 | 0 | EEO_CASE(EEOP_NULLTEST_ROWISNOTNULL) |
1262 | 0 | { |
1263 | | /* out of line implementation: too large */ |
1264 | 0 | ExecEvalRowNotNull(state, op, econtext); |
1265 | |
|
1266 | 0 | EEO_NEXT(); |
1267 | 0 | } |
1268 | | |
1269 | | /* BooleanTest implementations for all booltesttypes */ |
1270 | |
|
1271 | 0 | EEO_CASE(EEOP_BOOLTEST_IS_TRUE) |
1272 | 0 | { |
1273 | 0 | if (*op->resnull) |
1274 | 0 | { |
1275 | 0 | *op->resvalue = BoolGetDatum(false); |
1276 | 0 | *op->resnull = false; |
1277 | 0 | } |
1278 | | /* else, input value is the correct output as well */ |
1279 | |
|
1280 | 0 | EEO_NEXT(); |
1281 | 0 | } |
1282 | |
|
1283 | 0 | EEO_CASE(EEOP_BOOLTEST_IS_NOT_TRUE) |
1284 | 0 | { |
1285 | 0 | if (*op->resnull) |
1286 | 0 | { |
1287 | 0 | *op->resvalue = BoolGetDatum(true); |
1288 | 0 | *op->resnull = false; |
1289 | 0 | } |
1290 | 0 | else |
1291 | 0 | *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue)); |
1292 | |
|
1293 | 0 | EEO_NEXT(); |
1294 | 0 | } |
1295 | |
|
1296 | 0 | EEO_CASE(EEOP_BOOLTEST_IS_FALSE) |
1297 | 0 | { |
1298 | 0 | if (*op->resnull) |
1299 | 0 | { |
1300 | 0 | *op->resvalue = BoolGetDatum(false); |
1301 | 0 | *op->resnull = false; |
1302 | 0 | } |
1303 | 0 | else |
1304 | 0 | *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue)); |
1305 | |
|
1306 | 0 | EEO_NEXT(); |
1307 | 0 | } |
1308 | |
|
1309 | 0 | EEO_CASE(EEOP_BOOLTEST_IS_NOT_FALSE) |
1310 | 0 | { |
1311 | 0 | if (*op->resnull) |
1312 | 0 | { |
1313 | 0 | *op->resvalue = BoolGetDatum(true); |
1314 | 0 | *op->resnull = false; |
1315 | 0 | } |
1316 | | /* else, input value is the correct output as well */ |
1317 | |
|
1318 | 0 | EEO_NEXT(); |
1319 | 0 | } |
1320 | |
|
1321 | 0 | EEO_CASE(EEOP_PARAM_EXEC) |
1322 | 0 | { |
1323 | | /* out of line implementation: too large */ |
1324 | 0 | ExecEvalParamExec(state, op, econtext); |
1325 | |
|
1326 | 0 | EEO_NEXT(); |
1327 | 0 | } |
1328 | |
|
1329 | 0 | EEO_CASE(EEOP_PARAM_EXTERN) |
1330 | 0 | { |
1331 | | /* out of line implementation: too large */ |
1332 | 0 | ExecEvalParamExtern(state, op, econtext); |
1333 | 0 | EEO_NEXT(); |
1334 | 0 | } |
1335 | |
|
1336 | 0 | EEO_CASE(EEOP_PARAM_CALLBACK) |
1337 | 0 | { |
1338 | | /* allow an extension module to supply a PARAM_EXTERN value */ |
1339 | 0 | op->d.cparam.paramfunc(state, op, econtext); |
1340 | 0 | EEO_NEXT(); |
1341 | 0 | } |
1342 | |
|
1343 | 0 | EEO_CASE(EEOP_PARAM_SET) |
1344 | 0 | { |
1345 | | /* out of line, unlikely to matter performance-wise */ |
1346 | 0 | ExecEvalParamSet(state, op, econtext); |
1347 | 0 | EEO_NEXT(); |
1348 | 0 | } |
1349 | |
|
1350 | 0 | EEO_CASE(EEOP_CASE_TESTVAL) |
1351 | 0 | { |
1352 | 0 | *op->resvalue = *op->d.casetest.value; |
1353 | 0 | *op->resnull = *op->d.casetest.isnull; |
1354 | |
|
1355 | 0 | EEO_NEXT(); |
1356 | 0 | } |
1357 | |
|
1358 | 0 | EEO_CASE(EEOP_CASE_TESTVAL_EXT) |
1359 | 0 | { |
1360 | 0 | *op->resvalue = econtext->caseValue_datum; |
1361 | 0 | *op->resnull = econtext->caseValue_isNull; |
1362 | |
|
1363 | 0 | EEO_NEXT(); |
1364 | 0 | } |
1365 | |
|
1366 | 0 | EEO_CASE(EEOP_MAKE_READONLY) |
1367 | 0 | { |
1368 | | /* |
1369 | | * Force a varlena value that might be read multiple times to R/O |
1370 | | */ |
1371 | 0 | if (!*op->d.make_readonly.isnull) |
1372 | 0 | *op->resvalue = |
1373 | 0 | MakeExpandedObjectReadOnlyInternal(*op->d.make_readonly.value); |
1374 | 0 | *op->resnull = *op->d.make_readonly.isnull; |
1375 | |
|
1376 | 0 | EEO_NEXT(); |
1377 | 0 | } |
1378 | |
|
1379 | 0 | EEO_CASE(EEOP_IOCOERCE) |
1380 | 0 | { |
1381 | | /* |
1382 | | * Evaluate a CoerceViaIO node. This can be quite a hot path, so |
1383 | | * inline as much work as possible. The source value is in our |
1384 | | * result variable. |
1385 | | * |
1386 | | * Also look at ExecEvalCoerceViaIOSafe() if you change anything |
1387 | | * here. |
1388 | | */ |
1389 | 0 | char *str; |
1390 | | |
1391 | | /* call output function (similar to OutputFunctionCall) */ |
1392 | 0 | if (*op->resnull) |
1393 | 0 | { |
1394 | | /* output functions are not called on nulls */ |
1395 | 0 | str = NULL; |
1396 | 0 | } |
1397 | 0 | else |
1398 | 0 | { |
1399 | 0 | FunctionCallInfo fcinfo_out; |
1400 | |
|
1401 | 0 | fcinfo_out = op->d.iocoerce.fcinfo_data_out; |
1402 | 0 | fcinfo_out->args[0].value = *op->resvalue; |
1403 | 0 | fcinfo_out->args[0].isnull = false; |
1404 | |
|
1405 | 0 | fcinfo_out->isnull = false; |
1406 | 0 | str = DatumGetCString(FunctionCallInvoke(fcinfo_out)); |
1407 | | |
1408 | | /* OutputFunctionCall assumes result isn't null */ |
1409 | 0 | Assert(!fcinfo_out->isnull); |
1410 | 0 | } |
1411 | | |
1412 | | /* call input function (similar to InputFunctionCall) */ |
1413 | 0 | if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL) |
1414 | 0 | { |
1415 | 0 | FunctionCallInfo fcinfo_in; |
1416 | 0 | Datum d; |
1417 | |
|
1418 | 0 | fcinfo_in = op->d.iocoerce.fcinfo_data_in; |
1419 | 0 | fcinfo_in->args[0].value = PointerGetDatum(str); |
1420 | 0 | fcinfo_in->args[0].isnull = *op->resnull; |
1421 | | /* second and third arguments are already set up */ |
1422 | |
|
1423 | 0 | fcinfo_in->isnull = false; |
1424 | 0 | d = FunctionCallInvoke(fcinfo_in); |
1425 | 0 | *op->resvalue = d; |
1426 | | |
1427 | | /* Should get null result if and only if str is NULL */ |
1428 | 0 | if (str == NULL) |
1429 | 0 | { |
1430 | 0 | Assert(*op->resnull); |
1431 | 0 | Assert(fcinfo_in->isnull); |
1432 | 0 | } |
1433 | 0 | else |
1434 | 0 | { |
1435 | 0 | Assert(!*op->resnull); |
1436 | 0 | Assert(!fcinfo_in->isnull); |
1437 | 0 | } |
1438 | 0 | } |
1439 | |
|
1440 | 0 | EEO_NEXT(); |
1441 | 0 | } |
1442 | |
|
1443 | 0 | EEO_CASE(EEOP_IOCOERCE_SAFE) |
1444 | 0 | { |
1445 | 0 | ExecEvalCoerceViaIOSafe(state, op); |
1446 | 0 | EEO_NEXT(); |
1447 | 0 | } |
1448 | |
|
1449 | 0 | EEO_CASE(EEOP_DISTINCT) |
1450 | 0 | { |
1451 | | /* |
1452 | | * IS DISTINCT FROM must evaluate arguments (already done into |
1453 | | * fcinfo->args) to determine whether they are NULL; if either is |
1454 | | * NULL then the result is determined. If neither is NULL, then |
1455 | | * proceed to evaluate the comparison function, which is just the |
1456 | | * type's standard equality operator. We need not care whether |
1457 | | * that function is strict. Because the handling of nulls is |
1458 | | * different, we can't just reuse EEOP_FUNCEXPR. |
1459 | | */ |
1460 | 0 | FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
1461 | | |
1462 | | /* check function arguments for NULLness */ |
1463 | 0 | if (fcinfo->args[0].isnull && fcinfo->args[1].isnull) |
1464 | 0 | { |
1465 | | /* Both NULL? Then is not distinct... */ |
1466 | 0 | *op->resvalue = BoolGetDatum(false); |
1467 | 0 | *op->resnull = false; |
1468 | 0 | } |
1469 | 0 | else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull) |
1470 | 0 | { |
1471 | | /* Only one is NULL? Then is distinct... */ |
1472 | 0 | *op->resvalue = BoolGetDatum(true); |
1473 | 0 | *op->resnull = false; |
1474 | 0 | } |
1475 | 0 | else |
1476 | 0 | { |
1477 | | /* Neither null, so apply the equality function */ |
1478 | 0 | Datum eqresult; |
1479 | |
|
1480 | 0 | fcinfo->isnull = false; |
1481 | 0 | eqresult = op->d.func.fn_addr(fcinfo); |
1482 | | /* Must invert result of "="; safe to do even if null */ |
1483 | 0 | *op->resvalue = BoolGetDatum(!DatumGetBool(eqresult)); |
1484 | 0 | *op->resnull = fcinfo->isnull; |
1485 | 0 | } |
1486 | |
|
1487 | 0 | EEO_NEXT(); |
1488 | 0 | } |
1489 | | |
1490 | | /* see EEOP_DISTINCT for comments, this is just inverted */ |
1491 | 0 | EEO_CASE(EEOP_NOT_DISTINCT) |
1492 | 0 | { |
1493 | 0 | FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
1494 | |
|
1495 | 0 | if (fcinfo->args[0].isnull && fcinfo->args[1].isnull) |
1496 | 0 | { |
1497 | 0 | *op->resvalue = BoolGetDatum(true); |
1498 | 0 | *op->resnull = false; |
1499 | 0 | } |
1500 | 0 | else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull) |
1501 | 0 | { |
1502 | 0 | *op->resvalue = BoolGetDatum(false); |
1503 | 0 | *op->resnull = false; |
1504 | 0 | } |
1505 | 0 | else |
1506 | 0 | { |
1507 | 0 | Datum eqresult; |
1508 | |
|
1509 | 0 | fcinfo->isnull = false; |
1510 | 0 | eqresult = op->d.func.fn_addr(fcinfo); |
1511 | 0 | *op->resvalue = eqresult; |
1512 | 0 | *op->resnull = fcinfo->isnull; |
1513 | 0 | } |
1514 | |
|
1515 | 0 | EEO_NEXT(); |
1516 | 0 | } |
1517 | |
|
1518 | 0 | EEO_CASE(EEOP_NULLIF) |
1519 | 0 | { |
1520 | | /* |
1521 | | * The arguments are already evaluated into fcinfo->args. |
1522 | | */ |
1523 | 0 | FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
1524 | 0 | Datum save_arg0 = fcinfo->args[0].value; |
1525 | | |
1526 | | /* if either argument is NULL they can't be equal */ |
1527 | 0 | if (!fcinfo->args[0].isnull && !fcinfo->args[1].isnull) |
1528 | 0 | { |
1529 | 0 | Datum result; |
1530 | | |
1531 | | /* |
1532 | | * If first argument is of varlena type, it might be an |
1533 | | * expanded datum. We need to ensure that the value passed to |
1534 | | * the comparison function is a read-only pointer. However, |
1535 | | * if we end by returning the first argument, that will be the |
1536 | | * original read-write pointer if it was read-write. |
1537 | | */ |
1538 | 0 | if (op->d.func.make_ro) |
1539 | 0 | fcinfo->args[0].value = |
1540 | 0 | MakeExpandedObjectReadOnlyInternal(save_arg0); |
1541 | |
|
1542 | 0 | fcinfo->isnull = false; |
1543 | 0 | result = op->d.func.fn_addr(fcinfo); |
1544 | | |
1545 | | /* if the arguments are equal return null */ |
1546 | 0 | if (!fcinfo->isnull && DatumGetBool(result)) |
1547 | 0 | { |
1548 | 0 | *op->resvalue = (Datum) 0; |
1549 | 0 | *op->resnull = true; |
1550 | |
|
1551 | 0 | EEO_NEXT(); |
1552 | 0 | } |
1553 | 0 | } |
1554 | | |
1555 | | /* Arguments aren't equal, so return the first one */ |
1556 | 0 | *op->resvalue = save_arg0; |
1557 | 0 | *op->resnull = fcinfo->args[0].isnull; |
1558 | |
|
1559 | 0 | EEO_NEXT(); |
1560 | 0 | } |
1561 | |
|
1562 | 0 | EEO_CASE(EEOP_SQLVALUEFUNCTION) |
1563 | 0 | { |
1564 | | /* |
1565 | | * Doesn't seem worthwhile to have an inline implementation |
1566 | | * efficiency-wise. |
1567 | | */ |
1568 | 0 | ExecEvalSQLValueFunction(state, op); |
1569 | |
|
1570 | 0 | EEO_NEXT(); |
1571 | 0 | } |
1572 | |
|
1573 | 0 | EEO_CASE(EEOP_CURRENTOFEXPR) |
1574 | 0 | { |
1575 | | /* error invocation uses space, and shouldn't ever occur */ |
1576 | 0 | ExecEvalCurrentOfExpr(state, op); |
1577 | |
|
1578 | 0 | EEO_NEXT(); |
1579 | 0 | } |
1580 | |
|
1581 | 0 | EEO_CASE(EEOP_NEXTVALUEEXPR) |
1582 | 0 | { |
1583 | | /* |
1584 | | * Doesn't seem worthwhile to have an inline implementation |
1585 | | * efficiency-wise. |
1586 | | */ |
1587 | 0 | ExecEvalNextValueExpr(state, op); |
1588 | |
|
1589 | 0 | EEO_NEXT(); |
1590 | 0 | } |
1591 | |
|
1592 | 0 | EEO_CASE(EEOP_RETURNINGEXPR) |
1593 | 0 | { |
1594 | | /* |
1595 | | * The next op actually evaluates the expression. If the OLD/NEW |
1596 | | * row doesn't exist, skip that and return NULL. |
1597 | | */ |
1598 | 0 | if (state->flags & op->d.returningexpr.nullflag) |
1599 | 0 | { |
1600 | 0 | *op->resvalue = (Datum) 0; |
1601 | 0 | *op->resnull = true; |
1602 | |
|
1603 | 0 | EEO_JUMP(op->d.returningexpr.jumpdone); |
1604 | 0 | } |
1605 | |
|
1606 | 0 | EEO_NEXT(); |
1607 | 0 | } |
1608 | |
|
1609 | 0 | EEO_CASE(EEOP_ARRAYEXPR) |
1610 | 0 | { |
1611 | | /* too complex for an inline implementation */ |
1612 | 0 | ExecEvalArrayExpr(state, op); |
1613 | |
|
1614 | 0 | EEO_NEXT(); |
1615 | 0 | } |
1616 | |
|
1617 | 0 | EEO_CASE(EEOP_ARRAYCOERCE) |
1618 | 0 | { |
1619 | | /* too complex for an inline implementation */ |
1620 | 0 | ExecEvalArrayCoerce(state, op, econtext); |
1621 | |
|
1622 | 0 | EEO_NEXT(); |
1623 | 0 | } |
1624 | |
|
1625 | 0 | EEO_CASE(EEOP_ROW) |
1626 | 0 | { |
1627 | | /* too complex for an inline implementation */ |
1628 | 0 | ExecEvalRow(state, op); |
1629 | |
|
1630 | 0 | EEO_NEXT(); |
1631 | 0 | } |
1632 | |
|
1633 | 0 | EEO_CASE(EEOP_ROWCOMPARE_STEP) |
1634 | 0 | { |
1635 | 0 | FunctionCallInfo fcinfo = op->d.rowcompare_step.fcinfo_data; |
1636 | 0 | Datum d; |
1637 | | |
1638 | | /* force NULL result if strict fn and NULL input */ |
1639 | 0 | if (op->d.rowcompare_step.finfo->fn_strict && |
1640 | 0 | (fcinfo->args[0].isnull || fcinfo->args[1].isnull)) |
1641 | 0 | { |
1642 | 0 | *op->resnull = true; |
1643 | 0 | EEO_JUMP(op->d.rowcompare_step.jumpnull); |
1644 | 0 | } |
1645 | | |
1646 | | /* Apply comparison function */ |
1647 | 0 | fcinfo->isnull = false; |
1648 | 0 | d = op->d.rowcompare_step.fn_addr(fcinfo); |
1649 | 0 | *op->resvalue = d; |
1650 | | |
1651 | | /* force NULL result if NULL function result */ |
1652 | 0 | if (fcinfo->isnull) |
1653 | 0 | { |
1654 | 0 | *op->resnull = true; |
1655 | 0 | EEO_JUMP(op->d.rowcompare_step.jumpnull); |
1656 | 0 | } |
1657 | 0 | *op->resnull = false; |
1658 | | |
1659 | | /* If unequal, no need to compare remaining columns */ |
1660 | 0 | if (DatumGetInt32(*op->resvalue) != 0) |
1661 | 0 | { |
1662 | 0 | EEO_JUMP(op->d.rowcompare_step.jumpdone); |
1663 | 0 | } |
1664 | |
|
1665 | 0 | EEO_NEXT(); |
1666 | 0 | } |
1667 | |
|
1668 | 0 | EEO_CASE(EEOP_ROWCOMPARE_FINAL) |
1669 | 0 | { |
1670 | 0 | int32 cmpresult = DatumGetInt32(*op->resvalue); |
1671 | 0 | CompareType cmptype = op->d.rowcompare_final.cmptype; |
1672 | |
|
1673 | 0 | *op->resnull = false; |
1674 | 0 | switch (cmptype) |
1675 | 0 | { |
1676 | | /* EQ and NE cases aren't allowed here */ |
1677 | 0 | case COMPARE_LT: |
1678 | 0 | *op->resvalue = BoolGetDatum(cmpresult < 0); |
1679 | 0 | break; |
1680 | 0 | case COMPARE_LE: |
1681 | 0 | *op->resvalue = BoolGetDatum(cmpresult <= 0); |
1682 | 0 | break; |
1683 | 0 | case COMPARE_GE: |
1684 | 0 | *op->resvalue = BoolGetDatum(cmpresult >= 0); |
1685 | 0 | break; |
1686 | 0 | case COMPARE_GT: |
1687 | 0 | *op->resvalue = BoolGetDatum(cmpresult > 0); |
1688 | 0 | break; |
1689 | 0 | default: |
1690 | 0 | Assert(false); |
1691 | 0 | break; |
1692 | 0 | } |
1693 | | |
1694 | 0 | EEO_NEXT(); |
1695 | 0 | } |
1696 | | |
1697 | 0 | EEO_CASE(EEOP_MINMAX) |
1698 | 0 | { |
1699 | | /* too complex for an inline implementation */ |
1700 | 0 | ExecEvalMinMax(state, op); |
1701 | |
|
1702 | 0 | EEO_NEXT(); |
1703 | 0 | } |
1704 | |
|
1705 | 0 | EEO_CASE(EEOP_FIELDSELECT) |
1706 | 0 | { |
1707 | | /* too complex for an inline implementation */ |
1708 | 0 | ExecEvalFieldSelect(state, op, econtext); |
1709 | |
|
1710 | 0 | EEO_NEXT(); |
1711 | 0 | } |
1712 | |
|
1713 | 0 | EEO_CASE(EEOP_FIELDSTORE_DEFORM) |
1714 | 0 | { |
1715 | | /* too complex for an inline implementation */ |
1716 | 0 | ExecEvalFieldStoreDeForm(state, op, econtext); |
1717 | |
|
1718 | 0 | EEO_NEXT(); |
1719 | 0 | } |
1720 | |
|
1721 | 0 | EEO_CASE(EEOP_FIELDSTORE_FORM) |
1722 | 0 | { |
1723 | | /* too complex for an inline implementation */ |
1724 | 0 | ExecEvalFieldStoreForm(state, op, econtext); |
1725 | |
|
1726 | 0 | EEO_NEXT(); |
1727 | 0 | } |
1728 | |
|
1729 | 0 | EEO_CASE(EEOP_SBSREF_SUBSCRIPTS) |
1730 | 0 | { |
1731 | | /* Precheck SubscriptingRef subscript(s) */ |
1732 | 0 | if (op->d.sbsref_subscript.subscriptfunc(state, op, econtext)) |
1733 | 0 | { |
1734 | 0 | EEO_NEXT(); |
1735 | 0 | } |
1736 | 0 | else |
1737 | 0 | { |
1738 | | /* Subscript is null, short-circuit SubscriptingRef to NULL */ |
1739 | 0 | EEO_JUMP(op->d.sbsref_subscript.jumpdone); |
1740 | 0 | } |
1741 | 0 | } |
1742 | |
|
1743 | 0 | EEO_CASE(EEOP_SBSREF_OLD) |
1744 | 0 | EEO_CASE(EEOP_SBSREF_ASSIGN) |
1745 | 0 | EEO_CASE(EEOP_SBSREF_FETCH) |
1746 | 0 | { |
1747 | | /* Perform a SubscriptingRef fetch or assignment */ |
1748 | 0 | op->d.sbsref.subscriptfunc(state, op, econtext); |
1749 | |
|
1750 | 0 | EEO_NEXT(); |
1751 | 0 | } |
1752 | |
|
1753 | 0 | EEO_CASE(EEOP_CONVERT_ROWTYPE) |
1754 | 0 | { |
1755 | | /* too complex for an inline implementation */ |
1756 | 0 | ExecEvalConvertRowtype(state, op, econtext); |
1757 | |
|
1758 | 0 | EEO_NEXT(); |
1759 | 0 | } |
1760 | |
|
1761 | 0 | EEO_CASE(EEOP_SCALARARRAYOP) |
1762 | 0 | { |
1763 | | /* too complex for an inline implementation */ |
1764 | 0 | ExecEvalScalarArrayOp(state, op); |
1765 | |
|
1766 | 0 | EEO_NEXT(); |
1767 | 0 | } |
1768 | |
|
1769 | 0 | EEO_CASE(EEOP_HASHED_SCALARARRAYOP) |
1770 | 0 | { |
1771 | | /* too complex for an inline implementation */ |
1772 | 0 | ExecEvalHashedScalarArrayOp(state, op, econtext); |
1773 | |
|
1774 | 0 | EEO_NEXT(); |
1775 | 0 | } |
1776 | |
|
1777 | 0 | EEO_CASE(EEOP_DOMAIN_TESTVAL) |
1778 | 0 | { |
1779 | 0 | *op->resvalue = *op->d.casetest.value; |
1780 | 0 | *op->resnull = *op->d.casetest.isnull; |
1781 | |
|
1782 | 0 | EEO_NEXT(); |
1783 | 0 | } |
1784 | |
|
1785 | 0 | EEO_CASE(EEOP_DOMAIN_TESTVAL_EXT) |
1786 | 0 | { |
1787 | 0 | *op->resvalue = econtext->domainValue_datum; |
1788 | 0 | *op->resnull = econtext->domainValue_isNull; |
1789 | |
|
1790 | 0 | EEO_NEXT(); |
1791 | 0 | } |
1792 | |
|
1793 | 0 | EEO_CASE(EEOP_DOMAIN_NOTNULL) |
1794 | 0 | { |
1795 | | /* too complex for an inline implementation */ |
1796 | 0 | ExecEvalConstraintNotNull(state, op); |
1797 | |
|
1798 | 0 | EEO_NEXT(); |
1799 | 0 | } |
1800 | |
|
1801 | 0 | EEO_CASE(EEOP_DOMAIN_CHECK) |
1802 | 0 | { |
1803 | | /* too complex for an inline implementation */ |
1804 | 0 | ExecEvalConstraintCheck(state, op); |
1805 | |
|
1806 | 0 | EEO_NEXT(); |
1807 | 0 | } |
1808 | |
|
1809 | 0 | EEO_CASE(EEOP_HASHDATUM_SET_INITVAL) |
1810 | 0 | { |
1811 | 0 | *op->resvalue = op->d.hashdatum_initvalue.init_value; |
1812 | 0 | *op->resnull = false; |
1813 | |
|
1814 | 0 | EEO_NEXT(); |
1815 | 0 | } |
1816 | |
|
1817 | 0 | EEO_CASE(EEOP_HASHDATUM_FIRST) |
1818 | 0 | { |
1819 | 0 | FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data; |
1820 | | |
1821 | | /* |
1822 | | * Save the Datum on non-null inputs, otherwise store 0 so that |
1823 | | * subsequent NEXT32 operations combine with an initialized value. |
1824 | | */ |
1825 | 0 | if (!fcinfo->args[0].isnull) |
1826 | 0 | *op->resvalue = op->d.hashdatum.fn_addr(fcinfo); |
1827 | 0 | else |
1828 | 0 | *op->resvalue = (Datum) 0; |
1829 | |
|
1830 | 0 | *op->resnull = false; |
1831 | |
|
1832 | 0 | EEO_NEXT(); |
1833 | 0 | } |
1834 | |
|
1835 | 0 | EEO_CASE(EEOP_HASHDATUM_FIRST_STRICT) |
1836 | 0 | { |
1837 | 0 | FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data; |
1838 | |
|
1839 | 0 | if (fcinfo->args[0].isnull) |
1840 | 0 | { |
1841 | | /* |
1842 | | * With strict we have the expression return NULL instead of |
1843 | | * ignoring NULL input values. We've nothing more to do after |
1844 | | * finding a NULL. |
1845 | | */ |
1846 | 0 | state->resnull = true; |
1847 | 0 | state->resvalue = (Datum) 0; |
1848 | 0 | EEO_JUMP(op->d.hashdatum.jumpdone); |
1849 | 0 | } |
1850 | | |
1851 | | /* execute the hash function and save the resulting value */ |
1852 | 0 | *op->resvalue = op->d.hashdatum.fn_addr(fcinfo); |
1853 | 0 | *op->resnull = false; |
1854 | |
|
1855 | 0 | EEO_NEXT(); |
1856 | 0 | } |
1857 | |
|
1858 | 0 | EEO_CASE(EEOP_HASHDATUM_NEXT32) |
1859 | 0 | { |
1860 | 0 | FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data; |
1861 | 0 | uint32 existinghash; |
1862 | |
|
1863 | 0 | existinghash = DatumGetUInt32(op->d.hashdatum.iresult->value); |
1864 | | /* combine successive hash values by rotating */ |
1865 | 0 | existinghash = pg_rotate_left32(existinghash, 1); |
1866 | | |
1867 | | /* leave the hash value alone on NULL inputs */ |
1868 | 0 | if (!fcinfo->args[0].isnull) |
1869 | 0 | { |
1870 | 0 | uint32 hashvalue; |
1871 | | |
1872 | | /* execute hash func and combine with previous hash value */ |
1873 | 0 | hashvalue = DatumGetUInt32(op->d.hashdatum.fn_addr(fcinfo)); |
1874 | 0 | existinghash = existinghash ^ hashvalue; |
1875 | 0 | } |
1876 | |
|
1877 | 0 | *op->resvalue = UInt32GetDatum(existinghash); |
1878 | 0 | *op->resnull = false; |
1879 | |
|
1880 | 0 | EEO_NEXT(); |
1881 | 0 | } |
1882 | |
|
1883 | 0 | EEO_CASE(EEOP_HASHDATUM_NEXT32_STRICT) |
1884 | 0 | { |
1885 | 0 | FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data; |
1886 | |
|
1887 | 0 | if (fcinfo->args[0].isnull) |
1888 | 0 | { |
1889 | | /* |
1890 | | * With strict we have the expression return NULL instead of |
1891 | | * ignoring NULL input values. We've nothing more to do after |
1892 | | * finding a NULL. |
1893 | | */ |
1894 | 0 | state->resnull = true; |
1895 | 0 | state->resvalue = (Datum) 0; |
1896 | 0 | EEO_JUMP(op->d.hashdatum.jumpdone); |
1897 | 0 | } |
1898 | 0 | else |
1899 | 0 | { |
1900 | 0 | uint32 existinghash; |
1901 | 0 | uint32 hashvalue; |
1902 | |
|
1903 | 0 | existinghash = DatumGetUInt32(op->d.hashdatum.iresult->value); |
1904 | | /* combine successive hash values by rotating */ |
1905 | 0 | existinghash = pg_rotate_left32(existinghash, 1); |
1906 | | |
1907 | | /* execute hash func and combine with previous hash value */ |
1908 | 0 | hashvalue = DatumGetUInt32(op->d.hashdatum.fn_addr(fcinfo)); |
1909 | 0 | *op->resvalue = UInt32GetDatum(existinghash ^ hashvalue); |
1910 | 0 | *op->resnull = false; |
1911 | 0 | } |
1912 | |
|
1913 | 0 | EEO_NEXT(); |
1914 | 0 | } |
1915 | |
|
1916 | 0 | EEO_CASE(EEOP_XMLEXPR) |
1917 | 0 | { |
1918 | | /* too complex for an inline implementation */ |
1919 | 0 | ExecEvalXmlExpr(state, op); |
1920 | |
|
1921 | 0 | EEO_NEXT(); |
1922 | 0 | } |
1923 | |
|
1924 | 0 | EEO_CASE(EEOP_JSON_CONSTRUCTOR) |
1925 | 0 | { |
1926 | | /* too complex for an inline implementation */ |
1927 | 0 | ExecEvalJsonConstructor(state, op, econtext); |
1928 | 0 | EEO_NEXT(); |
1929 | 0 | } |
1930 | |
|
1931 | 0 | EEO_CASE(EEOP_IS_JSON) |
1932 | 0 | { |
1933 | | /* too complex for an inline implementation */ |
1934 | 0 | ExecEvalJsonIsPredicate(state, op); |
1935 | |
|
1936 | 0 | EEO_NEXT(); |
1937 | 0 | } |
1938 | |
|
1939 | 0 | EEO_CASE(EEOP_JSONEXPR_PATH) |
1940 | 0 | { |
1941 | | /* too complex for an inline implementation */ |
1942 | 0 | EEO_JUMP(ExecEvalJsonExprPath(state, op, econtext)); |
1943 | 0 | } |
1944 | |
|
1945 | 0 | EEO_CASE(EEOP_JSONEXPR_COERCION) |
1946 | 0 | { |
1947 | | /* too complex for an inline implementation */ |
1948 | 0 | ExecEvalJsonCoercion(state, op, econtext); |
1949 | |
|
1950 | 0 | EEO_NEXT(); |
1951 | 0 | } |
1952 | |
|
1953 | 0 | EEO_CASE(EEOP_JSONEXPR_COERCION_FINISH) |
1954 | 0 | { |
1955 | | /* too complex for an inline implementation */ |
1956 | 0 | ExecEvalJsonCoercionFinish(state, op); |
1957 | |
|
1958 | 0 | EEO_NEXT(); |
1959 | 0 | } |
1960 | |
|
1961 | 0 | EEO_CASE(EEOP_AGGREF) |
1962 | 0 | { |
1963 | | /* |
1964 | | * Returns a Datum whose value is the precomputed aggregate value |
1965 | | * found in the given expression context. |
1966 | | */ |
1967 | 0 | int aggno = op->d.aggref.aggno; |
1968 | |
|
1969 | 0 | Assert(econtext->ecxt_aggvalues != NULL); |
1970 | |
|
1971 | 0 | *op->resvalue = econtext->ecxt_aggvalues[aggno]; |
1972 | 0 | *op->resnull = econtext->ecxt_aggnulls[aggno]; |
1973 | |
|
1974 | 0 | EEO_NEXT(); |
1975 | 0 | } |
1976 | |
|
1977 | 0 | EEO_CASE(EEOP_GROUPING_FUNC) |
1978 | 0 | { |
1979 | | /* too complex/uncommon for an inline implementation */ |
1980 | 0 | ExecEvalGroupingFunc(state, op); |
1981 | |
|
1982 | 0 | EEO_NEXT(); |
1983 | 0 | } |
1984 | |
|
1985 | 0 | EEO_CASE(EEOP_WINDOW_FUNC) |
1986 | 0 | { |
1987 | | /* |
1988 | | * Like Aggref, just return a precomputed value from the econtext. |
1989 | | */ |
1990 | 0 | WindowFuncExprState *wfunc = op->d.window_func.wfstate; |
1991 | |
|
1992 | 0 | Assert(econtext->ecxt_aggvalues != NULL); |
1993 | |
|
1994 | 0 | *op->resvalue = econtext->ecxt_aggvalues[wfunc->wfuncno]; |
1995 | 0 | *op->resnull = econtext->ecxt_aggnulls[wfunc->wfuncno]; |
1996 | |
|
1997 | 0 | EEO_NEXT(); |
1998 | 0 | } |
1999 | |
|
2000 | 0 | EEO_CASE(EEOP_MERGE_SUPPORT_FUNC) |
2001 | 0 | { |
2002 | | /* too complex/uncommon for an inline implementation */ |
2003 | 0 | ExecEvalMergeSupportFunc(state, op, econtext); |
2004 | |
|
2005 | 0 | EEO_NEXT(); |
2006 | 0 | } |
2007 | |
|
2008 | 0 | EEO_CASE(EEOP_SUBPLAN) |
2009 | 0 | { |
2010 | | /* too complex for an inline implementation */ |
2011 | 0 | ExecEvalSubPlan(state, op, econtext); |
2012 | |
|
2013 | 0 | EEO_NEXT(); |
2014 | 0 | } |
2015 | | |
2016 | | /* evaluate a strict aggregate deserialization function */ |
2017 | 0 | EEO_CASE(EEOP_AGG_STRICT_DESERIALIZE) |
2018 | 0 | { |
2019 | | /* Don't call a strict deserialization function with NULL input */ |
2020 | 0 | if (op->d.agg_deserialize.fcinfo_data->args[0].isnull) |
2021 | 0 | EEO_JUMP(op->d.agg_deserialize.jumpnull); |
2022 | | |
2023 | | /* fallthrough */ |
2024 | 0 | } |
2025 | | |
2026 | | /* evaluate aggregate deserialization function (non-strict portion) */ |
2027 | 0 | EEO_CASE(EEOP_AGG_DESERIALIZE) |
2028 | 0 | { |
2029 | 0 | FunctionCallInfo fcinfo = op->d.agg_deserialize.fcinfo_data; |
2030 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2031 | 0 | MemoryContext oldContext; |
2032 | | |
2033 | | /* |
2034 | | * We run the deserialization functions in per-input-tuple memory |
2035 | | * context. |
2036 | | */ |
2037 | 0 | oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory); |
2038 | 0 | fcinfo->isnull = false; |
2039 | 0 | *op->resvalue = FunctionCallInvoke(fcinfo); |
2040 | 0 | *op->resnull = fcinfo->isnull; |
2041 | 0 | MemoryContextSwitchTo(oldContext); |
2042 | |
|
2043 | 0 | EEO_NEXT(); |
2044 | 0 | } |
2045 | | |
2046 | | /* |
2047 | | * Check that a strict aggregate transition / combination function's |
2048 | | * input is not NULL. |
2049 | | */ |
2050 | | |
2051 | | /* when checking more than one argument */ |
2052 | 0 | EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS) |
2053 | 0 | { |
2054 | 0 | NullableDatum *args = op->d.agg_strict_input_check.args; |
2055 | 0 | int nargs = op->d.agg_strict_input_check.nargs; |
2056 | |
|
2057 | 0 | Assert(nargs > 1); |
2058 | |
|
2059 | 0 | for (int argno = 0; argno < nargs; argno++) |
2060 | 0 | { |
2061 | 0 | if (args[argno].isnull) |
2062 | 0 | EEO_JUMP(op->d.agg_strict_input_check.jumpnull); |
2063 | 0 | } |
2064 | 0 | EEO_NEXT(); |
2065 | 0 | } |
2066 | | |
2067 | | /* special case for just one argument */ |
2068 | 0 | EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1) |
2069 | 0 | { |
2070 | 0 | NullableDatum *args = op->d.agg_strict_input_check.args; |
2071 | 0 | PG_USED_FOR_ASSERTS_ONLY int nargs = op->d.agg_strict_input_check.nargs; |
2072 | |
|
2073 | 0 | Assert(nargs == 1); |
2074 | |
|
2075 | 0 | if (args[0].isnull) |
2076 | 0 | EEO_JUMP(op->d.agg_strict_input_check.jumpnull); |
2077 | 0 | EEO_NEXT(); |
2078 | 0 | } |
2079 | |
|
2080 | 0 | EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_NULLS) |
2081 | 0 | { |
2082 | 0 | bool *nulls = op->d.agg_strict_input_check.nulls; |
2083 | 0 | int nargs = op->d.agg_strict_input_check.nargs; |
2084 | |
|
2085 | 0 | for (int argno = 0; argno < nargs; argno++) |
2086 | 0 | { |
2087 | 0 | if (nulls[argno]) |
2088 | 0 | EEO_JUMP(op->d.agg_strict_input_check.jumpnull); |
2089 | 0 | } |
2090 | 0 | EEO_NEXT(); |
2091 | 0 | } |
2092 | | |
2093 | | /* |
2094 | | * Check for a NULL pointer to the per-group states. |
2095 | | */ |
2096 | |
|
2097 | 0 | EEO_CASE(EEOP_AGG_PLAIN_PERGROUP_NULLCHECK) |
2098 | 0 | { |
2099 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2100 | 0 | AggStatePerGroup pergroup_allaggs = |
2101 | 0 | aggstate->all_pergroups[op->d.agg_plain_pergroup_nullcheck.setoff]; |
2102 | |
|
2103 | 0 | if (pergroup_allaggs == NULL) |
2104 | 0 | EEO_JUMP(op->d.agg_plain_pergroup_nullcheck.jumpnull); |
2105 | |
|
2106 | 0 | EEO_NEXT(); |
2107 | 0 | } |
2108 | | |
2109 | | /* |
2110 | | * Different types of aggregate transition functions are implemented |
2111 | | * as different types of steps, to avoid incurring unnecessary |
2112 | | * overhead. There's a step type for each valid combination of having |
2113 | | * a by value / by reference transition type, [not] needing to the |
2114 | | * initialize the transition value for the first row in a group from |
2115 | | * input, and [not] strict transition function. |
2116 | | * |
2117 | | * Could optimize further by splitting off by-reference for |
2118 | | * fixed-length types, but currently that doesn't seem worth it. |
2119 | | */ |
2120 | |
|
2121 | 0 | EEO_CASE(EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL) |
2122 | 0 | { |
2123 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2124 | 0 | AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
2125 | 0 | AggStatePerGroup pergroup = |
2126 | 0 | &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
2127 | |
|
2128 | 0 | Assert(pertrans->transtypeByVal); |
2129 | |
|
2130 | 0 | if (pergroup->noTransValue) |
2131 | 0 | { |
2132 | | /* If transValue has not yet been initialized, do so now. */ |
2133 | 0 | ExecAggInitGroup(aggstate, pertrans, pergroup, |
2134 | 0 | op->d.agg_trans.aggcontext); |
2135 | | /* copied trans value from input, done this round */ |
2136 | 0 | } |
2137 | 0 | else if (likely(!pergroup->transValueIsNull)) |
2138 | 0 | { |
2139 | | /* invoke transition function, unless prevented by strictness */ |
2140 | 0 | ExecAggPlainTransByVal(aggstate, pertrans, pergroup, |
2141 | 0 | op->d.agg_trans.aggcontext, |
2142 | 0 | op->d.agg_trans.setno); |
2143 | 0 | } |
2144 | |
|
2145 | 0 | EEO_NEXT(); |
2146 | 0 | } |
2147 | | |
2148 | | /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
2149 | 0 | EEO_CASE(EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL) |
2150 | 0 | { |
2151 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2152 | 0 | AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
2153 | 0 | AggStatePerGroup pergroup = |
2154 | 0 | &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
2155 | |
|
2156 | 0 | Assert(pertrans->transtypeByVal); |
2157 | |
|
2158 | 0 | if (likely(!pergroup->transValueIsNull)) |
2159 | 0 | ExecAggPlainTransByVal(aggstate, pertrans, pergroup, |
2160 | 0 | op->d.agg_trans.aggcontext, |
2161 | 0 | op->d.agg_trans.setno); |
2162 | |
|
2163 | 0 | EEO_NEXT(); |
2164 | 0 | } |
2165 | | |
2166 | | /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
2167 | 0 | EEO_CASE(EEOP_AGG_PLAIN_TRANS_BYVAL) |
2168 | 0 | { |
2169 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2170 | 0 | AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
2171 | 0 | AggStatePerGroup pergroup = |
2172 | 0 | &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
2173 | |
|
2174 | 0 | Assert(pertrans->transtypeByVal); |
2175 | |
|
2176 | 0 | ExecAggPlainTransByVal(aggstate, pertrans, pergroup, |
2177 | 0 | op->d.agg_trans.aggcontext, |
2178 | 0 | op->d.agg_trans.setno); |
2179 | |
|
2180 | 0 | EEO_NEXT(); |
2181 | 0 | } |
2182 | | |
2183 | | /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
2184 | 0 | EEO_CASE(EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF) |
2185 | 0 | { |
2186 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2187 | 0 | AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
2188 | 0 | AggStatePerGroup pergroup = |
2189 | 0 | &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
2190 | |
|
2191 | 0 | Assert(!pertrans->transtypeByVal); |
2192 | |
|
2193 | 0 | if (pergroup->noTransValue) |
2194 | 0 | ExecAggInitGroup(aggstate, pertrans, pergroup, |
2195 | 0 | op->d.agg_trans.aggcontext); |
2196 | 0 | else if (likely(!pergroup->transValueIsNull)) |
2197 | 0 | ExecAggPlainTransByRef(aggstate, pertrans, pergroup, |
2198 | 0 | op->d.agg_trans.aggcontext, |
2199 | 0 | op->d.agg_trans.setno); |
2200 | |
|
2201 | 0 | EEO_NEXT(); |
2202 | 0 | } |
2203 | | |
2204 | | /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
2205 | 0 | EEO_CASE(EEOP_AGG_PLAIN_TRANS_STRICT_BYREF) |
2206 | 0 | { |
2207 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2208 | 0 | AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
2209 | 0 | AggStatePerGroup pergroup = |
2210 | 0 | &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
2211 | |
|
2212 | 0 | Assert(!pertrans->transtypeByVal); |
2213 | |
|
2214 | 0 | if (likely(!pergroup->transValueIsNull)) |
2215 | 0 | ExecAggPlainTransByRef(aggstate, pertrans, pergroup, |
2216 | 0 | op->d.agg_trans.aggcontext, |
2217 | 0 | op->d.agg_trans.setno); |
2218 | 0 | EEO_NEXT(); |
2219 | 0 | } |
2220 | | |
2221 | | /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
2222 | 0 | EEO_CASE(EEOP_AGG_PLAIN_TRANS_BYREF) |
2223 | 0 | { |
2224 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2225 | 0 | AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
2226 | 0 | AggStatePerGroup pergroup = |
2227 | 0 | &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
2228 | |
|
2229 | 0 | Assert(!pertrans->transtypeByVal); |
2230 | |
|
2231 | 0 | ExecAggPlainTransByRef(aggstate, pertrans, pergroup, |
2232 | 0 | op->d.agg_trans.aggcontext, |
2233 | 0 | op->d.agg_trans.setno); |
2234 | |
|
2235 | 0 | EEO_NEXT(); |
2236 | 0 | } |
2237 | |
|
2238 | 0 | EEO_CASE(EEOP_AGG_PRESORTED_DISTINCT_SINGLE) |
2239 | 0 | { |
2240 | 0 | AggStatePerTrans pertrans = op->d.agg_presorted_distinctcheck.pertrans; |
2241 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2242 | |
|
2243 | 0 | if (ExecEvalPreOrderedDistinctSingle(aggstate, pertrans)) |
2244 | 0 | EEO_NEXT(); |
2245 | 0 | else |
2246 | 0 | EEO_JUMP(op->d.agg_presorted_distinctcheck.jumpdistinct); |
2247 | 0 | } |
2248 | |
|
2249 | 0 | EEO_CASE(EEOP_AGG_PRESORTED_DISTINCT_MULTI) |
2250 | 0 | { |
2251 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
2252 | 0 | AggStatePerTrans pertrans = op->d.agg_presorted_distinctcheck.pertrans; |
2253 | |
|
2254 | 0 | if (ExecEvalPreOrderedDistinctMulti(aggstate, pertrans)) |
2255 | 0 | EEO_NEXT(); |
2256 | 0 | else |
2257 | 0 | EEO_JUMP(op->d.agg_presorted_distinctcheck.jumpdistinct); |
2258 | 0 | } |
2259 | | |
2260 | | /* process single-column ordered aggregate datum */ |
2261 | 0 | EEO_CASE(EEOP_AGG_ORDERED_TRANS_DATUM) |
2262 | 0 | { |
2263 | | /* too complex for an inline implementation */ |
2264 | 0 | ExecEvalAggOrderedTransDatum(state, op, econtext); |
2265 | |
|
2266 | 0 | EEO_NEXT(); |
2267 | 0 | } |
2268 | | |
2269 | | /* process multi-column ordered aggregate tuple */ |
2270 | 0 | EEO_CASE(EEOP_AGG_ORDERED_TRANS_TUPLE) |
2271 | 0 | { |
2272 | | /* too complex for an inline implementation */ |
2273 | 0 | ExecEvalAggOrderedTransTuple(state, op, econtext); |
2274 | |
|
2275 | 0 | EEO_NEXT(); |
2276 | 0 | } |
2277 | |
|
2278 | 0 | EEO_CASE(EEOP_LAST) |
2279 | 0 | { |
2280 | | /* unreachable */ |
2281 | 0 | Assert(false); |
2282 | 0 | goto out_error; |
2283 | 0 | } |
2284 | 0 | } |
2285 | | |
2286 | 0 | out_error: |
2287 | 0 | pg_unreachable(); |
2288 | 0 | return (Datum) 0; |
2289 | 0 | } |
2290 | | |
2291 | | /* |
2292 | | * Expression evaluation callback that performs extra checks before executing |
2293 | | * the expression. Declared extern so other methods of execution can use it |
2294 | | * too. |
2295 | | */ |
2296 | | Datum |
2297 | | ExecInterpExprStillValid(ExprState *state, ExprContext *econtext, bool *isNull) |
2298 | 0 | { |
2299 | | /* |
2300 | | * First time through, check whether attribute matches Var. Might not be |
2301 | | * ok anymore, due to schema changes. |
2302 | | */ |
2303 | 0 | CheckExprStillValid(state, econtext); |
2304 | | |
2305 | | /* skip the check during further executions */ |
2306 | 0 | state->evalfunc = (ExprStateEvalFunc) state->evalfunc_private; |
2307 | | |
2308 | | /* and actually execute */ |
2309 | 0 | return state->evalfunc(state, econtext, isNull); |
2310 | 0 | } |
2311 | | |
2312 | | /* |
2313 | | * Check that an expression is still valid in the face of potential schema |
2314 | | * changes since the plan has been created. |
2315 | | */ |
2316 | | void |
2317 | | CheckExprStillValid(ExprState *state, ExprContext *econtext) |
2318 | 0 | { |
2319 | 0 | TupleTableSlot *innerslot; |
2320 | 0 | TupleTableSlot *outerslot; |
2321 | 0 | TupleTableSlot *scanslot; |
2322 | 0 | TupleTableSlot *oldslot; |
2323 | 0 | TupleTableSlot *newslot; |
2324 | |
|
2325 | 0 | innerslot = econtext->ecxt_innertuple; |
2326 | 0 | outerslot = econtext->ecxt_outertuple; |
2327 | 0 | scanslot = econtext->ecxt_scantuple; |
2328 | 0 | oldslot = econtext->ecxt_oldtuple; |
2329 | 0 | newslot = econtext->ecxt_newtuple; |
2330 | |
|
2331 | 0 | for (int i = 0; i < state->steps_len; i++) |
2332 | 0 | { |
2333 | 0 | ExprEvalStep *op = &state->steps[i]; |
2334 | |
|
2335 | 0 | switch (ExecEvalStepOp(state, op)) |
2336 | 0 | { |
2337 | 0 | case EEOP_INNER_VAR: |
2338 | 0 | { |
2339 | 0 | int attnum = op->d.var.attnum; |
2340 | |
|
2341 | 0 | CheckVarSlotCompatibility(innerslot, attnum + 1, op->d.var.vartype); |
2342 | 0 | break; |
2343 | 0 | } |
2344 | | |
2345 | 0 | case EEOP_OUTER_VAR: |
2346 | 0 | { |
2347 | 0 | int attnum = op->d.var.attnum; |
2348 | |
|
2349 | 0 | CheckVarSlotCompatibility(outerslot, attnum + 1, op->d.var.vartype); |
2350 | 0 | break; |
2351 | 0 | } |
2352 | | |
2353 | 0 | case EEOP_SCAN_VAR: |
2354 | 0 | { |
2355 | 0 | int attnum = op->d.var.attnum; |
2356 | |
|
2357 | 0 | CheckVarSlotCompatibility(scanslot, attnum + 1, op->d.var.vartype); |
2358 | 0 | break; |
2359 | 0 | } |
2360 | | |
2361 | 0 | case EEOP_OLD_VAR: |
2362 | 0 | { |
2363 | 0 | int attnum = op->d.var.attnum; |
2364 | |
|
2365 | 0 | CheckVarSlotCompatibility(oldslot, attnum + 1, op->d.var.vartype); |
2366 | 0 | break; |
2367 | 0 | } |
2368 | | |
2369 | 0 | case EEOP_NEW_VAR: |
2370 | 0 | { |
2371 | 0 | int attnum = op->d.var.attnum; |
2372 | |
|
2373 | 0 | CheckVarSlotCompatibility(newslot, attnum + 1, op->d.var.vartype); |
2374 | 0 | break; |
2375 | 0 | } |
2376 | 0 | default: |
2377 | 0 | break; |
2378 | 0 | } |
2379 | 0 | } |
2380 | 0 | } |
2381 | | |
2382 | | /* |
2383 | | * Check whether a user attribute in a slot can be referenced by a Var |
2384 | | * expression. This should succeed unless there have been schema changes |
2385 | | * since the expression tree has been created. |
2386 | | */ |
2387 | | static void |
2388 | | CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype) |
2389 | 0 | { |
2390 | | /* |
2391 | | * What we have to check for here is the possibility of an attribute |
2392 | | * having been dropped or changed in type since the plan tree was created. |
2393 | | * Ideally the plan will get invalidated and not re-used, but just in |
2394 | | * case, we keep these defenses. Fortunately it's sufficient to check |
2395 | | * once on the first time through. |
2396 | | * |
2397 | | * Note: ideally we'd check typmod as well as typid, but that seems |
2398 | | * impractical at the moment: in many cases the tupdesc will have been |
2399 | | * generated by ExecTypeFromTL(), and that can't guarantee to generate an |
2400 | | * accurate typmod in all cases, because some expression node types don't |
2401 | | * carry typmod. Fortunately, for precisely that reason, there should be |
2402 | | * no places with a critical dependency on the typmod of a value. |
2403 | | * |
2404 | | * System attributes don't require checking since their types never |
2405 | | * change. |
2406 | | */ |
2407 | 0 | if (attnum > 0) |
2408 | 0 | { |
2409 | 0 | TupleDesc slot_tupdesc = slot->tts_tupleDescriptor; |
2410 | 0 | Form_pg_attribute attr; |
2411 | |
|
2412 | 0 | if (attnum > slot_tupdesc->natts) /* should never happen */ |
2413 | 0 | elog(ERROR, "attribute number %d exceeds number of columns %d", |
2414 | 0 | attnum, slot_tupdesc->natts); |
2415 | | |
2416 | 0 | attr = TupleDescAttr(slot_tupdesc, attnum - 1); |
2417 | | |
2418 | | /* Internal error: somebody forgot to expand it. */ |
2419 | 0 | if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL) |
2420 | 0 | elog(ERROR, "unexpected virtual generated column reference"); |
2421 | | |
2422 | 0 | if (attr->attisdropped) |
2423 | 0 | ereport(ERROR, |
2424 | 0 | (errcode(ERRCODE_UNDEFINED_COLUMN), |
2425 | 0 | errmsg("attribute %d of type %s has been dropped", |
2426 | 0 | attnum, format_type_be(slot_tupdesc->tdtypeid)))); |
2427 | | |
2428 | 0 | if (vartype != attr->atttypid) |
2429 | 0 | ereport(ERROR, |
2430 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
2431 | 0 | errmsg("attribute %d of type %s has wrong type", |
2432 | 0 | attnum, format_type_be(slot_tupdesc->tdtypeid)), |
2433 | 0 | errdetail("Table has type %s, but query expects %s.", |
2434 | 0 | format_type_be(attr->atttypid), |
2435 | 0 | format_type_be(vartype)))); |
2436 | 0 | } |
2437 | 0 | } |
2438 | | |
2439 | | /* |
2440 | | * Verify that the slot is compatible with a EEOP_*_FETCHSOME operation. |
2441 | | */ |
2442 | | static void |
2443 | | CheckOpSlotCompatibility(ExprEvalStep *op, TupleTableSlot *slot) |
2444 | 0 | { |
2445 | | #ifdef USE_ASSERT_CHECKING |
2446 | | /* there's nothing to check */ |
2447 | | if (!op->d.fetch.fixed) |
2448 | | return; |
2449 | | |
2450 | | /* |
2451 | | * Should probably fixed at some point, but for now it's easier to allow |
2452 | | * buffer and heap tuples to be used interchangeably. |
2453 | | */ |
2454 | | if (slot->tts_ops == &TTSOpsBufferHeapTuple && |
2455 | | op->d.fetch.kind == &TTSOpsHeapTuple) |
2456 | | return; |
2457 | | if (slot->tts_ops == &TTSOpsHeapTuple && |
2458 | | op->d.fetch.kind == &TTSOpsBufferHeapTuple) |
2459 | | return; |
2460 | | |
2461 | | /* |
2462 | | * At the moment we consider it OK if a virtual slot is used instead of a |
2463 | | * specific type of slot, as a virtual slot never needs to be deformed. |
2464 | | */ |
2465 | | if (slot->tts_ops == &TTSOpsVirtual) |
2466 | | return; |
2467 | | |
2468 | | Assert(op->d.fetch.kind == slot->tts_ops); |
2469 | | #endif |
2470 | 0 | } |
2471 | | |
2472 | | /* |
2473 | | * get_cached_rowtype: utility function to lookup a rowtype tupdesc |
2474 | | * |
2475 | | * type_id, typmod: identity of the rowtype |
2476 | | * rowcache: space for caching identity info |
2477 | | * (rowcache->cacheptr must be initialized to NULL) |
2478 | | * changed: if not NULL, *changed is set to true on any update |
2479 | | * |
2480 | | * The returned TupleDesc is not guaranteed pinned; caller must pin it |
2481 | | * to use it across any operation that might incur cache invalidation, |
2482 | | * including for example detoasting of input tuples. |
2483 | | * (The TupleDesc is always refcounted, so just use IncrTupleDescRefCount.) |
2484 | | * |
2485 | | * NOTE: because composite types can change contents, we must be prepared |
2486 | | * to re-do this during any node execution; cannot call just once during |
2487 | | * expression initialization. |
2488 | | */ |
2489 | | static TupleDesc |
2490 | | get_cached_rowtype(Oid type_id, int32 typmod, |
2491 | | ExprEvalRowtypeCache *rowcache, |
2492 | | bool *changed) |
2493 | 0 | { |
2494 | 0 | if (type_id != RECORDOID) |
2495 | 0 | { |
2496 | | /* |
2497 | | * It's a named composite type, so use the regular typcache. Do a |
2498 | | * lookup first time through, or if the composite type changed. Note: |
2499 | | * "tupdesc_id == 0" may look redundant, but it protects against the |
2500 | | * admittedly-theoretical possibility that type_id was RECORDOID the |
2501 | | * last time through, so that the cacheptr isn't TypeCacheEntry *. |
2502 | | */ |
2503 | 0 | TypeCacheEntry *typentry = (TypeCacheEntry *) rowcache->cacheptr; |
2504 | |
|
2505 | 0 | if (unlikely(typentry == NULL || |
2506 | 0 | rowcache->tupdesc_id == 0 || |
2507 | 0 | typentry->tupDesc_identifier != rowcache->tupdesc_id)) |
2508 | 0 | { |
2509 | 0 | typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC); |
2510 | 0 | if (typentry->tupDesc == NULL) |
2511 | 0 | ereport(ERROR, |
2512 | 0 | (errcode(ERRCODE_WRONG_OBJECT_TYPE), |
2513 | 0 | errmsg("type %s is not composite", |
2514 | 0 | format_type_be(type_id)))); |
2515 | 0 | rowcache->cacheptr = typentry; |
2516 | 0 | rowcache->tupdesc_id = typentry->tupDesc_identifier; |
2517 | 0 | if (changed) |
2518 | 0 | *changed = true; |
2519 | 0 | } |
2520 | 0 | return typentry->tupDesc; |
2521 | 0 | } |
2522 | 0 | else |
2523 | 0 | { |
2524 | | /* |
2525 | | * A RECORD type, once registered, doesn't change for the life of the |
2526 | | * backend. So we don't need a typcache entry as such, which is good |
2527 | | * because there isn't one. It's possible that the caller is asking |
2528 | | * about a different type than before, though. |
2529 | | */ |
2530 | 0 | TupleDesc tupDesc = (TupleDesc) rowcache->cacheptr; |
2531 | |
|
2532 | 0 | if (unlikely(tupDesc == NULL || |
2533 | 0 | rowcache->tupdesc_id != 0 || |
2534 | 0 | type_id != tupDesc->tdtypeid || |
2535 | 0 | typmod != tupDesc->tdtypmod)) |
2536 | 0 | { |
2537 | 0 | tupDesc = lookup_rowtype_tupdesc(type_id, typmod); |
2538 | | /* Drop pin acquired by lookup_rowtype_tupdesc */ |
2539 | 0 | ReleaseTupleDesc(tupDesc); |
2540 | 0 | rowcache->cacheptr = tupDesc; |
2541 | 0 | rowcache->tupdesc_id = 0; /* not a valid value for non-RECORD */ |
2542 | 0 | if (changed) |
2543 | 0 | *changed = true; |
2544 | 0 | } |
2545 | 0 | return tupDesc; |
2546 | 0 | } |
2547 | 0 | } |
2548 | | |
2549 | | |
2550 | | /* |
2551 | | * Fast-path functions, for very simple expressions |
2552 | | */ |
2553 | | |
2554 | | /* implementation of ExecJust(Inner|Outer|Scan)Var */ |
2555 | | static pg_always_inline Datum |
2556 | | ExecJustVarImpl(ExprState *state, TupleTableSlot *slot, bool *isnull) |
2557 | 0 | { |
2558 | 0 | ExprEvalStep *op = &state->steps[1]; |
2559 | 0 | int attnum = op->d.var.attnum + 1; |
2560 | |
|
2561 | 0 | CheckOpSlotCompatibility(&state->steps[0], slot); |
2562 | | |
2563 | | /* |
2564 | | * Since we use slot_getattr(), we don't need to implement the FETCHSOME |
2565 | | * step explicitly, and we also needn't Assert that the attnum is in range |
2566 | | * --- slot_getattr() will take care of any problems. |
2567 | | */ |
2568 | 0 | return slot_getattr(slot, attnum, isnull); |
2569 | 0 | } |
2570 | | |
2571 | | /* Simple reference to inner Var */ |
2572 | | static Datum |
2573 | | ExecJustInnerVar(ExprState *state, ExprContext *econtext, bool *isnull) |
2574 | 0 | { |
2575 | 0 | return ExecJustVarImpl(state, econtext->ecxt_innertuple, isnull); |
2576 | 0 | } |
2577 | | |
2578 | | /* Simple reference to outer Var */ |
2579 | | static Datum |
2580 | | ExecJustOuterVar(ExprState *state, ExprContext *econtext, bool *isnull) |
2581 | 0 | { |
2582 | 0 | return ExecJustVarImpl(state, econtext->ecxt_outertuple, isnull); |
2583 | 0 | } |
2584 | | |
2585 | | /* Simple reference to scan Var */ |
2586 | | static Datum |
2587 | | ExecJustScanVar(ExprState *state, ExprContext *econtext, bool *isnull) |
2588 | 0 | { |
2589 | 0 | return ExecJustVarImpl(state, econtext->ecxt_scantuple, isnull); |
2590 | 0 | } |
2591 | | |
2592 | | /* implementation of ExecJustAssign(Inner|Outer|Scan)Var */ |
2593 | | static pg_always_inline Datum |
2594 | | ExecJustAssignVarImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull) |
2595 | 0 | { |
2596 | 0 | ExprEvalStep *op = &state->steps[1]; |
2597 | 0 | int attnum = op->d.assign_var.attnum + 1; |
2598 | 0 | int resultnum = op->d.assign_var.resultnum; |
2599 | 0 | TupleTableSlot *outslot = state->resultslot; |
2600 | |
|
2601 | 0 | CheckOpSlotCompatibility(&state->steps[0], inslot); |
2602 | | |
2603 | | /* |
2604 | | * We do not need CheckVarSlotCompatibility here; that was taken care of |
2605 | | * at compilation time. |
2606 | | * |
2607 | | * Since we use slot_getattr(), we don't need to implement the FETCHSOME |
2608 | | * step explicitly, and we also needn't Assert that the attnum is in range |
2609 | | * --- slot_getattr() will take care of any problems. Nonetheless, check |
2610 | | * that resultnum is in range. |
2611 | | */ |
2612 | 0 | Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts); |
2613 | 0 | outslot->tts_values[resultnum] = |
2614 | 0 | slot_getattr(inslot, attnum, &outslot->tts_isnull[resultnum]); |
2615 | 0 | return 0; |
2616 | 0 | } |
2617 | | |
2618 | | /* Evaluate inner Var and assign to appropriate column of result tuple */ |
2619 | | static Datum |
2620 | | ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull) |
2621 | 0 | { |
2622 | 0 | return ExecJustAssignVarImpl(state, econtext->ecxt_innertuple, isnull); |
2623 | 0 | } |
2624 | | |
2625 | | /* Evaluate outer Var and assign to appropriate column of result tuple */ |
2626 | | static Datum |
2627 | | ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull) |
2628 | 0 | { |
2629 | 0 | return ExecJustAssignVarImpl(state, econtext->ecxt_outertuple, isnull); |
2630 | 0 | } |
2631 | | |
2632 | | /* Evaluate scan Var and assign to appropriate column of result tuple */ |
2633 | | static Datum |
2634 | | ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull) |
2635 | 0 | { |
2636 | 0 | return ExecJustAssignVarImpl(state, econtext->ecxt_scantuple, isnull); |
2637 | 0 | } |
2638 | | |
2639 | | /* Evaluate CASE_TESTVAL and apply a strict function to it */ |
2640 | | static Datum |
2641 | | ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull) |
2642 | 0 | { |
2643 | 0 | ExprEvalStep *op = &state->steps[0]; |
2644 | 0 | FunctionCallInfo fcinfo; |
2645 | 0 | NullableDatum *args; |
2646 | 0 | int nargs; |
2647 | 0 | Datum d; |
2648 | | |
2649 | | /* |
2650 | | * XXX with some redesign of the CaseTestExpr mechanism, maybe we could |
2651 | | * get rid of this data shuffling? |
2652 | | */ |
2653 | 0 | *op->resvalue = *op->d.casetest.value; |
2654 | 0 | *op->resnull = *op->d.casetest.isnull; |
2655 | |
|
2656 | 0 | op++; |
2657 | |
|
2658 | 0 | nargs = op->d.func.nargs; |
2659 | 0 | fcinfo = op->d.func.fcinfo_data; |
2660 | 0 | args = fcinfo->args; |
2661 | | |
2662 | | /* strict function, so check for NULL args */ |
2663 | 0 | for (int argno = 0; argno < nargs; argno++) |
2664 | 0 | { |
2665 | 0 | if (args[argno].isnull) |
2666 | 0 | { |
2667 | 0 | *isnull = true; |
2668 | 0 | return (Datum) 0; |
2669 | 0 | } |
2670 | 0 | } |
2671 | 0 | fcinfo->isnull = false; |
2672 | 0 | d = op->d.func.fn_addr(fcinfo); |
2673 | 0 | *isnull = fcinfo->isnull; |
2674 | 0 | return d; |
2675 | 0 | } |
2676 | | |
2677 | | /* Simple Const expression */ |
2678 | | static Datum |
2679 | | ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull) |
2680 | 0 | { |
2681 | 0 | ExprEvalStep *op = &state->steps[0]; |
2682 | |
|
2683 | 0 | *isnull = op->d.constval.isnull; |
2684 | 0 | return op->d.constval.value; |
2685 | 0 | } |
2686 | | |
2687 | | /* implementation of ExecJust(Inner|Outer|Scan)VarVirt */ |
2688 | | static pg_always_inline Datum |
2689 | | ExecJustVarVirtImpl(ExprState *state, TupleTableSlot *slot, bool *isnull) |
2690 | 0 | { |
2691 | 0 | ExprEvalStep *op = &state->steps[0]; |
2692 | 0 | int attnum = op->d.var.attnum; |
2693 | | |
2694 | | /* |
2695 | | * As it is guaranteed that a virtual slot is used, there never is a need |
2696 | | * to perform tuple deforming (nor would it be possible). Therefore |
2697 | | * execExpr.c has not emitted an EEOP_*_FETCHSOME step. Verify, as much as |
2698 | | * possible, that that determination was accurate. |
2699 | | */ |
2700 | 0 | Assert(TTS_IS_VIRTUAL(slot)); |
2701 | 0 | Assert(TTS_FIXED(slot)); |
2702 | 0 | Assert(attnum >= 0 && attnum < slot->tts_nvalid); |
2703 | |
|
2704 | 0 | *isnull = slot->tts_isnull[attnum]; |
2705 | |
|
2706 | 0 | return slot->tts_values[attnum]; |
2707 | 0 | } |
2708 | | |
2709 | | /* Like ExecJustInnerVar, optimized for virtual slots */ |
2710 | | static Datum |
2711 | | ExecJustInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
2712 | 0 | { |
2713 | 0 | return ExecJustVarVirtImpl(state, econtext->ecxt_innertuple, isnull); |
2714 | 0 | } |
2715 | | |
2716 | | /* Like ExecJustOuterVar, optimized for virtual slots */ |
2717 | | static Datum |
2718 | | ExecJustOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
2719 | 0 | { |
2720 | 0 | return ExecJustVarVirtImpl(state, econtext->ecxt_outertuple, isnull); |
2721 | 0 | } |
2722 | | |
2723 | | /* Like ExecJustScanVar, optimized for virtual slots */ |
2724 | | static Datum |
2725 | | ExecJustScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
2726 | 0 | { |
2727 | 0 | return ExecJustVarVirtImpl(state, econtext->ecxt_scantuple, isnull); |
2728 | 0 | } |
2729 | | |
2730 | | /* implementation of ExecJustAssign(Inner|Outer|Scan)VarVirt */ |
2731 | | static pg_always_inline Datum |
2732 | | ExecJustAssignVarVirtImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull) |
2733 | 0 | { |
2734 | 0 | ExprEvalStep *op = &state->steps[0]; |
2735 | 0 | int attnum = op->d.assign_var.attnum; |
2736 | 0 | int resultnum = op->d.assign_var.resultnum; |
2737 | 0 | TupleTableSlot *outslot = state->resultslot; |
2738 | | |
2739 | | /* see ExecJustVarVirtImpl for comments */ |
2740 | |
|
2741 | 0 | Assert(TTS_IS_VIRTUAL(inslot)); |
2742 | 0 | Assert(TTS_FIXED(inslot)); |
2743 | 0 | Assert(attnum >= 0 && attnum < inslot->tts_nvalid); |
2744 | 0 | Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts); |
2745 | |
|
2746 | 0 | outslot->tts_values[resultnum] = inslot->tts_values[attnum]; |
2747 | 0 | outslot->tts_isnull[resultnum] = inslot->tts_isnull[attnum]; |
2748 | |
|
2749 | 0 | return 0; |
2750 | 0 | } |
2751 | | |
2752 | | /* Like ExecJustAssignInnerVar, optimized for virtual slots */ |
2753 | | static Datum |
2754 | | ExecJustAssignInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
2755 | 0 | { |
2756 | 0 | return ExecJustAssignVarVirtImpl(state, econtext->ecxt_innertuple, isnull); |
2757 | 0 | } |
2758 | | |
2759 | | /* Like ExecJustAssignOuterVar, optimized for virtual slots */ |
2760 | | static Datum |
2761 | | ExecJustAssignOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
2762 | 0 | { |
2763 | 0 | return ExecJustAssignVarVirtImpl(state, econtext->ecxt_outertuple, isnull); |
2764 | 0 | } |
2765 | | |
2766 | | /* Like ExecJustAssignScanVar, optimized for virtual slots */ |
2767 | | static Datum |
2768 | | ExecJustAssignScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
2769 | 0 | { |
2770 | 0 | return ExecJustAssignVarVirtImpl(state, econtext->ecxt_scantuple, isnull); |
2771 | 0 | } |
2772 | | |
2773 | | /* |
2774 | | * implementation for hashing an inner Var, seeding with an initial value. |
2775 | | */ |
2776 | | static Datum |
2777 | | ExecJustHashInnerVarWithIV(ExprState *state, ExprContext *econtext, |
2778 | | bool *isnull) |
2779 | 0 | { |
2780 | 0 | ExprEvalStep *fetchop = &state->steps[0]; |
2781 | 0 | ExprEvalStep *setivop = &state->steps[1]; |
2782 | 0 | ExprEvalStep *innervar = &state->steps[2]; |
2783 | 0 | ExprEvalStep *hashop = &state->steps[3]; |
2784 | 0 | FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data; |
2785 | 0 | int attnum = innervar->d.var.attnum; |
2786 | 0 | uint32 hashkey; |
2787 | |
|
2788 | 0 | CheckOpSlotCompatibility(fetchop, econtext->ecxt_innertuple); |
2789 | 0 | slot_getsomeattrs(econtext->ecxt_innertuple, fetchop->d.fetch.last_var); |
2790 | |
|
2791 | 0 | fcinfo->args[0].value = econtext->ecxt_innertuple->tts_values[attnum]; |
2792 | 0 | fcinfo->args[0].isnull = econtext->ecxt_innertuple->tts_isnull[attnum]; |
2793 | |
|
2794 | 0 | hashkey = DatumGetUInt32(setivop->d.hashdatum_initvalue.init_value); |
2795 | 0 | hashkey = pg_rotate_left32(hashkey, 1); |
2796 | |
|
2797 | 0 | if (!fcinfo->args[0].isnull) |
2798 | 0 | { |
2799 | 0 | uint32 hashvalue; |
2800 | |
|
2801 | 0 | hashvalue = DatumGetUInt32(hashop->d.hashdatum.fn_addr(fcinfo)); |
2802 | 0 | hashkey = hashkey ^ hashvalue; |
2803 | 0 | } |
2804 | |
|
2805 | 0 | *isnull = false; |
2806 | 0 | return UInt32GetDatum(hashkey); |
2807 | 0 | } |
2808 | | |
2809 | | /* implementation of ExecJustHash(Inner|Outer)Var */ |
2810 | | static pg_always_inline Datum |
2811 | | ExecJustHashVarImpl(ExprState *state, TupleTableSlot *slot, bool *isnull) |
2812 | 0 | { |
2813 | 0 | ExprEvalStep *fetchop = &state->steps[0]; |
2814 | 0 | ExprEvalStep *var = &state->steps[1]; |
2815 | 0 | ExprEvalStep *hashop = &state->steps[2]; |
2816 | 0 | FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data; |
2817 | 0 | int attnum = var->d.var.attnum; |
2818 | |
|
2819 | 0 | CheckOpSlotCompatibility(fetchop, slot); |
2820 | 0 | slot_getsomeattrs(slot, fetchop->d.fetch.last_var); |
2821 | |
|
2822 | 0 | fcinfo->args[0].value = slot->tts_values[attnum]; |
2823 | 0 | fcinfo->args[0].isnull = slot->tts_isnull[attnum]; |
2824 | |
|
2825 | 0 | *isnull = false; |
2826 | |
|
2827 | 0 | if (!fcinfo->args[0].isnull) |
2828 | 0 | return hashop->d.hashdatum.fn_addr(fcinfo); |
2829 | 0 | else |
2830 | 0 | return (Datum) 0; |
2831 | 0 | } |
2832 | | |
2833 | | /* implementation for hashing an outer Var */ |
2834 | | static Datum |
2835 | | ExecJustHashOuterVar(ExprState *state, ExprContext *econtext, bool *isnull) |
2836 | 0 | { |
2837 | 0 | return ExecJustHashVarImpl(state, econtext->ecxt_outertuple, isnull); |
2838 | 0 | } |
2839 | | |
2840 | | /* implementation for hashing an inner Var */ |
2841 | | static Datum |
2842 | | ExecJustHashInnerVar(ExprState *state, ExprContext *econtext, bool *isnull) |
2843 | 0 | { |
2844 | 0 | return ExecJustHashVarImpl(state, econtext->ecxt_innertuple, isnull); |
2845 | 0 | } |
2846 | | |
2847 | | /* implementation of ExecJustHash(Inner|Outer)VarVirt */ |
2848 | | static pg_always_inline Datum |
2849 | | ExecJustHashVarVirtImpl(ExprState *state, TupleTableSlot *slot, bool *isnull) |
2850 | 0 | { |
2851 | 0 | ExprEvalStep *var = &state->steps[0]; |
2852 | 0 | ExprEvalStep *hashop = &state->steps[1]; |
2853 | 0 | FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data; |
2854 | 0 | int attnum = var->d.var.attnum; |
2855 | |
|
2856 | 0 | fcinfo->args[0].value = slot->tts_values[attnum]; |
2857 | 0 | fcinfo->args[0].isnull = slot->tts_isnull[attnum]; |
2858 | |
|
2859 | 0 | *isnull = false; |
2860 | |
|
2861 | 0 | if (!fcinfo->args[0].isnull) |
2862 | 0 | return hashop->d.hashdatum.fn_addr(fcinfo); |
2863 | 0 | else |
2864 | 0 | return (Datum) 0; |
2865 | 0 | } |
2866 | | |
2867 | | /* Like ExecJustHashInnerVar, optimized for virtual slots */ |
2868 | | static Datum |
2869 | | ExecJustHashInnerVarVirt(ExprState *state, ExprContext *econtext, |
2870 | | bool *isnull) |
2871 | 0 | { |
2872 | 0 | return ExecJustHashVarVirtImpl(state, econtext->ecxt_innertuple, isnull); |
2873 | 0 | } |
2874 | | |
2875 | | /* Like ExecJustHashOuterVar, optimized for virtual slots */ |
2876 | | static Datum |
2877 | | ExecJustHashOuterVarVirt(ExprState *state, ExprContext *econtext, |
2878 | | bool *isnull) |
2879 | 0 | { |
2880 | 0 | return ExecJustHashVarVirtImpl(state, econtext->ecxt_outertuple, isnull); |
2881 | 0 | } |
2882 | | |
2883 | | /* |
2884 | | * implementation for hashing an outer Var. Returns NULL on NULL input. |
2885 | | */ |
2886 | | static Datum |
2887 | | ExecJustHashOuterVarStrict(ExprState *state, ExprContext *econtext, |
2888 | | bool *isnull) |
2889 | 0 | { |
2890 | 0 | ExprEvalStep *fetchop = &state->steps[0]; |
2891 | 0 | ExprEvalStep *var = &state->steps[1]; |
2892 | 0 | ExprEvalStep *hashop = &state->steps[2]; |
2893 | 0 | FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data; |
2894 | 0 | int attnum = var->d.var.attnum; |
2895 | |
|
2896 | 0 | CheckOpSlotCompatibility(fetchop, econtext->ecxt_outertuple); |
2897 | 0 | slot_getsomeattrs(econtext->ecxt_outertuple, fetchop->d.fetch.last_var); |
2898 | |
|
2899 | 0 | fcinfo->args[0].value = econtext->ecxt_outertuple->tts_values[attnum]; |
2900 | 0 | fcinfo->args[0].isnull = econtext->ecxt_outertuple->tts_isnull[attnum]; |
2901 | |
|
2902 | 0 | if (!fcinfo->args[0].isnull) |
2903 | 0 | { |
2904 | 0 | *isnull = false; |
2905 | 0 | return hashop->d.hashdatum.fn_addr(fcinfo); |
2906 | 0 | } |
2907 | 0 | else |
2908 | 0 | { |
2909 | | /* return NULL on NULL input */ |
2910 | 0 | *isnull = true; |
2911 | 0 | return (Datum) 0; |
2912 | 0 | } |
2913 | 0 | } |
2914 | | |
2915 | | #if defined(EEO_USE_COMPUTED_GOTO) |
2916 | | /* |
2917 | | * Comparator used when building address->opcode lookup table for |
2918 | | * ExecEvalStepOp() in the threaded dispatch case. |
2919 | | */ |
2920 | | static int |
2921 | | dispatch_compare_ptr(const void *a, const void *b) |
2922 | 0 | { |
2923 | 0 | const ExprEvalOpLookup *la = (const ExprEvalOpLookup *) a; |
2924 | 0 | const ExprEvalOpLookup *lb = (const ExprEvalOpLookup *) b; |
2925 | |
|
2926 | 0 | if (la->opcode < lb->opcode) |
2927 | 0 | return -1; |
2928 | 0 | else if (la->opcode > lb->opcode) |
2929 | 0 | return 1; |
2930 | 0 | return 0; |
2931 | 0 | } |
2932 | | #endif |
2933 | | |
2934 | | /* |
2935 | | * Do one-time initialization of interpretation machinery. |
2936 | | */ |
2937 | | static void |
2938 | | ExecInitInterpreter(void) |
2939 | 0 | { |
2940 | 0 | #if defined(EEO_USE_COMPUTED_GOTO) |
2941 | | /* Set up externally-visible pointer to dispatch table */ |
2942 | 0 | if (dispatch_table == NULL) |
2943 | 0 | { |
2944 | 0 | dispatch_table = (const void **) |
2945 | 0 | DatumGetPointer(ExecInterpExpr(NULL, NULL, NULL)); |
2946 | | |
2947 | | /* build reverse lookup table */ |
2948 | 0 | for (int i = 0; i < EEOP_LAST; i++) |
2949 | 0 | { |
2950 | 0 | reverse_dispatch_table[i].opcode = dispatch_table[i]; |
2951 | 0 | reverse_dispatch_table[i].op = (ExprEvalOp) i; |
2952 | 0 | } |
2953 | | |
2954 | | /* make it bsearch()able */ |
2955 | 0 | qsort(reverse_dispatch_table, |
2956 | 0 | EEOP_LAST /* nmembers */ , |
2957 | 0 | sizeof(ExprEvalOpLookup), |
2958 | 0 | dispatch_compare_ptr); |
2959 | 0 | } |
2960 | 0 | #endif |
2961 | 0 | } |
2962 | | |
2963 | | /* |
2964 | | * Function to return the opcode of an expression step. |
2965 | | * |
2966 | | * When direct-threading is in use, ExprState->opcode isn't easily |
2967 | | * decipherable. This function returns the appropriate enum member. |
2968 | | */ |
2969 | | ExprEvalOp |
2970 | | ExecEvalStepOp(ExprState *state, ExprEvalStep *op) |
2971 | 0 | { |
2972 | 0 | #if defined(EEO_USE_COMPUTED_GOTO) |
2973 | 0 | if (state->flags & EEO_FLAG_DIRECT_THREADED) |
2974 | 0 | { |
2975 | 0 | ExprEvalOpLookup key; |
2976 | 0 | ExprEvalOpLookup *res; |
2977 | |
|
2978 | 0 | key.opcode = (void *) op->opcode; |
2979 | 0 | res = bsearch(&key, |
2980 | 0 | reverse_dispatch_table, |
2981 | 0 | EEOP_LAST /* nmembers */ , |
2982 | 0 | sizeof(ExprEvalOpLookup), |
2983 | 0 | dispatch_compare_ptr); |
2984 | 0 | Assert(res); /* unknown ops shouldn't get looked up */ |
2985 | 0 | return res->op; |
2986 | 0 | } |
2987 | 0 | #endif |
2988 | 0 | return (ExprEvalOp) op->opcode; |
2989 | 0 | } |
2990 | | |
2991 | | |
2992 | | /* |
2993 | | * Out-of-line helper functions for complex instructions. |
2994 | | */ |
2995 | | |
2996 | | /* |
2997 | | * Evaluate EEOP_FUNCEXPR_FUSAGE |
2998 | | */ |
2999 | | void |
3000 | | ExecEvalFuncExprFusage(ExprState *state, ExprEvalStep *op, |
3001 | | ExprContext *econtext) |
3002 | 0 | { |
3003 | 0 | FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
3004 | 0 | PgStat_FunctionCallUsage fcusage; |
3005 | 0 | Datum d; |
3006 | |
|
3007 | 0 | pgstat_init_function_usage(fcinfo, &fcusage); |
3008 | |
|
3009 | 0 | fcinfo->isnull = false; |
3010 | 0 | d = op->d.func.fn_addr(fcinfo); |
3011 | 0 | *op->resvalue = d; |
3012 | 0 | *op->resnull = fcinfo->isnull; |
3013 | |
|
3014 | 0 | pgstat_end_function_usage(&fcusage, true); |
3015 | 0 | } |
3016 | | |
3017 | | /* |
3018 | | * Evaluate EEOP_FUNCEXPR_STRICT_FUSAGE |
3019 | | */ |
3020 | | void |
3021 | | ExecEvalFuncExprStrictFusage(ExprState *state, ExprEvalStep *op, |
3022 | | ExprContext *econtext) |
3023 | 0 | { |
3024 | |
|
3025 | 0 | FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
3026 | 0 | PgStat_FunctionCallUsage fcusage; |
3027 | 0 | NullableDatum *args = fcinfo->args; |
3028 | 0 | int nargs = op->d.func.nargs; |
3029 | 0 | Datum d; |
3030 | | |
3031 | | /* strict function, so check for NULL args */ |
3032 | 0 | for (int argno = 0; argno < nargs; argno++) |
3033 | 0 | { |
3034 | 0 | if (args[argno].isnull) |
3035 | 0 | { |
3036 | 0 | *op->resnull = true; |
3037 | 0 | return; |
3038 | 0 | } |
3039 | 0 | } |
3040 | | |
3041 | 0 | pgstat_init_function_usage(fcinfo, &fcusage); |
3042 | |
|
3043 | 0 | fcinfo->isnull = false; |
3044 | 0 | d = op->d.func.fn_addr(fcinfo); |
3045 | 0 | *op->resvalue = d; |
3046 | 0 | *op->resnull = fcinfo->isnull; |
3047 | |
|
3048 | 0 | pgstat_end_function_usage(&fcusage, true); |
3049 | 0 | } |
3050 | | |
3051 | | /* |
3052 | | * Evaluate a PARAM_EXEC parameter. |
3053 | | * |
3054 | | * PARAM_EXEC params (internal executor parameters) are stored in the |
3055 | | * ecxt_param_exec_vals array, and can be accessed by array index. |
3056 | | */ |
3057 | | void |
3058 | | ExecEvalParamExec(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3059 | 0 | { |
3060 | 0 | ParamExecData *prm; |
3061 | |
|
3062 | 0 | prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]); |
3063 | 0 | if (unlikely(prm->execPlan != NULL)) |
3064 | 0 | { |
3065 | | /* Parameter not evaluated yet, so go do it */ |
3066 | 0 | ExecSetParamPlan(prm->execPlan, econtext); |
3067 | | /* ExecSetParamPlan should have processed this param... */ |
3068 | 0 | Assert(prm->execPlan == NULL); |
3069 | 0 | } |
3070 | 0 | *op->resvalue = prm->value; |
3071 | 0 | *op->resnull = prm->isnull; |
3072 | 0 | } |
3073 | | |
3074 | | /* |
3075 | | * Evaluate a PARAM_EXTERN parameter. |
3076 | | * |
3077 | | * PARAM_EXTERN parameters must be sought in ecxt_param_list_info. |
3078 | | */ |
3079 | | void |
3080 | | ExecEvalParamExtern(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3081 | 0 | { |
3082 | 0 | ParamListInfo paramInfo = econtext->ecxt_param_list_info; |
3083 | 0 | int paramId = op->d.param.paramid; |
3084 | |
|
3085 | 0 | if (likely(paramInfo && |
3086 | 0 | paramId > 0 && paramId <= paramInfo->numParams)) |
3087 | 0 | { |
3088 | 0 | ParamExternData *prm; |
3089 | 0 | ParamExternData prmdata; |
3090 | | |
3091 | | /* give hook a chance in case parameter is dynamic */ |
3092 | 0 | if (paramInfo->paramFetch != NULL) |
3093 | 0 | prm = paramInfo->paramFetch(paramInfo, paramId, false, &prmdata); |
3094 | 0 | else |
3095 | 0 | prm = ¶mInfo->params[paramId - 1]; |
3096 | |
|
3097 | 0 | if (likely(OidIsValid(prm->ptype))) |
3098 | 0 | { |
3099 | | /* safety check in case hook did something unexpected */ |
3100 | 0 | if (unlikely(prm->ptype != op->d.param.paramtype)) |
3101 | 0 | ereport(ERROR, |
3102 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
3103 | 0 | errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)", |
3104 | 0 | paramId, |
3105 | 0 | format_type_be(prm->ptype), |
3106 | 0 | format_type_be(op->d.param.paramtype)))); |
3107 | 0 | *op->resvalue = prm->value; |
3108 | 0 | *op->resnull = prm->isnull; |
3109 | 0 | return; |
3110 | 0 | } |
3111 | 0 | } |
3112 | | |
3113 | 0 | ereport(ERROR, |
3114 | 0 | (errcode(ERRCODE_UNDEFINED_OBJECT), |
3115 | 0 | errmsg("no value found for parameter %d", paramId))); |
3116 | 0 | } |
3117 | | |
3118 | | /* |
3119 | | * Set value of a param (currently always PARAM_EXEC) from |
3120 | | * op->res{value,null}. |
3121 | | */ |
3122 | | void |
3123 | | ExecEvalParamSet(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3124 | 0 | { |
3125 | 0 | ParamExecData *prm; |
3126 | |
|
3127 | 0 | prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]); |
3128 | | |
3129 | | /* Shouldn't have a pending evaluation anymore */ |
3130 | 0 | Assert(prm->execPlan == NULL); |
3131 | |
|
3132 | 0 | prm->value = *op->resvalue; |
3133 | 0 | prm->isnull = *op->resnull; |
3134 | 0 | } |
3135 | | |
3136 | | /* |
3137 | | * Evaluate a CoerceViaIO node in soft-error mode. |
3138 | | * |
3139 | | * The source value is in op's result variable. |
3140 | | * |
3141 | | * Note: This implements EEOP_IOCOERCE_SAFE. If you change anything here, |
3142 | | * also look at the inline code for EEOP_IOCOERCE. |
3143 | | */ |
3144 | | void |
3145 | | ExecEvalCoerceViaIOSafe(ExprState *state, ExprEvalStep *op) |
3146 | 0 | { |
3147 | 0 | char *str; |
3148 | | |
3149 | | /* call output function (similar to OutputFunctionCall) */ |
3150 | 0 | if (*op->resnull) |
3151 | 0 | { |
3152 | | /* output functions are not called on nulls */ |
3153 | 0 | str = NULL; |
3154 | 0 | } |
3155 | 0 | else |
3156 | 0 | { |
3157 | 0 | FunctionCallInfo fcinfo_out; |
3158 | |
|
3159 | 0 | fcinfo_out = op->d.iocoerce.fcinfo_data_out; |
3160 | 0 | fcinfo_out->args[0].value = *op->resvalue; |
3161 | 0 | fcinfo_out->args[0].isnull = false; |
3162 | |
|
3163 | 0 | fcinfo_out->isnull = false; |
3164 | 0 | str = DatumGetCString(FunctionCallInvoke(fcinfo_out)); |
3165 | | |
3166 | | /* OutputFunctionCall assumes result isn't null */ |
3167 | 0 | Assert(!fcinfo_out->isnull); |
3168 | 0 | } |
3169 | | |
3170 | | /* call input function (similar to InputFunctionCallSafe) */ |
3171 | 0 | if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL) |
3172 | 0 | { |
3173 | 0 | FunctionCallInfo fcinfo_in; |
3174 | |
|
3175 | 0 | fcinfo_in = op->d.iocoerce.fcinfo_data_in; |
3176 | 0 | fcinfo_in->args[0].value = PointerGetDatum(str); |
3177 | 0 | fcinfo_in->args[0].isnull = *op->resnull; |
3178 | | /* second and third arguments are already set up */ |
3179 | | |
3180 | | /* ErrorSaveContext must be present. */ |
3181 | 0 | Assert(IsA(fcinfo_in->context, ErrorSaveContext)); |
3182 | |
|
3183 | 0 | fcinfo_in->isnull = false; |
3184 | 0 | *op->resvalue = FunctionCallInvoke(fcinfo_in); |
3185 | |
|
3186 | 0 | if (SOFT_ERROR_OCCURRED(fcinfo_in->context)) |
3187 | 0 | { |
3188 | 0 | *op->resnull = true; |
3189 | 0 | *op->resvalue = (Datum) 0; |
3190 | 0 | return; |
3191 | 0 | } |
3192 | | |
3193 | | /* Should get null result if and only if str is NULL */ |
3194 | 0 | if (str == NULL) |
3195 | 0 | Assert(*op->resnull); |
3196 | 0 | else |
3197 | 0 | Assert(!*op->resnull); |
3198 | 0 | } |
3199 | 0 | } |
3200 | | |
3201 | | /* |
3202 | | * Evaluate a SQLValueFunction expression. |
3203 | | */ |
3204 | | void |
3205 | | ExecEvalSQLValueFunction(ExprState *state, ExprEvalStep *op) |
3206 | 0 | { |
3207 | 0 | LOCAL_FCINFO(fcinfo, 0); |
3208 | 0 | SQLValueFunction *svf = op->d.sqlvaluefunction.svf; |
3209 | |
|
3210 | 0 | *op->resnull = false; |
3211 | | |
3212 | | /* |
3213 | | * Note: current_schema() can return NULL. current_user() etc currently |
3214 | | * cannot, but might as well code those cases the same way for safety. |
3215 | | */ |
3216 | 0 | switch (svf->op) |
3217 | 0 | { |
3218 | 0 | case SVFOP_CURRENT_DATE: |
3219 | 0 | *op->resvalue = DateADTGetDatum(GetSQLCurrentDate()); |
3220 | 0 | break; |
3221 | 0 | case SVFOP_CURRENT_TIME: |
3222 | 0 | case SVFOP_CURRENT_TIME_N: |
3223 | 0 | *op->resvalue = TimeTzADTPGetDatum(GetSQLCurrentTime(svf->typmod)); |
3224 | 0 | break; |
3225 | 0 | case SVFOP_CURRENT_TIMESTAMP: |
3226 | 0 | case SVFOP_CURRENT_TIMESTAMP_N: |
3227 | 0 | *op->resvalue = TimestampTzGetDatum(GetSQLCurrentTimestamp(svf->typmod)); |
3228 | 0 | break; |
3229 | 0 | case SVFOP_LOCALTIME: |
3230 | 0 | case SVFOP_LOCALTIME_N: |
3231 | 0 | *op->resvalue = TimeADTGetDatum(GetSQLLocalTime(svf->typmod)); |
3232 | 0 | break; |
3233 | 0 | case SVFOP_LOCALTIMESTAMP: |
3234 | 0 | case SVFOP_LOCALTIMESTAMP_N: |
3235 | 0 | *op->resvalue = TimestampGetDatum(GetSQLLocalTimestamp(svf->typmod)); |
3236 | 0 | break; |
3237 | 0 | case SVFOP_CURRENT_ROLE: |
3238 | 0 | case SVFOP_CURRENT_USER: |
3239 | 0 | case SVFOP_USER: |
3240 | 0 | InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
3241 | 0 | *op->resvalue = current_user(fcinfo); |
3242 | 0 | *op->resnull = fcinfo->isnull; |
3243 | 0 | break; |
3244 | 0 | case SVFOP_SESSION_USER: |
3245 | 0 | InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
3246 | 0 | *op->resvalue = session_user(fcinfo); |
3247 | 0 | *op->resnull = fcinfo->isnull; |
3248 | 0 | break; |
3249 | 0 | case SVFOP_CURRENT_CATALOG: |
3250 | 0 | InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
3251 | 0 | *op->resvalue = current_database(fcinfo); |
3252 | 0 | *op->resnull = fcinfo->isnull; |
3253 | 0 | break; |
3254 | 0 | case SVFOP_CURRENT_SCHEMA: |
3255 | 0 | InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
3256 | 0 | *op->resvalue = current_schema(fcinfo); |
3257 | 0 | *op->resnull = fcinfo->isnull; |
3258 | 0 | break; |
3259 | 0 | } |
3260 | 0 | } |
3261 | | |
3262 | | /* |
3263 | | * Raise error if a CURRENT OF expression is evaluated. |
3264 | | * |
3265 | | * The planner should convert CURRENT OF into a TidScan qualification, or some |
3266 | | * other special handling in a ForeignScan node. So we have to be able to do |
3267 | | * ExecInitExpr on a CurrentOfExpr, but we shouldn't ever actually execute it. |
3268 | | * If we get here, we suppose we must be dealing with CURRENT OF on a foreign |
3269 | | * table whose FDW doesn't handle it, and complain accordingly. |
3270 | | */ |
3271 | | void |
3272 | | ExecEvalCurrentOfExpr(ExprState *state, ExprEvalStep *op) |
3273 | 0 | { |
3274 | 0 | ereport(ERROR, |
3275 | 0 | (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
3276 | 0 | errmsg("WHERE CURRENT OF is not supported for this table type"))); |
3277 | 0 | } |
3278 | | |
3279 | | /* |
3280 | | * Evaluate NextValueExpr. |
3281 | | */ |
3282 | | void |
3283 | | ExecEvalNextValueExpr(ExprState *state, ExprEvalStep *op) |
3284 | 0 | { |
3285 | 0 | int64 newval = nextval_internal(op->d.nextvalueexpr.seqid, false); |
3286 | |
|
3287 | 0 | switch (op->d.nextvalueexpr.seqtypid) |
3288 | 0 | { |
3289 | 0 | case INT2OID: |
3290 | 0 | *op->resvalue = Int16GetDatum((int16) newval); |
3291 | 0 | break; |
3292 | 0 | case INT4OID: |
3293 | 0 | *op->resvalue = Int32GetDatum((int32) newval); |
3294 | 0 | break; |
3295 | 0 | case INT8OID: |
3296 | 0 | *op->resvalue = Int64GetDatum(newval); |
3297 | 0 | break; |
3298 | 0 | default: |
3299 | 0 | elog(ERROR, "unsupported sequence type %u", |
3300 | 0 | op->d.nextvalueexpr.seqtypid); |
3301 | 0 | } |
3302 | 0 | *op->resnull = false; |
3303 | 0 | } |
3304 | | |
3305 | | /* |
3306 | | * Evaluate NullTest / IS NULL for rows. |
3307 | | */ |
3308 | | void |
3309 | | ExecEvalRowNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3310 | 0 | { |
3311 | 0 | ExecEvalRowNullInt(state, op, econtext, true); |
3312 | 0 | } |
3313 | | |
3314 | | /* |
3315 | | * Evaluate NullTest / IS NOT NULL for rows. |
3316 | | */ |
3317 | | void |
3318 | | ExecEvalRowNotNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3319 | 0 | { |
3320 | 0 | ExecEvalRowNullInt(state, op, econtext, false); |
3321 | 0 | } |
3322 | | |
3323 | | /* Common code for IS [NOT] NULL on a row value */ |
3324 | | static void |
3325 | | ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op, |
3326 | | ExprContext *econtext, bool checkisnull) |
3327 | 0 | { |
3328 | 0 | Datum value = *op->resvalue; |
3329 | 0 | bool isnull = *op->resnull; |
3330 | 0 | HeapTupleHeader tuple; |
3331 | 0 | Oid tupType; |
3332 | 0 | int32 tupTypmod; |
3333 | 0 | TupleDesc tupDesc; |
3334 | 0 | HeapTupleData tmptup; |
3335 | |
|
3336 | 0 | *op->resnull = false; |
3337 | | |
3338 | | /* NULL row variables are treated just as NULL scalar columns */ |
3339 | 0 | if (isnull) |
3340 | 0 | { |
3341 | 0 | *op->resvalue = BoolGetDatum(checkisnull); |
3342 | 0 | return; |
3343 | 0 | } |
3344 | | |
3345 | | /* |
3346 | | * The SQL standard defines IS [NOT] NULL for a non-null rowtype argument |
3347 | | * as: |
3348 | | * |
3349 | | * "R IS NULL" is true if every field is the null value. |
3350 | | * |
3351 | | * "R IS NOT NULL" is true if no field is the null value. |
3352 | | * |
3353 | | * This definition is (apparently intentionally) not recursive; so our |
3354 | | * tests on the fields are primitive attisnull tests, not recursive checks |
3355 | | * to see if they are all-nulls or no-nulls rowtypes. |
3356 | | * |
3357 | | * The standard does not consider the possibility of zero-field rows, but |
3358 | | * here we consider them to vacuously satisfy both predicates. |
3359 | | */ |
3360 | | |
3361 | 0 | tuple = DatumGetHeapTupleHeader(value); |
3362 | |
|
3363 | 0 | tupType = HeapTupleHeaderGetTypeId(tuple); |
3364 | 0 | tupTypmod = HeapTupleHeaderGetTypMod(tuple); |
3365 | | |
3366 | | /* Lookup tupdesc if first time through or if type changes */ |
3367 | 0 | tupDesc = get_cached_rowtype(tupType, tupTypmod, |
3368 | 0 | &op->d.nulltest_row.rowcache, NULL); |
3369 | | |
3370 | | /* |
3371 | | * heap_attisnull needs a HeapTuple not a bare HeapTupleHeader. |
3372 | | */ |
3373 | 0 | tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple); |
3374 | 0 | tmptup.t_data = tuple; |
3375 | | |
3376 | | /* |
3377 | | * NB: It's not necessary to call heap_datum_check() here: Even if the |
3378 | | * tupledesc and tuple are mismatched, no invalid memory accesses or other |
3379 | | * type unsafety occur. It seems useful to be able to test for which |
3380 | | * attributes are actually set when encountering invalid datums. |
3381 | | */ |
3382 | |
|
3383 | 0 | for (int att = 1; att <= tupDesc->natts; att++) |
3384 | 0 | { |
3385 | | /* ignore dropped columns */ |
3386 | 0 | if (TupleDescCompactAttr(tupDesc, att - 1)->attisdropped) |
3387 | 0 | continue; |
3388 | 0 | if (heap_attisnull(&tmptup, att, tupDesc)) |
3389 | 0 | { |
3390 | | /* null field disproves IS NOT NULL */ |
3391 | 0 | if (!checkisnull) |
3392 | 0 | { |
3393 | 0 | *op->resvalue = BoolGetDatum(false); |
3394 | 0 | return; |
3395 | 0 | } |
3396 | 0 | } |
3397 | 0 | else |
3398 | 0 | { |
3399 | | /* non-null field disproves IS NULL */ |
3400 | 0 | if (checkisnull) |
3401 | 0 | { |
3402 | 0 | *op->resvalue = BoolGetDatum(false); |
3403 | 0 | return; |
3404 | 0 | } |
3405 | 0 | } |
3406 | 0 | } |
3407 | | |
3408 | 0 | *op->resvalue = BoolGetDatum(true); |
3409 | 0 | } |
3410 | | |
3411 | | /* |
3412 | | * Evaluate an ARRAY[] expression. |
3413 | | * |
3414 | | * The individual array elements (or subarrays) have already been evaluated |
3415 | | * into op->d.arrayexpr.elemvalues[]/elemnulls[]. |
3416 | | */ |
3417 | | void |
3418 | | ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op) |
3419 | 0 | { |
3420 | 0 | ArrayType *result; |
3421 | 0 | Oid element_type = op->d.arrayexpr.elemtype; |
3422 | 0 | int nelems = op->d.arrayexpr.nelems; |
3423 | 0 | int ndims = 0; |
3424 | 0 | int dims[MAXDIM]; |
3425 | 0 | int lbs[MAXDIM]; |
3426 | | |
3427 | | /* Set non-null as default */ |
3428 | 0 | *op->resnull = false; |
3429 | |
|
3430 | 0 | if (!op->d.arrayexpr.multidims) |
3431 | 0 | { |
3432 | | /* Elements are presumably of scalar type */ |
3433 | 0 | Datum *dvalues = op->d.arrayexpr.elemvalues; |
3434 | 0 | bool *dnulls = op->d.arrayexpr.elemnulls; |
3435 | | |
3436 | | /* setup for 1-D array of the given length */ |
3437 | 0 | ndims = 1; |
3438 | 0 | dims[0] = nelems; |
3439 | 0 | lbs[0] = 1; |
3440 | |
|
3441 | 0 | result = construct_md_array(dvalues, dnulls, ndims, dims, lbs, |
3442 | 0 | element_type, |
3443 | 0 | op->d.arrayexpr.elemlength, |
3444 | 0 | op->d.arrayexpr.elembyval, |
3445 | 0 | op->d.arrayexpr.elemalign); |
3446 | 0 | } |
3447 | 0 | else |
3448 | 0 | { |
3449 | | /* Must be nested array expressions */ |
3450 | 0 | int nbytes = 0; |
3451 | 0 | int nitems; |
3452 | 0 | int outer_nelems = 0; |
3453 | 0 | int elem_ndims = 0; |
3454 | 0 | int *elem_dims = NULL; |
3455 | 0 | int *elem_lbs = NULL; |
3456 | 0 | bool firstone = true; |
3457 | 0 | bool havenulls = false; |
3458 | 0 | bool haveempty = false; |
3459 | 0 | char **subdata; |
3460 | 0 | uint8 **subbitmaps; |
3461 | 0 | int *subbytes; |
3462 | 0 | int *subnitems; |
3463 | 0 | int32 dataoffset; |
3464 | 0 | char *dat; |
3465 | 0 | int iitem; |
3466 | |
|
3467 | 0 | subdata = palloc_array(char *, nelems); |
3468 | 0 | subbitmaps = palloc_array(uint8 *, nelems); |
3469 | 0 | subbytes = palloc_array(int, nelems); |
3470 | 0 | subnitems = palloc_array(int, nelems); |
3471 | | |
3472 | | /* loop through and get data area from each element */ |
3473 | 0 | for (int elemoff = 0; elemoff < nelems; elemoff++) |
3474 | 0 | { |
3475 | 0 | Datum arraydatum; |
3476 | 0 | bool eisnull; |
3477 | 0 | ArrayType *array; |
3478 | 0 | int this_ndims; |
3479 | |
|
3480 | 0 | arraydatum = op->d.arrayexpr.elemvalues[elemoff]; |
3481 | 0 | eisnull = op->d.arrayexpr.elemnulls[elemoff]; |
3482 | | |
3483 | | /* temporarily ignore null subarrays */ |
3484 | 0 | if (eisnull) |
3485 | 0 | { |
3486 | 0 | haveempty = true; |
3487 | 0 | continue; |
3488 | 0 | } |
3489 | | |
3490 | 0 | array = DatumGetArrayTypeP(arraydatum); |
3491 | | |
3492 | | /* run-time double-check on element type */ |
3493 | 0 | if (element_type != ARR_ELEMTYPE(array)) |
3494 | 0 | ereport(ERROR, |
3495 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
3496 | 0 | errmsg("cannot merge incompatible arrays"), |
3497 | 0 | errdetail("Array with element type %s cannot be " |
3498 | 0 | "included in ARRAY construct with element type %s.", |
3499 | 0 | format_type_be(ARR_ELEMTYPE(array)), |
3500 | 0 | format_type_be(element_type)))); |
3501 | | |
3502 | 0 | this_ndims = ARR_NDIM(array); |
3503 | | /* temporarily ignore zero-dimensional subarrays */ |
3504 | 0 | if (this_ndims <= 0) |
3505 | 0 | { |
3506 | 0 | haveempty = true; |
3507 | 0 | continue; |
3508 | 0 | } |
3509 | | |
3510 | 0 | if (firstone) |
3511 | 0 | { |
3512 | | /* Get sub-array details from first member */ |
3513 | 0 | elem_ndims = this_ndims; |
3514 | 0 | ndims = elem_ndims + 1; |
3515 | 0 | if (ndims <= 0 || ndims > MAXDIM) |
3516 | 0 | ereport(ERROR, |
3517 | 0 | (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
3518 | 0 | errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)", |
3519 | 0 | ndims, MAXDIM))); |
3520 | | |
3521 | 0 | elem_dims = palloc_array(int, elem_ndims); |
3522 | 0 | memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int)); |
3523 | 0 | elem_lbs = palloc_array(int, elem_ndims); |
3524 | 0 | memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int)); |
3525 | |
|
3526 | 0 | firstone = false; |
3527 | 0 | } |
3528 | 0 | else |
3529 | 0 | { |
3530 | | /* Check other sub-arrays are compatible */ |
3531 | 0 | if (elem_ndims != this_ndims || |
3532 | 0 | memcmp(elem_dims, ARR_DIMS(array), |
3533 | 0 | elem_ndims * sizeof(int)) != 0 || |
3534 | 0 | memcmp(elem_lbs, ARR_LBOUND(array), |
3535 | 0 | elem_ndims * sizeof(int)) != 0) |
3536 | 0 | ereport(ERROR, |
3537 | 0 | (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
3538 | 0 | errmsg("multidimensional arrays must have array " |
3539 | 0 | "expressions with matching dimensions"))); |
3540 | 0 | } |
3541 | | |
3542 | 0 | subdata[outer_nelems] = ARR_DATA_PTR(array); |
3543 | 0 | subbitmaps[outer_nelems] = ARR_NULLBITMAP(array); |
3544 | 0 | subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array); |
3545 | 0 | nbytes += subbytes[outer_nelems]; |
3546 | | /* check for overflow of total request */ |
3547 | 0 | if (!AllocSizeIsValid(nbytes)) |
3548 | 0 | ereport(ERROR, |
3549 | 0 | (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
3550 | 0 | errmsg("array size exceeds the maximum allowed (%d)", |
3551 | 0 | (int) MaxAllocSize))); |
3552 | 0 | subnitems[outer_nelems] = ArrayGetNItems(this_ndims, |
3553 | 0 | ARR_DIMS(array)); |
3554 | 0 | havenulls |= ARR_HASNULL(array); |
3555 | 0 | outer_nelems++; |
3556 | 0 | } |
3557 | | |
3558 | | /* |
3559 | | * If all items were null or empty arrays, return an empty array; |
3560 | | * otherwise, if some were and some weren't, raise error. (Note: we |
3561 | | * must special-case this somehow to avoid trying to generate a 1-D |
3562 | | * array formed from empty arrays. It's not ideal...) |
3563 | | */ |
3564 | 0 | if (haveempty) |
3565 | 0 | { |
3566 | 0 | if (ndims == 0) /* didn't find any nonempty array */ |
3567 | 0 | { |
3568 | 0 | *op->resvalue = PointerGetDatum(construct_empty_array(element_type)); |
3569 | 0 | return; |
3570 | 0 | } |
3571 | 0 | ereport(ERROR, |
3572 | 0 | (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
3573 | 0 | errmsg("multidimensional arrays must have array " |
3574 | 0 | "expressions with matching dimensions"))); |
3575 | 0 | } |
3576 | | |
3577 | | /* setup for multi-D array */ |
3578 | 0 | dims[0] = outer_nelems; |
3579 | 0 | lbs[0] = 1; |
3580 | 0 | for (int i = 1; i < ndims; i++) |
3581 | 0 | { |
3582 | 0 | dims[i] = elem_dims[i - 1]; |
3583 | 0 | lbs[i] = elem_lbs[i - 1]; |
3584 | 0 | } |
3585 | | |
3586 | | /* check for subscript overflow */ |
3587 | 0 | nitems = ArrayGetNItems(ndims, dims); |
3588 | 0 | ArrayCheckBounds(ndims, dims, lbs); |
3589 | |
|
3590 | 0 | if (havenulls) |
3591 | 0 | { |
3592 | 0 | dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems); |
3593 | 0 | nbytes += dataoffset; |
3594 | 0 | } |
3595 | 0 | else |
3596 | 0 | { |
3597 | 0 | dataoffset = 0; /* marker for no null bitmap */ |
3598 | 0 | nbytes += ARR_OVERHEAD_NONULLS(ndims); |
3599 | 0 | } |
3600 | |
|
3601 | 0 | result = (ArrayType *) palloc0(nbytes); |
3602 | 0 | SET_VARSIZE(result, nbytes); |
3603 | 0 | result->ndim = ndims; |
3604 | 0 | result->dataoffset = dataoffset; |
3605 | 0 | result->elemtype = element_type; |
3606 | 0 | memcpy(ARR_DIMS(result), dims, ndims * sizeof(int)); |
3607 | 0 | memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int)); |
3608 | |
|
3609 | 0 | dat = ARR_DATA_PTR(result); |
3610 | 0 | iitem = 0; |
3611 | 0 | for (int i = 0; i < outer_nelems; i++) |
3612 | 0 | { |
3613 | 0 | memcpy(dat, subdata[i], subbytes[i]); |
3614 | 0 | dat += subbytes[i]; |
3615 | 0 | if (havenulls) |
3616 | 0 | array_bitmap_copy(ARR_NULLBITMAP(result), iitem, |
3617 | 0 | subbitmaps[i], 0, |
3618 | 0 | subnitems[i]); |
3619 | 0 | iitem += subnitems[i]; |
3620 | 0 | } |
3621 | 0 | } |
3622 | | |
3623 | 0 | *op->resvalue = PointerGetDatum(result); |
3624 | 0 | } |
3625 | | |
3626 | | /* |
3627 | | * Evaluate an ArrayCoerceExpr expression. |
3628 | | * |
3629 | | * Source array is in step's result variable. |
3630 | | */ |
3631 | | void |
3632 | | ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3633 | 0 | { |
3634 | 0 | Datum arraydatum; |
3635 | | |
3636 | | /* NULL array -> NULL result */ |
3637 | 0 | if (*op->resnull) |
3638 | 0 | return; |
3639 | | |
3640 | 0 | arraydatum = *op->resvalue; |
3641 | | |
3642 | | /* |
3643 | | * If it's binary-compatible, modify the element type in the array header, |
3644 | | * but otherwise leave the array as we received it. |
3645 | | */ |
3646 | 0 | if (op->d.arraycoerce.elemexprstate == NULL) |
3647 | 0 | { |
3648 | | /* Detoast input array if necessary, and copy in any case */ |
3649 | 0 | ArrayType *array = DatumGetArrayTypePCopy(arraydatum); |
3650 | |
|
3651 | 0 | ARR_ELEMTYPE(array) = op->d.arraycoerce.resultelemtype; |
3652 | 0 | *op->resvalue = PointerGetDatum(array); |
3653 | 0 | return; |
3654 | 0 | } |
3655 | | |
3656 | | /* |
3657 | | * Use array_map to apply the sub-expression to each array element. |
3658 | | */ |
3659 | 0 | *op->resvalue = array_map(arraydatum, |
3660 | 0 | op->d.arraycoerce.elemexprstate, |
3661 | 0 | econtext, |
3662 | 0 | op->d.arraycoerce.resultelemtype, |
3663 | 0 | op->d.arraycoerce.amstate); |
3664 | 0 | } |
3665 | | |
3666 | | /* |
3667 | | * Evaluate a ROW() expression. |
3668 | | * |
3669 | | * The individual columns have already been evaluated into |
3670 | | * op->d.row.elemvalues[]/elemnulls[]. |
3671 | | */ |
3672 | | void |
3673 | | ExecEvalRow(ExprState *state, ExprEvalStep *op) |
3674 | 0 | { |
3675 | 0 | HeapTuple tuple; |
3676 | | |
3677 | | /* build tuple from evaluated field values */ |
3678 | 0 | tuple = heap_form_tuple(op->d.row.tupdesc, |
3679 | 0 | op->d.row.elemvalues, |
3680 | 0 | op->d.row.elemnulls); |
3681 | |
|
3682 | 0 | *op->resvalue = HeapTupleGetDatum(tuple); |
3683 | 0 | *op->resnull = false; |
3684 | 0 | } |
3685 | | |
3686 | | /* |
3687 | | * Evaluate GREATEST() or LEAST() expression (note this is *not* MIN()/MAX()). |
3688 | | * |
3689 | | * All of the to-be-compared expressions have already been evaluated into |
3690 | | * op->d.minmax.values[]/nulls[]. |
3691 | | */ |
3692 | | void |
3693 | | ExecEvalMinMax(ExprState *state, ExprEvalStep *op) |
3694 | 0 | { |
3695 | 0 | Datum *values = op->d.minmax.values; |
3696 | 0 | bool *nulls = op->d.minmax.nulls; |
3697 | 0 | FunctionCallInfo fcinfo = op->d.minmax.fcinfo_data; |
3698 | 0 | MinMaxOp operator = op->d.minmax.op; |
3699 | | |
3700 | | /* set at initialization */ |
3701 | 0 | Assert(fcinfo->args[0].isnull == false); |
3702 | 0 | Assert(fcinfo->args[1].isnull == false); |
3703 | | |
3704 | | /* default to null result */ |
3705 | 0 | *op->resnull = true; |
3706 | |
|
3707 | 0 | for (int off = 0; off < op->d.minmax.nelems; off++) |
3708 | 0 | { |
3709 | | /* ignore NULL inputs */ |
3710 | 0 | if (nulls[off]) |
3711 | 0 | continue; |
3712 | | |
3713 | 0 | if (*op->resnull) |
3714 | 0 | { |
3715 | | /* first nonnull input, adopt value */ |
3716 | 0 | *op->resvalue = values[off]; |
3717 | 0 | *op->resnull = false; |
3718 | 0 | } |
3719 | 0 | else |
3720 | 0 | { |
3721 | 0 | int cmpresult; |
3722 | | |
3723 | | /* apply comparison function */ |
3724 | 0 | fcinfo->args[0].value = *op->resvalue; |
3725 | 0 | fcinfo->args[1].value = values[off]; |
3726 | |
|
3727 | 0 | fcinfo->isnull = false; |
3728 | 0 | cmpresult = DatumGetInt32(FunctionCallInvoke(fcinfo)); |
3729 | 0 | if (fcinfo->isnull) /* probably should not happen */ |
3730 | 0 | continue; |
3731 | | |
3732 | 0 | if (cmpresult > 0 && operator == IS_LEAST) |
3733 | 0 | *op->resvalue = values[off]; |
3734 | 0 | else if (cmpresult < 0 && operator == IS_GREATEST) |
3735 | 0 | *op->resvalue = values[off]; |
3736 | 0 | } |
3737 | 0 | } |
3738 | 0 | } |
3739 | | |
3740 | | /* |
3741 | | * Evaluate a FieldSelect node. |
3742 | | * |
3743 | | * Source record is in step's result variable. |
3744 | | */ |
3745 | | void |
3746 | | ExecEvalFieldSelect(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3747 | 0 | { |
3748 | 0 | AttrNumber fieldnum = op->d.fieldselect.fieldnum; |
3749 | 0 | Datum tupDatum; |
3750 | 0 | HeapTupleHeader tuple; |
3751 | 0 | Oid tupType; |
3752 | 0 | int32 tupTypmod; |
3753 | 0 | TupleDesc tupDesc; |
3754 | 0 | Form_pg_attribute attr; |
3755 | 0 | HeapTupleData tmptup; |
3756 | | |
3757 | | /* NULL record -> NULL result */ |
3758 | 0 | if (*op->resnull) |
3759 | 0 | return; |
3760 | | |
3761 | 0 | tupDatum = *op->resvalue; |
3762 | | |
3763 | | /* We can special-case expanded records for speed */ |
3764 | 0 | if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(tupDatum))) |
3765 | 0 | { |
3766 | 0 | ExpandedRecordHeader *erh = (ExpandedRecordHeader *) DatumGetEOHP(tupDatum); |
3767 | |
|
3768 | 0 | Assert(erh->er_magic == ER_MAGIC); |
3769 | | |
3770 | | /* Extract record's TupleDesc */ |
3771 | 0 | tupDesc = expanded_record_get_tupdesc(erh); |
3772 | | |
3773 | | /* |
3774 | | * Find field's attr record. Note we don't support system columns |
3775 | | * here: a datum tuple doesn't have valid values for most of the |
3776 | | * interesting system columns anyway. |
3777 | | */ |
3778 | 0 | if (fieldnum <= 0) /* should never happen */ |
3779 | 0 | elog(ERROR, "unsupported reference to system column %d in FieldSelect", |
3780 | 0 | fieldnum); |
3781 | 0 | if (fieldnum > tupDesc->natts) /* should never happen */ |
3782 | 0 | elog(ERROR, "attribute number %d exceeds number of columns %d", |
3783 | 0 | fieldnum, tupDesc->natts); |
3784 | 0 | attr = TupleDescAttr(tupDesc, fieldnum - 1); |
3785 | | |
3786 | | /* Check for dropped column, and force a NULL result if so */ |
3787 | 0 | if (attr->attisdropped) |
3788 | 0 | { |
3789 | 0 | *op->resnull = true; |
3790 | 0 | return; |
3791 | 0 | } |
3792 | | |
3793 | | /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */ |
3794 | | /* As in CheckVarSlotCompatibility, we should but can't check typmod */ |
3795 | 0 | if (op->d.fieldselect.resulttype != attr->atttypid) |
3796 | 0 | ereport(ERROR, |
3797 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
3798 | 0 | errmsg("attribute %d has wrong type", fieldnum), |
3799 | 0 | errdetail("Table has type %s, but query expects %s.", |
3800 | 0 | format_type_be(attr->atttypid), |
3801 | 0 | format_type_be(op->d.fieldselect.resulttype)))); |
3802 | | |
3803 | | /* extract the field */ |
3804 | 0 | *op->resvalue = expanded_record_get_field(erh, fieldnum, |
3805 | 0 | op->resnull); |
3806 | 0 | } |
3807 | 0 | else |
3808 | 0 | { |
3809 | | /* Get the composite datum and extract its type fields */ |
3810 | 0 | tuple = DatumGetHeapTupleHeader(tupDatum); |
3811 | |
|
3812 | 0 | tupType = HeapTupleHeaderGetTypeId(tuple); |
3813 | 0 | tupTypmod = HeapTupleHeaderGetTypMod(tuple); |
3814 | | |
3815 | | /* Lookup tupdesc if first time through or if type changes */ |
3816 | 0 | tupDesc = get_cached_rowtype(tupType, tupTypmod, |
3817 | 0 | &op->d.fieldselect.rowcache, NULL); |
3818 | | |
3819 | | /* |
3820 | | * Find field's attr record. Note we don't support system columns |
3821 | | * here: a datum tuple doesn't have valid values for most of the |
3822 | | * interesting system columns anyway. |
3823 | | */ |
3824 | 0 | if (fieldnum <= 0) /* should never happen */ |
3825 | 0 | elog(ERROR, "unsupported reference to system column %d in FieldSelect", |
3826 | 0 | fieldnum); |
3827 | 0 | if (fieldnum > tupDesc->natts) /* should never happen */ |
3828 | 0 | elog(ERROR, "attribute number %d exceeds number of columns %d", |
3829 | 0 | fieldnum, tupDesc->natts); |
3830 | 0 | attr = TupleDescAttr(tupDesc, fieldnum - 1); |
3831 | | |
3832 | | /* Check for dropped column, and force a NULL result if so */ |
3833 | 0 | if (attr->attisdropped) |
3834 | 0 | { |
3835 | 0 | *op->resnull = true; |
3836 | 0 | return; |
3837 | 0 | } |
3838 | | |
3839 | | /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */ |
3840 | | /* As in CheckVarSlotCompatibility, we should but can't check typmod */ |
3841 | 0 | if (op->d.fieldselect.resulttype != attr->atttypid) |
3842 | 0 | ereport(ERROR, |
3843 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
3844 | 0 | errmsg("attribute %d has wrong type", fieldnum), |
3845 | 0 | errdetail("Table has type %s, but query expects %s.", |
3846 | 0 | format_type_be(attr->atttypid), |
3847 | 0 | format_type_be(op->d.fieldselect.resulttype)))); |
3848 | | |
3849 | | /* heap_getattr needs a HeapTuple not a bare HeapTupleHeader */ |
3850 | 0 | tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple); |
3851 | 0 | tmptup.t_data = tuple; |
3852 | | |
3853 | | /* extract the field */ |
3854 | 0 | *op->resvalue = heap_getattr(&tmptup, |
3855 | 0 | fieldnum, |
3856 | 0 | tupDesc, |
3857 | 0 | op->resnull); |
3858 | 0 | } |
3859 | 0 | } |
3860 | | |
3861 | | /* |
3862 | | * Deform source tuple, filling in the step's values/nulls arrays, before |
3863 | | * evaluating individual new values as part of a FieldStore expression. |
3864 | | * Subsequent steps will overwrite individual elements of the values/nulls |
3865 | | * arrays with the new field values, and then FIELDSTORE_FORM will build the |
3866 | | * new tuple value. |
3867 | | * |
3868 | | * Source record is in step's result variable. |
3869 | | */ |
3870 | | void |
3871 | | ExecEvalFieldStoreDeForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3872 | 0 | { |
3873 | 0 | if (*op->resnull) |
3874 | 0 | { |
3875 | | /* Convert null input tuple into an all-nulls row */ |
3876 | 0 | memset(op->d.fieldstore.nulls, true, |
3877 | 0 | op->d.fieldstore.tupdesc->natts * sizeof(bool)); |
3878 | 0 | } |
3879 | 0 | else |
3880 | 0 | { |
3881 | | /* |
3882 | | * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader. We |
3883 | | * set all the fields in the struct just in case. |
3884 | | */ |
3885 | 0 | Datum tupDatum = *op->resvalue; |
3886 | 0 | HeapTupleHeader tuphdr; |
3887 | 0 | HeapTupleData tmptup; |
3888 | |
|
3889 | 0 | tuphdr = DatumGetHeapTupleHeader(tupDatum); |
3890 | 0 | tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr); |
3891 | 0 | ItemPointerSetInvalid(&(tmptup.t_self)); |
3892 | 0 | tmptup.t_tableOid = InvalidOid; |
3893 | 0 | tmptup.t_data = tuphdr; |
3894 | |
|
3895 | 0 | heap_deform_tuple(&tmptup, op->d.fieldstore.tupdesc, |
3896 | 0 | op->d.fieldstore.values, |
3897 | 0 | op->d.fieldstore.nulls); |
3898 | 0 | } |
3899 | 0 | } |
3900 | | |
3901 | | /* |
3902 | | * Compute the new composite datum after each individual field value of a |
3903 | | * FieldStore expression has been evaluated. |
3904 | | */ |
3905 | | void |
3906 | | ExecEvalFieldStoreForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3907 | 0 | { |
3908 | 0 | HeapTuple tuple; |
3909 | |
|
3910 | 0 | tuple = heap_form_tuple(op->d.fieldstore.tupdesc, |
3911 | 0 | op->d.fieldstore.values, |
3912 | 0 | op->d.fieldstore.nulls); |
3913 | |
|
3914 | 0 | *op->resvalue = HeapTupleGetDatum(tuple); |
3915 | 0 | *op->resnull = false; |
3916 | 0 | } |
3917 | | |
3918 | | /* |
3919 | | * Evaluate a rowtype coercion operation. |
3920 | | * This may require rearranging field positions. |
3921 | | * |
3922 | | * Source record is in step's result variable. |
3923 | | */ |
3924 | | void |
3925 | | ExecEvalConvertRowtype(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
3926 | 0 | { |
3927 | 0 | HeapTuple result; |
3928 | 0 | Datum tupDatum; |
3929 | 0 | HeapTupleHeader tuple; |
3930 | 0 | HeapTupleData tmptup; |
3931 | 0 | TupleDesc indesc, |
3932 | 0 | outdesc; |
3933 | 0 | bool changed = false; |
3934 | | |
3935 | | /* NULL in -> NULL out */ |
3936 | 0 | if (*op->resnull) |
3937 | 0 | return; |
3938 | | |
3939 | 0 | tupDatum = *op->resvalue; |
3940 | 0 | tuple = DatumGetHeapTupleHeader(tupDatum); |
3941 | | |
3942 | | /* |
3943 | | * Lookup tupdescs if first time through or if type changes. We'd better |
3944 | | * pin them since type conversion functions could do catalog lookups and |
3945 | | * hence cause cache invalidation. |
3946 | | */ |
3947 | 0 | indesc = get_cached_rowtype(op->d.convert_rowtype.inputtype, -1, |
3948 | 0 | op->d.convert_rowtype.incache, |
3949 | 0 | &changed); |
3950 | 0 | IncrTupleDescRefCount(indesc); |
3951 | 0 | outdesc = get_cached_rowtype(op->d.convert_rowtype.outputtype, -1, |
3952 | 0 | op->d.convert_rowtype.outcache, |
3953 | 0 | &changed); |
3954 | 0 | IncrTupleDescRefCount(outdesc); |
3955 | | |
3956 | | /* |
3957 | | * We used to be able to assert that incoming tuples are marked with |
3958 | | * exactly the rowtype of indesc. However, now that ExecEvalWholeRowVar |
3959 | | * might change the tuples' marking to plain RECORD due to inserting |
3960 | | * aliases, we can only make this weak test: |
3961 | | */ |
3962 | 0 | Assert(HeapTupleHeaderGetTypeId(tuple) == indesc->tdtypeid || |
3963 | 0 | HeapTupleHeaderGetTypeId(tuple) == RECORDOID); |
3964 | | |
3965 | | /* if first time through, or after change, initialize conversion map */ |
3966 | 0 | if (changed) |
3967 | 0 | { |
3968 | 0 | MemoryContext old_cxt; |
3969 | | |
3970 | | /* allocate map in long-lived memory context */ |
3971 | 0 | old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
3972 | | |
3973 | | /* prepare map from old to new attribute numbers */ |
3974 | 0 | op->d.convert_rowtype.map = convert_tuples_by_name(indesc, outdesc); |
3975 | |
|
3976 | 0 | MemoryContextSwitchTo(old_cxt); |
3977 | 0 | } |
3978 | | |
3979 | | /* Following steps need a HeapTuple not a bare HeapTupleHeader */ |
3980 | 0 | tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple); |
3981 | 0 | tmptup.t_data = tuple; |
3982 | |
|
3983 | 0 | if (op->d.convert_rowtype.map != NULL) |
3984 | 0 | { |
3985 | | /* Full conversion with attribute rearrangement needed */ |
3986 | 0 | result = execute_attr_map_tuple(&tmptup, op->d.convert_rowtype.map); |
3987 | | /* Result already has appropriate composite-datum header fields */ |
3988 | 0 | *op->resvalue = HeapTupleGetDatum(result); |
3989 | 0 | } |
3990 | 0 | else |
3991 | 0 | { |
3992 | 0 | HeapTupleHeader resulthdr; |
3993 | | |
3994 | | /* |
3995 | | * The tuple is physically compatible as-is, but we need to insert the |
3996 | | * destination rowtype OID in its composite-datum header field, so we |
3997 | | * have to copy it anyway. heap_copy_tuple_as_datum() is convenient |
3998 | | * for making the physical copy. Note that we aren't expecting to |
3999 | | * have to flatten any toasted fields: the input was a composite |
4000 | | * datum, so it shouldn't contain any. So heap_copy_tuple_as_datum() |
4001 | | * is overkill here, but its check for external fields is cheap. |
4002 | | * |
4003 | | * Copying with indesc verifies that the tuple could be deformed with |
4004 | | * indesc, which is crucial, as otherwise we would not detect cases |
4005 | | * where the input tuple and the input tupledesc have gotten out of |
4006 | | * sync (when executing the attribute map, deforming the tuple does |
4007 | | * that). indesc is known to be compatible with outdesc (otherwise |
4008 | | * we'd have a map), so heap_tuple_set_datum() can then relabel the |
4009 | | * copy with outdesc's header fields without verifying it against |
4010 | | * outdesc, which would be more expensive. |
4011 | | */ |
4012 | 0 | *op->resvalue = heap_copy_tuple_as_datum(&tmptup, indesc); |
4013 | 0 | resulthdr = (HeapTupleHeader) DatumGetPointer(*op->resvalue); |
4014 | 0 | heap_tuple_set_datum(resulthdr, outdesc, |
4015 | 0 | HeapTupleHeaderGetDatumLength(resulthdr)); |
4016 | 0 | } |
4017 | |
|
4018 | 0 | DecrTupleDescRefCount(indesc); |
4019 | 0 | DecrTupleDescRefCount(outdesc); |
4020 | 0 | } |
4021 | | |
4022 | | /* |
4023 | | * Evaluate "scalar op ANY/ALL (array)". |
4024 | | * |
4025 | | * Source array is in our result area, scalar arg is already evaluated into |
4026 | | * fcinfo->args[0]. |
4027 | | * |
4028 | | * The operator always yields boolean, and we combine the results across all |
4029 | | * array elements using OR and AND (for ANY and ALL respectively). Of course |
4030 | | * we short-circuit as soon as the result is known. |
4031 | | */ |
4032 | | void |
4033 | | ExecEvalScalarArrayOp(ExprState *state, ExprEvalStep *op) |
4034 | 0 | { |
4035 | 0 | FunctionCallInfo fcinfo = op->d.scalararrayop.fcinfo_data; |
4036 | 0 | bool useOr = op->d.scalararrayop.useOr; |
4037 | 0 | bool strictfunc = op->d.scalararrayop.finfo->fn_strict; |
4038 | 0 | ArrayType *arr; |
4039 | 0 | int nitems; |
4040 | 0 | Datum result; |
4041 | 0 | bool resultnull; |
4042 | | |
4043 | | /* |
4044 | | * If the array is NULL then we return NULL --- it's not very meaningful |
4045 | | * to do anything else, even if the operator isn't strict. |
4046 | | */ |
4047 | 0 | if (*op->resnull) |
4048 | 0 | return; |
4049 | | |
4050 | | /* Else okay to fetch and detoast the array */ |
4051 | 0 | arr = DatumGetArrayTypeP(*op->resvalue); |
4052 | | |
4053 | | /* |
4054 | | * If the array is empty, we return either FALSE or TRUE per the useOr |
4055 | | * flag. This is correct even if the scalar is NULL; since we would |
4056 | | * evaluate the operator zero times, it matters not whether it would want |
4057 | | * to return NULL. |
4058 | | */ |
4059 | 0 | nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)); |
4060 | 0 | if (nitems <= 0) |
4061 | 0 | { |
4062 | 0 | *op->resvalue = BoolGetDatum(!useOr); |
4063 | 0 | *op->resnull = false; |
4064 | 0 | return; |
4065 | 0 | } |
4066 | | |
4067 | | /* |
4068 | | * If the scalar is NULL, and the function is strict, return NULL; no |
4069 | | * point in iterating the loop. |
4070 | | */ |
4071 | 0 | if (fcinfo->args[0].isnull && strictfunc) |
4072 | 0 | { |
4073 | 0 | *op->resnull = true; |
4074 | 0 | return; |
4075 | 0 | } |
4076 | | |
4077 | | /* |
4078 | | * We arrange to look up info about the element type only once per series |
4079 | | * of calls, assuming the element type doesn't change underneath us. |
4080 | | */ |
4081 | 0 | if (op->d.scalararrayop.element_type != ARR_ELEMTYPE(arr)) |
4082 | 0 | { |
4083 | 0 | get_typlenbyvalalign(ARR_ELEMTYPE(arr), |
4084 | 0 | &op->d.scalararrayop.typlen, |
4085 | 0 | &op->d.scalararrayop.typbyval, |
4086 | 0 | &op->d.scalararrayop.typalign); |
4087 | 0 | op->d.scalararrayop.element_type = ARR_ELEMTYPE(arr); |
4088 | 0 | } |
4089 | |
|
4090 | 0 | ExecEvalArrayCompareInternal(fcinfo, |
4091 | 0 | arr, |
4092 | 0 | op->d.scalararrayop.typlen, |
4093 | 0 | op->d.scalararrayop.typbyval, |
4094 | 0 | op->d.scalararrayop.typalign, |
4095 | 0 | useOr, |
4096 | 0 | &result, |
4097 | 0 | &resultnull); |
4098 | |
|
4099 | 0 | *op->resvalue = result; |
4100 | 0 | *op->resnull = resultnull; |
4101 | 0 | } |
4102 | | |
4103 | | /* |
4104 | | * Shared helper for ExecEvalScalarArrayOp() and the NULL-LHS fallback for |
4105 | | * non-strict ExecEvalHashedScalarArrayOp(). |
4106 | | * |
4107 | | * Callers must handle the strict LHS-is-NULL; return NULL fast path prior to |
4108 | | * calling this. |
4109 | | */ |
4110 | | static pg_always_inline void |
4111 | | ExecEvalArrayCompareInternal(FunctionCallInfo fcinfo, ArrayType *arr, |
4112 | | int16 typlen, bool typbyval, char typalign, |
4113 | | bool useOr, Datum *result, bool *resultnull) |
4114 | 0 | { |
4115 | 0 | uint8 typalignby = typalign_to_alignby(typalign); |
4116 | 0 | int nitems; |
4117 | 0 | char *s; |
4118 | 0 | uint8 *bitmap; |
4119 | 0 | int bitmask; |
4120 | 0 | bool strictfunc = fcinfo->flinfo->fn_strict; |
4121 | |
|
4122 | 0 | nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)); |
4123 | | |
4124 | | /* Initialize result appropriately depending on useOr */ |
4125 | 0 | *result = BoolGetDatum(!useOr); |
4126 | 0 | *resultnull = false; |
4127 | | |
4128 | | /* Loop over the array elements */ |
4129 | 0 | s = (char *) ARR_DATA_PTR(arr); |
4130 | 0 | bitmap = ARR_NULLBITMAP(arr); |
4131 | 0 | bitmask = 1; |
4132 | |
|
4133 | 0 | for (int i = 0; i < nitems; i++) |
4134 | 0 | { |
4135 | 0 | Datum elt; |
4136 | 0 | Datum thisresult; |
4137 | | |
4138 | | /* Get array element, checking for NULL */ |
4139 | 0 | if (bitmap && (*bitmap & bitmask) == 0) |
4140 | 0 | { |
4141 | 0 | fcinfo->args[1].value = (Datum) 0; |
4142 | 0 | fcinfo->args[1].isnull = true; |
4143 | 0 | } |
4144 | 0 | else |
4145 | 0 | { |
4146 | 0 | elt = fetch_att(s, typbyval, typlen); |
4147 | 0 | s = att_addlength_pointer(s, typlen, s); |
4148 | 0 | s = (char *) att_nominal_alignby(s, typalignby); |
4149 | 0 | fcinfo->args[1].value = elt; |
4150 | 0 | fcinfo->args[1].isnull = false; |
4151 | 0 | } |
4152 | | |
4153 | | /* Call comparison function */ |
4154 | 0 | if (fcinfo->args[1].isnull && strictfunc) |
4155 | 0 | { |
4156 | 0 | fcinfo->isnull = true; |
4157 | 0 | thisresult = (Datum) 0; |
4158 | 0 | } |
4159 | 0 | else |
4160 | 0 | { |
4161 | 0 | fcinfo->isnull = false; |
4162 | 0 | thisresult = fcinfo->flinfo->fn_addr(fcinfo); |
4163 | 0 | } |
4164 | | |
4165 | | /* Combine results per OR or AND semantics */ |
4166 | 0 | if (fcinfo->isnull) |
4167 | 0 | *resultnull = true; |
4168 | 0 | else if (useOr) |
4169 | 0 | { |
4170 | 0 | if (DatumGetBool(thisresult)) |
4171 | 0 | { |
4172 | 0 | *result = BoolGetDatum(true); |
4173 | 0 | *resultnull = false; |
4174 | 0 | break; /* needn't look at any more elements */ |
4175 | 0 | } |
4176 | 0 | } |
4177 | 0 | else |
4178 | 0 | { |
4179 | 0 | if (!DatumGetBool(thisresult)) |
4180 | 0 | { |
4181 | 0 | *result = BoolGetDatum(false); |
4182 | 0 | *resultnull = false; |
4183 | 0 | break; /* needn't look at any more elements */ |
4184 | 0 | } |
4185 | 0 | } |
4186 | | |
4187 | | /* advance bitmap pointer if any */ |
4188 | 0 | if (bitmap) |
4189 | 0 | { |
4190 | 0 | bitmask <<= 1; |
4191 | 0 | if (bitmask == 0x100) |
4192 | 0 | { |
4193 | 0 | bitmap++; |
4194 | 0 | bitmask = 1; |
4195 | 0 | } |
4196 | 0 | } |
4197 | 0 | } |
4198 | 0 | } |
4199 | | |
4200 | | /* |
4201 | | * Hash function for scalar array hash op elements. |
4202 | | * |
4203 | | * We use the element type's default hash opclass, and the column collation |
4204 | | * if the type is collation-sensitive. |
4205 | | */ |
4206 | | static uint32 |
4207 | | saop_element_hash(struct saophash_hash *tb, Datum key) |
4208 | 0 | { |
4209 | 0 | ScalarArrayOpExprHashTable *elements_tab = (ScalarArrayOpExprHashTable *) tb->private_data; |
4210 | 0 | FunctionCallInfo fcinfo = &elements_tab->hash_fcinfo_data; |
4211 | 0 | Datum hash; |
4212 | |
|
4213 | 0 | fcinfo->args[0].value = key; |
4214 | 0 | fcinfo->args[0].isnull = false; |
4215 | |
|
4216 | 0 | hash = elements_tab->hash_finfo.fn_addr(fcinfo); |
4217 | |
|
4218 | 0 | return DatumGetUInt32(hash); |
4219 | 0 | } |
4220 | | |
4221 | | /* |
4222 | | * Matching function for scalar array hash op elements, to be used in hashtable |
4223 | | * lookups. |
4224 | | */ |
4225 | | static bool |
4226 | | saop_hash_element_match(struct saophash_hash *tb, Datum key1, Datum key2) |
4227 | 0 | { |
4228 | 0 | Datum result; |
4229 | |
|
4230 | 0 | ScalarArrayOpExprHashTable *elements_tab = (ScalarArrayOpExprHashTable *) tb->private_data; |
4231 | 0 | FunctionCallInfo fcinfo = elements_tab->op->d.hashedscalararrayop.fcinfo_data; |
4232 | |
|
4233 | 0 | fcinfo->args[0].value = key1; |
4234 | 0 | fcinfo->args[0].isnull = false; |
4235 | 0 | fcinfo->args[1].value = key2; |
4236 | 0 | fcinfo->args[1].isnull = false; |
4237 | |
|
4238 | 0 | result = elements_tab->op->d.hashedscalararrayop.finfo->fn_addr(fcinfo); |
4239 | |
|
4240 | 0 | return DatumGetBool(result); |
4241 | 0 | } |
4242 | | |
4243 | | /* |
4244 | | * Evaluate "scalar op ANY (const array)". |
4245 | | * |
4246 | | * Similar to ExecEvalScalarArrayOp, but optimized for faster repeat lookups |
4247 | | * by building a hashtable on the first lookup. This hashtable will be reused |
4248 | | * by subsequent lookups. Unlike ExecEvalScalarArrayOp, this version only |
4249 | | * supports OR semantics. |
4250 | | * |
4251 | | * Source array is in our result area, scalar arg is already evaluated into |
4252 | | * fcinfo->args[0]. |
4253 | | * |
4254 | | * The operator always yields boolean. |
4255 | | */ |
4256 | | void |
4257 | | ExecEvalHashedScalarArrayOp(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
4258 | 0 | { |
4259 | 0 | ScalarArrayOpExprHashTable *elements_tab = op->d.hashedscalararrayop.elements_tab; |
4260 | 0 | FunctionCallInfo fcinfo = op->d.hashedscalararrayop.fcinfo_data; |
4261 | 0 | bool inclause = op->d.hashedscalararrayop.inclause; |
4262 | 0 | bool strictfunc = op->d.hashedscalararrayop.finfo->fn_strict; |
4263 | 0 | Datum scalar = fcinfo->args[0].value; |
4264 | 0 | bool scalar_isnull = fcinfo->args[0].isnull; |
4265 | 0 | Datum result; |
4266 | 0 | bool resultnull; |
4267 | 0 | bool hashfound; |
4268 | | |
4269 | | /* We don't setup a hashed scalar array op if the array const is null. */ |
4270 | 0 | Assert(!*op->resnull); |
4271 | | |
4272 | | /* |
4273 | | * If the scalar is NULL, and the function is strict, return NULL; no |
4274 | | * point in executing the search. |
4275 | | */ |
4276 | 0 | if (scalar_isnull && strictfunc) |
4277 | 0 | { |
4278 | 0 | *op->resnull = true; |
4279 | 0 | return; |
4280 | 0 | } |
4281 | | |
4282 | | /* Build the hash table on first evaluation */ |
4283 | 0 | if (elements_tab == NULL) |
4284 | 0 | { |
4285 | 0 | ScalarArrayOpExpr *saop; |
4286 | 0 | int16 typlen; |
4287 | 0 | bool typbyval; |
4288 | 0 | char typalign; |
4289 | 0 | uint8 typalignby; |
4290 | 0 | int nitems; |
4291 | 0 | bool has_nulls = false; |
4292 | 0 | char *s; |
4293 | 0 | uint8 *bitmap; |
4294 | 0 | int bitmask; |
4295 | 0 | MemoryContext oldcontext; |
4296 | 0 | ArrayType *arr; |
4297 | |
|
4298 | 0 | saop = op->d.hashedscalararrayop.saop; |
4299 | |
|
4300 | 0 | arr = DatumGetArrayTypeP(*op->resvalue); |
4301 | 0 | nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)); |
4302 | |
|
4303 | 0 | get_typlenbyvalalign(ARR_ELEMTYPE(arr), |
4304 | 0 | &typlen, |
4305 | 0 | &typbyval, |
4306 | 0 | &typalign); |
4307 | 0 | typalignby = typalign_to_alignby(typalign); |
4308 | |
|
4309 | 0 | oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
4310 | |
|
4311 | 0 | elements_tab = (ScalarArrayOpExprHashTable *) |
4312 | 0 | palloc0(offsetof(ScalarArrayOpExprHashTable, hash_fcinfo_data) + |
4313 | 0 | SizeForFunctionCallInfo(1)); |
4314 | 0 | op->d.hashedscalararrayop.elements_tab = elements_tab; |
4315 | 0 | elements_tab->op = op; |
4316 | |
|
4317 | 0 | fmgr_info(saop->hashfuncid, &elements_tab->hash_finfo); |
4318 | 0 | fmgr_info_set_expr((Node *) saop, &elements_tab->hash_finfo); |
4319 | |
|
4320 | 0 | InitFunctionCallInfoData(elements_tab->hash_fcinfo_data, |
4321 | 0 | &elements_tab->hash_finfo, |
4322 | 0 | 1, |
4323 | 0 | saop->inputcollid, |
4324 | 0 | NULL, |
4325 | 0 | NULL); |
4326 | | |
4327 | | /* |
4328 | | * Create the hash table sizing it according to the number of elements |
4329 | | * in the array. This does assume that the array has no duplicates. |
4330 | | * If the array happens to contain many duplicate values then it'll |
4331 | | * just mean that we sized the table a bit on the large side. |
4332 | | */ |
4333 | 0 | elements_tab->hashtab = saophash_create(CurrentMemoryContext, nitems, |
4334 | 0 | elements_tab); |
4335 | |
|
4336 | 0 | MemoryContextSwitchTo(oldcontext); |
4337 | |
|
4338 | 0 | s = (char *) ARR_DATA_PTR(arr); |
4339 | 0 | bitmap = ARR_NULLBITMAP(arr); |
4340 | 0 | bitmask = 1; |
4341 | 0 | for (int i = 0; i < nitems; i++) |
4342 | 0 | { |
4343 | | /* Get array element, checking for NULL. */ |
4344 | 0 | if (bitmap && (*bitmap & bitmask) == 0) |
4345 | 0 | { |
4346 | 0 | has_nulls = true; |
4347 | 0 | } |
4348 | 0 | else |
4349 | 0 | { |
4350 | 0 | Datum element; |
4351 | |
|
4352 | 0 | element = fetch_att(s, typbyval, typlen); |
4353 | 0 | s = att_addlength_pointer(s, typlen, s); |
4354 | 0 | s = (char *) att_nominal_alignby(s, typalignby); |
4355 | |
|
4356 | 0 | saophash_insert(elements_tab->hashtab, element, &hashfound); |
4357 | 0 | } |
4358 | | |
4359 | | /* Advance bitmap pointer if any. */ |
4360 | 0 | if (bitmap) |
4361 | 0 | { |
4362 | 0 | bitmask <<= 1; |
4363 | 0 | if (bitmask == 0x100) |
4364 | 0 | { |
4365 | 0 | bitmap++; |
4366 | 0 | bitmask = 1; |
4367 | 0 | } |
4368 | 0 | } |
4369 | 0 | } |
4370 | | |
4371 | | /* |
4372 | | * Remember if we had any nulls so that we know if we need to execute |
4373 | | * non-strict functions with a null lhs value if no match is found. |
4374 | | */ |
4375 | 0 | op->d.hashedscalararrayop.has_nulls = has_nulls; |
4376 | | |
4377 | | /* |
4378 | | * When we have a non-strict equality function, check and cache the |
4379 | | * result from looking up a NULL. Non-strict functions are free to |
4380 | | * treat a NULL as equal to any other value, e.g. a 0 or an empty |
4381 | | * string. Here we perform a linear search over the array and cache |
4382 | | * the outcome so that we can use that result any time we receive a |
4383 | | * NULL. |
4384 | | */ |
4385 | 0 | if (!strictfunc) |
4386 | 0 | { |
4387 | 0 | bool null_lhs_result; |
4388 | |
|
4389 | 0 | fcinfo->args[0].value = (Datum) 0; |
4390 | 0 | fcinfo->args[0].isnull = true; |
4391 | |
|
4392 | 0 | ExecEvalArrayCompareInternal(fcinfo, arr, typlen, typbyval, |
4393 | 0 | typalign, true, &result, |
4394 | 0 | &resultnull); |
4395 | |
|
4396 | 0 | null_lhs_result = DatumGetBool(result); |
4397 | | |
4398 | | /* invert non-NULL results for NOT IN */ |
4399 | 0 | if (!resultnull && !inclause) |
4400 | 0 | null_lhs_result = !null_lhs_result; |
4401 | |
|
4402 | 0 | op->d.hashedscalararrayop.null_lhs_isnull = resultnull; |
4403 | 0 | op->d.hashedscalararrayop.null_lhs_result = null_lhs_result; |
4404 | 0 | } |
4405 | 0 | } |
4406 | | |
4407 | | /* |
4408 | | * When looking up an SQL NULL value with non-strict functions, we defer |
4409 | | * to the value we cached when building the hash table. |
4410 | | */ |
4411 | 0 | if (scalar_isnull) |
4412 | 0 | { |
4413 | 0 | Assert(!strictfunc); |
4414 | |
|
4415 | 0 | *op->resnull = op->d.hashedscalararrayop.null_lhs_isnull; |
4416 | 0 | *op->resvalue = BoolGetDatum(op->d.hashedscalararrayop.null_lhs_result); |
4417 | 0 | return; |
4418 | 0 | } |
4419 | | |
4420 | | |
4421 | | /* Check the hash to see if we have a match. */ |
4422 | 0 | hashfound = NULL != saophash_lookup(elements_tab->hashtab, scalar); |
4423 | | |
4424 | | /* the result depends on if the clause is an IN or NOT IN clause */ |
4425 | 0 | if (inclause) |
4426 | 0 | result = BoolGetDatum(hashfound); /* IN */ |
4427 | 0 | else |
4428 | 0 | result = BoolGetDatum(!hashfound); /* NOT IN */ |
4429 | |
|
4430 | 0 | resultnull = false; |
4431 | | |
4432 | | /* |
4433 | | * If we didn't find a match in the array, we still might need to handle |
4434 | | * the possibility of null values. We didn't put any NULLs into the |
4435 | | * hashtable, but instead marked if we found any when building the table |
4436 | | * in has_nulls. |
4437 | | */ |
4438 | 0 | if (!hashfound && op->d.hashedscalararrayop.has_nulls) |
4439 | 0 | { |
4440 | 0 | if (strictfunc) |
4441 | 0 | { |
4442 | | |
4443 | | /* |
4444 | | * We have nulls in the array so a non-null lhs and no match must |
4445 | | * yield NULL. |
4446 | | */ |
4447 | 0 | result = (Datum) 0; |
4448 | 0 | resultnull = true; |
4449 | 0 | } |
4450 | 0 | else |
4451 | 0 | { |
4452 | | /* |
4453 | | * Execute function will null rhs just once. |
4454 | | * |
4455 | | * The hash lookup path will have scribbled on the lhs argument so |
4456 | | * we need to set it up also (even though we entered this function |
4457 | | * with it already set). |
4458 | | */ |
4459 | 0 | fcinfo->args[0].value = scalar; |
4460 | 0 | fcinfo->args[0].isnull = scalar_isnull; |
4461 | 0 | fcinfo->args[1].value = (Datum) 0; |
4462 | 0 | fcinfo->args[1].isnull = true; |
4463 | |
|
4464 | 0 | result = op->d.hashedscalararrayop.finfo->fn_addr(fcinfo); |
4465 | 0 | resultnull = fcinfo->isnull; |
4466 | | |
4467 | | /* |
4468 | | * Reverse the result for NOT IN clauses since the above function |
4469 | | * is the equality function and we need not-equals. |
4470 | | */ |
4471 | 0 | if (!inclause) |
4472 | 0 | result = BoolGetDatum(!DatumGetBool(result)); |
4473 | 0 | } |
4474 | 0 | } |
4475 | |
|
4476 | 0 | *op->resvalue = result; |
4477 | 0 | *op->resnull = resultnull; |
4478 | 0 | } |
4479 | | |
4480 | | /* |
4481 | | * Evaluate a NOT NULL domain constraint. |
4482 | | */ |
4483 | | void |
4484 | | ExecEvalConstraintNotNull(ExprState *state, ExprEvalStep *op) |
4485 | 0 | { |
4486 | 0 | if (*op->resnull) |
4487 | 0 | errsave((Node *) op->d.domaincheck.escontext, |
4488 | 0 | (errcode(ERRCODE_NOT_NULL_VIOLATION), |
4489 | 0 | errmsg("domain %s does not allow null values", |
4490 | 0 | format_type_be(op->d.domaincheck.resulttype)), |
4491 | 0 | errdatatype(op->d.domaincheck.resulttype))); |
4492 | 0 | } |
4493 | | |
4494 | | /* |
4495 | | * Evaluate a CHECK domain constraint. |
4496 | | */ |
4497 | | void |
4498 | | ExecEvalConstraintCheck(ExprState *state, ExprEvalStep *op) |
4499 | 0 | { |
4500 | 0 | if (!*op->d.domaincheck.checknull && |
4501 | 0 | !DatumGetBool(*op->d.domaincheck.checkvalue)) |
4502 | 0 | errsave((Node *) op->d.domaincheck.escontext, |
4503 | 0 | (errcode(ERRCODE_CHECK_VIOLATION), |
4504 | 0 | errmsg("value for domain %s violates check constraint \"%s\"", |
4505 | 0 | format_type_be(op->d.domaincheck.resulttype), |
4506 | 0 | op->d.domaincheck.constraintname), |
4507 | 0 | errdomainconstraint(op->d.domaincheck.resulttype, |
4508 | 0 | op->d.domaincheck.constraintname))); |
4509 | 0 | } |
4510 | | |
4511 | | /* |
4512 | | * Evaluate the various forms of XmlExpr. |
4513 | | * |
4514 | | * Arguments have been evaluated into named_argvalue/named_argnull |
4515 | | * and/or argvalue/argnull arrays. |
4516 | | */ |
4517 | | void |
4518 | | ExecEvalXmlExpr(ExprState *state, ExprEvalStep *op) |
4519 | 0 | { |
4520 | 0 | XmlExpr *xexpr = op->d.xmlexpr.xexpr; |
4521 | 0 | Datum value; |
4522 | |
|
4523 | 0 | *op->resnull = true; /* until we get a result */ |
4524 | 0 | *op->resvalue = (Datum) 0; |
4525 | |
|
4526 | 0 | switch (xexpr->op) |
4527 | 0 | { |
4528 | 0 | case IS_XMLCONCAT: |
4529 | 0 | { |
4530 | 0 | Datum *argvalue = op->d.xmlexpr.argvalue; |
4531 | 0 | bool *argnull = op->d.xmlexpr.argnull; |
4532 | 0 | List *values = NIL; |
4533 | |
|
4534 | 0 | for (int i = 0; i < list_length(xexpr->args); i++) |
4535 | 0 | { |
4536 | 0 | if (!argnull[i]) |
4537 | 0 | values = lappend(values, DatumGetPointer(argvalue[i])); |
4538 | 0 | } |
4539 | |
|
4540 | 0 | if (values != NIL) |
4541 | 0 | { |
4542 | 0 | *op->resvalue = PointerGetDatum(xmlconcat(values)); |
4543 | 0 | *op->resnull = false; |
4544 | 0 | } |
4545 | 0 | } |
4546 | 0 | break; |
4547 | | |
4548 | 0 | case IS_XMLFOREST: |
4549 | 0 | { |
4550 | 0 | Datum *argvalue = op->d.xmlexpr.named_argvalue; |
4551 | 0 | bool *argnull = op->d.xmlexpr.named_argnull; |
4552 | 0 | StringInfoData buf; |
4553 | 0 | ListCell *lc; |
4554 | 0 | ListCell *lc2; |
4555 | 0 | int i; |
4556 | |
|
4557 | 0 | initStringInfo(&buf); |
4558 | |
|
4559 | 0 | i = 0; |
4560 | 0 | forboth(lc, xexpr->named_args, lc2, xexpr->arg_names) |
4561 | 0 | { |
4562 | 0 | Expr *e = (Expr *) lfirst(lc); |
4563 | 0 | char *argname = strVal(lfirst(lc2)); |
4564 | |
|
4565 | 0 | if (!argnull[i]) |
4566 | 0 | { |
4567 | 0 | value = argvalue[i]; |
4568 | 0 | appendStringInfo(&buf, "<%s>%s</%s>", |
4569 | 0 | argname, |
4570 | 0 | map_sql_value_to_xml_value(value, |
4571 | 0 | exprType((Node *) e), true), |
4572 | 0 | argname); |
4573 | 0 | *op->resnull = false; |
4574 | 0 | } |
4575 | 0 | i++; |
4576 | 0 | } |
4577 | |
|
4578 | 0 | if (!*op->resnull) |
4579 | 0 | { |
4580 | 0 | text *result; |
4581 | |
|
4582 | 0 | result = cstring_to_text_with_len(buf.data, buf.len); |
4583 | 0 | *op->resvalue = PointerGetDatum(result); |
4584 | 0 | } |
4585 | |
|
4586 | 0 | pfree(buf.data); |
4587 | 0 | } |
4588 | 0 | break; |
4589 | | |
4590 | 0 | case IS_XMLELEMENT: |
4591 | 0 | *op->resvalue = PointerGetDatum(xmlelement(xexpr, |
4592 | 0 | op->d.xmlexpr.named_argvalue, |
4593 | 0 | op->d.xmlexpr.named_argnull, |
4594 | 0 | op->d.xmlexpr.argvalue, |
4595 | 0 | op->d.xmlexpr.argnull)); |
4596 | 0 | *op->resnull = false; |
4597 | 0 | break; |
4598 | | |
4599 | 0 | case IS_XMLPARSE: |
4600 | 0 | { |
4601 | 0 | Datum *argvalue = op->d.xmlexpr.argvalue; |
4602 | 0 | bool *argnull = op->d.xmlexpr.argnull; |
4603 | 0 | text *data; |
4604 | 0 | bool preserve_whitespace; |
4605 | | |
4606 | | /* arguments are known to be text, bool */ |
4607 | 0 | Assert(list_length(xexpr->args) == 2); |
4608 | |
|
4609 | 0 | if (argnull[0]) |
4610 | 0 | return; |
4611 | 0 | value = argvalue[0]; |
4612 | 0 | data = DatumGetTextPP(value); |
4613 | |
|
4614 | 0 | if (argnull[1]) /* probably can't happen */ |
4615 | 0 | return; |
4616 | 0 | value = argvalue[1]; |
4617 | 0 | preserve_whitespace = DatumGetBool(value); |
4618 | |
|
4619 | 0 | *op->resvalue = PointerGetDatum(xmlparse(data, |
4620 | 0 | xexpr->xmloption, |
4621 | 0 | preserve_whitespace, NULL)); |
4622 | 0 | *op->resnull = false; |
4623 | 0 | } |
4624 | 0 | break; |
4625 | | |
4626 | 0 | case IS_XMLPI: |
4627 | 0 | { |
4628 | 0 | text *arg; |
4629 | 0 | bool isnull; |
4630 | | |
4631 | | /* optional argument is known to be text */ |
4632 | 0 | Assert(list_length(xexpr->args) <= 1); |
4633 | |
|
4634 | 0 | if (xexpr->args) |
4635 | 0 | { |
4636 | 0 | isnull = op->d.xmlexpr.argnull[0]; |
4637 | 0 | if (isnull) |
4638 | 0 | arg = NULL; |
4639 | 0 | else |
4640 | 0 | arg = DatumGetTextPP(op->d.xmlexpr.argvalue[0]); |
4641 | 0 | } |
4642 | 0 | else |
4643 | 0 | { |
4644 | 0 | arg = NULL; |
4645 | 0 | isnull = false; |
4646 | 0 | } |
4647 | |
|
4648 | 0 | *op->resvalue = PointerGetDatum(xmlpi(xexpr->name, |
4649 | 0 | arg, |
4650 | 0 | isnull, |
4651 | 0 | op->resnull)); |
4652 | 0 | } |
4653 | 0 | break; |
4654 | | |
4655 | 0 | case IS_XMLROOT: |
4656 | 0 | { |
4657 | 0 | Datum *argvalue = op->d.xmlexpr.argvalue; |
4658 | 0 | bool *argnull = op->d.xmlexpr.argnull; |
4659 | 0 | xmltype *data; |
4660 | 0 | text *version; |
4661 | 0 | int standalone; |
4662 | | |
4663 | | /* arguments are known to be xml, text, int */ |
4664 | 0 | Assert(list_length(xexpr->args) == 3); |
4665 | |
|
4666 | 0 | if (argnull[0]) |
4667 | 0 | return; |
4668 | 0 | data = DatumGetXmlP(argvalue[0]); |
4669 | |
|
4670 | 0 | if (argnull[1]) |
4671 | 0 | version = NULL; |
4672 | 0 | else |
4673 | 0 | version = DatumGetTextPP(argvalue[1]); |
4674 | |
|
4675 | 0 | Assert(!argnull[2]); /* always present */ |
4676 | 0 | standalone = DatumGetInt32(argvalue[2]); |
4677 | |
|
4678 | 0 | *op->resvalue = PointerGetDatum(xmlroot(data, |
4679 | 0 | version, |
4680 | 0 | standalone)); |
4681 | 0 | *op->resnull = false; |
4682 | 0 | } |
4683 | 0 | break; |
4684 | | |
4685 | 0 | case IS_XMLSERIALIZE: |
4686 | 0 | { |
4687 | 0 | Datum *argvalue = op->d.xmlexpr.argvalue; |
4688 | 0 | bool *argnull = op->d.xmlexpr.argnull; |
4689 | | |
4690 | | /* argument type is known to be xml */ |
4691 | 0 | Assert(list_length(xexpr->args) == 1); |
4692 | |
|
4693 | 0 | if (argnull[0]) |
4694 | 0 | return; |
4695 | 0 | value = argvalue[0]; |
4696 | |
|
4697 | 0 | *op->resvalue = |
4698 | 0 | PointerGetDatum(xmltotext_with_options(DatumGetXmlP(value), |
4699 | 0 | xexpr->xmloption, |
4700 | 0 | xexpr->indent)); |
4701 | 0 | *op->resnull = false; |
4702 | 0 | } |
4703 | 0 | break; |
4704 | | |
4705 | 0 | case IS_DOCUMENT: |
4706 | 0 | { |
4707 | 0 | Datum *argvalue = op->d.xmlexpr.argvalue; |
4708 | 0 | bool *argnull = op->d.xmlexpr.argnull; |
4709 | | |
4710 | | /* optional argument is known to be xml */ |
4711 | 0 | Assert(list_length(xexpr->args) == 1); |
4712 | |
|
4713 | 0 | if (argnull[0]) |
4714 | 0 | return; |
4715 | 0 | value = argvalue[0]; |
4716 | |
|
4717 | 0 | *op->resvalue = |
4718 | 0 | BoolGetDatum(xml_is_document(DatumGetXmlP(value))); |
4719 | 0 | *op->resnull = false; |
4720 | 0 | } |
4721 | 0 | break; |
4722 | | |
4723 | 0 | default: |
4724 | 0 | elog(ERROR, "unrecognized XML operation"); |
4725 | 0 | break; |
4726 | 0 | } |
4727 | 0 | } |
4728 | | |
4729 | | /* |
4730 | | * Evaluate a JSON constructor expression. |
4731 | | */ |
4732 | | void |
4733 | | ExecEvalJsonConstructor(ExprState *state, ExprEvalStep *op, |
4734 | | ExprContext *econtext) |
4735 | 0 | { |
4736 | 0 | Datum res; |
4737 | 0 | JsonConstructorExprState *jcstate = op->d.json_constructor.jcstate; |
4738 | 0 | JsonConstructorExpr *ctor = jcstate->constructor; |
4739 | 0 | bool is_jsonb = ctor->returning->format->format_type == JS_FORMAT_JSONB; |
4740 | 0 | bool isnull = false; |
4741 | |
|
4742 | 0 | if (ctor->type == JSCTOR_JSON_ARRAY) |
4743 | 0 | res = (is_jsonb ? |
4744 | 0 | jsonb_build_array_worker : |
4745 | 0 | json_build_array_worker) (jcstate->nargs, |
4746 | 0 | jcstate->arg_values, |
4747 | 0 | jcstate->arg_nulls, |
4748 | 0 | jcstate->arg_types, |
4749 | 0 | jcstate->constructor->absent_on_null); |
4750 | 0 | else if (ctor->type == JSCTOR_JSON_OBJECT) |
4751 | 0 | res = (is_jsonb ? |
4752 | 0 | jsonb_build_object_worker : |
4753 | 0 | json_build_object_worker) (jcstate->nargs, |
4754 | 0 | jcstate->arg_values, |
4755 | 0 | jcstate->arg_nulls, |
4756 | 0 | jcstate->arg_types, |
4757 | 0 | jcstate->constructor->absent_on_null, |
4758 | 0 | jcstate->constructor->unique); |
4759 | 0 | else if (ctor->type == JSCTOR_JSON_SCALAR) |
4760 | 0 | { |
4761 | 0 | if (jcstate->arg_nulls[0]) |
4762 | 0 | { |
4763 | 0 | res = (Datum) 0; |
4764 | 0 | isnull = true; |
4765 | 0 | } |
4766 | 0 | else |
4767 | 0 | { |
4768 | 0 | Datum value = jcstate->arg_values[0]; |
4769 | 0 | Oid outfuncid = jcstate->arg_type_cache[0].outfuncid; |
4770 | 0 | JsonTypeCategory category = (JsonTypeCategory) |
4771 | 0 | jcstate->arg_type_cache[0].category; |
4772 | |
|
4773 | 0 | if (is_jsonb) |
4774 | 0 | res = datum_to_jsonb(value, category, outfuncid); |
4775 | 0 | else |
4776 | 0 | res = datum_to_json(value, category, outfuncid); |
4777 | 0 | } |
4778 | 0 | } |
4779 | 0 | else if (ctor->type == JSCTOR_JSON_PARSE) |
4780 | 0 | { |
4781 | 0 | if (jcstate->arg_nulls[0]) |
4782 | 0 | { |
4783 | 0 | res = (Datum) 0; |
4784 | 0 | isnull = true; |
4785 | 0 | } |
4786 | 0 | else |
4787 | 0 | { |
4788 | 0 | Datum value = jcstate->arg_values[0]; |
4789 | 0 | text *js = DatumGetTextP(value); |
4790 | |
|
4791 | 0 | if (is_jsonb) |
4792 | 0 | res = jsonb_from_text(js, true); |
4793 | 0 | else |
4794 | 0 | { |
4795 | 0 | (void) json_validate(js, true, true); |
4796 | 0 | res = value; |
4797 | 0 | } |
4798 | 0 | } |
4799 | 0 | } |
4800 | 0 | else |
4801 | 0 | elog(ERROR, "invalid JsonConstructorExpr type %d", ctor->type); |
4802 | | |
4803 | 0 | *op->resvalue = res; |
4804 | 0 | *op->resnull = isnull; |
4805 | 0 | } |
4806 | | |
4807 | | /* |
4808 | | * Evaluate a IS JSON predicate. |
4809 | | */ |
4810 | | void |
4811 | | ExecEvalJsonIsPredicate(ExprState *state, ExprEvalStep *op) |
4812 | 0 | { |
4813 | 0 | JsonIsPredicate *pred = op->d.is_json.pred; |
4814 | 0 | Datum js = *op->resvalue; |
4815 | 0 | Oid exprtype = pred->exprBaseType; |
4816 | 0 | bool res; |
4817 | |
|
4818 | 0 | if (*op->resnull) |
4819 | 0 | { |
4820 | 0 | *op->resvalue = BoolGetDatum(false); |
4821 | 0 | return; |
4822 | 0 | } |
4823 | | |
4824 | 0 | if (exprtype == TEXTOID || exprtype == JSONOID) |
4825 | 0 | { |
4826 | 0 | text *json = DatumGetTextP(js); |
4827 | |
|
4828 | 0 | if (pred->item_type == JS_TYPE_ANY) |
4829 | 0 | res = true; |
4830 | 0 | else |
4831 | 0 | { |
4832 | 0 | switch (json_get_first_token(json, false)) |
4833 | 0 | { |
4834 | 0 | case JSON_TOKEN_OBJECT_START: |
4835 | 0 | res = pred->item_type == JS_TYPE_OBJECT; |
4836 | 0 | break; |
4837 | 0 | case JSON_TOKEN_ARRAY_START: |
4838 | 0 | res = pred->item_type == JS_TYPE_ARRAY; |
4839 | 0 | break; |
4840 | 0 | case JSON_TOKEN_STRING: |
4841 | 0 | case JSON_TOKEN_NUMBER: |
4842 | 0 | case JSON_TOKEN_TRUE: |
4843 | 0 | case JSON_TOKEN_FALSE: |
4844 | 0 | case JSON_TOKEN_NULL: |
4845 | 0 | res = pred->item_type == JS_TYPE_SCALAR; |
4846 | 0 | break; |
4847 | 0 | default: |
4848 | 0 | res = false; |
4849 | 0 | break; |
4850 | 0 | } |
4851 | 0 | } |
4852 | | |
4853 | | /* |
4854 | | * Do full parsing pass only for uniqueness check or for JSON text |
4855 | | * validation. |
4856 | | */ |
4857 | 0 | if (res && (pred->unique_keys || exprtype == TEXTOID)) |
4858 | 0 | res = json_validate(json, pred->unique_keys, false); |
4859 | 0 | } |
4860 | 0 | else if (exprtype == JSONBOID) |
4861 | 0 | { |
4862 | 0 | if (pred->item_type == JS_TYPE_ANY) |
4863 | 0 | res = true; |
4864 | 0 | else |
4865 | 0 | { |
4866 | 0 | Jsonb *jb = DatumGetJsonbP(js); |
4867 | |
|
4868 | 0 | switch (pred->item_type) |
4869 | 0 | { |
4870 | 0 | case JS_TYPE_OBJECT: |
4871 | 0 | res = JB_ROOT_IS_OBJECT(jb); |
4872 | 0 | break; |
4873 | 0 | case JS_TYPE_ARRAY: |
4874 | 0 | res = JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb); |
4875 | 0 | break; |
4876 | 0 | case JS_TYPE_SCALAR: |
4877 | 0 | res = JB_ROOT_IS_ARRAY(jb) && JB_ROOT_IS_SCALAR(jb); |
4878 | 0 | break; |
4879 | 0 | default: |
4880 | 0 | res = false; |
4881 | 0 | break; |
4882 | 0 | } |
4883 | 0 | } |
4884 | | |
4885 | | /* Key uniqueness check is redundant for jsonb */ |
4886 | 0 | } |
4887 | 0 | else |
4888 | 0 | res = false; |
4889 | | |
4890 | 0 | *op->resvalue = BoolGetDatum(res); |
4891 | 0 | } |
4892 | | |
4893 | | /* |
4894 | | * Evaluate a jsonpath against a document, whose values (if not NULL) have been |
4895 | | * saved in op->d.jsonexpr.jsestate. |
4896 | | * |
4897 | | * This step is reached even if the document or the jsonpath is NULL, so that |
4898 | | * JsonExprState.error and JsonExprState.empty are reset for every row before |
4899 | | * the ON ERROR / ON EMPTY steps look at them. In that case, the result is |
4900 | | * NULL and jsonpath evaluation is skipped, as are the ON ERROR / ON EMPTY |
4901 | | * steps; the NULL is still passed through the coercion steps, if any, so that |
4902 | | * the constraints of a RETURNING domain type are checked. |
4903 | | * |
4904 | | * If an error occurs during JsonPath* evaluation or when coercing its result |
4905 | | * to the RETURNING type, JsonExprState.error is set to true, provided the |
4906 | | * ON ERROR behavior is not ERROR. Similarly, if JsonPath{Query|Value}() found |
4907 | | * no matching items, JsonExprState.empty is set to true, provided the ON EMPTY |
4908 | | * behavior is not ERROR. That is to signal to the subsequent steps that check |
4909 | | * those flags to return the ON ERROR / ON EMPTY expression. |
4910 | | * |
4911 | | * Return value is the step address to be performed next. It will be one of |
4912 | | * jump_error, jump_empty, jump_eval_coercion, or jump_end, all given in |
4913 | | * op->d.jsonexpr.jsestate. |
4914 | | */ |
4915 | | int |
4916 | | ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op, |
4917 | | ExprContext *econtext) |
4918 | 0 | { |
4919 | 0 | JsonExprState *jsestate = op->d.jsonexpr.jsestate; |
4920 | 0 | JsonExpr *jsexpr = jsestate->jsexpr; |
4921 | 0 | Datum item; |
4922 | 0 | JsonPath *path; |
4923 | 0 | bool throw_error = jsexpr->on_error->btype == JSON_BEHAVIOR_ERROR; |
4924 | 0 | bool error = false, |
4925 | 0 | empty = false; |
4926 | 0 | int jump_eval_coercion = jsestate->jump_eval_coercion; |
4927 | 0 | char *val_string = NULL; |
4928 | | |
4929 | | /* Set error/empty to false. */ |
4930 | 0 | memset(&jsestate->error, 0, sizeof(NullableDatum)); |
4931 | 0 | memset(&jsestate->empty, 0, sizeof(NullableDatum)); |
4932 | | |
4933 | | /* Also reset ErrorSaveContext contents for the next row. */ |
4934 | 0 | if (jsestate->escontext.details_wanted) |
4935 | 0 | { |
4936 | 0 | jsestate->escontext.error_data = NULL; |
4937 | 0 | jsestate->escontext.details_wanted = false; |
4938 | 0 | } |
4939 | 0 | jsestate->escontext.error_occurred = false; |
4940 | | |
4941 | | /* |
4942 | | * Return NULL if formatted_expr or pathspec is NULL, skipping the ON |
4943 | | * ERROR and ON EMPTY steps. If the RETURNING type is a domain with |
4944 | | * constraints, the NULL must still be coerced to it so that the |
4945 | | * constraints are checked. |
4946 | | */ |
4947 | 0 | if (jsestate->formatted_expr.isnull || jsestate->pathspec.isnull) |
4948 | 0 | { |
4949 | 0 | *op->resvalue = (Datum) 0; |
4950 | 0 | *op->resnull = true; |
4951 | |
|
4952 | 0 | if (jump_eval_coercion >= 0) |
4953 | 0 | return jump_eval_coercion; |
4954 | 0 | else |
4955 | 0 | return jsestate->jump_end; |
4956 | 0 | } |
4957 | | |
4958 | 0 | item = jsestate->formatted_expr.value; |
4959 | 0 | path = DatumGetJsonPathP(jsestate->pathspec.value); |
4960 | |
|
4961 | 0 | switch (jsexpr->op) |
4962 | 0 | { |
4963 | 0 | case JSON_EXISTS_OP: |
4964 | 0 | { |
4965 | 0 | bool exists = JsonPathExists(item, path, |
4966 | 0 | !throw_error ? &error : NULL, |
4967 | 0 | jsestate->args); |
4968 | |
|
4969 | 0 | if (!error) |
4970 | 0 | { |
4971 | 0 | *op->resnull = false; |
4972 | 0 | *op->resvalue = BoolGetDatum(exists); |
4973 | 0 | } |
4974 | 0 | } |
4975 | 0 | break; |
4976 | | |
4977 | 0 | case JSON_QUERY_OP: |
4978 | 0 | *op->resvalue = JsonPathQuery(item, path, jsexpr->wrapper, &empty, |
4979 | 0 | !throw_error ? &error : NULL, |
4980 | 0 | jsestate->args, |
4981 | 0 | jsexpr->column_name); |
4982 | |
|
4983 | 0 | *op->resnull = (DatumGetPointer(*op->resvalue) == NULL); |
4984 | 0 | break; |
4985 | | |
4986 | 0 | case JSON_VALUE_OP: |
4987 | 0 | { |
4988 | 0 | JsonbValue *jbv = JsonPathValue(item, path, &empty, |
4989 | 0 | !throw_error ? &error : NULL, |
4990 | 0 | jsestate->args, |
4991 | 0 | jsexpr->column_name); |
4992 | |
|
4993 | 0 | if (jbv == NULL) |
4994 | 0 | { |
4995 | | /* Will be coerced with json_populate_type(), if needed. */ |
4996 | 0 | *op->resvalue = (Datum) 0; |
4997 | 0 | *op->resnull = true; |
4998 | 0 | } |
4999 | 0 | else if (!error && !empty) |
5000 | 0 | { |
5001 | 0 | if (jsexpr->returning->typid == JSONOID || |
5002 | 0 | jsexpr->returning->typid == JSONBOID) |
5003 | 0 | { |
5004 | 0 | val_string = DatumGetCString(DirectFunctionCall1(jsonb_out, |
5005 | 0 | JsonbPGetDatum(JsonbValueToJsonb(jbv)))); |
5006 | 0 | *op->resnull = false; |
5007 | 0 | } |
5008 | 0 | else if (jsexpr->use_json_coercion) |
5009 | 0 | { |
5010 | 0 | *op->resvalue = JsonbPGetDatum(JsonbValueToJsonb(jbv)); |
5011 | 0 | *op->resnull = false; |
5012 | 0 | } |
5013 | 0 | else |
5014 | 0 | { |
5015 | | /* ExecGetJsonValueItemString() sets *op->resnull. */ |
5016 | 0 | val_string = ExecGetJsonValueItemString(jbv, op->resnull); |
5017 | | |
5018 | | /* |
5019 | | * Simply convert to the default RETURNING type (text) |
5020 | | * if no coercion needed. |
5021 | | */ |
5022 | 0 | if (!jsexpr->use_io_coercion) |
5023 | 0 | *op->resvalue = DirectFunctionCall1(textin, |
5024 | 0 | CStringGetDatum(val_string)); |
5025 | 0 | } |
5026 | 0 | } |
5027 | 0 | break; |
5028 | 0 | } |
5029 | | |
5030 | | /* JSON_TABLE_OP can't happen here */ |
5031 | | |
5032 | 0 | default: |
5033 | 0 | elog(ERROR, "unrecognized SQL/JSON expression op %d", |
5034 | 0 | (int) jsexpr->op); |
5035 | 0 | return false; |
5036 | 0 | } |
5037 | | |
5038 | | /* |
5039 | | * Coerce the result value to the RETURNING type by calling its input |
5040 | | * function. |
5041 | | */ |
5042 | 0 | if (!*op->resnull && jsexpr->use_io_coercion) |
5043 | 0 | { |
5044 | 0 | FunctionCallInfo fcinfo; |
5045 | |
|
5046 | 0 | Assert(jump_eval_coercion == -1); |
5047 | 0 | fcinfo = jsestate->input_fcinfo; |
5048 | 0 | Assert(fcinfo != NULL); |
5049 | 0 | Assert(val_string != NULL); |
5050 | 0 | fcinfo->args[0].value = PointerGetDatum(val_string); |
5051 | 0 | fcinfo->args[0].isnull = *op->resnull; |
5052 | | |
5053 | | /* |
5054 | | * Second and third arguments are already set up in |
5055 | | * ExecInitJsonExpr(). |
5056 | | */ |
5057 | |
|
5058 | 0 | fcinfo->isnull = false; |
5059 | 0 | *op->resvalue = FunctionCallInvoke(fcinfo); |
5060 | 0 | if (SOFT_ERROR_OCCURRED(&jsestate->escontext)) |
5061 | 0 | error = true; |
5062 | 0 | } |
5063 | | |
5064 | | /* |
5065 | | * When setting up the ErrorSaveContext (if needed) for capturing the |
5066 | | * errors that occur when coercing the JsonBehavior expression, set |
5067 | | * details_wanted to be able to show the actual error message as the |
5068 | | * DETAIL of the error message that tells that it is the JsonBehavior |
5069 | | * expression that caused the error; see ExecEvalJsonCoercionFinish(). |
5070 | | */ |
5071 | | |
5072 | | /* Handle ON EMPTY. */ |
5073 | 0 | if (empty) |
5074 | 0 | { |
5075 | 0 | *op->resvalue = (Datum) 0; |
5076 | 0 | *op->resnull = true; |
5077 | 0 | if (jsexpr->on_empty) |
5078 | 0 | { |
5079 | 0 | if (jsexpr->on_empty->btype != JSON_BEHAVIOR_ERROR) |
5080 | 0 | { |
5081 | 0 | jsestate->empty.value = BoolGetDatum(true); |
5082 | | /* Set up to catch coercion errors of the ON EMPTY value. */ |
5083 | 0 | jsestate->escontext.error_occurred = false; |
5084 | 0 | jsestate->escontext.details_wanted = true; |
5085 | | /* Jump to end if the ON EMPTY behavior is to return NULL */ |
5086 | 0 | return jsestate->jump_empty >= 0 ? jsestate->jump_empty : jsestate->jump_end; |
5087 | 0 | } |
5088 | 0 | } |
5089 | 0 | else if (jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR) |
5090 | 0 | { |
5091 | 0 | jsestate->error.value = BoolGetDatum(true); |
5092 | | /* Set up to catch coercion errors of the ON ERROR value. */ |
5093 | 0 | jsestate->escontext.error_occurred = false; |
5094 | 0 | jsestate->escontext.details_wanted = true; |
5095 | 0 | Assert(!throw_error); |
5096 | | /* Jump to end if the ON ERROR behavior is to return NULL */ |
5097 | 0 | return jsestate->jump_error >= 0 ? jsestate->jump_error : jsestate->jump_end; |
5098 | 0 | } |
5099 | | |
5100 | 0 | if (jsexpr->column_name) |
5101 | 0 | ereport(ERROR, |
5102 | 0 | errcode(ERRCODE_NO_SQL_JSON_ITEM), |
5103 | 0 | errmsg("no SQL/JSON item found for specified path of column \"%s\"", |
5104 | 0 | jsexpr->column_name)); |
5105 | 0 | else |
5106 | 0 | ereport(ERROR, |
5107 | 0 | errcode(ERRCODE_NO_SQL_JSON_ITEM), |
5108 | 0 | errmsg("no SQL/JSON item found for specified path")); |
5109 | 0 | } |
5110 | | |
5111 | | /* |
5112 | | * ON ERROR. Wouldn't get here if the behavior is ERROR, because they |
5113 | | * would have already been thrown. |
5114 | | */ |
5115 | 0 | if (error) |
5116 | 0 | { |
5117 | 0 | Assert(!throw_error); |
5118 | 0 | *op->resvalue = (Datum) 0; |
5119 | 0 | *op->resnull = true; |
5120 | 0 | jsestate->error.value = BoolGetDatum(true); |
5121 | | /* Set up to catch coercion errors of the ON ERROR value. */ |
5122 | 0 | jsestate->escontext.error_occurred = false; |
5123 | 0 | jsestate->escontext.details_wanted = true; |
5124 | | /* Jump to end if the ON ERROR behavior is to return NULL */ |
5125 | 0 | return jsestate->jump_error >= 0 ? jsestate->jump_error : jsestate->jump_end; |
5126 | 0 | } |
5127 | | |
5128 | 0 | return jump_eval_coercion >= 0 ? jump_eval_coercion : jsestate->jump_end; |
5129 | 0 | } |
5130 | | |
5131 | | /* |
5132 | | * Convert the given JsonbValue to its C string representation |
5133 | | * |
5134 | | * *resnull is set if the JsonbValue is a jbvNull. |
5135 | | */ |
5136 | | static char * |
5137 | | ExecGetJsonValueItemString(JsonbValue *item, bool *resnull) |
5138 | 0 | { |
5139 | 0 | *resnull = false; |
5140 | | |
5141 | | /* get coercion state reference and datum of the corresponding SQL type */ |
5142 | 0 | switch (item->type) |
5143 | 0 | { |
5144 | 0 | case jbvNull: |
5145 | 0 | *resnull = true; |
5146 | 0 | return NULL; |
5147 | | |
5148 | 0 | case jbvString: |
5149 | 0 | { |
5150 | 0 | char *str = palloc(item->val.string.len + 1); |
5151 | |
|
5152 | 0 | memcpy(str, item->val.string.val, item->val.string.len); |
5153 | 0 | str[item->val.string.len] = '\0'; |
5154 | 0 | return str; |
5155 | 0 | } |
5156 | | |
5157 | 0 | case jbvNumeric: |
5158 | 0 | return DatumGetCString(DirectFunctionCall1(numeric_out, |
5159 | 0 | NumericGetDatum(item->val.numeric))); |
5160 | | |
5161 | 0 | case jbvBool: |
5162 | 0 | return DatumGetCString(DirectFunctionCall1(boolout, |
5163 | 0 | BoolGetDatum(item->val.boolean))); |
5164 | | |
5165 | 0 | case jbvDatetime: |
5166 | 0 | switch (item->val.datetime.typid) |
5167 | 0 | { |
5168 | 0 | case DATEOID: |
5169 | 0 | return DatumGetCString(DirectFunctionCall1(date_out, |
5170 | 0 | item->val.datetime.value)); |
5171 | 0 | case TIMEOID: |
5172 | 0 | return DatumGetCString(DirectFunctionCall1(time_out, |
5173 | 0 | item->val.datetime.value)); |
5174 | 0 | case TIMETZOID: |
5175 | 0 | return DatumGetCString(DirectFunctionCall1(timetz_out, |
5176 | 0 | item->val.datetime.value)); |
5177 | 0 | case TIMESTAMPOID: |
5178 | 0 | return DatumGetCString(DirectFunctionCall1(timestamp_out, |
5179 | 0 | item->val.datetime.value)); |
5180 | 0 | case TIMESTAMPTZOID: |
5181 | 0 | return DatumGetCString(DirectFunctionCall1(timestamptz_out, |
5182 | 0 | item->val.datetime.value)); |
5183 | 0 | default: |
5184 | 0 | elog(ERROR, "unexpected jsonb datetime type oid %u", |
5185 | 0 | item->val.datetime.typid); |
5186 | 0 | } |
5187 | 0 | break; |
5188 | | |
5189 | 0 | case jbvArray: |
5190 | 0 | case jbvObject: |
5191 | 0 | case jbvBinary: |
5192 | 0 | return DatumGetCString(DirectFunctionCall1(jsonb_out, |
5193 | 0 | JsonbPGetDatum(JsonbValueToJsonb(item)))); |
5194 | | |
5195 | 0 | default: |
5196 | 0 | elog(ERROR, "unexpected jsonb value type %d", item->type); |
5197 | 0 | } |
5198 | | |
5199 | 0 | Assert(false); |
5200 | 0 | *resnull = true; |
5201 | 0 | return NULL; |
5202 | 0 | } |
5203 | | |
5204 | | /* |
5205 | | * Coerce a jsonb value produced by ExecEvalJsonExprPath() or an ON ERROR / |
5206 | | * ON EMPTY behavior expression to the target type. |
5207 | | * |
5208 | | * Any soft errors that occur here will be checked by |
5209 | | * EEOP_JSONEXPR_COERCION_FINISH that will run after this. |
5210 | | */ |
5211 | | void |
5212 | | ExecEvalJsonCoercion(ExprState *state, ExprEvalStep *op, |
5213 | | ExprContext *econtext) |
5214 | 0 | { |
5215 | 0 | ErrorSaveContext *escontext = op->d.jsonexpr_coercion.escontext; |
5216 | | |
5217 | | /* |
5218 | | * If an error was saved while evaluating the ON ERROR or ON EMPTY DEFAULT |
5219 | | * expression whose result we are to coerce, that result is going to be |
5220 | | * discarded when the following COERCION_FINISH step throws the error. |
5221 | | * Don't coerce it; doing so might throw a hard error or overwrite the |
5222 | | * saved error. |
5223 | | */ |
5224 | 0 | if (SOFT_ERROR_OCCURRED(escontext)) |
5225 | 0 | { |
5226 | 0 | *op->resnull = true; |
5227 | 0 | *op->resvalue = (Datum) 0; |
5228 | 0 | return; |
5229 | 0 | } |
5230 | | |
5231 | | /* |
5232 | | * Prepare to call json_populate_type() to coerce the boolean result of |
5233 | | * JSON_EXISTS_OP to the target type. If the target type is integer or a |
5234 | | * domain over integer, call the boolean-to-integer cast function instead, |
5235 | | * because the integer's input function (which is what |
5236 | | * json_populate_type() calls to coerce to scalar target types) doesn't |
5237 | | * accept boolean literals as valid input. We only have a special case |
5238 | | * for integer and domains thereof as it seems common to use those types |
5239 | | * for EXISTS columns in JSON_TABLE(). |
5240 | | */ |
5241 | 0 | if (op->d.jsonexpr_coercion.exists_coerce) |
5242 | 0 | { |
5243 | 0 | if (op->d.jsonexpr_coercion.exists_cast_to_int) |
5244 | 0 | { |
5245 | | /* Check domain constraints if any. */ |
5246 | 0 | if (op->d.jsonexpr_coercion.exists_check_domain && |
5247 | 0 | !domain_check_safe(*op->resvalue, *op->resnull, |
5248 | 0 | op->d.jsonexpr_coercion.targettype, |
5249 | 0 | &op->d.jsonexpr_coercion.json_coercion_cache, |
5250 | 0 | econtext->ecxt_per_query_memory, |
5251 | 0 | (Node *) escontext)) |
5252 | 0 | { |
5253 | 0 | *op->resnull = true; |
5254 | 0 | *op->resvalue = (Datum) 0; |
5255 | 0 | } |
5256 | 0 | else |
5257 | 0 | *op->resvalue = DirectFunctionCall1(bool_int4, *op->resvalue); |
5258 | 0 | return; |
5259 | 0 | } |
5260 | | |
5261 | 0 | *op->resvalue = DirectFunctionCall1(jsonb_in, |
5262 | 0 | DatumGetBool(*op->resvalue) ? |
5263 | 0 | CStringGetDatum("true") : |
5264 | 0 | CStringGetDatum("false")); |
5265 | 0 | } |
5266 | | |
5267 | 0 | *op->resvalue = json_populate_type(*op->resvalue, JSONBOID, |
5268 | 0 | op->d.jsonexpr_coercion.targettype, |
5269 | 0 | op->d.jsonexpr_coercion.targettypmod, |
5270 | 0 | &op->d.jsonexpr_coercion.json_coercion_cache, |
5271 | 0 | econtext->ecxt_per_query_memory, |
5272 | 0 | op->resnull, |
5273 | 0 | op->d.jsonexpr_coercion.omit_quotes, |
5274 | 0 | (Node *) escontext); |
5275 | 0 | } |
5276 | | |
5277 | | static char * |
5278 | | GetJsonBehaviorValueString(JsonBehavior *behavior) |
5279 | 0 | { |
5280 | | /* |
5281 | | * The order of array elements must correspond to the order of |
5282 | | * JsonBehaviorType members. |
5283 | | */ |
5284 | 0 | const char *behavior_names[] = |
5285 | 0 | { |
5286 | 0 | "NULL", |
5287 | 0 | "ERROR", |
5288 | 0 | "EMPTY", |
5289 | 0 | "TRUE", |
5290 | 0 | "FALSE", |
5291 | 0 | "UNKNOWN", |
5292 | 0 | "EMPTY ARRAY", |
5293 | 0 | "EMPTY OBJECT", |
5294 | 0 | "DEFAULT" |
5295 | 0 | }; |
5296 | |
|
5297 | 0 | return pstrdup(behavior_names[behavior->btype]); |
5298 | 0 | } |
5299 | | |
5300 | | /* |
5301 | | * Checks if an error occurred in ExecEvalJsonCoercion(). If so, this sets |
5302 | | * JsonExprState.error to trigger the ON ERROR handling steps, unless the |
5303 | | * error is thrown when coercing a JsonBehavior value. |
5304 | | */ |
5305 | | void |
5306 | | ExecEvalJsonCoercionFinish(ExprState *state, ExprEvalStep *op) |
5307 | 0 | { |
5308 | 0 | JsonExprState *jsestate = op->d.jsonexpr.jsestate; |
5309 | |
|
5310 | 0 | if (SOFT_ERROR_OCCURRED(&jsestate->escontext)) |
5311 | 0 | { |
5312 | | /* |
5313 | | * jsestate->error or jsestate->empty being set means that the error |
5314 | | * occurred when coercing the JsonBehavior value. Throw the error in |
5315 | | * that case with the actual coercion error message shown in the |
5316 | | * DETAIL part. |
5317 | | */ |
5318 | 0 | if (DatumGetBool(jsestate->error.value)) |
5319 | 0 | ereport(ERROR, |
5320 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
5321 | | /*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */ |
5322 | 0 | errmsg("could not coerce %s expression (%s) to the RETURNING type", |
5323 | 0 | "ON ERROR", |
5324 | 0 | GetJsonBehaviorValueString(jsestate->jsexpr->on_error)), |
5325 | 0 | errdetail("%s", jsestate->escontext.error_data->message))); |
5326 | 0 | else if (DatumGetBool(jsestate->empty.value)) |
5327 | 0 | ereport(ERROR, |
5328 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
5329 | | /*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */ |
5330 | 0 | errmsg("could not coerce %s expression (%s) to the RETURNING type", |
5331 | 0 | "ON EMPTY", |
5332 | 0 | GetJsonBehaviorValueString(jsestate->jsexpr->on_empty)), |
5333 | 0 | errdetail("%s", jsestate->escontext.error_data->message))); |
5334 | | |
5335 | 0 | *op->resvalue = (Datum) 0; |
5336 | 0 | *op->resnull = true; |
5337 | |
|
5338 | 0 | jsestate->error.value = BoolGetDatum(true); |
5339 | | |
5340 | | /* |
5341 | | * Reset for next use such as for catching errors when coercing a |
5342 | | * JsonBehavior expression. |
5343 | | */ |
5344 | 0 | jsestate->escontext.error_occurred = false; |
5345 | 0 | jsestate->escontext.details_wanted = true; |
5346 | 0 | } |
5347 | 0 | } |
5348 | | |
5349 | | /* |
5350 | | * ExecEvalGroupingFunc |
5351 | | * |
5352 | | * Computes a bitmask with a bit for each (unevaluated) argument expression |
5353 | | * (rightmost arg is least significant bit). |
5354 | | * |
5355 | | * A bit is set if the corresponding expression is NOT part of the set of |
5356 | | * grouping expressions in the current grouping set. |
5357 | | */ |
5358 | | void |
5359 | | ExecEvalGroupingFunc(ExprState *state, ExprEvalStep *op) |
5360 | 0 | { |
5361 | 0 | AggState *aggstate = castNode(AggState, state->parent); |
5362 | 0 | int result = 0; |
5363 | 0 | Bitmapset *grouped_cols = aggstate->grouped_cols; |
5364 | 0 | ListCell *lc; |
5365 | |
|
5366 | 0 | foreach(lc, op->d.grouping_func.clauses) |
5367 | 0 | { |
5368 | 0 | int attnum = lfirst_int(lc); |
5369 | |
|
5370 | 0 | result <<= 1; |
5371 | |
|
5372 | 0 | if (!bms_is_member(attnum, grouped_cols)) |
5373 | 0 | result |= 1; |
5374 | 0 | } |
5375 | |
|
5376 | 0 | *op->resvalue = Int32GetDatum(result); |
5377 | 0 | *op->resnull = false; |
5378 | 0 | } |
5379 | | |
5380 | | /* |
5381 | | * ExecEvalMergeSupportFunc |
5382 | | * |
5383 | | * Returns information about the current MERGE action for its RETURNING list. |
5384 | | */ |
5385 | | void |
5386 | | ExecEvalMergeSupportFunc(ExprState *state, ExprEvalStep *op, |
5387 | | ExprContext *econtext) |
5388 | 0 | { |
5389 | 0 | ModifyTableState *mtstate = castNode(ModifyTableState, state->parent); |
5390 | 0 | MergeActionState *relaction = mtstate->mt_merge_action; |
5391 | |
|
5392 | 0 | if (!relaction) |
5393 | 0 | elog(ERROR, "no merge action in progress"); |
5394 | | |
5395 | | /* Return the MERGE action ("INSERT", "UPDATE", or "DELETE") */ |
5396 | 0 | switch (relaction->mas_action->commandType) |
5397 | 0 | { |
5398 | 0 | case CMD_INSERT: |
5399 | 0 | *op->resvalue = PointerGetDatum(cstring_to_text_with_len("INSERT", 6)); |
5400 | 0 | *op->resnull = false; |
5401 | 0 | break; |
5402 | 0 | case CMD_UPDATE: |
5403 | 0 | *op->resvalue = PointerGetDatum(cstring_to_text_with_len("UPDATE", 6)); |
5404 | 0 | *op->resnull = false; |
5405 | 0 | break; |
5406 | 0 | case CMD_DELETE: |
5407 | 0 | *op->resvalue = PointerGetDatum(cstring_to_text_with_len("DELETE", 6)); |
5408 | 0 | *op->resnull = false; |
5409 | 0 | break; |
5410 | 0 | case CMD_NOTHING: |
5411 | 0 | elog(ERROR, "unexpected merge action: DO NOTHING"); |
5412 | 0 | break; |
5413 | 0 | default: |
5414 | 0 | elog(ERROR, "unrecognized commandType: %d", |
5415 | 0 | (int) relaction->mas_action->commandType); |
5416 | 0 | } |
5417 | 0 | } |
5418 | | |
5419 | | /* |
5420 | | * Hand off evaluation of a subplan to nodeSubplan.c |
5421 | | */ |
5422 | | void |
5423 | | ExecEvalSubPlan(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
5424 | 0 | { |
5425 | 0 | SubPlanState *sstate = op->d.subplan.sstate; |
5426 | | |
5427 | | /* could potentially be nested, so make sure there's enough stack */ |
5428 | 0 | check_stack_depth(); |
5429 | |
|
5430 | 0 | *op->resvalue = ExecSubPlan(sstate, econtext, op->resnull); |
5431 | 0 | } |
5432 | | |
5433 | | /* |
5434 | | * Evaluate a wholerow Var expression. |
5435 | | * |
5436 | | * Returns a Datum whose value is the value of a whole-row range variable |
5437 | | * with respect to given expression context. |
5438 | | */ |
5439 | | void |
5440 | | ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
5441 | 0 | { |
5442 | 0 | Var *variable = op->d.wholerow.var; |
5443 | 0 | TupleTableSlot *slot = NULL; |
5444 | 0 | TupleDesc output_tupdesc; |
5445 | 0 | MemoryContext oldcontext; |
5446 | 0 | HeapTupleHeader dtuple; |
5447 | 0 | HeapTuple tuple; |
5448 | | |
5449 | | /* This was checked by ExecInitExpr */ |
5450 | 0 | Assert(variable->varattno == InvalidAttrNumber); |
5451 | | |
5452 | | /* Get the input slot we want */ |
5453 | 0 | switch (variable->varno) |
5454 | 0 | { |
5455 | 0 | case INNER_VAR: |
5456 | | /* get the tuple from the inner node */ |
5457 | 0 | slot = econtext->ecxt_innertuple; |
5458 | 0 | break; |
5459 | | |
5460 | 0 | case OUTER_VAR: |
5461 | | /* get the tuple from the outer node */ |
5462 | 0 | slot = econtext->ecxt_outertuple; |
5463 | 0 | break; |
5464 | | |
5465 | | /* INDEX_VAR is handled by default case */ |
5466 | | |
5467 | 0 | default: |
5468 | | |
5469 | | /* |
5470 | | * Get the tuple from the relation being scanned. |
5471 | | * |
5472 | | * By default, this uses the "scan" tuple slot, but a wholerow Var |
5473 | | * in the RETURNING list may explicitly refer to OLD/NEW. If the |
5474 | | * OLD/NEW row doesn't exist, we just return NULL. |
5475 | | */ |
5476 | 0 | switch (variable->varreturningtype) |
5477 | 0 | { |
5478 | 0 | case VAR_RETURNING_DEFAULT: |
5479 | 0 | slot = econtext->ecxt_scantuple; |
5480 | 0 | break; |
5481 | | |
5482 | 0 | case VAR_RETURNING_OLD: |
5483 | 0 | if (state->flags & EEO_FLAG_OLD_IS_NULL) |
5484 | 0 | { |
5485 | 0 | *op->resvalue = (Datum) 0; |
5486 | 0 | *op->resnull = true; |
5487 | 0 | return; |
5488 | 0 | } |
5489 | 0 | slot = econtext->ecxt_oldtuple; |
5490 | 0 | break; |
5491 | | |
5492 | 0 | case VAR_RETURNING_NEW: |
5493 | 0 | if (state->flags & EEO_FLAG_NEW_IS_NULL) |
5494 | 0 | { |
5495 | 0 | *op->resvalue = (Datum) 0; |
5496 | 0 | *op->resnull = true; |
5497 | 0 | return; |
5498 | 0 | } |
5499 | 0 | slot = econtext->ecxt_newtuple; |
5500 | 0 | break; |
5501 | 0 | } |
5502 | 0 | break; |
5503 | 0 | } |
5504 | | |
5505 | | /* Apply the junkfilter if any */ |
5506 | 0 | if (op->d.wholerow.junkFilter != NULL) |
5507 | 0 | slot = ExecFilterJunk(op->d.wholerow.junkFilter, slot); |
5508 | | |
5509 | | /* |
5510 | | * If first time through, obtain tuple descriptor and check compatibility. |
5511 | | * |
5512 | | * XXX: It'd be great if this could be moved to the expression |
5513 | | * initialization phase, but due to using slots that's currently not |
5514 | | * feasible. |
5515 | | */ |
5516 | 0 | if (op->d.wholerow.first) |
5517 | 0 | { |
5518 | | /* optimistically assume we don't need slow path */ |
5519 | 0 | op->d.wholerow.slow = false; |
5520 | | |
5521 | | /* |
5522 | | * If the Var identifies a named composite type, we must check that |
5523 | | * the actual tuple type is compatible with it. |
5524 | | */ |
5525 | 0 | if (variable->vartype != RECORDOID) |
5526 | 0 | { |
5527 | 0 | TupleDesc var_tupdesc; |
5528 | 0 | TupleDesc slot_tupdesc; |
5529 | | |
5530 | | /* |
5531 | | * We really only care about numbers of attributes and data types. |
5532 | | * Also, we can ignore type mismatch on columns that are dropped |
5533 | | * in the destination type, so long as (1) the physical storage |
5534 | | * matches or (2) the actual column value is NULL. Case (1) is |
5535 | | * helpful in some cases involving out-of-date cached plans, while |
5536 | | * case (2) is expected behavior in situations such as an INSERT |
5537 | | * into a table with dropped columns (the planner typically |
5538 | | * generates an INT4 NULL regardless of the dropped column type). |
5539 | | * If we find a dropped column and cannot verify that case (1) |
5540 | | * holds, we have to use the slow path to check (2) for each row. |
5541 | | * |
5542 | | * If vartype is a domain over composite, just look through that |
5543 | | * to the base composite type. |
5544 | | */ |
5545 | 0 | var_tupdesc = lookup_rowtype_tupdesc_domain(variable->vartype, |
5546 | 0 | -1, false); |
5547 | |
|
5548 | 0 | slot_tupdesc = slot->tts_tupleDescriptor; |
5549 | |
|
5550 | 0 | if (var_tupdesc->natts != slot_tupdesc->natts) |
5551 | 0 | ereport(ERROR, |
5552 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
5553 | 0 | errmsg("table row type and query-specified row type do not match"), |
5554 | 0 | errdetail_plural("Table row contains %d attribute, but query expects %d.", |
5555 | 0 | "Table row contains %d attributes, but query expects %d.", |
5556 | 0 | slot_tupdesc->natts, |
5557 | 0 | slot_tupdesc->natts, |
5558 | 0 | var_tupdesc->natts))); |
5559 | | |
5560 | 0 | for (int i = 0; i < var_tupdesc->natts; i++) |
5561 | 0 | { |
5562 | 0 | Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i); |
5563 | 0 | Form_pg_attribute sattr = TupleDescAttr(slot_tupdesc, i); |
5564 | |
|
5565 | 0 | if (vattr->atttypid == sattr->atttypid) |
5566 | 0 | continue; /* no worries */ |
5567 | 0 | if (!vattr->attisdropped) |
5568 | 0 | ereport(ERROR, |
5569 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
5570 | 0 | errmsg("table row type and query-specified row type do not match"), |
5571 | 0 | errdetail("Table has type %s at ordinal position %d, but query expects %s.", |
5572 | 0 | format_type_be(sattr->atttypid), |
5573 | 0 | i + 1, |
5574 | 0 | format_type_be(vattr->atttypid)))); |
5575 | | |
5576 | 0 | if (vattr->attlen != sattr->attlen || |
5577 | 0 | vattr->attalign != sattr->attalign) |
5578 | 0 | op->d.wholerow.slow = true; /* need to check for nulls */ |
5579 | 0 | } |
5580 | | |
5581 | | /* |
5582 | | * Use the variable's declared rowtype as the descriptor for the |
5583 | | * output values. In particular, we *must* absorb any |
5584 | | * attisdropped markings. |
5585 | | */ |
5586 | 0 | oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
5587 | 0 | output_tupdesc = CreateTupleDescCopy(var_tupdesc); |
5588 | 0 | MemoryContextSwitchTo(oldcontext); |
5589 | |
|
5590 | 0 | ReleaseTupleDesc(var_tupdesc); |
5591 | 0 | } |
5592 | 0 | else |
5593 | 0 | { |
5594 | | /* |
5595 | | * In the RECORD case, we use the input slot's rowtype as the |
5596 | | * descriptor for the output values, modulo possibly assigning new |
5597 | | * column names below. |
5598 | | */ |
5599 | 0 | oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
5600 | 0 | output_tupdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor); |
5601 | 0 | MemoryContextSwitchTo(oldcontext); |
5602 | | |
5603 | | /* |
5604 | | * It's possible that the input slot is a relation scan slot and |
5605 | | * so is marked with that relation's rowtype. But we're supposed |
5606 | | * to be returning RECORD, so reset to that. |
5607 | | */ |
5608 | 0 | output_tupdesc->tdtypeid = RECORDOID; |
5609 | 0 | output_tupdesc->tdtypmod = -1; |
5610 | | |
5611 | | /* |
5612 | | * We already got the correct physical datatype info above, but |
5613 | | * now we should try to find the source RTE and adopt its column |
5614 | | * aliases, since it's unlikely that the input slot has the |
5615 | | * desired names. |
5616 | | * |
5617 | | * If we can't locate the RTE, assume the column names we've got |
5618 | | * are OK. (As of this writing, the only cases where we can't |
5619 | | * locate the RTE are in execution of trigger WHEN clauses, and |
5620 | | * then the Var will have the trigger's relation's rowtype, so its |
5621 | | * names are fine.) Also, if the creator of the RTE didn't bother |
5622 | | * to fill in an eref field, assume our column names are OK. (This |
5623 | | * happens in COPY, and perhaps other places.) |
5624 | | */ |
5625 | 0 | if (econtext->ecxt_estate && |
5626 | 0 | variable->varno <= econtext->ecxt_estate->es_range_table_size) |
5627 | 0 | { |
5628 | 0 | RangeTblEntry *rte = exec_rt_fetch(variable->varno, |
5629 | 0 | econtext->ecxt_estate); |
5630 | |
|
5631 | 0 | if (rte->eref) |
5632 | 0 | ExecTypeSetColNames(output_tupdesc, rte->eref->colnames); |
5633 | 0 | } |
5634 | 0 | } |
5635 | | |
5636 | | /* Bless the tupdesc if needed, and save it in the execution state */ |
5637 | 0 | op->d.wholerow.tupdesc = BlessTupleDesc(output_tupdesc); |
5638 | |
|
5639 | 0 | op->d.wholerow.first = false; |
5640 | 0 | } |
5641 | | |
5642 | | /* |
5643 | | * Make sure all columns of the slot are accessible in the slot's |
5644 | | * Datum/isnull arrays. |
5645 | | */ |
5646 | 0 | slot_getallattrs(slot); |
5647 | |
|
5648 | 0 | if (op->d.wholerow.slow) |
5649 | 0 | { |
5650 | | /* Check to see if any dropped attributes are non-null */ |
5651 | 0 | TupleDesc tupleDesc = slot->tts_tupleDescriptor; |
5652 | 0 | TupleDesc var_tupdesc = op->d.wholerow.tupdesc; |
5653 | |
|
5654 | 0 | Assert(var_tupdesc->natts == tupleDesc->natts); |
5655 | |
|
5656 | 0 | for (int i = 0; i < var_tupdesc->natts; i++) |
5657 | 0 | { |
5658 | 0 | CompactAttribute *vattr = TupleDescCompactAttr(var_tupdesc, i); |
5659 | 0 | CompactAttribute *sattr = TupleDescCompactAttr(tupleDesc, i); |
5660 | |
|
5661 | 0 | if (!vattr->attisdropped) |
5662 | 0 | continue; /* already checked non-dropped cols */ |
5663 | 0 | if (slot->tts_isnull[i]) |
5664 | 0 | continue; /* null is always okay */ |
5665 | 0 | if (vattr->attlen != sattr->attlen || |
5666 | 0 | vattr->attalignby != sattr->attalignby) |
5667 | 0 | ereport(ERROR, |
5668 | 0 | (errcode(ERRCODE_DATATYPE_MISMATCH), |
5669 | 0 | errmsg("table row type and query-specified row type do not match"), |
5670 | 0 | errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.", |
5671 | 0 | i + 1))); |
5672 | 0 | } |
5673 | 0 | } |
5674 | | |
5675 | | /* |
5676 | | * Build a composite datum, making sure any toasted fields get detoasted. |
5677 | | * |
5678 | | * (Note: it is critical that we not change the slot's state here.) |
5679 | | */ |
5680 | 0 | tuple = toast_build_flattened_tuple(slot->tts_tupleDescriptor, |
5681 | 0 | slot->tts_values, |
5682 | 0 | slot->tts_isnull); |
5683 | 0 | dtuple = tuple->t_data; |
5684 | | |
5685 | | /* |
5686 | | * Label the datum with the composite type info we identified before. |
5687 | | * |
5688 | | * (Note: we could skip doing this by passing op->d.wholerow.tupdesc to |
5689 | | * the tuple build step; but that seems a tad risky so let's not.) |
5690 | | * |
5691 | | * XXX: What is the risk mentioned here? Not following... If the tuple |
5692 | | * descriptors disagree, we're royally screwed, regardless of which |
5693 | | * tupledesc is used for heap_tuple_set_datum()? |
5694 | | */ |
5695 | 0 | heap_tuple_set_datum(dtuple, op->d.wholerow.tupdesc, tuple->t_len); |
5696 | |
|
5697 | 0 | *op->resvalue = PointerGetDatum(dtuple); |
5698 | 0 | *op->resnull = false; |
5699 | 0 | } |
5700 | | |
5701 | | void |
5702 | | ExecEvalSysVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext, |
5703 | | TupleTableSlot *slot) |
5704 | 0 | { |
5705 | 0 | Datum d; |
5706 | | |
5707 | | /* OLD/NEW system attribute is NULL if OLD/NEW row is NULL */ |
5708 | 0 | if ((op->d.var.varreturningtype == VAR_RETURNING_OLD && |
5709 | 0 | state->flags & EEO_FLAG_OLD_IS_NULL) || |
5710 | 0 | (op->d.var.varreturningtype == VAR_RETURNING_NEW && |
5711 | 0 | state->flags & EEO_FLAG_NEW_IS_NULL)) |
5712 | 0 | { |
5713 | 0 | *op->resvalue = (Datum) 0; |
5714 | 0 | *op->resnull = true; |
5715 | 0 | return; |
5716 | 0 | } |
5717 | | |
5718 | | /* slot_getsysattr has sufficient defenses against bad attnums */ |
5719 | 0 | d = slot_getsysattr(slot, |
5720 | 0 | op->d.var.attnum, |
5721 | 0 | op->resnull); |
5722 | 0 | *op->resvalue = d; |
5723 | | /* this ought to be unreachable, but it's cheap enough to check */ |
5724 | 0 | if (unlikely(*op->resnull)) |
5725 | 0 | elog(ERROR, "failed to fetch attribute from slot"); |
5726 | 0 | } |
5727 | | |
5728 | | /* |
5729 | | * Transition value has not been initialized. This is the first non-NULL input |
5730 | | * value for a group. We use it as the initial value for transValue. |
5731 | | */ |
5732 | | void |
5733 | | ExecAggInitGroup(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup, |
5734 | | ExprContext *aggcontext) |
5735 | 0 | { |
5736 | 0 | FunctionCallInfo fcinfo = pertrans->transfn_fcinfo; |
5737 | 0 | MemoryContext oldContext; |
5738 | | |
5739 | | /* |
5740 | | * We must copy the datum into aggcontext if it is pass-by-ref. We do not |
5741 | | * need to pfree the old transValue, since it's NULL. (We already checked |
5742 | | * that the agg's input type is binary-compatible with its transtype, so |
5743 | | * straight copy here is OK.) |
5744 | | */ |
5745 | 0 | oldContext = MemoryContextSwitchTo(aggcontext->ecxt_per_tuple_memory); |
5746 | 0 | pergroup->transValue = datumCopy(fcinfo->args[1].value, |
5747 | 0 | pertrans->transtypeByVal, |
5748 | 0 | pertrans->transtypeLen); |
5749 | 0 | pergroup->transValueIsNull = false; |
5750 | 0 | pergroup->noTransValue = false; |
5751 | 0 | MemoryContextSwitchTo(oldContext); |
5752 | 0 | } |
5753 | | |
5754 | | /* |
5755 | | * Ensure that the new transition value is stored in the aggcontext, |
5756 | | * rather than the per-tuple context. This should be invoked only when |
5757 | | * we know (a) the transition data type is pass-by-reference, and (b) |
5758 | | * the newValue is distinct from the oldValue. |
5759 | | * |
5760 | | * NB: This can change the current memory context. |
5761 | | * |
5762 | | * We copy the presented newValue into the aggcontext, except when the datum |
5763 | | * points to a R/W expanded object that is already a child of the aggcontext, |
5764 | | * in which case we need not copy. We then delete the oldValue, if not null. |
5765 | | * |
5766 | | * If the presented datum points to a R/W expanded object that is a child of |
5767 | | * some other context, ideally we would just reparent it under the aggcontext. |
5768 | | * Unfortunately, that doesn't work easily, and it wouldn't help anyway for |
5769 | | * aggregate-aware transfns. We expect that a transfn that deals in expanded |
5770 | | * objects and is aware of the memory management conventions for aggregate |
5771 | | * transition values will (1) on first call, return a R/W expanded object that |
5772 | | * is already in the right context, allowing us to do nothing here, and (2) on |
5773 | | * subsequent calls, modify and return that same object, so that control |
5774 | | * doesn't even reach here. However, if we have a generic transfn that |
5775 | | * returns a new R/W expanded object (probably in the per-tuple context), |
5776 | | * reparenting that result would cause problems. We'd pass that R/W object to |
5777 | | * the next invocation of the transfn, and then it would be at liberty to |
5778 | | * change or delete that object, and if it deletes it then our own attempt to |
5779 | | * delete the now-old transvalue afterwards would be a double free. We avoid |
5780 | | * this problem by forcing the stored transvalue to always be a flat |
5781 | | * non-expanded object unless the transfn is visibly doing aggregate-aware |
5782 | | * memory management. This is somewhat inefficient, but the best answer to |
5783 | | * that is to write a smarter transfn. |
5784 | | */ |
5785 | | Datum |
5786 | | ExecAggCopyTransValue(AggState *aggstate, AggStatePerTrans pertrans, |
5787 | | Datum newValue, bool newValueIsNull, |
5788 | | Datum oldValue, bool oldValueIsNull) |
5789 | 0 | { |
5790 | 0 | Assert(newValue != oldValue); |
5791 | |
|
5792 | 0 | if (!newValueIsNull) |
5793 | 0 | { |
5794 | 0 | MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory); |
5795 | 0 | if (DatumIsReadWriteExpandedObject(newValue, |
5796 | 0 | false, |
5797 | 0 | pertrans->transtypeLen) && |
5798 | 0 | MemoryContextGetParent(DatumGetEOHP(newValue)->eoh_context) == CurrentMemoryContext) |
5799 | 0 | /* do nothing */ ; |
5800 | 0 | else |
5801 | 0 | newValue = datumCopy(newValue, |
5802 | 0 | pertrans->transtypeByVal, |
5803 | 0 | pertrans->transtypeLen); |
5804 | 0 | } |
5805 | 0 | else |
5806 | 0 | { |
5807 | | /* |
5808 | | * Ensure that AggStatePerGroup->transValue ends up being 0, so |
5809 | | * callers can safely compare newValue/oldValue without having to |
5810 | | * check their respective nullness. |
5811 | | */ |
5812 | 0 | newValue = (Datum) 0; |
5813 | 0 | } |
5814 | |
|
5815 | 0 | if (!oldValueIsNull) |
5816 | 0 | { |
5817 | 0 | if (DatumIsReadWriteExpandedObject(oldValue, |
5818 | 0 | false, |
5819 | 0 | pertrans->transtypeLen)) |
5820 | 0 | DeleteExpandedObject(oldValue); |
5821 | 0 | else |
5822 | 0 | pfree(DatumGetPointer(oldValue)); |
5823 | 0 | } |
5824 | |
|
5825 | 0 | return newValue; |
5826 | 0 | } |
5827 | | |
5828 | | /* |
5829 | | * ExecEvalPreOrderedDistinctSingle |
5830 | | * Returns true when the aggregate transition value Datum is distinct |
5831 | | * from the previous input Datum and returns false when the input Datum |
5832 | | * matches the previous input Datum. |
5833 | | */ |
5834 | | bool |
5835 | | ExecEvalPreOrderedDistinctSingle(AggState *aggstate, AggStatePerTrans pertrans) |
5836 | 0 | { |
5837 | 0 | Datum value = pertrans->transfn_fcinfo->args[1].value; |
5838 | 0 | bool isnull = pertrans->transfn_fcinfo->args[1].isnull; |
5839 | |
|
5840 | 0 | if (!pertrans->haslast || |
5841 | 0 | pertrans->lastisnull != isnull || |
5842 | 0 | (!isnull && !DatumGetBool(FunctionCall2Coll(&pertrans->equalfnOne, |
5843 | 0 | pertrans->aggCollation, |
5844 | 0 | pertrans->lastdatum, value)))) |
5845 | 0 | { |
5846 | 0 | if (pertrans->haslast && !pertrans->inputtypeByVal && |
5847 | 0 | !pertrans->lastisnull) |
5848 | 0 | pfree(DatumGetPointer(pertrans->lastdatum)); |
5849 | |
|
5850 | 0 | pertrans->haslast = true; |
5851 | 0 | if (!isnull) |
5852 | 0 | { |
5853 | 0 | MemoryContext oldContext; |
5854 | |
|
5855 | 0 | oldContext = MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory); |
5856 | |
|
5857 | 0 | pertrans->lastdatum = datumCopy(value, pertrans->inputtypeByVal, |
5858 | 0 | pertrans->inputtypeLen); |
5859 | |
|
5860 | 0 | MemoryContextSwitchTo(oldContext); |
5861 | 0 | } |
5862 | 0 | else |
5863 | 0 | pertrans->lastdatum = (Datum) 0; |
5864 | 0 | pertrans->lastisnull = isnull; |
5865 | 0 | return true; |
5866 | 0 | } |
5867 | | |
5868 | 0 | return false; |
5869 | 0 | } |
5870 | | |
5871 | | /* |
5872 | | * ExecEvalPreOrderedDistinctMulti |
5873 | | * Returns true when the aggregate input is distinct from the previous |
5874 | | * input and returns false when the input matches the previous input, or |
5875 | | * when there was no previous input. |
5876 | | */ |
5877 | | bool |
5878 | | ExecEvalPreOrderedDistinctMulti(AggState *aggstate, AggStatePerTrans pertrans) |
5879 | 0 | { |
5880 | 0 | ExprContext *tmpcontext = aggstate->tmpcontext; |
5881 | 0 | bool isdistinct = false; /* for now */ |
5882 | 0 | TupleTableSlot *save_outer; |
5883 | 0 | TupleTableSlot *save_inner; |
5884 | |
|
5885 | 0 | for (int i = 0; i < pertrans->numTransInputs; i++) |
5886 | 0 | { |
5887 | 0 | pertrans->sortslot->tts_values[i] = pertrans->transfn_fcinfo->args[i + 1].value; |
5888 | 0 | pertrans->sortslot->tts_isnull[i] = pertrans->transfn_fcinfo->args[i + 1].isnull; |
5889 | 0 | } |
5890 | |
|
5891 | 0 | ExecClearTuple(pertrans->sortslot); |
5892 | 0 | pertrans->sortslot->tts_nvalid = pertrans->numInputs; |
5893 | 0 | ExecStoreVirtualTuple(pertrans->sortslot); |
5894 | | |
5895 | | /* save the previous slots before we overwrite them */ |
5896 | 0 | save_outer = tmpcontext->ecxt_outertuple; |
5897 | 0 | save_inner = tmpcontext->ecxt_innertuple; |
5898 | |
|
5899 | 0 | tmpcontext->ecxt_outertuple = pertrans->sortslot; |
5900 | 0 | tmpcontext->ecxt_innertuple = pertrans->uniqslot; |
5901 | |
|
5902 | 0 | if (!pertrans->haslast || |
5903 | 0 | !ExecQual(pertrans->equalfnMulti, tmpcontext)) |
5904 | 0 | { |
5905 | 0 | if (pertrans->haslast) |
5906 | 0 | ExecClearTuple(pertrans->uniqslot); |
5907 | |
|
5908 | 0 | pertrans->haslast = true; |
5909 | 0 | ExecCopySlot(pertrans->uniqslot, pertrans->sortslot); |
5910 | |
|
5911 | 0 | isdistinct = true; |
5912 | 0 | } |
5913 | | |
5914 | | /* restore the original slots */ |
5915 | 0 | tmpcontext->ecxt_outertuple = save_outer; |
5916 | 0 | tmpcontext->ecxt_innertuple = save_inner; |
5917 | |
|
5918 | 0 | return isdistinct; |
5919 | 0 | } |
5920 | | |
5921 | | /* |
5922 | | * Invoke ordered transition function, with a datum argument. |
5923 | | */ |
5924 | | void |
5925 | | ExecEvalAggOrderedTransDatum(ExprState *state, ExprEvalStep *op, |
5926 | | ExprContext *econtext) |
5927 | 0 | { |
5928 | 0 | AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
5929 | 0 | int setno = op->d.agg_trans.setno; |
5930 | |
|
5931 | 0 | tuplesort_putdatum(pertrans->sortstates[setno], |
5932 | 0 | *op->resvalue, *op->resnull); |
5933 | 0 | } |
5934 | | |
5935 | | /* |
5936 | | * Invoke ordered transition function, with a tuple argument. |
5937 | | */ |
5938 | | void |
5939 | | ExecEvalAggOrderedTransTuple(ExprState *state, ExprEvalStep *op, |
5940 | | ExprContext *econtext) |
5941 | 0 | { |
5942 | 0 | AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
5943 | 0 | int setno = op->d.agg_trans.setno; |
5944 | |
|
5945 | 0 | ExecClearTuple(pertrans->sortslot); |
5946 | 0 | pertrans->sortslot->tts_nvalid = pertrans->numInputs; |
5947 | 0 | ExecStoreVirtualTuple(pertrans->sortslot); |
5948 | 0 | tuplesort_puttupleslot(pertrans->sortstates[setno], pertrans->sortslot); |
5949 | 0 | } |
5950 | | |
5951 | | /* implementation of transition function invocation for byval types */ |
5952 | | static pg_always_inline void |
5953 | | ExecAggPlainTransByVal(AggState *aggstate, AggStatePerTrans pertrans, |
5954 | | AggStatePerGroup pergroup, |
5955 | | ExprContext *aggcontext, int setno) |
5956 | 0 | { |
5957 | 0 | FunctionCallInfo fcinfo = pertrans->transfn_fcinfo; |
5958 | 0 | MemoryContext oldContext; |
5959 | 0 | Datum newVal; |
5960 | | |
5961 | | /* cf. select_current_set() */ |
5962 | 0 | aggstate->curaggcontext = aggcontext; |
5963 | 0 | aggstate->current_set = setno; |
5964 | | |
5965 | | /* set up aggstate->curpertrans for AggGetAggref() */ |
5966 | 0 | aggstate->curpertrans = pertrans; |
5967 | | |
5968 | | /* invoke transition function in per-tuple context */ |
5969 | 0 | oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory); |
5970 | |
|
5971 | 0 | fcinfo->args[0].value = pergroup->transValue; |
5972 | 0 | fcinfo->args[0].isnull = pergroup->transValueIsNull; |
5973 | 0 | fcinfo->isnull = false; /* just in case transfn doesn't set it */ |
5974 | |
|
5975 | 0 | newVal = FunctionCallInvoke(fcinfo); |
5976 | |
|
5977 | 0 | pergroup->transValue = newVal; |
5978 | 0 | pergroup->transValueIsNull = fcinfo->isnull; |
5979 | |
|
5980 | 0 | MemoryContextSwitchTo(oldContext); |
5981 | 0 | } |
5982 | | |
5983 | | /* implementation of transition function invocation for byref types */ |
5984 | | static pg_always_inline void |
5985 | | ExecAggPlainTransByRef(AggState *aggstate, AggStatePerTrans pertrans, |
5986 | | AggStatePerGroup pergroup, |
5987 | | ExprContext *aggcontext, int setno) |
5988 | 0 | { |
5989 | 0 | FunctionCallInfo fcinfo = pertrans->transfn_fcinfo; |
5990 | 0 | MemoryContext oldContext; |
5991 | 0 | Datum newVal; |
5992 | | |
5993 | | /* cf. select_current_set() */ |
5994 | 0 | aggstate->curaggcontext = aggcontext; |
5995 | 0 | aggstate->current_set = setno; |
5996 | | |
5997 | | /* set up aggstate->curpertrans for AggGetAggref() */ |
5998 | 0 | aggstate->curpertrans = pertrans; |
5999 | | |
6000 | | /* invoke transition function in per-tuple context */ |
6001 | 0 | oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory); |
6002 | |
|
6003 | 0 | fcinfo->args[0].value = pergroup->transValue; |
6004 | 0 | fcinfo->args[0].isnull = pergroup->transValueIsNull; |
6005 | 0 | fcinfo->isnull = false; /* just in case transfn doesn't set it */ |
6006 | |
|
6007 | 0 | newVal = FunctionCallInvoke(fcinfo); |
6008 | | |
6009 | | /* |
6010 | | * For pass-by-ref datatype, must copy the new value into aggcontext and |
6011 | | * free the prior transValue. But if transfn returned a pointer to its |
6012 | | * first input, we don't need to do anything. |
6013 | | * |
6014 | | * It's safe to compare newVal with pergroup->transValue without regard |
6015 | | * for either being NULL, because ExecAggCopyTransValue takes care to set |
6016 | | * transValue to 0 when NULL. Otherwise we could end up accidentally not |
6017 | | * reparenting, when the transValue has the same numerical value as |
6018 | | * newValue, despite being NULL. This is a somewhat hot path, making it |
6019 | | * undesirable to instead solve this with another branch for the common |
6020 | | * case of the transition function returning its (modified) input |
6021 | | * argument. |
6022 | | */ |
6023 | 0 | if (DatumGetPointer(newVal) != DatumGetPointer(pergroup->transValue)) |
6024 | 0 | newVal = ExecAggCopyTransValue(aggstate, pertrans, |
6025 | 0 | newVal, fcinfo->isnull, |
6026 | 0 | pergroup->transValue, |
6027 | 0 | pergroup->transValueIsNull); |
6028 | |
|
6029 | 0 | pergroup->transValue = newVal; |
6030 | 0 | pergroup->transValueIsNull = fcinfo->isnull; |
6031 | |
|
6032 | 0 | MemoryContextSwitchTo(oldContext); |
6033 | 0 | } |