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