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