Branch data Line data Source code
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 : : #define EEO_CASE(name) CASE_##name:
121 : : #define EEO_DISPATCH() goto *((void *) op->opcode)
122 : : #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 : : do { \
135 : : op++; \
136 : : EEO_DISPATCH(); \
137 : : } while (0)
138 : :
139 : : #define EEO_JUMP(stepno) \
140 : : do { \
141 : : op = &state->steps[stepno]; \
142 : : EEO_DISPATCH(); \
143 : : } 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 : : #define SH_ELEMENT_TYPE ScalarArrayOpExprHashEntry
238 : : #define SH_KEY_TYPE Datum
239 : : #define SH_KEY key
240 : : #define SH_HASH_KEY(tb, key) saop_element_hash(tb, key)
241 : : #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 : : #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 : 1631875 : ExecReadyInterpretedExpr(ExprState *state)
253 : : {
254 : : /* Ensure one-time interpreter setup has been done */
255 : 1631875 : ExecInitInterpreter();
256 : :
257 : : /* Simple validity checks on expression */
258 : : Assert(state->steps_len >= 1);
259 : : Assert(state->steps[state->steps_len - 1].opcode == EEOP_DONE_RETURN ||
260 : : 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 [ - + ]: 1631875 : 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 : 1631875 : state->evalfunc = ExecInterpExprStillValid;
277 : :
278 : : /* DIRECT_THREADED should not already be set */
279 : : 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 : 1631875 : 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 [ + + ]: 1631875 : if (state->steps_len == 5)
294 : : {
295 : 212722 : ExprEvalOp step0 = state->steps[0].opcode;
296 : 212722 : ExprEvalOp step1 = state->steps[1].opcode;
297 : 212722 : ExprEvalOp step2 = state->steps[2].opcode;
298 : 212722 : ExprEvalOp step3 = state->steps[3].opcode;
299 : :
300 [ + + + + ]: 212722 : if (step0 == EEOP_INNER_FETCHSOME &&
301 [ + - ]: 538 : step1 == EEOP_HASHDATUM_SET_INITVAL &&
302 [ + - ]: 538 : step2 == EEOP_INNER_VAR &&
303 : : step3 == EEOP_HASHDATUM_NEXT32)
304 : : {
305 : 538 : state->evalfunc_private = (void *) ExecJustHashInnerVarWithIV;
306 : 538 : return;
307 : : }
308 : : }
309 [ + + ]: 1419153 : else if (state->steps_len == 4)
310 : : {
311 : 95296 : ExprEvalOp step0 = state->steps[0].opcode;
312 : 95296 : ExprEvalOp step1 = state->steps[1].opcode;
313 : 95296 : ExprEvalOp step2 = state->steps[2].opcode;
314 : :
315 [ + + + + ]: 95296 : if (step0 == EEOP_OUTER_FETCHSOME &&
316 [ - + ]: 23241 : step1 == EEOP_OUTER_VAR &&
317 : : step2 == EEOP_HASHDATUM_FIRST)
318 : : {
319 : 0 : state->evalfunc_private = (void *) ExecJustHashOuterVar;
320 : 0 : return;
321 : : }
322 [ + + + + ]: 95296 : else if (step0 == EEOP_INNER_FETCHSOME &&
323 [ + + ]: 2382 : step1 == EEOP_INNER_VAR &&
324 : : step2 == EEOP_HASHDATUM_FIRST)
325 : : {
326 : 2354 : state->evalfunc_private = (void *) ExecJustHashInnerVar;
327 : 2354 : return;
328 : : }
329 [ + + + + ]: 92942 : else if (step0 == EEOP_OUTER_FETCHSOME &&
330 [ + + ]: 23241 : step1 == EEOP_OUTER_VAR &&
331 : : step2 == EEOP_HASHDATUM_FIRST_STRICT)
332 : : {
333 : 20601 : state->evalfunc_private = (void *) ExecJustHashOuterVarStrict;
334 : 20601 : return;
335 : : }
336 : : }
337 [ + + ]: 1323857 : else if (state->steps_len == 3)
338 : : {
339 : 282413 : ExprEvalOp step0 = state->steps[0].opcode;
340 : 282413 : ExprEvalOp step1 = state->steps[1].opcode;
341 : :
342 [ + + + + ]: 282413 : if (step0 == EEOP_INNER_FETCHSOME &&
343 : : step1 == EEOP_INNER_VAR)
344 : : {
345 : 5231 : state->evalfunc_private = ExecJustInnerVar;
346 : 5231 : return;
347 : : }
348 [ + + + + ]: 277182 : else if (step0 == EEOP_OUTER_FETCHSOME &&
349 : : step1 == EEOP_OUTER_VAR)
350 : : {
351 : 7067 : state->evalfunc_private = ExecJustOuterVar;
352 : 7067 : return;
353 : : }
354 [ + + + + ]: 270115 : else if (step0 == EEOP_SCAN_FETCHSOME &&
355 : : step1 == EEOP_SCAN_VAR)
356 : : {
357 : 18 : state->evalfunc_private = ExecJustScanVar;
358 : 18 : return;
359 : : }
360 [ + + + - ]: 270097 : else if (step0 == EEOP_INNER_FETCHSOME &&
361 : : step1 == EEOP_ASSIGN_INNER_VAR)
362 : : {
363 : 5564 : state->evalfunc_private = ExecJustAssignInnerVar;
364 : 5564 : return;
365 : : }
366 [ + + + - ]: 264533 : else if (step0 == EEOP_OUTER_FETCHSOME &&
367 : : step1 == EEOP_ASSIGN_OUTER_VAR)
368 : : {
369 : 10878 : state->evalfunc_private = ExecJustAssignOuterVar;
370 : 10878 : return;
371 : : }
372 [ + + + - ]: 253655 : else if (step0 == EEOP_SCAN_FETCHSOME &&
373 : : step1 == EEOP_ASSIGN_SCAN_VAR)
374 : : {
375 : 33303 : state->evalfunc_private = ExecJustAssignScanVar;
376 : 33303 : return;
377 : : }
378 [ + + + + ]: 220352 : else if (step0 == EEOP_CASE_TESTVAL &&
379 [ + + ]: 268 : (step1 == EEOP_FUNCEXPR_STRICT ||
380 [ - + ]: 40 : step1 == EEOP_FUNCEXPR_STRICT_1 ||
381 : : step1 == EEOP_FUNCEXPR_STRICT_2))
382 : : {
383 : 295 : state->evalfunc_private = ExecJustApplyFuncToCase;
384 : 295 : return;
385 : : }
386 [ + + + + ]: 220057 : else if (step0 == EEOP_INNER_VAR &&
387 : : step1 == EEOP_HASHDATUM_FIRST)
388 : : {
389 : 1553 : state->evalfunc_private = (void *) ExecJustHashInnerVarVirt;
390 : 1553 : return;
391 : : }
392 [ + + - + ]: 218504 : else if (step0 == EEOP_OUTER_VAR &&
393 : : step1 == EEOP_HASHDATUM_FIRST)
394 : : {
395 : 0 : state->evalfunc_private = (void *) ExecJustHashOuterVarVirt;
396 : 0 : return;
397 : : }
398 : : }
399 [ + + ]: 1041444 : else if (state->steps_len == 2)
400 : : {
401 : 591623 : ExprEvalOp step0 = state->steps[0].opcode;
402 : :
403 [ + + ]: 591623 : if (step0 == EEOP_CONST)
404 : : {
405 : 257138 : state->evalfunc_private = ExecJustConst;
406 : 257138 : return;
407 : : }
408 [ + + ]: 334485 : else if (step0 == EEOP_INNER_VAR)
409 : : {
410 : 219 : state->evalfunc_private = ExecJustInnerVarVirt;
411 : 219 : return;
412 : : }
413 [ + + ]: 334266 : else if (step0 == EEOP_OUTER_VAR)
414 : : {
415 : 2001 : state->evalfunc_private = ExecJustOuterVarVirt;
416 : 2001 : return;
417 : : }
418 [ - + ]: 332265 : else if (step0 == EEOP_SCAN_VAR)
419 : : {
420 : 0 : state->evalfunc_private = ExecJustScanVarVirt;
421 : 0 : return;
422 : : }
423 [ + + ]: 332265 : else if (step0 == EEOP_ASSIGN_INNER_VAR)
424 : : {
425 : 342 : state->evalfunc_private = ExecJustAssignInnerVarVirt;
426 : 342 : return;
427 : : }
428 [ + + ]: 331923 : else if (step0 == EEOP_ASSIGN_OUTER_VAR)
429 : : {
430 : 3280 : state->evalfunc_private = ExecJustAssignOuterVarVirt;
431 : 3280 : return;
432 : : }
433 [ + + ]: 328643 : else if (step0 == EEOP_ASSIGN_SCAN_VAR)
434 : : {
435 : 2344 : state->evalfunc_private = ExecJustAssignScanVarVirt;
436 : 2344 : return;
437 : : }
438 : : }
439 : :
440 : : #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 [ + + ]: 8606334 : for (int off = 0; off < state->steps_len; off++)
447 : : {
448 : 7327185 : ExprEvalStep *op = &state->steps[off];
449 : :
450 : 7327185 : op->opcode = EEO_OPCODE(op->opcode);
451 : : }
452 : :
453 : 1279149 : state->flags |= EEO_FLAG_DIRECT_THREADED;
454 : : #endif /* EEO_USE_COMPUTED_GOTO */
455 : :
456 : 1279149 : state->evalfunc_private = ExecInterpExpr;
457 : : }
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 : 134458466 : ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
471 : : {
472 : : ExprEvalStep *op;
473 : : TupleTableSlot *resultslot;
474 : : TupleTableSlot *innerslot;
475 : : TupleTableSlot *outerslot;
476 : : TupleTableSlot *scanslot;
477 : : TupleTableSlot *oldslot;
478 : : TupleTableSlot *newslot;
479 : :
480 : : /*
481 : : * This array has to be in the same order as enum ExprEvalOp.
482 : : */
483 : : #if defined(EEO_USE_COMPUTED_GOTO)
484 : : static const void *const dispatch_table[] = {
485 : : &&CASE_EEOP_DONE_RETURN,
486 : : &&CASE_EEOP_DONE_NO_RETURN,
487 : : &&CASE_EEOP_INNER_FETCHSOME,
488 : : &&CASE_EEOP_OUTER_FETCHSOME,
489 : : &&CASE_EEOP_SCAN_FETCHSOME,
490 : : &&CASE_EEOP_OLD_FETCHSOME,
491 : : &&CASE_EEOP_NEW_FETCHSOME,
492 : : &&CASE_EEOP_INNER_VAR,
493 : : &&CASE_EEOP_OUTER_VAR,
494 : : &&CASE_EEOP_SCAN_VAR,
495 : : &&CASE_EEOP_OLD_VAR,
496 : : &&CASE_EEOP_NEW_VAR,
497 : : &&CASE_EEOP_INNER_SYSVAR,
498 : : &&CASE_EEOP_OUTER_SYSVAR,
499 : : &&CASE_EEOP_SCAN_SYSVAR,
500 : : &&CASE_EEOP_OLD_SYSVAR,
501 : : &&CASE_EEOP_NEW_SYSVAR,
502 : : &&CASE_EEOP_WHOLEROW,
503 : : &&CASE_EEOP_ASSIGN_INNER_VAR,
504 : : &&CASE_EEOP_ASSIGN_OUTER_VAR,
505 : : &&CASE_EEOP_ASSIGN_SCAN_VAR,
506 : : &&CASE_EEOP_ASSIGN_OLD_VAR,
507 : : &&CASE_EEOP_ASSIGN_NEW_VAR,
508 : : &&CASE_EEOP_ASSIGN_TMP,
509 : : &&CASE_EEOP_ASSIGN_TMP_MAKE_RO,
510 : : &&CASE_EEOP_CONST,
511 : : &&CASE_EEOP_FUNCEXPR,
512 : : &&CASE_EEOP_FUNCEXPR_STRICT,
513 : : &&CASE_EEOP_FUNCEXPR_STRICT_1,
514 : : &&CASE_EEOP_FUNCEXPR_STRICT_2,
515 : : &&CASE_EEOP_FUNCEXPR_FUSAGE,
516 : : &&CASE_EEOP_FUNCEXPR_STRICT_FUSAGE,
517 : : &&CASE_EEOP_BOOL_AND_STEP_FIRST,
518 : : &&CASE_EEOP_BOOL_AND_STEP,
519 : : &&CASE_EEOP_BOOL_AND_STEP_LAST,
520 : : &&CASE_EEOP_BOOL_OR_STEP_FIRST,
521 : : &&CASE_EEOP_BOOL_OR_STEP,
522 : : &&CASE_EEOP_BOOL_OR_STEP_LAST,
523 : : &&CASE_EEOP_BOOL_NOT_STEP,
524 : : &&CASE_EEOP_QUAL,
525 : : &&CASE_EEOP_JUMP,
526 : : &&CASE_EEOP_JUMP_IF_NULL,
527 : : &&CASE_EEOP_JUMP_IF_NOT_NULL,
528 : : &&CASE_EEOP_JUMP_IF_NOT_TRUE,
529 : : &&CASE_EEOP_NULLTEST_ISNULL,
530 : : &&CASE_EEOP_NULLTEST_ISNOTNULL,
531 : : &&CASE_EEOP_NULLTEST_ROWISNULL,
532 : : &&CASE_EEOP_NULLTEST_ROWISNOTNULL,
533 : : &&CASE_EEOP_BOOLTEST_IS_TRUE,
534 : : &&CASE_EEOP_BOOLTEST_IS_NOT_TRUE,
535 : : &&CASE_EEOP_BOOLTEST_IS_FALSE,
536 : : &&CASE_EEOP_BOOLTEST_IS_NOT_FALSE,
537 : : &&CASE_EEOP_PARAM_EXEC,
538 : : &&CASE_EEOP_PARAM_EXTERN,
539 : : &&CASE_EEOP_PARAM_CALLBACK,
540 : : &&CASE_EEOP_PARAM_SET,
541 : : &&CASE_EEOP_CASE_TESTVAL,
542 : : &&CASE_EEOP_CASE_TESTVAL_EXT,
543 : : &&CASE_EEOP_MAKE_READONLY,
544 : : &&CASE_EEOP_IOCOERCE,
545 : : &&CASE_EEOP_IOCOERCE_SAFE,
546 : : &&CASE_EEOP_DISTINCT,
547 : : &&CASE_EEOP_NOT_DISTINCT,
548 : : &&CASE_EEOP_NULLIF,
549 : : &&CASE_EEOP_SQLVALUEFUNCTION,
550 : : &&CASE_EEOP_CURRENTOFEXPR,
551 : : &&CASE_EEOP_NEXTVALUEEXPR,
552 : : &&CASE_EEOP_RETURNINGEXPR,
553 : : &&CASE_EEOP_ARRAYEXPR,
554 : : &&CASE_EEOP_ARRAYCOERCE,
555 : : &&CASE_EEOP_ROW,
556 : : &&CASE_EEOP_ROWCOMPARE_STEP,
557 : : &&CASE_EEOP_ROWCOMPARE_FINAL,
558 : : &&CASE_EEOP_MINMAX,
559 : : &&CASE_EEOP_FIELDSELECT,
560 : : &&CASE_EEOP_FIELDSTORE_DEFORM,
561 : : &&CASE_EEOP_FIELDSTORE_FORM,
562 : : &&CASE_EEOP_SBSREF_SUBSCRIPTS,
563 : : &&CASE_EEOP_SBSREF_OLD,
564 : : &&CASE_EEOP_SBSREF_ASSIGN,
565 : : &&CASE_EEOP_SBSREF_FETCH,
566 : : &&CASE_EEOP_DOMAIN_TESTVAL,
567 : : &&CASE_EEOP_DOMAIN_TESTVAL_EXT,
568 : : &&CASE_EEOP_DOMAIN_NOTNULL,
569 : : &&CASE_EEOP_DOMAIN_CHECK,
570 : : &&CASE_EEOP_HASHDATUM_SET_INITVAL,
571 : : &&CASE_EEOP_HASHDATUM_FIRST,
572 : : &&CASE_EEOP_HASHDATUM_FIRST_STRICT,
573 : : &&CASE_EEOP_HASHDATUM_NEXT32,
574 : : &&CASE_EEOP_HASHDATUM_NEXT32_STRICT,
575 : : &&CASE_EEOP_CONVERT_ROWTYPE,
576 : : &&CASE_EEOP_SCALARARRAYOP,
577 : : &&CASE_EEOP_HASHED_SCALARARRAYOP,
578 : : &&CASE_EEOP_XMLEXPR,
579 : : &&CASE_EEOP_JSON_CONSTRUCTOR,
580 : : &&CASE_EEOP_IS_JSON,
581 : : &&CASE_EEOP_JSONEXPR_PATH,
582 : : &&CASE_EEOP_JSONEXPR_COERCION,
583 : : &&CASE_EEOP_JSONEXPR_COERCION_FINISH,
584 : : &&CASE_EEOP_AGGREF,
585 : : &&CASE_EEOP_GROUPING_FUNC,
586 : : &&CASE_EEOP_WINDOW_FUNC,
587 : : &&CASE_EEOP_MERGE_SUPPORT_FUNC,
588 : : &&CASE_EEOP_SUBPLAN,
589 : : &&CASE_EEOP_AGG_STRICT_DESERIALIZE,
590 : : &&CASE_EEOP_AGG_DESERIALIZE,
591 : : &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS,
592 : : &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1,
593 : : &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_NULLS,
594 : : &&CASE_EEOP_AGG_PLAIN_PERGROUP_NULLCHECK,
595 : : &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL,
596 : : &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL,
597 : : &&CASE_EEOP_AGG_PLAIN_TRANS_BYVAL,
598 : : &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF,
599 : : &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYREF,
600 : : &&CASE_EEOP_AGG_PLAIN_TRANS_BYREF,
601 : : &&CASE_EEOP_AGG_PRESORTED_DISTINCT_SINGLE,
602 : : &&CASE_EEOP_AGG_PRESORTED_DISTINCT_MULTI,
603 : : &&CASE_EEOP_AGG_ORDERED_TRANS_DATUM,
604 : : &&CASE_EEOP_AGG_ORDERED_TRANS_TUPLE,
605 : : &&CASE_EEOP_LAST
606 : : };
607 : :
608 : : StaticAssertDecl(lengthof(dispatch_table) == EEOP_LAST + 1,
609 : : "dispatch_table out of whack with ExprEvalOp");
610 : :
611 [ + + ]: 134458466 : if (unlikely(state == NULL))
612 : 14303 : return PointerGetDatum(dispatch_table);
613 : : #else
614 : : Assert(state != NULL);
615 : : #endif /* EEO_USE_COMPUTED_GOTO */
616 : :
617 : : /* setup state */
618 : 134444163 : op = state->steps;
619 : 134444163 : resultslot = state->resultslot;
620 : 134444163 : innerslot = econtext->ecxt_innertuple;
621 : 134444163 : outerslot = econtext->ecxt_outertuple;
622 : 134444163 : scanslot = econtext->ecxt_scantuple;
623 : 134444163 : oldslot = econtext->ecxt_oldtuple;
624 : 134444163 : newslot = econtext->ecxt_newtuple;
625 : :
626 : : #if defined(EEO_USE_COMPUTED_GOTO)
627 : 134444163 : EEO_DISPATCH();
628 : : #endif
629 : :
630 : : EEO_SWITCH()
631 : : {
632 : 75107591 : EEO_CASE(EEOP_DONE_RETURN)
633 : : {
634 : 75107591 : *isnull = state->resnull;
635 : 75107591 : return state->resvalue;
636 : : }
637 : :
638 : 59323855 : EEO_CASE(EEOP_DONE_NO_RETURN)
639 : : {
640 : : Assert(isnull == NULL);
641 : 59323855 : return (Datum) 0;
642 : : }
643 : :
644 : 28882418 : EEO_CASE(EEOP_INNER_FETCHSOME)
645 : : {
646 : 28882418 : CheckOpSlotCompatibility(op, innerslot);
647 : :
648 : 28882418 : slot_getsomeattrs(innerslot, op->d.fetch.last_var);
649 : :
650 : 28882418 : EEO_NEXT();
651 : : }
652 : :
653 : 25719956 : EEO_CASE(EEOP_OUTER_FETCHSOME)
654 : : {
655 : 25719956 : CheckOpSlotCompatibility(op, outerslot);
656 : :
657 : 25719956 : slot_getsomeattrs(outerslot, op->d.fetch.last_var);
658 : :
659 : 25719956 : EEO_NEXT();
660 : : }
661 : :
662 : 53549135 : EEO_CASE(EEOP_SCAN_FETCHSOME)
663 : : {
664 : 53549135 : CheckOpSlotCompatibility(op, scanslot);
665 : :
666 : 53549135 : slot_getsomeattrs(scanslot, op->d.fetch.last_var);
667 : :
668 : 53549135 : EEO_NEXT();
669 : : }
670 : :
671 : 337 : EEO_CASE(EEOP_OLD_FETCHSOME)
672 : : {
673 : 337 : CheckOpSlotCompatibility(op, oldslot);
674 : :
675 : 337 : slot_getsomeattrs(oldslot, op->d.fetch.last_var);
676 : :
677 : 337 : EEO_NEXT();
678 : : }
679 : :
680 : 331 : EEO_CASE(EEOP_NEW_FETCHSOME)
681 : : {
682 : 331 : CheckOpSlotCompatibility(op, newslot);
683 : :
684 : 331 : slot_getsomeattrs(newslot, op->d.fetch.last_var);
685 : :
686 : 331 : EEO_NEXT();
687 : : }
688 : :
689 : 31787073 : EEO_CASE(EEOP_INNER_VAR)
690 : : {
691 : 31787073 : 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 : : Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
700 : 31787073 : *op->resvalue = innerslot->tts_values[attnum];
701 : 31787073 : *op->resnull = innerslot->tts_isnull[attnum];
702 : :
703 : 31787073 : EEO_NEXT();
704 : : }
705 : :
706 : 59986667 : EEO_CASE(EEOP_OUTER_VAR)
707 : : {
708 : 59986667 : int attnum = op->d.var.attnum;
709 : :
710 : : /* See EEOP_INNER_VAR comments */
711 : :
712 : : Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
713 : 59986667 : *op->resvalue = outerslot->tts_values[attnum];
714 : 59986667 : *op->resnull = outerslot->tts_isnull[attnum];
715 : :
716 : 59986667 : EEO_NEXT();
717 : : }
718 : :
719 : 56307211 : EEO_CASE(EEOP_SCAN_VAR)
720 : : {
721 : 56307211 : int attnum = op->d.var.attnum;
722 : :
723 : : /* See EEOP_INNER_VAR comments */
724 : :
725 : : Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
726 : 56307211 : *op->resvalue = scanslot->tts_values[attnum];
727 : 56307211 : *op->resnull = scanslot->tts_isnull[attnum];
728 : :
729 : 56307211 : EEO_NEXT();
730 : : }
731 : :
732 : 276 : EEO_CASE(EEOP_OLD_VAR)
733 : : {
734 : 276 : int attnum = op->d.var.attnum;
735 : :
736 : : /* See EEOP_INNER_VAR comments */
737 : :
738 : : Assert(attnum >= 0 && attnum < oldslot->tts_nvalid);
739 : 276 : *op->resvalue = oldslot->tts_values[attnum];
740 : 276 : *op->resnull = oldslot->tts_isnull[attnum];
741 : :
742 : 276 : EEO_NEXT();
743 : : }
744 : :
745 : 274 : EEO_CASE(EEOP_NEW_VAR)
746 : : {
747 : 274 : int attnum = op->d.var.attnum;
748 : :
749 : : /* See EEOP_INNER_VAR comments */
750 : :
751 : : Assert(attnum >= 0 && attnum < newslot->tts_nvalid);
752 : 274 : *op->resvalue = newslot->tts_values[attnum];
753 : 274 : *op->resnull = newslot->tts_isnull[attnum];
754 : :
755 : 274 : EEO_NEXT();
756 : : }
757 : :
758 : 4 : EEO_CASE(EEOP_INNER_SYSVAR)
759 : : {
760 : 4 : ExecEvalSysVar(state, op, econtext, innerslot);
761 : 4 : EEO_NEXT();
762 : : }
763 : :
764 : 8 : EEO_CASE(EEOP_OUTER_SYSVAR)
765 : : {
766 : 8 : ExecEvalSysVar(state, op, econtext, outerslot);
767 : 8 : EEO_NEXT();
768 : : }
769 : :
770 : 6695160 : EEO_CASE(EEOP_SCAN_SYSVAR)
771 : : {
772 : 6695160 : ExecEvalSysVar(state, op, econtext, scanslot);
773 : 6695152 : EEO_NEXT();
774 : : }
775 : :
776 : 152 : EEO_CASE(EEOP_OLD_SYSVAR)
777 : : {
778 : 152 : ExecEvalSysVar(state, op, econtext, oldslot);
779 : 152 : EEO_NEXT();
780 : : }
781 : :
782 : 152 : EEO_CASE(EEOP_NEW_SYSVAR)
783 : : {
784 : 152 : ExecEvalSysVar(state, op, econtext, newslot);
785 : 152 : EEO_NEXT();
786 : : }
787 : :
788 : 35004 : EEO_CASE(EEOP_WHOLEROW)
789 : : {
790 : : /* too complex for an inline implementation */
791 : 35004 : ExecEvalWholeRowVar(state, op, econtext);
792 : :
793 : 35004 : EEO_NEXT();
794 : : }
795 : :
796 : 8675844 : EEO_CASE(EEOP_ASSIGN_INNER_VAR)
797 : : {
798 : 8675844 : int resultnum = op->d.assign_var.resultnum;
799 : 8675844 : 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 : : Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
806 : : Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
807 : 8675844 : resultslot->tts_values[resultnum] = innerslot->tts_values[attnum];
808 : 8675844 : resultslot->tts_isnull[resultnum] = innerslot->tts_isnull[attnum];
809 : :
810 : 8675844 : EEO_NEXT();
811 : : }
812 : :
813 : 22088175 : EEO_CASE(EEOP_ASSIGN_OUTER_VAR)
814 : : {
815 : 22088175 : int resultnum = op->d.assign_var.resultnum;
816 : 22088175 : 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 : : Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
823 : : Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
824 : 22088175 : resultslot->tts_values[resultnum] = outerslot->tts_values[attnum];
825 : 22088175 : resultslot->tts_isnull[resultnum] = outerslot->tts_isnull[attnum];
826 : :
827 : 22088175 : EEO_NEXT();
828 : : }
829 : :
830 : 42001224 : EEO_CASE(EEOP_ASSIGN_SCAN_VAR)
831 : : {
832 : 42001224 : int resultnum = op->d.assign_var.resultnum;
833 : 42001224 : 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 : : Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
840 : : Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
841 : 42001224 : resultslot->tts_values[resultnum] = scanslot->tts_values[attnum];
842 : 42001224 : resultslot->tts_isnull[resultnum] = scanslot->tts_isnull[attnum];
843 : :
844 : 42001224 : EEO_NEXT();
845 : : }
846 : :
847 : 658 : EEO_CASE(EEOP_ASSIGN_OLD_VAR)
848 : : {
849 : 658 : int resultnum = op->d.assign_var.resultnum;
850 : 658 : 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 : : Assert(attnum >= 0 && attnum < oldslot->tts_nvalid);
857 : : Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
858 : 658 : resultslot->tts_values[resultnum] = oldslot->tts_values[attnum];
859 : 658 : resultslot->tts_isnull[resultnum] = oldslot->tts_isnull[attnum];
860 : :
861 : 658 : EEO_NEXT();
862 : : }
863 : :
864 : 658 : EEO_CASE(EEOP_ASSIGN_NEW_VAR)
865 : : {
866 : 658 : int resultnum = op->d.assign_var.resultnum;
867 : 658 : 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 : : Assert(attnum >= 0 && attnum < newslot->tts_nvalid);
874 : : Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
875 : 658 : resultslot->tts_values[resultnum] = newslot->tts_values[attnum];
876 : 658 : resultslot->tts_isnull[resultnum] = newslot->tts_isnull[attnum];
877 : :
878 : 658 : EEO_NEXT();
879 : : }
880 : :
881 : 24616118 : EEO_CASE(EEOP_ASSIGN_TMP)
882 : : {
883 : 24616118 : int resultnum = op->d.assign_tmp.resultnum;
884 : :
885 : : Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
886 : 24616118 : resultslot->tts_values[resultnum] = state->resvalue;
887 : 24616118 : resultslot->tts_isnull[resultnum] = state->resnull;
888 : :
889 : 24616118 : EEO_NEXT();
890 : : }
891 : :
892 : 7412671 : EEO_CASE(EEOP_ASSIGN_TMP_MAKE_RO)
893 : : {
894 : 7412671 : int resultnum = op->d.assign_tmp.resultnum;
895 : :
896 : : Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
897 : 7412671 : resultslot->tts_isnull[resultnum] = state->resnull;
898 [ + + ]: 7412671 : if (!resultslot->tts_isnull[resultnum])
899 : 5631909 : resultslot->tts_values[resultnum] =
900 : 5631909 : MakeExpandedObjectReadOnlyInternal(state->resvalue);
901 : : else
902 : 1780762 : resultslot->tts_values[resultnum] = state->resvalue;
903 : :
904 : 7412671 : EEO_NEXT();
905 : : }
906 : :
907 : 14017094 : EEO_CASE(EEOP_CONST)
908 : : {
909 : 14017094 : *op->resnull = op->d.constval.isnull;
910 : 14017094 : *op->resvalue = op->d.constval.value;
911 : :
912 : 14017094 : EEO_NEXT();
913 : : }
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 : 1780528 : EEO_CASE(EEOP_FUNCEXPR)
931 : : {
932 : 1780528 : FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
933 : : Datum d;
934 : :
935 : 1780528 : fcinfo->isnull = false;
936 : 1780528 : d = op->d.func.fn_addr(fcinfo);
937 : 1773991 : *op->resvalue = d;
938 : 1773991 : *op->resnull = fcinfo->isnull;
939 : :
940 : 1773991 : EEO_NEXT();
941 : : }
942 : :
943 : : /* strict function call with more than two arguments */
944 : 480594 : EEO_CASE(EEOP_FUNCEXPR_STRICT)
945 : : {
946 : 480594 : FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
947 : 480594 : NullableDatum *args = fcinfo->args;
948 : 480594 : int nargs = op->d.func.nargs;
949 : : Datum d;
950 : :
951 : : Assert(nargs > 2);
952 : :
953 : : /* strict function, so check for NULL args */
954 [ + + ]: 1841762 : for (int argno = 0; argno < nargs; argno++)
955 : : {
956 [ + + ]: 1392333 : if (args[argno].isnull)
957 : : {
958 : 31165 : *op->resnull = true;
959 : 31165 : goto strictfail;
960 : : }
961 : : }
962 : 449429 : fcinfo->isnull = false;
963 : 449429 : d = op->d.func.fn_addr(fcinfo);
964 : 447490 : *op->resvalue = d;
965 : 447490 : *op->resnull = fcinfo->isnull;
966 : :
967 : 478655 : strictfail:
968 : 478655 : EEO_NEXT();
969 : : }
970 : :
971 : : /* strict function call with one argument */
972 : 9673086 : EEO_CASE(EEOP_FUNCEXPR_STRICT_1)
973 : : {
974 : 9673086 : FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
975 : 9673086 : NullableDatum *args = fcinfo->args;
976 : :
977 : : Assert(op->d.func.nargs == 1);
978 : :
979 : : /* strict function, so check for NULL args */
980 [ + + ]: 9673086 : if (args[0].isnull)
981 : 40158 : *op->resnull = true;
982 : : else
983 : : {
984 : : Datum d;
985 : :
986 : 9632928 : fcinfo->isnull = false;
987 : 9632928 : d = op->d.func.fn_addr(fcinfo);
988 : 9631985 : *op->resvalue = d;
989 : 9631985 : *op->resnull = fcinfo->isnull;
990 : : }
991 : :
992 : 9672143 : EEO_NEXT();
993 : : }
994 : :
995 : : /* strict function call with two arguments */
996 : 70842491 : EEO_CASE(EEOP_FUNCEXPR_STRICT_2)
997 : : {
998 : 70842491 : FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
999 : 70842491 : NullableDatum *args = fcinfo->args;
1000 : :
1001 : : Assert(op->d.func.nargs == 2);
1002 : :
1003 : : /* strict function, so check for NULL args */
1004 [ + + + + ]: 70842491 : if (args[0].isnull || args[1].isnull)
1005 : 725113 : *op->resnull = true;
1006 : : else
1007 : : {
1008 : : Datum d;
1009 : :
1010 : 70117378 : fcinfo->isnull = false;
1011 : 70117378 : d = op->d.func.fn_addr(fcinfo);
1012 : 70115202 : *op->resvalue = d;
1013 : 70115202 : *op->resnull = fcinfo->isnull;
1014 : : }
1015 : :
1016 : 70840315 : EEO_NEXT();
1017 : : }
1018 : :
1019 : 112 : EEO_CASE(EEOP_FUNCEXPR_FUSAGE)
1020 : : {
1021 : : /* not common enough to inline */
1022 : 112 : ExecEvalFuncExprFusage(state, op, econtext);
1023 : :
1024 : 112 : EEO_NEXT();
1025 : : }
1026 : :
1027 : 4 : EEO_CASE(EEOP_FUNCEXPR_STRICT_FUSAGE)
1028 : : {
1029 : : /* not common enough to inline */
1030 : 4 : ExecEvalFuncExprStrictFusage(state, op, econtext);
1031 : :
1032 : 4 : EEO_NEXT();
1033 : : }
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 : 720696 : EEO_CASE(EEOP_BOOL_AND_STEP_FIRST)
1046 : : {
1047 : 720696 : *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 : : }
1056 : :
1057 : 820027 : EEO_CASE(EEOP_BOOL_AND_STEP)
1058 : : {
1059 [ + + ]: 820027 : if (*op->resnull)
1060 : : {
1061 : 616 : *op->d.boolexpr.anynull = true;
1062 : : }
1063 [ + + ]: 819411 : else if (!DatumGetBool(*op->resvalue))
1064 : : {
1065 : : /* result is already set to FALSE, need not change it */
1066 : : /* bail out early */
1067 : 528789 : EEO_JUMP(op->d.boolexpr.jumpdone);
1068 : : }
1069 : :
1070 : 291238 : EEO_NEXT();
1071 : : }
1072 : :
1073 : 191907 : EEO_CASE(EEOP_BOOL_AND_STEP_LAST)
1074 : : {
1075 [ + + ]: 191907 : if (*op->resnull)
1076 : : {
1077 : : /* result is already set to NULL, need not change it */
1078 : : }
1079 [ + + ]: 191449 : else if (!DatumGetBool(*op->resvalue))
1080 : : {
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 : : }
1089 [ + + ]: 138307 : else if (*op->d.boolexpr.anynull)
1090 : : {
1091 : 188 : *op->resvalue = (Datum) 0;
1092 : 188 : *op->resnull = true;
1093 : : }
1094 : : else
1095 : : {
1096 : : /* result is already set to TRUE, need not change it */
1097 : : }
1098 : :
1099 : 191907 : EEO_NEXT();
1100 : : }
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 : 2541010 : EEO_CASE(EEOP_BOOL_OR_STEP_FIRST)
1113 : : {
1114 : 2541010 : *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 : : }
1123 : :
1124 : 5020503 : EEO_CASE(EEOP_BOOL_OR_STEP)
1125 : : {
1126 [ + + ]: 5020503 : if (*op->resnull)
1127 : : {
1128 : 107727 : *op->d.boolexpr.anynull = true;
1129 : : }
1130 [ + + ]: 4912776 : else if (DatumGetBool(*op->resvalue))
1131 : : {
1132 : : /* result is already set to TRUE, need not change it */
1133 : : /* bail out early */
1134 : 337983 : EEO_JUMP(op->d.boolexpr.jumpdone);
1135 : : }
1136 : :
1137 : 4682520 : EEO_NEXT();
1138 : : }
1139 : :
1140 : 2203027 : EEO_CASE(EEOP_BOOL_OR_STEP_LAST)
1141 : : {
1142 [ + + ]: 2203027 : if (*op->resnull)
1143 : : {
1144 : : /* result is already set to NULL, need not change it */
1145 : : }
1146 [ + + ]: 2103113 : else if (DatumGetBool(*op->resvalue))
1147 : : {
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 : : }
1156 [ + + ]: 2062424 : else if (*op->d.boolexpr.anynull)
1157 : : {
1158 : 4285 : *op->resvalue = (Datum) 0;
1159 : 4285 : *op->resnull = true;
1160 : : }
1161 : : else
1162 : : {
1163 : : /* result is already set to FALSE, need not change it */
1164 : : }
1165 : :
1166 : 2203027 : EEO_NEXT();
1167 : : }
1168 : :
1169 : 3458636 : EEO_CASE(EEOP_BOOL_NOT_STEP)
1170 : : {
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 : 3458636 : *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
1178 : :
1179 : 3458636 : EEO_NEXT();
1180 : : }
1181 : :
1182 : 63877936 : EEO_CASE(EEOP_QUAL)
1183 : : {
1184 : : /* simplified version of BOOL_AND_STEP for use by ExecQual() */
1185 : :
1186 : : /* If argument (also result) is false or null ... */
1187 [ + + ]: 63877936 : if (*op->resnull ||
1188 [ + + ]: 63255991 : !DatumGetBool(*op->resvalue))
1189 : : {
1190 : : /* ... bail out early, returning FALSE */
1191 : 27214103 : *op->resnull = false;
1192 : 27214103 : *op->resvalue = BoolGetDatum(false);
1193 : 27214103 : EEO_JUMP(op->d.qualexpr.jumpdone);
1194 : : }
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 : 36663833 : EEO_NEXT();
1202 : : }
1203 : :
1204 : 257399 : EEO_CASE(EEOP_JUMP)
1205 : : {
1206 : : /* Unconditionally jump to target step */
1207 : 257399 : EEO_JUMP(op->d.jump.jumpdone);
1208 : : }
1209 : :
1210 : 484610 : EEO_CASE(EEOP_JUMP_IF_NULL)
1211 : : {
1212 : : /* Transfer control if current result is null */
1213 [ + + ]: 484610 : if (*op->resnull)
1214 : 2159 : EEO_JUMP(op->d.jump.jumpdone);
1215 : :
1216 : 482451 : EEO_NEXT();
1217 : : }
1218 : :
1219 : 404897 : EEO_CASE(EEOP_JUMP_IF_NOT_NULL)
1220 : : {
1221 : : /* Transfer control if current result is non-null */
1222 [ + + ]: 404897 : if (!*op->resnull)
1223 : 254958 : EEO_JUMP(op->d.jump.jumpdone);
1224 : :
1225 : 149939 : EEO_NEXT();
1226 : : }
1227 : :
1228 : 1413029 : EEO_CASE(EEOP_JUMP_IF_NOT_TRUE)
1229 : : {
1230 : : /* Transfer control if current result is null or false */
1231 [ + + + + ]: 1413029 : if (*op->resnull || !DatumGetBool(*op->resvalue))
1232 : 1077639 : EEO_JUMP(op->d.jump.jumpdone);
1233 : :
1234 : 335390 : EEO_NEXT();
1235 : : }
1236 : :
1237 : 650377 : EEO_CASE(EEOP_NULLTEST_ISNULL)
1238 : : {
1239 : 650377 : *op->resvalue = BoolGetDatum(*op->resnull);
1240 : 650377 : *op->resnull = false;
1241 : :
1242 : 650377 : EEO_NEXT();
1243 : : }
1244 : :
1245 : 2066849 : EEO_CASE(EEOP_NULLTEST_ISNOTNULL)
1246 : : {
1247 : 2066849 : *op->resvalue = BoolGetDatum(!*op->resnull);
1248 : 2066849 : *op->resnull = false;
1249 : :
1250 : 2066849 : EEO_NEXT();
1251 : : }
1252 : :
1253 : 475 : EEO_CASE(EEOP_NULLTEST_ROWISNULL)
1254 : : {
1255 : : /* out of line implementation: too large */
1256 : 475 : ExecEvalRowNull(state, op, econtext);
1257 : :
1258 : 475 : EEO_NEXT();
1259 : : }
1260 : :
1261 : 353 : EEO_CASE(EEOP_NULLTEST_ROWISNOTNULL)
1262 : : {
1263 : : /* out of line implementation: too large */
1264 : 353 : ExecEvalRowNotNull(state, op, econtext);
1265 : :
1266 : 353 : EEO_NEXT();
1267 : : }
1268 : :
1269 : : /* BooleanTest implementations for all booltesttypes */
1270 : :
1271 : 57537 : EEO_CASE(EEOP_BOOLTEST_IS_TRUE)
1272 : : {
1273 [ + + ]: 57537 : if (*op->resnull)
1274 : : {
1275 : 57014 : *op->resvalue = BoolGetDatum(false);
1276 : 57014 : *op->resnull = false;
1277 : : }
1278 : : /* else, input value is the correct output as well */
1279 : :
1280 : 57537 : EEO_NEXT();
1281 : : }
1282 : :
1283 : 539 : EEO_CASE(EEOP_BOOLTEST_IS_NOT_TRUE)
1284 : : {
1285 [ + + ]: 539 : if (*op->resnull)
1286 : : {
1287 : 98 : *op->resvalue = BoolGetDatum(true);
1288 : 98 : *op->resnull = false;
1289 : : }
1290 : : else
1291 : 441 : *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
1292 : :
1293 : 539 : EEO_NEXT();
1294 : : }
1295 : :
1296 : 381 : EEO_CASE(EEOP_BOOLTEST_IS_FALSE)
1297 : : {
1298 [ + + ]: 381 : if (*op->resnull)
1299 : : {
1300 : 83 : *op->resvalue = BoolGetDatum(false);
1301 : 83 : *op->resnull = false;
1302 : : }
1303 : : else
1304 : 298 : *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
1305 : :
1306 : 381 : EEO_NEXT();
1307 : : }
1308 : :
1309 : 362 : EEO_CASE(EEOP_BOOLTEST_IS_NOT_FALSE)
1310 : : {
1311 [ + + ]: 362 : if (*op->resnull)
1312 : : {
1313 : 28 : *op->resvalue = BoolGetDatum(true);
1314 : 28 : *op->resnull = false;
1315 : : }
1316 : : /* else, input value is the correct output as well */
1317 : :
1318 : 362 : EEO_NEXT();
1319 : : }
1320 : :
1321 : 5974649 : EEO_CASE(EEOP_PARAM_EXEC)
1322 : : {
1323 : : /* out of line implementation: too large */
1324 : 5974649 : ExecEvalParamExec(state, op, econtext);
1325 : :
1326 : 5974638 : EEO_NEXT();
1327 : : }
1328 : :
1329 : 293689 : EEO_CASE(EEOP_PARAM_EXTERN)
1330 : : {
1331 : : /* out of line implementation: too large */
1332 : 293689 : ExecEvalParamExtern(state, op, econtext);
1333 : 293689 : EEO_NEXT();
1334 : : }
1335 : :
1336 : 310085 : EEO_CASE(EEOP_PARAM_CALLBACK)
1337 : : {
1338 : : /* allow an extension module to supply a PARAM_EXTERN value */
1339 : 310085 : op->d.cparam.paramfunc(state, op, econtext);
1340 : 310082 : EEO_NEXT();
1341 : : }
1342 : :
1343 : 1035193 : EEO_CASE(EEOP_PARAM_SET)
1344 : : {
1345 : : /* out of line, unlikely to matter performance-wise */
1346 : 1035193 : ExecEvalParamSet(state, op, econtext);
1347 : 1035193 : EEO_NEXT();
1348 : : }
1349 : :
1350 : 28694 : EEO_CASE(EEOP_CASE_TESTVAL)
1351 : : {
1352 : 28694 : *op->resvalue = *op->d.casetest.value;
1353 : 28694 : *op->resnull = *op->d.casetest.isnull;
1354 : :
1355 : 28694 : EEO_NEXT();
1356 : : }
1357 : :
1358 : 5829 : EEO_CASE(EEOP_CASE_TESTVAL_EXT)
1359 : : {
1360 : 5829 : *op->resvalue = econtext->caseValue_datum;
1361 : 5829 : *op->resnull = econtext->caseValue_isNull;
1362 : :
1363 : 5829 : EEO_NEXT();
1364 : : }
1365 : :
1366 : 3390 : EEO_CASE(EEOP_MAKE_READONLY)
1367 : : {
1368 : : /*
1369 : : * Force a varlena value that might be read multiple times to R/O
1370 : : */
1371 [ + + ]: 3390 : if (!*op->d.make_readonly.isnull)
1372 : 3326 : *op->resvalue =
1373 : 3326 : MakeExpandedObjectReadOnlyInternal(*op->d.make_readonly.value);
1374 : 3390 : *op->resnull = *op->d.make_readonly.isnull;
1375 : :
1376 : 3390 : EEO_NEXT();
1377 : : }
1378 : :
1379 : 4668246 : EEO_CASE(EEOP_IOCOERCE)
1380 : : {
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 : : char *str;
1390 : :
1391 : : /* call output function (similar to OutputFunctionCall) */
1392 [ + + ]: 4668246 : if (*op->resnull)
1393 : : {
1394 : : /* output functions are not called on nulls */
1395 : 42387 : str = NULL;
1396 : : }
1397 : : else
1398 : : {
1399 : : FunctionCallInfo fcinfo_out;
1400 : :
1401 : 4625859 : fcinfo_out = op->d.iocoerce.fcinfo_data_out;
1402 : 4625859 : fcinfo_out->args[0].value = *op->resvalue;
1403 : 4625859 : fcinfo_out->args[0].isnull = false;
1404 : :
1405 : 4625859 : fcinfo_out->isnull = false;
1406 : 4625859 : str = DatumGetCString(FunctionCallInvoke(fcinfo_out));
1407 : :
1408 : : /* OutputFunctionCall assumes result isn't null */
1409 : : Assert(!fcinfo_out->isnull);
1410 : : }
1411 : :
1412 : : /* call input function (similar to InputFunctionCall) */
1413 [ + + + + ]: 4668192 : if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL)
1414 : : {
1415 : : FunctionCallInfo fcinfo_in;
1416 : : Datum d;
1417 : :
1418 : 4625890 : fcinfo_in = op->d.iocoerce.fcinfo_data_in;
1419 : 4625890 : fcinfo_in->args[0].value = PointerGetDatum(str);
1420 : 4625890 : fcinfo_in->args[0].isnull = *op->resnull;
1421 : : /* second and third arguments are already set up */
1422 : :
1423 : 4625890 : fcinfo_in->isnull = false;
1424 : 4625890 : d = FunctionCallInvoke(fcinfo_in);
1425 : 4625865 : *op->resvalue = d;
1426 : :
1427 : : /* Should get null result if and only if str is NULL */
1428 : : if (str == NULL)
1429 : : {
1430 : : Assert(*op->resnull);
1431 : : Assert(fcinfo_in->isnull);
1432 : : }
1433 : : else
1434 : : {
1435 : : Assert(!*op->resnull);
1436 : : Assert(!fcinfo_in->isnull);
1437 : : }
1438 : : }
1439 : :
1440 : 4668167 : EEO_NEXT();
1441 : : }
1442 : :
1443 : 36 : EEO_CASE(EEOP_IOCOERCE_SAFE)
1444 : : {
1445 : 36 : ExecEvalCoerceViaIOSafe(state, op);
1446 : 36 : EEO_NEXT();
1447 : : }
1448 : :
1449 : 952066 : EEO_CASE(EEOP_DISTINCT)
1450 : : {
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 : 952066 : FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1461 : :
1462 : : /* check function arguments for NULLness */
1463 [ + + + + ]: 952066 : if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
1464 : : {
1465 : : /* Both NULL? Then is not distinct... */
1466 : 722320 : *op->resvalue = BoolGetDatum(false);
1467 : 722320 : *op->resnull = false;
1468 : : }
1469 [ + + + + ]: 229746 : else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
1470 : : {
1471 : : /* Only one is NULL? Then is distinct... */
1472 : 188 : *op->resvalue = BoolGetDatum(true);
1473 : 188 : *op->resnull = false;
1474 : : }
1475 : : else
1476 : : {
1477 : : /* Neither null, so apply the equality function */
1478 : : Datum eqresult;
1479 : :
1480 : 229558 : fcinfo->isnull = false;
1481 : 229558 : eqresult = op->d.func.fn_addr(fcinfo);
1482 : : /* Must invert result of "="; safe to do even if null */
1483 : 229558 : *op->resvalue = BoolGetDatum(!DatumGetBool(eqresult));
1484 : 229558 : *op->resnull = fcinfo->isnull;
1485 : : }
1486 : :
1487 : 952066 : EEO_NEXT();
1488 : : }
1489 : :
1490 : : /* see EEOP_DISTINCT for comments, this is just inverted */
1491 : 11358370 : EEO_CASE(EEOP_NOT_DISTINCT)
1492 : : {
1493 : 11358370 : FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1494 : :
1495 [ + + + + ]: 11358370 : if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
1496 : : {
1497 : 86529 : *op->resvalue = BoolGetDatum(true);
1498 : 86529 : *op->resnull = false;
1499 : : }
1500 [ + + + + ]: 11271841 : else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
1501 : : {
1502 : 306 : *op->resvalue = BoolGetDatum(false);
1503 : 306 : *op->resnull = false;
1504 : : }
1505 : : else
1506 : : {
1507 : : Datum eqresult;
1508 : :
1509 : 11271535 : fcinfo->isnull = false;
1510 : 11271535 : eqresult = op->d.func.fn_addr(fcinfo);
1511 : 11271535 : *op->resvalue = eqresult;
1512 : 11271535 : *op->resnull = fcinfo->isnull;
1513 : : }
1514 : :
1515 : 11358370 : EEO_NEXT();
1516 : : }
1517 : :
1518 : 4951 : EEO_CASE(EEOP_NULLIF)
1519 : : {
1520 : : /*
1521 : : * The arguments are already evaluated into fcinfo->args.
1522 : : */
1523 : 4951 : FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1524 : 4951 : Datum save_arg0 = fcinfo->args[0].value;
1525 : :
1526 : : /* if either argument is NULL they can't be equal */
1527 [ + + + + ]: 4951 : if (!fcinfo->args[0].isnull && !fcinfo->args[1].isnull)
1528 : : {
1529 : : 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 [ + + ]: 4907 : if (op->d.func.make_ro)
1539 : 203 : fcinfo->args[0].value =
1540 : 203 : MakeExpandedObjectReadOnlyInternal(save_arg0);
1541 : :
1542 : 4907 : fcinfo->isnull = false;
1543 : 4907 : result = op->d.func.fn_addr(fcinfo);
1544 : :
1545 : : /* if the arguments are equal return null */
1546 [ + - + + ]: 4907 : if (!fcinfo->isnull && DatumGetBool(result))
1547 : : {
1548 : 172 : *op->resvalue = (Datum) 0;
1549 : 172 : *op->resnull = true;
1550 : :
1551 : 172 : EEO_NEXT();
1552 : : }
1553 : : }
1554 : :
1555 : : /* Arguments aren't equal, so return the first one */
1556 : 4779 : *op->resvalue = save_arg0;
1557 : 4779 : *op->resnull = fcinfo->args[0].isnull;
1558 : :
1559 : 4779 : EEO_NEXT();
1560 : : }
1561 : :
1562 : 7180 : EEO_CASE(EEOP_SQLVALUEFUNCTION)
1563 : : {
1564 : : /*
1565 : : * Doesn't seem worthwhile to have an inline implementation
1566 : : * efficiency-wise.
1567 : : */
1568 : 7180 : ExecEvalSQLValueFunction(state, op);
1569 : :
1570 : 7180 : EEO_NEXT();
1571 : : }
1572 : :
1573 : 1 : EEO_CASE(EEOP_CURRENTOFEXPR)
1574 : : {
1575 : : /* error invocation uses space, and shouldn't ever occur */
1576 : 1 : ExecEvalCurrentOfExpr(state, op);
1577 : :
1578 : 0 : EEO_NEXT();
1579 : : }
1580 : :
1581 : 1910 : EEO_CASE(EEOP_NEXTVALUEEXPR)
1582 : : {
1583 : : /*
1584 : : * Doesn't seem worthwhile to have an inline implementation
1585 : : * efficiency-wise.
1586 : : */
1587 : 1910 : ExecEvalNextValueExpr(state, op);
1588 : :
1589 : 1910 : EEO_NEXT();
1590 : : }
1591 : :
1592 : 604 : EEO_CASE(EEOP_RETURNINGEXPR)
1593 : : {
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 [ + + ]: 604 : if (state->flags & op->d.returningexpr.nullflag)
1599 : : {
1600 : 112 : *op->resvalue = (Datum) 0;
1601 : 112 : *op->resnull = true;
1602 : :
1603 : 112 : EEO_JUMP(op->d.returningexpr.jumpdone);
1604 : : }
1605 : :
1606 : 492 : EEO_NEXT();
1607 : : }
1608 : :
1609 : 573047 : EEO_CASE(EEOP_ARRAYEXPR)
1610 : : {
1611 : : /* too complex for an inline implementation */
1612 : 573047 : ExecEvalArrayExpr(state, op);
1613 : :
1614 : 573047 : EEO_NEXT();
1615 : : }
1616 : :
1617 : 66408 : EEO_CASE(EEOP_ARRAYCOERCE)
1618 : : {
1619 : : /* too complex for an inline implementation */
1620 : 66408 : ExecEvalArrayCoerce(state, op, econtext);
1621 : :
1622 : 66388 : EEO_NEXT();
1623 : : }
1624 : :
1625 : 45415 : EEO_CASE(EEOP_ROW)
1626 : : {
1627 : : /* too complex for an inline implementation */
1628 : 45415 : ExecEvalRow(state, op);
1629 : :
1630 : 45411 : EEO_NEXT();
1631 : : }
1632 : :
1633 : 139152 : EEO_CASE(EEOP_ROWCOMPARE_STEP)
1634 : : {
1635 : 139152 : FunctionCallInfo fcinfo = op->d.rowcompare_step.fcinfo_data;
1636 : : Datum d;
1637 : :
1638 : : /* force NULL result if strict fn and NULL input */
1639 [ + - ]: 139152 : if (op->d.rowcompare_step.finfo->fn_strict &&
1640 [ + - + + ]: 139152 : (fcinfo->args[0].isnull || fcinfo->args[1].isnull))
1641 : : {
1642 : 12 : *op->resnull = true;
1643 : 12 : EEO_JUMP(op->d.rowcompare_step.jumpnull);
1644 : : }
1645 : :
1646 : : /* Apply comparison function */
1647 : 139140 : fcinfo->isnull = false;
1648 : 139140 : d = op->d.rowcompare_step.fn_addr(fcinfo);
1649 : 139140 : *op->resvalue = d;
1650 : :
1651 : : /* force NULL result if NULL function result */
1652 [ - + ]: 139140 : if (fcinfo->isnull)
1653 : : {
1654 : 0 : *op->resnull = true;
1655 : 0 : EEO_JUMP(op->d.rowcompare_step.jumpnull);
1656 : : }
1657 : 139140 : *op->resnull = false;
1658 : :
1659 : : /* If unequal, no need to compare remaining columns */
1660 [ + + ]: 139140 : if (DatumGetInt32(*op->resvalue) != 0)
1661 : : {
1662 : 63032 : EEO_JUMP(op->d.rowcompare_step.jumpdone);
1663 : : }
1664 : :
1665 : 76108 : EEO_NEXT();
1666 : : }
1667 : :
1668 : 63032 : EEO_CASE(EEOP_ROWCOMPARE_FINAL)
1669 : : {
1670 : 63032 : int32 cmpresult = DatumGetInt32(*op->resvalue);
1671 : 63032 : CompareType cmptype = op->d.rowcompare_final.cmptype;
1672 : :
1673 : 63032 : *op->resnull = false;
1674 [ + + + + : 63032 : switch (cmptype)
- ]
1675 : : {
1676 : : /* EQ and NE cases aren't allowed here */
1677 : 22960 : case COMPARE_LT:
1678 : 22960 : *op->resvalue = BoolGetDatum(cmpresult < 0);
1679 : 22960 : break;
1680 : 40000 : case COMPARE_LE:
1681 : 40000 : *op->resvalue = BoolGetDatum(cmpresult <= 0);
1682 : 40000 : break;
1683 : 4 : case COMPARE_GE:
1684 : 4 : *op->resvalue = BoolGetDatum(cmpresult >= 0);
1685 : 4 : break;
1686 : 68 : case COMPARE_GT:
1687 : 68 : *op->resvalue = BoolGetDatum(cmpresult > 0);
1688 : 68 : break;
1689 : 0 : default:
1690 : : Assert(false);
1691 : 0 : break;
1692 : : }
1693 : :
1694 : 63032 : EEO_NEXT();
1695 : : }
1696 : :
1697 : 11801 : EEO_CASE(EEOP_MINMAX)
1698 : : {
1699 : : /* too complex for an inline implementation */
1700 : 11801 : ExecEvalMinMax(state, op);
1701 : :
1702 : 11801 : EEO_NEXT();
1703 : : }
1704 : :
1705 : 1323414 : EEO_CASE(EEOP_FIELDSELECT)
1706 : : {
1707 : : /* too complex for an inline implementation */
1708 : 1323414 : ExecEvalFieldSelect(state, op, econtext);
1709 : :
1710 : 1323389 : EEO_NEXT();
1711 : : }
1712 : :
1713 : 397 : EEO_CASE(EEOP_FIELDSTORE_DEFORM)
1714 : : {
1715 : : /* too complex for an inline implementation */
1716 : 397 : ExecEvalFieldStoreDeForm(state, op, econtext);
1717 : :
1718 : 397 : EEO_NEXT();
1719 : : }
1720 : :
1721 : 397 : EEO_CASE(EEOP_FIELDSTORE_FORM)
1722 : : {
1723 : : /* too complex for an inline implementation */
1724 : 397 : ExecEvalFieldStoreForm(state, op, econtext);
1725 : :
1726 : 397 : EEO_NEXT();
1727 : : }
1728 : :
1729 : 473282 : EEO_CASE(EEOP_SBSREF_SUBSCRIPTS)
1730 : : {
1731 : : /* Precheck SubscriptingRef subscript(s) */
1732 [ + + ]: 473282 : if (op->d.sbsref_subscript.subscriptfunc(state, op, econtext))
1733 : : {
1734 : 473241 : EEO_NEXT();
1735 : : }
1736 : : else
1737 : : {
1738 : : /* Subscript is null, short-circuit SubscriptingRef to NULL */
1739 : 25 : EEO_JUMP(op->d.sbsref_subscript.jumpdone);
1740 : : }
1741 : : }
1742 : :
1743 : 187 : EEO_CASE(EEOP_SBSREF_OLD)
1744 : 1307 : EEO_CASE(EEOP_SBSREF_ASSIGN)
1745 : 473421 : EEO_CASE(EEOP_SBSREF_FETCH)
1746 : : {
1747 : : /* Perform a SubscriptingRef fetch or assignment */
1748 : 473421 : op->d.sbsref.subscriptfunc(state, op, econtext);
1749 : :
1750 : 473337 : EEO_NEXT();
1751 : : }
1752 : :
1753 : 7116 : EEO_CASE(EEOP_CONVERT_ROWTYPE)
1754 : : {
1755 : : /* too complex for an inline implementation */
1756 : 7116 : ExecEvalConvertRowtype(state, op, econtext);
1757 : :
1758 : 7112 : EEO_NEXT();
1759 : : }
1760 : :
1761 : 3465342 : EEO_CASE(EEOP_SCALARARRAYOP)
1762 : : {
1763 : : /* too complex for an inline implementation */
1764 : 3465342 : ExecEvalScalarArrayOp(state, op);
1765 : :
1766 : 3465342 : EEO_NEXT();
1767 : : }
1768 : :
1769 : 17921 : EEO_CASE(EEOP_HASHED_SCALARARRAYOP)
1770 : : {
1771 : : /* too complex for an inline implementation */
1772 : 17921 : ExecEvalHashedScalarArrayOp(state, op, econtext);
1773 : :
1774 : 17921 : EEO_NEXT();
1775 : : }
1776 : :
1777 : 9032 : EEO_CASE(EEOP_DOMAIN_TESTVAL)
1778 : : {
1779 : 9032 : *op->resvalue = *op->d.casetest.value;
1780 : 9032 : *op->resnull = *op->d.casetest.isnull;
1781 : :
1782 : 9032 : EEO_NEXT();
1783 : : }
1784 : :
1785 : 46316 : EEO_CASE(EEOP_DOMAIN_TESTVAL_EXT)
1786 : : {
1787 : 46316 : *op->resvalue = econtext->domainValue_datum;
1788 : 46316 : *op->resnull = econtext->domainValue_isNull;
1789 : :
1790 : 46316 : EEO_NEXT();
1791 : : }
1792 : :
1793 : 264 : EEO_CASE(EEOP_DOMAIN_NOTNULL)
1794 : : {
1795 : : /* too complex for an inline implementation */
1796 : 264 : ExecEvalConstraintNotNull(state, op);
1797 : :
1798 : 197 : EEO_NEXT();
1799 : : }
1800 : :
1801 : 8697 : EEO_CASE(EEOP_DOMAIN_CHECK)
1802 : : {
1803 : : /* too complex for an inline implementation */
1804 : 8697 : ExecEvalConstraintCheck(state, op);
1805 : :
1806 : 8409 : EEO_NEXT();
1807 : : }
1808 : :
1809 : 133360 : EEO_CASE(EEOP_HASHDATUM_SET_INITVAL)
1810 : : {
1811 : 133360 : *op->resvalue = op->d.hashdatum_initvalue.init_value;
1812 : 133360 : *op->resnull = false;
1813 : :
1814 : 133360 : EEO_NEXT();
1815 : : }
1816 : :
1817 : 1634295 : EEO_CASE(EEOP_HASHDATUM_FIRST)
1818 : : {
1819 : 1634295 : 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 [ + + ]: 1634295 : if (!fcinfo->args[0].isnull)
1826 : 1596950 : *op->resvalue = op->d.hashdatum.fn_addr(fcinfo);
1827 : : else
1828 : 37345 : *op->resvalue = (Datum) 0;
1829 : :
1830 : 1634291 : *op->resnull = false;
1831 : :
1832 : 1634291 : EEO_NEXT();
1833 : : }
1834 : :
1835 : 14070191 : EEO_CASE(EEOP_HASHDATUM_FIRST_STRICT)
1836 : : {
1837 : 14070191 : FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data;
1838 : :
1839 [ + + ]: 14070191 : if (fcinfo->args[0].isnull)
1840 : : {
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 : 593 : state->resnull = true;
1847 : 593 : state->resvalue = (Datum) 0;
1848 : 593 : EEO_JUMP(op->d.hashdatum.jumpdone);
1849 : : }
1850 : :
1851 : : /* execute the hash function and save the resulting value */
1852 : 14069598 : *op->resvalue = op->d.hashdatum.fn_addr(fcinfo);
1853 : 14069598 : *op->resnull = false;
1854 : :
1855 : 14069598 : EEO_NEXT();
1856 : : }
1857 : :
1858 : 3335740 : EEO_CASE(EEOP_HASHDATUM_NEXT32)
1859 : : {
1860 : 3335740 : FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data;
1861 : : uint32 existinghash;
1862 : :
1863 : 3335740 : existinghash = DatumGetUInt32(op->d.hashdatum.iresult->value);
1864 : : /* combine successive hash values by rotating */
1865 : 3335740 : existinghash = pg_rotate_left32(existinghash, 1);
1866 : :
1867 : : /* leave the hash value alone on NULL inputs */
1868 [ + + ]: 3335740 : if (!fcinfo->args[0].isnull)
1869 : : {
1870 : : uint32 hashvalue;
1871 : :
1872 : : /* execute hash func and combine with previous hash value */
1873 : 3230008 : hashvalue = DatumGetUInt32(op->d.hashdatum.fn_addr(fcinfo));
1874 : 3230008 : existinghash = existinghash ^ hashvalue;
1875 : : }
1876 : :
1877 : 3335740 : *op->resvalue = UInt32GetDatum(existinghash);
1878 : 3335740 : *op->resnull = false;
1879 : :
1880 : 3335740 : EEO_NEXT();
1881 : : }
1882 : :
1883 : 1703098 : EEO_CASE(EEOP_HASHDATUM_NEXT32_STRICT)
1884 : : {
1885 : 1703098 : FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data;
1886 : :
1887 [ + + ]: 1703098 : if (fcinfo->args[0].isnull)
1888 : : {
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 : 114 : state->resnull = true;
1895 : 114 : state->resvalue = (Datum) 0;
1896 : 114 : EEO_JUMP(op->d.hashdatum.jumpdone);
1897 : : }
1898 : : else
1899 : : {
1900 : : uint32 existinghash;
1901 : : uint32 hashvalue;
1902 : :
1903 : 1702984 : existinghash = DatumGetUInt32(op->d.hashdatum.iresult->value);
1904 : : /* combine successive hash values by rotating */
1905 : 1702984 : existinghash = pg_rotate_left32(existinghash, 1);
1906 : :
1907 : : /* execute hash func and combine with previous hash value */
1908 : 1702984 : hashvalue = DatumGetUInt32(op->d.hashdatum.fn_addr(fcinfo));
1909 : 1702984 : *op->resvalue = UInt32GetDatum(existinghash ^ hashvalue);
1910 : 1702984 : *op->resnull = false;
1911 : : }
1912 : :
1913 : 1702984 : EEO_NEXT();
1914 : : }
1915 : :
1916 : 30698 : EEO_CASE(EEOP_XMLEXPR)
1917 : : {
1918 : : /* too complex for an inline implementation */
1919 : 30698 : ExecEvalXmlExpr(state, op);
1920 : :
1921 : 30630 : EEO_NEXT();
1922 : : }
1923 : :
1924 : 484 : EEO_CASE(EEOP_JSON_CONSTRUCTOR)
1925 : : {
1926 : : /* too complex for an inline implementation */
1927 : 484 : ExecEvalJsonConstructor(state, op, econtext);
1928 : 428 : EEO_NEXT();
1929 : : }
1930 : :
1931 : 1864 : EEO_CASE(EEOP_IS_JSON)
1932 : : {
1933 : : /* too complex for an inline implementation */
1934 : 1864 : ExecEvalJsonIsPredicate(state, op);
1935 : :
1936 : 1864 : EEO_NEXT();
1937 : : }
1938 : :
1939 : 5156 : EEO_CASE(EEOP_JSONEXPR_PATH)
1940 : : {
1941 : : /* too complex for an inline implementation */
1942 : 5156 : EEO_JUMP(ExecEvalJsonExprPath(state, op, econtext));
1943 : : }
1944 : :
1945 : 1400 : EEO_CASE(EEOP_JSONEXPR_COERCION)
1946 : : {
1947 : : /* too complex for an inline implementation */
1948 : 1400 : ExecEvalJsonCoercion(state, op, econtext);
1949 : :
1950 : 1292 : EEO_NEXT();
1951 : : }
1952 : :
1953 : 1648 : EEO_CASE(EEOP_JSONEXPR_COERCION_FINISH)
1954 : : {
1955 : : /* too complex for an inline implementation */
1956 : 1648 : ExecEvalJsonCoercionFinish(state, op);
1957 : :
1958 : 1556 : EEO_NEXT();
1959 : : }
1960 : :
1961 : 623652 : EEO_CASE(EEOP_AGGREF)
1962 : : {
1963 : : /*
1964 : : * Returns a Datum whose value is the precomputed aggregate value
1965 : : * found in the given expression context.
1966 : : */
1967 : 623652 : int aggno = op->d.aggref.aggno;
1968 : :
1969 : : Assert(econtext->ecxt_aggvalues != NULL);
1970 : :
1971 : 623652 : *op->resvalue = econtext->ecxt_aggvalues[aggno];
1972 : 623652 : *op->resnull = econtext->ecxt_aggnulls[aggno];
1973 : :
1974 : 623652 : EEO_NEXT();
1975 : : }
1976 : :
1977 : 1367 : EEO_CASE(EEOP_GROUPING_FUNC)
1978 : : {
1979 : : /* too complex/uncommon for an inline implementation */
1980 : 1367 : ExecEvalGroupingFunc(state, op);
1981 : :
1982 : 1367 : EEO_NEXT();
1983 : : }
1984 : :
1985 : 769198 : EEO_CASE(EEOP_WINDOW_FUNC)
1986 : : {
1987 : : /*
1988 : : * Like Aggref, just return a precomputed value from the econtext.
1989 : : */
1990 : 769198 : WindowFuncExprState *wfunc = op->d.window_func.wfstate;
1991 : :
1992 : : Assert(econtext->ecxt_aggvalues != NULL);
1993 : :
1994 : 769198 : *op->resvalue = econtext->ecxt_aggvalues[wfunc->wfuncno];
1995 : 769198 : *op->resnull = econtext->ecxt_aggnulls[wfunc->wfuncno];
1996 : :
1997 : 769198 : EEO_NEXT();
1998 : : }
1999 : :
2000 : 343 : EEO_CASE(EEOP_MERGE_SUPPORT_FUNC)
2001 : : {
2002 : : /* too complex/uncommon for an inline implementation */
2003 : 343 : ExecEvalMergeSupportFunc(state, op, econtext);
2004 : :
2005 : 343 : EEO_NEXT();
2006 : : }
2007 : :
2008 : 2221069 : EEO_CASE(EEOP_SUBPLAN)
2009 : : {
2010 : : /* too complex for an inline implementation */
2011 : 2221069 : ExecEvalSubPlan(state, op, econtext);
2012 : :
2013 : 2221065 : EEO_NEXT();
2014 : : }
2015 : :
2016 : : /* evaluate a strict aggregate deserialization function */
2017 : 364 : EEO_CASE(EEOP_AGG_STRICT_DESERIALIZE)
2018 : : {
2019 : : /* Don't call a strict deserialization function with NULL input */
2020 [ + + ]: 364 : if (op->d.agg_deserialize.fcinfo_data->args[0].isnull)
2021 : 52 : EEO_JUMP(op->d.agg_deserialize.jumpnull);
2022 : :
2023 : : /* fallthrough */
2024 : : }
2025 : :
2026 : : /* evaluate aggregate deserialization function (non-strict portion) */
2027 : 312 : EEO_CASE(EEOP_AGG_DESERIALIZE)
2028 : : {
2029 : 312 : FunctionCallInfo fcinfo = op->d.agg_deserialize.fcinfo_data;
2030 : 312 : AggState *aggstate = castNode(AggState, state->parent);
2031 : : MemoryContext oldContext;
2032 : :
2033 : : /*
2034 : : * We run the deserialization functions in per-input-tuple memory
2035 : : * context.
2036 : : */
2037 : 312 : oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
2038 : 312 : fcinfo->isnull = false;
2039 : 312 : *op->resvalue = FunctionCallInvoke(fcinfo);
2040 : 312 : *op->resnull = fcinfo->isnull;
2041 : 312 : MemoryContextSwitchTo(oldContext);
2042 : :
2043 : 312 : EEO_NEXT();
2044 : : }
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 : 165200 : EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS)
2053 : : {
2054 : 165200 : NullableDatum *args = op->d.agg_strict_input_check.args;
2055 : 165200 : int nargs = op->d.agg_strict_input_check.nargs;
2056 : :
2057 : : Assert(nargs > 1);
2058 : :
2059 [ + + ]: 495656 : for (int argno = 0; argno < nargs; argno++)
2060 : : {
2061 [ + + ]: 330484 : if (args[argno].isnull)
2062 : 28 : EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
2063 : : }
2064 : 165172 : EEO_NEXT();
2065 : : }
2066 : :
2067 : : /* special case for just one argument */
2068 : 3994927 : EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1)
2069 : : {
2070 : 3994927 : NullableDatum *args = op->d.agg_strict_input_check.args;
2071 : 3994927 : PG_USED_FOR_ASSERTS_ONLY int nargs = op->d.agg_strict_input_check.nargs;
2072 : :
2073 : : Assert(nargs == 1);
2074 : :
2075 [ + + ]: 3994927 : if (args[0].isnull)
2076 : 105990 : EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
2077 : 3888937 : EEO_NEXT();
2078 : : }
2079 : :
2080 : 251256 : EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
2081 : : {
2082 : 251256 : bool *nulls = op->d.agg_strict_input_check.nulls;
2083 : 251256 : int nargs = op->d.agg_strict_input_check.nargs;
2084 : :
2085 [ + + ]: 472512 : for (int argno = 0; argno < nargs; argno++)
2086 : : {
2087 [ + + ]: 251256 : if (nulls[argno])
2088 : 30000 : EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
2089 : : }
2090 : 221256 : EEO_NEXT();
2091 : : }
2092 : :
2093 : : /*
2094 : : * Check for a NULL pointer to the per-group states.
2095 : : */
2096 : :
2097 : 2522606 : EEO_CASE(EEOP_AGG_PLAIN_PERGROUP_NULLCHECK)
2098 : : {
2099 : 2522606 : AggState *aggstate = castNode(AggState, state->parent);
2100 : 2522606 : AggStatePerGroup pergroup_allaggs =
2101 : 2522606 : aggstate->all_pergroups[op->d.agg_plain_pergroup_nullcheck.setoff];
2102 : :
2103 [ + + ]: 2522606 : if (pergroup_allaggs == NULL)
2104 : 1739574 : EEO_JUMP(op->d.agg_plain_pergroup_nullcheck.jumpnull);
2105 : :
2106 : 783032 : EEO_NEXT();
2107 : : }
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 : 1091689 : EEO_CASE(EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL)
2122 : : {
2123 : 1091689 : AggState *aggstate = castNode(AggState, state->parent);
2124 : 1091689 : AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
2125 : 1091689 : AggStatePerGroup pergroup =
2126 : 1091689 : &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
2127 : :
2128 : : Assert(pertrans->transtypeByVal);
2129 : :
2130 [ + + ]: 1091689 : if (pergroup->noTransValue)
2131 : : {
2132 : : /* If transValue has not yet been initialized, do so now. */
2133 : 6625 : ExecAggInitGroup(aggstate, pertrans, pergroup,
2134 : : op->d.agg_trans.aggcontext);
2135 : : /* copied trans value from input, done this round */
2136 : : }
2137 [ + - ]: 1085064 : else if (likely(!pergroup->transValueIsNull))
2138 : : {
2139 : : /* invoke transition function, unless prevented by strictness */
2140 : 1085064 : ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
2141 : : op->d.agg_trans.aggcontext,
2142 : : op->d.agg_trans.setno);
2143 : : }
2144 : :
2145 : 1091689 : EEO_NEXT();
2146 : : }
2147 : :
2148 : : /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
2149 : 14855348 : EEO_CASE(EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL)
2150 : : {
2151 : 14855348 : AggState *aggstate = castNode(AggState, state->parent);
2152 : 14855348 : AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
2153 : 14855348 : AggStatePerGroup pergroup =
2154 : 14855348 : &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
2155 : :
2156 : : Assert(pertrans->transtypeByVal);
2157 : :
2158 [ + + ]: 14855348 : if (likely(!pergroup->transValueIsNull))
2159 : 14815336 : ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
2160 : : op->d.agg_trans.aggcontext,
2161 : : op->d.agg_trans.setno);
2162 : :
2163 : 14855348 : EEO_NEXT();
2164 : : }
2165 : :
2166 : : /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
2167 : 8290507 : EEO_CASE(EEOP_AGG_PLAIN_TRANS_BYVAL)
2168 : : {
2169 : 8290507 : AggState *aggstate = castNode(AggState, state->parent);
2170 : 8290507 : AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
2171 : 8290507 : AggStatePerGroup pergroup =
2172 : 8290507 : &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
2173 : :
2174 : : Assert(pertrans->transtypeByVal);
2175 : :
2176 : 8290507 : ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
2177 : : op->d.agg_trans.aggcontext,
2178 : : op->d.agg_trans.setno);
2179 : :
2180 : 8290455 : EEO_NEXT();
2181 : : }
2182 : :
2183 : : /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
2184 : 246346 : EEO_CASE(EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF)
2185 : : {
2186 : 246346 : AggState *aggstate = castNode(AggState, state->parent);
2187 : 246346 : AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
2188 : 246346 : AggStatePerGroup pergroup =
2189 : 246346 : &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
2190 : :
2191 : : Assert(!pertrans->transtypeByVal);
2192 : :
2193 [ + + ]: 246346 : if (pergroup->noTransValue)
2194 : 34305 : ExecAggInitGroup(aggstate, pertrans, pergroup,
2195 : : op->d.agg_trans.aggcontext);
2196 [ + - ]: 212041 : else if (likely(!pergroup->transValueIsNull))
2197 : 212041 : ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
2198 : : op->d.agg_trans.aggcontext,
2199 : : op->d.agg_trans.setno);
2200 : :
2201 : 246342 : EEO_NEXT();
2202 : : }
2203 : :
2204 : : /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
2205 : 1782239 : EEO_CASE(EEOP_AGG_PLAIN_TRANS_STRICT_BYREF)
2206 : : {
2207 : 1782239 : AggState *aggstate = castNode(AggState, state->parent);
2208 : 1782239 : AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
2209 : 1782239 : AggStatePerGroup pergroup =
2210 : 1782239 : &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
2211 : :
2212 : : Assert(!pertrans->transtypeByVal);
2213 : :
2214 [ + - ]: 1782239 : if (likely(!pergroup->transValueIsNull))
2215 : 1782239 : ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
2216 : : op->d.agg_trans.aggcontext,
2217 : : op->d.agg_trans.setno);
2218 : 1782239 : EEO_NEXT();
2219 : : }
2220 : :
2221 : : /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
2222 : 16301 : EEO_CASE(EEOP_AGG_PLAIN_TRANS_BYREF)
2223 : : {
2224 : 16301 : AggState *aggstate = castNode(AggState, state->parent);
2225 : 16301 : AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
2226 : 16301 : AggStatePerGroup pergroup =
2227 : 16301 : &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
2228 : :
2229 : : Assert(!pertrans->transtypeByVal);
2230 : :
2231 : 16301 : ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
2232 : : op->d.agg_trans.aggcontext,
2233 : : op->d.agg_trans.setno);
2234 : :
2235 : 16301 : EEO_NEXT();
2236 : : }
2237 : :
2238 : 243696 : EEO_CASE(EEOP_AGG_PRESORTED_DISTINCT_SINGLE)
2239 : : {
2240 : 243696 : AggStatePerTrans pertrans = op->d.agg_presorted_distinctcheck.pertrans;
2241 : 243696 : AggState *aggstate = castNode(AggState, state->parent);
2242 : :
2243 [ + + ]: 243696 : if (ExecEvalPreOrderedDistinctSingle(aggstate, pertrans))
2244 : 67729 : EEO_NEXT();
2245 : : else
2246 : 175967 : EEO_JUMP(op->d.agg_presorted_distinctcheck.jumpdistinct);
2247 : : }
2248 : :
2249 : 500 : EEO_CASE(EEOP_AGG_PRESORTED_DISTINCT_MULTI)
2250 : : {
2251 : 500 : AggState *aggstate = castNode(AggState, state->parent);
2252 : 500 : AggStatePerTrans pertrans = op->d.agg_presorted_distinctcheck.pertrans;
2253 : :
2254 [ + + ]: 500 : if (ExecEvalPreOrderedDistinctMulti(aggstate, pertrans))
2255 : 220 : EEO_NEXT();
2256 : : else
2257 : 280 : EEO_JUMP(op->d.agg_presorted_distinctcheck.jumpdistinct);
2258 : : }
2259 : :
2260 : : /* process single-column ordered aggregate datum */
2261 : 559741 : EEO_CASE(EEOP_AGG_ORDERED_TRANS_DATUM)
2262 : : {
2263 : : /* too complex for an inline implementation */
2264 : 559741 : ExecEvalAggOrderedTransDatum(state, op, econtext);
2265 : :
2266 : 559741 : EEO_NEXT();
2267 : : }
2268 : :
2269 : : /* process multi-column ordered aggregate tuple */
2270 : 168 : EEO_CASE(EEOP_AGG_ORDERED_TRANS_TUPLE)
2271 : : {
2272 : : /* too complex for an inline implementation */
2273 : 168 : ExecEvalAggOrderedTransTuple(state, op, econtext);
2274 : :
2275 : 168 : EEO_NEXT();
2276 : : }
2277 : :
2278 : 0 : EEO_CASE(EEOP_LAST)
2279 : : {
2280 : : /* unreachable */
2281 : : Assert(false);
2282 : 0 : goto out_error;
2283 : : }
2284 : : }
2285 : :
2286 : 0 : out_error:
2287 : 0 : pg_unreachable();
2288 : : return (Datum) 0;
2289 : : }
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 : 1203608 : ExecInterpExprStillValid(ExprState *state, ExprContext *econtext, bool *isNull)
2298 : : {
2299 : : /*
2300 : : * First time through, check whether attribute matches Var. Might not be
2301 : : * ok anymore, due to schema changes.
2302 : : */
2303 : 1203608 : CheckExprStillValid(state, econtext);
2304 : :
2305 : : /* skip the check during further executions */
2306 : 1203592 : state->evalfunc = (ExprStateEvalFunc) state->evalfunc_private;
2307 : :
2308 : : /* and actually execute */
2309 : 1203592 : return state->evalfunc(state, econtext, isNull);
2310 : : }
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 : 1203608 : CheckExprStillValid(ExprState *state, ExprContext *econtext)
2318 : : {
2319 : : TupleTableSlot *innerslot;
2320 : : TupleTableSlot *outerslot;
2321 : : TupleTableSlot *scanslot;
2322 : : TupleTableSlot *oldslot;
2323 : : TupleTableSlot *newslot;
2324 : :
2325 : 1203608 : innerslot = econtext->ecxt_innertuple;
2326 : 1203608 : outerslot = econtext->ecxt_outertuple;
2327 : 1203608 : scanslot = econtext->ecxt_scantuple;
2328 : 1203608 : oldslot = econtext->ecxt_oldtuple;
2329 : 1203608 : newslot = econtext->ecxt_newtuple;
2330 : :
2331 [ + + ]: 6676830 : for (int i = 0; i < state->steps_len; i++)
2332 : : {
2333 : 5473238 : ExprEvalStep *op = &state->steps[i];
2334 : :
2335 [ + + + + : 5473238 : switch (ExecEvalStepOp(state, op))
+ + ]
2336 : : {
2337 : 75629 : case EEOP_INNER_VAR:
2338 : : {
2339 : 75629 : int attnum = op->d.var.attnum;
2340 : :
2341 : 75629 : CheckVarSlotCompatibility(innerslot, attnum + 1, op->d.var.vartype);
2342 : 75629 : break;
2343 : : }
2344 : :
2345 : 168691 : case EEOP_OUTER_VAR:
2346 : : {
2347 : 168691 : int attnum = op->d.var.attnum;
2348 : :
2349 : 168691 : CheckVarSlotCompatibility(outerslot, attnum + 1, op->d.var.vartype);
2350 : 168691 : break;
2351 : : }
2352 : :
2353 : 221447 : case EEOP_SCAN_VAR:
2354 : : {
2355 : 221447 : int attnum = op->d.var.attnum;
2356 : :
2357 : 221447 : CheckVarSlotCompatibility(scanslot, attnum + 1, op->d.var.vartype);
2358 : 221431 : break;
2359 : : }
2360 : :
2361 : 167 : case EEOP_OLD_VAR:
2362 : : {
2363 : 167 : int attnum = op->d.var.attnum;
2364 : :
2365 : 167 : CheckVarSlotCompatibility(oldslot, attnum + 1, op->d.var.vartype);
2366 : 167 : break;
2367 : : }
2368 : :
2369 : 161 : case EEOP_NEW_VAR:
2370 : : {
2371 : 161 : int attnum = op->d.var.attnum;
2372 : :
2373 : 161 : CheckVarSlotCompatibility(newslot, attnum + 1, op->d.var.vartype);
2374 : 161 : break;
2375 : : }
2376 : 5007143 : default:
2377 : 5007143 : break;
2378 : : }
2379 : : }
2380 : 1203592 : }
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 : 466095 : CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype)
2389 : : {
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 [ + - ]: 466095 : if (attnum > 0)
2408 : : {
2409 : 466095 : TupleDesc slot_tupdesc = slot->tts_tupleDescriptor;
2410 : : Form_pg_attribute attr;
2411 : :
2412 [ - + ]: 466095 : if (attnum > slot_tupdesc->natts) /* should never happen */
2413 [ # # ]: 0 : elog(ERROR, "attribute number %d exceeds number of columns %d",
2414 : : attnum, slot_tupdesc->natts);
2415 : :
2416 : 466095 : attr = TupleDescAttr(slot_tupdesc, attnum - 1);
2417 : :
2418 : : /* Internal error: somebody forgot to expand it. */
2419 [ - + ]: 466095 : if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
2420 [ # # ]: 0 : elog(ERROR, "unexpected virtual generated column reference");
2421 : :
2422 [ + + ]: 466095 : if (attr->attisdropped)
2423 [ + - ]: 8 : ereport(ERROR,
2424 : : (errcode(ERRCODE_UNDEFINED_COLUMN),
2425 : : errmsg("attribute %d of type %s has been dropped",
2426 : : attnum, format_type_be(slot_tupdesc->tdtypeid))));
2427 : :
2428 [ + + ]: 466087 : if (vartype != attr->atttypid)
2429 [ + - ]: 8 : ereport(ERROR,
2430 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
2431 : : errmsg("attribute %d of type %s has wrong type",
2432 : : attnum, format_type_be(slot_tupdesc->tdtypeid)),
2433 : : errdetail("Table has type %s, but query expects %s.",
2434 : : format_type_be(attr->atttypid),
2435 : : format_type_be(vartype))));
2436 : : }
2437 : 466079 : }
2438 : :
2439 : : /*
2440 : : * Verify that the slot is compatible with a EEOP_*_FETCHSOME operation.
2441 : : */
2442 : : static void
2443 : 134959561 : CheckOpSlotCompatibility(ExprEvalStep *op, TupleTableSlot *slot)
2444 : : {
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 : 134959561 : }
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 : 1336765 : get_cached_rowtype(Oid type_id, int32 typmod,
2491 : : ExprEvalRowtypeCache *rowcache,
2492 : : bool *changed)
2493 : : {
2494 [ + + ]: 1336765 : if (type_id != RECORDOID)
2495 : : {
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 : 32225 : TypeCacheEntry *typentry = (TypeCacheEntry *) rowcache->cacheptr;
2504 : :
2505 [ + + - + : 32225 : if (unlikely(typentry == NULL ||
+ + - + +
+ ]
2506 : : rowcache->tupdesc_id == 0 ||
2507 : : typentry->tupDesc_identifier != rowcache->tupdesc_id))
2508 : : {
2509 : 8546 : typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
2510 [ - + ]: 8546 : if (typentry->tupDesc == NULL)
2511 [ # # ]: 0 : ereport(ERROR,
2512 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2513 : : errmsg("type %s is not composite",
2514 : : format_type_be(type_id))));
2515 : 8546 : rowcache->cacheptr = typentry;
2516 : 8546 : rowcache->tupdesc_id = typentry->tupDesc_identifier;
2517 [ + + ]: 8546 : if (changed)
2518 : 658 : *changed = true;
2519 : : }
2520 : 32225 : return typentry->tupDesc;
2521 : : }
2522 : : else
2523 : : {
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 : 1304540 : TupleDesc tupDesc = (TupleDesc) rowcache->cacheptr;
2531 : :
2532 [ + + - + : 1304540 : if (unlikely(tupDesc == NULL ||
+ + - + +
+ - + +
+ ]
2533 : : rowcache->tupdesc_id != 0 ||
2534 : : type_id != tupDesc->tdtypeid ||
2535 : : typmod != tupDesc->tdtypmod))
2536 : : {
2537 : 1389 : tupDesc = lookup_rowtype_tupdesc(type_id, typmod);
2538 : : /* Drop pin acquired by lookup_rowtype_tupdesc */
2539 [ + + ]: 1389 : ReleaseTupleDesc(tupDesc);
2540 : 1389 : rowcache->cacheptr = tupDesc;
2541 : 1389 : rowcache->tupdesc_id = 0; /* not a valid value for non-RECORD */
2542 [ - + ]: 1389 : if (changed)
2543 : 0 : *changed = true;
2544 : : }
2545 : 1304540 : return tupDesc;
2546 : : }
2547 : : }
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 : 5912147 : ExecJustVarImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
2557 : : {
2558 : 5912147 : ExprEvalStep *op = &state->steps[1];
2559 : 5912147 : int attnum = op->d.var.attnum + 1;
2560 : :
2561 : 5912147 : 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 : 5912147 : return slot_getattr(slot, attnum, isnull);
2569 : : }
2570 : :
2571 : : /* Simple reference to inner Var */
2572 : : static Datum
2573 : 3308258 : ExecJustInnerVar(ExprState *state, ExprContext *econtext, bool *isnull)
2574 : : {
2575 : 3308258 : return ExecJustVarImpl(state, econtext->ecxt_innertuple, isnull);
2576 : : }
2577 : :
2578 : : /* Simple reference to outer Var */
2579 : : static Datum
2580 : 2602871 : ExecJustOuterVar(ExprState *state, ExprContext *econtext, bool *isnull)
2581 : : {
2582 : 2602871 : return ExecJustVarImpl(state, econtext->ecxt_outertuple, isnull);
2583 : : }
2584 : :
2585 : : /* Simple reference to scan Var */
2586 : : static Datum
2587 : 1018 : ExecJustScanVar(ExprState *state, ExprContext *econtext, bool *isnull)
2588 : : {
2589 : 1018 : return ExecJustVarImpl(state, econtext->ecxt_scantuple, isnull);
2590 : : }
2591 : :
2592 : : /* implementation of ExecJustAssign(Inner|Outer|Scan)Var */
2593 : : static pg_always_inline Datum
2594 : 9225387 : ExecJustAssignVarImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull)
2595 : : {
2596 : 9225387 : ExprEvalStep *op = &state->steps[1];
2597 : 9225387 : int attnum = op->d.assign_var.attnum + 1;
2598 : 9225387 : int resultnum = op->d.assign_var.resultnum;
2599 : 9225387 : TupleTableSlot *outslot = state->resultslot;
2600 : :
2601 : 9225387 : 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 : : Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts);
2613 : 18450774 : outslot->tts_values[resultnum] =
2614 : 9225387 : slot_getattr(inslot, attnum, &outslot->tts_isnull[resultnum]);
2615 : 9225387 : return 0;
2616 : : }
2617 : :
2618 : : /* Evaluate inner Var and assign to appropriate column of result tuple */
2619 : : static Datum
2620 : 39514 : ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull)
2621 : : {
2622 : 39514 : return ExecJustAssignVarImpl(state, econtext->ecxt_innertuple, isnull);
2623 : : }
2624 : :
2625 : : /* Evaluate outer Var and assign to appropriate column of result tuple */
2626 : : static Datum
2627 : 764717 : ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull)
2628 : : {
2629 : 764717 : return ExecJustAssignVarImpl(state, econtext->ecxt_outertuple, isnull);
2630 : : }
2631 : :
2632 : : /* Evaluate scan Var and assign to appropriate column of result tuple */
2633 : : static Datum
2634 : 8421156 : ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull)
2635 : : {
2636 : 8421156 : return ExecJustAssignVarImpl(state, econtext->ecxt_scantuple, isnull);
2637 : : }
2638 : :
2639 : : /* Evaluate CASE_TESTVAL and apply a strict function to it */
2640 : : static Datum
2641 : 211017 : ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull)
2642 : : {
2643 : 211017 : ExprEvalStep *op = &state->steps[0];
2644 : : FunctionCallInfo fcinfo;
2645 : : NullableDatum *args;
2646 : : int nargs;
2647 : : 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 : 211017 : *op->resvalue = *op->d.casetest.value;
2654 : 211017 : *op->resnull = *op->d.casetest.isnull;
2655 : :
2656 : 211017 : op++;
2657 : :
2658 : 211017 : nargs = op->d.func.nargs;
2659 : 211017 : fcinfo = op->d.func.fcinfo_data;
2660 : 211017 : args = fcinfo->args;
2661 : :
2662 : : /* strict function, so check for NULL args */
2663 [ + + ]: 422288 : for (int argno = 0; argno < nargs; argno++)
2664 : : {
2665 [ + + ]: 211281 : if (args[argno].isnull)
2666 : : {
2667 : 10 : *isnull = true;
2668 : 10 : return (Datum) 0;
2669 : : }
2670 : : }
2671 : 211007 : fcinfo->isnull = false;
2672 : 211007 : d = op->d.func.fn_addr(fcinfo);
2673 : 210995 : *isnull = fcinfo->isnull;
2674 : 210995 : return d;
2675 : : }
2676 : :
2677 : : /* Simple Const expression */
2678 : : static Datum
2679 : 1322280 : ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull)
2680 : : {
2681 : 1322280 : ExprEvalStep *op = &state->steps[0];
2682 : :
2683 : 1322280 : *isnull = op->d.constval.isnull;
2684 : 1322280 : return op->d.constval.value;
2685 : : }
2686 : :
2687 : : /* implementation of ExecJust(Inner|Outer|Scan)VarVirt */
2688 : : static pg_always_inline Datum
2689 : 1224281 : ExecJustVarVirtImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
2690 : : {
2691 : 1224281 : ExprEvalStep *op = &state->steps[0];
2692 : 1224281 : 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 : : Assert(TTS_IS_VIRTUAL(slot));
2701 : : Assert(TTS_FIXED(slot));
2702 : : Assert(attnum >= 0 && attnum < slot->tts_nvalid);
2703 : :
2704 : 1224281 : *isnull = slot->tts_isnull[attnum];
2705 : :
2706 : 1224281 : return slot->tts_values[attnum];
2707 : : }
2708 : :
2709 : : /* Like ExecJustInnerVar, optimized for virtual slots */
2710 : : static Datum
2711 : 394411 : ExecJustInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
2712 : : {
2713 : 394411 : return ExecJustVarVirtImpl(state, econtext->ecxt_innertuple, isnull);
2714 : : }
2715 : :
2716 : : /* Like ExecJustOuterVar, optimized for virtual slots */
2717 : : static Datum
2718 : 829870 : ExecJustOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
2719 : : {
2720 : 829870 : return ExecJustVarVirtImpl(state, econtext->ecxt_outertuple, isnull);
2721 : : }
2722 : :
2723 : : /* Like ExecJustScanVar, optimized for virtual slots */
2724 : : static Datum
2725 : 0 : ExecJustScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
2726 : : {
2727 : 0 : return ExecJustVarVirtImpl(state, econtext->ecxt_scantuple, isnull);
2728 : : }
2729 : :
2730 : : /* implementation of ExecJustAssign(Inner|Outer|Scan)VarVirt */
2731 : : static pg_always_inline Datum
2732 : 3189151 : ExecJustAssignVarVirtImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull)
2733 : : {
2734 : 3189151 : ExprEvalStep *op = &state->steps[0];
2735 : 3189151 : int attnum = op->d.assign_var.attnum;
2736 : 3189151 : int resultnum = op->d.assign_var.resultnum;
2737 : 3189151 : TupleTableSlot *outslot = state->resultslot;
2738 : :
2739 : : /* see ExecJustVarVirtImpl for comments */
2740 : :
2741 : : Assert(TTS_IS_VIRTUAL(inslot));
2742 : : Assert(TTS_FIXED(inslot));
2743 : : Assert(attnum >= 0 && attnum < inslot->tts_nvalid);
2744 : : Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts);
2745 : :
2746 : 3189151 : outslot->tts_values[resultnum] = inslot->tts_values[attnum];
2747 : 3189151 : outslot->tts_isnull[resultnum] = inslot->tts_isnull[attnum];
2748 : :
2749 : 3189151 : return 0;
2750 : : }
2751 : :
2752 : : /* Like ExecJustAssignInnerVar, optimized for virtual slots */
2753 : : static Datum
2754 : 120824 : ExecJustAssignInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
2755 : : {
2756 : 120824 : return ExecJustAssignVarVirtImpl(state, econtext->ecxt_innertuple, isnull);
2757 : : }
2758 : :
2759 : : /* Like ExecJustAssignOuterVar, optimized for virtual slots */
2760 : : static Datum
2761 : 3011770 : ExecJustAssignOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
2762 : : {
2763 : 3011770 : return ExecJustAssignVarVirtImpl(state, econtext->ecxt_outertuple, isnull);
2764 : : }
2765 : :
2766 : : /* Like ExecJustAssignScanVar, optimized for virtual slots */
2767 : : static Datum
2768 : 56557 : ExecJustAssignScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
2769 : : {
2770 : 56557 : return ExecJustAssignVarVirtImpl(state, econtext->ecxt_scantuple, isnull);
2771 : : }
2772 : :
2773 : : /*
2774 : : * implementation for hashing an inner Var, seeding with an initial value.
2775 : : */
2776 : : static Datum
2777 : 1593288 : ExecJustHashInnerVarWithIV(ExprState *state, ExprContext *econtext,
2778 : : bool *isnull)
2779 : : {
2780 : 1593288 : ExprEvalStep *fetchop = &state->steps[0];
2781 : 1593288 : ExprEvalStep *setivop = &state->steps[1];
2782 : 1593288 : ExprEvalStep *innervar = &state->steps[2];
2783 : 1593288 : ExprEvalStep *hashop = &state->steps[3];
2784 : 1593288 : FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data;
2785 : 1593288 : int attnum = innervar->d.var.attnum;
2786 : : uint32 hashkey;
2787 : :
2788 : 1593288 : CheckOpSlotCompatibility(fetchop, econtext->ecxt_innertuple);
2789 : 1593288 : slot_getsomeattrs(econtext->ecxt_innertuple, fetchop->d.fetch.last_var);
2790 : :
2791 : 1593288 : fcinfo->args[0].value = econtext->ecxt_innertuple->tts_values[attnum];
2792 : 1593288 : fcinfo->args[0].isnull = econtext->ecxt_innertuple->tts_isnull[attnum];
2793 : :
2794 : 1593288 : hashkey = DatumGetUInt32(setivop->d.hashdatum_initvalue.init_value);
2795 : 1593288 : hashkey = pg_rotate_left32(hashkey, 1);
2796 : :
2797 [ + + ]: 1593288 : if (!fcinfo->args[0].isnull)
2798 : : {
2799 : : uint32 hashvalue;
2800 : :
2801 : 1592688 : hashvalue = DatumGetUInt32(hashop->d.hashdatum.fn_addr(fcinfo));
2802 : 1592688 : hashkey = hashkey ^ hashvalue;
2803 : : }
2804 : :
2805 : 1593288 : *isnull = false;
2806 : 1593288 : return UInt32GetDatum(hashkey);
2807 : : }
2808 : :
2809 : : /* implementation of ExecJustHash(Inner|Outer)Var */
2810 : : static pg_always_inline Datum
2811 : 1992257 : ExecJustHashVarImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
2812 : : {
2813 : 1992257 : ExprEvalStep *fetchop = &state->steps[0];
2814 : 1992257 : ExprEvalStep *var = &state->steps[1];
2815 : 1992257 : ExprEvalStep *hashop = &state->steps[2];
2816 : 1992257 : FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data;
2817 : 1992257 : int attnum = var->d.var.attnum;
2818 : :
2819 : 1992257 : CheckOpSlotCompatibility(fetchop, slot);
2820 : 1992257 : slot_getsomeattrs(slot, fetchop->d.fetch.last_var);
2821 : :
2822 : 1992257 : fcinfo->args[0].value = slot->tts_values[attnum];
2823 : 1992257 : fcinfo->args[0].isnull = slot->tts_isnull[attnum];
2824 : :
2825 : 1992257 : *isnull = false;
2826 : :
2827 [ + + ]: 1992257 : if (!fcinfo->args[0].isnull)
2828 : 1986003 : return hashop->d.hashdatum.fn_addr(fcinfo);
2829 : : else
2830 : 6254 : return (Datum) 0;
2831 : : }
2832 : :
2833 : : /* implementation for hashing an outer Var */
2834 : : static Datum
2835 : 0 : ExecJustHashOuterVar(ExprState *state, ExprContext *econtext, bool *isnull)
2836 : : {
2837 : 0 : return ExecJustHashVarImpl(state, econtext->ecxt_outertuple, isnull);
2838 : : }
2839 : :
2840 : : /* implementation for hashing an inner Var */
2841 : : static Datum
2842 : 1992257 : ExecJustHashInnerVar(ExprState *state, ExprContext *econtext, bool *isnull)
2843 : : {
2844 : 1992257 : return ExecJustHashVarImpl(state, econtext->ecxt_innertuple, isnull);
2845 : : }
2846 : :
2847 : : /* implementation of ExecJustHash(Inner|Outer)VarVirt */
2848 : : static pg_always_inline Datum
2849 : 849365 : ExecJustHashVarVirtImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
2850 : : {
2851 : 849365 : ExprEvalStep *var = &state->steps[0];
2852 : 849365 : ExprEvalStep *hashop = &state->steps[1];
2853 : 849365 : FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data;
2854 : 849365 : int attnum = var->d.var.attnum;
2855 : :
2856 : 849365 : fcinfo->args[0].value = slot->tts_values[attnum];
2857 : 849365 : fcinfo->args[0].isnull = slot->tts_isnull[attnum];
2858 : :
2859 : 849365 : *isnull = false;
2860 : :
2861 [ + + ]: 849365 : if (!fcinfo->args[0].isnull)
2862 : 849357 : return hashop->d.hashdatum.fn_addr(fcinfo);
2863 : : else
2864 : 8 : return (Datum) 0;
2865 : : }
2866 : :
2867 : : /* Like ExecJustHashInnerVar, optimized for virtual slots */
2868 : : static Datum
2869 : 849365 : ExecJustHashInnerVarVirt(ExprState *state, ExprContext *econtext,
2870 : : bool *isnull)
2871 : : {
2872 : 849365 : return ExecJustHashVarVirtImpl(state, econtext->ecxt_innertuple, isnull);
2873 : : }
2874 : :
2875 : : /* Like ExecJustHashOuterVar, optimized for virtual slots */
2876 : : static Datum
2877 : 0 : ExecJustHashOuterVarVirt(ExprState *state, ExprContext *econtext,
2878 : : bool *isnull)
2879 : : {
2880 : 0 : return ExecJustHashVarVirtImpl(state, econtext->ecxt_outertuple, isnull);
2881 : : }
2882 : :
2883 : : /*
2884 : : * implementation for hashing an outer Var. Returns NULL on NULL input.
2885 : : */
2886 : : static Datum
2887 : 8084305 : ExecJustHashOuterVarStrict(ExprState *state, ExprContext *econtext,
2888 : : bool *isnull)
2889 : : {
2890 : 8084305 : ExprEvalStep *fetchop = &state->steps[0];
2891 : 8084305 : ExprEvalStep *var = &state->steps[1];
2892 : 8084305 : ExprEvalStep *hashop = &state->steps[2];
2893 : 8084305 : FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data;
2894 : 8084305 : int attnum = var->d.var.attnum;
2895 : :
2896 : 8084305 : CheckOpSlotCompatibility(fetchop, econtext->ecxt_outertuple);
2897 : 8084305 : slot_getsomeattrs(econtext->ecxt_outertuple, fetchop->d.fetch.last_var);
2898 : :
2899 : 8084305 : fcinfo->args[0].value = econtext->ecxt_outertuple->tts_values[attnum];
2900 : 8084305 : fcinfo->args[0].isnull = econtext->ecxt_outertuple->tts_isnull[attnum];
2901 : :
2902 [ + + ]: 8084305 : if (!fcinfo->args[0].isnull)
2903 : : {
2904 : 8084011 : *isnull = false;
2905 : 8084011 : return hashop->d.hashdatum.fn_addr(fcinfo);
2906 : : }
2907 : : else
2908 : : {
2909 : : /* return NULL on NULL input */
2910 : 294 : *isnull = true;
2911 : 294 : return (Datum) 0;
2912 : : }
2913 : : }
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 : 37046819 : dispatch_compare_ptr(const void *a, const void *b)
2922 : : {
2923 : 37046819 : const ExprEvalOpLookup *la = (const ExprEvalOpLookup *) a;
2924 : 37046819 : const ExprEvalOpLookup *lb = (const ExprEvalOpLookup *) b;
2925 : :
2926 [ + + ]: 37046819 : if (la->opcode < lb->opcode)
2927 : 22498422 : return -1;
2928 [ + + ]: 14548397 : else if (la->opcode > lb->opcode)
2929 : 9737576 : return 1;
2930 : 4810821 : return 0;
2931 : : }
2932 : : #endif
2933 : :
2934 : : /*
2935 : : * Do one-time initialization of interpretation machinery.
2936 : : */
2937 : : static void
2938 : 1631876 : ExecInitInterpreter(void)
2939 : : {
2940 : : #if defined(EEO_USE_COMPUTED_GOTO)
2941 : : /* Set up externally-visible pointer to dispatch table */
2942 [ + + ]: 1631876 : if (dispatch_table == NULL)
2943 : : {
2944 : 14303 : dispatch_table = (const void **)
2945 : 14303 : DatumGetPointer(ExecInterpExpr(NULL, NULL, NULL));
2946 : :
2947 : : /* build reverse lookup table */
2948 [ + + ]: 1730663 : for (int i = 0; i < EEOP_LAST; i++)
2949 : : {
2950 : 1716360 : reverse_dispatch_table[i].opcode = dispatch_table[i];
2951 : 1716360 : reverse_dispatch_table[i].op = (ExprEvalOp) i;
2952 : : }
2953 : :
2954 : : /* make it bsearch()able */
2955 : 14303 : qsort(reverse_dispatch_table,
2956 : : EEOP_LAST /* nmembers */ ,
2957 : : sizeof(ExprEvalOpLookup),
2958 : : dispatch_compare_ptr);
2959 : : }
2960 : : #endif
2961 : 1631876 : }
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 : 5473238 : ExecEvalStepOp(ExprState *state, ExprEvalStep *op)
2971 : : {
2972 : : #if defined(EEO_USE_COMPUTED_GOTO)
2973 [ + + ]: 5473238 : if (state->flags & EEO_FLAG_DIRECT_THREADED)
2974 : : {
2975 : : ExprEvalOpLookup key;
2976 : : ExprEvalOpLookup *res;
2977 : :
2978 : 4810821 : key.opcode = (void *) op->opcode;
2979 : 4810821 : res = bsearch(&key,
2980 : : reverse_dispatch_table,
2981 : : EEOP_LAST /* nmembers */ ,
2982 : : sizeof(ExprEvalOpLookup),
2983 : : dispatch_compare_ptr);
2984 : : Assert(res); /* unknown ops shouldn't get looked up */
2985 : 4810821 : return res->op;
2986 : : }
2987 : : #endif
2988 : 662417 : return (ExprEvalOp) op->opcode;
2989 : : }
2990 : :
2991 : :
2992 : : /*
2993 : : * Out-of-line helper functions for complex instructions.
2994 : : */
2995 : :
2996 : : /*
2997 : : * Evaluate EEOP_FUNCEXPR_FUSAGE
2998 : : */
2999 : : void
3000 : 112 : ExecEvalFuncExprFusage(ExprState *state, ExprEvalStep *op,
3001 : : ExprContext *econtext)
3002 : : {
3003 : 112 : FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
3004 : : PgStat_FunctionCallUsage fcusage;
3005 : : Datum d;
3006 : :
3007 : 112 : pgstat_init_function_usage(fcinfo, &fcusage);
3008 : :
3009 : 112 : fcinfo->isnull = false;
3010 : 112 : d = op->d.func.fn_addr(fcinfo);
3011 : 112 : *op->resvalue = d;
3012 : 112 : *op->resnull = fcinfo->isnull;
3013 : :
3014 : 112 : pgstat_end_function_usage(&fcusage, true);
3015 : 112 : }
3016 : :
3017 : : /*
3018 : : * Evaluate EEOP_FUNCEXPR_STRICT_FUSAGE
3019 : : */
3020 : : void
3021 : 4 : ExecEvalFuncExprStrictFusage(ExprState *state, ExprEvalStep *op,
3022 : : ExprContext *econtext)
3023 : : {
3024 : :
3025 : 4 : FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
3026 : : PgStat_FunctionCallUsage fcusage;
3027 : 4 : NullableDatum *args = fcinfo->args;
3028 : 4 : int nargs = op->d.func.nargs;
3029 : : Datum d;
3030 : :
3031 : : /* strict function, so check for NULL args */
3032 [ + + ]: 12 : for (int argno = 0; argno < nargs; argno++)
3033 : : {
3034 [ - + ]: 8 : if (args[argno].isnull)
3035 : : {
3036 : 0 : *op->resnull = true;
3037 : 0 : return;
3038 : : }
3039 : : }
3040 : :
3041 : 4 : pgstat_init_function_usage(fcinfo, &fcusage);
3042 : :
3043 : 4 : fcinfo->isnull = false;
3044 : 4 : d = op->d.func.fn_addr(fcinfo);
3045 : 4 : *op->resvalue = d;
3046 : 4 : *op->resnull = fcinfo->isnull;
3047 : :
3048 : 4 : pgstat_end_function_usage(&fcusage, true);
3049 : : }
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 : 5974649 : ExecEvalParamExec(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3059 : : {
3060 : : ParamExecData *prm;
3061 : :
3062 : 5974649 : prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]);
3063 [ + + ]: 5974649 : if (unlikely(prm->execPlan != NULL))
3064 : : {
3065 : : /* Parameter not evaluated yet, so go do it */
3066 : 6705 : ExecSetParamPlan(prm->execPlan, econtext);
3067 : : /* ExecSetParamPlan should have processed this param... */
3068 : : Assert(prm->execPlan == NULL);
3069 : : }
3070 : 5974638 : *op->resvalue = prm->value;
3071 : 5974638 : *op->resnull = prm->isnull;
3072 : 5974638 : }
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 : 293689 : ExecEvalParamExtern(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3081 : : {
3082 : 293689 : ParamListInfo paramInfo = econtext->ecxt_param_list_info;
3083 : 293689 : int paramId = op->d.param.paramid;
3084 : :
3085 [ + - + - : 293689 : if (likely(paramInfo &&
+ - + - +
- ]
3086 : : paramId > 0 && paramId <= paramInfo->numParams))
3087 : : {
3088 : : ParamExternData *prm;
3089 : : ParamExternData prmdata;
3090 : :
3091 : : /* give hook a chance in case parameter is dynamic */
3092 [ + + ]: 293689 : if (paramInfo->paramFetch != NULL)
3093 : 93 : prm = paramInfo->paramFetch(paramInfo, paramId, false, &prmdata);
3094 : : else
3095 : 293596 : prm = ¶mInfo->params[paramId - 1];
3096 : :
3097 [ + - ]: 293689 : if (likely(OidIsValid(prm->ptype)))
3098 : : {
3099 : : /* safety check in case hook did something unexpected */
3100 [ - + ]: 293689 : if (unlikely(prm->ptype != op->d.param.paramtype))
3101 [ # # ]: 0 : ereport(ERROR,
3102 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
3103 : : errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
3104 : : paramId,
3105 : : format_type_be(prm->ptype),
3106 : : format_type_be(op->d.param.paramtype))));
3107 : 293689 : *op->resvalue = prm->value;
3108 : 293689 : *op->resnull = prm->isnull;
3109 : 293689 : return;
3110 : : }
3111 : : }
3112 : :
3113 [ # # ]: 0 : ereport(ERROR,
3114 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
3115 : : errmsg("no value found for parameter %d", paramId)));
3116 : : }
3117 : :
3118 : : /*
3119 : : * Set value of a param (currently always PARAM_EXEC) from
3120 : : * op->res{value,null}.
3121 : : */
3122 : : void
3123 : 1035193 : ExecEvalParamSet(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3124 : : {
3125 : : ParamExecData *prm;
3126 : :
3127 : 1035193 : prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]);
3128 : :
3129 : : /* Shouldn't have a pending evaluation anymore */
3130 : : Assert(prm->execPlan == NULL);
3131 : :
3132 : 1035193 : prm->value = *op->resvalue;
3133 : 1035193 : prm->isnull = *op->resnull;
3134 : 1035193 : }
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 : 36 : ExecEvalCoerceViaIOSafe(ExprState *state, ExprEvalStep *op)
3146 : : {
3147 : : char *str;
3148 : :
3149 : : /* call output function (similar to OutputFunctionCall) */
3150 [ - + ]: 36 : if (*op->resnull)
3151 : : {
3152 : : /* output functions are not called on nulls */
3153 : 0 : str = NULL;
3154 : : }
3155 : : else
3156 : : {
3157 : : FunctionCallInfo fcinfo_out;
3158 : :
3159 : 36 : fcinfo_out = op->d.iocoerce.fcinfo_data_out;
3160 : 36 : fcinfo_out->args[0].value = *op->resvalue;
3161 : 36 : fcinfo_out->args[0].isnull = false;
3162 : :
3163 : 36 : fcinfo_out->isnull = false;
3164 : 36 : str = DatumGetCString(FunctionCallInvoke(fcinfo_out));
3165 : :
3166 : : /* OutputFunctionCall assumes result isn't null */
3167 : : Assert(!fcinfo_out->isnull);
3168 : : }
3169 : :
3170 : : /* call input function (similar to InputFunctionCallSafe) */
3171 [ + - + - ]: 36 : if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL)
3172 : : {
3173 : : FunctionCallInfo fcinfo_in;
3174 : :
3175 : 36 : fcinfo_in = op->d.iocoerce.fcinfo_data_in;
3176 : 36 : fcinfo_in->args[0].value = PointerGetDatum(str);
3177 : 36 : fcinfo_in->args[0].isnull = *op->resnull;
3178 : : /* second and third arguments are already set up */
3179 : :
3180 : : /* ErrorSaveContext must be present. */
3181 : : Assert(IsA(fcinfo_in->context, ErrorSaveContext));
3182 : :
3183 : 36 : fcinfo_in->isnull = false;
3184 : 36 : *op->resvalue = FunctionCallInvoke(fcinfo_in);
3185 : :
3186 [ + - + - : 36 : if (SOFT_ERROR_OCCURRED(fcinfo_in->context))
+ + ]
3187 : : {
3188 : 20 : *op->resnull = true;
3189 : 20 : *op->resvalue = (Datum) 0;
3190 : 20 : return;
3191 : : }
3192 : :
3193 : : /* Should get null result if and only if str is NULL */
3194 : : if (str == NULL)
3195 : : Assert(*op->resnull);
3196 : : else
3197 : : Assert(!*op->resnull);
3198 : : }
3199 : : }
3200 : :
3201 : : /*
3202 : : * Evaluate a SQLValueFunction expression.
3203 : : */
3204 : : void
3205 : 7180 : ExecEvalSQLValueFunction(ExprState *state, ExprEvalStep *op)
3206 : : {
3207 : 7180 : LOCAL_FCINFO(fcinfo, 0);
3208 : 7180 : SQLValueFunction *svf = op->d.sqlvaluefunction.svf;
3209 : :
3210 : 7180 : *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 [ + + + + : 7180 : switch (svf->op)
+ + + + +
- ]
3217 : : {
3218 : 33 : case SVFOP_CURRENT_DATE:
3219 : 33 : *op->resvalue = DateADTGetDatum(GetSQLCurrentDate());
3220 : 33 : break;
3221 : 16 : case SVFOP_CURRENT_TIME:
3222 : : case SVFOP_CURRENT_TIME_N:
3223 : 16 : *op->resvalue = TimeTzADTPGetDatum(GetSQLCurrentTime(svf->typmod));
3224 : 16 : break;
3225 : 197 : case SVFOP_CURRENT_TIMESTAMP:
3226 : : case SVFOP_CURRENT_TIMESTAMP_N:
3227 : 197 : *op->resvalue = TimestampTzGetDatum(GetSQLCurrentTimestamp(svf->typmod));
3228 : 197 : break;
3229 : 16 : case SVFOP_LOCALTIME:
3230 : : case SVFOP_LOCALTIME_N:
3231 : 16 : *op->resvalue = TimeADTGetDatum(GetSQLLocalTime(svf->typmod));
3232 : 16 : break;
3233 : 50 : case SVFOP_LOCALTIMESTAMP:
3234 : : case SVFOP_LOCALTIMESTAMP_N:
3235 : 50 : *op->resvalue = TimestampGetDatum(GetSQLLocalTimestamp(svf->typmod));
3236 : 50 : break;
3237 : 6516 : case SVFOP_CURRENT_ROLE:
3238 : : case SVFOP_CURRENT_USER:
3239 : : case SVFOP_USER:
3240 : 6516 : InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
3241 : 6516 : *op->resvalue = current_user(fcinfo);
3242 : 6516 : *op->resnull = fcinfo->isnull;
3243 : 6516 : break;
3244 : 314 : case SVFOP_SESSION_USER:
3245 : 314 : InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
3246 : 314 : *op->resvalue = session_user(fcinfo);
3247 : 314 : *op->resnull = fcinfo->isnull;
3248 : 314 : break;
3249 : 26 : case SVFOP_CURRENT_CATALOG:
3250 : 26 : InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
3251 : 26 : *op->resvalue = current_database(fcinfo);
3252 : 26 : *op->resnull = fcinfo->isnull;
3253 : 26 : break;
3254 : 12 : case SVFOP_CURRENT_SCHEMA:
3255 : 12 : InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
3256 : 12 : *op->resvalue = current_schema(fcinfo);
3257 : 12 : *op->resnull = fcinfo->isnull;
3258 : 12 : break;
3259 : : }
3260 : 7180 : }
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 : 1 : ExecEvalCurrentOfExpr(ExprState *state, ExprEvalStep *op)
3273 : : {
3274 [ + - ]: 1 : ereport(ERROR,
3275 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3276 : : errmsg("WHERE CURRENT OF is not supported for this table type")));
3277 : : }
3278 : :
3279 : : /*
3280 : : * Evaluate NextValueExpr.
3281 : : */
3282 : : void
3283 : 1910 : ExecEvalNextValueExpr(ExprState *state, ExprEvalStep *op)
3284 : : {
3285 : 1910 : int64 newval = nextval_internal(op->d.nextvalueexpr.seqid, false);
3286 : :
3287 [ + + + - ]: 1910 : switch (op->d.nextvalueexpr.seqtypid)
3288 : : {
3289 : 20 : case INT2OID:
3290 : 20 : *op->resvalue = Int16GetDatum((int16) newval);
3291 : 20 : break;
3292 : 1838 : case INT4OID:
3293 : 1838 : *op->resvalue = Int32GetDatum((int32) newval);
3294 : 1838 : break;
3295 : 52 : case INT8OID:
3296 : 52 : *op->resvalue = Int64GetDatum(newval);
3297 : 52 : break;
3298 : 0 : default:
3299 [ # # ]: 0 : elog(ERROR, "unsupported sequence type %u",
3300 : : op->d.nextvalueexpr.seqtypid);
3301 : : }
3302 : 1910 : *op->resnull = false;
3303 : 1910 : }
3304 : :
3305 : : /*
3306 : : * Evaluate NullTest / IS NULL for rows.
3307 : : */
3308 : : void
3309 : 475 : ExecEvalRowNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3310 : : {
3311 : 475 : ExecEvalRowNullInt(state, op, econtext, true);
3312 : 475 : }
3313 : :
3314 : : /*
3315 : : * Evaluate NullTest / IS NOT NULL for rows.
3316 : : */
3317 : : void
3318 : 353 : ExecEvalRowNotNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3319 : : {
3320 : 353 : ExecEvalRowNullInt(state, op, econtext, false);
3321 : 353 : }
3322 : :
3323 : : /* Common code for IS [NOT] NULL on a row value */
3324 : : static void
3325 : 828 : ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op,
3326 : : ExprContext *econtext, bool checkisnull)
3327 : : {
3328 : 828 : Datum value = *op->resvalue;
3329 : 828 : bool isnull = *op->resnull;
3330 : : HeapTupleHeader tuple;
3331 : : Oid tupType;
3332 : : int32 tupTypmod;
3333 : : TupleDesc tupDesc;
3334 : : HeapTupleData tmptup;
3335 : :
3336 : 828 : *op->resnull = false;
3337 : :
3338 : : /* NULL row variables are treated just as NULL scalar columns */
3339 [ + + ]: 828 : if (isnull)
3340 : : {
3341 : 120 : *op->resvalue = BoolGetDatum(checkisnull);
3342 : 500 : return;
3343 : : }
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 : 708 : tuple = DatumGetHeapTupleHeader(value);
3362 : :
3363 : 708 : tupType = HeapTupleHeaderGetTypeId(tuple);
3364 : 708 : tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3365 : :
3366 : : /* Lookup tupdesc if first time through or if type changes */
3367 : 708 : tupDesc = get_cached_rowtype(tupType, tupTypmod,
3368 : : &op->d.nulltest_row.rowcache, NULL);
3369 : :
3370 : : /*
3371 : : * heap_attisnull needs a HeapTuple not a bare HeapTupleHeader.
3372 : : */
3373 : 708 : tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3374 : 708 : 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 [ + + ]: 1689 : for (int att = 1; att <= tupDesc->natts; att++)
3384 : : {
3385 : : /* ignore dropped columns */
3386 [ - + ]: 1361 : if (TupleDescCompactAttr(tupDesc, att - 1)->attisdropped)
3387 : 0 : continue;
3388 [ + + ]: 1361 : if (heap_attisnull(&tmptup, att, tupDesc))
3389 : : {
3390 : : /* null field disproves IS NOT NULL */
3391 [ + + ]: 44 : if (!checkisnull)
3392 : : {
3393 : 28 : *op->resvalue = BoolGetDatum(false);
3394 : 28 : return;
3395 : : }
3396 : : }
3397 : : else
3398 : : {
3399 : : /* non-null field disproves IS NULL */
3400 [ + + ]: 1317 : if (checkisnull)
3401 : : {
3402 : 352 : *op->resvalue = BoolGetDatum(false);
3403 : 352 : return;
3404 : : }
3405 : : }
3406 : : }
3407 : :
3408 : 328 : *op->resvalue = BoolGetDatum(true);
3409 : : }
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 : 573047 : ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op)
3419 : : {
3420 : : ArrayType *result;
3421 : 573047 : Oid element_type = op->d.arrayexpr.elemtype;
3422 : 573047 : int nelems = op->d.arrayexpr.nelems;
3423 : 573047 : int ndims = 0;
3424 : : int dims[MAXDIM];
3425 : : int lbs[MAXDIM];
3426 : :
3427 : : /* Set non-null as default */
3428 : 573047 : *op->resnull = false;
3429 : :
3430 [ + + ]: 573047 : if (!op->d.arrayexpr.multidims)
3431 : : {
3432 : : /* Elements are presumably of scalar type */
3433 : 572686 : Datum *dvalues = op->d.arrayexpr.elemvalues;
3434 : 572686 : bool *dnulls = op->d.arrayexpr.elemnulls;
3435 : :
3436 : : /* setup for 1-D array of the given length */
3437 : 572686 : ndims = 1;
3438 : 572686 : dims[0] = nelems;
3439 : 572686 : lbs[0] = 1;
3440 : :
3441 : 572686 : result = construct_md_array(dvalues, dnulls, ndims, dims, lbs,
3442 : : element_type,
3443 : 572686 : op->d.arrayexpr.elemlength,
3444 : 572686 : op->d.arrayexpr.elembyval,
3445 : 572686 : op->d.arrayexpr.elemalign);
3446 : : }
3447 : : else
3448 : : {
3449 : : /* Must be nested array expressions */
3450 : 361 : int nbytes = 0;
3451 : : int nitems;
3452 : 361 : int outer_nelems = 0;
3453 : 361 : int elem_ndims = 0;
3454 : 361 : int *elem_dims = NULL;
3455 : 361 : int *elem_lbs = NULL;
3456 : 361 : bool firstone = true;
3457 : 361 : bool havenulls = false;
3458 : 361 : bool haveempty = false;
3459 : : char **subdata;
3460 : : uint8 **subbitmaps;
3461 : : int *subbytes;
3462 : : int *subnitems;
3463 : : int32 dataoffset;
3464 : : char *dat;
3465 : : int iitem;
3466 : :
3467 : 361 : subdata = palloc_array(char *, nelems);
3468 : 361 : subbitmaps = palloc_array(uint8 *, nelems);
3469 : 361 : subbytes = palloc_array(int, nelems);
3470 : 361 : subnitems = palloc_array(int, nelems);
3471 : :
3472 : : /* loop through and get data area from each element */
3473 [ + + ]: 1006 : for (int elemoff = 0; elemoff < nelems; elemoff++)
3474 : : {
3475 : : Datum arraydatum;
3476 : : bool eisnull;
3477 : : ArrayType *array;
3478 : : int this_ndims;
3479 : :
3480 : 645 : arraydatum = op->d.arrayexpr.elemvalues[elemoff];
3481 : 645 : eisnull = op->d.arrayexpr.elemnulls[elemoff];
3482 : :
3483 : : /* temporarily ignore null subarrays */
3484 [ - + ]: 645 : if (eisnull)
3485 : : {
3486 : 0 : haveempty = true;
3487 : 0 : continue;
3488 : : }
3489 : :
3490 : 645 : array = DatumGetArrayTypeP(arraydatum);
3491 : :
3492 : : /* run-time double-check on element type */
3493 [ - + ]: 645 : if (element_type != ARR_ELEMTYPE(array))
3494 [ # # ]: 0 : ereport(ERROR,
3495 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
3496 : : errmsg("cannot merge incompatible arrays"),
3497 : : errdetail("Array with element type %s cannot be "
3498 : : "included in ARRAY construct with element type %s.",
3499 : : format_type_be(ARR_ELEMTYPE(array)),
3500 : : format_type_be(element_type))));
3501 : :
3502 : 645 : this_ndims = ARR_NDIM(array);
3503 : : /* temporarily ignore zero-dimensional subarrays */
3504 [ - + ]: 645 : if (this_ndims <= 0)
3505 : : {
3506 : 0 : haveempty = true;
3507 : 0 : continue;
3508 : : }
3509 : :
3510 [ + + ]: 645 : if (firstone)
3511 : : {
3512 : : /* Get sub-array details from first member */
3513 : 361 : elem_ndims = this_ndims;
3514 : 361 : ndims = elem_ndims + 1;
3515 [ + - - + ]: 361 : if (ndims <= 0 || ndims > MAXDIM)
3516 [ # # ]: 0 : ereport(ERROR,
3517 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3518 : : errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
3519 : : ndims, MAXDIM)));
3520 : :
3521 : 361 : elem_dims = palloc_array(int, elem_ndims);
3522 : 361 : memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int));
3523 : 361 : elem_lbs = palloc_array(int, elem_ndims);
3524 : 361 : memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int));
3525 : :
3526 : 361 : firstone = false;
3527 : : }
3528 : : else
3529 : : {
3530 : : /* Check other sub-arrays are compatible */
3531 [ + - ]: 284 : if (elem_ndims != this_ndims ||
3532 [ + - ]: 284 : memcmp(elem_dims, ARR_DIMS(array),
3533 : 284 : elem_ndims * sizeof(int)) != 0 ||
3534 [ - + ]: 284 : memcmp(elem_lbs, ARR_LBOUND(array),
3535 : : elem_ndims * sizeof(int)) != 0)
3536 [ # # ]: 0 : ereport(ERROR,
3537 : : (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3538 : : errmsg("multidimensional arrays must have array "
3539 : : "expressions with matching dimensions")));
3540 : : }
3541 : :
3542 [ + + ]: 645 : subdata[outer_nelems] = ARR_DATA_PTR(array);
3543 [ + + ]: 645 : subbitmaps[outer_nelems] = ARR_NULLBITMAP(array);
3544 [ + + ]: 645 : subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
3545 : 645 : nbytes += subbytes[outer_nelems];
3546 : : /* check for overflow of total request */
3547 [ - + ]: 645 : if (!AllocSizeIsValid(nbytes))
3548 [ # # ]: 0 : ereport(ERROR,
3549 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3550 : : errmsg("array size exceeds the maximum allowed (%d)",
3551 : : (int) MaxAllocSize)));
3552 : 645 : subnitems[outer_nelems] = ArrayGetNItems(this_ndims,
3553 : : ARR_DIMS(array));
3554 : 645 : havenulls |= ARR_HASNULL(array);
3555 : 645 : outer_nelems++;
3556 : : }
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 [ - + ]: 361 : if (haveempty)
3565 : : {
3566 [ # # ]: 0 : if (ndims == 0) /* didn't find any nonempty array */
3567 : : {
3568 : 0 : *op->resvalue = PointerGetDatum(construct_empty_array(element_type));
3569 : 0 : return;
3570 : : }
3571 [ # # ]: 0 : ereport(ERROR,
3572 : : (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3573 : : errmsg("multidimensional arrays must have array "
3574 : : "expressions with matching dimensions")));
3575 : : }
3576 : :
3577 : : /* setup for multi-D array */
3578 : 361 : dims[0] = outer_nelems;
3579 : 361 : lbs[0] = 1;
3580 [ + + ]: 894 : for (int i = 1; i < ndims; i++)
3581 : : {
3582 : 533 : dims[i] = elem_dims[i - 1];
3583 : 533 : lbs[i] = elem_lbs[i - 1];
3584 : : }
3585 : :
3586 : : /* check for subscript overflow */
3587 : 361 : nitems = ArrayGetNItems(ndims, dims);
3588 : 361 : ArrayCheckBounds(ndims, dims, lbs);
3589 : :
3590 [ + + ]: 361 : if (havenulls)
3591 : : {
3592 : 17 : dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
3593 : 17 : nbytes += dataoffset;
3594 : : }
3595 : : else
3596 : : {
3597 : 344 : dataoffset = 0; /* marker for no null bitmap */
3598 : 344 : nbytes += ARR_OVERHEAD_NONULLS(ndims);
3599 : : }
3600 : :
3601 : 361 : result = (ArrayType *) palloc0(nbytes);
3602 : 361 : SET_VARSIZE(result, nbytes);
3603 : 361 : result->ndim = ndims;
3604 : 361 : result->dataoffset = dataoffset;
3605 : 361 : result->elemtype = element_type;
3606 : 361 : memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
3607 : 361 : memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
3608 : :
3609 [ + + ]: 361 : dat = ARR_DATA_PTR(result);
3610 : 361 : iitem = 0;
3611 [ + + ]: 1006 : for (int i = 0; i < outer_nelems; i++)
3612 : : {
3613 : 645 : memcpy(dat, subdata[i], subbytes[i]);
3614 : 645 : dat += subbytes[i];
3615 [ + + ]: 645 : if (havenulls)
3616 : 34 : array_bitmap_copy(ARR_NULLBITMAP(result), iitem,
3617 : 34 : subbitmaps[i], 0,
3618 [ + - ]: 34 : subnitems[i]);
3619 : 645 : iitem += subnitems[i];
3620 : : }
3621 : : }
3622 : :
3623 : 573047 : *op->resvalue = PointerGetDatum(result);
3624 : : }
3625 : :
3626 : : /*
3627 : : * Evaluate an ArrayCoerceExpr expression.
3628 : : *
3629 : : * Source array is in step's result variable.
3630 : : */
3631 : : void
3632 : 66408 : ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3633 : : {
3634 : : Datum arraydatum;
3635 : :
3636 : : /* NULL array -> NULL result */
3637 [ + + ]: 66408 : if (*op->resnull)
3638 : 189 : return;
3639 : :
3640 : 66219 : 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 [ + + ]: 66219 : if (op->d.arraycoerce.elemexprstate == NULL)
3647 : : {
3648 : : /* Detoast input array if necessary, and copy in any case */
3649 : 45565 : ArrayType *array = DatumGetArrayTypePCopy(arraydatum);
3650 : :
3651 : 45565 : ARR_ELEMTYPE(array) = op->d.arraycoerce.resultelemtype;
3652 : 45565 : *op->resvalue = PointerGetDatum(array);
3653 : 45565 : return;
3654 : : }
3655 : :
3656 : : /*
3657 : : * Use array_map to apply the sub-expression to each array element.
3658 : : */
3659 : 20634 : *op->resvalue = array_map(arraydatum,
3660 : : op->d.arraycoerce.elemexprstate,
3661 : : econtext,
3662 : : op->d.arraycoerce.resultelemtype,
3663 : 20654 : op->d.arraycoerce.amstate);
3664 : : }
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 : 45415 : ExecEvalRow(ExprState *state, ExprEvalStep *op)
3674 : : {
3675 : : HeapTuple tuple;
3676 : :
3677 : : /* build tuple from evaluated field values */
3678 : 45415 : tuple = heap_form_tuple(op->d.row.tupdesc,
3679 : 45415 : op->d.row.elemvalues,
3680 : 45415 : op->d.row.elemnulls);
3681 : :
3682 : 45415 : *op->resvalue = HeapTupleGetDatum(tuple);
3683 : 45411 : *op->resnull = false;
3684 : 45411 : }
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 : 11801 : ExecEvalMinMax(ExprState *state, ExprEvalStep *op)
3694 : : {
3695 : 11801 : Datum *values = op->d.minmax.values;
3696 : 11801 : bool *nulls = op->d.minmax.nulls;
3697 : 11801 : FunctionCallInfo fcinfo = op->d.minmax.fcinfo_data;
3698 : 11801 : MinMaxOp operator = op->d.minmax.op;
3699 : :
3700 : : /* set at initialization */
3701 : : Assert(fcinfo->args[0].isnull == false);
3702 : : Assert(fcinfo->args[1].isnull == false);
3703 : :
3704 : : /* default to null result */
3705 : 11801 : *op->resnull = true;
3706 : :
3707 [ + + ]: 35599 : for (int off = 0; off < op->d.minmax.nelems; off++)
3708 : : {
3709 : : /* ignore NULL inputs */
3710 [ + + ]: 23798 : if (nulls[off])
3711 : 68 : continue;
3712 : :
3713 [ + + ]: 23730 : if (*op->resnull)
3714 : : {
3715 : : /* first nonnull input, adopt value */
3716 : 11801 : *op->resvalue = values[off];
3717 : 11801 : *op->resnull = false;
3718 : : }
3719 : : else
3720 : : {
3721 : : int cmpresult;
3722 : :
3723 : : /* apply comparison function */
3724 : 11929 : fcinfo->args[0].value = *op->resvalue;
3725 : 11929 : fcinfo->args[1].value = values[off];
3726 : :
3727 : 11929 : fcinfo->isnull = false;
3728 : 11929 : cmpresult = DatumGetInt32(FunctionCallInvoke(fcinfo));
3729 [ - + ]: 11929 : if (fcinfo->isnull) /* probably should not happen */
3730 : 0 : continue;
3731 : :
3732 [ + + + + ]: 11929 : if (cmpresult > 0 && operator == IS_LEAST)
3733 : 100 : *op->resvalue = values[off];
3734 [ + + + + ]: 11829 : else if (cmpresult < 0 && operator == IS_GREATEST)
3735 : 129 : *op->resvalue = values[off];
3736 : : }
3737 : : }
3738 : 11801 : }
3739 : :
3740 : : /*
3741 : : * Evaluate a FieldSelect node.
3742 : : *
3743 : : * Source record is in step's result variable.
3744 : : */
3745 : : void
3746 : 1323414 : ExecEvalFieldSelect(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3747 : : {
3748 : 1323414 : AttrNumber fieldnum = op->d.fieldselect.fieldnum;
3749 : : Datum tupDatum;
3750 : : HeapTupleHeader tuple;
3751 : : Oid tupType;
3752 : : int32 tupTypmod;
3753 : : TupleDesc tupDesc;
3754 : : Form_pg_attribute attr;
3755 : : HeapTupleData tmptup;
3756 : :
3757 : : /* NULL record -> NULL result */
3758 [ + + ]: 1323414 : if (*op->resnull)
3759 : 707 : return;
3760 : :
3761 : 1322707 : tupDatum = *op->resvalue;
3762 : :
3763 : : /* We can special-case expanded records for speed */
3764 [ + + ]: 1322707 : if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(tupDatum)))
3765 : : {
3766 : 822 : ExpandedRecordHeader *erh = (ExpandedRecordHeader *) DatumGetEOHP(tupDatum);
3767 : :
3768 : : Assert(erh->er_magic == ER_MAGIC);
3769 : :
3770 : : /* Extract record's TupleDesc */
3771 : 822 : 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 [ - + ]: 822 : if (fieldnum <= 0) /* should never happen */
3779 [ # # ]: 0 : elog(ERROR, "unsupported reference to system column %d in FieldSelect",
3780 : : fieldnum);
3781 [ - + ]: 822 : if (fieldnum > tupDesc->natts) /* should never happen */
3782 [ # # ]: 0 : elog(ERROR, "attribute number %d exceeds number of columns %d",
3783 : : fieldnum, tupDesc->natts);
3784 : 822 : attr = TupleDescAttr(tupDesc, fieldnum - 1);
3785 : :
3786 : : /* Check for dropped column, and force a NULL result if so */
3787 [ - + ]: 822 : if (attr->attisdropped)
3788 : : {
3789 : 0 : *op->resnull = true;
3790 : 0 : return;
3791 : : }
3792 : :
3793 : : /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3794 : : /* As in CheckVarSlotCompatibility, we should but can't check typmod */
3795 [ - + ]: 822 : if (op->d.fieldselect.resulttype != attr->atttypid)
3796 [ # # ]: 0 : ereport(ERROR,
3797 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
3798 : : errmsg("attribute %d has wrong type", fieldnum),
3799 : : errdetail("Table has type %s, but query expects %s.",
3800 : : format_type_be(attr->atttypid),
3801 : : format_type_be(op->d.fieldselect.resulttype))));
3802 : :
3803 : : /* extract the field */
3804 : 822 : *op->resvalue = expanded_record_get_field(erh, fieldnum,
3805 : : op->resnull);
3806 : : }
3807 : : else
3808 : : {
3809 : : /* Get the composite datum and extract its type fields */
3810 : 1321885 : tuple = DatumGetHeapTupleHeader(tupDatum);
3811 : :
3812 : 1321885 : tupType = HeapTupleHeaderGetTypeId(tuple);
3813 : 1321885 : tupTypmod = HeapTupleHeaderGetTypMod(tuple);
3814 : :
3815 : : /* Lookup tupdesc if first time through or if type changes */
3816 : 1321885 : tupDesc = get_cached_rowtype(tupType, tupTypmod,
3817 : : &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 [ - + ]: 1321885 : if (fieldnum <= 0) /* should never happen */
3825 [ # # ]: 0 : elog(ERROR, "unsupported reference to system column %d in FieldSelect",
3826 : : fieldnum);
3827 [ - + ]: 1321885 : if (fieldnum > tupDesc->natts) /* should never happen */
3828 [ # # ]: 0 : elog(ERROR, "attribute number %d exceeds number of columns %d",
3829 : : fieldnum, tupDesc->natts);
3830 : 1321885 : attr = TupleDescAttr(tupDesc, fieldnum - 1);
3831 : :
3832 : : /* Check for dropped column, and force a NULL result if so */
3833 [ - + ]: 1321885 : if (attr->attisdropped)
3834 : : {
3835 : 0 : *op->resnull = true;
3836 : 0 : return;
3837 : : }
3838 : :
3839 : : /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
3840 : : /* As in CheckVarSlotCompatibility, we should but can't check typmod */
3841 [ + + ]: 1321885 : if (op->d.fieldselect.resulttype != attr->atttypid)
3842 [ + - ]: 8 : ereport(ERROR,
3843 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
3844 : : errmsg("attribute %d has wrong type", fieldnum),
3845 : : errdetail("Table has type %s, but query expects %s.",
3846 : : format_type_be(attr->atttypid),
3847 : : format_type_be(op->d.fieldselect.resulttype))));
3848 : :
3849 : : /* heap_getattr needs a HeapTuple not a bare HeapTupleHeader */
3850 : 1321877 : tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3851 : 1321877 : tmptup.t_data = tuple;
3852 : :
3853 : : /* extract the field */
3854 : 1321877 : *op->resvalue = heap_getattr(&tmptup,
3855 : : fieldnum,
3856 : : tupDesc,
3857 : : op->resnull);
3858 : : }
3859 : : }
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 : 397 : ExecEvalFieldStoreDeForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3872 : : {
3873 [ + + ]: 397 : if (*op->resnull)
3874 : : {
3875 : : /* Convert null input tuple into an all-nulls row */
3876 : 174 : memset(op->d.fieldstore.nulls, true,
3877 : 174 : op->d.fieldstore.tupdesc->natts * sizeof(bool));
3878 : : }
3879 : : else
3880 : : {
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 : 223 : Datum tupDatum = *op->resvalue;
3886 : : HeapTupleHeader tuphdr;
3887 : : HeapTupleData tmptup;
3888 : :
3889 : 223 : tuphdr = DatumGetHeapTupleHeader(tupDatum);
3890 : 223 : tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
3891 : 223 : ItemPointerSetInvalid(&(tmptup.t_self));
3892 : 223 : tmptup.t_tableOid = InvalidOid;
3893 : 223 : tmptup.t_data = tuphdr;
3894 : :
3895 : 223 : heap_deform_tuple(&tmptup, op->d.fieldstore.tupdesc,
3896 : : op->d.fieldstore.values,
3897 : : op->d.fieldstore.nulls);
3898 : : }
3899 : 397 : }
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 : 397 : ExecEvalFieldStoreForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3907 : : {
3908 : : HeapTuple tuple;
3909 : :
3910 : 397 : tuple = heap_form_tuple(op->d.fieldstore.tupdesc,
3911 : 397 : op->d.fieldstore.values,
3912 : 397 : op->d.fieldstore.nulls);
3913 : :
3914 : 397 : *op->resvalue = HeapTupleGetDatum(tuple);
3915 : 397 : *op->resnull = false;
3916 : 397 : }
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 : 7116 : ExecEvalConvertRowtype(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
3926 : : {
3927 : : HeapTuple result;
3928 : : Datum tupDatum;
3929 : : HeapTupleHeader tuple;
3930 : : HeapTupleData tmptup;
3931 : : TupleDesc indesc,
3932 : : outdesc;
3933 : 7116 : bool changed = false;
3934 : :
3935 : : /* NULL in -> NULL out */
3936 [ + + ]: 7116 : if (*op->resnull)
3937 : 30 : return;
3938 : :
3939 : 7086 : tupDatum = *op->resvalue;
3940 : 7086 : 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 : 7086 : indesc = get_cached_rowtype(op->d.convert_rowtype.inputtype, -1,
3948 : : op->d.convert_rowtype.incache,
3949 : : &changed);
3950 : 7086 : IncrTupleDescRefCount(indesc);
3951 : 7086 : outdesc = get_cached_rowtype(op->d.convert_rowtype.outputtype, -1,
3952 : : op->d.convert_rowtype.outcache,
3953 : : &changed);
3954 : 7086 : 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 : : Assert(HeapTupleHeaderGetTypeId(tuple) == indesc->tdtypeid ||
3963 : : HeapTupleHeaderGetTypeId(tuple) == RECORDOID);
3964 : :
3965 : : /* if first time through, or after change, initialize conversion map */
3966 [ + + ]: 7086 : if (changed)
3967 : : {
3968 : : MemoryContext old_cxt;
3969 : :
3970 : : /* allocate map in long-lived memory context */
3971 : 329 : old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
3972 : :
3973 : : /* prepare map from old to new attribute numbers */
3974 : 329 : op->d.convert_rowtype.map = convert_tuples_by_name(indesc, outdesc);
3975 : :
3976 : 329 : MemoryContextSwitchTo(old_cxt);
3977 : : }
3978 : :
3979 : : /* Following steps need a HeapTuple not a bare HeapTupleHeader */
3980 : 7086 : tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
3981 : 7086 : tmptup.t_data = tuple;
3982 : :
3983 [ + + ]: 7086 : if (op->d.convert_rowtype.map != NULL)
3984 : : {
3985 : : /* Full conversion with attribute rearrangement needed */
3986 : 355 : result = execute_attr_map_tuple(&tmptup, op->d.convert_rowtype.map);
3987 : : /* Result already has appropriate composite-datum header fields */
3988 : 355 : *op->resvalue = HeapTupleGetDatum(result);
3989 : : }
3990 : : else
3991 : : {
3992 : : 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 : 6731 : *op->resvalue = heap_copy_tuple_as_datum(&tmptup, indesc);
4013 : 6727 : resulthdr = (HeapTupleHeader) DatumGetPointer(*op->resvalue);
4014 : 6727 : heap_tuple_set_datum(resulthdr, outdesc,
4015 : : HeapTupleHeaderGetDatumLength(resulthdr));
4016 : : }
4017 : :
4018 : 7082 : DecrTupleDescRefCount(indesc);
4019 : 7082 : DecrTupleDescRefCount(outdesc);
4020 : : }
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 : 3465342 : ExecEvalScalarArrayOp(ExprState *state, ExprEvalStep *op)
4034 : : {
4035 : 3465342 : FunctionCallInfo fcinfo = op->d.scalararrayop.fcinfo_data;
4036 : 3465342 : bool useOr = op->d.scalararrayop.useOr;
4037 : 3465342 : bool strictfunc = op->d.scalararrayop.finfo->fn_strict;
4038 : : ArrayType *arr;
4039 : : int nitems;
4040 : : Datum result;
4041 : : 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 [ + + ]: 3465342 : if (*op->resnull)
4048 : 123051 : return;
4049 : :
4050 : : /* Else okay to fetch and detoast the array */
4051 : 3351914 : 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 : 3351914 : nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
4060 [ + + ]: 3351914 : if (nitems <= 0)
4061 : : {
4062 : 9013 : *op->resvalue = BoolGetDatum(!useOr);
4063 : 9013 : *op->resnull = false;
4064 : 9013 : return;
4065 : : }
4066 : :
4067 : : /*
4068 : : * If the scalar is NULL, and the function is strict, return NULL; no
4069 : : * point in iterating the loop.
4070 : : */
4071 [ + + + + ]: 3342901 : if (fcinfo->args[0].isnull && strictfunc)
4072 : : {
4073 : 610 : *op->resnull = true;
4074 : 610 : return;
4075 : : }
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 [ + + ]: 3342291 : if (op->d.scalararrayop.element_type != ARR_ELEMTYPE(arr))
4082 : : {
4083 : 10499 : get_typlenbyvalalign(ARR_ELEMTYPE(arr),
4084 : : &op->d.scalararrayop.typlen,
4085 : : &op->d.scalararrayop.typbyval,
4086 : : &op->d.scalararrayop.typalign);
4087 : 10499 : op->d.scalararrayop.element_type = ARR_ELEMTYPE(arr);
4088 : : }
4089 : :
4090 : 3342291 : ExecEvalArrayCompareInternal(fcinfo,
4091 : : arr,
4092 : 3342291 : op->d.scalararrayop.typlen,
4093 : 3342291 : op->d.scalararrayop.typbyval,
4094 : 3342291 : op->d.scalararrayop.typalign,
4095 : : useOr,
4096 : : &result,
4097 : : &resultnull);
4098 : :
4099 : 3342291 : *op->resvalue = result;
4100 : 3342291 : *op->resnull = resultnull;
4101 : : }
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 : 3342347 : ExecEvalArrayCompareInternal(FunctionCallInfo fcinfo, ArrayType *arr,
4112 : : int16 typlen, bool typbyval, char typalign,
4113 : : bool useOr, Datum *result, bool *resultnull)
4114 : : {
4115 : 3342347 : uint8 typalignby = typalign_to_alignby(typalign);
4116 : : int nitems;
4117 : : char *s;
4118 : : uint8 *bitmap;
4119 : : int bitmask;
4120 : 3342347 : bool strictfunc = fcinfo->flinfo->fn_strict;
4121 : :
4122 : 3342347 : nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
4123 : :
4124 : : /* Initialize result appropriately depending on useOr */
4125 : 3342347 : *result = BoolGetDatum(!useOr);
4126 : 3342347 : *resultnull = false;
4127 : :
4128 : : /* Loop over the array elements */
4129 [ + + ]: 3342347 : s = (char *) ARR_DATA_PTR(arr);
4130 [ + + ]: 3342347 : bitmap = ARR_NULLBITMAP(arr);
4131 : 3342347 : bitmask = 1;
4132 : :
4133 [ + + ]: 9266879 : for (int i = 0; i < nitems; i++)
4134 : : {
4135 : : Datum elt;
4136 : : Datum thisresult;
4137 : :
4138 : : /* Get array element, checking for NULL */
4139 [ + + + + ]: 7642894 : if (bitmap && (*bitmap & bitmask) == 0)
4140 : : {
4141 : 143523 : fcinfo->args[1].value = (Datum) 0;
4142 : 143523 : fcinfo->args[1].isnull = true;
4143 : : }
4144 : : else
4145 : : {
4146 : 7499371 : elt = fetch_att(s, typbyval, typlen);
4147 [ + + + - ]: 7499371 : s = att_addlength_pointer(s, typlen, s);
4148 : 7499371 : s = (char *) att_nominal_alignby(s, typalignby);
4149 : 7499371 : fcinfo->args[1].value = elt;
4150 : 7499371 : fcinfo->args[1].isnull = false;
4151 : : }
4152 : :
4153 : : /* Call comparison function */
4154 [ + + + + ]: 7642894 : if (fcinfo->args[1].isnull && strictfunc)
4155 : : {
4156 : 143427 : fcinfo->isnull = true;
4157 : 143427 : thisresult = (Datum) 0;
4158 : : }
4159 : : else
4160 : : {
4161 : 7499467 : fcinfo->isnull = false;
4162 : 7499467 : thisresult = fcinfo->flinfo->fn_addr(fcinfo);
4163 : : }
4164 : :
4165 : : /* Combine results per OR or AND semantics */
4166 [ + + ]: 7642894 : if (fcinfo->isnull)
4167 : 143851 : *resultnull = true;
4168 [ + + ]: 7499043 : else if (useOr)
4169 : : {
4170 [ + + ]: 6700925 : if (DatumGetBool(thisresult))
4171 : : {
4172 : 1091643 : *result = BoolGetDatum(true);
4173 : 1091643 : *resultnull = false;
4174 : 1091643 : break; /* needn't look at any more elements */
4175 : : }
4176 : : }
4177 : : else
4178 : : {
4179 [ + + ]: 798118 : if (!DatumGetBool(thisresult))
4180 : : {
4181 : 626719 : *result = BoolGetDatum(false);
4182 : 626719 : *resultnull = false;
4183 : 626719 : break; /* needn't look at any more elements */
4184 : : }
4185 : : }
4186 : :
4187 : : /* advance bitmap pointer if any */
4188 [ + + ]: 5924532 : if (bitmap)
4189 : : {
4190 : 512021 : bitmask <<= 1;
4191 [ + + ]: 512021 : if (bitmask == 0x100)
4192 : : {
4193 : 561 : bitmap++;
4194 : 561 : bitmask = 1;
4195 : : }
4196 : : }
4197 : : }
4198 : 3342347 : }
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 : 19039 : saop_element_hash(struct saophash_hash *tb, Datum key)
4208 : : {
4209 : 19039 : ScalarArrayOpExprHashTable *elements_tab = (ScalarArrayOpExprHashTable *) tb->private_data;
4210 : 19039 : FunctionCallInfo fcinfo = &elements_tab->hash_fcinfo_data;
4211 : : Datum hash;
4212 : :
4213 : 19039 : fcinfo->args[0].value = key;
4214 : 19039 : fcinfo->args[0].isnull = false;
4215 : :
4216 : 19039 : hash = elements_tab->hash_finfo.fn_addr(fcinfo);
4217 : :
4218 : 19039 : return DatumGetUInt32(hash);
4219 : : }
4220 : :
4221 : : /*
4222 : : * Matching function for scalar array hash op elements, to be used in hashtable
4223 : : * lookups.
4224 : : */
4225 : : static bool
4226 : 4247 : saop_hash_element_match(struct saophash_hash *tb, Datum key1, Datum key2)
4227 : : {
4228 : : Datum result;
4229 : :
4230 : 4247 : ScalarArrayOpExprHashTable *elements_tab = (ScalarArrayOpExprHashTable *) tb->private_data;
4231 : 4247 : FunctionCallInfo fcinfo = elements_tab->op->d.hashedscalararrayop.fcinfo_data;
4232 : :
4233 : 4247 : fcinfo->args[0].value = key1;
4234 : 4247 : fcinfo->args[0].isnull = false;
4235 : 4247 : fcinfo->args[1].value = key2;
4236 : 4247 : fcinfo->args[1].isnull = false;
4237 : :
4238 : 4247 : result = elements_tab->op->d.hashedscalararrayop.finfo->fn_addr(fcinfo);
4239 : :
4240 : 4247 : return DatumGetBool(result);
4241 : : }
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 : 17921 : ExecEvalHashedScalarArrayOp(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
4258 : : {
4259 : 17921 : ScalarArrayOpExprHashTable *elements_tab = op->d.hashedscalararrayop.elements_tab;
4260 : 17921 : FunctionCallInfo fcinfo = op->d.hashedscalararrayop.fcinfo_data;
4261 : 17921 : bool inclause = op->d.hashedscalararrayop.inclause;
4262 : 17921 : bool strictfunc = op->d.hashedscalararrayop.finfo->fn_strict;
4263 : 17921 : Datum scalar = fcinfo->args[0].value;
4264 : 17921 : bool scalar_isnull = fcinfo->args[0].isnull;
4265 : : Datum result;
4266 : : bool resultnull;
4267 : : bool hashfound;
4268 : :
4269 : : /* We don't setup a hashed scalar array op if the array const is null. */
4270 : : 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 [ + + + + ]: 17921 : if (scalar_isnull && strictfunc)
4277 : : {
4278 : 38 : *op->resnull = true;
4279 : 94 : return;
4280 : : }
4281 : :
4282 : : /* Build the hash table on first evaluation */
4283 [ + + ]: 17883 : if (elements_tab == NULL)
4284 : : {
4285 : : ScalarArrayOpExpr *saop;
4286 : : int16 typlen;
4287 : : bool typbyval;
4288 : : char typalign;
4289 : : uint8 typalignby;
4290 : : int nitems;
4291 : 112 : bool has_nulls = false;
4292 : : char *s;
4293 : : uint8 *bitmap;
4294 : : int bitmask;
4295 : : MemoryContext oldcontext;
4296 : : ArrayType *arr;
4297 : :
4298 : 112 : saop = op->d.hashedscalararrayop.saop;
4299 : :
4300 : 112 : arr = DatumGetArrayTypeP(*op->resvalue);
4301 : 112 : nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
4302 : :
4303 : 112 : get_typlenbyvalalign(ARR_ELEMTYPE(arr),
4304 : : &typlen,
4305 : : &typbyval,
4306 : : &typalign);
4307 : 112 : typalignby = typalign_to_alignby(typalign);
4308 : :
4309 : 112 : oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
4310 : :
4311 : : elements_tab = (ScalarArrayOpExprHashTable *)
4312 : 112 : palloc0(offsetof(ScalarArrayOpExprHashTable, hash_fcinfo_data) +
4313 : : SizeForFunctionCallInfo(1));
4314 : 112 : op->d.hashedscalararrayop.elements_tab = elements_tab;
4315 : 112 : elements_tab->op = op;
4316 : :
4317 : 112 : fmgr_info(saop->hashfuncid, &elements_tab->hash_finfo);
4318 : 112 : fmgr_info_set_expr((Node *) saop, &elements_tab->hash_finfo);
4319 : :
4320 : 112 : InitFunctionCallInfoData(elements_tab->hash_fcinfo_data,
4321 : : &elements_tab->hash_finfo,
4322 : : 1,
4323 : : saop->inputcollid,
4324 : : NULL,
4325 : : 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 : 112 : elements_tab->hashtab = saophash_create(CurrentMemoryContext, nitems,
4334 : : elements_tab);
4335 : :
4336 : 112 : MemoryContextSwitchTo(oldcontext);
4337 : :
4338 [ + + ]: 112 : s = (char *) ARR_DATA_PTR(arr);
4339 [ + + ]: 112 : bitmap = ARR_NULLBITMAP(arr);
4340 : 112 : bitmask = 1;
4341 [ + + ]: 1452 : for (int i = 0; i < nitems; i++)
4342 : : {
4343 : : /* Get array element, checking for NULL. */
4344 [ + + + + ]: 1340 : if (bitmap && (*bitmap & bitmask) == 0)
4345 : : {
4346 : 128 : has_nulls = true;
4347 : : }
4348 : : else
4349 : : {
4350 : : Datum element;
4351 : :
4352 : 1212 : element = fetch_att(s, typbyval, typlen);
4353 [ + + + - ]: 1212 : s = att_addlength_pointer(s, typlen, s);
4354 : 1212 : s = (char *) att_nominal_alignby(s, typalignby);
4355 : :
4356 : 1212 : saophash_insert(elements_tab->hashtab, element, &hashfound);
4357 : : }
4358 : :
4359 : : /* Advance bitmap pointer if any. */
4360 [ + + ]: 1340 : if (bitmap)
4361 : : {
4362 : 476 : bitmask <<= 1;
4363 [ + + ]: 476 : if (bitmask == 0x100)
4364 : : {
4365 : 48 : bitmap++;
4366 : 48 : bitmask = 1;
4367 : : }
4368 : : }
4369 : : }
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 : 112 : 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 [ + + ]: 112 : if (!strictfunc)
4386 : : {
4387 : : bool null_lhs_result;
4388 : :
4389 : 56 : fcinfo->args[0].value = (Datum) 0;
4390 : 56 : fcinfo->args[0].isnull = true;
4391 : :
4392 : 56 : ExecEvalArrayCompareInternal(fcinfo, arr, typlen, typbyval,
4393 : : typalign, true, &result,
4394 : : &resultnull);
4395 : :
4396 : 56 : null_lhs_result = DatumGetBool(result);
4397 : :
4398 : : /* invert non-NULL results for NOT IN */
4399 [ + + + + ]: 56 : if (!resultnull && !inclause)
4400 : 16 : null_lhs_result = !null_lhs_result;
4401 : :
4402 : 56 : op->d.hashedscalararrayop.null_lhs_isnull = resultnull;
4403 : 56 : op->d.hashedscalararrayop.null_lhs_result = null_lhs_result;
4404 : : }
4405 : : }
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 [ + + ]: 17883 : if (scalar_isnull)
4412 : : {
4413 : : Assert(!strictfunc);
4414 : :
4415 : 56 : *op->resnull = op->d.hashedscalararrayop.null_lhs_isnull;
4416 : 56 : *op->resvalue = BoolGetDatum(op->d.hashedscalararrayop.null_lhs_result);
4417 : 56 : return;
4418 : : }
4419 : :
4420 : :
4421 : : /* Check the hash to see if we have a match. */
4422 : 17827 : 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 [ + + ]: 17827 : if (inclause)
4426 : 15270 : result = BoolGetDatum(hashfound); /* IN */
4427 : : else
4428 : 2557 : result = BoolGetDatum(!hashfound); /* NOT IN */
4429 : :
4430 : 17827 : 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 [ + + + + ]: 17827 : if (!hashfound && op->d.hashedscalararrayop.has_nulls)
4439 : : {
4440 [ + + ]: 40 : if (strictfunc)
4441 : : {
4442 : :
4443 : : /*
4444 : : * We have nulls in the array so a non-null lhs and no match must
4445 : : * yield NULL.
4446 : : */
4447 : 16 : result = (Datum) 0;
4448 : 16 : resultnull = true;
4449 : : }
4450 : : else
4451 : : {
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 : 24 : fcinfo->args[0].value = scalar;
4460 : 24 : fcinfo->args[0].isnull = scalar_isnull;
4461 : 24 : fcinfo->args[1].value = (Datum) 0;
4462 : 24 : fcinfo->args[1].isnull = true;
4463 : :
4464 : 24 : result = op->d.hashedscalararrayop.finfo->fn_addr(fcinfo);
4465 : 24 : 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 [ + + ]: 24 : if (!inclause)
4472 : 12 : result = BoolGetDatum(!DatumGetBool(result));
4473 : : }
4474 : : }
4475 : :
4476 : 17827 : *op->resvalue = result;
4477 : 17827 : *op->resnull = resultnull;
4478 : : }
4479 : :
4480 : : /*
4481 : : * Evaluate a NOT NULL domain constraint.
4482 : : */
4483 : : void
4484 : 264 : ExecEvalConstraintNotNull(ExprState *state, ExprEvalStep *op)
4485 : : {
4486 [ + + ]: 264 : if (*op->resnull)
4487 [ + - ]: 75 : errsave((Node *) op->d.domaincheck.escontext,
4488 : : (errcode(ERRCODE_NOT_NULL_VIOLATION),
4489 : : errmsg("domain %s does not allow null values",
4490 : : format_type_be(op->d.domaincheck.resulttype)),
4491 : : errdatatype(op->d.domaincheck.resulttype)));
4492 : 197 : }
4493 : :
4494 : : /*
4495 : : * Evaluate a CHECK domain constraint.
4496 : : */
4497 : : void
4498 : 8697 : ExecEvalConstraintCheck(ExprState *state, ExprEvalStep *op)
4499 : : {
4500 [ + + ]: 8697 : if (!*op->d.domaincheck.checknull &&
4501 [ + + ]: 7897 : !DatumGetBool(*op->d.domaincheck.checkvalue))
4502 [ + - ]: 316 : errsave((Node *) op->d.domaincheck.escontext,
4503 : : (errcode(ERRCODE_CHECK_VIOLATION),
4504 : : errmsg("value for domain %s violates check constraint \"%s\"",
4505 : : format_type_be(op->d.domaincheck.resulttype),
4506 : : op->d.domaincheck.constraintname),
4507 : : errdomainconstraint(op->d.domaincheck.resulttype,
4508 : : op->d.domaincheck.constraintname)));
4509 : 8409 : }
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 : 30698 : ExecEvalXmlExpr(ExprState *state, ExprEvalStep *op)
4519 : : {
4520 : 30698 : XmlExpr *xexpr = op->d.xmlexpr.xexpr;
4521 : : Datum value;
4522 : :
4523 : 30698 : *op->resnull = true; /* until we get a result */
4524 : 30698 : *op->resvalue = (Datum) 0;
4525 : :
4526 [ + + + + : 30698 : switch (xexpr->op)
+ + + +
- ]
4527 : : {
4528 : 44 : case IS_XMLCONCAT:
4529 : : {
4530 : 44 : Datum *argvalue = op->d.xmlexpr.argvalue;
4531 : 44 : bool *argnull = op->d.xmlexpr.argnull;
4532 : 44 : List *values = NIL;
4533 : :
4534 [ + + ]: 141 : for (int i = 0; i < list_length(xexpr->args); i++)
4535 : : {
4536 [ + + ]: 97 : if (!argnull[i])
4537 : 72 : values = lappend(values, DatumGetPointer(argvalue[i]));
4538 : : }
4539 : :
4540 [ + + ]: 44 : if (values != NIL)
4541 : : {
4542 : 34 : *op->resvalue = PointerGetDatum(xmlconcat(values));
4543 : 34 : *op->resnull = false;
4544 : : }
4545 : : }
4546 : 44 : break;
4547 : :
4548 : 15067 : case IS_XMLFOREST:
4549 : : {
4550 : 15067 : Datum *argvalue = op->d.xmlexpr.named_argvalue;
4551 : 15067 : bool *argnull = op->d.xmlexpr.named_argnull;
4552 : : StringInfoData buf;
4553 : : ListCell *lc;
4554 : : ListCell *lc2;
4555 : : int i;
4556 : :
4557 : 15067 : initStringInfo(&buf);
4558 : :
4559 : 15067 : i = 0;
4560 [ + - + + : 105389 : forboth(lc, xexpr->named_args, lc2, xexpr->arg_names)
+ - + + +
+ + - +
+ ]
4561 : : {
4562 : 90322 : Expr *e = (Expr *) lfirst(lc);
4563 : 90322 : char *argname = strVal(lfirst(lc2));
4564 : :
4565 [ + + ]: 90322 : if (!argnull[i])
4566 : : {
4567 : 75442 : value = argvalue[i];
4568 : 75442 : appendStringInfo(&buf, "<%s>%s</%s>",
4569 : : argname,
4570 : : map_sql_value_to_xml_value(value,
4571 : : exprType((Node *) e), true),
4572 : : argname);
4573 : 75442 : *op->resnull = false;
4574 : : }
4575 : 90322 : i++;
4576 : : }
4577 : :
4578 [ + - ]: 15067 : if (!*op->resnull)
4579 : : {
4580 : : text *result;
4581 : :
4582 : 15067 : result = cstring_to_text_with_len(buf.data, buf.len);
4583 : 15067 : *op->resvalue = PointerGetDatum(result);
4584 : : }
4585 : :
4586 : 15067 : pfree(buf.data);
4587 : : }
4588 : 15067 : break;
4589 : :
4590 : 15171 : case IS_XMLELEMENT:
4591 : 15171 : *op->resvalue = PointerGetDatum(xmlelement(xexpr,
4592 : : op->d.xmlexpr.named_argvalue,
4593 : : op->d.xmlexpr.named_argnull,
4594 : : op->d.xmlexpr.argvalue,
4595 : : op->d.xmlexpr.argnull));
4596 : 15167 : *op->resnull = false;
4597 : 15167 : break;
4598 : :
4599 : 117 : case IS_XMLPARSE:
4600 : : {
4601 : 117 : Datum *argvalue = op->d.xmlexpr.argvalue;
4602 : 117 : bool *argnull = op->d.xmlexpr.argnull;
4603 : : text *data;
4604 : : bool preserve_whitespace;
4605 : :
4606 : : /* arguments are known to be text, bool */
4607 : : Assert(list_length(xexpr->args) == 2);
4608 : :
4609 [ - + ]: 117 : if (argnull[0])
4610 : 0 : return;
4611 : 117 : value = argvalue[0];
4612 : 117 : data = DatumGetTextPP(value);
4613 : :
4614 [ - + ]: 117 : if (argnull[1]) /* probably can't happen */
4615 : 0 : return;
4616 : 117 : value = argvalue[1];
4617 : 117 : preserve_whitespace = DatumGetBool(value);
4618 : :
4619 : 117 : *op->resvalue = PointerGetDatum(xmlparse(data,
4620 : : xexpr->xmloption,
4621 : : preserve_whitespace, NULL));
4622 : 85 : *op->resnull = false;
4623 : : }
4624 : 85 : break;
4625 : :
4626 : 57 : case IS_XMLPI:
4627 : : {
4628 : : text *arg;
4629 : : bool isnull;
4630 : :
4631 : : /* optional argument is known to be text */
4632 : : Assert(list_length(xexpr->args) <= 1);
4633 : :
4634 [ + + ]: 57 : if (xexpr->args)
4635 : : {
4636 : 33 : isnull = op->d.xmlexpr.argnull[0];
4637 [ + + ]: 33 : if (isnull)
4638 : 14 : arg = NULL;
4639 : : else
4640 : 19 : arg = DatumGetTextPP(op->d.xmlexpr.argvalue[0]);
4641 : : }
4642 : : else
4643 : : {
4644 : 24 : arg = NULL;
4645 : 24 : isnull = false;
4646 : : }
4647 : :
4648 : 57 : *op->resvalue = PointerGetDatum(xmlpi(xexpr->name,
4649 : : arg,
4650 : : isnull,
4651 : : op->resnull));
4652 : : }
4653 : 45 : break;
4654 : :
4655 : 49 : case IS_XMLROOT:
4656 : : {
4657 : 49 : Datum *argvalue = op->d.xmlexpr.argvalue;
4658 : 49 : bool *argnull = op->d.xmlexpr.argnull;
4659 : : xmltype *data;
4660 : : text *version;
4661 : : int standalone;
4662 : :
4663 : : /* arguments are known to be xml, text, int */
4664 : : Assert(list_length(xexpr->args) == 3);
4665 : :
4666 [ - + ]: 49 : if (argnull[0])
4667 : 0 : return;
4668 : 49 : data = DatumGetXmlP(argvalue[0]);
4669 : :
4670 [ + + ]: 49 : if (argnull[1])
4671 : 30 : version = NULL;
4672 : : else
4673 : 19 : version = DatumGetTextPP(argvalue[1]);
4674 : :
4675 : : Assert(!argnull[2]); /* always present */
4676 : 49 : standalone = DatumGetInt32(argvalue[2]);
4677 : :
4678 : 49 : *op->resvalue = PointerGetDatum(xmlroot(data,
4679 : : version,
4680 : : standalone));
4681 : 49 : *op->resnull = false;
4682 : : }
4683 : 49 : break;
4684 : :
4685 : 168 : case IS_XMLSERIALIZE:
4686 : : {
4687 : 168 : Datum *argvalue = op->d.xmlexpr.argvalue;
4688 : 168 : bool *argnull = op->d.xmlexpr.argnull;
4689 : :
4690 : : /* argument type is known to be xml */
4691 : : Assert(list_length(xexpr->args) == 1);
4692 : :
4693 [ + + ]: 168 : if (argnull[0])
4694 : 10 : return;
4695 : 158 : value = argvalue[0];
4696 : :
4697 : 138 : *op->resvalue =
4698 : 158 : PointerGetDatum(xmltotext_with_options(DatumGetXmlP(value),
4699 : : xexpr->xmloption,
4700 : : xexpr->indent));
4701 : 138 : *op->resnull = false;
4702 : : }
4703 : 138 : break;
4704 : :
4705 : 25 : case IS_DOCUMENT:
4706 : : {
4707 : 25 : Datum *argvalue = op->d.xmlexpr.argvalue;
4708 : 25 : bool *argnull = op->d.xmlexpr.argnull;
4709 : :
4710 : : /* optional argument is known to be xml */
4711 : : Assert(list_length(xexpr->args) == 1);
4712 : :
4713 [ - + ]: 25 : if (argnull[0])
4714 : 0 : return;
4715 : 25 : value = argvalue[0];
4716 : :
4717 : 50 : *op->resvalue =
4718 : 25 : BoolGetDatum(xml_is_document(DatumGetXmlP(value)));
4719 : 25 : *op->resnull = false;
4720 : : }
4721 : 25 : break;
4722 : :
4723 : 0 : default:
4724 [ # # ]: 0 : elog(ERROR, "unrecognized XML operation");
4725 : : break;
4726 : : }
4727 : : }
4728 : :
4729 : : /*
4730 : : * Evaluate a JSON constructor expression.
4731 : : */
4732 : : void
4733 : 484 : ExecEvalJsonConstructor(ExprState *state, ExprEvalStep *op,
4734 : : ExprContext *econtext)
4735 : : {
4736 : : Datum res;
4737 : 484 : JsonConstructorExprState *jcstate = op->d.json_constructor.jcstate;
4738 : 484 : JsonConstructorExpr *ctor = jcstate->constructor;
4739 : 484 : bool is_jsonb = ctor->returning->format->format_type == JS_FORMAT_JSONB;
4740 : 484 : bool isnull = false;
4741 : :
4742 [ + + ]: 484 : if (ctor->type == JSCTOR_JSON_ARRAY)
4743 : : res = (is_jsonb ?
4744 [ + + ]: 145 : jsonb_build_array_worker :
4745 : : json_build_array_worker) (jcstate->nargs,
4746 : 145 : jcstate->arg_values,
4747 : 145 : jcstate->arg_nulls,
4748 : 145 : jcstate->arg_types,
4749 : 145 : jcstate->constructor->absent_on_null);
4750 [ + + ]: 339 : else if (ctor->type == JSCTOR_JSON_OBJECT)
4751 : : res = (is_jsonb ?
4752 [ + + ]: 271 : jsonb_build_object_worker :
4753 : : json_build_object_worker) (jcstate->nargs,
4754 : 271 : jcstate->arg_values,
4755 : 271 : jcstate->arg_nulls,
4756 : 271 : jcstate->arg_types,
4757 : 271 : jcstate->constructor->absent_on_null,
4758 : 271 : jcstate->constructor->unique);
4759 [ + + ]: 68 : else if (ctor->type == JSCTOR_JSON_SCALAR)
4760 : : {
4761 [ + + ]: 62 : if (jcstate->arg_nulls[0])
4762 : : {
4763 : 12 : res = (Datum) 0;
4764 : 12 : isnull = true;
4765 : : }
4766 : : else
4767 : : {
4768 : 50 : Datum value = jcstate->arg_values[0];
4769 : 50 : Oid outfuncid = jcstate->arg_type_cache[0].outfuncid;
4770 : 50 : JsonTypeCategory category = (JsonTypeCategory)
4771 : 50 : jcstate->arg_type_cache[0].category;
4772 : :
4773 [ - + ]: 50 : if (is_jsonb)
4774 : 0 : res = datum_to_jsonb(value, category, outfuncid);
4775 : : else
4776 : 50 : res = datum_to_json(value, category, outfuncid);
4777 : : }
4778 : : }
4779 [ + - ]: 6 : else if (ctor->type == JSCTOR_JSON_PARSE)
4780 : : {
4781 [ - + ]: 6 : if (jcstate->arg_nulls[0])
4782 : : {
4783 : 0 : res = (Datum) 0;
4784 : 0 : isnull = true;
4785 : : }
4786 : : else
4787 : : {
4788 : 6 : Datum value = jcstate->arg_values[0];
4789 : 6 : text *js = DatumGetTextP(value);
4790 : :
4791 [ - + ]: 6 : if (is_jsonb)
4792 : 0 : res = jsonb_from_text(js, true);
4793 : : else
4794 : : {
4795 : 6 : (void) json_validate(js, true, true);
4796 : 0 : res = value;
4797 : : }
4798 : : }
4799 : : }
4800 : : else
4801 [ # # ]: 0 : elog(ERROR, "invalid JsonConstructorExpr type %d", ctor->type);
4802 : :
4803 : 428 : *op->resvalue = res;
4804 : 428 : *op->resnull = isnull;
4805 : 428 : }
4806 : :
4807 : : /*
4808 : : * Evaluate a IS JSON predicate.
4809 : : */
4810 : : void
4811 : 1864 : ExecEvalJsonIsPredicate(ExprState *state, ExprEvalStep *op)
4812 : : {
4813 : 1864 : JsonIsPredicate *pred = op->d.is_json.pred;
4814 : 1864 : Datum js = *op->resvalue;
4815 : 1864 : Oid exprtype = pred->exprBaseType;
4816 : : bool res;
4817 : :
4818 [ + + ]: 1864 : if (*op->resnull)
4819 : : {
4820 : 88 : *op->resvalue = BoolGetDatum(false);
4821 : 88 : return;
4822 : : }
4823 : :
4824 [ + + + + ]: 1776 : if (exprtype == TEXTOID || exprtype == JSONOID)
4825 : 1424 : {
4826 : 1424 : text *json = DatumGetTextP(js);
4827 : :
4828 [ + + ]: 1424 : if (pred->item_type == JS_TYPE_ANY)
4829 : 974 : res = true;
4830 : : else
4831 : : {
4832 [ + + + + ]: 450 : switch (json_get_first_token(json, false))
4833 : : {
4834 : 198 : case JSON_TOKEN_OBJECT_START:
4835 : 198 : res = pred->item_type == JS_TYPE_OBJECT;
4836 : 198 : break;
4837 : 84 : case JSON_TOKEN_ARRAY_START:
4838 : 84 : res = pred->item_type == JS_TYPE_ARRAY;
4839 : 84 : break;
4840 : 144 : case JSON_TOKEN_STRING:
4841 : : case JSON_TOKEN_NUMBER:
4842 : : case JSON_TOKEN_TRUE:
4843 : : case JSON_TOKEN_FALSE:
4844 : : case JSON_TOKEN_NULL:
4845 : 144 : res = pred->item_type == JS_TYPE_SCALAR;
4846 : 144 : break;
4847 : 24 : default:
4848 : 24 : res = false;
4849 : 24 : break;
4850 : : }
4851 : : }
4852 : :
4853 : : /*
4854 : : * Do full parsing pass only for uniqueness check or for JSON text
4855 : : * validation.
4856 : : */
4857 [ + + + + : 1424 : if (res && (pred->unique_keys || exprtype == TEXTOID))
+ + ]
4858 : 896 : res = json_validate(json, pred->unique_keys, false);
4859 : : }
4860 [ + - ]: 352 : else if (exprtype == JSONBOID)
4861 : : {
4862 [ + + ]: 352 : if (pred->item_type == JS_TYPE_ANY)
4863 : 220 : res = true;
4864 : : else
4865 : : {
4866 : 132 : Jsonb *jb = DatumGetJsonbP(js);
4867 : :
4868 [ + + + - ]: 132 : switch (pred->item_type)
4869 : : {
4870 : 44 : case JS_TYPE_OBJECT:
4871 : 44 : res = JB_ROOT_IS_OBJECT(jb);
4872 : 44 : break;
4873 : 44 : case JS_TYPE_ARRAY:
4874 [ + + + + ]: 44 : res = JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb);
4875 : 44 : break;
4876 : 44 : case JS_TYPE_SCALAR:
4877 [ + + + + ]: 44 : res = JB_ROOT_IS_ARRAY(jb) && JB_ROOT_IS_SCALAR(jb);
4878 : 44 : break;
4879 : 0 : default:
4880 : 0 : res = false;
4881 : 0 : break;
4882 : : }
4883 : : }
4884 : :
4885 : : /* Key uniqueness check is redundant for jsonb */
4886 : : }
4887 : : else
4888 : 0 : res = false;
4889 : :
4890 : 1776 : *op->resvalue = BoolGetDatum(res);
4891 : : }
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 : 5156 : ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
4917 : : ExprContext *econtext)
4918 : : {
4919 : 5156 : JsonExprState *jsestate = op->d.jsonexpr.jsestate;
4920 : 5156 : JsonExpr *jsexpr = jsestate->jsexpr;
4921 : : Datum item;
4922 : : JsonPath *path;
4923 : 5156 : bool throw_error = jsexpr->on_error->btype == JSON_BEHAVIOR_ERROR;
4924 : 5156 : bool error = false,
4925 : 5156 : empty = false;
4926 : 5156 : int jump_eval_coercion = jsestate->jump_eval_coercion;
4927 : 5156 : char *val_string = NULL;
4928 : :
4929 : : /* Set error/empty to false. */
4930 : 5156 : memset(&jsestate->error, 0, sizeof(NullableDatum));
4931 : 5156 : memset(&jsestate->empty, 0, sizeof(NullableDatum));
4932 : :
4933 : : /* Also reset ErrorSaveContext contents for the next row. */
4934 [ + + ]: 5156 : if (jsestate->escontext.details_wanted)
4935 : : {
4936 : 672 : jsestate->escontext.error_data = NULL;
4937 : 672 : jsestate->escontext.details_wanted = false;
4938 : : }
4939 : 5156 : 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 [ + + + + ]: 5156 : if (jsestate->formatted_expr.isnull || jsestate->pathspec.isnull)
4948 : : {
4949 : 48 : *op->resvalue = (Datum) 0;
4950 : 48 : *op->resnull = true;
4951 : :
4952 [ + + ]: 48 : if (jump_eval_coercion >= 0)
4953 : 4 : return jump_eval_coercion;
4954 : : else
4955 : 44 : return jsestate->jump_end;
4956 : : }
4957 : :
4958 : 5108 : item = jsestate->formatted_expr.value;
4959 : 5108 : path = DatumGetJsonPathP(jsestate->pathspec.value);
4960 : :
4961 [ + + + - ]: 5108 : switch (jsexpr->op)
4962 : : {
4963 : 424 : case JSON_EXISTS_OP:
4964 : : {
4965 : 424 : bool exists = JsonPathExists(item, path,
4966 [ + + ]: 424 : !throw_error ? &error : NULL,
4967 : : jsestate->args);
4968 : :
4969 [ + + ]: 420 : if (!error)
4970 : : {
4971 : 300 : *op->resnull = false;
4972 : 300 : *op->resvalue = BoolGetDatum(exists);
4973 : : }
4974 : : }
4975 : 420 : break;
4976 : :
4977 : 1880 : case JSON_QUERY_OP:
4978 : 1880 : *op->resvalue = JsonPathQuery(item, path, jsexpr->wrapper, &empty,
4979 : 1880 : !throw_error ? &error : NULL,
4980 : : jsestate->args,
4981 [ + + ]: 1880 : jsexpr->column_name);
4982 : :
4983 : 1860 : *op->resnull = (DatumGetPointer(*op->resvalue) == NULL);
4984 : 1860 : break;
4985 : :
4986 : 2804 : case JSON_VALUE_OP:
4987 : : {
4988 : 2804 : JsonbValue *jbv = JsonPathValue(item, path, &empty,
4989 : 2804 : !throw_error ? &error : NULL,
4990 : : jsestate->args,
4991 [ + + ]: 2804 : jsexpr->column_name);
4992 : :
4993 [ + + ]: 2784 : if (jbv == NULL)
4994 : : {
4995 : : /* Will be coerced with json_populate_type(), if needed. */
4996 : 556 : *op->resvalue = (Datum) 0;
4997 : 556 : *op->resnull = true;
4998 : : }
4999 [ + - + - ]: 2228 : else if (!error && !empty)
5000 : : {
5001 [ + + ]: 2228 : if (jsexpr->returning->typid == JSONOID ||
5002 [ + + ]: 2200 : jsexpr->returning->typid == JSONBOID)
5003 : : {
5004 : 80 : val_string = DatumGetCString(DirectFunctionCall1(jsonb_out,
5005 : : JsonbPGetDatum(JsonbValueToJsonb(jbv))));
5006 : 80 : *op->resnull = false;
5007 : : }
5008 [ + + ]: 2148 : else if (jsexpr->use_json_coercion)
5009 : : {
5010 : 76 : *op->resvalue = JsonbPGetDatum(JsonbValueToJsonb(jbv));
5011 : 76 : *op->resnull = false;
5012 : : }
5013 : : else
5014 : : {
5015 : : /* ExecGetJsonValueItemString() sets *op->resnull. */
5016 : 2072 : val_string = ExecGetJsonValueItemString(jbv, op->resnull);
5017 : :
5018 : : /*
5019 : : * Simply convert to the default RETURNING type (text)
5020 : : * if no coercion needed.
5021 : : */
5022 [ + + ]: 2072 : if (!jsexpr->use_io_coercion)
5023 : 92 : *op->resvalue = DirectFunctionCall1(textin,
5024 : : CStringGetDatum(val_string));
5025 : : }
5026 : : }
5027 : 2784 : break;
5028 : : }
5029 : :
5030 : : /* JSON_TABLE_OP can't happen here */
5031 : :
5032 : 0 : default:
5033 [ # # ]: 0 : elog(ERROR, "unrecognized SQL/JSON expression op %d",
5034 : : (int) jsexpr->op);
5035 : : return false;
5036 : : }
5037 : :
5038 : : /*
5039 : : * Coerce the result value to the RETURNING type by calling its input
5040 : : * function.
5041 : : */
5042 [ + + + + ]: 5064 : if (!*op->resnull && jsexpr->use_io_coercion)
5043 : : {
5044 : : FunctionCallInfo fcinfo;
5045 : :
5046 : : Assert(jump_eval_coercion == -1);
5047 : 2060 : fcinfo = jsestate->input_fcinfo;
5048 : : Assert(fcinfo != NULL);
5049 : : Assert(val_string != NULL);
5050 : 2060 : fcinfo->args[0].value = PointerGetDatum(val_string);
5051 : 2060 : fcinfo->args[0].isnull = *op->resnull;
5052 : :
5053 : : /*
5054 : : * Second and third arguments are already set up in
5055 : : * ExecInitJsonExpr().
5056 : : */
5057 : :
5058 : 2060 : fcinfo->isnull = false;
5059 : 2060 : *op->resvalue = FunctionCallInvoke(fcinfo);
5060 [ + - + - : 2020 : if (SOFT_ERROR_OCCURRED(&jsestate->escontext))
+ + ]
5061 : 140 : error = true;
5062 : : }
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 [ + + ]: 5024 : if (empty)
5074 : : {
5075 : 480 : *op->resvalue = (Datum) 0;
5076 : 480 : *op->resnull = true;
5077 [ + - ]: 480 : if (jsexpr->on_empty)
5078 : : {
5079 [ + + ]: 480 : if (jsexpr->on_empty->btype != JSON_BEHAVIOR_ERROR)
5080 : : {
5081 : 440 : jsestate->empty.value = BoolGetDatum(true);
5082 : : /* Set up to catch coercion errors of the ON EMPTY value. */
5083 : 440 : jsestate->escontext.error_occurred = false;
5084 : 440 : jsestate->escontext.details_wanted = true;
5085 : : /* Jump to end if the ON EMPTY behavior is to return NULL */
5086 [ + + ]: 440 : return jsestate->jump_empty >= 0 ? jsestate->jump_empty : jsestate->jump_end;
5087 : : }
5088 : : }
5089 [ # # ]: 0 : else if (jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR)
5090 : : {
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 : : 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 : : }
5099 : :
5100 [ + + ]: 40 : if (jsexpr->column_name)
5101 [ + - ]: 8 : ereport(ERROR,
5102 : : errcode(ERRCODE_NO_SQL_JSON_ITEM),
5103 : : errmsg("no SQL/JSON item found for specified path of column \"%s\"",
5104 : : jsexpr->column_name));
5105 : : else
5106 [ + - ]: 32 : ereport(ERROR,
5107 : : errcode(ERRCODE_NO_SQL_JSON_ITEM),
5108 : : errmsg("no SQL/JSON item found for specified path"));
5109 : : }
5110 : :
5111 : : /*
5112 : : * ON ERROR. Wouldn't get here if the behavior is ERROR, because they
5113 : : * would have already been thrown.
5114 : : */
5115 [ + + ]: 4544 : if (error)
5116 : : {
5117 : : Assert(!throw_error);
5118 : 420 : *op->resvalue = (Datum) 0;
5119 : 420 : *op->resnull = true;
5120 : 420 : jsestate->error.value = BoolGetDatum(true);
5121 : : /* Set up to catch coercion errors of the ON ERROR value. */
5122 : 420 : jsestate->escontext.error_occurred = false;
5123 : 420 : jsestate->escontext.details_wanted = true;
5124 : : /* Jump to end if the ON ERROR behavior is to return NULL */
5125 [ + + ]: 420 : return jsestate->jump_error >= 0 ? jsestate->jump_error : jsestate->jump_end;
5126 : : }
5127 : :
5128 [ + + ]: 4124 : return jump_eval_coercion >= 0 ? jump_eval_coercion : jsestate->jump_end;
5129 : : }
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 : 2072 : ExecGetJsonValueItemString(JsonbValue *item, bool *resnull)
5138 : : {
5139 : 2072 : *resnull = false;
5140 : :
5141 : : /* get coercion state reference and datum of the corresponding SQL type */
5142 [ - + + + : 2072 : switch (item->type)
+ - - ]
5143 : : {
5144 : 0 : case jbvNull:
5145 : 0 : *resnull = true;
5146 : 0 : return NULL;
5147 : :
5148 : 242 : case jbvString:
5149 : : {
5150 : 242 : char *str = palloc(item->val.string.len + 1);
5151 : :
5152 : 242 : memcpy(str, item->val.string.val, item->val.string.len);
5153 : 242 : str[item->val.string.len] = '\0';
5154 : 242 : return str;
5155 : : }
5156 : :
5157 : 1690 : case jbvNumeric:
5158 : 1690 : return DatumGetCString(DirectFunctionCall1(numeric_out,
5159 : : NumericGetDatum(item->val.numeric)));
5160 : :
5161 : 112 : case jbvBool:
5162 : 112 : return DatumGetCString(DirectFunctionCall1(boolout,
5163 : : BoolGetDatum(item->val.boolean)));
5164 : :
5165 : 28 : case jbvDatetime:
5166 [ + + + + : 28 : switch (item->val.datetime.typid)
+ - ]
5167 : : {
5168 : 4 : case DATEOID:
5169 : 4 : return DatumGetCString(DirectFunctionCall1(date_out,
5170 : : item->val.datetime.value));
5171 : 4 : case TIMEOID:
5172 : 4 : return DatumGetCString(DirectFunctionCall1(time_out,
5173 : : item->val.datetime.value));
5174 : 4 : case TIMETZOID:
5175 : 4 : return DatumGetCString(DirectFunctionCall1(timetz_out,
5176 : : item->val.datetime.value));
5177 : 4 : case TIMESTAMPOID:
5178 : 4 : return DatumGetCString(DirectFunctionCall1(timestamp_out,
5179 : : item->val.datetime.value));
5180 : 12 : case TIMESTAMPTZOID:
5181 : 12 : return DatumGetCString(DirectFunctionCall1(timestamptz_out,
5182 : : item->val.datetime.value));
5183 : 0 : default:
5184 [ # # ]: 0 : elog(ERROR, "unexpected jsonb datetime type oid %u",
5185 : : item->val.datetime.typid);
5186 : : }
5187 : : break;
5188 : :
5189 : 0 : case jbvArray:
5190 : : case jbvObject:
5191 : : case jbvBinary:
5192 : 0 : return DatumGetCString(DirectFunctionCall1(jsonb_out,
5193 : : JsonbPGetDatum(JsonbValueToJsonb(item))));
5194 : :
5195 : 0 : default:
5196 [ # # ]: 0 : elog(ERROR, "unexpected jsonb value type %d", item->type);
5197 : : }
5198 : :
5199 : : Assert(false);
5200 : : *resnull = true;
5201 : : return NULL;
5202 : : }
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 : 1400 : ExecEvalJsonCoercion(ExprState *state, ExprEvalStep *op,
5213 : : ExprContext *econtext)
5214 : : {
5215 : 1400 : 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 [ + + + - : 1400 : if (SOFT_ERROR_OCCURRED(escontext))
- + ]
5225 : : {
5226 : 0 : *op->resnull = true;
5227 : 0 : *op->resvalue = (Datum) 0;
5228 : 0 : return;
5229 : : }
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 [ + + ]: 1400 : if (op->d.jsonexpr_coercion.exists_coerce)
5242 : : {
5243 [ + + ]: 120 : if (op->d.jsonexpr_coercion.exists_cast_to_int)
5244 : : {
5245 : : /* Check domain constraints if any. */
5246 [ + + ]: 84 : if (op->d.jsonexpr_coercion.exists_check_domain &&
5247 [ + - ]: 16 : !domain_check_safe(*op->resvalue, *op->resnull,
5248 : : op->d.jsonexpr_coercion.targettype,
5249 : : &op->d.jsonexpr_coercion.json_coercion_cache,
5250 : : econtext->ecxt_per_query_memory,
5251 : : (Node *) escontext))
5252 : : {
5253 : 12 : *op->resnull = true;
5254 : 12 : *op->resvalue = (Datum) 0;
5255 : : }
5256 : : else
5257 : 68 : *op->resvalue = DirectFunctionCall1(bool_int4, *op->resvalue);
5258 : 80 : return;
5259 : : }
5260 : :
5261 [ + + ]: 36 : *op->resvalue = DirectFunctionCall1(jsonb_in,
5262 : : DatumGetBool(*op->resvalue) ?
5263 : : CStringGetDatum("true") :
5264 : : CStringGetDatum("false"));
5265 : : }
5266 : :
5267 : 1212 : *op->resvalue = json_populate_type(*op->resvalue, JSONBOID,
5268 : : op->d.jsonexpr_coercion.targettype,
5269 : : op->d.jsonexpr_coercion.targettypmod,
5270 : : &op->d.jsonexpr_coercion.json_coercion_cache,
5271 : : econtext->ecxt_per_query_memory,
5272 : : op->resnull,
5273 : 1316 : op->d.jsonexpr_coercion.omit_quotes,
5274 : : (Node *) escontext);
5275 : : }
5276 : :
5277 : : static char *
5278 : 92 : GetJsonBehaviorValueString(JsonBehavior *behavior)
5279 : : {
5280 : : /*
5281 : : * The order of array elements must correspond to the order of
5282 : : * JsonBehaviorType members.
5283 : : */
5284 : 92 : const char *behavior_names[] =
5285 : : {
5286 : : "NULL",
5287 : : "ERROR",
5288 : : "EMPTY",
5289 : : "TRUE",
5290 : : "FALSE",
5291 : : "UNKNOWN",
5292 : : "EMPTY ARRAY",
5293 : : "EMPTY OBJECT",
5294 : : "DEFAULT"
5295 : : };
5296 : :
5297 : 92 : return pstrdup(behavior_names[behavior->btype]);
5298 : : }
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 : 1648 : ExecEvalJsonCoercionFinish(ExprState *state, ExprEvalStep *op)
5307 : : {
5308 : 1648 : JsonExprState *jsestate = op->d.jsonexpr.jsestate;
5309 : :
5310 [ + - + - : 1648 : if (SOFT_ERROR_OCCURRED(&jsestate->escontext))
+ + ]
5311 : : {
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 [ + + ]: 444 : if (DatumGetBool(jsestate->error.value))
5319 [ + - ]: 64 : ereport(ERROR,
5320 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
5321 : : /*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */
5322 : : errmsg("could not coerce %s expression (%s) to the RETURNING type",
5323 : : "ON ERROR",
5324 : : GetJsonBehaviorValueString(jsestate->jsexpr->on_error)),
5325 : : errdetail("%s", jsestate->escontext.error_data->message)));
5326 [ + + ]: 380 : else if (DatumGetBool(jsestate->empty.value))
5327 [ + - ]: 28 : ereport(ERROR,
5328 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
5329 : : /*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */
5330 : : errmsg("could not coerce %s expression (%s) to the RETURNING type",
5331 : : "ON EMPTY",
5332 : : GetJsonBehaviorValueString(jsestate->jsexpr->on_empty)),
5333 : : errdetail("%s", jsestate->escontext.error_data->message)));
5334 : :
5335 : 352 : *op->resvalue = (Datum) 0;
5336 : 352 : *op->resnull = true;
5337 : :
5338 : 352 : 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 : 352 : jsestate->escontext.error_occurred = false;
5345 : 352 : jsestate->escontext.details_wanted = true;
5346 : : }
5347 : 1556 : }
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 : 1367 : ExecEvalGroupingFunc(ExprState *state, ExprEvalStep *op)
5360 : : {
5361 : 1367 : AggState *aggstate = castNode(AggState, state->parent);
5362 : 1367 : int result = 0;
5363 : 1367 : Bitmapset *grouped_cols = aggstate->grouped_cols;
5364 : : ListCell *lc;
5365 : :
5366 [ + + + + : 3345 : foreach(lc, op->d.grouping_func.clauses)
+ + ]
5367 : : {
5368 : 1978 : int attnum = lfirst_int(lc);
5369 : :
5370 : 1978 : result <<= 1;
5371 : :
5372 [ + + ]: 1978 : if (!bms_is_member(attnum, grouped_cols))
5373 : 751 : result |= 1;
5374 : : }
5375 : :
5376 : 1367 : *op->resvalue = Int32GetDatum(result);
5377 : 1367 : *op->resnull = false;
5378 : 1367 : }
5379 : :
5380 : : /*
5381 : : * ExecEvalMergeSupportFunc
5382 : : *
5383 : : * Returns information about the current MERGE action for its RETURNING list.
5384 : : */
5385 : : void
5386 : 343 : ExecEvalMergeSupportFunc(ExprState *state, ExprEvalStep *op,
5387 : : ExprContext *econtext)
5388 : : {
5389 : 343 : ModifyTableState *mtstate = castNode(ModifyTableState, state->parent);
5390 : 343 : MergeActionState *relaction = mtstate->mt_merge_action;
5391 : :
5392 [ - + ]: 343 : if (!relaction)
5393 [ # # ]: 0 : elog(ERROR, "no merge action in progress");
5394 : :
5395 : : /* Return the MERGE action ("INSERT", "UPDATE", or "DELETE") */
5396 [ + + + - : 343 : switch (relaction->mas_action->commandType)
- ]
5397 : : {
5398 : 115 : case CMD_INSERT:
5399 : 115 : *op->resvalue = PointerGetDatum(cstring_to_text_with_len("INSERT", 6));
5400 : 115 : *op->resnull = false;
5401 : 115 : break;
5402 : 136 : case CMD_UPDATE:
5403 : 136 : *op->resvalue = PointerGetDatum(cstring_to_text_with_len("UPDATE", 6));
5404 : 136 : *op->resnull = false;
5405 : 136 : break;
5406 : 92 : case CMD_DELETE:
5407 : 92 : *op->resvalue = PointerGetDatum(cstring_to_text_with_len("DELETE", 6));
5408 : 92 : *op->resnull = false;
5409 : 92 : break;
5410 : 0 : case CMD_NOTHING:
5411 [ # # ]: 0 : elog(ERROR, "unexpected merge action: DO NOTHING");
5412 : : break;
5413 : 0 : default:
5414 [ # # ]: 0 : elog(ERROR, "unrecognized commandType: %d",
5415 : : (int) relaction->mas_action->commandType);
5416 : : }
5417 : 343 : }
5418 : :
5419 : : /*
5420 : : * Hand off evaluation of a subplan to nodeSubplan.c
5421 : : */
5422 : : void
5423 : 2221069 : ExecEvalSubPlan(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
5424 : : {
5425 : 2221069 : SubPlanState *sstate = op->d.subplan.sstate;
5426 : :
5427 : : /* could potentially be nested, so make sure there's enough stack */
5428 : 2221069 : check_stack_depth();
5429 : :
5430 : 2221069 : *op->resvalue = ExecSubPlan(sstate, econtext, op->resnull);
5431 : 2221065 : }
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 : 35004 : ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
5441 : : {
5442 : 35004 : Var *variable = op->d.wholerow.var;
5443 : 35004 : TupleTableSlot *slot = NULL;
5444 : : TupleDesc output_tupdesc;
5445 : : MemoryContext oldcontext;
5446 : : HeapTupleHeader dtuple;
5447 : : HeapTuple tuple;
5448 : :
5449 : : /* This was checked by ExecInitExpr */
5450 : : Assert(variable->varattno == InvalidAttrNumber);
5451 : :
5452 : : /* Get the input slot we want */
5453 [ + + + ]: 35004 : switch (variable->varno)
5454 : : {
5455 : 72 : case INNER_VAR:
5456 : : /* get the tuple from the inner node */
5457 : 72 : slot = econtext->ecxt_innertuple;
5458 : 72 : break;
5459 : :
5460 : 12 : case OUTER_VAR:
5461 : : /* get the tuple from the outer node */
5462 : 12 : slot = econtext->ecxt_outertuple;
5463 : 12 : break;
5464 : :
5465 : : /* INDEX_VAR is handled by default case */
5466 : :
5467 : 34920 : 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 [ + + + - ]: 34920 : switch (variable->varreturningtype)
5477 : : {
5478 : 34576 : case VAR_RETURNING_DEFAULT:
5479 : 34576 : slot = econtext->ecxt_scantuple;
5480 : 34576 : break;
5481 : :
5482 : 172 : case VAR_RETURNING_OLD:
5483 [ + + ]: 172 : if (state->flags & EEO_FLAG_OLD_IS_NULL)
5484 : : {
5485 : 48 : *op->resvalue = (Datum) 0;
5486 : 48 : *op->resnull = true;
5487 : 48 : return;
5488 : : }
5489 : 124 : slot = econtext->ecxt_oldtuple;
5490 : 124 : break;
5491 : :
5492 : 172 : case VAR_RETURNING_NEW:
5493 [ + + ]: 172 : if (state->flags & EEO_FLAG_NEW_IS_NULL)
5494 : : {
5495 : 24 : *op->resvalue = (Datum) 0;
5496 : 24 : *op->resnull = true;
5497 : 24 : return;
5498 : : }
5499 : 148 : slot = econtext->ecxt_newtuple;
5500 : 148 : break;
5501 : : }
5502 : 34848 : break;
5503 : : }
5504 : :
5505 : : /* Apply the junkfilter if any */
5506 [ + + ]: 34932 : if (op->d.wholerow.junkFilter != NULL)
5507 : 45 : 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 [ + + ]: 34932 : if (op->d.wholerow.first)
5517 : : {
5518 : : /* optimistically assume we don't need slow path */
5519 : 2452 : 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 [ + + ]: 2452 : if (variable->vartype != RECORDOID)
5526 : : {
5527 : : TupleDesc var_tupdesc;
5528 : : 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 : 1712 : var_tupdesc = lookup_rowtype_tupdesc_domain(variable->vartype,
5546 : : -1, false);
5547 : :
5548 : 1712 : slot_tupdesc = slot->tts_tupleDescriptor;
5549 : :
5550 [ - + ]: 1712 : if (var_tupdesc->natts != slot_tupdesc->natts)
5551 [ # # ]: 0 : ereport(ERROR,
5552 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
5553 : : errmsg("table row type and query-specified row type do not match"),
5554 : : errdetail_plural("Table row contains %d attribute, but query expects %d.",
5555 : : "Table row contains %d attributes, but query expects %d.",
5556 : : slot_tupdesc->natts,
5557 : : slot_tupdesc->natts,
5558 : : var_tupdesc->natts)));
5559 : :
5560 [ + + ]: 6558 : for (int i = 0; i < var_tupdesc->natts; i++)
5561 : : {
5562 : 4846 : Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i);
5563 : 4846 : Form_pg_attribute sattr = TupleDescAttr(slot_tupdesc, i);
5564 : :
5565 [ + + ]: 4846 : if (vattr->atttypid == sattr->atttypid)
5566 : 4837 : continue; /* no worries */
5567 [ - + ]: 9 : if (!vattr->attisdropped)
5568 [ # # ]: 0 : ereport(ERROR,
5569 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
5570 : : errmsg("table row type and query-specified row type do not match"),
5571 : : errdetail("Table has type %s at ordinal position %d, but query expects %s.",
5572 : : format_type_be(sattr->atttypid),
5573 : : i + 1,
5574 : : format_type_be(vattr->atttypid))));
5575 : :
5576 [ + + ]: 9 : if (vattr->attlen != sattr->attlen ||
5577 [ - + ]: 3 : vattr->attalign != sattr->attalign)
5578 : 6 : op->d.wholerow.slow = true; /* need to check for nulls */
5579 : : }
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 : 1712 : oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
5587 : 1712 : output_tupdesc = CreateTupleDescCopy(var_tupdesc);
5588 : 1712 : MemoryContextSwitchTo(oldcontext);
5589 : :
5590 [ + - ]: 1712 : ReleaseTupleDesc(var_tupdesc);
5591 : : }
5592 : : else
5593 : : {
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 : 740 : oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
5600 : 740 : output_tupdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor);
5601 : 740 : 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 : 740 : output_tupdesc->tdtypeid = RECORDOID;
5609 : 740 : 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 [ + - ]: 740 : if (econtext->ecxt_estate &&
5626 [ + - ]: 740 : variable->varno <= econtext->ecxt_estate->es_range_table_size)
5627 : : {
5628 : 740 : RangeTblEntry *rte = exec_rt_fetch(variable->varno,
5629 : 740 : econtext->ecxt_estate);
5630 : :
5631 [ + - ]: 740 : if (rte->eref)
5632 : 740 : ExecTypeSetColNames(output_tupdesc, rte->eref->colnames);
5633 : : }
5634 : : }
5635 : :
5636 : : /* Bless the tupdesc if needed, and save it in the execution state */
5637 : 2452 : op->d.wholerow.tupdesc = BlessTupleDesc(output_tupdesc);
5638 : :
5639 : 2452 : op->d.wholerow.first = false;
5640 : : }
5641 : :
5642 : : /*
5643 : : * Make sure all columns of the slot are accessible in the slot's
5644 : : * Datum/isnull arrays.
5645 : : */
5646 : 34932 : slot_getallattrs(slot);
5647 : :
5648 [ + + ]: 34932 : if (op->d.wholerow.slow)
5649 : : {
5650 : : /* Check to see if any dropped attributes are non-null */
5651 : 3 : TupleDesc tupleDesc = slot->tts_tupleDescriptor;
5652 : 3 : TupleDesc var_tupdesc = op->d.wholerow.tupdesc;
5653 : :
5654 : : Assert(var_tupdesc->natts == tupleDesc->natts);
5655 : :
5656 [ + + ]: 18 : for (int i = 0; i < var_tupdesc->natts; i++)
5657 : : {
5658 : 15 : CompactAttribute *vattr = TupleDescCompactAttr(var_tupdesc, i);
5659 : 15 : CompactAttribute *sattr = TupleDescCompactAttr(tupleDesc, i);
5660 : :
5661 [ + + ]: 15 : if (!vattr->attisdropped)
5662 : 6 : continue; /* already checked non-dropped cols */
5663 [ + - ]: 9 : if (slot->tts_isnull[i])
5664 : 9 : continue; /* null is always okay */
5665 [ # # ]: 0 : if (vattr->attlen != sattr->attlen ||
5666 [ # # ]: 0 : vattr->attalignby != sattr->attalignby)
5667 [ # # ]: 0 : ereport(ERROR,
5668 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
5669 : : errmsg("table row type and query-specified row type do not match"),
5670 : : errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
5671 : : i + 1)));
5672 : : }
5673 : : }
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 : 34932 : tuple = toast_build_flattened_tuple(slot->tts_tupleDescriptor,
5681 : 34932 : slot->tts_values,
5682 : 34932 : slot->tts_isnull);
5683 : 34932 : 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 : 34932 : heap_tuple_set_datum(dtuple, op->d.wholerow.tupdesc, tuple->t_len);
5696 : :
5697 : 34932 : *op->resvalue = PointerGetDatum(dtuple);
5698 : 34932 : *op->resnull = false;
5699 : : }
5700 : :
5701 : : void
5702 : 6695476 : ExecEvalSysVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext,
5703 : : TupleTableSlot *slot)
5704 : : {
5705 : : Datum d;
5706 : :
5707 : : /* OLD/NEW system attribute is NULL if OLD/NEW row is NULL */
5708 [ + + ]: 6695476 : if ((op->d.var.varreturningtype == VAR_RETURNING_OLD &&
5709 [ + + ]: 152 : state->flags & EEO_FLAG_OLD_IS_NULL) ||
5710 [ + + ]: 6695420 : (op->d.var.varreturningtype == VAR_RETURNING_NEW &&
5711 [ + + ]: 152 : state->flags & EEO_FLAG_NEW_IS_NULL))
5712 : : {
5713 : 104 : *op->resvalue = (Datum) 0;
5714 : 104 : *op->resnull = true;
5715 : 104 : return;
5716 : : }
5717 : :
5718 : : /* slot_getsysattr has sufficient defenses against bad attnums */
5719 : 6695372 : d = slot_getsysattr(slot,
5720 : : op->d.var.attnum,
5721 : : op->resnull);
5722 : 6695364 : *op->resvalue = d;
5723 : : /* this ought to be unreachable, but it's cheap enough to check */
5724 [ - + ]: 6695364 : if (unlikely(*op->resnull))
5725 [ # # ]: 0 : elog(ERROR, "failed to fetch attribute from slot");
5726 : : }
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 : 40930 : ExecAggInitGroup(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup,
5734 : : ExprContext *aggcontext)
5735 : : {
5736 : 40930 : FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
5737 : : 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 : 40930 : oldContext = MemoryContextSwitchTo(aggcontext->ecxt_per_tuple_memory);
5746 : 81860 : pergroup->transValue = datumCopy(fcinfo->args[1].value,
5747 : 40930 : pertrans->transtypeByVal,
5748 : 40930 : pertrans->transtypeLen);
5749 : 40930 : pergroup->transValueIsNull = false;
5750 : 40930 : pergroup->noTransValue = false;
5751 : 40930 : MemoryContextSwitchTo(oldContext);
5752 : 40930 : }
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 : 41182 : ExecAggCopyTransValue(AggState *aggstate, AggStatePerTrans pertrans,
5787 : : Datum newValue, bool newValueIsNull,
5788 : : Datum oldValue, bool oldValueIsNull)
5789 : : {
5790 : : Assert(newValue != oldValue);
5791 : :
5792 [ + - ]: 41182 : if (!newValueIsNull)
5793 : : {
5794 : 41182 : MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory);
5795 [ + + ]: 41182 : if (DatumIsReadWriteExpandedObject(newValue,
5796 : : false,
5797 [ + + ]: 40378 : pertrans->transtypeLen) &&
5798 [ + + ]: 112 : MemoryContextGetParent(DatumGetEOHP(newValue)->eoh_context) == CurrentMemoryContext)
5799 : : /* do nothing */ ;
5800 : : else
5801 : 41178 : newValue = datumCopy(newValue,
5802 : 41178 : pertrans->transtypeByVal,
5803 : 41178 : pertrans->transtypeLen);
5804 : : }
5805 : : else
5806 : : {
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 : : }
5814 : :
5815 [ + + ]: 41182 : if (!oldValueIsNull)
5816 : : {
5817 [ + + - + ]: 41106 : if (DatumIsReadWriteExpandedObject(oldValue,
5818 : : false,
5819 : : pertrans->transtypeLen))
5820 : 0 : DeleteExpandedObject(oldValue);
5821 : : else
5822 : 41106 : pfree(DatumGetPointer(oldValue));
5823 : : }
5824 : :
5825 : 41182 : return newValue;
5826 : : }
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 : 243696 : ExecEvalPreOrderedDistinctSingle(AggState *aggstate, AggStatePerTrans pertrans)
5836 : : {
5837 : 243696 : Datum value = pertrans->transfn_fcinfo->args[1].value;
5838 : 243696 : bool isnull = pertrans->transfn_fcinfo->args[1].isnull;
5839 : :
5840 [ + + ]: 243696 : if (!pertrans->haslast ||
5841 [ + + ]: 231510 : pertrans->lastisnull != isnull ||
5842 [ + + + + ]: 231491 : (!isnull && !DatumGetBool(FunctionCall2Coll(&pertrans->equalfnOne,
5843 : : pertrans->aggCollation,
5844 : : pertrans->lastdatum, value))))
5845 : : {
5846 [ + + + + ]: 67729 : if (pertrans->haslast && !pertrans->inputtypeByVal &&
5847 [ + - ]: 16994 : !pertrans->lastisnull)
5848 : 16994 : pfree(DatumGetPointer(pertrans->lastdatum));
5849 : :
5850 : 67729 : pertrans->haslast = true;
5851 [ + + ]: 67729 : if (!isnull)
5852 : : {
5853 : : MemoryContext oldContext;
5854 : :
5855 : 67706 : oldContext = MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory);
5856 : :
5857 : 135412 : pertrans->lastdatum = datumCopy(value, pertrans->inputtypeByVal,
5858 : 67706 : pertrans->inputtypeLen);
5859 : :
5860 : 67706 : MemoryContextSwitchTo(oldContext);
5861 : : }
5862 : : else
5863 : 23 : pertrans->lastdatum = (Datum) 0;
5864 : 67729 : pertrans->lastisnull = isnull;
5865 : 67729 : return true;
5866 : : }
5867 : :
5868 : 175967 : return false;
5869 : : }
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 : 500 : ExecEvalPreOrderedDistinctMulti(AggState *aggstate, AggStatePerTrans pertrans)
5879 : : {
5880 : 500 : ExprContext *tmpcontext = aggstate->tmpcontext;
5881 : 500 : bool isdistinct = false; /* for now */
5882 : : TupleTableSlot *save_outer;
5883 : : TupleTableSlot *save_inner;
5884 : :
5885 [ + + ]: 1924 : for (int i = 0; i < pertrans->numTransInputs; i++)
5886 : : {
5887 : 1424 : pertrans->sortslot->tts_values[i] = pertrans->transfn_fcinfo->args[i + 1].value;
5888 : 1424 : pertrans->sortslot->tts_isnull[i] = pertrans->transfn_fcinfo->args[i + 1].isnull;
5889 : : }
5890 : :
5891 : 500 : ExecClearTuple(pertrans->sortslot);
5892 : 500 : pertrans->sortslot->tts_nvalid = pertrans->numInputs;
5893 : 500 : ExecStoreVirtualTuple(pertrans->sortslot);
5894 : :
5895 : : /* save the previous slots before we overwrite them */
5896 : 500 : save_outer = tmpcontext->ecxt_outertuple;
5897 : 500 : save_inner = tmpcontext->ecxt_innertuple;
5898 : :
5899 : 500 : tmpcontext->ecxt_outertuple = pertrans->sortslot;
5900 : 500 : tmpcontext->ecxt_innertuple = pertrans->uniqslot;
5901 : :
5902 [ + + ]: 500 : if (!pertrans->haslast ||
5903 [ + + ]: 428 : !ExecQual(pertrans->equalfnMulti, tmpcontext))
5904 : : {
5905 [ + + ]: 220 : if (pertrans->haslast)
5906 : 148 : ExecClearTuple(pertrans->uniqslot);
5907 : :
5908 : 220 : pertrans->haslast = true;
5909 : 220 : ExecCopySlot(pertrans->uniqslot, pertrans->sortslot);
5910 : :
5911 : 220 : isdistinct = true;
5912 : : }
5913 : :
5914 : : /* restore the original slots */
5915 : 500 : tmpcontext->ecxt_outertuple = save_outer;
5916 : 500 : tmpcontext->ecxt_innertuple = save_inner;
5917 : :
5918 : 500 : return isdistinct;
5919 : : }
5920 : :
5921 : : /*
5922 : : * Invoke ordered transition function, with a datum argument.
5923 : : */
5924 : : void
5925 : 559741 : ExecEvalAggOrderedTransDatum(ExprState *state, ExprEvalStep *op,
5926 : : ExprContext *econtext)
5927 : : {
5928 : 559741 : AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
5929 : 559741 : int setno = op->d.agg_trans.setno;
5930 : :
5931 : 559741 : tuplesort_putdatum(pertrans->sortstates[setno],
5932 : 559741 : *op->resvalue, *op->resnull);
5933 : 559741 : }
5934 : :
5935 : : /*
5936 : : * Invoke ordered transition function, with a tuple argument.
5937 : : */
5938 : : void
5939 : 168 : ExecEvalAggOrderedTransTuple(ExprState *state, ExprEvalStep *op,
5940 : : ExprContext *econtext)
5941 : : {
5942 : 168 : AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
5943 : 168 : int setno = op->d.agg_trans.setno;
5944 : :
5945 : 168 : ExecClearTuple(pertrans->sortslot);
5946 : 168 : pertrans->sortslot->tts_nvalid = pertrans->numInputs;
5947 : 168 : ExecStoreVirtualTuple(pertrans->sortslot);
5948 : 168 : tuplesort_puttupleslot(pertrans->sortstates[setno], pertrans->sortslot);
5949 : 168 : }
5950 : :
5951 : : /* implementation of transition function invocation for byval types */
5952 : : static pg_always_inline void
5953 : 24190907 : ExecAggPlainTransByVal(AggState *aggstate, AggStatePerTrans pertrans,
5954 : : AggStatePerGroup pergroup,
5955 : : ExprContext *aggcontext, int setno)
5956 : : {
5957 : 24190907 : FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
5958 : : MemoryContext oldContext;
5959 : : Datum newVal;
5960 : :
5961 : : /* cf. select_current_set() */
5962 : 24190907 : aggstate->curaggcontext = aggcontext;
5963 : 24190907 : aggstate->current_set = setno;
5964 : :
5965 : : /* set up aggstate->curpertrans for AggGetAggref() */
5966 : 24190907 : aggstate->curpertrans = pertrans;
5967 : :
5968 : : /* invoke transition function in per-tuple context */
5969 : 24190907 : oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
5970 : :
5971 : 24190907 : fcinfo->args[0].value = pergroup->transValue;
5972 : 24190907 : fcinfo->args[0].isnull = pergroup->transValueIsNull;
5973 : 24190907 : fcinfo->isnull = false; /* just in case transfn doesn't set it */
5974 : :
5975 : 24190907 : newVal = FunctionCallInvoke(fcinfo);
5976 : :
5977 : 24190855 : pergroup->transValue = newVal;
5978 : 24190855 : pergroup->transValueIsNull = fcinfo->isnull;
5979 : :
5980 : 24190855 : MemoryContextSwitchTo(oldContext);
5981 : 24190855 : }
5982 : :
5983 : : /* implementation of transition function invocation for byref types */
5984 : : static pg_always_inline void
5985 : 2010581 : ExecAggPlainTransByRef(AggState *aggstate, AggStatePerTrans pertrans,
5986 : : AggStatePerGroup pergroup,
5987 : : ExprContext *aggcontext, int setno)
5988 : : {
5989 : 2010581 : FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
5990 : : MemoryContext oldContext;
5991 : : Datum newVal;
5992 : :
5993 : : /* cf. select_current_set() */
5994 : 2010581 : aggstate->curaggcontext = aggcontext;
5995 : 2010581 : aggstate->current_set = setno;
5996 : :
5997 : : /* set up aggstate->curpertrans for AggGetAggref() */
5998 : 2010581 : aggstate->curpertrans = pertrans;
5999 : :
6000 : : /* invoke transition function in per-tuple context */
6001 : 2010581 : oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
6002 : :
6003 : 2010581 : fcinfo->args[0].value = pergroup->transValue;
6004 : 2010581 : fcinfo->args[0].isnull = pergroup->transValueIsNull;
6005 : 2010581 : fcinfo->isnull = false; /* just in case transfn doesn't set it */
6006 : :
6007 : 2010581 : 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 [ + + ]: 2010577 : if (DatumGetPointer(newVal) != DatumGetPointer(pergroup->transValue))
6024 : 41182 : newVal = ExecAggCopyTransValue(aggstate, pertrans,
6025 : 41182 : newVal, fcinfo->isnull,
6026 : : pergroup->transValue,
6027 : 41182 : pergroup->transValueIsNull);
6028 : :
6029 : 2010577 : pergroup->transValue = newVal;
6030 : 2010577 : pergroup->transValueIsNull = fcinfo->isnull;
6031 : :
6032 : 2010577 : MemoryContextSwitchTo(oldContext);
6033 : 2010577 : }
|