Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * execExpr.c
4 : : * Expression evaluation infrastructure.
5 : : *
6 : : * During executor startup, we compile each expression tree (which has
7 : : * previously been processed by the parser and planner) into an ExprState,
8 : : * using ExecInitExpr() et al. This converts the tree into a flat array
9 : : * of ExprEvalSteps, which may be thought of as instructions in a program.
10 : : * At runtime, we'll execute steps, starting with the first, until we reach
11 : : * an EEOP_DONE_{RETURN|NO_RETURN} opcode.
12 : : *
13 : : * This file contains the "compilation" logic. It is independent of the
14 : : * specific execution technology we use (switch statement, computed goto,
15 : : * JIT compilation, etc).
16 : : *
17 : : * See src/backend/executor/README for some background, specifically the
18 : : * "Expression Trees and ExprState nodes", "Expression Initialization",
19 : : * and "Expression Evaluation" sections.
20 : : *
21 : : *
22 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
23 : : * Portions Copyright (c) 1994, Regents of the University of California
24 : : *
25 : : *
26 : : * IDENTIFICATION
27 : : * src/backend/executor/execExpr.c
28 : : *
29 : : *-------------------------------------------------------------------------
30 : : */
31 : : #include "postgres.h"
32 : :
33 : : #include "access/nbtree.h"
34 : : #include "catalog/objectaccess.h"
35 : : #include "catalog/pg_proc.h"
36 : : #include "catalog/pg_type.h"
37 : : #include "executor/execExpr.h"
38 : : #include "executor/nodeSubplan.h"
39 : : #include "funcapi.h"
40 : : #include "jit/jit.h"
41 : : #include "miscadmin.h"
42 : : #include "nodes/makefuncs.h"
43 : : #include "nodes/nodeFuncs.h"
44 : : #include "nodes/subscripting.h"
45 : : #include "optimizer/optimizer.h"
46 : : #include "pgstat.h"
47 : : #include "utils/acl.h"
48 : : #include "utils/array.h"
49 : : #include "utils/builtins.h"
50 : : #include "utils/jsonfuncs.h"
51 : : #include "utils/jsonpath.h"
52 : : #include "utils/lsyscache.h"
53 : : #include "utils/typcache.h"
54 : :
55 : :
56 : : typedef struct ExprSetupInfo
57 : : {
58 : : /*
59 : : * Highest attribute numbers fetched from inner/outer/scan/old/new tuple
60 : : * slots:
61 : : */
62 : : AttrNumber last_inner;
63 : : AttrNumber last_outer;
64 : : AttrNumber last_scan;
65 : : AttrNumber last_old;
66 : : AttrNumber last_new;
67 : : /* MULTIEXPR SubPlan nodes appearing in the expression: */
68 : : List *multiexpr_subplans;
69 : : } ExprSetupInfo;
70 : :
71 : : static void ExecReadyExpr(ExprState *state);
72 : : static void ExecInitExprRec(Expr *node, ExprState *state,
73 : : Datum *resv, bool *resnull);
74 : : static void ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args,
75 : : Oid funcid, Oid inputcollid,
76 : : ExprState *state);
77 : : static void ExecInitSubPlanExpr(SubPlan *subplan,
78 : : ExprState *state,
79 : : Datum *resv, bool *resnull);
80 : : static void ExecCreateExprSetupSteps(ExprState *state, Node *node);
81 : : static void ExecPushExprSetupSteps(ExprState *state, ExprSetupInfo *info);
82 : : static bool expr_setup_walker(Node *node, ExprSetupInfo *info);
83 : : static bool ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op);
84 : : static void ExecInitWholeRowVar(ExprEvalStep *scratch, Var *variable,
85 : : ExprState *state);
86 : : static void ExecInitSubscriptingRef(ExprEvalStep *scratch,
87 : : SubscriptingRef *sbsref,
88 : : ExprState *state,
89 : : Datum *resv, bool *resnull);
90 : : static bool isAssignmentIndirectionExpr(Expr *expr);
91 : : static void ExecInitCoerceToDomain(ExprEvalStep *scratch, CoerceToDomain *ctest,
92 : : ExprState *state,
93 : : Datum *resv, bool *resnull);
94 : : static void ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
95 : : ExprEvalStep *scratch,
96 : : FunctionCallInfo fcinfo, AggStatePerTrans pertrans,
97 : : int transno, int setno, int setoff, bool ishash,
98 : : bool nullcheck);
99 : : static void ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
100 : : Datum *resv, bool *resnull,
101 : : ExprEvalStep *scratch);
102 : : static void ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
103 : : ErrorSaveContext *escontext, bool omit_quotes,
104 : : bool exists_coerce,
105 : : Datum *resv, bool *resnull);
106 : :
107 : :
108 : : /*
109 : : * ExecInitExpr: prepare an expression tree for execution
110 : : *
111 : : * This function builds and returns an ExprState implementing the given
112 : : * Expr node tree. The return ExprState can then be handed to ExecEvalExpr
113 : : * for execution. Because the Expr tree itself is read-only as far as
114 : : * ExecInitExpr and ExecEvalExpr are concerned, several different executions
115 : : * of the same plan tree can occur concurrently. (But note that an ExprState
116 : : * does mutate at runtime, so it can't be re-used concurrently.)
117 : : *
118 : : * This must be called in a memory context that will last as long as repeated
119 : : * executions of the expression are needed. Typically the context will be
120 : : * the same as the per-query context of the associated ExprContext.
121 : : *
122 : : * Any Aggref, WindowFunc, or SubPlan nodes found in the tree are added to
123 : : * the lists of such nodes held by the parent PlanState.
124 : : *
125 : : * Note: there is no ExecEndExpr function; we assume that any resource
126 : : * cleanup needed will be handled by just releasing the memory context
127 : : * in which the state tree is built. Functions that require additional
128 : : * cleanup work can register a shutdown callback in the ExprContext.
129 : : *
130 : : * 'node' is the root of the expression tree to compile.
131 : : * 'parent' is the PlanState node that owns the expression.
132 : : *
133 : : * 'parent' may be NULL if we are preparing an expression that is not
134 : : * associated with a plan tree. (If so, it can't have aggs or subplans.)
135 : : * Such cases should usually come through ExecPrepareExpr, not directly here.
136 : : *
137 : : * Also, if 'node' is NULL, we just return NULL. This is convenient for some
138 : : * callers that may or may not have an expression that needs to be compiled.
139 : : * Note that a NULL ExprState pointer *cannot* be handed to ExecEvalExpr,
140 : : * although ExecQual and ExecCheck will accept one (and treat it as "true").
141 : : */
142 : : ExprState *
143 : 658875 : ExecInitExpr(Expr *node, PlanState *parent)
144 : : {
145 : : ExprState *state;
146 : 658875 : ExprEvalStep scratch = {0};
147 : :
148 : : /* Special case: NULL expression produces a NULL ExprState pointer */
149 [ + + ]: 658875 : if (node == NULL)
150 : 38384 : return NULL;
151 : :
152 : : /* Initialize ExprState with empty step list */
153 : 620491 : state = makeNode(ExprState);
154 : 620491 : state->expr = node;
155 : 620491 : state->parent = parent;
156 : 620491 : state->ext_params = NULL;
157 : :
158 : : /* Insert setup steps as needed */
159 : 620491 : ExecCreateExprSetupSteps(state, (Node *) node);
160 : :
161 : : /* Compile the expression proper */
162 : 620491 : ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
163 : :
164 : : /* Finally, append a DONE step */
165 : 620467 : scratch.opcode = EEOP_DONE_RETURN;
166 : 620467 : ExprEvalPushStep(state, &scratch);
167 : :
168 : 620467 : ExecReadyExpr(state);
169 : :
170 : 620467 : return state;
171 : : }
172 : :
173 : : /*
174 : : * ExecInitExprWithParams: prepare a standalone expression tree for execution
175 : : *
176 : : * This is the same as ExecInitExpr, except that there is no parent PlanState,
177 : : * and instead we may have a ParamListInfo describing PARAM_EXTERN Params.
178 : : */
179 : : ExprState *
180 : 53405 : ExecInitExprWithParams(Expr *node, ParamListInfo ext_params)
181 : : {
182 : : ExprState *state;
183 : 53405 : ExprEvalStep scratch = {0};
184 : :
185 : : /* Special case: NULL expression produces a NULL ExprState pointer */
186 [ - + ]: 53405 : if (node == NULL)
187 : 0 : return NULL;
188 : :
189 : : /* Initialize ExprState with empty step list */
190 : 53405 : state = makeNode(ExprState);
191 : 53405 : state->expr = node;
192 : 53405 : state->parent = NULL;
193 : 53405 : state->ext_params = ext_params;
194 : :
195 : : /* Insert setup steps as needed */
196 : 53405 : ExecCreateExprSetupSteps(state, (Node *) node);
197 : :
198 : : /* Compile the expression proper */
199 : 53405 : ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
200 : :
201 : : /* Finally, append a DONE step */
202 : 53405 : scratch.opcode = EEOP_DONE_RETURN;
203 : 53405 : ExprEvalPushStep(state, &scratch);
204 : :
205 : 53405 : ExecReadyExpr(state);
206 : :
207 : 53405 : return state;
208 : : }
209 : :
210 : : /*
211 : : * ExecInitQual: prepare a qual for execution by ExecQual
212 : : *
213 : : * Prepares for the evaluation of a conjunctive boolean expression (qual list
214 : : * with implicit AND semantics) that returns true if none of the
215 : : * subexpressions are false.
216 : : *
217 : : * We must return true if the list is empty. Since that's a very common case,
218 : : * we optimize it a bit further by translating to a NULL ExprState pointer
219 : : * rather than setting up an ExprState that computes constant TRUE. (Some
220 : : * especially hot-spot callers of ExecQual detect this and avoid calling
221 : : * ExecQual at all.)
222 : : *
223 : : * If any of the subexpressions yield NULL, then the result of the conjunction
224 : : * is false. This makes ExecQual primarily useful for evaluating WHERE
225 : : * clauses, since SQL specifies that tuples with null WHERE results do not
226 : : * get selected.
227 : : */
228 : : ExprState *
229 : 1189890 : ExecInitQual(List *qual, PlanState *parent)
230 : : {
231 : : ExprState *state;
232 : 1189890 : ExprEvalStep scratch = {0};
233 : 1189890 : List *adjust_jumps = NIL;
234 : :
235 : : /* short-circuit (here and in ExecQual) for empty restriction list */
236 [ + + ]: 1189890 : if (qual == NIL)
237 : 869236 : return NULL;
238 : :
239 : : Assert(IsA(qual, List));
240 : :
241 : 320654 : state = makeNode(ExprState);
242 : 320654 : state->expr = (Expr *) qual;
243 : 320654 : state->parent = parent;
244 : 320654 : state->ext_params = NULL;
245 : :
246 : : /* mark expression as to be used with ExecQual() */
247 : 320654 : state->flags = EEO_FLAG_IS_QUAL;
248 : :
249 : : /* Insert setup steps as needed */
250 : 320654 : ExecCreateExprSetupSteps(state, (Node *) qual);
251 : :
252 : : /*
253 : : * ExecQual() needs to return false for an expression returning NULL. That
254 : : * allows us to short-circuit the evaluation the first time a NULL is
255 : : * encountered. As qual evaluation is a hot-path this warrants using a
256 : : * special opcode for qual evaluation that's simpler than BOOL_AND (which
257 : : * has more complex NULL handling).
258 : : */
259 : 320654 : scratch.opcode = EEOP_QUAL;
260 : :
261 : : /*
262 : : * We can use ExprState's resvalue/resnull as target for each qual expr.
263 : : */
264 : 320654 : scratch.resvalue = &state->resvalue;
265 : 320654 : scratch.resnull = &state->resnull;
266 : :
267 [ + - + + : 1035507 : foreach_ptr(Expr, node, qual)
+ + ]
268 : : {
269 : : /* first evaluate expression */
270 : 394207 : ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
271 : :
272 : : /* then emit EEOP_QUAL to detect if it's false (or null) */
273 : 394203 : scratch.d.qualexpr.jumpdone = -1;
274 : 394203 : ExprEvalPushStep(state, &scratch);
275 : 394203 : adjust_jumps = lappend_int(adjust_jumps,
276 : 394203 : state->steps_len - 1);
277 : : }
278 : :
279 : : /* adjust jump targets */
280 [ + - + + : 1035503 : foreach_int(jump, adjust_jumps)
+ + ]
281 : : {
282 : 394203 : ExprEvalStep *as = &state->steps[jump];
283 : :
284 : : Assert(as->opcode == EEOP_QUAL);
285 : : Assert(as->d.qualexpr.jumpdone == -1);
286 : 394203 : as->d.qualexpr.jumpdone = state->steps_len;
287 : : }
288 : :
289 : : /*
290 : : * At the end, we don't need to do anything more. The last qual expr must
291 : : * have yielded TRUE, and since its result is stored in the desired output
292 : : * location, we're done.
293 : : */
294 : 320650 : scratch.opcode = EEOP_DONE_RETURN;
295 : 320650 : ExprEvalPushStep(state, &scratch);
296 : :
297 : 320650 : ExecReadyExpr(state);
298 : :
299 : 320650 : return state;
300 : : }
301 : :
302 : : /*
303 : : * ExecInitCheck: prepare a check constraint for execution by ExecCheck
304 : : *
305 : : * This is much like ExecInitQual/ExecQual, except that a null result from
306 : : * the conjunction is treated as TRUE. This behavior is appropriate for
307 : : * evaluating CHECK constraints, since SQL specifies that NULL constraint
308 : : * conditions are not failures.
309 : : *
310 : : * Note that like ExecInitQual, this expects input in implicit-AND format.
311 : : * Users of ExecCheck that have expressions in normal explicit-AND format
312 : : * can just apply ExecInitExpr to produce suitable input for ExecCheck.
313 : : */
314 : : ExprState *
315 : 2394 : ExecInitCheck(List *qual, PlanState *parent)
316 : : {
317 : : /* short-circuit (here and in ExecCheck) for empty restriction list */
318 [ + + ]: 2394 : if (qual == NIL)
319 : 56 : return NULL;
320 : :
321 : : Assert(IsA(qual, List));
322 : :
323 : : /*
324 : : * Just convert the implicit-AND list to an explicit AND (if there's more
325 : : * than one entry), and compile normally. Unlike ExecQual, we can't
326 : : * short-circuit on NULL results, so the regular AND behavior is needed.
327 : : */
328 : 2338 : return ExecInitExpr(make_ands_explicit(qual), parent);
329 : : }
330 : :
331 : : /*
332 : : * Call ExecInitExpr() on a list of expressions, return a list of ExprStates.
333 : : */
334 : : List *
335 : 310606 : ExecInitExprList(List *nodes, PlanState *parent)
336 : : {
337 : 310606 : List *result = NIL;
338 : : ListCell *lc;
339 : :
340 [ + + + + : 582201 : foreach(lc, nodes)
+ + ]
341 : : {
342 : 271595 : Expr *e = lfirst(lc);
343 : :
344 : 271595 : result = lappend(result, ExecInitExpr(e, parent));
345 : : }
346 : :
347 : 310606 : return result;
348 : : }
349 : :
350 : : /*
351 : : * ExecBuildProjectionInfo
352 : : *
353 : : * Build a ProjectionInfo node for evaluating the given tlist in the given
354 : : * econtext, and storing the result into the tuple slot. (Caller must have
355 : : * ensured that tuple slot has a descriptor matching the tlist!)
356 : : *
357 : : * inputDesc can be NULL, but if it is not, we check to see whether simple
358 : : * Vars in the tlist match the descriptor. It is important to provide
359 : : * inputDesc for relation-scan plan nodes, as a cross check that the relation
360 : : * hasn't been changed since the plan was made. At higher levels of a plan,
361 : : * there is no need to recheck.
362 : : *
363 : : * This is implemented by internally building an ExprState that performs the
364 : : * whole projection in one go.
365 : : *
366 : : * Caution: before PG v10, the targetList was a list of ExprStates; now it
367 : : * should be the planner-created targetlist, since we do the compilation here.
368 : : */
369 : : ProjectionInfo *
370 : 506762 : ExecBuildProjectionInfo(List *targetList,
371 : : ExprContext *econtext,
372 : : TupleTableSlot *slot,
373 : : PlanState *parent,
374 : : TupleDesc inputDesc)
375 : : {
376 : 506762 : ProjectionInfo *projInfo = makeNode(ProjectionInfo);
377 : : ExprState *state;
378 : 506762 : ExprEvalStep scratch = {0};
379 : : ListCell *lc;
380 : :
381 : 506762 : projInfo->pi_exprContext = econtext;
382 : : /* We embed ExprState into ProjectionInfo instead of doing extra palloc */
383 : 506762 : projInfo->pi_state.type = T_ExprState;
384 : 506762 : state = &projInfo->pi_state;
385 : 506762 : state->expr = (Expr *) targetList;
386 : 506762 : state->parent = parent;
387 : 506762 : state->ext_params = NULL;
388 : :
389 : 506762 : state->resultslot = slot;
390 : :
391 : : /* Insert setup steps as needed */
392 : 506762 : ExecCreateExprSetupSteps(state, (Node *) targetList);
393 : :
394 : : /* Now compile each tlist column */
395 [ + + + + : 1890666 : foreach(lc, targetList)
+ + ]
396 : : {
397 : 1383947 : TargetEntry *tle = lfirst_node(TargetEntry, lc);
398 : 1383947 : Var *variable = NULL;
399 : 1383947 : AttrNumber attnum = 0;
400 : 1383947 : bool isSafeVar = false;
401 : :
402 : : /*
403 : : * If tlist expression is a safe non-system Var, use the fast-path
404 : : * ASSIGN_*_VAR opcodes. "Safe" means that we don't need to apply
405 : : * CheckVarSlotCompatibility() during plan startup. If a source slot
406 : : * was provided, we make the equivalent tests here; if a slot was not
407 : : * provided, we assume that no check is needed because we're dealing
408 : : * with a non-relation-scan-level expression.
409 : : */
410 [ + - ]: 1383947 : if (tle->expr != NULL &&
411 [ + + ]: 1383947 : IsA(tle->expr, Var) &&
412 [ + + ]: 859846 : ((Var *) tle->expr)->varattno > 0)
413 : : {
414 : : /* Non-system Var, but how safe is it? */
415 : 801213 : variable = (Var *) tle->expr;
416 : 801213 : attnum = variable->varattno;
417 : :
418 [ + + ]: 801213 : if (inputDesc == NULL)
419 : 479171 : isSafeVar = true; /* can't check, just assume OK */
420 [ + + ]: 322042 : else if (attnum <= inputDesc->natts)
421 : : {
422 : 321594 : Form_pg_attribute attr = TupleDescAttr(inputDesc, attnum - 1);
423 : :
424 : : /*
425 : : * If user attribute is dropped or has a type mismatch, don't
426 : : * use ASSIGN_*_VAR. Instead let the normal expression
427 : : * machinery handle it (which'll possibly error out).
428 : : */
429 [ + + + + ]: 321594 : if (!attr->attisdropped && variable->vartype == attr->atttypid)
430 : : {
431 : 320958 : isSafeVar = true;
432 : : }
433 : : }
434 : : }
435 : :
436 [ + + ]: 1383947 : if (isSafeVar)
437 : : {
438 : : /* Fast-path: just generate an EEOP_ASSIGN_*_VAR step */
439 [ + + + ]: 800129 : switch (variable->varno)
440 : : {
441 : 149581 : case INNER_VAR:
442 : : /* get the tuple from the inner node */
443 : 149581 : scratch.opcode = EEOP_ASSIGN_INNER_VAR;
444 : 149581 : break;
445 : :
446 : 328655 : case OUTER_VAR:
447 : : /* get the tuple from the outer node */
448 : 328655 : scratch.opcode = EEOP_ASSIGN_OUTER_VAR;
449 : 328655 : break;
450 : :
451 : : /* INDEX_VAR is handled by default case */
452 : :
453 : 321893 : default:
454 : :
455 : : /*
456 : : * Get the tuple from the relation being scanned, or the
457 : : * old/new tuple slot, if old/new values were requested.
458 : : */
459 [ + + + - ]: 321893 : switch (variable->varreturningtype)
460 : : {
461 : 320459 : case VAR_RETURNING_DEFAULT:
462 : 320459 : scratch.opcode = EEOP_ASSIGN_SCAN_VAR;
463 : 320459 : break;
464 : 722 : case VAR_RETURNING_OLD:
465 : 722 : scratch.opcode = EEOP_ASSIGN_OLD_VAR;
466 : 722 : state->flags |= EEO_FLAG_HAS_OLD;
467 : 722 : break;
468 : 712 : case VAR_RETURNING_NEW:
469 : 712 : scratch.opcode = EEOP_ASSIGN_NEW_VAR;
470 : 712 : state->flags |= EEO_FLAG_HAS_NEW;
471 : 712 : break;
472 : : }
473 : 321893 : break;
474 : : }
475 : :
476 : 800129 : scratch.d.assign_var.attnum = attnum - 1;
477 : 800129 : scratch.d.assign_var.resultnum = tle->resno - 1;
478 : 800129 : ExprEvalPushStep(state, &scratch);
479 : : }
480 : : else
481 : : {
482 : : /*
483 : : * Otherwise, compile the column expression normally.
484 : : *
485 : : * We can't tell the expression to evaluate directly into the
486 : : * result slot, as the result slot (and the exprstate for that
487 : : * matter) can change between executions. We instead evaluate
488 : : * into the ExprState's resvalue/resnull and then move.
489 : : */
490 : 583818 : ExecInitExprRec(tle->expr, state,
491 : : &state->resvalue, &state->resnull);
492 : :
493 : : /*
494 : : * Column might be referenced multiple times in upper nodes, so
495 : : * force value to R/O - but only if it could be an expanded datum.
496 : : */
497 [ + + ]: 583779 : if (get_typlen(exprType((Node *) tle->expr)) == -1)
498 : 218147 : scratch.opcode = EEOP_ASSIGN_TMP_MAKE_RO;
499 : : else
500 : 365632 : scratch.opcode = EEOP_ASSIGN_TMP;
501 : 583779 : scratch.d.assign_tmp.resultnum = tle->resno - 1;
502 : 583779 : ExprEvalPushStep(state, &scratch);
503 : : }
504 : : }
505 : :
506 : 506719 : scratch.opcode = EEOP_DONE_NO_RETURN;
507 : 506719 : ExprEvalPushStep(state, &scratch);
508 : :
509 : 506719 : ExecReadyExpr(state);
510 : :
511 : 506719 : return projInfo;
512 : : }
513 : :
514 : : /*
515 : : * ExecBuildUpdateProjection
516 : : *
517 : : * Build a ProjectionInfo node for constructing a new tuple during UPDATE.
518 : : * The projection will be executed in the given econtext and the result will
519 : : * be stored into the given tuple slot. (Caller must have ensured that tuple
520 : : * slot has a descriptor matching the target rel!)
521 : : *
522 : : * When evalTargetList is false, targetList contains the UPDATE ... SET
523 : : * expressions that have already been computed by a subplan node; the values
524 : : * from this tlist are assumed to be available in the "outer" tuple slot.
525 : : * When evalTargetList is true, targetList contains the UPDATE ... SET
526 : : * expressions that must be computed (which could contain references to
527 : : * the outer, inner, or scan tuple slots).
528 : : *
529 : : * In either case, targetColnos contains a list of the target column numbers
530 : : * corresponding to the non-resjunk entries of targetList. The tlist values
531 : : * are assigned into these columns of the result tuple slot. Target columns
532 : : * not listed in targetColnos are filled from the UPDATE's old tuple, which
533 : : * is assumed to be available in the "scan" tuple slot.
534 : : *
535 : : * targetList can also contain resjunk columns. These must be evaluated
536 : : * if evalTargetList is true, but their values are discarded.
537 : : *
538 : : * relDesc must describe the relation we intend to update.
539 : : *
540 : : * This is basically a specialized variant of ExecBuildProjectionInfo.
541 : : * However, it also performs sanity checks equivalent to ExecCheckPlanOutput.
542 : : * Since we never make a normal tlist equivalent to the whole
543 : : * tuple-to-be-assigned, there is no convenient way to apply
544 : : * ExecCheckPlanOutput, so we must do our safety checks here.
545 : : */
546 : : ProjectionInfo *
547 : 10474 : ExecBuildUpdateProjection(List *targetList,
548 : : bool evalTargetList,
549 : : List *targetColnos,
550 : : TupleDesc relDesc,
551 : : ExprContext *econtext,
552 : : TupleTableSlot *slot,
553 : : PlanState *parent)
554 : : {
555 : 10474 : ProjectionInfo *projInfo = makeNode(ProjectionInfo);
556 : : ExprState *state;
557 : : int nAssignableCols;
558 : : bool sawJunk;
559 : : Bitmapset *assignedCols;
560 : 10474 : ExprSetupInfo deform = {0, 0, 0, 0, 0, NIL};
561 : 10474 : ExprEvalStep scratch = {0};
562 : : int outerattnum;
563 : : ListCell *lc,
564 : : *lc2;
565 : :
566 : 10474 : projInfo->pi_exprContext = econtext;
567 : : /* We embed ExprState into ProjectionInfo instead of doing extra palloc */
568 : 10474 : projInfo->pi_state.type = T_ExprState;
569 : 10474 : state = &projInfo->pi_state;
570 [ + + ]: 10474 : if (evalTargetList)
571 : 1961 : state->expr = (Expr *) targetList;
572 : : else
573 : 8513 : state->expr = NULL; /* not used */
574 : 10474 : state->parent = parent;
575 : 10474 : state->ext_params = NULL;
576 : :
577 : 10474 : state->resultslot = slot;
578 : :
579 : : /*
580 : : * Examine the targetList to see how many non-junk columns there are, and
581 : : * to verify that the non-junk columns come before the junk ones.
582 : : */
583 : 10474 : nAssignableCols = 0;
584 : 10474 : sawJunk = false;
585 [ + - + + : 34710 : foreach(lc, targetList)
+ + ]
586 : : {
587 : 24236 : TargetEntry *tle = lfirst_node(TargetEntry, lc);
588 : :
589 [ + + ]: 24236 : if (tle->resjunk)
590 : 10809 : sawJunk = true;
591 : : else
592 : : {
593 [ - + ]: 13427 : if (sawJunk)
594 [ # # ]: 0 : elog(ERROR, "subplan target list is out of order");
595 : 13427 : nAssignableCols++;
596 : : }
597 : : }
598 : :
599 : : /* We should have one targetColnos entry per non-junk column */
600 [ - + ]: 10474 : if (nAssignableCols != list_length(targetColnos))
601 [ # # ]: 0 : elog(ERROR, "targetColnos does not match subplan target list");
602 : :
603 : : /*
604 : : * Build a bitmapset of the columns in targetColnos. (We could just use
605 : : * list_member_int() tests, but that risks O(N^2) behavior with many
606 : : * columns.)
607 : : */
608 : 10474 : assignedCols = NULL;
609 [ + + + + : 23901 : foreach(lc, targetColnos)
+ + ]
610 : : {
611 : 13427 : AttrNumber targetattnum = lfirst_int(lc);
612 : :
613 : 13427 : assignedCols = bms_add_member(assignedCols, targetattnum);
614 : : }
615 : :
616 : : /*
617 : : * We need to insert EEOP_*_FETCHSOME steps to ensure the input tuples are
618 : : * sufficiently deconstructed. The scan tuple must be deconstructed at
619 : : * least as far as the last old column we need.
620 : : */
621 [ + + ]: 17716 : for (int attnum = relDesc->natts; attnum > 0; attnum--)
622 : : {
623 : 16048 : CompactAttribute *attr = TupleDescCompactAttr(relDesc, attnum - 1);
624 : :
625 [ + + ]: 16048 : if (attr->attisdropped)
626 : 152 : continue;
627 [ + + ]: 15896 : if (bms_is_member(attnum, assignedCols))
628 : 7090 : continue;
629 : 8806 : deform.last_scan = attnum;
630 : 8806 : break;
631 : : }
632 : :
633 : : /*
634 : : * If we're actually evaluating the tlist, incorporate its input
635 : : * requirements too; otherwise, we'll just need to fetch the appropriate
636 : : * number of columns of the "outer" tuple.
637 : : */
638 [ + + ]: 10474 : if (evalTargetList)
639 : 1961 : expr_setup_walker((Node *) targetList, &deform);
640 : : else
641 : 8513 : deform.last_outer = nAssignableCols;
642 : :
643 : 10474 : ExecPushExprSetupSteps(state, &deform);
644 : :
645 : : /*
646 : : * Now generate code to evaluate the tlist's assignable expressions or
647 : : * fetch them from the outer tuple, incidentally validating that they'll
648 : : * be of the right data type. The checks above ensure that the forboth()
649 : : * will iterate over exactly the non-junk columns. Note that we don't
650 : : * bother evaluating any remaining resjunk columns.
651 : : */
652 : 10474 : outerattnum = 0;
653 [ + - + + : 23901 : forboth(lc, targetList, lc2, targetColnos)
+ + + + +
+ + + +
+ ]
654 : : {
655 : 13427 : TargetEntry *tle = lfirst_node(TargetEntry, lc);
656 : 13427 : AttrNumber targetattnum = lfirst_int(lc2);
657 : : Form_pg_attribute attr;
658 : :
659 : : Assert(!tle->resjunk);
660 : :
661 : : /*
662 : : * Apply sanity checks comparable to ExecCheckPlanOutput().
663 : : */
664 [ + - - + ]: 13427 : if (targetattnum <= 0 || targetattnum > relDesc->natts)
665 [ # # ]: 0 : ereport(ERROR,
666 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
667 : : errmsg("table row type and query-specified row type do not match"),
668 : : errdetail("Query has too many columns.")));
669 : 13427 : attr = TupleDescAttr(relDesc, targetattnum - 1);
670 : :
671 [ - + ]: 13427 : if (attr->attisdropped)
672 [ # # ]: 0 : ereport(ERROR,
673 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
674 : : errmsg("table row type and query-specified row type do not match"),
675 : : errdetail("Query provides a value for a dropped column at ordinal position %d.",
676 : : targetattnum)));
677 [ - + ]: 13427 : if (exprType((Node *) tle->expr) != attr->atttypid)
678 [ # # ]: 0 : ereport(ERROR,
679 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
680 : : errmsg("table row type and query-specified row type do not match"),
681 : : errdetail("Table has type %s at ordinal position %d, but query expects %s.",
682 : : format_type_be(attr->atttypid),
683 : : targetattnum,
684 : : format_type_be(exprType((Node *) tle->expr)))));
685 : :
686 : : /* OK, generate code to perform the assignment. */
687 [ + + ]: 13427 : if (evalTargetList)
688 : : {
689 : : /*
690 : : * We must evaluate the TLE's expression and assign it. We do not
691 : : * bother jumping through hoops for "safe" Vars like
692 : : * ExecBuildProjectionInfo does; this is a relatively less-used
693 : : * path and it doesn't seem worth expending code for that.
694 : : */
695 : 2611 : ExecInitExprRec(tle->expr, state,
696 : : &state->resvalue, &state->resnull);
697 : : /* Needn't worry about read-only-ness here, either. */
698 : 2611 : scratch.opcode = EEOP_ASSIGN_TMP;
699 : 2611 : scratch.d.assign_tmp.resultnum = targetattnum - 1;
700 : 2611 : ExprEvalPushStep(state, &scratch);
701 : : }
702 : : else
703 : : {
704 : : /* Just assign from the outer tuple. */
705 : 10816 : scratch.opcode = EEOP_ASSIGN_OUTER_VAR;
706 : 10816 : scratch.d.assign_var.attnum = outerattnum;
707 : 10816 : scratch.d.assign_var.resultnum = targetattnum - 1;
708 : 10816 : ExprEvalPushStep(state, &scratch);
709 : : }
710 : 13427 : outerattnum++;
711 : : }
712 : :
713 : : /*
714 : : * Now generate code to copy over any old columns that were not assigned
715 : : * to, and to ensure that dropped columns are set to NULL.
716 : : */
717 [ + + ]: 93261 : for (int attnum = 1; attnum <= relDesc->natts; attnum++)
718 : : {
719 : 82787 : CompactAttribute *attr = TupleDescCompactAttr(relDesc, attnum - 1);
720 : :
721 [ + + ]: 82787 : if (attr->attisdropped)
722 : : {
723 : : /* Put a null into the ExprState's resvalue/resnull ... */
724 : 294 : scratch.opcode = EEOP_CONST;
725 : 294 : scratch.resvalue = &state->resvalue;
726 : 294 : scratch.resnull = &state->resnull;
727 : 294 : scratch.d.constval.value = (Datum) 0;
728 : 294 : scratch.d.constval.isnull = true;
729 : 294 : ExprEvalPushStep(state, &scratch);
730 : : /* ... then assign it to the result slot */
731 : 294 : scratch.opcode = EEOP_ASSIGN_TMP;
732 : 294 : scratch.d.assign_tmp.resultnum = attnum - 1;
733 : 294 : ExprEvalPushStep(state, &scratch);
734 : : }
735 [ + + ]: 82493 : else if (!bms_is_member(attnum, assignedCols))
736 : : {
737 : : /* Certainly the right type, so needn't check */
738 : 69066 : scratch.opcode = EEOP_ASSIGN_SCAN_VAR;
739 : 69066 : scratch.d.assign_var.attnum = attnum - 1;
740 : 69066 : scratch.d.assign_var.resultnum = attnum - 1;
741 : 69066 : ExprEvalPushStep(state, &scratch);
742 : : }
743 : : }
744 : :
745 : 10474 : scratch.opcode = EEOP_DONE_NO_RETURN;
746 : 10474 : ExprEvalPushStep(state, &scratch);
747 : :
748 : 10474 : ExecReadyExpr(state);
749 : :
750 : 10474 : return projInfo;
751 : : }
752 : :
753 : : /*
754 : : * ExecPrepareExpr --- initialize for expression execution outside a normal
755 : : * Plan tree context.
756 : : *
757 : : * This differs from ExecInitExpr in that we don't assume the caller is
758 : : * already running in the EState's per-query context. Also, we run the
759 : : * passed expression tree through expression_planner() to prepare it for
760 : : * execution. (In ordinary Plan trees the regular planning process will have
761 : : * made the appropriate transformations on expressions, but for standalone
762 : : * expressions this won't have happened.)
763 : : */
764 : : ExprState *
765 : 15496 : ExecPrepareExpr(Expr *node, EState *estate)
766 : : {
767 : : ExprState *result;
768 : : MemoryContext oldcontext;
769 : :
770 : 15496 : oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
771 : :
772 : 15496 : node = expression_planner(node);
773 : :
774 : 15496 : result = ExecInitExpr(node, NULL);
775 : :
776 : 15492 : MemoryContextSwitchTo(oldcontext);
777 : :
778 : 15492 : return result;
779 : : }
780 : :
781 : : /*
782 : : * ExecPrepareQual --- initialize for qual execution outside a normal
783 : : * Plan tree context.
784 : : *
785 : : * This differs from ExecInitQual in that we don't assume the caller is
786 : : * already running in the EState's per-query context. Also, we run the
787 : : * passed expression tree through expression_planner() to prepare it for
788 : : * execution. (In ordinary Plan trees the regular planning process will have
789 : : * made the appropriate transformations on expressions, but for standalone
790 : : * expressions this won't have happened.)
791 : : */
792 : : ExprState *
793 : 37863 : ExecPrepareQual(List *qual, EState *estate)
794 : : {
795 : : ExprState *result;
796 : : MemoryContext oldcontext;
797 : :
798 : 37863 : oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
799 : :
800 : 37863 : qual = (List *) expression_planner((Expr *) qual);
801 : :
802 : 37863 : result = ExecInitQual(qual, NULL);
803 : :
804 : 37863 : MemoryContextSwitchTo(oldcontext);
805 : :
806 : 37863 : return result;
807 : : }
808 : :
809 : : /*
810 : : * ExecPrepareCheck -- initialize check constraint for execution outside a
811 : : * normal Plan tree context.
812 : : *
813 : : * See ExecPrepareExpr() and ExecInitCheck() for details.
814 : : */
815 : : ExprState *
816 : 2394 : ExecPrepareCheck(List *qual, EState *estate)
817 : : {
818 : : ExprState *result;
819 : : MemoryContext oldcontext;
820 : :
821 : 2394 : oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
822 : :
823 : 2394 : qual = (List *) expression_planner((Expr *) qual);
824 : :
825 : 2394 : result = ExecInitCheck(qual, NULL);
826 : :
827 : 2394 : MemoryContextSwitchTo(oldcontext);
828 : :
829 : 2394 : return result;
830 : : }
831 : :
832 : : /*
833 : : * Call ExecPrepareExpr() on each member of a list of Exprs, and return
834 : : * a list of ExprStates.
835 : : *
836 : : * See ExecPrepareExpr() for details.
837 : : */
838 : : List *
839 : 9482 : ExecPrepareExprList(List *nodes, EState *estate)
840 : : {
841 : 9482 : List *result = NIL;
842 : : MemoryContext oldcontext;
843 : : ListCell *lc;
844 : :
845 : : /* Ensure that the list cell nodes are in the right context too */
846 : 9482 : oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
847 : :
848 [ + + + + : 19336 : foreach(lc, nodes)
+ + ]
849 : : {
850 : 9854 : Expr *e = (Expr *) lfirst(lc);
851 : :
852 : 9854 : result = lappend(result, ExecPrepareExpr(e, estate));
853 : : }
854 : :
855 : 9482 : MemoryContextSwitchTo(oldcontext);
856 : :
857 : 9482 : return result;
858 : : }
859 : :
860 : : /*
861 : : * ExecCheck - evaluate a check constraint
862 : : *
863 : : * For check constraints, a null result is taken as TRUE, ie the constraint
864 : : * passes.
865 : : *
866 : : * The check constraint may have been prepared with ExecInitCheck
867 : : * (possibly via ExecPrepareCheck) if the caller had it in implicit-AND
868 : : * format, but a regular boolean expression prepared with ExecInitExpr or
869 : : * ExecPrepareExpr works too.
870 : : */
871 : : bool
872 : 67580 : ExecCheck(ExprState *state, ExprContext *econtext)
873 : : {
874 : : Datum ret;
875 : : bool isnull;
876 : :
877 : : /* short-circuit (here and in ExecInitCheck) for empty restriction list */
878 [ + + ]: 67580 : if (state == NULL)
879 : 56 : return true;
880 : :
881 : : /* verify that expression was not compiled using ExecInitQual */
882 : : Assert(!(state->flags & EEO_FLAG_IS_QUAL));
883 : :
884 : 67524 : ret = ExecEvalExprSwitchContext(state, econtext, &isnull);
885 : :
886 [ + + ]: 67499 : if (isnull)
887 : 1877 : return true;
888 : :
889 : 65622 : return DatumGetBool(ret);
890 : : }
891 : :
892 : : /*
893 : : * Prepare a compiled expression for execution. This has to be called for
894 : : * every ExprState before it can be executed.
895 : : *
896 : : * NB: While this currently only calls ExecReadyInterpretedExpr(),
897 : : * this will likely get extended to further expression evaluation methods.
898 : : * Therefore this should be used instead of directly calling
899 : : * ExecReadyInterpretedExpr().
900 : : */
901 : : static void
902 : 1630213 : ExecReadyExpr(ExprState *state)
903 : : {
904 [ - + ]: 1630213 : if (jit_compile_expr(state))
905 : 0 : return;
906 : :
907 : 1630213 : ExecReadyInterpretedExpr(state);
908 : : }
909 : :
910 : : /*
911 : : * Append the steps necessary for the evaluation of node to ExprState->steps,
912 : : * possibly recursing into sub-expressions of node.
913 : : *
914 : : * node - expression to evaluate
915 : : * state - ExprState to whose ->steps to append the necessary operations
916 : : * resv / resnull - where to store the result of the node into
917 : : */
918 : : static void
919 : 3498160 : ExecInitExprRec(Expr *node, ExprState *state,
920 : : Datum *resv, bool *resnull)
921 : : {
922 : 3498160 : ExprEvalStep scratch = {0};
923 : :
924 : : /* Guard against stack overflow due to overly complex expressions */
925 : 3498160 : check_stack_depth();
926 : :
927 : : /* Step's output location is always what the caller gave us */
928 : : Assert(resv != NULL && resnull != NULL);
929 : 3498160 : scratch.resvalue = resv;
930 : 3498160 : scratch.resnull = resnull;
931 : :
932 : : /* cases should be ordered as they are in enum NodeTag */
933 [ + + + + : 3498160 : switch (nodeTag(node))
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + - ]
934 : : {
935 : 926655 : case T_Var:
936 : : {
937 : 926655 : Var *variable = (Var *) node;
938 : :
939 [ + + ]: 926655 : if (variable->varattno == InvalidAttrNumber)
940 : : {
941 : : /* whole-row Var */
942 : 3548 : ExecInitWholeRowVar(&scratch, variable, state);
943 : : }
944 [ + + ]: 923107 : else if (variable->varattno <= 0)
945 : : {
946 : : /* system column */
947 : 59570 : scratch.d.var.attnum = variable->varattno;
948 : 59570 : scratch.d.var.vartype = variable->vartype;
949 : 59570 : scratch.d.var.varreturningtype = variable->varreturningtype;
950 [ + + + ]: 59570 : switch (variable->varno)
951 : : {
952 : 4 : case INNER_VAR:
953 : 4 : scratch.opcode = EEOP_INNER_SYSVAR;
954 : 4 : break;
955 : 8 : case OUTER_VAR:
956 : 8 : scratch.opcode = EEOP_OUTER_SYSVAR;
957 : 8 : break;
958 : :
959 : : /* INDEX_VAR is handled by default case */
960 : :
961 : 59558 : default:
962 [ + + + - ]: 59558 : switch (variable->varreturningtype)
963 : : {
964 : 59126 : case VAR_RETURNING_DEFAULT:
965 : 59126 : scratch.opcode = EEOP_SCAN_SYSVAR;
966 : 59126 : break;
967 : 216 : case VAR_RETURNING_OLD:
968 : 216 : scratch.opcode = EEOP_OLD_SYSVAR;
969 : 216 : state->flags |= EEO_FLAG_HAS_OLD;
970 : 216 : break;
971 : 216 : case VAR_RETURNING_NEW:
972 : 216 : scratch.opcode = EEOP_NEW_SYSVAR;
973 : 216 : state->flags |= EEO_FLAG_HAS_NEW;
974 : 216 : break;
975 : : }
976 : 59558 : break;
977 : : }
978 : : }
979 : : else
980 : : {
981 : : /* regular user column */
982 : 863537 : scratch.d.var.attnum = variable->varattno - 1;
983 : 863537 : scratch.d.var.vartype = variable->vartype;
984 : 863537 : scratch.d.var.varreturningtype = variable->varreturningtype;
985 [ + + + ]: 863537 : switch (variable->varno)
986 : : {
987 : 121277 : case INNER_VAR:
988 : 121277 : scratch.opcode = EEOP_INNER_VAR;
989 : 121277 : break;
990 : 267728 : case OUTER_VAR:
991 : 267728 : scratch.opcode = EEOP_OUTER_VAR;
992 : 267728 : break;
993 : :
994 : : /* INDEX_VAR is handled by default case */
995 : :
996 : 474532 : default:
997 [ + + + - ]: 474532 : switch (variable->varreturningtype)
998 : : {
999 : 474114 : case VAR_RETURNING_DEFAULT:
1000 : 474114 : scratch.opcode = EEOP_SCAN_VAR;
1001 : 474114 : break;
1002 : 215 : case VAR_RETURNING_OLD:
1003 : 215 : scratch.opcode = EEOP_OLD_VAR;
1004 : 215 : state->flags |= EEO_FLAG_HAS_OLD;
1005 : 215 : break;
1006 : 203 : case VAR_RETURNING_NEW:
1007 : 203 : scratch.opcode = EEOP_NEW_VAR;
1008 : 203 : state->flags |= EEO_FLAG_HAS_NEW;
1009 : 203 : break;
1010 : : }
1011 : 474532 : break;
1012 : : }
1013 : : }
1014 : :
1015 : 926655 : ExprEvalPushStep(state, &scratch);
1016 : 926655 : break;
1017 : : }
1018 : :
1019 : 701024 : case T_Const:
1020 : : {
1021 : 701024 : Const *con = (Const *) node;
1022 : :
1023 : 701024 : scratch.opcode = EEOP_CONST;
1024 : 701024 : scratch.d.constval.value = con->constvalue;
1025 : 701024 : scratch.d.constval.isnull = con->constisnull;
1026 : :
1027 : 701024 : ExprEvalPushStep(state, &scratch);
1028 : 701024 : break;
1029 : : }
1030 : :
1031 : 474593 : case T_Param:
1032 : : {
1033 : 474593 : Param *param = (Param *) node;
1034 : : ParamListInfo params;
1035 : :
1036 [ + + - ]: 474593 : switch (param->paramkind)
1037 : : {
1038 : 173116 : case PARAM_EXEC:
1039 : 173116 : scratch.opcode = EEOP_PARAM_EXEC;
1040 : 173116 : scratch.d.param.paramid = param->paramid;
1041 : 173116 : scratch.d.param.paramtype = param->paramtype;
1042 : 173116 : ExprEvalPushStep(state, &scratch);
1043 : 173116 : break;
1044 : 301477 : case PARAM_EXTERN:
1045 : :
1046 : : /*
1047 : : * If we have a relevant ParamCompileHook, use it;
1048 : : * otherwise compile a standard EEOP_PARAM_EXTERN
1049 : : * step. ext_params, if supplied, takes precedence
1050 : : * over info from the parent node's EState (if any).
1051 : : */
1052 [ + + ]: 301477 : if (state->ext_params)
1053 : 52121 : params = state->ext_params;
1054 [ + + ]: 249356 : else if (state->parent &&
1055 [ + - ]: 249253 : state->parent->state)
1056 : 249253 : params = state->parent->state->es_param_list_info;
1057 : : else
1058 : 103 : params = NULL;
1059 [ + + + + ]: 301477 : if (params && params->paramCompile)
1060 : : {
1061 : 106668 : params->paramCompile(params, param, state,
1062 : : resv, resnull);
1063 : : }
1064 : : else
1065 : : {
1066 : 194809 : scratch.opcode = EEOP_PARAM_EXTERN;
1067 : 194809 : scratch.d.param.paramid = param->paramid;
1068 : 194809 : scratch.d.param.paramtype = param->paramtype;
1069 : 194809 : ExprEvalPushStep(state, &scratch);
1070 : : }
1071 : 301477 : break;
1072 : 0 : default:
1073 [ # # ]: 0 : elog(ERROR, "unrecognized paramkind: %d",
1074 : : (int) param->paramkind);
1075 : : break;
1076 : : }
1077 : 474593 : break;
1078 : : }
1079 : :
1080 : 39746 : case T_Aggref:
1081 : : {
1082 : 39746 : Aggref *aggref = (Aggref *) node;
1083 : :
1084 : 39746 : scratch.opcode = EEOP_AGGREF;
1085 : 39746 : scratch.d.aggref.aggno = aggref->aggno;
1086 : :
1087 [ + - + - ]: 39746 : if (state->parent && IsA(state->parent, AggState))
1088 : 39746 : {
1089 : 39746 : AggState *aggstate = (AggState *) state->parent;
1090 : :
1091 : 39746 : aggstate->aggs = lappend(aggstate->aggs, aggref);
1092 : : }
1093 : : else
1094 : : {
1095 : : /* planner messed up */
1096 [ # # ]: 0 : elog(ERROR, "Aggref found in non-Agg plan node");
1097 : : }
1098 : :
1099 : 39746 : ExprEvalPushStep(state, &scratch);
1100 : 39746 : break;
1101 : : }
1102 : :
1103 : 248 : case T_GroupingFunc:
1104 : : {
1105 : 248 : GroupingFunc *grp_node = (GroupingFunc *) node;
1106 : : Agg *agg;
1107 : :
1108 [ + - + - ]: 248 : if (!state->parent || !IsA(state->parent, AggState) ||
1109 [ - + ]: 248 : !IsA(state->parent->plan, Agg))
1110 [ # # ]: 0 : elog(ERROR, "GroupingFunc found in non-Agg plan node");
1111 : :
1112 : 248 : scratch.opcode = EEOP_GROUPING_FUNC;
1113 : :
1114 : 248 : agg = (Agg *) (state->parent->plan);
1115 : :
1116 [ + + ]: 248 : if (agg->groupingSets)
1117 : 185 : scratch.d.grouping_func.clauses = grp_node->cols;
1118 : : else
1119 : 63 : scratch.d.grouping_func.clauses = NIL;
1120 : :
1121 : 248 : ExprEvalPushStep(state, &scratch);
1122 : 248 : break;
1123 : : }
1124 : :
1125 : 2640 : case T_WindowFunc:
1126 : : {
1127 : 2640 : WindowFunc *wfunc = (WindowFunc *) node;
1128 : 2640 : WindowFuncExprState *wfstate = makeNode(WindowFuncExprState);
1129 : :
1130 : 2640 : wfstate->wfunc = wfunc;
1131 : :
1132 [ + - + - ]: 2640 : if (state->parent && IsA(state->parent, WindowAggState))
1133 : 2640 : {
1134 : 2640 : WindowAggState *winstate = (WindowAggState *) state->parent;
1135 : : int nfuncs;
1136 : :
1137 : 2640 : winstate->funcs = lappend(winstate->funcs, wfstate);
1138 : 2640 : nfuncs = ++winstate->numfuncs;
1139 [ + + ]: 2640 : if (wfunc->winagg)
1140 : 1214 : winstate->numaggs++;
1141 : :
1142 : : /* for now initialize agg using old style expressions */
1143 : 2640 : wfstate->args = ExecInitExprList(wfunc->args,
1144 : : state->parent);
1145 : 2640 : wfstate->aggfilter = ExecInitExpr(wfunc->aggfilter,
1146 : : state->parent);
1147 : :
1148 : : /*
1149 : : * Complain if the windowfunc's arguments contain any
1150 : : * windowfuncs; nested window functions are semantically
1151 : : * nonsensical. (This should have been caught earlier,
1152 : : * but we defend against it here anyway.)
1153 : : */
1154 [ - + ]: 2640 : if (nfuncs != winstate->numfuncs)
1155 [ # # ]: 0 : ereport(ERROR,
1156 : : (errcode(ERRCODE_WINDOWING_ERROR),
1157 : : errmsg("window function calls cannot be nested")));
1158 : : }
1159 : : else
1160 : : {
1161 : : /* planner messed up */
1162 [ # # ]: 0 : elog(ERROR, "WindowFunc found in non-WindowAgg plan node");
1163 : : }
1164 : :
1165 : 2640 : scratch.opcode = EEOP_WINDOW_FUNC;
1166 : 2640 : scratch.d.window_func.wfstate = wfstate;
1167 : 2640 : ExprEvalPushStep(state, &scratch);
1168 : 2640 : break;
1169 : : }
1170 : :
1171 : 178 : case T_MergeSupportFunc:
1172 : : {
1173 : : /* must be in a MERGE, else something messed up */
1174 [ + - ]: 178 : if (!state->parent ||
1175 [ + - ]: 178 : !IsA(state->parent, ModifyTableState) ||
1176 [ - + ]: 178 : ((ModifyTableState *) state->parent)->operation != CMD_MERGE)
1177 [ # # ]: 0 : elog(ERROR, "MergeSupportFunc found in non-merge plan node");
1178 : :
1179 : 178 : scratch.opcode = EEOP_MERGE_SUPPORT_FUNC;
1180 : 178 : ExprEvalPushStep(state, &scratch);
1181 : 178 : break;
1182 : : }
1183 : :
1184 : 20310 : case T_SubscriptingRef:
1185 : : {
1186 : 20310 : SubscriptingRef *sbsref = (SubscriptingRef *) node;
1187 : :
1188 : 20310 : ExecInitSubscriptingRef(&scratch, sbsref, state, resv, resnull);
1189 : 20310 : break;
1190 : : }
1191 : :
1192 : 371954 : case T_FuncExpr:
1193 : : {
1194 : 371954 : FuncExpr *func = (FuncExpr *) node;
1195 : :
1196 : 371954 : ExecInitFunc(&scratch, node,
1197 : : func->args, func->funcid, func->inputcollid,
1198 : : state);
1199 : 371891 : ExprEvalPushStep(state, &scratch);
1200 : 371891 : break;
1201 : : }
1202 : :
1203 : 581968 : case T_OpExpr:
1204 : : {
1205 : 581968 : OpExpr *op = (OpExpr *) node;
1206 : :
1207 : 581968 : ExecInitFunc(&scratch, node,
1208 : : op->args, op->opfuncid, op->inputcollid,
1209 : : state);
1210 : 581964 : ExprEvalPushStep(state, &scratch);
1211 : 581964 : break;
1212 : : }
1213 : :
1214 : 643 : case T_DistinctExpr:
1215 : : {
1216 : 643 : DistinctExpr *op = (DistinctExpr *) node;
1217 : :
1218 : 643 : ExecInitFunc(&scratch, node,
1219 : : op->args, op->opfuncid, op->inputcollid,
1220 : : state);
1221 : :
1222 : : /*
1223 : : * Change opcode of call instruction to EEOP_DISTINCT.
1224 : : *
1225 : : * XXX: historically we've not called the function usage
1226 : : * pgstat infrastructure - that seems inconsistent given that
1227 : : * we do so for normal function *and* operator evaluation. If
1228 : : * we decided to do that here, we'd probably want separate
1229 : : * opcodes for FUSAGE or not.
1230 : : */
1231 : 643 : scratch.opcode = EEOP_DISTINCT;
1232 : 643 : ExprEvalPushStep(state, &scratch);
1233 : 643 : break;
1234 : : }
1235 : :
1236 : 600 : case T_NullIfExpr:
1237 : : {
1238 : 600 : NullIfExpr *op = (NullIfExpr *) node;
1239 : :
1240 : 600 : ExecInitFunc(&scratch, node,
1241 : : op->args, op->opfuncid, op->inputcollid,
1242 : : state);
1243 : :
1244 : : /*
1245 : : * If first argument is of varlena type, we'll need to ensure
1246 : : * that the value passed to the comparison function is a
1247 : : * read-only pointer.
1248 : : */
1249 : 600 : scratch.d.func.make_ro =
1250 : 600 : (get_typlen(exprType((Node *) linitial(op->args))) == -1);
1251 : :
1252 : : /*
1253 : : * Change opcode of call instruction to EEOP_NULLIF.
1254 : : *
1255 : : * XXX: historically we've not called the function usage
1256 : : * pgstat infrastructure - that seems inconsistent given that
1257 : : * we do so for normal function *and* operator evaluation. If
1258 : : * we decided to do that here, we'd probably want separate
1259 : : * opcodes for FUSAGE or not.
1260 : : */
1261 : 600 : scratch.opcode = EEOP_NULLIF;
1262 : 600 : ExprEvalPushStep(state, &scratch);
1263 : 600 : break;
1264 : : }
1265 : :
1266 : 24381 : case T_ScalarArrayOpExpr:
1267 : : {
1268 : 24381 : ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
1269 : : Expr *scalararg;
1270 : : Expr *arrayarg;
1271 : : FmgrInfo *finfo;
1272 : : FunctionCallInfo fcinfo;
1273 : : AclResult aclresult;
1274 : : Oid cmpfuncid;
1275 : :
1276 : : /*
1277 : : * Select the correct comparison function. When we do hashed
1278 : : * NOT IN clauses, the opfuncid will be the inequality
1279 : : * comparison function and negfuncid will be set to equality.
1280 : : * We need to use the equality function for hash probes.
1281 : : */
1282 [ + + ]: 24381 : if (OidIsValid(opexpr->negfuncid))
1283 : : {
1284 : : Assert(OidIsValid(opexpr->hashfuncid));
1285 : 66 : cmpfuncid = opexpr->negfuncid;
1286 : : }
1287 : : else
1288 : 24315 : cmpfuncid = opexpr->opfuncid;
1289 : :
1290 : : Assert(list_length(opexpr->args) == 2);
1291 : 24381 : scalararg = (Expr *) linitial(opexpr->args);
1292 : 24381 : arrayarg = (Expr *) lsecond(opexpr->args);
1293 : :
1294 : : /* Check permission to call function */
1295 : 24381 : aclresult = object_aclcheck(ProcedureRelationId, cmpfuncid,
1296 : : GetUserId(),
1297 : : ACL_EXECUTE);
1298 [ - + ]: 24381 : if (aclresult != ACLCHECK_OK)
1299 : 0 : aclcheck_error(aclresult, OBJECT_FUNCTION,
1300 : 0 : get_func_name(cmpfuncid));
1301 [ - + ]: 24381 : InvokeFunctionExecuteHook(cmpfuncid);
1302 : :
1303 [ + + ]: 24381 : if (OidIsValid(opexpr->hashfuncid))
1304 : : {
1305 : 287 : aclresult = object_aclcheck(ProcedureRelationId, opexpr->hashfuncid,
1306 : : GetUserId(),
1307 : : ACL_EXECUTE);
1308 [ - + ]: 287 : if (aclresult != ACLCHECK_OK)
1309 : 0 : aclcheck_error(aclresult, OBJECT_FUNCTION,
1310 : 0 : get_func_name(opexpr->hashfuncid));
1311 [ - + ]: 287 : InvokeFunctionExecuteHook(opexpr->hashfuncid);
1312 : : }
1313 : :
1314 : : /* Set up the primary fmgr lookup information */
1315 : 24381 : finfo = palloc0_object(FmgrInfo);
1316 : 24381 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
1317 : 24381 : fmgr_info(cmpfuncid, finfo);
1318 : 24381 : fmgr_info_set_expr((Node *) node, finfo);
1319 : 24381 : InitFunctionCallInfoData(*fcinfo, finfo, 2,
1320 : : opexpr->inputcollid, NULL, NULL);
1321 : :
1322 : : /*
1323 : : * If hashfuncid is set, we create a EEOP_HASHED_SCALARARRAYOP
1324 : : * step instead of a EEOP_SCALARARRAYOP. This provides much
1325 : : * faster lookup performance than the normal linear search
1326 : : * when the number of items in the array is anything but very
1327 : : * small.
1328 : : */
1329 [ + + ]: 24381 : if (OidIsValid(opexpr->hashfuncid))
1330 : : {
1331 : : /* Evaluate scalar directly into left function argument */
1332 : 287 : ExecInitExprRec(scalararg, state,
1333 : : &fcinfo->args[0].value, &fcinfo->args[0].isnull);
1334 : :
1335 : : /*
1336 : : * Evaluate array argument into our return value. There's
1337 : : * no danger in that, because the return value is
1338 : : * guaranteed to be overwritten by
1339 : : * EEOP_HASHED_SCALARARRAYOP, and will not be passed to
1340 : : * any other expression.
1341 : : */
1342 : 287 : ExecInitExprRec(arrayarg, state, resv, resnull);
1343 : :
1344 : : /* And perform the operation */
1345 : 287 : scratch.opcode = EEOP_HASHED_SCALARARRAYOP;
1346 : 287 : scratch.d.hashedscalararrayop.inclause = opexpr->useOr;
1347 : 287 : scratch.d.hashedscalararrayop.finfo = finfo;
1348 : 287 : scratch.d.hashedscalararrayop.fcinfo_data = fcinfo;
1349 : 287 : scratch.d.hashedscalararrayop.saop = opexpr;
1350 : :
1351 : :
1352 : 287 : ExprEvalPushStep(state, &scratch);
1353 : : }
1354 : : else
1355 : : {
1356 : : /* Evaluate scalar directly into left function argument */
1357 : 24094 : ExecInitExprRec(scalararg, state,
1358 : : &fcinfo->args[0].value,
1359 : : &fcinfo->args[0].isnull);
1360 : :
1361 : : /*
1362 : : * Evaluate array argument into our return value. There's
1363 : : * no danger in that, because the return value is
1364 : : * guaranteed to be overwritten by EEOP_SCALARARRAYOP, and
1365 : : * will not be passed to any other expression.
1366 : : */
1367 : 24094 : ExecInitExprRec(arrayarg, state, resv, resnull);
1368 : :
1369 : : /* And perform the operation */
1370 : 24094 : scratch.opcode = EEOP_SCALARARRAYOP;
1371 : 24094 : scratch.d.scalararrayop.element_type = InvalidOid;
1372 : 24094 : scratch.d.scalararrayop.useOr = opexpr->useOr;
1373 : 24094 : scratch.d.scalararrayop.finfo = finfo;
1374 : 24094 : scratch.d.scalararrayop.fcinfo_data = fcinfo;
1375 : 24094 : scratch.d.scalararrayop.fn_addr = finfo->fn_addr;
1376 : 24094 : ExprEvalPushStep(state, &scratch);
1377 : : }
1378 : 24381 : break;
1379 : : }
1380 : :
1381 : 45857 : case T_BoolExpr:
1382 : : {
1383 : 45857 : BoolExpr *boolexpr = (BoolExpr *) node;
1384 : 45857 : int nargs = list_length(boolexpr->args);
1385 : 45857 : List *adjust_jumps = NIL;
1386 : : int off;
1387 : : ListCell *lc;
1388 : :
1389 : : /* allocate scratch memory used by all steps of AND/OR */
1390 [ + + ]: 45857 : if (boolexpr->boolop != NOT_EXPR)
1391 : 32722 : scratch.d.boolexpr.anynull = palloc_object(bool);
1392 : :
1393 : : /*
1394 : : * For each argument evaluate the argument itself, then
1395 : : * perform the bool operation's appropriate handling.
1396 : : *
1397 : : * We can evaluate each argument into our result area, since
1398 : : * the short-circuiting logic means we only need to remember
1399 : : * previous NULL values.
1400 : : *
1401 : : * AND/OR is split into separate STEP_FIRST (one) / STEP (zero
1402 : : * or more) / STEP_LAST (one) steps, as each of those has to
1403 : : * perform different work. The FIRST/LAST split is valid
1404 : : * because AND/OR have at least two arguments.
1405 : : */
1406 : 45857 : off = 0;
1407 [ + - + + : 133140 : foreach(lc, boolexpr->args)
+ + ]
1408 : : {
1409 : 87283 : Expr *arg = (Expr *) lfirst(lc);
1410 : :
1411 : : /* Evaluate argument into our output variable */
1412 : 87283 : ExecInitExprRec(arg, state, resv, resnull);
1413 : :
1414 : : /* Perform the appropriate step type */
1415 [ + + + - ]: 87283 : switch (boolexpr->boolop)
1416 : : {
1417 : 46717 : case AND_EXPR:
1418 : : Assert(nargs >= 2);
1419 : :
1420 [ + + ]: 46717 : if (off == 0)
1421 : 20681 : scratch.opcode = EEOP_BOOL_AND_STEP_FIRST;
1422 [ + + ]: 26036 : else if (off + 1 == nargs)
1423 : 20681 : scratch.opcode = EEOP_BOOL_AND_STEP_LAST;
1424 : : else
1425 : 5355 : scratch.opcode = EEOP_BOOL_AND_STEP;
1426 : 46717 : break;
1427 : 27431 : case OR_EXPR:
1428 : : Assert(nargs >= 2);
1429 : :
1430 [ + + ]: 27431 : if (off == 0)
1431 : 12041 : scratch.opcode = EEOP_BOOL_OR_STEP_FIRST;
1432 [ + + ]: 15390 : else if (off + 1 == nargs)
1433 : 12041 : scratch.opcode = EEOP_BOOL_OR_STEP_LAST;
1434 : : else
1435 : 3349 : scratch.opcode = EEOP_BOOL_OR_STEP;
1436 : 27431 : break;
1437 : 13135 : case NOT_EXPR:
1438 : : Assert(nargs == 1);
1439 : :
1440 : 13135 : scratch.opcode = EEOP_BOOL_NOT_STEP;
1441 : 13135 : break;
1442 : 0 : default:
1443 [ # # ]: 0 : elog(ERROR, "unrecognized boolop: %d",
1444 : : (int) boolexpr->boolop);
1445 : : break;
1446 : : }
1447 : :
1448 : 87283 : scratch.d.boolexpr.jumpdone = -1;
1449 : 87283 : ExprEvalPushStep(state, &scratch);
1450 : 87283 : adjust_jumps = lappend_int(adjust_jumps,
1451 : 87283 : state->steps_len - 1);
1452 : 87283 : off++;
1453 : : }
1454 : :
1455 : : /* adjust jump targets */
1456 [ + - + + : 133140 : foreach(lc, adjust_jumps)
+ + ]
1457 : : {
1458 : 87283 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1459 : :
1460 : : Assert(as->d.boolexpr.jumpdone == -1);
1461 : 87283 : as->d.boolexpr.jumpdone = state->steps_len;
1462 : : }
1463 : :
1464 : 45857 : break;
1465 : : }
1466 : :
1467 : 19211 : case T_SubPlan:
1468 : : {
1469 : 19211 : SubPlan *subplan = (SubPlan *) node;
1470 : :
1471 : : /*
1472 : : * Real execution of a MULTIEXPR SubPlan has already been
1473 : : * done. What we have to do here is return a dummy NULL record
1474 : : * value in case this targetlist element is assigned
1475 : : * someplace.
1476 : : */
1477 [ + + ]: 19211 : if (subplan->subLinkType == MULTIEXPR_SUBLINK)
1478 : : {
1479 : 39 : scratch.opcode = EEOP_CONST;
1480 : 39 : scratch.d.constval.value = (Datum) 0;
1481 : 39 : scratch.d.constval.isnull = true;
1482 : 39 : ExprEvalPushStep(state, &scratch);
1483 : 39 : break;
1484 : : }
1485 : :
1486 : 19172 : ExecInitSubPlanExpr(subplan, state, resv, resnull);
1487 : 19172 : break;
1488 : : }
1489 : :
1490 : 27210 : case T_FieldSelect:
1491 : : {
1492 : 27210 : FieldSelect *fselect = (FieldSelect *) node;
1493 : :
1494 : : /* evaluate row/record argument into result area */
1495 : 27210 : ExecInitExprRec(fselect->arg, state, resv, resnull);
1496 : :
1497 : : /* and extract field */
1498 : 27210 : scratch.opcode = EEOP_FIELDSELECT;
1499 : 27210 : scratch.d.fieldselect.fieldnum = fselect->fieldnum;
1500 : 27210 : scratch.d.fieldselect.resulttype = fselect->resulttype;
1501 : 27210 : scratch.d.fieldselect.rowcache.cacheptr = NULL;
1502 : :
1503 : 27210 : ExprEvalPushStep(state, &scratch);
1504 : 27210 : break;
1505 : : }
1506 : :
1507 : 305 : case T_FieldStore:
1508 : : {
1509 : 305 : FieldStore *fstore = (FieldStore *) node;
1510 : : TupleDesc tupDesc;
1511 : : Datum *values;
1512 : : bool *nulls;
1513 : : int ncolumns;
1514 : : ListCell *l1,
1515 : : *l2;
1516 : :
1517 : : /* Copy the tupdesc to describe result tuples */
1518 : 305 : tupDesc = lookup_rowtype_tupdesc_copy(fstore->resulttype, -1);
1519 : 305 : ncolumns = tupDesc->natts;
1520 : :
1521 : : /* create workspace for column values */
1522 : 305 : values = palloc_array(Datum, ncolumns);
1523 : 305 : nulls = palloc_array(bool, ncolumns);
1524 : :
1525 : : /* emit code to evaluate the composite input value */
1526 : 305 : ExecInitExprRec(fstore->arg, state, resv, resnull);
1527 : :
1528 : : /* next, deform the input tuple into our workspace */
1529 : 305 : scratch.opcode = EEOP_FIELDSTORE_DEFORM;
1530 : 305 : scratch.d.fieldstore.fstore = fstore;
1531 : 305 : scratch.d.fieldstore.tupdesc = tupDesc;
1532 : 305 : scratch.d.fieldstore.values = values;
1533 : 305 : scratch.d.fieldstore.nulls = nulls;
1534 : 305 : ExprEvalPushStep(state, &scratch);
1535 : :
1536 : : /* evaluate new field values, store in workspace columns */
1537 [ + - + + : 698 : forboth(l1, fstore->newvals, l2, fstore->fieldnums)
+ - + + +
+ + - +
+ ]
1538 : : {
1539 : 393 : Expr *e = (Expr *) lfirst(l1);
1540 : 393 : Oid etype = exprType((Node *) e);
1541 : 393 : AttrNumber fieldnum = lfirst_int(l2);
1542 : 393 : Oid atttype = TupleDescAttr(tupDesc, fieldnum - 1)->atttypid;
1543 : : Datum *save_innermost_caseval;
1544 : : bool *save_innermost_casenull;
1545 : :
1546 [ + - - + ]: 393 : if (fieldnum <= 0 || fieldnum > ncolumns)
1547 [ # # ]: 0 : elog(ERROR, "field number %d is out of range in FieldStore",
1548 : : fieldnum);
1549 : :
1550 : : /*
1551 : : * Be paranoid about the plan and the current type
1552 : : * definition having gotten out of sync.
1553 : : */
1554 [ - + ]: 393 : if (etype != atttype)
1555 [ # # ]: 0 : ereport(ERROR,
1556 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
1557 : : errmsg("attribute %d of type %s has wrong type",
1558 : : fieldnum, format_type_be(fstore->resulttype)),
1559 : : errdetail("Attribute has type %s, but query expects %s.",
1560 : : format_type_be(atttype),
1561 : : format_type_be(etype))));
1562 : :
1563 : : /*
1564 : : * Use the CaseTestExpr mechanism to pass down the old
1565 : : * value of the field being replaced; this is needed in
1566 : : * case the newval is itself a FieldStore or
1567 : : * SubscriptingRef that has to obtain and modify the old
1568 : : * value. It's safe to reuse the CASE mechanism because
1569 : : * there cannot be a CASE between here and where the value
1570 : : * would be needed, and a field assignment can't be within
1571 : : * a CASE either. (So saving and restoring
1572 : : * innermost_caseval is just paranoia, but let's do it
1573 : : * anyway.)
1574 : : *
1575 : : * Another non-obvious point is that it's safe to use the
1576 : : * field's values[]/nulls[] entries as both the caseval
1577 : : * source and the result address for this subexpression.
1578 : : * That's okay only because (1) both FieldStore and
1579 : : * SubscriptingRef evaluate their arg or refexpr inputs
1580 : : * first, and (2) any such CaseTestExpr is directly the
1581 : : * arg or refexpr input. So any read of the caseval will
1582 : : * occur before there's a chance to overwrite it. Also,
1583 : : * if multiple entries in the newvals/fieldnums lists
1584 : : * target the same field, they'll effectively be applied
1585 : : * left-to-right which is what we want.
1586 : : */
1587 : 393 : save_innermost_caseval = state->innermost_caseval;
1588 : 393 : save_innermost_casenull = state->innermost_casenull;
1589 : 393 : state->innermost_caseval = &values[fieldnum - 1];
1590 : 393 : state->innermost_casenull = &nulls[fieldnum - 1];
1591 : :
1592 : 393 : ExecInitExprRec(e, state,
1593 : 393 : &values[fieldnum - 1],
1594 : 393 : &nulls[fieldnum - 1]);
1595 : :
1596 : 393 : state->innermost_caseval = save_innermost_caseval;
1597 : 393 : state->innermost_casenull = save_innermost_casenull;
1598 : : }
1599 : :
1600 : : /* finally, form result tuple */
1601 : 305 : scratch.opcode = EEOP_FIELDSTORE_FORM;
1602 : 305 : scratch.d.fieldstore.fstore = fstore;
1603 : 305 : scratch.d.fieldstore.tupdesc = tupDesc;
1604 : 305 : scratch.d.fieldstore.values = values;
1605 : 305 : scratch.d.fieldstore.nulls = nulls;
1606 : 305 : ExprEvalPushStep(state, &scratch);
1607 : 305 : break;
1608 : : }
1609 : :
1610 : 74404 : case T_RelabelType:
1611 : : {
1612 : : /* relabel doesn't need to do anything at runtime */
1613 : 74404 : RelabelType *relabel = (RelabelType *) node;
1614 : :
1615 : 74404 : ExecInitExprRec(relabel->arg, state, resv, resnull);
1616 : 74404 : break;
1617 : : }
1618 : :
1619 : 27032 : case T_CoerceViaIO:
1620 : : {
1621 : 27032 : CoerceViaIO *iocoerce = (CoerceViaIO *) node;
1622 : : Oid iofunc;
1623 : : bool typisvarlena;
1624 : : Oid typioparam;
1625 : : FunctionCallInfo fcinfo_in;
1626 : :
1627 : : /* evaluate argument into step's result area */
1628 : 27032 : ExecInitExprRec(iocoerce->arg, state, resv, resnull);
1629 : :
1630 : : /*
1631 : : * Prepare both output and input function calls, to be
1632 : : * evaluated inside a single evaluation step for speed - this
1633 : : * can be a very common operation.
1634 : : *
1635 : : * We don't check permissions here as a type's input/output
1636 : : * function are assumed to be executable by everyone.
1637 : : */
1638 [ + + ]: 27032 : if (state->escontext == NULL)
1639 : 26996 : scratch.opcode = EEOP_IOCOERCE;
1640 : : else
1641 : 36 : scratch.opcode = EEOP_IOCOERCE_SAFE;
1642 : :
1643 : : /* lookup the source type's output function */
1644 : 27032 : scratch.d.iocoerce.finfo_out = palloc0_object(FmgrInfo);
1645 : 27032 : scratch.d.iocoerce.fcinfo_data_out = palloc0(SizeForFunctionCallInfo(1));
1646 : :
1647 : 27032 : getTypeOutputInfo(exprType((Node *) iocoerce->arg),
1648 : : &iofunc, &typisvarlena);
1649 : 27032 : fmgr_info(iofunc, scratch.d.iocoerce.finfo_out);
1650 : 27032 : fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_out);
1651 : 27032 : InitFunctionCallInfoData(*scratch.d.iocoerce.fcinfo_data_out,
1652 : : scratch.d.iocoerce.finfo_out,
1653 : : 1, InvalidOid, NULL, NULL);
1654 : :
1655 : : /* lookup the result type's input function */
1656 : 27032 : scratch.d.iocoerce.finfo_in = palloc0_object(FmgrInfo);
1657 : 27032 : scratch.d.iocoerce.fcinfo_data_in = palloc0(SizeForFunctionCallInfo(3));
1658 : :
1659 : 27032 : getTypeInputInfo(iocoerce->resulttype,
1660 : : &iofunc, &typioparam);
1661 : 27032 : fmgr_info(iofunc, scratch.d.iocoerce.finfo_in);
1662 : 27032 : fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_in);
1663 : 27032 : InitFunctionCallInfoData(*scratch.d.iocoerce.fcinfo_data_in,
1664 : : scratch.d.iocoerce.finfo_in,
1665 : : 3, InvalidOid, NULL, NULL);
1666 : :
1667 : : /*
1668 : : * We can preload the second and third arguments for the input
1669 : : * function, since they're constants.
1670 : : */
1671 : 27032 : fcinfo_in = scratch.d.iocoerce.fcinfo_data_in;
1672 : 27032 : fcinfo_in->args[1].value = ObjectIdGetDatum(typioparam);
1673 : 27032 : fcinfo_in->args[1].isnull = false;
1674 : 27032 : fcinfo_in->args[2].value = Int32GetDatum(-1);
1675 : 27032 : fcinfo_in->args[2].isnull = false;
1676 : :
1677 : 27032 : fcinfo_in->context = (Node *) state->escontext;
1678 : :
1679 : 27032 : ExprEvalPushStep(state, &scratch);
1680 : 27032 : break;
1681 : : }
1682 : :
1683 : 3903 : case T_ArrayCoerceExpr:
1684 : : {
1685 : 3903 : ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
1686 : : Oid resultelemtype;
1687 : : ExprState *elemstate;
1688 : :
1689 : : /* evaluate argument into step's result area */
1690 : 3903 : ExecInitExprRec(acoerce->arg, state, resv, resnull);
1691 : :
1692 : 3903 : resultelemtype = get_element_type(acoerce->resulttype);
1693 [ - + ]: 3903 : if (!OidIsValid(resultelemtype))
1694 [ # # ]: 0 : ereport(ERROR,
1695 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1696 : : errmsg("target type is not an array")));
1697 : :
1698 : : /*
1699 : : * Construct a sub-expression for the per-element expression;
1700 : : * but don't ready it until after we check it for triviality.
1701 : : * We assume it hasn't any Var references, but does have a
1702 : : * CaseTestExpr representing the source array element values.
1703 : : */
1704 : 3903 : elemstate = makeNode(ExprState);
1705 : 3903 : elemstate->expr = acoerce->elemexpr;
1706 : 3903 : elemstate->parent = state->parent;
1707 : 3903 : elemstate->ext_params = state->ext_params;
1708 : :
1709 : 3903 : elemstate->innermost_caseval = palloc_object(Datum);
1710 : 3903 : elemstate->innermost_casenull = palloc_object(bool);
1711 : :
1712 : 3903 : ExecInitExprRec(acoerce->elemexpr, elemstate,
1713 : : &elemstate->resvalue, &elemstate->resnull);
1714 : :
1715 [ + + ]: 3899 : if (elemstate->steps_len == 1 &&
1716 [ + - ]: 3531 : elemstate->steps[0].opcode == EEOP_CASE_TESTVAL)
1717 : : {
1718 : : /* Trivial, so we need no per-element work at runtime */
1719 : 3531 : elemstate = NULL;
1720 : : }
1721 : : else
1722 : : {
1723 : : /* Not trivial, so append a DONE step */
1724 : 368 : scratch.opcode = EEOP_DONE_RETURN;
1725 : 368 : ExprEvalPushStep(elemstate, &scratch);
1726 : : /* and ready the subexpression */
1727 : 368 : ExecReadyExpr(elemstate);
1728 : : }
1729 : :
1730 : 3899 : scratch.opcode = EEOP_ARRAYCOERCE;
1731 : 3899 : scratch.d.arraycoerce.elemexprstate = elemstate;
1732 : 3899 : scratch.d.arraycoerce.resultelemtype = resultelemtype;
1733 : :
1734 [ + + ]: 3899 : if (elemstate)
1735 : : {
1736 : : /* Set up workspace for array_map */
1737 : 368 : scratch.d.arraycoerce.amstate = palloc0_object(ArrayMapState);
1738 : : }
1739 : : else
1740 : : {
1741 : : /* Don't need workspace if there's no subexpression */
1742 : 3531 : scratch.d.arraycoerce.amstate = NULL;
1743 : : }
1744 : :
1745 : 3899 : ExprEvalPushStep(state, &scratch);
1746 : 3899 : break;
1747 : : }
1748 : :
1749 : 550 : case T_ConvertRowtypeExpr:
1750 : : {
1751 : 550 : ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
1752 : : ExprEvalRowtypeCache *rowcachep;
1753 : :
1754 : : /* cache structs must be out-of-line for space reasons */
1755 : 550 : rowcachep = palloc_array(ExprEvalRowtypeCache, 2);
1756 : 550 : rowcachep[0].cacheptr = NULL;
1757 : 550 : rowcachep[1].cacheptr = NULL;
1758 : :
1759 : : /* evaluate argument into step's result area */
1760 : 550 : ExecInitExprRec(convert->arg, state, resv, resnull);
1761 : :
1762 : : /* and push conversion step */
1763 : 550 : scratch.opcode = EEOP_CONVERT_ROWTYPE;
1764 : 550 : scratch.d.convert_rowtype.inputtype =
1765 : 550 : exprType((Node *) convert->arg);
1766 : 550 : scratch.d.convert_rowtype.outputtype = convert->resulttype;
1767 : 550 : scratch.d.convert_rowtype.incache = &rowcachep[0];
1768 : 550 : scratch.d.convert_rowtype.outcache = &rowcachep[1];
1769 : 550 : scratch.d.convert_rowtype.map = NULL;
1770 : :
1771 : 550 : ExprEvalPushStep(state, &scratch);
1772 : 550 : break;
1773 : : }
1774 : :
1775 : : /* note that CaseWhen expressions are handled within this block */
1776 : 61361 : case T_CaseExpr:
1777 : : {
1778 : 61361 : CaseExpr *caseExpr = (CaseExpr *) node;
1779 : 61361 : List *adjust_jumps = NIL;
1780 : 61361 : Datum *caseval = NULL;
1781 : 61361 : bool *casenull = NULL;
1782 : : ListCell *lc;
1783 : :
1784 : : /*
1785 : : * If there's a test expression, we have to evaluate it and
1786 : : * save the value where the CaseTestExpr placeholders can find
1787 : : * it.
1788 : : */
1789 [ + + ]: 61361 : if (caseExpr->arg != NULL)
1790 : : {
1791 : : /* Evaluate testexpr into caseval/casenull workspace */
1792 : 3201 : caseval = palloc_object(Datum);
1793 : 3201 : casenull = palloc_object(bool);
1794 : :
1795 : 3201 : ExecInitExprRec(caseExpr->arg, state,
1796 : : caseval, casenull);
1797 : :
1798 : : /*
1799 : : * Since value might be read multiple times, force to R/O
1800 : : * - but only if it could be an expanded datum.
1801 : : */
1802 [ + + ]: 3201 : if (get_typlen(exprType((Node *) caseExpr->arg)) == -1)
1803 : : {
1804 : : /* change caseval in-place */
1805 : 91 : scratch.opcode = EEOP_MAKE_READONLY;
1806 : 91 : scratch.resvalue = caseval;
1807 : 91 : scratch.resnull = casenull;
1808 : 91 : scratch.d.make_readonly.value = caseval;
1809 : 91 : scratch.d.make_readonly.isnull = casenull;
1810 : 91 : ExprEvalPushStep(state, &scratch);
1811 : : /* restore normal settings of scratch fields */
1812 : 91 : scratch.resvalue = resv;
1813 : 91 : scratch.resnull = resnull;
1814 : : }
1815 : : }
1816 : :
1817 : : /*
1818 : : * Prepare to evaluate each of the WHEN clauses in turn; as
1819 : : * soon as one is true we return the value of the
1820 : : * corresponding THEN clause. If none are true then we return
1821 : : * the value of the ELSE clause, or NULL if there is none.
1822 : : */
1823 [ + - + + : 179653 : foreach(lc, caseExpr->args)
+ + ]
1824 : : {
1825 : 118292 : CaseWhen *when = (CaseWhen *) lfirst(lc);
1826 : : Datum *save_innermost_caseval;
1827 : : bool *save_innermost_casenull;
1828 : : int whenstep;
1829 : :
1830 : : /*
1831 : : * Make testexpr result available to CaseTestExpr nodes
1832 : : * within the condition. We must save and restore prior
1833 : : * setting of innermost_caseval fields, in case this node
1834 : : * is itself within a larger CASE.
1835 : : *
1836 : : * If there's no test expression, we don't actually need
1837 : : * to save and restore these fields; but it's less code to
1838 : : * just do so unconditionally.
1839 : : */
1840 : 118292 : save_innermost_caseval = state->innermost_caseval;
1841 : 118292 : save_innermost_casenull = state->innermost_casenull;
1842 : 118292 : state->innermost_caseval = caseval;
1843 : 118292 : state->innermost_casenull = casenull;
1844 : :
1845 : : /* evaluate condition into CASE's result variables */
1846 : 118292 : ExecInitExprRec(when->expr, state, resv, resnull);
1847 : :
1848 : 118292 : state->innermost_caseval = save_innermost_caseval;
1849 : 118292 : state->innermost_casenull = save_innermost_casenull;
1850 : :
1851 : : /* If WHEN result isn't true, jump to next CASE arm */
1852 : 118292 : scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
1853 : 118292 : scratch.d.jump.jumpdone = -1; /* computed later */
1854 : 118292 : ExprEvalPushStep(state, &scratch);
1855 : 118292 : whenstep = state->steps_len - 1;
1856 : :
1857 : : /*
1858 : : * If WHEN result is true, evaluate THEN result, storing
1859 : : * it into the CASE's result variables.
1860 : : */
1861 : 118292 : ExecInitExprRec(when->result, state, resv, resnull);
1862 : :
1863 : : /* Emit JUMP step to jump to end of CASE's code */
1864 : 118292 : scratch.opcode = EEOP_JUMP;
1865 : 118292 : scratch.d.jump.jumpdone = -1; /* computed later */
1866 : 118292 : ExprEvalPushStep(state, &scratch);
1867 : :
1868 : : /*
1869 : : * Don't know address for that jump yet, compute once the
1870 : : * whole CASE expression is built.
1871 : : */
1872 : 118292 : adjust_jumps = lappend_int(adjust_jumps,
1873 : 118292 : state->steps_len - 1);
1874 : :
1875 : : /*
1876 : : * But we can set WHEN test's jump target now, to make it
1877 : : * jump to the next WHEN subexpression or the ELSE.
1878 : : */
1879 : 118292 : state->steps[whenstep].d.jump.jumpdone = state->steps_len;
1880 : : }
1881 : :
1882 : : /* transformCaseExpr always adds a default */
1883 : : Assert(caseExpr->defresult);
1884 : :
1885 : : /* evaluate ELSE expr into CASE's result variables */
1886 : 61361 : ExecInitExprRec(caseExpr->defresult, state,
1887 : : resv, resnull);
1888 : :
1889 : : /* adjust jump targets */
1890 [ + - + + : 179653 : foreach(lc, adjust_jumps)
+ + ]
1891 : : {
1892 : 118292 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1893 : :
1894 : : Assert(as->opcode == EEOP_JUMP);
1895 : : Assert(as->d.jump.jumpdone == -1);
1896 : 118292 : as->d.jump.jumpdone = state->steps_len;
1897 : : }
1898 : :
1899 : 61361 : break;
1900 : : }
1901 : :
1902 : 17902 : case T_CaseTestExpr:
1903 : : {
1904 : : /*
1905 : : * Read from location identified by innermost_caseval. Note
1906 : : * that innermost_caseval could be NULL, if this node isn't
1907 : : * actually within a CaseExpr, ArrayCoerceExpr, etc structure.
1908 : : * That can happen because some parts of the system abuse
1909 : : * CaseTestExpr to cause a read of a value externally supplied
1910 : : * in econtext->caseValue_datum. We'll take care of that by
1911 : : * generating a specialized operation.
1912 : : */
1913 [ + + ]: 17902 : if (state->innermost_caseval == NULL)
1914 : 1529 : scratch.opcode = EEOP_CASE_TESTVAL_EXT;
1915 : : else
1916 : : {
1917 : 16373 : scratch.opcode = EEOP_CASE_TESTVAL;
1918 : 16373 : scratch.d.casetest.value = state->innermost_caseval;
1919 : 16373 : scratch.d.casetest.isnull = state->innermost_casenull;
1920 : : }
1921 : 17902 : ExprEvalPushStep(state, &scratch);
1922 : 17902 : break;
1923 : : }
1924 : :
1925 : 21431 : case T_ArrayExpr:
1926 : : {
1927 : 21431 : ArrayExpr *arrayexpr = (ArrayExpr *) node;
1928 : 21431 : int nelems = list_length(arrayexpr->elements);
1929 : : ListCell *lc;
1930 : : int elemoff;
1931 : :
1932 : : /*
1933 : : * Evaluate by computing each element, and then forming the
1934 : : * array. Elements are computed into scratch arrays
1935 : : * associated with the ARRAYEXPR step.
1936 : : */
1937 : 21431 : scratch.opcode = EEOP_ARRAYEXPR;
1938 : 21431 : scratch.d.arrayexpr.elemvalues =
1939 : 21431 : palloc_array(Datum, nelems);
1940 : 21431 : scratch.d.arrayexpr.elemnulls =
1941 : 21431 : palloc_array(bool, nelems);
1942 : 21431 : scratch.d.arrayexpr.nelems = nelems;
1943 : :
1944 : : /* fill remaining fields of step */
1945 : 21431 : scratch.d.arrayexpr.multidims = arrayexpr->multidims;
1946 : 21431 : scratch.d.arrayexpr.elemtype = arrayexpr->element_typeid;
1947 : :
1948 : : /* do one-time catalog lookup for type info */
1949 : 21431 : get_typlenbyvalalign(arrayexpr->element_typeid,
1950 : : &scratch.d.arrayexpr.elemlength,
1951 : : &scratch.d.arrayexpr.elembyval,
1952 : : &scratch.d.arrayexpr.elemalign);
1953 : :
1954 : : /* prepare to evaluate all arguments */
1955 : 21431 : elemoff = 0;
1956 [ + + + + : 80253 : foreach(lc, arrayexpr->elements)
+ + ]
1957 : : {
1958 : 58822 : Expr *e = (Expr *) lfirst(lc);
1959 : :
1960 : 58822 : ExecInitExprRec(e, state,
1961 : 58822 : &scratch.d.arrayexpr.elemvalues[elemoff],
1962 : 58822 : &scratch.d.arrayexpr.elemnulls[elemoff]);
1963 : 58822 : elemoff++;
1964 : : }
1965 : :
1966 : : /* and then collect all into an array */
1967 : 21431 : ExprEvalPushStep(state, &scratch);
1968 : 21431 : break;
1969 : : }
1970 : :
1971 : 5256 : case T_RowExpr:
1972 : : {
1973 : 5256 : RowExpr *rowexpr = (RowExpr *) node;
1974 : 5256 : int nelems = list_length(rowexpr->args);
1975 : : TupleDesc tupdesc;
1976 : : int i;
1977 : : ListCell *l;
1978 : :
1979 : : /* Build tupdesc to describe result tuples */
1980 [ + + ]: 5256 : if (rowexpr->row_typeid == RECORDOID)
1981 : : {
1982 : : /* generic record, use types of given expressions */
1983 : 2490 : tupdesc = ExecTypeFromExprList(rowexpr->args);
1984 : : /* ... but adopt RowExpr's column aliases */
1985 : 2490 : ExecTypeSetColNames(tupdesc, rowexpr->colnames);
1986 : : /* Bless the tupdesc so it can be looked up later */
1987 : 2490 : BlessTupleDesc(tupdesc);
1988 : : }
1989 : : else
1990 : : {
1991 : : /* it's been cast to a named type, use that */
1992 : 2766 : tupdesc = lookup_rowtype_tupdesc_copy(rowexpr->row_typeid, -1);
1993 : : }
1994 : :
1995 : : /*
1996 : : * In the named-type case, the tupdesc could have more columns
1997 : : * than are in the args list, since the type might have had
1998 : : * columns added since the ROW() was parsed. We want those
1999 : : * extra columns to go to nulls, so we make sure that the
2000 : : * workspace arrays are large enough and then initialize any
2001 : : * extra columns to read as NULLs.
2002 : : */
2003 : : Assert(nelems <= tupdesc->natts);
2004 : 5256 : nelems = Max(nelems, tupdesc->natts);
2005 : :
2006 : : /*
2007 : : * Evaluate by first building datums for each field, and then
2008 : : * a final step forming the composite datum.
2009 : : */
2010 : 5256 : scratch.opcode = EEOP_ROW;
2011 : 5256 : scratch.d.row.tupdesc = tupdesc;
2012 : :
2013 : : /* space for the individual field datums */
2014 : 5256 : scratch.d.row.elemvalues =
2015 : 5256 : palloc_array(Datum, nelems);
2016 : 5256 : scratch.d.row.elemnulls =
2017 : 5256 : palloc_array(bool, nelems);
2018 : : /* as explained above, make sure any extra columns are null */
2019 : 5256 : memset(scratch.d.row.elemnulls, true, sizeof(bool) * nelems);
2020 : :
2021 : : /* Set up evaluation, skipping any deleted columns */
2022 : 5256 : i = 0;
2023 [ + + + + : 18853 : foreach(l, rowexpr->args)
+ + ]
2024 : : {
2025 : 13597 : Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2026 : 13597 : Expr *e = (Expr *) lfirst(l);
2027 : :
2028 [ + + ]: 13597 : if (!att->attisdropped)
2029 : : {
2030 : : /*
2031 : : * Guard against ALTER COLUMN TYPE on rowtype since
2032 : : * the RowExpr was created. XXX should we check
2033 : : * typmod too? Not sure we can be sure it'll be the
2034 : : * same.
2035 : : */
2036 [ - + ]: 13517 : if (exprType((Node *) e) != att->atttypid)
2037 [ # # ]: 0 : ereport(ERROR,
2038 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
2039 : : errmsg("ROW() column has type %s instead of type %s",
2040 : : format_type_be(exprType((Node *) e)),
2041 : : format_type_be(att->atttypid))));
2042 : : }
2043 : : else
2044 : : {
2045 : : /*
2046 : : * Ignore original expression and insert a NULL. We
2047 : : * don't really care what type of NULL it is, so
2048 : : * always make an int4 NULL.
2049 : : */
2050 : 80 : e = (Expr *) makeNullConst(INT4OID, -1, InvalidOid);
2051 : : }
2052 : :
2053 : : /* Evaluate column expr into appropriate workspace slot */
2054 : 13597 : ExecInitExprRec(e, state,
2055 : 13597 : &scratch.d.row.elemvalues[i],
2056 : 13597 : &scratch.d.row.elemnulls[i]);
2057 : 13597 : i++;
2058 : : }
2059 : :
2060 : : /* And finally build the row value */
2061 : 5256 : ExprEvalPushStep(state, &scratch);
2062 : 5256 : break;
2063 : : }
2064 : :
2065 : 232 : case T_RowCompareExpr:
2066 : : {
2067 : 232 : RowCompareExpr *rcexpr = (RowCompareExpr *) node;
2068 : 232 : int nopers = list_length(rcexpr->opnos);
2069 : 232 : List *adjust_jumps = NIL;
2070 : : ListCell *l_left_expr,
2071 : : *l_right_expr,
2072 : : *l_opno,
2073 : : *l_opfamily,
2074 : : *l_inputcollid;
2075 : : ListCell *lc;
2076 : :
2077 : : /*
2078 : : * Iterate over each field, prepare comparisons. To handle
2079 : : * NULL results, prepare jumps to after the expression. If a
2080 : : * comparison yields a != 0 result, jump to the final step.
2081 : : */
2082 : : Assert(list_length(rcexpr->largs) == nopers);
2083 : : Assert(list_length(rcexpr->rargs) == nopers);
2084 : : Assert(list_length(rcexpr->opfamilies) == nopers);
2085 : : Assert(list_length(rcexpr->inputcollids) == nopers);
2086 : :
2087 [ + - + + : 732 : forfive(l_left_expr, rcexpr->largs,
+ - + + +
- + + + -
+ + + - +
+ + + + -
+ - + - +
- + + ]
2088 : : l_right_expr, rcexpr->rargs,
2089 : : l_opno, rcexpr->opnos,
2090 : : l_opfamily, rcexpr->opfamilies,
2091 : : l_inputcollid, rcexpr->inputcollids)
2092 : : {
2093 : 500 : Expr *left_expr = (Expr *) lfirst(l_left_expr);
2094 : 500 : Expr *right_expr = (Expr *) lfirst(l_right_expr);
2095 : 500 : Oid opno = lfirst_oid(l_opno);
2096 : 500 : Oid opfamily = lfirst_oid(l_opfamily);
2097 : 500 : Oid inputcollid = lfirst_oid(l_inputcollid);
2098 : : int strategy;
2099 : : Oid lefttype;
2100 : : Oid righttype;
2101 : : Oid proc;
2102 : : FmgrInfo *finfo;
2103 : : FunctionCallInfo fcinfo;
2104 : :
2105 : 500 : get_op_opfamily_properties(opno, opfamily, false,
2106 : : &strategy,
2107 : : &lefttype,
2108 : : &righttype);
2109 : 500 : proc = get_opfamily_proc(opfamily,
2110 : : lefttype,
2111 : : righttype,
2112 : : BTORDER_PROC);
2113 [ - + ]: 500 : if (!OidIsValid(proc))
2114 [ # # ]: 0 : elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
2115 : : BTORDER_PROC, lefttype, righttype, opfamily);
2116 : :
2117 : : /* Set up the primary fmgr lookup information */
2118 : 500 : finfo = palloc0_object(FmgrInfo);
2119 : 500 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
2120 : 500 : fmgr_info(proc, finfo);
2121 : 500 : fmgr_info_set_expr((Node *) node, finfo);
2122 : 500 : InitFunctionCallInfoData(*fcinfo, finfo, 2,
2123 : : inputcollid, NULL, NULL);
2124 : :
2125 : : /*
2126 : : * If we enforced permissions checks on index support
2127 : : * functions, we'd need to make a check here. But the
2128 : : * index support machinery doesn't do that, and thus
2129 : : * neither does this code.
2130 : : */
2131 : :
2132 : : /* evaluate left and right args directly into fcinfo */
2133 : 500 : ExecInitExprRec(left_expr, state,
2134 : : &fcinfo->args[0].value, &fcinfo->args[0].isnull);
2135 : 500 : ExecInitExprRec(right_expr, state,
2136 : : &fcinfo->args[1].value, &fcinfo->args[1].isnull);
2137 : :
2138 : 500 : scratch.opcode = EEOP_ROWCOMPARE_STEP;
2139 : 500 : scratch.d.rowcompare_step.finfo = finfo;
2140 : 500 : scratch.d.rowcompare_step.fcinfo_data = fcinfo;
2141 : 500 : scratch.d.rowcompare_step.fn_addr = finfo->fn_addr;
2142 : : /* jump targets filled below */
2143 : 500 : scratch.d.rowcompare_step.jumpnull = -1;
2144 : 500 : scratch.d.rowcompare_step.jumpdone = -1;
2145 : :
2146 : 500 : ExprEvalPushStep(state, &scratch);
2147 : 500 : adjust_jumps = lappend_int(adjust_jumps,
2148 : 500 : state->steps_len - 1);
2149 : : }
2150 : :
2151 : : /*
2152 : : * We could have a zero-column rowtype, in which case the rows
2153 : : * necessarily compare equal.
2154 : : */
2155 [ - + ]: 232 : if (nopers == 0)
2156 : : {
2157 : 0 : scratch.opcode = EEOP_CONST;
2158 : 0 : scratch.d.constval.value = Int32GetDatum(0);
2159 : 0 : scratch.d.constval.isnull = false;
2160 : 0 : ExprEvalPushStep(state, &scratch);
2161 : : }
2162 : :
2163 : : /* Finally, examine the last comparison result */
2164 : 232 : scratch.opcode = EEOP_ROWCOMPARE_FINAL;
2165 : 232 : scratch.d.rowcompare_final.cmptype = rcexpr->cmptype;
2166 : 232 : ExprEvalPushStep(state, &scratch);
2167 : :
2168 : : /* adjust jump targets */
2169 [ + - + + : 732 : foreach(lc, adjust_jumps)
+ + ]
2170 : : {
2171 : 500 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
2172 : :
2173 : : Assert(as->opcode == EEOP_ROWCOMPARE_STEP);
2174 : : Assert(as->d.rowcompare_step.jumpdone == -1);
2175 : : Assert(as->d.rowcompare_step.jumpnull == -1);
2176 : :
2177 : : /* jump to comparison evaluation */
2178 : 500 : as->d.rowcompare_step.jumpdone = state->steps_len - 1;
2179 : : /* jump to the following expression */
2180 : 500 : as->d.rowcompare_step.jumpnull = state->steps_len;
2181 : : }
2182 : :
2183 : 232 : break;
2184 : : }
2185 : :
2186 : 2962 : case T_CoalesceExpr:
2187 : : {
2188 : 2962 : CoalesceExpr *coalesce = (CoalesceExpr *) node;
2189 : 2962 : List *adjust_jumps = NIL;
2190 : : ListCell *lc;
2191 : :
2192 : : /* We assume there's at least one arg */
2193 : : Assert(coalesce->args != NIL);
2194 : :
2195 : : /*
2196 : : * Prepare evaluation of all coalesced arguments, after each
2197 : : * one push a step that short-circuits if not null.
2198 : : */
2199 [ + - + + : 8894 : foreach(lc, coalesce->args)
+ + ]
2200 : : {
2201 : 5932 : Expr *e = (Expr *) lfirst(lc);
2202 : :
2203 : : /* evaluate argument, directly into result datum */
2204 : 5932 : ExecInitExprRec(e, state, resv, resnull);
2205 : :
2206 : : /* if it's not null, skip to end of COALESCE expr */
2207 : 5932 : scratch.opcode = EEOP_JUMP_IF_NOT_NULL;
2208 : 5932 : scratch.d.jump.jumpdone = -1; /* adjust later */
2209 : 5932 : ExprEvalPushStep(state, &scratch);
2210 : :
2211 : 5932 : adjust_jumps = lappend_int(adjust_jumps,
2212 : 5932 : state->steps_len - 1);
2213 : : }
2214 : :
2215 : : /*
2216 : : * No need to add a constant NULL return - we only can get to
2217 : : * the end of the expression if a NULL already is being
2218 : : * returned.
2219 : : */
2220 : :
2221 : : /* adjust jump targets */
2222 [ + - + + : 8894 : foreach(lc, adjust_jumps)
+ + ]
2223 : : {
2224 : 5932 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
2225 : :
2226 : : Assert(as->opcode == EEOP_JUMP_IF_NOT_NULL);
2227 : : Assert(as->d.jump.jumpdone == -1);
2228 : 5932 : as->d.jump.jumpdone = state->steps_len;
2229 : : }
2230 : :
2231 : 2962 : break;
2232 : : }
2233 : :
2234 : 1310 : case T_MinMaxExpr:
2235 : : {
2236 : 1310 : MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
2237 : 1310 : int nelems = list_length(minmaxexpr->args);
2238 : : TypeCacheEntry *typentry;
2239 : : FmgrInfo *finfo;
2240 : : FunctionCallInfo fcinfo;
2241 : : ListCell *lc;
2242 : : int off;
2243 : :
2244 : : /* Look up the btree comparison function for the datatype */
2245 : 1310 : typentry = lookup_type_cache(minmaxexpr->minmaxtype,
2246 : : TYPECACHE_CMP_PROC);
2247 [ - + ]: 1310 : if (!OidIsValid(typentry->cmp_proc))
2248 [ # # ]: 0 : ereport(ERROR,
2249 : : (errcode(ERRCODE_UNDEFINED_FUNCTION),
2250 : : errmsg("could not identify a comparison function for type %s",
2251 : : format_type_be(minmaxexpr->minmaxtype))));
2252 : :
2253 : : /*
2254 : : * If we enforced permissions checks on index support
2255 : : * functions, we'd need to make a check here. But the index
2256 : : * support machinery doesn't do that, and thus neither does
2257 : : * this code.
2258 : : */
2259 : :
2260 : : /* Perform function lookup */
2261 : 1310 : finfo = palloc0_object(FmgrInfo);
2262 : 1310 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
2263 : 1310 : fmgr_info(typentry->cmp_proc, finfo);
2264 : 1310 : fmgr_info_set_expr((Node *) node, finfo);
2265 : 1310 : InitFunctionCallInfoData(*fcinfo, finfo, 2,
2266 : : minmaxexpr->inputcollid, NULL, NULL);
2267 : :
2268 : 1310 : scratch.opcode = EEOP_MINMAX;
2269 : : /* allocate space to store arguments */
2270 : 1310 : scratch.d.minmax.values = palloc_array(Datum, nelems);
2271 : 1310 : scratch.d.minmax.nulls = palloc_array(bool, nelems);
2272 : 1310 : scratch.d.minmax.nelems = nelems;
2273 : :
2274 : 1310 : scratch.d.minmax.op = minmaxexpr->op;
2275 : 1310 : scratch.d.minmax.finfo = finfo;
2276 : 1310 : scratch.d.minmax.fcinfo_data = fcinfo;
2277 : :
2278 : : /* evaluate expressions into minmax->values/nulls */
2279 : 1310 : off = 0;
2280 [ + - + + : 4002 : foreach(lc, minmaxexpr->args)
+ + ]
2281 : : {
2282 : 2692 : Expr *e = (Expr *) lfirst(lc);
2283 : :
2284 : 2692 : ExecInitExprRec(e, state,
2285 : 2692 : &scratch.d.minmax.values[off],
2286 : 2692 : &scratch.d.minmax.nulls[off]);
2287 : 2692 : off++;
2288 : : }
2289 : :
2290 : : /* and push the final comparison */
2291 : 1310 : ExprEvalPushStep(state, &scratch);
2292 : 1310 : break;
2293 : : }
2294 : :
2295 : 3876 : case T_SQLValueFunction:
2296 : : {
2297 : 3876 : SQLValueFunction *svf = (SQLValueFunction *) node;
2298 : :
2299 : 3876 : scratch.opcode = EEOP_SQLVALUEFUNCTION;
2300 : 3876 : scratch.d.sqlvaluefunction.svf = svf;
2301 : :
2302 : 3876 : ExprEvalPushStep(state, &scratch);
2303 : 3876 : break;
2304 : : }
2305 : :
2306 : 572 : case T_XmlExpr:
2307 : : {
2308 : 572 : XmlExpr *xexpr = (XmlExpr *) node;
2309 : 572 : int nnamed = list_length(xexpr->named_args);
2310 : 572 : int nargs = list_length(xexpr->args);
2311 : : int off;
2312 : : ListCell *arg;
2313 : :
2314 : 572 : scratch.opcode = EEOP_XMLEXPR;
2315 : 572 : scratch.d.xmlexpr.xexpr = xexpr;
2316 : :
2317 : : /* allocate space for storing all the arguments */
2318 [ + + ]: 572 : if (nnamed)
2319 : : {
2320 : 40 : scratch.d.xmlexpr.named_argvalue = palloc_array(Datum, nnamed);
2321 : 40 : scratch.d.xmlexpr.named_argnull = palloc_array(bool, nnamed);
2322 : : }
2323 : : else
2324 : : {
2325 : 532 : scratch.d.xmlexpr.named_argvalue = NULL;
2326 : 532 : scratch.d.xmlexpr.named_argnull = NULL;
2327 : : }
2328 : :
2329 [ + + ]: 572 : if (nargs)
2330 : : {
2331 : 512 : scratch.d.xmlexpr.argvalue = palloc_array(Datum, nargs);
2332 : 512 : scratch.d.xmlexpr.argnull = palloc_array(bool, nargs);
2333 : : }
2334 : : else
2335 : : {
2336 : 60 : scratch.d.xmlexpr.argvalue = NULL;
2337 : 60 : scratch.d.xmlexpr.argnull = NULL;
2338 : : }
2339 : :
2340 : : /* prepare argument execution */
2341 : 572 : off = 0;
2342 [ + + + + : 684 : foreach(arg, xexpr->named_args)
+ + ]
2343 : : {
2344 : 112 : Expr *e = (Expr *) lfirst(arg);
2345 : :
2346 : 112 : ExecInitExprRec(e, state,
2347 : 112 : &scratch.d.xmlexpr.named_argvalue[off],
2348 : 112 : &scratch.d.xmlexpr.named_argnull[off]);
2349 : 112 : off++;
2350 : : }
2351 : :
2352 : 572 : off = 0;
2353 [ + + + + : 1352 : foreach(arg, xexpr->args)
+ + ]
2354 : : {
2355 : 780 : Expr *e = (Expr *) lfirst(arg);
2356 : :
2357 : 780 : ExecInitExprRec(e, state,
2358 : 780 : &scratch.d.xmlexpr.argvalue[off],
2359 : 780 : &scratch.d.xmlexpr.argnull[off]);
2360 : 780 : off++;
2361 : : }
2362 : :
2363 : : /* and evaluate the actual XML expression */
2364 : 572 : ExprEvalPushStep(state, &scratch);
2365 : 572 : break;
2366 : : }
2367 : :
2368 : 183 : case T_JsonValueExpr:
2369 : : {
2370 : 183 : JsonValueExpr *jve = (JsonValueExpr *) node;
2371 : :
2372 : : Assert(jve->raw_expr != NULL);
2373 : 183 : ExecInitExprRec(jve->raw_expr, state, resv, resnull);
2374 : : Assert(jve->formatted_expr != NULL);
2375 : 183 : ExecInitExprRec(jve->formatted_expr, state, resv, resnull);
2376 : 183 : break;
2377 : : }
2378 : :
2379 : 1041 : case T_JsonConstructorExpr:
2380 : : {
2381 : 1041 : JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
2382 : 1041 : List *args = ctor->args;
2383 : : ListCell *lc;
2384 : 1041 : int nargs = list_length(args);
2385 : 1041 : int argno = 0;
2386 : :
2387 [ + + ]: 1041 : if (ctor->func)
2388 : : {
2389 : 380 : ExecInitExprRec(ctor->func, state, resv, resnull);
2390 : : }
2391 [ + + + + ]: 661 : else if ((ctor->type == JSCTOR_JSON_PARSE && !ctor->unique) ||
2392 [ + + ]: 581 : ctor->type == JSCTOR_JSON_SERIALIZE)
2393 : : {
2394 : : /* Use the value of the first argument as result */
2395 : 145 : ExecInitExprRec(linitial(args), state, resv, resnull);
2396 : : }
2397 : : else
2398 : : {
2399 : : JsonConstructorExprState *jcstate;
2400 : :
2401 : 516 : jcstate = palloc0_object(JsonConstructorExprState);
2402 : :
2403 : 516 : scratch.opcode = EEOP_JSON_CONSTRUCTOR;
2404 : 516 : scratch.d.json_constructor.jcstate = jcstate;
2405 : :
2406 : 516 : jcstate->constructor = ctor;
2407 : 516 : jcstate->arg_values = palloc_array(Datum, nargs);
2408 : 516 : jcstate->arg_nulls = palloc_array(bool, nargs);
2409 : 516 : jcstate->arg_types = palloc_array(Oid, nargs);
2410 : 516 : jcstate->nargs = nargs;
2411 : :
2412 [ + + + + : 1611 : foreach(lc, args)
+ + ]
2413 : : {
2414 : 1095 : Expr *arg = (Expr *) lfirst(lc);
2415 : :
2416 : 1095 : jcstate->arg_types[argno] = exprType((Node *) arg);
2417 : :
2418 [ + + ]: 1095 : if (IsA(arg, Const))
2419 : : {
2420 : : /* Don't evaluate const arguments every round */
2421 : 981 : Const *con = (Const *) arg;
2422 : :
2423 : 981 : jcstate->arg_values[argno] = con->constvalue;
2424 : 981 : jcstate->arg_nulls[argno] = con->constisnull;
2425 : : }
2426 : : else
2427 : : {
2428 : 114 : ExecInitExprRec(arg, state,
2429 : 114 : &jcstate->arg_values[argno],
2430 : 114 : &jcstate->arg_nulls[argno]);
2431 : : }
2432 : 1095 : argno++;
2433 : : }
2434 : :
2435 : : /* prepare type cache for datum_to_json[b]() */
2436 [ + + ]: 516 : if (ctor->type == JSCTOR_JSON_SCALAR)
2437 : : {
2438 : 71 : bool is_jsonb =
2439 : 71 : ctor->returning->format->format_type == JS_FORMAT_JSONB;
2440 : :
2441 : 71 : jcstate->arg_type_cache =
2442 : 71 : palloc(sizeof(*jcstate->arg_type_cache) * nargs);
2443 : :
2444 [ + + ]: 142 : for (int i = 0; i < nargs; i++)
2445 : : {
2446 : : JsonTypeCategory category;
2447 : : Oid outfuncid;
2448 : 71 : Oid typid = jcstate->arg_types[i];
2449 : :
2450 : 71 : json_categorize_type(typid, is_jsonb,
2451 : : &category, &outfuncid);
2452 : :
2453 : 71 : jcstate->arg_type_cache[i].outfuncid = outfuncid;
2454 : 71 : jcstate->arg_type_cache[i].category = (int) category;
2455 : : }
2456 : : }
2457 : :
2458 : 516 : ExprEvalPushStep(state, &scratch);
2459 : : }
2460 : :
2461 [ + + ]: 1041 : if (ctor->coercion)
2462 : : {
2463 : 291 : Datum *innermost_caseval = state->innermost_caseval;
2464 : 291 : bool *innermost_isnull = state->innermost_casenull;
2465 : :
2466 : 291 : state->innermost_caseval = resv;
2467 : 291 : state->innermost_casenull = resnull;
2468 : :
2469 : 291 : ExecInitExprRec(ctor->coercion, state, resv, resnull);
2470 : :
2471 : 291 : state->innermost_caseval = innermost_caseval;
2472 : 291 : state->innermost_casenull = innermost_isnull;
2473 : : }
2474 : : }
2475 : 1041 : break;
2476 : :
2477 : 276 : case T_JsonIsPredicate:
2478 : : {
2479 : 276 : JsonIsPredicate *pred = (JsonIsPredicate *) node;
2480 : :
2481 : 276 : ExecInitExprRec((Expr *) pred->expr, state, resv, resnull);
2482 : :
2483 : 276 : scratch.opcode = EEOP_IS_JSON;
2484 : 276 : scratch.d.is_json.pred = pred;
2485 : :
2486 : 276 : ExprEvalPushStep(state, &scratch);
2487 : 276 : break;
2488 : : }
2489 : :
2490 : 2636 : case T_JsonExpr:
2491 : : {
2492 : 2636 : JsonExpr *jsexpr = castNode(JsonExpr, node);
2493 : :
2494 : : /*
2495 : : * No need to initialize a full JsonExprState For
2496 : : * JSON_TABLE(), because the upstream caller tfuncFetchRows()
2497 : : * is only interested in the value of formatted_expr.
2498 : : */
2499 [ + + ]: 2636 : if (jsexpr->op == JSON_TABLE_OP)
2500 : 442 : ExecInitExprRec((Expr *) jsexpr->formatted_expr, state,
2501 : : resv, resnull);
2502 : : else
2503 : 2194 : ExecInitJsonExpr(jsexpr, state, resv, resnull, &scratch);
2504 : 2636 : break;
2505 : : }
2506 : :
2507 : 19239 : case T_NullTest:
2508 : : {
2509 : 19239 : NullTest *ntest = (NullTest *) node;
2510 : :
2511 [ + + ]: 19239 : if (ntest->nulltesttype == IS_NULL)
2512 : : {
2513 [ + + ]: 5435 : if (ntest->argisrow)
2514 : 182 : scratch.opcode = EEOP_NULLTEST_ROWISNULL;
2515 : : else
2516 : 5253 : scratch.opcode = EEOP_NULLTEST_ISNULL;
2517 : : }
2518 [ + - ]: 13804 : else if (ntest->nulltesttype == IS_NOT_NULL)
2519 : : {
2520 [ + + ]: 13804 : if (ntest->argisrow)
2521 : 181 : scratch.opcode = EEOP_NULLTEST_ROWISNOTNULL;
2522 : : else
2523 : 13623 : scratch.opcode = EEOP_NULLTEST_ISNOTNULL;
2524 : : }
2525 : : else
2526 : : {
2527 [ # # ]: 0 : elog(ERROR, "unrecognized nulltesttype: %d",
2528 : : (int) ntest->nulltesttype);
2529 : : }
2530 : : /* initialize cache in case it's a row test */
2531 : 19239 : scratch.d.nulltest_row.rowcache.cacheptr = NULL;
2532 : :
2533 : : /* first evaluate argument into result variable */
2534 : 19239 : ExecInitExprRec(ntest->arg, state,
2535 : : resv, resnull);
2536 : :
2537 : : /* then push the test of that argument */
2538 : 19239 : ExprEvalPushStep(state, &scratch);
2539 : 19239 : break;
2540 : : }
2541 : :
2542 : 942 : case T_BooleanTest:
2543 : : {
2544 : 942 : BooleanTest *btest = (BooleanTest *) node;
2545 : :
2546 : : /*
2547 : : * Evaluate argument, directly into result datum. That's ok,
2548 : : * because resv/resnull is definitely not used anywhere else,
2549 : : * and will get overwritten by the below EEOP_BOOLTEST_IS_*
2550 : : * step.
2551 : : */
2552 : 942 : ExecInitExprRec(btest->arg, state, resv, resnull);
2553 : :
2554 [ + + + + : 942 : switch (btest->booltesttype)
+ + - ]
2555 : : {
2556 : 301 : case IS_TRUE:
2557 : 301 : scratch.opcode = EEOP_BOOLTEST_IS_TRUE;
2558 : 301 : break;
2559 : 263 : case IS_NOT_TRUE:
2560 : 263 : scratch.opcode = EEOP_BOOLTEST_IS_NOT_TRUE;
2561 : 263 : break;
2562 : 142 : case IS_FALSE:
2563 : 142 : scratch.opcode = EEOP_BOOLTEST_IS_FALSE;
2564 : 142 : break;
2565 : 114 : case IS_NOT_FALSE:
2566 : 114 : scratch.opcode = EEOP_BOOLTEST_IS_NOT_FALSE;
2567 : 114 : break;
2568 : 46 : case IS_UNKNOWN:
2569 : : /* Same as scalar IS NULL test */
2570 : 46 : scratch.opcode = EEOP_NULLTEST_ISNULL;
2571 : 46 : break;
2572 : 76 : case IS_NOT_UNKNOWN:
2573 : : /* Same as scalar IS NOT NULL test */
2574 : 76 : scratch.opcode = EEOP_NULLTEST_ISNOTNULL;
2575 : 76 : break;
2576 : 0 : default:
2577 [ # # ]: 0 : elog(ERROR, "unrecognized booltesttype: %d",
2578 : : (int) btest->booltesttype);
2579 : : }
2580 : :
2581 : 942 : ExprEvalPushStep(state, &scratch);
2582 : 942 : break;
2583 : : }
2584 : :
2585 : 6111 : case T_CoerceToDomain:
2586 : : {
2587 : 6111 : CoerceToDomain *ctest = (CoerceToDomain *) node;
2588 : :
2589 : 6111 : ExecInitCoerceToDomain(&scratch, ctest, state,
2590 : : resv, resnull);
2591 : 6111 : break;
2592 : : }
2593 : :
2594 : 8640 : case T_CoerceToDomainValue:
2595 : : {
2596 : : /*
2597 : : * Read from location identified by innermost_domainval. Note
2598 : : * that innermost_domainval could be NULL, if we're compiling
2599 : : * a standalone domain check rather than one embedded in a
2600 : : * larger expression. In that case we must read from
2601 : : * econtext->domainValue_datum. We'll take care of that by
2602 : : * generating a specialized operation.
2603 : : */
2604 [ + + ]: 8640 : if (state->innermost_domainval == NULL)
2605 : 1897 : scratch.opcode = EEOP_DOMAIN_TESTVAL_EXT;
2606 : : else
2607 : : {
2608 : 6743 : scratch.opcode = EEOP_DOMAIN_TESTVAL;
2609 : : /* we share instruction union variant with case testval */
2610 : 6743 : scratch.d.casetest.value = state->innermost_domainval;
2611 : 6743 : scratch.d.casetest.isnull = state->innermost_domainnull;
2612 : : }
2613 : 8640 : ExprEvalPushStep(state, &scratch);
2614 : 8640 : break;
2615 : : }
2616 : :
2617 : 1 : case T_CurrentOfExpr:
2618 : : {
2619 : 1 : scratch.opcode = EEOP_CURRENTOFEXPR;
2620 : 1 : ExprEvalPushStep(state, &scratch);
2621 : 1 : break;
2622 : : }
2623 : :
2624 : 473 : case T_NextValueExpr:
2625 : : {
2626 : 473 : NextValueExpr *nve = (NextValueExpr *) node;
2627 : :
2628 : 473 : scratch.opcode = EEOP_NEXTVALUEEXPR;
2629 : 473 : scratch.d.nextvalueexpr.seqid = nve->seqid;
2630 : 473 : scratch.d.nextvalueexpr.seqtypid = nve->typeId;
2631 : :
2632 : 473 : ExprEvalPushStep(state, &scratch);
2633 : 473 : break;
2634 : : }
2635 : :
2636 : 304 : case T_ReturningExpr:
2637 : : {
2638 : 304 : ReturningExpr *rexpr = (ReturningExpr *) node;
2639 : : int retstep;
2640 : :
2641 : : /* Skip expression evaluation if OLD/NEW row doesn't exist */
2642 : 304 : scratch.opcode = EEOP_RETURNINGEXPR;
2643 [ + + ]: 304 : scratch.d.returningexpr.nullflag = rexpr->retold ?
2644 : : EEO_FLAG_OLD_IS_NULL : EEO_FLAG_NEW_IS_NULL;
2645 : 304 : scratch.d.returningexpr.jumpdone = -1; /* set below */
2646 : 304 : ExprEvalPushStep(state, &scratch);
2647 : 304 : retstep = state->steps_len - 1;
2648 : :
2649 : : /* Steps to evaluate expression to return */
2650 : 304 : ExecInitExprRec(rexpr->retexpr, state, resv, resnull);
2651 : :
2652 : : /* Jump target used if OLD/NEW row doesn't exist */
2653 : 304 : state->steps[retstep].d.returningexpr.jumpdone = state->steps_len;
2654 : :
2655 : : /* Update ExprState flags */
2656 [ + + ]: 304 : if (rexpr->retold)
2657 : 152 : state->flags |= EEO_FLAG_HAS_OLD;
2658 : : else
2659 : 152 : state->flags |= EEO_FLAG_HAS_NEW;
2660 : :
2661 : 304 : break;
2662 : : }
2663 : :
2664 : 0 : default:
2665 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d",
2666 : : (int) nodeTag(node));
2667 : : break;
2668 : : }
2669 : 3498089 : }
2670 : :
2671 : : /*
2672 : : * Add another expression evaluation step to ExprState->steps.
2673 : : *
2674 : : * Note that this potentially re-allocates es->steps, therefore no pointer
2675 : : * into that array may be used while the expression is still being built.
2676 : : */
2677 : : void
2678 : 8135579 : ExprEvalPushStep(ExprState *es, const ExprEvalStep *s)
2679 : : {
2680 [ + + ]: 8135579 : if (es->steps_alloc == 0)
2681 : : {
2682 : 1633756 : es->steps_alloc = 16;
2683 : 1633756 : es->steps = palloc_array(ExprEvalStep, es->steps_alloc);
2684 : : }
2685 [ + + ]: 6501823 : else if (es->steps_alloc == es->steps_len)
2686 : : {
2687 : 60674 : es->steps_alloc *= 2;
2688 : 60674 : es->steps = repalloc_array(es->steps, ExprEvalStep, es->steps_alloc);
2689 : : }
2690 : :
2691 : 8135579 : memcpy(&es->steps[es->steps_len++], s, sizeof(ExprEvalStep));
2692 : 8135579 : }
2693 : :
2694 : : /*
2695 : : * Perform setup necessary for the evaluation of a function-like expression,
2696 : : * appending argument evaluation steps to the steps list in *state, and
2697 : : * setting up *scratch so it is ready to be pushed.
2698 : : *
2699 : : * *scratch is not pushed here, so that callers may override the opcode,
2700 : : * which is useful for function-like cases like DISTINCT.
2701 : : */
2702 : : static void
2703 : 955165 : ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args, Oid funcid,
2704 : : Oid inputcollid, ExprState *state)
2705 : : {
2706 : 955165 : int nargs = list_length(args);
2707 : : AclResult aclresult;
2708 : : FmgrInfo *flinfo;
2709 : : FunctionCallInfo fcinfo;
2710 : : int argno;
2711 : : ListCell *lc;
2712 : :
2713 : : /* Check permission to call function */
2714 : 955165 : aclresult = object_aclcheck(ProcedureRelationId, funcid, GetUserId(), ACL_EXECUTE);
2715 [ + + ]: 955165 : if (aclresult != ACLCHECK_OK)
2716 : 64 : aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(funcid));
2717 [ - + ]: 955101 : InvokeFunctionExecuteHook(funcid);
2718 : :
2719 : : /*
2720 : : * Safety check on nargs. Under normal circumstances this should never
2721 : : * fail, as parser should check sooner. But possibly it might fail if
2722 : : * server has been compiled with FUNC_MAX_ARGS smaller than some functions
2723 : : * declared in pg_proc?
2724 : : */
2725 [ - + ]: 955101 : if (nargs > FUNC_MAX_ARGS)
2726 [ # # ]: 0 : ereport(ERROR,
2727 : : (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
2728 : : errmsg_plural("cannot pass more than %d argument to a function",
2729 : : "cannot pass more than %d arguments to a function",
2730 : : FUNC_MAX_ARGS,
2731 : : FUNC_MAX_ARGS)));
2732 : :
2733 : : /* Allocate function lookup data and parameter workspace for this call */
2734 : 955101 : scratch->d.func.finfo = palloc0_object(FmgrInfo);
2735 : 955101 : scratch->d.func.fcinfo_data = palloc0(SizeForFunctionCallInfo(nargs));
2736 : 955101 : flinfo = scratch->d.func.finfo;
2737 : 955101 : fcinfo = scratch->d.func.fcinfo_data;
2738 : :
2739 : : /* Set up the primary fmgr lookup information */
2740 : 955101 : fmgr_info(funcid, flinfo);
2741 : 955098 : fmgr_info_set_expr((Node *) node, flinfo);
2742 : :
2743 : : /* Initialize function call parameter structure too */
2744 : 955098 : InitFunctionCallInfoData(*fcinfo, flinfo,
2745 : : nargs, inputcollid, NULL, NULL);
2746 : :
2747 : : /* Keep extra copies of this info to save an indirection at runtime */
2748 : 955098 : scratch->d.func.fn_addr = flinfo->fn_addr;
2749 : 955098 : scratch->d.func.nargs = nargs;
2750 : :
2751 : : /* We only support non-set functions here */
2752 [ - + ]: 955098 : if (flinfo->fn_retset)
2753 [ # # # # ]: 0 : ereport(ERROR,
2754 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2755 : : errmsg("set-valued function called in context that cannot accept a set"),
2756 : : state->parent ?
2757 : : executor_errposition(state->parent->state,
2758 : : exprLocation((Node *) node)) : 0));
2759 : :
2760 : : /* Build code to evaluate arguments directly into the fcinfo struct */
2761 : 955098 : argno = 0;
2762 [ + + + + : 2628836 : foreach(lc, args)
+ + ]
2763 : : {
2764 : 1673738 : Expr *arg = (Expr *) lfirst(lc);
2765 : :
2766 [ + + ]: 1673738 : if (IsA(arg, Const))
2767 : : {
2768 : : /*
2769 : : * Don't evaluate const arguments every round; especially
2770 : : * interesting for constants in comparisons.
2771 : : */
2772 : 695958 : Const *con = (Const *) arg;
2773 : :
2774 : 695958 : fcinfo->args[argno].value = con->constvalue;
2775 : 695958 : fcinfo->args[argno].isnull = con->constisnull;
2776 : : }
2777 : : else
2778 : : {
2779 : 977780 : ExecInitExprRec(arg, state,
2780 : : &fcinfo->args[argno].value,
2781 : : &fcinfo->args[argno].isnull);
2782 : : }
2783 : 1673738 : argno++;
2784 : : }
2785 : :
2786 : : /* Insert appropriate opcode depending on strictness and stats level */
2787 [ + + ]: 955098 : if (pgstat_track_functions <= flinfo->fn_stats)
2788 : : {
2789 [ + + + + ]: 954982 : if (flinfo->fn_strict && nargs > 0)
2790 : : {
2791 : : /* Choose nargs optimized implementation if available. */
2792 [ + + ]: 864221 : if (nargs == 1)
2793 : 167618 : scratch->opcode = EEOP_FUNCEXPR_STRICT_1;
2794 [ + + ]: 696603 : else if (nargs == 2)
2795 : 672092 : scratch->opcode = EEOP_FUNCEXPR_STRICT_2;
2796 : : else
2797 : 24511 : scratch->opcode = EEOP_FUNCEXPR_STRICT;
2798 : : }
2799 : : else
2800 : 90761 : scratch->opcode = EEOP_FUNCEXPR;
2801 : : }
2802 : : else
2803 : : {
2804 [ + + + - ]: 116 : if (flinfo->fn_strict && nargs > 0)
2805 : 4 : scratch->opcode = EEOP_FUNCEXPR_STRICT_FUSAGE;
2806 : : else
2807 : 112 : scratch->opcode = EEOP_FUNCEXPR_FUSAGE;
2808 : : }
2809 : 955098 : }
2810 : :
2811 : : /*
2812 : : * Append the steps necessary for the evaluation of a SubPlan node to
2813 : : * ExprState->steps.
2814 : : *
2815 : : * subplan - SubPlan expression to evaluate
2816 : : * state - ExprState to whose ->steps to append the necessary operations
2817 : : * resv / resnull - where to store the result of the node into
2818 : : */
2819 : : static void
2820 : 19255 : ExecInitSubPlanExpr(SubPlan *subplan,
2821 : : ExprState *state,
2822 : : Datum *resv, bool *resnull)
2823 : : {
2824 : 19255 : ExprEvalStep scratch = {0};
2825 : : SubPlanState *sstate;
2826 : : ListCell *pvar;
2827 : : ListCell *l;
2828 : :
2829 [ - + ]: 19255 : if (!state->parent)
2830 [ # # ]: 0 : elog(ERROR, "SubPlan found with no parent plan");
2831 : :
2832 : : /*
2833 : : * Generate steps to evaluate input arguments for the subplan.
2834 : : *
2835 : : * We evaluate the argument expressions into resv/resnull, and then use
2836 : : * PARAM_SET to update the parameter. We do that, instead of evaluating
2837 : : * directly into the param, to avoid depending on the pointer value
2838 : : * remaining stable / being included in the generated expression. It's ok
2839 : : * to use resv/resnull for multiple params, as each parameter evaluation
2840 : : * is immediately followed by an EEOP_PARAM_SET (and thus are saved before
2841 : : * they could be overwritten again).
2842 : : *
2843 : : * Any calculation we have to do can be done in the parent econtext, since
2844 : : * the Param values don't need to have per-query lifetime.
2845 : : */
2846 : : Assert(list_length(subplan->parParam) == list_length(subplan->args));
2847 [ + + + + : 48556 : forboth(l, subplan->parParam, pvar, subplan->args)
+ + + + +
+ + - +
+ ]
2848 : : {
2849 : 29301 : int paramid = lfirst_int(l);
2850 : 29301 : Expr *arg = (Expr *) lfirst(pvar);
2851 : :
2852 : 29301 : ExecInitExprRec(arg, state, resv, resnull);
2853 : :
2854 : 29301 : scratch.opcode = EEOP_PARAM_SET;
2855 : 29301 : scratch.resvalue = resv;
2856 : 29301 : scratch.resnull = resnull;
2857 : 29301 : scratch.d.param.paramid = paramid;
2858 : : /* paramtype's not actually used, but we might as well fill it */
2859 : 29301 : scratch.d.param.paramtype = exprType((Node *) arg);
2860 : 29301 : ExprEvalPushStep(state, &scratch);
2861 : : }
2862 : :
2863 : 19255 : sstate = ExecInitSubPlan(subplan, state->parent);
2864 : :
2865 : : /* add SubPlanState nodes to state->parent->subPlan */
2866 : 19255 : state->parent->subPlan = lappend(state->parent->subPlan,
2867 : : sstate);
2868 : :
2869 : 19255 : scratch.opcode = EEOP_SUBPLAN;
2870 : 19255 : scratch.resvalue = resv;
2871 : 19255 : scratch.resnull = resnull;
2872 : 19255 : scratch.d.subplan.sstate = sstate;
2873 : :
2874 : 19255 : ExprEvalPushStep(state, &scratch);
2875 : 19255 : }
2876 : :
2877 : : /*
2878 : : * Add expression steps performing setup that's needed before any of the
2879 : : * main execution of the expression.
2880 : : */
2881 : : static void
2882 : 1561774 : ExecCreateExprSetupSteps(ExprState *state, Node *node)
2883 : : {
2884 : 1561774 : ExprSetupInfo info = {0, 0, 0, 0, 0, NIL};
2885 : :
2886 : : /* Prescan to find out what we need. */
2887 : 1561774 : expr_setup_walker(node, &info);
2888 : :
2889 : : /* And generate those steps. */
2890 : 1561770 : ExecPushExprSetupSteps(state, &info);
2891 : 1561770 : }
2892 : :
2893 : : /*
2894 : : * Add steps performing expression setup as indicated by "info".
2895 : : * This is useful when building an ExprState covering more than one expression.
2896 : : */
2897 : : static void
2898 : 1608175 : ExecPushExprSetupSteps(ExprState *state, ExprSetupInfo *info)
2899 : : {
2900 : 1608175 : ExprEvalStep scratch = {0};
2901 : : ListCell *lc;
2902 : :
2903 : 1608175 : scratch.resvalue = NULL;
2904 : 1608175 : scratch.resnull = NULL;
2905 : :
2906 : : /*
2907 : : * Add steps deforming the ExprState's inner/outer/scan/old/new slots as
2908 : : * much as required by any Vars appearing in the expression.
2909 : : */
2910 [ + + ]: 1608175 : if (info->last_inner > 0)
2911 : : {
2912 : 132255 : scratch.opcode = EEOP_INNER_FETCHSOME;
2913 : 132255 : scratch.d.fetch.last_var = info->last_inner;
2914 : 132255 : scratch.d.fetch.fixed = false;
2915 : 132255 : scratch.d.fetch.kind = NULL;
2916 : 132255 : scratch.d.fetch.known_desc = NULL;
2917 [ + + ]: 132255 : if (ExecComputeSlotInfo(state, &scratch))
2918 : 121850 : ExprEvalPushStep(state, &scratch);
2919 : : }
2920 [ + + ]: 1608175 : if (info->last_outer > 0)
2921 : : {
2922 : 257396 : scratch.opcode = EEOP_OUTER_FETCHSOME;
2923 : 257396 : scratch.d.fetch.last_var = info->last_outer;
2924 : 257396 : scratch.d.fetch.fixed = false;
2925 : 257396 : scratch.d.fetch.kind = NULL;
2926 : 257396 : scratch.d.fetch.known_desc = NULL;
2927 [ + + ]: 257396 : if (ExecComputeSlotInfo(state, &scratch))
2928 : 140467 : ExprEvalPushStep(state, &scratch);
2929 : : }
2930 [ + + ]: 1608175 : if (info->last_scan > 0)
2931 : : {
2932 : 414072 : scratch.opcode = EEOP_SCAN_FETCHSOME;
2933 : 414072 : scratch.d.fetch.last_var = info->last_scan;
2934 : 414072 : scratch.d.fetch.fixed = false;
2935 : 414072 : scratch.d.fetch.kind = NULL;
2936 : 414072 : scratch.d.fetch.known_desc = NULL;
2937 [ + + ]: 414072 : if (ExecComputeSlotInfo(state, &scratch))
2938 : 386350 : ExprEvalPushStep(state, &scratch);
2939 : : }
2940 [ + + ]: 1608175 : if (info->last_old > 0)
2941 : : {
2942 : 336 : scratch.opcode = EEOP_OLD_FETCHSOME;
2943 : 336 : scratch.d.fetch.last_var = info->last_old;
2944 : 336 : scratch.d.fetch.fixed = false;
2945 : 336 : scratch.d.fetch.kind = NULL;
2946 : 336 : scratch.d.fetch.known_desc = NULL;
2947 [ + - ]: 336 : if (ExecComputeSlotInfo(state, &scratch))
2948 : 336 : ExprEvalPushStep(state, &scratch);
2949 : : }
2950 [ + + ]: 1608175 : if (info->last_new > 0)
2951 : : {
2952 : 322 : scratch.opcode = EEOP_NEW_FETCHSOME;
2953 : 322 : scratch.d.fetch.last_var = info->last_new;
2954 : 322 : scratch.d.fetch.fixed = false;
2955 : 322 : scratch.d.fetch.kind = NULL;
2956 : 322 : scratch.d.fetch.known_desc = NULL;
2957 [ + - ]: 322 : if (ExecComputeSlotInfo(state, &scratch))
2958 : 322 : ExprEvalPushStep(state, &scratch);
2959 : : }
2960 : :
2961 : : /*
2962 : : * Add steps to execute any MULTIEXPR SubPlans appearing in the
2963 : : * expression. We need to evaluate these before any of the Params
2964 : : * referencing their outputs are used, but after we've prepared for any
2965 : : * Var references they may contain. (There cannot be cross-references
2966 : : * between MULTIEXPR SubPlans, so we needn't worry about their order.)
2967 : : */
2968 [ + + + + : 1608258 : foreach(lc, info->multiexpr_subplans)
+ + ]
2969 : : {
2970 : 83 : SubPlan *subplan = (SubPlan *) lfirst(lc);
2971 : :
2972 : : Assert(subplan->subLinkType == MULTIEXPR_SUBLINK);
2973 : :
2974 : : /* The result can be ignored, but we better put it somewhere */
2975 : 83 : ExecInitSubPlanExpr(subplan, state,
2976 : : &state->resvalue, &state->resnull);
2977 : : }
2978 : 1608175 : }
2979 : :
2980 : : /*
2981 : : * expr_setup_walker: expression walker for ExecCreateExprSetupSteps
2982 : : */
2983 : : static bool
2984 : 7626268 : expr_setup_walker(Node *node, ExprSetupInfo *info)
2985 : : {
2986 [ + + ]: 7626268 : if (node == NULL)
2987 : 293215 : return false;
2988 [ + + ]: 7333053 : if (IsA(node, Var))
2989 : : {
2990 : 1728701 : Var *variable = (Var *) node;
2991 : 1728701 : AttrNumber attnum = variable->varattno;
2992 : :
2993 [ + + + ]: 1728701 : switch (variable->varno)
2994 : : {
2995 : 270986 : case INNER_VAR:
2996 : 270986 : info->last_inner = Max(info->last_inner, attnum);
2997 : 270986 : break;
2998 : :
2999 : 597903 : case OUTER_VAR:
3000 : 597903 : info->last_outer = Max(info->last_outer, attnum);
3001 : 597903 : break;
3002 : :
3003 : : /* INDEX_VAR is handled by default case */
3004 : :
3005 : 859812 : default:
3006 [ + + + - ]: 859812 : switch (variable->varreturningtype)
3007 : : {
3008 : 857342 : case VAR_RETURNING_DEFAULT:
3009 : 857342 : info->last_scan = Max(info->last_scan, attnum);
3010 : 857342 : break;
3011 : 1246 : case VAR_RETURNING_OLD:
3012 : 1246 : info->last_old = Max(info->last_old, attnum);
3013 : 1246 : break;
3014 : 1224 : case VAR_RETURNING_NEW:
3015 : 1224 : info->last_new = Max(info->last_new, attnum);
3016 : 1224 : break;
3017 : : }
3018 : 859812 : break;
3019 : : }
3020 : 1728701 : return false;
3021 : : }
3022 : :
3023 : : /* Collect all MULTIEXPR SubPlans, too */
3024 [ + + ]: 5604352 : if (IsA(node, SubPlan))
3025 : : {
3026 : 19271 : SubPlan *subplan = (SubPlan *) node;
3027 : :
3028 [ + + ]: 19271 : if (subplan->subLinkType == MULTIEXPR_SUBLINK)
3029 : 83 : info->multiexpr_subplans = lappend(info->multiexpr_subplans,
3030 : : subplan);
3031 : : }
3032 : :
3033 : : /*
3034 : : * Don't examine the arguments or filters of Aggrefs or WindowFuncs,
3035 : : * because those do not represent expressions to be evaluated within the
3036 : : * calling expression's econtext. GroupingFunc arguments are never
3037 : : * evaluated at all.
3038 : : */
3039 [ + + ]: 5604352 : if (IsA(node, Aggref))
3040 : 39754 : return false;
3041 [ + + ]: 5564598 : if (IsA(node, WindowFunc))
3042 : 2640 : return false;
3043 [ + + ]: 5561958 : if (IsA(node, GroupingFunc))
3044 : 248 : return false;
3045 : 5561710 : return expression_tree_walker(node, expr_setup_walker, info);
3046 : : }
3047 : :
3048 : : /*
3049 : : * Compute additional information for EEOP_*_FETCHSOME ops.
3050 : : *
3051 : : * The goal is to determine whether a slot is 'fixed', that is, every
3052 : : * evaluation of the expression will have the same type of slot, with an
3053 : : * equivalent descriptor.
3054 : : *
3055 : : * EEOP_OLD_FETCHSOME and EEOP_NEW_FETCHSOME are used to process RETURNING, if
3056 : : * OLD/NEW columns are referred to explicitly. In both cases, the tuple
3057 : : * descriptor comes from the parent scan node, so we treat them the same as
3058 : : * EEOP_SCAN_FETCHSOME.
3059 : : *
3060 : : * Returns true if the deforming step is required, false otherwise.
3061 : : */
3062 : : static bool
3063 : 841592 : ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op)
3064 : : {
3065 : 841592 : PlanState *parent = state->parent;
3066 : 841592 : TupleDesc desc = NULL;
3067 : 841592 : const TupleTableSlotOps *tts_ops = NULL;
3068 : 841592 : bool isfixed = false;
3069 : 841592 : ExprEvalOp opcode = op->opcode;
3070 : :
3071 : : Assert(opcode == EEOP_INNER_FETCHSOME ||
3072 : : opcode == EEOP_OUTER_FETCHSOME ||
3073 : : opcode == EEOP_SCAN_FETCHSOME ||
3074 : : opcode == EEOP_OLD_FETCHSOME ||
3075 : : opcode == EEOP_NEW_FETCHSOME);
3076 : :
3077 [ + + ]: 841592 : if (op->d.fetch.known_desc != NULL)
3078 : : {
3079 : 37211 : desc = op->d.fetch.known_desc;
3080 : 37211 : tts_ops = op->d.fetch.kind;
3081 : 37211 : isfixed = op->d.fetch.kind != NULL;
3082 : : }
3083 [ + + ]: 804381 : else if (!parent)
3084 : : {
3085 : 10051 : isfixed = false;
3086 : : }
3087 [ + + ]: 794330 : else if (opcode == EEOP_INNER_FETCHSOME)
3088 : : {
3089 : 132185 : PlanState *is = innerPlanState(parent);
3090 : :
3091 [ - + - - ]: 132185 : if (parent->inneropsset && !parent->inneropsfixed)
3092 : : {
3093 : 0 : isfixed = false;
3094 : : }
3095 [ - + - - ]: 132185 : else if (parent->inneropsset && parent->innerops)
3096 : : {
3097 : 0 : isfixed = true;
3098 : 0 : tts_ops = parent->innerops;
3099 : 0 : desc = ExecGetResultType(is);
3100 : : }
3101 [ + + ]: 132185 : else if (is)
3102 : : {
3103 : 130279 : tts_ops = ExecGetResultSlotOps(is, &isfixed);
3104 : 130279 : desc = ExecGetResultType(is);
3105 : : }
3106 : : }
3107 [ + + ]: 662145 : else if (opcode == EEOP_OUTER_FETCHSOME)
3108 : : {
3109 : 257256 : PlanState *os = outerPlanState(parent);
3110 : :
3111 [ + + + + ]: 257256 : if (parent->outeropsset && !parent->outeropsfixed)
3112 : : {
3113 : 863 : isfixed = false;
3114 : : }
3115 [ + + + - ]: 256393 : else if (parent->outeropsset && parent->outerops)
3116 : : {
3117 : 31256 : isfixed = true;
3118 : 31256 : tts_ops = parent->outerops;
3119 : 31256 : desc = ExecGetResultType(os);
3120 : : }
3121 [ + + ]: 225137 : else if (os)
3122 : : {
3123 : 225129 : tts_ops = ExecGetResultSlotOps(os, &isfixed);
3124 : 225129 : desc = ExecGetResultType(os);
3125 : : }
3126 : : }
3127 [ + + + + ]: 404889 : else if (opcode == EEOP_SCAN_FETCHSOME ||
3128 [ + - ]: 322 : opcode == EEOP_OLD_FETCHSOME ||
3129 : : opcode == EEOP_NEW_FETCHSOME)
3130 : : {
3131 : 404889 : desc = parent->scandesc;
3132 : :
3133 [ + + ]: 404889 : if (parent->scanops)
3134 : 388512 : tts_ops = parent->scanops;
3135 : :
3136 [ + + ]: 404889 : if (parent->scanopsset)
3137 : 388512 : isfixed = parent->scanopsfixed;
3138 : : }
3139 : :
3140 [ + + + - : 841592 : if (isfixed && desc != NULL && tts_ops != NULL)
+ - ]
3141 : : {
3142 : 793958 : op->d.fetch.fixed = true;
3143 : 793958 : op->d.fetch.kind = tts_ops;
3144 : 793958 : op->d.fetch.known_desc = desc;
3145 : : }
3146 : : else
3147 : : {
3148 : 47634 : op->d.fetch.fixed = false;
3149 : 47634 : op->d.fetch.kind = NULL;
3150 : 47634 : op->d.fetch.known_desc = NULL;
3151 : : }
3152 : :
3153 : : /* if the slot is known to always virtual we never need to deform */
3154 [ + + + + ]: 841592 : if (op->d.fetch.fixed && op->d.fetch.kind == &TTSOpsVirtual)
3155 : 160068 : return false;
3156 : :
3157 : 681524 : return true;
3158 : : }
3159 : :
3160 : : /*
3161 : : * Prepare step for the evaluation of a whole-row variable.
3162 : : * The caller still has to push the step.
3163 : : */
3164 : : static void
3165 : 3548 : ExecInitWholeRowVar(ExprEvalStep *scratch, Var *variable, ExprState *state)
3166 : : {
3167 : 3548 : PlanState *parent = state->parent;
3168 : :
3169 : : /* fill in all but the target */
3170 : 3548 : scratch->opcode = EEOP_WHOLEROW;
3171 : 3548 : scratch->d.wholerow.var = variable;
3172 : 3548 : scratch->d.wholerow.first = true;
3173 : 3548 : scratch->d.wholerow.slow = false;
3174 : 3548 : scratch->d.wholerow.tupdesc = NULL; /* filled at runtime */
3175 : 3548 : scratch->d.wholerow.junkFilter = NULL;
3176 : :
3177 : : /* update ExprState flags if Var refers to OLD/NEW */
3178 [ + + ]: 3548 : if (variable->varreturningtype == VAR_RETURNING_OLD)
3179 : 93 : state->flags |= EEO_FLAG_HAS_OLD;
3180 [ + + ]: 3455 : else if (variable->varreturningtype == VAR_RETURNING_NEW)
3181 : 93 : state->flags |= EEO_FLAG_HAS_NEW;
3182 : :
3183 : : /*
3184 : : * If the input tuple came from a subquery, it might contain "resjunk"
3185 : : * columns (such as GROUP BY or ORDER BY columns), which we don't want to
3186 : : * keep in the whole-row result. We can get rid of such columns by
3187 : : * passing the tuple through a JunkFilter --- but to make one, we have to
3188 : : * lay our hands on the subquery's targetlist. Fortunately, there are not
3189 : : * very many cases where this can happen, and we can identify all of them
3190 : : * by examining our parent PlanState. We assume this is not an issue in
3191 : : * standalone expressions that don't have parent plans. (Whole-row Vars
3192 : : * can occur in such expressions, but they will always be referencing
3193 : : * table rows.)
3194 : : */
3195 [ + + ]: 3548 : if (parent)
3196 : : {
3197 : 3457 : PlanState *subplan = NULL;
3198 : :
3199 [ + + + ]: 3457 : switch (nodeTag(parent))
3200 : : {
3201 : 234 : case T_SubqueryScanState:
3202 : 234 : subplan = ((SubqueryScanState *) parent)->subplan;
3203 : 234 : break;
3204 : 105 : case T_CteScanState:
3205 : 105 : subplan = ((CteScanState *) parent)->cteplanstate;
3206 : 105 : break;
3207 : 3118 : default:
3208 : 3118 : break;
3209 : : }
3210 : :
3211 [ + + ]: 3457 : if (subplan)
3212 : : {
3213 : 339 : bool junk_filter_needed = false;
3214 : : ListCell *tlist;
3215 : :
3216 : : /* Detect whether subplan tlist actually has any junk columns */
3217 [ + - + + : 1050 : foreach(tlist, subplan->plan->targetlist)
+ + ]
3218 : : {
3219 : 725 : TargetEntry *tle = (TargetEntry *) lfirst(tlist);
3220 : :
3221 [ + + ]: 725 : if (tle->resjunk)
3222 : : {
3223 : 14 : junk_filter_needed = true;
3224 : 14 : break;
3225 : : }
3226 : : }
3227 : :
3228 : : /* If so, build the junkfilter now */
3229 [ + + ]: 339 : if (junk_filter_needed)
3230 : : {
3231 : 14 : scratch->d.wholerow.junkFilter =
3232 : 14 : ExecInitJunkFilter(subplan->plan->targetlist,
3233 : : ExecInitExtraTupleSlot(parent->state, NULL,
3234 : : &TTSOpsVirtual));
3235 : : }
3236 : : }
3237 : : }
3238 : 3548 : }
3239 : :
3240 : : /*
3241 : : * Prepare evaluation of a SubscriptingRef expression.
3242 : : */
3243 : : static void
3244 : 20310 : ExecInitSubscriptingRef(ExprEvalStep *scratch, SubscriptingRef *sbsref,
3245 : : ExprState *state, Datum *resv, bool *resnull)
3246 : : {
3247 : 20310 : bool isAssignment = (sbsref->refassgnexpr != NULL);
3248 : 20310 : int nupper = list_length(sbsref->refupperindexpr);
3249 : 20310 : int nlower = list_length(sbsref->reflowerindexpr);
3250 : : const SubscriptRoutines *sbsroutines;
3251 : : SubscriptingRefState *sbsrefstate;
3252 : : SubscriptExecSteps methods;
3253 : : char *ptr;
3254 : 20310 : List *adjust_jumps = NIL;
3255 : : ListCell *lc;
3256 : : int i;
3257 : :
3258 : : /* Look up the subscripting support methods */
3259 : 20310 : sbsroutines = getSubscriptingRoutines(sbsref->refcontainertype, NULL);
3260 [ - + ]: 20310 : if (!sbsroutines)
3261 [ # # # # ]: 0 : ereport(ERROR,
3262 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
3263 : : errmsg("cannot subscript type %s because it does not support subscripting",
3264 : : format_type_be(sbsref->refcontainertype)),
3265 : : state->parent ?
3266 : : executor_errposition(state->parent->state,
3267 : : exprLocation((Node *) sbsref)) : 0));
3268 : :
3269 : : /* Allocate sbsrefstate, with enough space for per-subscript arrays too */
3270 : 20310 : sbsrefstate = palloc0(MAXALIGN(sizeof(SubscriptingRefState)) +
3271 : 20310 : (nupper + nlower) * (sizeof(Datum) +
3272 : : 2 * sizeof(bool)));
3273 : :
3274 : : /* Fill constant fields of SubscriptingRefState */
3275 : 20310 : sbsrefstate->isassignment = isAssignment;
3276 : 20310 : sbsrefstate->numupper = nupper;
3277 : 20310 : sbsrefstate->numlower = nlower;
3278 : : /* Set up per-subscript arrays */
3279 : 20310 : ptr = ((char *) sbsrefstate) + MAXALIGN(sizeof(SubscriptingRefState));
3280 : 20310 : sbsrefstate->upperindex = (Datum *) ptr;
3281 : 20310 : ptr += nupper * sizeof(Datum);
3282 : 20310 : sbsrefstate->lowerindex = (Datum *) ptr;
3283 : 20310 : ptr += nlower * sizeof(Datum);
3284 : 20310 : sbsrefstate->upperprovided = (bool *) ptr;
3285 : 20310 : ptr += nupper * sizeof(bool);
3286 : 20310 : sbsrefstate->lowerprovided = (bool *) ptr;
3287 : 20310 : ptr += nlower * sizeof(bool);
3288 : 20310 : sbsrefstate->upperindexnull = (bool *) ptr;
3289 : 20310 : ptr += nupper * sizeof(bool);
3290 : 20310 : sbsrefstate->lowerindexnull = (bool *) ptr;
3291 : : /* ptr += nlower * sizeof(bool); */
3292 : :
3293 : : /*
3294 : : * Let the container-type-specific code have a chance. It must fill the
3295 : : * "methods" struct with function pointers for us to possibly use in
3296 : : * execution steps below; and it can optionally set up some data pointed
3297 : : * to by the workspace field.
3298 : : */
3299 : 20310 : memset(&methods, 0, sizeof(methods));
3300 : 20310 : sbsroutines->exec_setup(sbsref, sbsrefstate, &methods);
3301 : :
3302 : : /*
3303 : : * Evaluate array input. It's safe to do so into resv/resnull, because we
3304 : : * won't use that as target for any of the other subexpressions, and it'll
3305 : : * be overwritten by the final EEOP_SBSREF_FETCH/ASSIGN step, which is
3306 : : * pushed last.
3307 : : */
3308 : 20310 : ExecInitExprRec(sbsref->refexpr, state, resv, resnull);
3309 : :
3310 : : /*
3311 : : * If refexpr yields NULL, and the operation should be strict, then result
3312 : : * is NULL. We can implement this with just JUMP_IF_NULL, since we
3313 : : * evaluated the array into the desired target location.
3314 : : */
3315 [ + + + - ]: 20310 : if (!isAssignment && sbsroutines->fetch_strict)
3316 : : {
3317 : 19400 : scratch->opcode = EEOP_JUMP_IF_NULL;
3318 : 19400 : scratch->d.jump.jumpdone = -1; /* adjust later */
3319 : 19400 : ExprEvalPushStep(state, scratch);
3320 : 19400 : adjust_jumps = lappend_int(adjust_jumps,
3321 : 19400 : state->steps_len - 1);
3322 : : }
3323 : :
3324 : : /* Evaluate upper subscripts */
3325 : 20310 : i = 0;
3326 [ + - + + : 41044 : foreach(lc, sbsref->refupperindexpr)
+ + ]
3327 : : {
3328 : 20734 : Expr *e = (Expr *) lfirst(lc);
3329 : :
3330 : : /* When slicing, individual subscript bounds can be omitted */
3331 [ + + ]: 20734 : if (!e)
3332 : : {
3333 : 52 : sbsrefstate->upperprovided[i] = false;
3334 : 52 : sbsrefstate->upperindexnull[i] = true;
3335 : : }
3336 : : else
3337 : : {
3338 : 20682 : sbsrefstate->upperprovided[i] = true;
3339 : : /* Each subscript is evaluated into appropriate array entry */
3340 : 20682 : ExecInitExprRec(e, state,
3341 : 20682 : &sbsrefstate->upperindex[i],
3342 : 20682 : &sbsrefstate->upperindexnull[i]);
3343 : : }
3344 : 20734 : i++;
3345 : : }
3346 : :
3347 : : /* Evaluate lower subscripts similarly */
3348 : 20310 : i = 0;
3349 [ + + + + : 20735 : foreach(lc, sbsref->reflowerindexpr)
+ + ]
3350 : : {
3351 : 425 : Expr *e = (Expr *) lfirst(lc);
3352 : :
3353 : : /* When slicing, individual subscript bounds can be omitted */
3354 [ + + ]: 425 : if (!e)
3355 : : {
3356 : 52 : sbsrefstate->lowerprovided[i] = false;
3357 : 52 : sbsrefstate->lowerindexnull[i] = true;
3358 : : }
3359 : : else
3360 : : {
3361 : 373 : sbsrefstate->lowerprovided[i] = true;
3362 : : /* Each subscript is evaluated into appropriate array entry */
3363 : 373 : ExecInitExprRec(e, state,
3364 : 373 : &sbsrefstate->lowerindex[i],
3365 : 373 : &sbsrefstate->lowerindexnull[i]);
3366 : : }
3367 : 425 : i++;
3368 : : }
3369 : :
3370 : : /* SBSREF_SUBSCRIPTS checks and converts all the subscripts at once */
3371 [ + + ]: 20310 : if (methods.sbs_check_subscripts)
3372 : : {
3373 : 20303 : scratch->opcode = EEOP_SBSREF_SUBSCRIPTS;
3374 : 20303 : scratch->d.sbsref_subscript.subscriptfunc = methods.sbs_check_subscripts;
3375 : 20303 : scratch->d.sbsref_subscript.state = sbsrefstate;
3376 : 20303 : scratch->d.sbsref_subscript.jumpdone = -1; /* adjust later */
3377 : 20303 : ExprEvalPushStep(state, scratch);
3378 : 20303 : adjust_jumps = lappend_int(adjust_jumps,
3379 : 20303 : state->steps_len - 1);
3380 : : }
3381 : :
3382 [ + + ]: 20310 : if (isAssignment)
3383 : : {
3384 : : Datum *save_innermost_caseval;
3385 : : bool *save_innermost_casenull;
3386 : :
3387 : : /* Check for unimplemented methods */
3388 [ - + ]: 910 : if (!methods.sbs_assign)
3389 [ # # ]: 0 : ereport(ERROR,
3390 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3391 : : errmsg("type %s does not support subscripted assignment",
3392 : : format_type_be(sbsref->refcontainertype))));
3393 : :
3394 : : /*
3395 : : * We might have a nested-assignment situation, in which the
3396 : : * refassgnexpr is itself a FieldStore or SubscriptingRef that needs
3397 : : * to obtain and modify the previous value of the array element or
3398 : : * slice being replaced. If so, we have to extract that value from
3399 : : * the array and pass it down via the CaseTestExpr mechanism. It's
3400 : : * safe to reuse the CASE mechanism because there cannot be a CASE
3401 : : * between here and where the value would be needed, and an array
3402 : : * assignment can't be within a CASE either. (So saving and restoring
3403 : : * innermost_caseval is just paranoia, but let's do it anyway.)
3404 : : *
3405 : : * Since fetching the old element might be a nontrivial expense, do it
3406 : : * only if the argument actually needs it.
3407 : : */
3408 [ + + ]: 910 : if (isAssignmentIndirectionExpr(sbsref->refassgnexpr))
3409 : : {
3410 [ - + ]: 127 : if (!methods.sbs_fetch_old)
3411 [ # # ]: 0 : ereport(ERROR,
3412 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3413 : : errmsg("type %s does not support subscripted assignment",
3414 : : format_type_be(sbsref->refcontainertype))));
3415 : 127 : scratch->opcode = EEOP_SBSREF_OLD;
3416 : 127 : scratch->d.sbsref.subscriptfunc = methods.sbs_fetch_old;
3417 : 127 : scratch->d.sbsref.state = sbsrefstate;
3418 : 127 : ExprEvalPushStep(state, scratch);
3419 : : }
3420 : :
3421 : : /* SBSREF_OLD puts extracted value into prevvalue/prevnull */
3422 : 910 : save_innermost_caseval = state->innermost_caseval;
3423 : 910 : save_innermost_casenull = state->innermost_casenull;
3424 : 910 : state->innermost_caseval = &sbsrefstate->prevvalue;
3425 : 910 : state->innermost_casenull = &sbsrefstate->prevnull;
3426 : :
3427 : : /* evaluate replacement value into replacevalue/replacenull */
3428 : 910 : ExecInitExprRec(sbsref->refassgnexpr, state,
3429 : : &sbsrefstate->replacevalue, &sbsrefstate->replacenull);
3430 : :
3431 : 910 : state->innermost_caseval = save_innermost_caseval;
3432 : 910 : state->innermost_casenull = save_innermost_casenull;
3433 : :
3434 : : /* and perform the assignment */
3435 : 910 : scratch->opcode = EEOP_SBSREF_ASSIGN;
3436 : 910 : scratch->d.sbsref.subscriptfunc = methods.sbs_assign;
3437 : 910 : scratch->d.sbsref.state = sbsrefstate;
3438 : 910 : ExprEvalPushStep(state, scratch);
3439 : : }
3440 : : else
3441 : : {
3442 : : /* array fetch is much simpler */
3443 : 19400 : scratch->opcode = EEOP_SBSREF_FETCH;
3444 : 19400 : scratch->d.sbsref.subscriptfunc = methods.sbs_fetch;
3445 : 19400 : scratch->d.sbsref.state = sbsrefstate;
3446 : 19400 : ExprEvalPushStep(state, scratch);
3447 : : }
3448 : :
3449 : : /* adjust jump targets */
3450 [ + + + + : 60013 : foreach(lc, adjust_jumps)
+ + ]
3451 : : {
3452 : 39703 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
3453 : :
3454 [ + + ]: 39703 : if (as->opcode == EEOP_SBSREF_SUBSCRIPTS)
3455 : : {
3456 : : Assert(as->d.sbsref_subscript.jumpdone == -1);
3457 : 20303 : as->d.sbsref_subscript.jumpdone = state->steps_len;
3458 : : }
3459 : : else
3460 : : {
3461 : : Assert(as->opcode == EEOP_JUMP_IF_NULL);
3462 : : Assert(as->d.jump.jumpdone == -1);
3463 : 19400 : as->d.jump.jumpdone = state->steps_len;
3464 : : }
3465 : : }
3466 : 20310 : }
3467 : :
3468 : : /*
3469 : : * Helper for preparing SubscriptingRef expressions for evaluation: is expr
3470 : : * a nested FieldStore or SubscriptingRef that needs the old element value
3471 : : * passed down?
3472 : : *
3473 : : * (We could use this in FieldStore too, but in that case passing the old
3474 : : * value is so cheap there's no need.)
3475 : : *
3476 : : * Note: it might seem that this needs to recurse, but in most cases it does
3477 : : * not; the CaseTestExpr, if any, will be directly the arg or refexpr of the
3478 : : * top-level node. Nested-assignment situations give rise to expression
3479 : : * trees in which each level of assignment has its own CaseTestExpr, and the
3480 : : * recursive structure appears within the newvals or refassgnexpr field.
3481 : : * There is an exception, though: if the array is an array-of-domain, we will
3482 : : * have a CoerceToDomain or RelabelType as the refassgnexpr, and we need to
3483 : : * be able to look through that.
3484 : : */
3485 : : static bool
3486 : 966 : isAssignmentIndirectionExpr(Expr *expr)
3487 : : {
3488 [ - + ]: 966 : if (expr == NULL)
3489 : 0 : return false; /* just paranoia */
3490 [ + + ]: 966 : if (IsA(expr, FieldStore))
3491 : : {
3492 : 127 : FieldStore *fstore = (FieldStore *) expr;
3493 : :
3494 [ + - + - ]: 127 : if (fstore->arg && IsA(fstore->arg, CaseTestExpr))
3495 : 127 : return true;
3496 : : }
3497 [ + + ]: 839 : else if (IsA(expr, SubscriptingRef))
3498 : : {
3499 : 21 : SubscriptingRef *sbsRef = (SubscriptingRef *) expr;
3500 : :
3501 [ + - - + ]: 21 : if (sbsRef->refexpr && IsA(sbsRef->refexpr, CaseTestExpr))
3502 : 0 : return true;
3503 : : }
3504 [ + + ]: 818 : else if (IsA(expr, CoerceToDomain))
3505 : : {
3506 : 44 : CoerceToDomain *cd = (CoerceToDomain *) expr;
3507 : :
3508 : 44 : return isAssignmentIndirectionExpr(cd->arg);
3509 : : }
3510 [ + + ]: 774 : else if (IsA(expr, RelabelType))
3511 : : {
3512 : 12 : RelabelType *r = (RelabelType *) expr;
3513 : :
3514 : 12 : return isAssignmentIndirectionExpr(r->arg);
3515 : : }
3516 : 783 : return false;
3517 : : }
3518 : :
3519 : : /*
3520 : : * Prepare evaluation of a CoerceToDomain expression.
3521 : : */
3522 : : static void
3523 : 6111 : ExecInitCoerceToDomain(ExprEvalStep *scratch, CoerceToDomain *ctest,
3524 : : ExprState *state, Datum *resv, bool *resnull)
3525 : : {
3526 : : DomainConstraintRef *constraint_ref;
3527 : 6111 : Datum *domainval = NULL;
3528 : 6111 : bool *domainnull = NULL;
3529 : : ListCell *l;
3530 : 6111 : List *skipjumps = NIL;
3531 : :
3532 : 6111 : scratch->d.domaincheck.resulttype = ctest->resulttype;
3533 : : /* we'll allocate workspace only if needed */
3534 : 6111 : scratch->d.domaincheck.checkvalue = NULL;
3535 : 6111 : scratch->d.domaincheck.checknull = NULL;
3536 : 6111 : scratch->d.domaincheck.escontext = state->escontext;
3537 : :
3538 : : /*
3539 : : * Evaluate argument - it's fine to directly store it into resv/resnull,
3540 : : * if there's constraint failures there'll be errors, otherwise it's what
3541 : : * needs to be returned.
3542 : : */
3543 : 6111 : ExecInitExprRec(ctest->arg, state, resv, resnull);
3544 : :
3545 : : /*
3546 : : * Note: if the argument is of varlena type, it could be a R/W expanded
3547 : : * object. We want to return the R/W pointer as the final result, but we
3548 : : * have to pass a R/O pointer as the value to be tested by any functions
3549 : : * in check expressions. We don't bother to emit a MAKE_READONLY step
3550 : : * unless there's actually at least one check expression, though. Until
3551 : : * we've tested that, domainval/domainnull are NULL.
3552 : : */
3553 : :
3554 : : /*
3555 : : * Collect the constraints associated with the domain.
3556 : : *
3557 : : * Note: before PG v10 we'd recheck the set of constraints during each
3558 : : * evaluation of the expression. Now we bake them into the ExprState
3559 : : * during executor initialization. That means we don't need typcache.c to
3560 : : * provide compiled exprs.
3561 : : */
3562 : 6111 : constraint_ref = palloc_object(DomainConstraintRef);
3563 : 6111 : InitDomainConstraintRef(ctest->resulttype,
3564 : : constraint_ref,
3565 : : CurrentMemoryContext,
3566 : : false);
3567 : :
3568 : : /*
3569 : : * Compile code to check each domain constraint. NOTNULL constraints can
3570 : : * just be applied on the resv/resnull value, but for CHECK constraints we
3571 : : * need more pushups.
3572 : : */
3573 [ + + + + : 12760 : foreach(l, constraint_ref->constraints)
+ + ]
3574 : : {
3575 : 6649 : DomainConstraintState *con = (DomainConstraintState *) lfirst(l);
3576 : : Datum *save_innermost_domainval;
3577 : : bool *save_innermost_domainnull;
3578 : :
3579 : 6649 : scratch->d.domaincheck.constraintname = con->name;
3580 : :
3581 : : /*
3582 : : * If this CoerceToDomain is being evaluated softly and its argument,
3583 : : * or a constraint checked before this one, has already failed, the
3584 : : * result is going to be discarded, so don't check the remaining
3585 : : * constraints against it; doing so might throw a hard error or
3586 : : * overwrite the saved error.
3587 : : */
3588 [ + + ]: 6649 : if (state->escontext != NULL)
3589 : : {
3590 : 88 : ExprEvalStep jump = {0};
3591 : :
3592 : 88 : jump.opcode = EEOP_JUMP_IF_NULL;
3593 : 88 : jump.resnull = &state->escontext->error_occurred;
3594 : 88 : jump.d.jump.jumpdone = -1; /* set below */
3595 : 88 : skipjumps = lappend_int(skipjumps, state->steps_len);
3596 : 88 : ExprEvalPushStep(state, &jump);
3597 : : }
3598 : :
3599 [ + + - ]: 6649 : switch (con->constrainttype)
3600 : : {
3601 : 272 : case DOM_CONSTRAINT_NOTNULL:
3602 : 272 : scratch->opcode = EEOP_DOMAIN_NOTNULL;
3603 : 272 : ExprEvalPushStep(state, scratch);
3604 : 272 : break;
3605 : 6377 : case DOM_CONSTRAINT_CHECK:
3606 : : /* Allocate workspace for CHECK output if we didn't yet */
3607 [ + + ]: 6377 : if (scratch->d.domaincheck.checkvalue == NULL)
3608 : : {
3609 : 5931 : scratch->d.domaincheck.checkvalue =
3610 : 5931 : palloc_object(Datum);
3611 : 5931 : scratch->d.domaincheck.checknull =
3612 : 5931 : palloc_object(bool);
3613 : : }
3614 : :
3615 : : /*
3616 : : * If first time through, determine where CoerceToDomainValue
3617 : : * nodes should read from.
3618 : : */
3619 [ + + ]: 6377 : if (domainval == NULL)
3620 : : {
3621 : : /*
3622 : : * Since value might be read multiple times, force to R/O
3623 : : * - but only if it could be an expanded datum.
3624 : : */
3625 [ + + ]: 5931 : if (get_typlen(ctest->resulttype) == -1)
3626 : : {
3627 : 2031 : ExprEvalStep scratch2 = {0};
3628 : :
3629 : : /* Yes, so make output workspace for MAKE_READONLY */
3630 : 2031 : domainval = palloc_object(Datum);
3631 : 2031 : domainnull = palloc_object(bool);
3632 : :
3633 : : /* Emit MAKE_READONLY */
3634 : 2031 : scratch2.opcode = EEOP_MAKE_READONLY;
3635 : 2031 : scratch2.resvalue = domainval;
3636 : 2031 : scratch2.resnull = domainnull;
3637 : 2031 : scratch2.d.make_readonly.value = resv;
3638 : 2031 : scratch2.d.make_readonly.isnull = resnull;
3639 : 2031 : ExprEvalPushStep(state, &scratch2);
3640 : : }
3641 : : else
3642 : : {
3643 : : /* No, so it's fine to read from resv/resnull */
3644 : 3900 : domainval = resv;
3645 : 3900 : domainnull = resnull;
3646 : : }
3647 : : }
3648 : :
3649 : : /*
3650 : : * Set up value to be returned by CoerceToDomainValue nodes.
3651 : : * We must save and restore innermost_domainval/null fields,
3652 : : * in case this node is itself within a check expression for
3653 : : * another domain.
3654 : : */
3655 : 6377 : save_innermost_domainval = state->innermost_domainval;
3656 : 6377 : save_innermost_domainnull = state->innermost_domainnull;
3657 : 6377 : state->innermost_domainval = domainval;
3658 : 6377 : state->innermost_domainnull = domainnull;
3659 : :
3660 : : /* evaluate check expression value */
3661 : 6377 : ExecInitExprRec(con->check_expr, state,
3662 : : scratch->d.domaincheck.checkvalue,
3663 : : scratch->d.domaincheck.checknull);
3664 : :
3665 : 6377 : state->innermost_domainval = save_innermost_domainval;
3666 : 6377 : state->innermost_domainnull = save_innermost_domainnull;
3667 : :
3668 : : /* now test result */
3669 : 6377 : scratch->opcode = EEOP_DOMAIN_CHECK;
3670 : 6377 : ExprEvalPushStep(state, scratch);
3671 : :
3672 : 6377 : break;
3673 : 0 : default:
3674 [ # # ]: 0 : elog(ERROR, "unrecognized constraint type: %d",
3675 : : (int) con->constrainttype);
3676 : : break;
3677 : : }
3678 : : }
3679 : :
3680 : : /* Now that we know where the constraint steps end, fix up the jumps. */
3681 [ + + + + : 12310 : foreach_int(jumpno, skipjumps)
+ + ]
3682 : 88 : state->steps[jumpno].d.jump.jumpdone = state->steps_len;
3683 : 6111 : }
3684 : :
3685 : : /*
3686 : : * Build transition/combine function invocations for all aggregate transition
3687 : : * / combination function invocations in a grouping sets phase. This has to
3688 : : * invoke all sort based transitions in a phase (if doSort is true), all hash
3689 : : * based transitions (if doHash is true), or both (both true).
3690 : : *
3691 : : * The resulting expression will, for each set of transition values, first
3692 : : * check for filters, evaluate aggregate input, check that that input is not
3693 : : * NULL for a strict transition function, and then finally invoke the
3694 : : * transition for each of the concurrently computed grouping sets.
3695 : : *
3696 : : * If nullcheck is true, the generated code will check for a NULL pointer to
3697 : : * the array of AggStatePerGroup, and skip evaluation if so.
3698 : : */
3699 : : ExprState *
3700 : 35931 : ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
3701 : : bool doSort, bool doHash, bool nullcheck)
3702 : : {
3703 : 35931 : ExprState *state = makeNode(ExprState);
3704 : 35931 : PlanState *parent = &aggstate->ss.ps;
3705 : 35931 : ExprEvalStep scratch = {0};
3706 : 35931 : bool isCombine = DO_AGGSPLIT_COMBINE(aggstate->aggsplit);
3707 : 35931 : ExprSetupInfo deform = {0, 0, 0, 0, 0, NIL};
3708 : :
3709 : 35931 : state->expr = (Expr *) aggstate;
3710 : 35931 : state->parent = parent;
3711 : :
3712 : 35931 : scratch.resvalue = &state->resvalue;
3713 : 35931 : scratch.resnull = &state->resnull;
3714 : :
3715 : : /*
3716 : : * First figure out which slots, and how many columns from each, we're
3717 : : * going to need.
3718 : : */
3719 [ + + ]: 75514 : for (int transno = 0; transno < aggstate->numtrans; transno++)
3720 : : {
3721 : 39583 : AggStatePerTrans pertrans = &aggstate->pertrans[transno];
3722 : :
3723 : 39583 : expr_setup_walker((Node *) pertrans->aggref->aggdirectargs,
3724 : : &deform);
3725 : 39583 : expr_setup_walker((Node *) pertrans->aggref->args,
3726 : : &deform);
3727 : 39583 : expr_setup_walker((Node *) pertrans->aggref->aggorder,
3728 : : &deform);
3729 : 39583 : expr_setup_walker((Node *) pertrans->aggref->aggdistinct,
3730 : : &deform);
3731 : 39583 : expr_setup_walker((Node *) pertrans->aggref->aggfilter,
3732 : : &deform);
3733 : : }
3734 : 35931 : ExecPushExprSetupSteps(state, &deform);
3735 : :
3736 : : /*
3737 : : * Emit instructions for each transition value / grouping set combination.
3738 : : */
3739 [ + + ]: 75514 : for (int transno = 0; transno < aggstate->numtrans; transno++)
3740 : : {
3741 : 39583 : AggStatePerTrans pertrans = &aggstate->pertrans[transno];
3742 : 39583 : FunctionCallInfo trans_fcinfo = pertrans->transfn_fcinfo;
3743 : 39583 : List *adjust_bailout = NIL;
3744 : 39583 : NullableDatum *strictargs = NULL;
3745 : 39583 : bool *strictnulls = NULL;
3746 : : int argno;
3747 : : ListCell *bail;
3748 : :
3749 : : /*
3750 : : * If filter present, emit. Do so before evaluating the input, to
3751 : : * avoid potentially unneeded computations, or even worse, unintended
3752 : : * side-effects. When combining, all the necessary filtering has
3753 : : * already been done.
3754 : : */
3755 [ + + + - ]: 39583 : if (pertrans->aggref->aggfilter && !isCombine)
3756 : : {
3757 : : /* evaluate filter expression */
3758 : 437 : ExecInitExprRec(pertrans->aggref->aggfilter, state,
3759 : : &state->resvalue, &state->resnull);
3760 : : /* and jump out if false */
3761 : 437 : scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
3762 : 437 : scratch.d.jump.jumpdone = -1; /* adjust later */
3763 : 437 : ExprEvalPushStep(state, &scratch);
3764 : 437 : adjust_bailout = lappend_int(adjust_bailout,
3765 : 437 : state->steps_len - 1);
3766 : : }
3767 : :
3768 : : /*
3769 : : * Evaluate arguments to aggregate/combine function.
3770 : : */
3771 : 39583 : argno = 0;
3772 [ + + ]: 39583 : if (isCombine)
3773 : : {
3774 : : /*
3775 : : * Combining two aggregate transition values. Instead of directly
3776 : : * coming from a tuple the input is a, potentially deserialized,
3777 : : * transition value.
3778 : : */
3779 : : TargetEntry *source_tle;
3780 : :
3781 : : Assert(pertrans->numSortCols == 0);
3782 : : Assert(list_length(pertrans->aggref->args) == 1);
3783 : :
3784 : 1611 : strictargs = trans_fcinfo->args + 1;
3785 : 1611 : source_tle = (TargetEntry *) linitial(pertrans->aggref->args);
3786 : :
3787 : : /*
3788 : : * deserialfn_oid will be set if we must deserialize the input
3789 : : * state before calling the combine function.
3790 : : */
3791 [ + + ]: 1611 : if (!OidIsValid(pertrans->deserialfn_oid))
3792 : : {
3793 : : /*
3794 : : * Start from 1, since the 0th arg will be the transition
3795 : : * value
3796 : : */
3797 : 1531 : ExecInitExprRec(source_tle->expr, state,
3798 : 1531 : &trans_fcinfo->args[argno + 1].value,
3799 : 1531 : &trans_fcinfo->args[argno + 1].isnull);
3800 : : }
3801 : : else
3802 : : {
3803 : 80 : FunctionCallInfo ds_fcinfo = pertrans->deserialfn_fcinfo;
3804 : :
3805 : : /* evaluate argument */
3806 : 80 : ExecInitExprRec(source_tle->expr, state,
3807 : : &ds_fcinfo->args[0].value,
3808 : : &ds_fcinfo->args[0].isnull);
3809 : :
3810 : : /* Dummy second argument for type-safety reasons */
3811 : 80 : ds_fcinfo->args[1].value = PointerGetDatum(NULL);
3812 : 80 : ds_fcinfo->args[1].isnull = false;
3813 : :
3814 : : /*
3815 : : * Don't call a strict deserialization function with NULL
3816 : : * input
3817 : : */
3818 [ + - ]: 80 : if (pertrans->deserialfn.fn_strict)
3819 : 80 : scratch.opcode = EEOP_AGG_STRICT_DESERIALIZE;
3820 : : else
3821 : 0 : scratch.opcode = EEOP_AGG_DESERIALIZE;
3822 : :
3823 : 80 : scratch.d.agg_deserialize.fcinfo_data = ds_fcinfo;
3824 : 80 : scratch.d.agg_deserialize.jumpnull = -1; /* adjust later */
3825 : 80 : scratch.resvalue = &trans_fcinfo->args[argno + 1].value;
3826 : 80 : scratch.resnull = &trans_fcinfo->args[argno + 1].isnull;
3827 : :
3828 : 80 : ExprEvalPushStep(state, &scratch);
3829 : : /* don't add an adjustment unless the function is strict */
3830 [ + - ]: 80 : if (pertrans->deserialfn.fn_strict)
3831 : 80 : adjust_bailout = lappend_int(adjust_bailout,
3832 : 80 : state->steps_len - 1);
3833 : :
3834 : : /* restore normal settings of scratch fields */
3835 : 80 : scratch.resvalue = &state->resvalue;
3836 : 80 : scratch.resnull = &state->resnull;
3837 : : }
3838 : 1611 : argno++;
3839 : :
3840 : : Assert(pertrans->numInputs == argno);
3841 : : }
3842 [ + + ]: 37972 : else if (!pertrans->aggsortrequired)
3843 : : {
3844 : : ListCell *arg;
3845 : :
3846 : : /*
3847 : : * Normal transition function without ORDER BY / DISTINCT or with
3848 : : * ORDER BY / DISTINCT but the planner has given us pre-sorted
3849 : : * input.
3850 : : */
3851 : 37760 : strictargs = trans_fcinfo->args + 1;
3852 : :
3853 [ + + + + : 65169 : foreach(arg, pertrans->aggref->args)
+ + ]
3854 : : {
3855 : 28386 : TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
3856 : :
3857 : : /*
3858 : : * Don't initialize args for any ORDER BY clause that might
3859 : : * exist in a presorted aggregate.
3860 : : */
3861 [ + + ]: 28386 : if (argno == pertrans->numTransInputs)
3862 : 977 : break;
3863 : :
3864 : : /*
3865 : : * Start from 1, since the 0th arg will be the transition
3866 : : * value
3867 : : */
3868 : 27409 : ExecInitExprRec(source_tle->expr, state,
3869 : 27409 : &trans_fcinfo->args[argno + 1].value,
3870 : 27409 : &trans_fcinfo->args[argno + 1].isnull);
3871 : 27409 : argno++;
3872 : : }
3873 : : Assert(pertrans->numTransInputs == argno);
3874 : : }
3875 [ + + + + ]: 212 : else if (pertrans->numInputs == 1 && pertrans->numSortCols == 1)
3876 : 172 : {
3877 : : /*
3878 : : * Non-presorted DISTINCT and/or ORDER BY case, with a single
3879 : : * input column sorted on a single key.
3880 : : */
3881 : 172 : TargetEntry *source_tle =
3882 : 172 : (TargetEntry *) linitial(pertrans->aggref->args);
3883 : :
3884 : : Assert(list_length(pertrans->aggref->args) == 1);
3885 : :
3886 : 172 : ExecInitExprRec(source_tle->expr, state,
3887 : : &state->resvalue,
3888 : : &state->resnull);
3889 : 172 : strictnulls = &state->resnull;
3890 : 172 : argno++;
3891 : :
3892 : : Assert(pertrans->numInputs == argno);
3893 : : }
3894 : : else
3895 : : {
3896 : : /*
3897 : : * Non-presorted DISTINCT and/or ORDER BY case, with multiple
3898 : : * input columns or sort keys.
3899 : : */
3900 : 40 : Datum *values = pertrans->sortslot->tts_values;
3901 : 40 : bool *nulls = pertrans->sortslot->tts_isnull;
3902 : : ListCell *arg;
3903 : :
3904 : 40 : strictnulls = nulls;
3905 : :
3906 [ + - + + : 128 : foreach(arg, pertrans->aggref->args)
+ + ]
3907 : : {
3908 : 88 : TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
3909 : :
3910 : 88 : ExecInitExprRec(source_tle->expr, state,
3911 : 88 : &values[argno], &nulls[argno]);
3912 : 88 : argno++;
3913 : : }
3914 : : Assert(pertrans->numInputs == argno);
3915 : : }
3916 : :
3917 : : /*
3918 : : * For a strict transfn, nothing happens when there's a NULL input; we
3919 : : * just keep the prior transValue. This is true for both plain and
3920 : : * sorted/distinct aggregates.
3921 : : */
3922 [ + + + + ]: 39583 : if (trans_fcinfo->flinfo->fn_strict && pertrans->numTransInputs > 0)
3923 : : {
3924 [ + + ]: 7477 : if (strictnulls)
3925 : 116 : scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_NULLS;
3926 [ + - + + ]: 7361 : else if (strictargs && pertrans->numTransInputs == 1)
3927 : 7057 : scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1;
3928 : : else
3929 : 304 : scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_ARGS;
3930 : 7477 : scratch.d.agg_strict_input_check.nulls = strictnulls;
3931 : 7477 : scratch.d.agg_strict_input_check.args = strictargs;
3932 : 7477 : scratch.d.agg_strict_input_check.jumpnull = -1; /* adjust later */
3933 : 7477 : scratch.d.agg_strict_input_check.nargs = pertrans->numTransInputs;
3934 : 7477 : ExprEvalPushStep(state, &scratch);
3935 : 7477 : adjust_bailout = lappend_int(adjust_bailout,
3936 : 7477 : state->steps_len - 1);
3937 : : }
3938 : :
3939 : : /* Handle DISTINCT aggregates which have pre-sorted input */
3940 [ + + + + ]: 39583 : if (pertrans->numDistinctCols > 0 && !pertrans->aggsortrequired)
3941 : : {
3942 [ + + ]: 319 : if (pertrans->numDistinctCols > 1)
3943 : 76 : scratch.opcode = EEOP_AGG_PRESORTED_DISTINCT_MULTI;
3944 : : else
3945 : 243 : scratch.opcode = EEOP_AGG_PRESORTED_DISTINCT_SINGLE;
3946 : :
3947 : 319 : scratch.d.agg_presorted_distinctcheck.pertrans = pertrans;
3948 : 319 : scratch.d.agg_presorted_distinctcheck.jumpdistinct = -1; /* adjust later */
3949 : 319 : ExprEvalPushStep(state, &scratch);
3950 : 319 : adjust_bailout = lappend_int(adjust_bailout,
3951 : 319 : state->steps_len - 1);
3952 : : }
3953 : :
3954 : : /*
3955 : : * Call transition function (once for each concurrently evaluated
3956 : : * grouping set). Do so for both sort and hash based computations, as
3957 : : * applicable.
3958 : : */
3959 [ + + ]: 39583 : if (doSort)
3960 : : {
3961 : 33940 : int processGroupingSets = Max(phase->numsets, 1);
3962 : 33940 : int setoff = 0;
3963 : :
3964 [ + + ]: 68668 : for (int setno = 0; setno < processGroupingSets; setno++)
3965 : : {
3966 : 34728 : ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
3967 : : pertrans, transno, setno, setoff, false,
3968 : : nullcheck);
3969 : 34728 : setoff++;
3970 : : }
3971 : : }
3972 : :
3973 [ + + ]: 39583 : if (doHash)
3974 : : {
3975 : 5893 : int numHashes = aggstate->num_hashes;
3976 : : int setoff;
3977 : :
3978 : : /* in MIXED mode, there'll be preceding transition values */
3979 [ + + ]: 5893 : if (aggstate->aggstrategy != AGG_HASHED)
3980 : 266 : setoff = aggstate->maxsets;
3981 : : else
3982 : 5627 : setoff = 0;
3983 : :
3984 [ + + ]: 12592 : for (int setno = 0; setno < numHashes; setno++)
3985 : : {
3986 : 6699 : ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
3987 : : pertrans, transno, setno, setoff, true,
3988 : : nullcheck);
3989 : 6699 : setoff++;
3990 : : }
3991 : : }
3992 : :
3993 : : /* adjust early bail out jump target(s) */
3994 [ + + + + : 47896 : foreach(bail, adjust_bailout)
+ + ]
3995 : : {
3996 : 8313 : ExprEvalStep *as = &state->steps[lfirst_int(bail)];
3997 : :
3998 [ + + ]: 8313 : if (as->opcode == EEOP_JUMP_IF_NOT_TRUE)
3999 : : {
4000 : : Assert(as->d.jump.jumpdone == -1);
4001 : 437 : as->d.jump.jumpdone = state->steps_len;
4002 : : }
4003 [ + + ]: 7876 : else if (as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS ||
4004 [ + + ]: 7572 : as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1 ||
4005 [ + + ]: 515 : as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
4006 : : {
4007 : : Assert(as->d.agg_strict_input_check.jumpnull == -1);
4008 : 7477 : as->d.agg_strict_input_check.jumpnull = state->steps_len;
4009 : : }
4010 [ + + ]: 399 : else if (as->opcode == EEOP_AGG_STRICT_DESERIALIZE)
4011 : : {
4012 : : Assert(as->d.agg_deserialize.jumpnull == -1);
4013 : 80 : as->d.agg_deserialize.jumpnull = state->steps_len;
4014 : : }
4015 [ + + ]: 319 : else if (as->opcode == EEOP_AGG_PRESORTED_DISTINCT_SINGLE ||
4016 [ + - ]: 76 : as->opcode == EEOP_AGG_PRESORTED_DISTINCT_MULTI)
4017 : : {
4018 : : Assert(as->d.agg_presorted_distinctcheck.jumpdistinct == -1);
4019 : 319 : as->d.agg_presorted_distinctcheck.jumpdistinct = state->steps_len;
4020 : : }
4021 : : else
4022 : : Assert(false);
4023 : : }
4024 : : }
4025 : :
4026 : 35931 : scratch.resvalue = NULL;
4027 : 35931 : scratch.resnull = NULL;
4028 : 35931 : scratch.opcode = EEOP_DONE_NO_RETURN;
4029 : 35931 : ExprEvalPushStep(state, &scratch);
4030 : :
4031 : 35931 : ExecReadyExpr(state);
4032 : :
4033 : 35931 : return state;
4034 : : }
4035 : :
4036 : : /*
4037 : : * Build transition/combine function invocation for a single transition
4038 : : * value. This is separated from ExecBuildAggTrans() because there are
4039 : : * multiple callsites (hash and sort in some grouping set cases).
4040 : : */
4041 : : static void
4042 : 41427 : ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
4043 : : ExprEvalStep *scratch,
4044 : : FunctionCallInfo fcinfo, AggStatePerTrans pertrans,
4045 : : int transno, int setno, int setoff, bool ishash,
4046 : : bool nullcheck)
4047 : : {
4048 : : ExprContext *aggcontext;
4049 : 41427 : int adjust_jumpnull = -1;
4050 : :
4051 [ + + ]: 41427 : if (ishash)
4052 : 6699 : aggcontext = aggstate->hashcontext;
4053 : : else
4054 : 34728 : aggcontext = aggstate->aggcontexts[setno];
4055 : :
4056 : : /* add check for NULL pointer? */
4057 [ + + ]: 41427 : if (nullcheck)
4058 : : {
4059 : 302 : scratch->opcode = EEOP_AGG_PLAIN_PERGROUP_NULLCHECK;
4060 : 302 : scratch->d.agg_plain_pergroup_nullcheck.setoff = setoff;
4061 : : /* adjust later */
4062 : 302 : scratch->d.agg_plain_pergroup_nullcheck.jumpnull = -1;
4063 : 302 : ExprEvalPushStep(state, scratch);
4064 : 302 : adjust_jumpnull = state->steps_len - 1;
4065 : : }
4066 : :
4067 : : /*
4068 : : * Determine appropriate transition implementation.
4069 : : *
4070 : : * For non-ordered aggregates and ORDER BY / DISTINCT aggregates with
4071 : : * presorted input:
4072 : : *
4073 : : * If the initial value for the transition state doesn't exist in the
4074 : : * pg_aggregate table then we will let the first non-NULL value returned
4075 : : * from the outer procNode become the initial value. (This is useful for
4076 : : * aggregates like max() and min().) The noTransValue flag signals that we
4077 : : * need to do so. If true, generate a
4078 : : * EEOP_AGG_INIT_STRICT_PLAIN_TRANS{,_BYVAL} step. This step also needs to
4079 : : * do the work described next:
4080 : : *
4081 : : * If the function is strict, but does have an initial value, choose
4082 : : * EEOP_AGG_STRICT_PLAIN_TRANS{,_BYVAL}, which skips the transition
4083 : : * function if the transition value has become NULL (because a previous
4084 : : * transition function returned NULL). This step also needs to do the work
4085 : : * described next:
4086 : : *
4087 : : * Otherwise we call EEOP_AGG_PLAIN_TRANS{,_BYVAL}, which does not have to
4088 : : * perform either of the above checks.
4089 : : *
4090 : : * Having steps with overlapping responsibilities is not nice, but
4091 : : * aggregations are very performance sensitive, making this worthwhile.
4092 : : *
4093 : : * For ordered aggregates:
4094 : : *
4095 : : * Only need to choose between the faster path for a single ordered
4096 : : * column, and the one between multiple columns. Checking strictness etc
4097 : : * is done when finalizing the aggregate. See
4098 : : * process_ordered_aggregate_{single, multi} and
4099 : : * advance_transition_function.
4100 : : */
4101 [ + + ]: 41427 : if (!pertrans->aggsortrequired)
4102 : : {
4103 [ + + ]: 41183 : if (pertrans->transtypeByVal)
4104 : : {
4105 [ + + ]: 38143 : if (fcinfo->flinfo->fn_strict &&
4106 [ + + ]: 19781 : pertrans->initValueIsNull)
4107 : 3205 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL;
4108 [ + + ]: 34938 : else if (fcinfo->flinfo->fn_strict)
4109 : 16576 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL;
4110 : : else
4111 : 18362 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_BYVAL;
4112 : : }
4113 : : else
4114 : : {
4115 [ + + ]: 3040 : if (fcinfo->flinfo->fn_strict &&
4116 [ + + ]: 2793 : pertrans->initValueIsNull)
4117 : 754 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF;
4118 [ + + ]: 2286 : else if (fcinfo->flinfo->fn_strict)
4119 : 2039 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_STRICT_BYREF;
4120 : : else
4121 : 247 : scratch->opcode = EEOP_AGG_PLAIN_TRANS_BYREF;
4122 : : }
4123 : : }
4124 [ + + + + ]: 244 : else if (pertrans->numInputs == 1 && pertrans->numSortCols == 1)
4125 : 196 : scratch->opcode = EEOP_AGG_ORDERED_TRANS_DATUM;
4126 : : else
4127 : 48 : scratch->opcode = EEOP_AGG_ORDERED_TRANS_TUPLE;
4128 : :
4129 : 41427 : scratch->d.agg_trans.pertrans = pertrans;
4130 : 41427 : scratch->d.agg_trans.setno = setno;
4131 : 41427 : scratch->d.agg_trans.setoff = setoff;
4132 : 41427 : scratch->d.agg_trans.transno = transno;
4133 : 41427 : scratch->d.agg_trans.aggcontext = aggcontext;
4134 : 41427 : ExprEvalPushStep(state, scratch);
4135 : :
4136 : : /* fix up jumpnull */
4137 [ + + ]: 41427 : if (adjust_jumpnull != -1)
4138 : : {
4139 : 302 : ExprEvalStep *as = &state->steps[adjust_jumpnull];
4140 : :
4141 : : Assert(as->opcode == EEOP_AGG_PLAIN_PERGROUP_NULLCHECK);
4142 : : Assert(as->d.agg_plain_pergroup_nullcheck.jumpnull == -1);
4143 : 302 : as->d.agg_plain_pergroup_nullcheck.jumpnull = state->steps_len;
4144 : : }
4145 : 41427 : }
4146 : :
4147 : : /*
4148 : : * Build an ExprState that calls the given hash function(s) on the attnums
4149 : : * given by 'keyColIdx' . When numCols > 1, the hash values returned by each
4150 : : * hash function are combined to produce a single hash value.
4151 : : *
4152 : : * desc: tuple descriptor for the to-be-hashed columns
4153 : : * ops: TupleTableSlotOps to use for the give TupleDesc
4154 : : * hashfunctions: FmgrInfos for each hash function to call, one per numCols.
4155 : : * These are used directly in the returned ExprState so must remain allocated.
4156 : : * collations: collation to use when calling the hash function.
4157 : : * numCols: array length of hashfunctions, collations and keyColIdx.
4158 : : * parent: PlanState node that the resulting ExprState will be evaluated at
4159 : : * init_value: Normally 0, but can be set to other values to seed the hash
4160 : : * with. Non-zero is marginally slower, so best to only use if it's provably
4161 : : * worthwhile.
4162 : : */
4163 : : ExprState *
4164 : 6263 : ExecBuildHash32FromAttrs(TupleDesc desc, const TupleTableSlotOps *ops,
4165 : : FmgrInfo *hashfunctions, Oid *collations,
4166 : : int numCols, AttrNumber *keyColIdx,
4167 : : PlanState *parent, uint32 init_value)
4168 : : {
4169 : 6263 : ExprState *state = makeNode(ExprState);
4170 : 6263 : ExprEvalStep scratch = {0};
4171 : 6263 : NullableDatum *iresult = NULL;
4172 : : intptr_t opcode;
4173 : 6263 : AttrNumber last_attnum = 0;
4174 : :
4175 : : Assert(numCols >= 0);
4176 : :
4177 : 6263 : state->parent = parent;
4178 : :
4179 : : /*
4180 : : * Make a place to store intermediate hash values between subsequent
4181 : : * hashing of individual columns. We only need this if there is more than
4182 : : * one column to hash or an initial value plus one column.
4183 : : */
4184 [ + + ]: 6263 : if ((int64) numCols + (init_value != 0) > 1)
4185 : 2330 : iresult = palloc_object(NullableDatum);
4186 : :
4187 : : /* find the highest attnum so we deform the tuple to that point */
4188 [ + + ]: 16831 : for (int i = 0; i < numCols; i++)
4189 : 10568 : last_attnum = Max(last_attnum, keyColIdx[i]);
4190 : :
4191 : 6263 : scratch.opcode = EEOP_INNER_FETCHSOME;
4192 : 6263 : scratch.d.fetch.last_var = last_attnum;
4193 : 6263 : scratch.d.fetch.fixed = false;
4194 : 6263 : scratch.d.fetch.kind = ops;
4195 : 6263 : scratch.d.fetch.known_desc = desc;
4196 [ + + ]: 6263 : if (ExecComputeSlotInfo(state, &scratch))
4197 : 4329 : ExprEvalPushStep(state, &scratch);
4198 : :
4199 [ + + ]: 6263 : if (init_value == 0)
4200 : : {
4201 : : /*
4202 : : * No initial value, so we can assign the result of the hash function
4203 : : * for the first attribute without having to concern ourselves with
4204 : : * combining the result with any initial value.
4205 : : */
4206 : 5699 : opcode = EEOP_HASHDATUM_FIRST;
4207 : : }
4208 : : else
4209 : : {
4210 : : /*
4211 : : * Set up operation to set the initial value. Normally we store this
4212 : : * in the intermediate hash value location, but if there are no
4213 : : * columns to hash, store it in the ExprState's result field.
4214 : : */
4215 : 564 : scratch.opcode = EEOP_HASHDATUM_SET_INITVAL;
4216 : 564 : scratch.d.hashdatum_initvalue.init_value = UInt32GetDatum(init_value);
4217 [ + - ]: 564 : scratch.resvalue = numCols > 0 ? &iresult->value : &state->resvalue;
4218 [ + - ]: 564 : scratch.resnull = numCols > 0 ? &iresult->isnull : &state->resnull;
4219 : :
4220 : 564 : ExprEvalPushStep(state, &scratch);
4221 : :
4222 : : /*
4223 : : * When using an initial value use the NEXT32 ops as the FIRST ops
4224 : : * would overwrite the stored initial value.
4225 : : */
4226 : 564 : opcode = EEOP_HASHDATUM_NEXT32;
4227 : : }
4228 : :
4229 [ + + ]: 16831 : for (int i = 0; i < numCols; i++)
4230 : : {
4231 : : FmgrInfo *finfo;
4232 : : FunctionCallInfo fcinfo;
4233 : 10568 : Oid inputcollid = collations[i];
4234 : 10568 : AttrNumber attnum = keyColIdx[i] - 1;
4235 : :
4236 : 10568 : finfo = &hashfunctions[i];
4237 : 10568 : fcinfo = palloc0(SizeForFunctionCallInfo(1));
4238 : :
4239 : : /* Initialize function call parameter structure too */
4240 : 10568 : InitFunctionCallInfoData(*fcinfo, finfo, 1, inputcollid, NULL, NULL);
4241 : :
4242 : : /*
4243 : : * Fetch inner Var for this attnum and store it in the 1st arg of the
4244 : : * hash func.
4245 : : */
4246 : 10568 : scratch.opcode = EEOP_INNER_VAR;
4247 : 10568 : scratch.resvalue = &fcinfo->args[0].value;
4248 : 10568 : scratch.resnull = &fcinfo->args[0].isnull;
4249 : 10568 : scratch.d.var.attnum = attnum;
4250 : 10568 : scratch.d.var.vartype = TupleDescAttr(desc, attnum)->atttypid;
4251 : 10568 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
4252 : :
4253 : 10568 : ExprEvalPushStep(state, &scratch);
4254 : :
4255 : : /* Call the hash function */
4256 : 10568 : scratch.opcode = opcode;
4257 : :
4258 [ + + ]: 10568 : if (i == numCols - 1)
4259 : : {
4260 : : /*
4261 : : * The result for hashing the final column is stored in the
4262 : : * ExprState.
4263 : : */
4264 : 6239 : scratch.resvalue = &state->resvalue;
4265 : 6239 : scratch.resnull = &state->resnull;
4266 : : }
4267 : : else
4268 : : {
4269 : : Assert(iresult != NULL);
4270 : :
4271 : : /* intermediate values are stored in an intermediate result */
4272 : 4329 : scratch.resvalue = &iresult->value;
4273 : 4329 : scratch.resnull = &iresult->isnull;
4274 : : }
4275 : :
4276 : : /*
4277 : : * NEXT32 opcodes need to look at the intermediate result. We might
4278 : : * as well just set this for all ops. FIRSTs won't look at it.
4279 : : */
4280 : 10568 : scratch.d.hashdatum.iresult = iresult;
4281 : :
4282 : 10568 : scratch.d.hashdatum.finfo = finfo;
4283 : 10568 : scratch.d.hashdatum.fcinfo_data = fcinfo;
4284 : 10568 : scratch.d.hashdatum.fn_addr = finfo->fn_addr;
4285 : 10568 : scratch.d.hashdatum.jumpdone = -1;
4286 : :
4287 : 10568 : ExprEvalPushStep(state, &scratch);
4288 : :
4289 : : /* subsequent attnums must be combined with the previous */
4290 : 10568 : opcode = EEOP_HASHDATUM_NEXT32;
4291 : : }
4292 : :
4293 : 6263 : scratch.resvalue = NULL;
4294 : 6263 : scratch.resnull = NULL;
4295 : 6263 : scratch.opcode = EEOP_DONE_RETURN;
4296 : 6263 : ExprEvalPushStep(state, &scratch);
4297 : :
4298 : 6263 : ExecReadyExpr(state);
4299 : :
4300 : 6263 : return state;
4301 : : }
4302 : :
4303 : : /*
4304 : : * Build an ExprState that calls the given hash function(s) on the given
4305 : : * 'hash_exprs'. When multiple expressions are present, the hash values
4306 : : * returned by each hash function are combined to produce a single hash value.
4307 : : *
4308 : : * If any hash_expr yields NULL and the corresponding hash operator is strict,
4309 : : * the created ExprState will return NULL. (If the operator is not strict,
4310 : : * we treat NULL values as having a hash value of zero. The hash functions
4311 : : * themselves are always treated as strict.)
4312 : : *
4313 : : * desc: tuple descriptor for the to-be-hashed expressions
4314 : : * ops: TupleTableSlotOps for the TupleDesc
4315 : : * hashfunc_oids: Oid for each hash function to call, one for each 'hash_expr'
4316 : : * collations: collation to use when calling the hash function
4317 : : * hash_exprs: list of expressions to hash the value of
4318 : : * opstrict: strictness flag for each hash function's comparison operator
4319 : : * parent: PlanState node that the 'hash_exprs' will be evaluated at
4320 : : * init_value: Normally 0, but can be set to other values to seed the hash
4321 : : * with some other value. Using non-zero is slightly less efficient but can
4322 : : * be useful.
4323 : : */
4324 : : ExprState *
4325 : 60462 : ExecBuildHash32Expr(TupleDesc desc, const TupleTableSlotOps *ops,
4326 : : const Oid *hashfunc_oids, const List *collations,
4327 : : const List *hash_exprs, const bool *opstrict,
4328 : : PlanState *parent, uint32 init_value)
4329 : : {
4330 : 60462 : ExprState *state = makeNode(ExprState);
4331 : 60462 : ExprEvalStep scratch = {0};
4332 : 60462 : NullableDatum *iresult = NULL;
4333 : 60462 : List *adjust_jumps = NIL;
4334 : : ListCell *lc;
4335 : : ListCell *lc2;
4336 : : intptr_t strict_opcode;
4337 : : intptr_t opcode;
4338 : 60462 : int num_exprs = list_length(hash_exprs);
4339 : :
4340 : : Assert(num_exprs == list_length(collations));
4341 : :
4342 : 60462 : state->parent = parent;
4343 : :
4344 : : /* Insert setup steps as needed. */
4345 : 60462 : ExecCreateExprSetupSteps(state, unconstify(Node *, (const Node *) hash_exprs));
4346 : :
4347 : : /*
4348 : : * Make a place to store intermediate hash values between subsequent
4349 : : * hashing of individual expressions. We only need this if there is more
4350 : : * than one expression to hash or an initial value plus one expression.
4351 : : */
4352 [ + + ]: 60462 : if ((int64) num_exprs + (init_value != 0) > 1)
4353 : 4930 : iresult = palloc_object(NullableDatum);
4354 : :
4355 [ + - ]: 60462 : if (init_value == 0)
4356 : : {
4357 : : /*
4358 : : * No initial value, so we can assign the result of the hash function
4359 : : * for the first hash_expr without having to concern ourselves with
4360 : : * combining the result with any initial value.
4361 : : */
4362 : 60462 : strict_opcode = EEOP_HASHDATUM_FIRST_STRICT;
4363 : 60462 : opcode = EEOP_HASHDATUM_FIRST;
4364 : : }
4365 : : else
4366 : : {
4367 : : /*
4368 : : * Set up operation to set the initial value. Normally we store this
4369 : : * in the intermediate hash value location, but if there are no exprs
4370 : : * to hash, store it in the ExprState's result field.
4371 : : */
4372 : 0 : scratch.opcode = EEOP_HASHDATUM_SET_INITVAL;
4373 : 0 : scratch.d.hashdatum_initvalue.init_value = UInt32GetDatum(init_value);
4374 [ # # ]: 0 : scratch.resvalue = num_exprs > 0 ? &iresult->value : &state->resvalue;
4375 [ # # ]: 0 : scratch.resnull = num_exprs > 0 ? &iresult->isnull : &state->resnull;
4376 : :
4377 : 0 : ExprEvalPushStep(state, &scratch);
4378 : :
4379 : : /*
4380 : : * When using an initial value use the NEXT32/NEXT32_STRICT ops as the
4381 : : * FIRST/FIRST_STRICT ops would overwrite the stored initial value.
4382 : : */
4383 : 0 : strict_opcode = EEOP_HASHDATUM_NEXT32_STRICT;
4384 : 0 : opcode = EEOP_HASHDATUM_NEXT32;
4385 : : }
4386 : :
4387 [ + - + + : 125982 : forboth(lc, hash_exprs, lc2, collations)
+ - + + +
+ + - +
+ ]
4388 : : {
4389 : 65520 : Expr *expr = (Expr *) lfirst(lc);
4390 : : FmgrInfo *finfo;
4391 : : FunctionCallInfo fcinfo;
4392 : 65520 : int i = foreach_current_index(lc);
4393 : : Oid funcid;
4394 : 65520 : Oid inputcollid = lfirst_oid(lc2);
4395 : :
4396 : 65520 : funcid = hashfunc_oids[i];
4397 : :
4398 : : /* Allocate hash function lookup data. */
4399 : 65520 : finfo = palloc0_object(FmgrInfo);
4400 : 65520 : fcinfo = palloc0(SizeForFunctionCallInfo(1));
4401 : :
4402 : 65520 : fmgr_info(funcid, finfo);
4403 : :
4404 : : /*
4405 : : * Build the steps to evaluate the hash function's argument, placing
4406 : : * the value in the 0th argument of the hash func.
4407 : : */
4408 : 65520 : ExecInitExprRec(expr,
4409 : : state,
4410 : : &fcinfo->args[0].value,
4411 : : &fcinfo->args[0].isnull);
4412 : :
4413 [ + + ]: 65520 : if (i == num_exprs - 1)
4414 : : {
4415 : : /* the result for hashing the final expr is stored in the state */
4416 : 60462 : scratch.resvalue = &state->resvalue;
4417 : 60462 : scratch.resnull = &state->resnull;
4418 : : }
4419 : : else
4420 : : {
4421 : : Assert(iresult != NULL);
4422 : :
4423 : : /* intermediate values are stored in an intermediate result */
4424 : 5058 : scratch.resvalue = &iresult->value;
4425 : 5058 : scratch.resnull = &iresult->isnull;
4426 : : }
4427 : :
4428 : : /*
4429 : : * NEXT32 opcodes need to look at the intermediate result. We might
4430 : : * as well just set this for all ops. FIRSTs won't look at it.
4431 : : */
4432 : 65520 : scratch.d.hashdatum.iresult = iresult;
4433 : :
4434 : : /* Initialize function call parameter structure too */
4435 : 65520 : InitFunctionCallInfoData(*fcinfo, finfo, 1, inputcollid, NULL, NULL);
4436 : :
4437 : 65520 : scratch.d.hashdatum.finfo = finfo;
4438 : 65520 : scratch.d.hashdatum.fcinfo_data = fcinfo;
4439 : 65520 : scratch.d.hashdatum.fn_addr = finfo->fn_addr;
4440 : :
4441 [ + - ]: 65520 : scratch.opcode = opstrict[i] ? strict_opcode : opcode;
4442 : 65520 : scratch.d.hashdatum.jumpdone = -1;
4443 : :
4444 : 65520 : ExprEvalPushStep(state, &scratch);
4445 : 65520 : adjust_jumps = lappend_int(adjust_jumps, state->steps_len - 1);
4446 : :
4447 : : /*
4448 : : * For subsequent keys we must combine the hash value with the
4449 : : * previous hashes.
4450 : : */
4451 : 65520 : strict_opcode = EEOP_HASHDATUM_NEXT32_STRICT;
4452 : 65520 : opcode = EEOP_HASHDATUM_NEXT32;
4453 : : }
4454 : :
4455 : : /* adjust jump targets */
4456 [ + - + + : 125982 : foreach(lc, adjust_jumps)
+ + ]
4457 : : {
4458 : 65520 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4459 : :
4460 : : Assert(as->opcode == EEOP_HASHDATUM_FIRST ||
4461 : : as->opcode == EEOP_HASHDATUM_FIRST_STRICT ||
4462 : : as->opcode == EEOP_HASHDATUM_NEXT32 ||
4463 : : as->opcode == EEOP_HASHDATUM_NEXT32_STRICT);
4464 : : Assert(as->d.hashdatum.jumpdone == -1);
4465 : 65520 : as->d.hashdatum.jumpdone = state->steps_len;
4466 : : }
4467 : :
4468 : 60462 : scratch.resvalue = NULL;
4469 : 60462 : scratch.resnull = NULL;
4470 : 60462 : scratch.opcode = EEOP_DONE_RETURN;
4471 : 60462 : ExprEvalPushStep(state, &scratch);
4472 : :
4473 : 60462 : ExecReadyExpr(state);
4474 : :
4475 : 60462 : return state;
4476 : : }
4477 : :
4478 : : /*
4479 : : * Build equality expression that can be evaluated using ExecQual(), returning
4480 : : * true if the expression context's inner/outer tuple are NOT DISTINCT. I.e
4481 : : * two nulls match, a null and a not-null don't match.
4482 : : *
4483 : : * desc: tuple descriptor of the to-be-compared tuples
4484 : : * numCols: the number of attributes to be examined
4485 : : * keyColIdx: array of attribute column numbers
4486 : : * eqFunctions: array of function oids of the equality functions to use
4487 : : * parent: parent executor node
4488 : : */
4489 : : ExprState *
4490 : 14334 : ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
4491 : : const TupleTableSlotOps *lops, const TupleTableSlotOps *rops,
4492 : : int numCols,
4493 : : const AttrNumber *keyColIdx,
4494 : : const Oid *eqfunctions,
4495 : : const Oid *collations,
4496 : : PlanState *parent)
4497 : : {
4498 : 14334 : ExprState *state = makeNode(ExprState);
4499 : 14334 : ExprEvalStep scratch = {0};
4500 : 14334 : int maxatt = -1;
4501 : 14334 : List *adjust_jumps = NIL;
4502 : : ListCell *lc;
4503 : :
4504 : : /*
4505 : : * When no columns are actually compared, the result's always true. See
4506 : : * special case in ExecQual().
4507 : : */
4508 [ + + ]: 14334 : if (numCols == 0)
4509 : 24 : return NULL;
4510 : :
4511 : 14310 : state->expr = NULL;
4512 : 14310 : state->flags = EEO_FLAG_IS_QUAL;
4513 : 14310 : state->parent = parent;
4514 : :
4515 : 14310 : scratch.resvalue = &state->resvalue;
4516 : 14310 : scratch.resnull = &state->resnull;
4517 : :
4518 : : /* compute max needed attribute */
4519 [ + + ]: 38736 : for (int natt = 0; natt < numCols; natt++)
4520 : : {
4521 : 24426 : int attno = keyColIdx[natt];
4522 : :
4523 [ + + ]: 24426 : if (attno > maxatt)
4524 : 24155 : maxatt = attno;
4525 : : }
4526 : : Assert(maxatt >= 0);
4527 : :
4528 : : /* push deform steps */
4529 : 14310 : scratch.opcode = EEOP_INNER_FETCHSOME;
4530 : 14310 : scratch.d.fetch.last_var = maxatt;
4531 : 14310 : scratch.d.fetch.fixed = false;
4532 : 14310 : scratch.d.fetch.known_desc = ldesc;
4533 : 14310 : scratch.d.fetch.kind = lops;
4534 [ + + ]: 14310 : if (ExecComputeSlotInfo(state, &scratch))
4535 : 12396 : ExprEvalPushStep(state, &scratch);
4536 : :
4537 : 14310 : scratch.opcode = EEOP_OUTER_FETCHSOME;
4538 : 14310 : scratch.d.fetch.last_var = maxatt;
4539 : 14310 : scratch.d.fetch.fixed = false;
4540 : 14310 : scratch.d.fetch.known_desc = rdesc;
4541 : 14310 : scratch.d.fetch.kind = rops;
4542 [ + - ]: 14310 : if (ExecComputeSlotInfo(state, &scratch))
4543 : 14310 : ExprEvalPushStep(state, &scratch);
4544 : :
4545 : : /*
4546 : : * Start comparing at the last field (least significant sort key). That's
4547 : : * the most likely to be different if we are dealing with sorted input.
4548 : : */
4549 [ + + ]: 38736 : for (int natt = numCols; --natt >= 0;)
4550 : : {
4551 : 24426 : int attno = keyColIdx[natt];
4552 : 24426 : Form_pg_attribute latt = TupleDescAttr(ldesc, attno - 1);
4553 : 24426 : Form_pg_attribute ratt = TupleDescAttr(rdesc, attno - 1);
4554 : 24426 : Oid foid = eqfunctions[natt];
4555 : 24426 : Oid collid = collations[natt];
4556 : : FmgrInfo *finfo;
4557 : : FunctionCallInfo fcinfo;
4558 : : AclResult aclresult;
4559 : :
4560 : : /* Check permission to call function */
4561 : 24426 : aclresult = object_aclcheck(ProcedureRelationId, foid, GetUserId(), ACL_EXECUTE);
4562 [ - + ]: 24426 : if (aclresult != ACLCHECK_OK)
4563 : 0 : aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
4564 : :
4565 [ - + ]: 24426 : InvokeFunctionExecuteHook(foid);
4566 : :
4567 : : /* Set up the primary fmgr lookup information */
4568 : 24426 : finfo = palloc0_object(FmgrInfo);
4569 : 24426 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
4570 : 24426 : fmgr_info(foid, finfo);
4571 : 24426 : fmgr_info_set_expr(NULL, finfo);
4572 : 24426 : InitFunctionCallInfoData(*fcinfo, finfo, 2,
4573 : : collid, NULL, NULL);
4574 : :
4575 : : /* left arg */
4576 : 24426 : scratch.opcode = EEOP_INNER_VAR;
4577 : 24426 : scratch.d.var.attnum = attno - 1;
4578 : 24426 : scratch.d.var.vartype = latt->atttypid;
4579 : 24426 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
4580 : 24426 : scratch.resvalue = &fcinfo->args[0].value;
4581 : 24426 : scratch.resnull = &fcinfo->args[0].isnull;
4582 : 24426 : ExprEvalPushStep(state, &scratch);
4583 : :
4584 : : /* right arg */
4585 : 24426 : scratch.opcode = EEOP_OUTER_VAR;
4586 : 24426 : scratch.d.var.attnum = attno - 1;
4587 : 24426 : scratch.d.var.vartype = ratt->atttypid;
4588 : 24426 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
4589 : 24426 : scratch.resvalue = &fcinfo->args[1].value;
4590 : 24426 : scratch.resnull = &fcinfo->args[1].isnull;
4591 : 24426 : ExprEvalPushStep(state, &scratch);
4592 : :
4593 : : /* evaluate distinctness */
4594 : 24426 : scratch.opcode = EEOP_NOT_DISTINCT;
4595 : 24426 : scratch.d.func.finfo = finfo;
4596 : 24426 : scratch.d.func.fcinfo_data = fcinfo;
4597 : 24426 : scratch.d.func.fn_addr = finfo->fn_addr;
4598 : 24426 : scratch.d.func.nargs = 2;
4599 : 24426 : scratch.resvalue = &state->resvalue;
4600 : 24426 : scratch.resnull = &state->resnull;
4601 : 24426 : ExprEvalPushStep(state, &scratch);
4602 : :
4603 : : /* then emit EEOP_QUAL to detect if result is false (or null) */
4604 : 24426 : scratch.opcode = EEOP_QUAL;
4605 : 24426 : scratch.d.qualexpr.jumpdone = -1;
4606 : 24426 : scratch.resvalue = &state->resvalue;
4607 : 24426 : scratch.resnull = &state->resnull;
4608 : 24426 : ExprEvalPushStep(state, &scratch);
4609 : 24426 : adjust_jumps = lappend_int(adjust_jumps,
4610 : 24426 : state->steps_len - 1);
4611 : : }
4612 : :
4613 : : /* adjust jump targets */
4614 [ + - + + : 38736 : foreach(lc, adjust_jumps)
+ + ]
4615 : : {
4616 : 24426 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4617 : :
4618 : : Assert(as->opcode == EEOP_QUAL);
4619 : : Assert(as->d.qualexpr.jumpdone == -1);
4620 : 24426 : as->d.qualexpr.jumpdone = state->steps_len;
4621 : : }
4622 : :
4623 : 14310 : scratch.resvalue = NULL;
4624 : 14310 : scratch.resnull = NULL;
4625 : 14310 : scratch.opcode = EEOP_DONE_RETURN;
4626 : 14310 : ExprEvalPushStep(state, &scratch);
4627 : :
4628 : 14310 : ExecReadyExpr(state);
4629 : :
4630 : 14310 : return state;
4631 : : }
4632 : :
4633 : : /*
4634 : : * Build equality expression that can be evaluated using ExecQual(), returning
4635 : : * true if the expression context's inner/outer tuples are equal. Datums in
4636 : : * the inner/outer slots are assumed to be in the same order and quantity as
4637 : : * the 'eqfunctions' parameter. NULLs are treated as equal.
4638 : : *
4639 : : * desc: tuple descriptor of the to-be-compared tuples
4640 : : * lops: the slot ops for the inner tuple slots
4641 : : * rops: the slot ops for the outer tuple slots
4642 : : * eqFunctions: array of function oids of the equality functions to use
4643 : : * this must be the same length as the 'param_exprs' list.
4644 : : * collations: collation Oids to use for equality comparison. Must be the
4645 : : * same length as the 'param_exprs' list.
4646 : : * parent: parent executor node
4647 : : */
4648 : : ExprState *
4649 : 1164 : ExecBuildParamSetEqual(TupleDesc desc,
4650 : : const TupleTableSlotOps *lops,
4651 : : const TupleTableSlotOps *rops,
4652 : : const Oid *eqfunctions,
4653 : : const Oid *collations,
4654 : : const List *param_exprs,
4655 : : PlanState *parent)
4656 : : {
4657 : 1164 : ExprState *state = makeNode(ExprState);
4658 : 1164 : ExprEvalStep scratch = {0};
4659 : 1164 : int maxatt = list_length(param_exprs);
4660 : 1164 : List *adjust_jumps = NIL;
4661 : : ListCell *lc;
4662 : :
4663 : 1164 : state->expr = NULL;
4664 : 1164 : state->flags = EEO_FLAG_IS_QUAL;
4665 : 1164 : state->parent = parent;
4666 : :
4667 : 1164 : scratch.resvalue = &state->resvalue;
4668 : 1164 : scratch.resnull = &state->resnull;
4669 : :
4670 : : /* push deform steps */
4671 : 1164 : scratch.opcode = EEOP_INNER_FETCHSOME;
4672 : 1164 : scratch.d.fetch.last_var = maxatt;
4673 : 1164 : scratch.d.fetch.fixed = false;
4674 : 1164 : scratch.d.fetch.known_desc = desc;
4675 : 1164 : scratch.d.fetch.kind = lops;
4676 [ + - ]: 1164 : if (ExecComputeSlotInfo(state, &scratch))
4677 : 1164 : ExprEvalPushStep(state, &scratch);
4678 : :
4679 : 1164 : scratch.opcode = EEOP_OUTER_FETCHSOME;
4680 : 1164 : scratch.d.fetch.last_var = maxatt;
4681 : 1164 : scratch.d.fetch.fixed = false;
4682 : 1164 : scratch.d.fetch.known_desc = desc;
4683 : 1164 : scratch.d.fetch.kind = rops;
4684 [ - + ]: 1164 : if (ExecComputeSlotInfo(state, &scratch))
4685 : 0 : ExprEvalPushStep(state, &scratch);
4686 : :
4687 [ + + ]: 2336 : for (int attno = 0; attno < maxatt; attno++)
4688 : : {
4689 : 1172 : Form_pg_attribute att = TupleDescAttr(desc, attno);
4690 : 1172 : Oid foid = eqfunctions[attno];
4691 : 1172 : Oid collid = collations[attno];
4692 : : FmgrInfo *finfo;
4693 : : FunctionCallInfo fcinfo;
4694 : : AclResult aclresult;
4695 : :
4696 : : /* Check permission to call function */
4697 : 1172 : aclresult = object_aclcheck(ProcedureRelationId, foid, GetUserId(), ACL_EXECUTE);
4698 [ - + ]: 1172 : if (aclresult != ACLCHECK_OK)
4699 : 0 : aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
4700 : :
4701 [ - + ]: 1172 : InvokeFunctionExecuteHook(foid);
4702 : :
4703 : : /* Set up the primary fmgr lookup information */
4704 : 1172 : finfo = palloc0_object(FmgrInfo);
4705 : 1172 : fcinfo = palloc0(SizeForFunctionCallInfo(2));
4706 : 1172 : fmgr_info(foid, finfo);
4707 : 1172 : fmgr_info_set_expr(NULL, finfo);
4708 : 1172 : InitFunctionCallInfoData(*fcinfo, finfo, 2,
4709 : : collid, NULL, NULL);
4710 : :
4711 : : /* left arg */
4712 : 1172 : scratch.opcode = EEOP_INNER_VAR;
4713 : 1172 : scratch.d.var.attnum = attno;
4714 : 1172 : scratch.d.var.vartype = att->atttypid;
4715 : 1172 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
4716 : 1172 : scratch.resvalue = &fcinfo->args[0].value;
4717 : 1172 : scratch.resnull = &fcinfo->args[0].isnull;
4718 : 1172 : ExprEvalPushStep(state, &scratch);
4719 : :
4720 : : /* right arg */
4721 : 1172 : scratch.opcode = EEOP_OUTER_VAR;
4722 : 1172 : scratch.d.var.attnum = attno;
4723 : 1172 : scratch.d.var.vartype = att->atttypid;
4724 : 1172 : scratch.d.var.varreturningtype = VAR_RETURNING_DEFAULT;
4725 : 1172 : scratch.resvalue = &fcinfo->args[1].value;
4726 : 1172 : scratch.resnull = &fcinfo->args[1].isnull;
4727 : 1172 : ExprEvalPushStep(state, &scratch);
4728 : :
4729 : : /* evaluate distinctness */
4730 : 1172 : scratch.opcode = EEOP_NOT_DISTINCT;
4731 : 1172 : scratch.d.func.finfo = finfo;
4732 : 1172 : scratch.d.func.fcinfo_data = fcinfo;
4733 : 1172 : scratch.d.func.fn_addr = finfo->fn_addr;
4734 : 1172 : scratch.d.func.nargs = 2;
4735 : 1172 : scratch.resvalue = &state->resvalue;
4736 : 1172 : scratch.resnull = &state->resnull;
4737 : 1172 : ExprEvalPushStep(state, &scratch);
4738 : :
4739 : : /* then emit EEOP_QUAL to detect if result is false (or null) */
4740 : 1172 : scratch.opcode = EEOP_QUAL;
4741 : 1172 : scratch.d.qualexpr.jumpdone = -1;
4742 : 1172 : scratch.resvalue = &state->resvalue;
4743 : 1172 : scratch.resnull = &state->resnull;
4744 : 1172 : ExprEvalPushStep(state, &scratch);
4745 : 1172 : adjust_jumps = lappend_int(adjust_jumps,
4746 : 1172 : state->steps_len - 1);
4747 : : }
4748 : :
4749 : : /* adjust jump targets */
4750 [ + - + + : 2336 : foreach(lc, adjust_jumps)
+ + ]
4751 : : {
4752 : 1172 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4753 : :
4754 : : Assert(as->opcode == EEOP_QUAL);
4755 : : Assert(as->d.qualexpr.jumpdone == -1);
4756 : 1172 : as->d.qualexpr.jumpdone = state->steps_len;
4757 : : }
4758 : :
4759 : 1164 : scratch.resvalue = NULL;
4760 : 1164 : scratch.resnull = NULL;
4761 : 1164 : scratch.opcode = EEOP_DONE_RETURN;
4762 : 1164 : ExprEvalPushStep(state, &scratch);
4763 : :
4764 : 1164 : ExecReadyExpr(state);
4765 : :
4766 : 1164 : return state;
4767 : : }
4768 : :
4769 : : /*
4770 : : * Push steps to evaluate a JsonExpr and its various subsidiary expressions.
4771 : : */
4772 : : static void
4773 : 2194 : ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
4774 : : Datum *resv, bool *resnull,
4775 : : ExprEvalStep *scratch)
4776 : : {
4777 : 2194 : JsonExprState *jsestate = palloc0_object(JsonExprState);
4778 : : ListCell *argexprlc;
4779 : : ListCell *argnamelc;
4780 : 2194 : List *jumps_return_null = NIL;
4781 : 2194 : List *jumps_to_end = NIL;
4782 : : ListCell *lc;
4783 : : ErrorSaveContext *escontext;
4784 : 2194 : bool returning_domain =
4785 : 2194 : get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN;
4786 : :
4787 : : Assert(jsexpr->on_error != NULL);
4788 : :
4789 : 2194 : jsestate->jsexpr = jsexpr;
4790 : :
4791 : : /*
4792 : : * Evaluate formatted_expr storing the result into
4793 : : * jsestate->formatted_expr.
4794 : : */
4795 : 2194 : ExecInitExprRec((Expr *) jsexpr->formatted_expr, state,
4796 : : &jsestate->formatted_expr.value,
4797 : : &jsestate->formatted_expr.isnull);
4798 : :
4799 : : /* JUMP to return NULL if formatted_expr evaluates to NULL */
4800 : 2194 : jumps_return_null = lappend_int(jumps_return_null, state->steps_len);
4801 : 2194 : scratch->opcode = EEOP_JUMP_IF_NULL;
4802 : 2194 : scratch->resnull = &jsestate->formatted_expr.isnull;
4803 : 2194 : scratch->d.jump.jumpdone = -1; /* set below */
4804 : 2194 : ExprEvalPushStep(state, scratch);
4805 : :
4806 : : /*
4807 : : * Evaluate pathspec expression storing the result into
4808 : : * jsestate->pathspec.
4809 : : */
4810 : 2194 : ExecInitExprRec((Expr *) jsexpr->path_spec, state,
4811 : : &jsestate->pathspec.value,
4812 : : &jsestate->pathspec.isnull);
4813 : :
4814 : : /* JUMP to return NULL if path_spec evaluates to NULL */
4815 : 2194 : jumps_return_null = lappend_int(jumps_return_null, state->steps_len);
4816 : 2194 : scratch->opcode = EEOP_JUMP_IF_NULL;
4817 : 2194 : scratch->resnull = &jsestate->pathspec.isnull;
4818 : 2194 : scratch->d.jump.jumpdone = -1; /* set below */
4819 : 2194 : ExprEvalPushStep(state, scratch);
4820 : :
4821 : : /* Steps to compute PASSING args. */
4822 : 2194 : jsestate->args = NIL;
4823 [ + + + + : 3177 : forboth(argexprlc, jsexpr->passing_values,
+ + + + +
+ + - +
+ ]
4824 : : argnamelc, jsexpr->passing_names)
4825 : : {
4826 : 983 : Expr *argexpr = (Expr *) lfirst(argexprlc);
4827 : 983 : String *argname = lfirst_node(String, argnamelc);
4828 : 983 : JsonPathVariable *var = palloc_object(JsonPathVariable);
4829 : :
4830 : 983 : var->name = argname->sval;
4831 : 983 : var->namelen = strlen(var->name);
4832 : 983 : var->typid = exprType((Node *) argexpr);
4833 : 983 : var->typmod = exprTypmod((Node *) argexpr);
4834 : :
4835 : 983 : ExecInitExprRec(argexpr, state, &var->value, &var->isnull);
4836 : :
4837 : 983 : jsestate->args = lappend(jsestate->args, var);
4838 : : }
4839 : :
4840 : : /*
4841 : : * Adjust jump target addresses of JUMPs that we added above to point to
4842 : : * the EEOP_JSONEXPR_PATH step. That step itself handles the case where
4843 : : * either formatted_expr or pathspec is NULL by returning NULL, which
4844 : : * ensures that JsonExprState's empty/error flags are reset for every row
4845 : : * before the ON EMPTY / ON ERROR steps below look at them.
4846 : : */
4847 [ + - + + : 6582 : foreach(lc, jumps_return_null)
+ + ]
4848 : : {
4849 : 4388 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
4850 : :
4851 : 4388 : as->d.jump.jumpdone = state->steps_len;
4852 : : }
4853 : :
4854 : : /* Step for jsonpath evaluation; see ExecEvalJsonExprPath(). */
4855 : 2194 : scratch->opcode = EEOP_JSONEXPR_PATH;
4856 : 2194 : scratch->resvalue = resv;
4857 : 2194 : scratch->resnull = resnull;
4858 : 2194 : scratch->d.jsonexpr.jsestate = jsestate;
4859 : 2194 : ExprEvalPushStep(state, scratch);
4860 : :
4861 : 4388 : escontext = jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR ?
4862 [ + + ]: 2194 : &jsestate->escontext : NULL;
4863 : :
4864 : : /*
4865 : : * To handle coercion errors softly, use the following ErrorSaveContext to
4866 : : * pass to ExecInitExprRec() when initializing the coercion expressions
4867 : : * and in the EEOP_JSONEXPR_COERCION step.
4868 : : */
4869 : 2194 : jsestate->escontext.type = T_ErrorSaveContext;
4870 : :
4871 : : /*
4872 : : * Steps to coerce the result value computed by EEOP_JSONEXPR_PATH or the
4873 : : * NULL that it returns on NULL input.
4874 : : */
4875 : 2194 : jsestate->jump_eval_coercion = -1;
4876 [ + + ]: 2194 : if (jsexpr->use_json_coercion)
4877 : : {
4878 : 723 : jsestate->jump_eval_coercion = state->steps_len;
4879 : :
4880 : 723 : ExecInitJsonCoercion(state, jsexpr->returning, escontext,
4881 : 723 : jsexpr->omit_quotes,
4882 : 723 : jsexpr->op == JSON_EXISTS_OP,
4883 : : resv, resnull);
4884 : : }
4885 [ + + ]: 1471 : else if (jsexpr->use_io_coercion)
4886 : : {
4887 : : /*
4888 : : * Here we only need to initialize the FunctionCallInfo for the target
4889 : : * type's input function, which is called by ExecEvalJsonExprPath()
4890 : : * itself, so no additional step is necessary.
4891 : : */
4892 : : Oid typinput;
4893 : : Oid typioparam;
4894 : : FmgrInfo *finfo;
4895 : : FunctionCallInfo fcinfo;
4896 : :
4897 : 801 : getTypeInputInfo(jsexpr->returning->typid, &typinput, &typioparam);
4898 : 801 : finfo = palloc0_object(FmgrInfo);
4899 : 801 : fcinfo = palloc0(SizeForFunctionCallInfo(3));
4900 : 801 : fmgr_info(typinput, finfo);
4901 : 801 : fmgr_info_set_expr((Node *) jsexpr->returning, finfo);
4902 : 801 : InitFunctionCallInfoData(*fcinfo, finfo, 3, InvalidOid, NULL, NULL);
4903 : :
4904 : : /*
4905 : : * We can preload the second and third arguments for the input
4906 : : * function, since they're constants.
4907 : : */
4908 : 801 : fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
4909 : 801 : fcinfo->args[1].isnull = false;
4910 : 801 : fcinfo->args[2].value = Int32GetDatum(jsexpr->returning->typmod);
4911 : 801 : fcinfo->args[2].isnull = false;
4912 : 801 : fcinfo->context = (Node *) escontext;
4913 : :
4914 : 801 : jsestate->input_fcinfo = fcinfo;
4915 : : }
4916 : :
4917 : : /*
4918 : : * Add a special step, if needed, to check if the coercion evaluation ran
4919 : : * into an error but was not thrown because the ON ERROR behavior is not
4920 : : * ERROR. It will set jsestate->error if an error did occur.
4921 : : */
4922 [ + + + + ]: 2194 : if (jsestate->jump_eval_coercion >= 0 && escontext != NULL)
4923 : : {
4924 : 574 : scratch->opcode = EEOP_JSONEXPR_COERCION_FINISH;
4925 : 574 : scratch->d.jsonexpr.jsestate = jsestate;
4926 : 574 : ExprEvalPushStep(state, scratch);
4927 : : }
4928 : :
4929 : 2194 : jsestate->jump_empty = jsestate->jump_error = -1;
4930 : :
4931 : : /*
4932 : : * Step to check jsestate->error and return the ON ERROR expression if
4933 : : * there is one. This handles both the errors that occur during jsonpath
4934 : : * evaluation in EEOP_JSONEXPR_PATH and subsequent coercion evaluation.
4935 : : *
4936 : : * Speed up common cases by avoiding extra steps for a NULL-valued ON
4937 : : * ERROR expression unless RETURNING a domain type, where constraints must
4938 : : * be checked. ExecEvalJsonExprPath() already returns NULL on error,
4939 : : * making additional steps unnecessary in typical scenarios. Note that the
4940 : : * default ON ERROR behavior for JSON_VALUE() and JSON_QUERY() is to
4941 : : * return NULL.
4942 : : */
4943 [ + + ]: 2194 : if (jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR &&
4944 [ + + ]: 1903 : (!(IsA(jsexpr->on_error->expr, Const) &&
4945 [ + + + + ]: 1843 : ((Const *) jsexpr->on_error->expr)->constisnull) ||
4946 : : returning_domain))
4947 : : {
4948 : : ErrorSaveContext *saved_escontext;
4949 : :
4950 : 497 : jsestate->jump_error = state->steps_len;
4951 : :
4952 : : /* JUMP to end if false, that is, skip the ON ERROR expression. */
4953 : 497 : jumps_to_end = lappend_int(jumps_to_end, state->steps_len);
4954 : 497 : scratch->opcode = EEOP_JUMP_IF_NOT_TRUE;
4955 : 497 : scratch->resvalue = &jsestate->error.value;
4956 : 497 : scratch->resnull = &jsestate->error.isnull;
4957 : 497 : scratch->d.jump.jumpdone = -1; /* set below */
4958 : 497 : ExprEvalPushStep(state, scratch);
4959 : :
4960 : : /*
4961 : : * Steps to evaluate the ON ERROR expression; handle errors softly to
4962 : : * rethrow them in COERCION_FINISH step that will be added later.
4963 : : */
4964 : 497 : saved_escontext = state->escontext;
4965 : 497 : state->escontext = escontext;
4966 : 497 : ExecInitExprRec((Expr *) jsexpr->on_error->expr,
4967 : : state, resv, resnull);
4968 : 497 : state->escontext = saved_escontext;
4969 : :
4970 : : /* Step to coerce the ON ERROR expression if needed */
4971 [ + + ]: 497 : if (jsexpr->on_error->coerce)
4972 : 124 : ExecInitJsonCoercion(state, jsexpr->returning, escontext,
4973 : 124 : jsexpr->omit_quotes, false,
4974 : : resv, resnull);
4975 : :
4976 : : /*
4977 : : * Add a COERCION_FINISH step to throw any error saved while
4978 : : * evaluating the ON ERROR expression or coercing its result.
4979 : : */
4980 : 497 : scratch->opcode = EEOP_JSONEXPR_COERCION_FINISH;
4981 : 497 : scratch->resvalue = resv;
4982 : 497 : scratch->resnull = resnull;
4983 : 497 : scratch->d.jsonexpr.jsestate = jsestate;
4984 : 497 : ExprEvalPushStep(state, scratch);
4985 : :
4986 : : /* JUMP to end to skip the ON EMPTY steps added below. */
4987 : 497 : jumps_to_end = lappend_int(jumps_to_end, state->steps_len);
4988 : 497 : scratch->opcode = EEOP_JUMP;
4989 : 497 : scratch->d.jump.jumpdone = -1;
4990 : 497 : ExprEvalPushStep(state, scratch);
4991 : : }
4992 : :
4993 : : /*
4994 : : * Step to check jsestate->empty and return the ON EMPTY expression if
4995 : : * there is one.
4996 : : *
4997 : : * See the comment above for details on the optimization for NULL-valued
4998 : : * expressions.
4999 : : */
5000 [ + + ]: 2194 : if (jsexpr->on_empty != NULL &&
5001 [ + + ]: 1934 : jsexpr->on_empty->btype != JSON_BEHAVIOR_ERROR &&
5002 [ + + ]: 1886 : (!(IsA(jsexpr->on_empty->expr, Const) &&
5003 [ + + + + ]: 1834 : ((Const *) jsexpr->on_empty->expr)->constisnull) ||
5004 : : returning_domain))
5005 : : {
5006 : : ErrorSaveContext *saved_escontext;
5007 : :
5008 : 354 : jsestate->jump_empty = state->steps_len;
5009 : :
5010 : : /* JUMP to end if false, that is, skip the ON EMPTY expression. */
5011 : 354 : jumps_to_end = lappend_int(jumps_to_end, state->steps_len);
5012 : 354 : scratch->opcode = EEOP_JUMP_IF_NOT_TRUE;
5013 : 354 : scratch->resvalue = &jsestate->empty.value;
5014 : 354 : scratch->resnull = &jsestate->empty.isnull;
5015 : 354 : scratch->d.jump.jumpdone = -1; /* set below */
5016 : 354 : ExprEvalPushStep(state, scratch);
5017 : :
5018 : : /*
5019 : : * Steps to evaluate the ON EMPTY expression. Unless ON ERROR is
5020 : : * ERROR, errors are handled softly, to be rethrown by the
5021 : : * COERCION_FINISH step added below; otherwise they are thrown
5022 : : * directly.
5023 : : */
5024 : 354 : saved_escontext = state->escontext;
5025 : 354 : state->escontext = escontext;
5026 : 354 : ExecInitExprRec((Expr *) jsexpr->on_empty->expr,
5027 : : state, resv, resnull);
5028 : 354 : state->escontext = saved_escontext;
5029 : :
5030 : : /* Step to coerce the ON EMPTY expression if needed */
5031 [ + + ]: 354 : if (jsexpr->on_empty->coerce)
5032 : 136 : ExecInitJsonCoercion(state, jsexpr->returning, escontext,
5033 : 136 : jsexpr->omit_quotes, false,
5034 : : resv, resnull);
5035 : :
5036 : : /*
5037 : : * If the ON EMPTY expression was evaluated softly, add a
5038 : : * COERCION_FINISH step to throw any error saved while evaluating it
5039 : : * or coercing its result.
5040 : : */
5041 [ + + ]: 354 : if (escontext)
5042 : : {
5043 : 285 : scratch->opcode = EEOP_JSONEXPR_COERCION_FINISH;
5044 : 285 : scratch->resvalue = resv;
5045 : 285 : scratch->resnull = resnull;
5046 : 285 : scratch->d.jsonexpr.jsestate = jsestate;
5047 : 285 : ExprEvalPushStep(state, scratch);
5048 : : }
5049 : : }
5050 : :
5051 [ + + + + : 3542 : foreach(lc, jumps_to_end)
+ + ]
5052 : : {
5053 : 1348 : ExprEvalStep *as = &state->steps[lfirst_int(lc)];
5054 : :
5055 : 1348 : as->d.jump.jumpdone = state->steps_len;
5056 : : }
5057 : :
5058 : 2194 : jsestate->jump_end = state->steps_len;
5059 : 2194 : }
5060 : :
5061 : : /*
5062 : : * Initialize a EEOP_JSONEXPR_COERCION step to coerce the value given in resv
5063 : : * to the given RETURNING type.
5064 : : */
5065 : : static void
5066 : 983 : ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
5067 : : ErrorSaveContext *escontext, bool omit_quotes,
5068 : : bool exists_coerce,
5069 : : Datum *resv, bool *resnull)
5070 : : {
5071 : 983 : ExprEvalStep scratch = {0};
5072 : :
5073 : : /* For json_populate_type() */
5074 : 983 : scratch.opcode = EEOP_JSONEXPR_COERCION;
5075 : 983 : scratch.resvalue = resv;
5076 : 983 : scratch.resnull = resnull;
5077 : 983 : scratch.d.jsonexpr_coercion.targettype = returning->typid;
5078 : 983 : scratch.d.jsonexpr_coercion.targettypmod = returning->typmod;
5079 : 983 : scratch.d.jsonexpr_coercion.json_coercion_cache = NULL;
5080 : 983 : scratch.d.jsonexpr_coercion.escontext = escontext;
5081 : 983 : scratch.d.jsonexpr_coercion.omit_quotes = omit_quotes;
5082 : 983 : scratch.d.jsonexpr_coercion.exists_coerce = exists_coerce;
5083 [ + + + + ]: 1076 : scratch.d.jsonexpr_coercion.exists_cast_to_int = exists_coerce &&
5084 : 93 : getBaseType(returning->typid) == INT4OID;
5085 [ + + + + ]: 1076 : scratch.d.jsonexpr_coercion.exists_check_domain = exists_coerce &&
5086 : 93 : DomainHasConstraints(returning->typid, NULL);
5087 : 983 : ExprEvalPushStep(state, &scratch);
5088 : 983 : }
|