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-2020, 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_type.h"
36 #include "executor/execExpr.h"
37 #include "executor/nodeSubplan.h"
38 #include "funcapi.h"
39 #include "jit/jit.h"
40 #include "miscadmin.h"
41 #include "nodes/makefuncs.h"
42 #include "nodes/nodeFuncs.h"
43 #include "optimizer/optimizer.h"
44 #include "pgstat.h"
45 #include "utils/acl.h"
46 #include "utils/array.h"
47 #include "utils/builtins.h"
48 #include "utils/datum.h"
49 #include "utils/lsyscache.h"
50 #include "utils/typcache.h"
51
52
53 typedef struct LastAttnumInfo
54 {
55 AttrNumber last_inner;
56 AttrNumber last_outer;
57 AttrNumber last_scan;
58 } LastAttnumInfo;
59
60 static void ExecReadyExpr(ExprState *state);
61 static void ExecInitExprRec(Expr *node, ExprState *state,
62 Datum *resv, bool *resnull);
63 static void ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args,
64 Oid funcid, Oid inputcollid,
65 ExprState *state);
66 static void ExecInitExprSlots(ExprState *state, Node *node);
67 static void ExecPushExprSlots(ExprState *state, LastAttnumInfo *info);
68 static bool get_last_attnums_walker(Node *node, LastAttnumInfo *info);
69 static bool ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op);
70 static void ExecInitWholeRowVar(ExprEvalStep *scratch, Var *variable,
71 ExprState *state);
72 static void ExecInitSubscriptingRef(ExprEvalStep *scratch,
73 SubscriptingRef *sbsref,
74 ExprState *state,
75 Datum *resv, bool *resnull);
76 static bool isAssignmentIndirectionExpr(Expr *expr);
77 static void ExecInitCoerceToDomain(ExprEvalStep *scratch, CoerceToDomain *ctest,
78 ExprState *state,
79 Datum *resv, bool *resnull);
80 static void ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
81 ExprEvalStep *scratch,
82 FunctionCallInfo fcinfo, AggStatePerTrans pertrans,
83 int transno, int setno, int setoff, bool ishash,
84 bool nullcheck);
85
86
87 /*
88 * ExecInitExpr: prepare an expression tree for execution
89 *
90 * This function builds and returns an ExprState implementing the given
91 * Expr node tree. The return ExprState can then be handed to ExecEvalExpr
92 * for execution. Because the Expr tree itself is read-only as far as
93 * ExecInitExpr and ExecEvalExpr are concerned, several different executions
94 * of the same plan tree can occur concurrently. (But note that an ExprState
95 * does mutate at runtime, so it can't be re-used concurrently.)
96 *
97 * This must be called in a memory context that will last as long as repeated
98 * executions of the expression are needed. Typically the context will be
99 * the same as the per-query context of the associated ExprContext.
100 *
101 * Any Aggref, WindowFunc, or SubPlan nodes found in the tree are added to
102 * the lists of such nodes held by the parent PlanState (or more accurately,
103 * the AggrefExprState etc. nodes created for them are added).
104 *
105 * Note: there is no ExecEndExpr function; we assume that any resource
106 * cleanup needed will be handled by just releasing the memory context
107 * in which the state tree is built. Functions that require additional
108 * cleanup work can register a shutdown callback in the ExprContext.
109 *
110 * 'node' is the root of the expression tree to compile.
111 * 'parent' is the PlanState node that owns the expression.
112 *
113 * 'parent' may be NULL if we are preparing an expression that is not
114 * associated with a plan tree. (If so, it can't have aggs or subplans.)
115 * Such cases should usually come through ExecPrepareExpr, not directly here.
116 *
117 * Also, if 'node' is NULL, we just return NULL. This is convenient for some
118 * callers that may or may not have an expression that needs to be compiled.
119 * Note that a NULL ExprState pointer *cannot* be handed to ExecEvalExpr,
120 * although ExecQual and ExecCheck will accept one (and treat it as "true").
121 */
122 ExprState *
ExecInitExpr(Expr * node,PlanState * parent)123 ExecInitExpr(Expr *node, PlanState *parent)
124 {
125 ExprState *state;
126 ExprEvalStep scratch = {0};
127
128 /* Special case: NULL expression produces a NULL ExprState pointer */
129 if (node == NULL)
130 return NULL;
131
132 /* Initialize ExprState with empty step list */
133 state = makeNode(ExprState);
134 state->expr = node;
135 state->parent = parent;
136 state->ext_params = NULL;
137
138 /* Insert EEOP_*_FETCHSOME steps as needed */
139 ExecInitExprSlots(state, (Node *) node);
140
141 /* Compile the expression proper */
142 ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
143
144 /* Finally, append a DONE step */
145 scratch.opcode = EEOP_DONE;
146 ExprEvalPushStep(state, &scratch);
147
148 ExecReadyExpr(state);
149
150 return state;
151 }
152
153 /*
154 * ExecInitExprWithParams: prepare a standalone expression tree for execution
155 *
156 * This is the same as ExecInitExpr, except that there is no parent PlanState,
157 * and instead we may have a ParamListInfo describing PARAM_EXTERN Params.
158 */
159 ExprState *
ExecInitExprWithParams(Expr * node,ParamListInfo ext_params)160 ExecInitExprWithParams(Expr *node, ParamListInfo ext_params)
161 {
162 ExprState *state;
163 ExprEvalStep scratch = {0};
164
165 /* Special case: NULL expression produces a NULL ExprState pointer */
166 if (node == NULL)
167 return NULL;
168
169 /* Initialize ExprState with empty step list */
170 state = makeNode(ExprState);
171 state->expr = node;
172 state->parent = NULL;
173 state->ext_params = ext_params;
174
175 /* Insert EEOP_*_FETCHSOME steps as needed */
176 ExecInitExprSlots(state, (Node *) node);
177
178 /* Compile the expression proper */
179 ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
180
181 /* Finally, append a DONE step */
182 scratch.opcode = EEOP_DONE;
183 ExprEvalPushStep(state, &scratch);
184
185 ExecReadyExpr(state);
186
187 return state;
188 }
189
190 /*
191 * ExecInitQual: prepare a qual for execution by ExecQual
192 *
193 * Prepares for the evaluation of a conjunctive boolean expression (qual list
194 * with implicit AND semantics) that returns true if none of the
195 * subexpressions are false.
196 *
197 * We must return true if the list is empty. Since that's a very common case,
198 * we optimize it a bit further by translating to a NULL ExprState pointer
199 * rather than setting up an ExprState that computes constant TRUE. (Some
200 * especially hot-spot callers of ExecQual detect this and avoid calling
201 * ExecQual at all.)
202 *
203 * If any of the subexpressions yield NULL, then the result of the conjunction
204 * is false. This makes ExecQual primarily useful for evaluating WHERE
205 * clauses, since SQL specifies that tuples with null WHERE results do not
206 * get selected.
207 */
208 ExprState *
ExecInitQual(List * qual,PlanState * parent)209 ExecInitQual(List *qual, PlanState *parent)
210 {
211 ExprState *state;
212 ExprEvalStep scratch = {0};
213 List *adjust_jumps = NIL;
214 ListCell *lc;
215
216 /* short-circuit (here and in ExecQual) for empty restriction list */
217 if (qual == NIL)
218 return NULL;
219
220 Assert(IsA(qual, List));
221
222 state = makeNode(ExprState);
223 state->expr = (Expr *) qual;
224 state->parent = parent;
225 state->ext_params = NULL;
226
227 /* mark expression as to be used with ExecQual() */
228 state->flags = EEO_FLAG_IS_QUAL;
229
230 /* Insert EEOP_*_FETCHSOME steps as needed */
231 ExecInitExprSlots(state, (Node *) qual);
232
233 /*
234 * ExecQual() needs to return false for an expression returning NULL. That
235 * allows us to short-circuit the evaluation the first time a NULL is
236 * encountered. As qual evaluation is a hot-path this warrants using a
237 * special opcode for qual evaluation that's simpler than BOOL_AND (which
238 * has more complex NULL handling).
239 */
240 scratch.opcode = EEOP_QUAL;
241
242 /*
243 * We can use ExprState's resvalue/resnull as target for each qual expr.
244 */
245 scratch.resvalue = &state->resvalue;
246 scratch.resnull = &state->resnull;
247
248 foreach(lc, qual)
249 {
250 Expr *node = (Expr *) lfirst(lc);
251
252 /* first evaluate expression */
253 ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
254
255 /* then emit EEOP_QUAL to detect if it's false (or null) */
256 scratch.d.qualexpr.jumpdone = -1;
257 ExprEvalPushStep(state, &scratch);
258 adjust_jumps = lappend_int(adjust_jumps,
259 state->steps_len - 1);
260 }
261
262 /* adjust jump targets */
263 foreach(lc, adjust_jumps)
264 {
265 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
266
267 Assert(as->opcode == EEOP_QUAL);
268 Assert(as->d.qualexpr.jumpdone == -1);
269 as->d.qualexpr.jumpdone = state->steps_len;
270 }
271
272 /*
273 * At the end, we don't need to do anything more. The last qual expr must
274 * have yielded TRUE, and since its result is stored in the desired output
275 * location, we're done.
276 */
277 scratch.opcode = EEOP_DONE;
278 ExprEvalPushStep(state, &scratch);
279
280 ExecReadyExpr(state);
281
282 return state;
283 }
284
285 /*
286 * ExecInitCheck: prepare a check constraint for execution by ExecCheck
287 *
288 * This is much like ExecInitQual/ExecQual, except that a null result from
289 * the conjunction is treated as TRUE. This behavior is appropriate for
290 * evaluating CHECK constraints, since SQL specifies that NULL constraint
291 * conditions are not failures.
292 *
293 * Note that like ExecInitQual, this expects input in implicit-AND format.
294 * Users of ExecCheck that have expressions in normal explicit-AND format
295 * can just apply ExecInitExpr to produce suitable input for ExecCheck.
296 */
297 ExprState *
ExecInitCheck(List * qual,PlanState * parent)298 ExecInitCheck(List *qual, PlanState *parent)
299 {
300 /* short-circuit (here and in ExecCheck) for empty restriction list */
301 if (qual == NIL)
302 return NULL;
303
304 Assert(IsA(qual, List));
305
306 /*
307 * Just convert the implicit-AND list to an explicit AND (if there's more
308 * than one entry), and compile normally. Unlike ExecQual, we can't
309 * short-circuit on NULL results, so the regular AND behavior is needed.
310 */
311 return ExecInitExpr(make_ands_explicit(qual), parent);
312 }
313
314 /*
315 * Call ExecInitExpr() on a list of expressions, return a list of ExprStates.
316 */
317 List *
ExecInitExprList(List * nodes,PlanState * parent)318 ExecInitExprList(List *nodes, PlanState *parent)
319 {
320 List *result = NIL;
321 ListCell *lc;
322
323 foreach(lc, nodes)
324 {
325 Expr *e = lfirst(lc);
326
327 result = lappend(result, ExecInitExpr(e, parent));
328 }
329
330 return result;
331 }
332
333 /*
334 * ExecBuildProjectionInfo
335 *
336 * Build a ProjectionInfo node for evaluating the given tlist in the given
337 * econtext, and storing the result into the tuple slot. (Caller must have
338 * ensured that tuple slot has a descriptor matching the tlist!)
339 *
340 * inputDesc can be NULL, but if it is not, we check to see whether simple
341 * Vars in the tlist match the descriptor. It is important to provide
342 * inputDesc for relation-scan plan nodes, as a cross check that the relation
343 * hasn't been changed since the plan was made. At higher levels of a plan,
344 * there is no need to recheck.
345 *
346 * This is implemented by internally building an ExprState that performs the
347 * whole projection in one go.
348 *
349 * Caution: before PG v10, the targetList was a list of ExprStates; now it
350 * should be the planner-created targetlist, since we do the compilation here.
351 */
352 ProjectionInfo *
ExecBuildProjectionInfo(List * targetList,ExprContext * econtext,TupleTableSlot * slot,PlanState * parent,TupleDesc inputDesc)353 ExecBuildProjectionInfo(List *targetList,
354 ExprContext *econtext,
355 TupleTableSlot *slot,
356 PlanState *parent,
357 TupleDesc inputDesc)
358 {
359 return ExecBuildProjectionInfoExt(targetList,
360 econtext,
361 slot,
362 true,
363 parent,
364 inputDesc);
365 }
366
367 /*
368 * ExecBuildProjectionInfoExt
369 *
370 * As above, with one additional option.
371 *
372 * If assignJunkEntries is true (the usual case), resjunk entries in the tlist
373 * are not handled specially: they are evaluated and assigned to the proper
374 * column of the result slot. If assignJunkEntries is false, resjunk entries
375 * are evaluated, but their result is discarded without assignment.
376 */
377 ProjectionInfo *
ExecBuildProjectionInfoExt(List * targetList,ExprContext * econtext,TupleTableSlot * slot,bool assignJunkEntries,PlanState * parent,TupleDesc inputDesc)378 ExecBuildProjectionInfoExt(List *targetList,
379 ExprContext *econtext,
380 TupleTableSlot *slot,
381 bool assignJunkEntries,
382 PlanState *parent,
383 TupleDesc inputDesc)
384 {
385 ProjectionInfo *projInfo = makeNode(ProjectionInfo);
386 ExprState *state;
387 ExprEvalStep scratch = {0};
388 ListCell *lc;
389
390 projInfo->pi_exprContext = econtext;
391 /* We embed ExprState into ProjectionInfo instead of doing extra palloc */
392 projInfo->pi_state.tag = T_ExprState;
393 state = &projInfo->pi_state;
394 state->expr = (Expr *) targetList;
395 state->parent = parent;
396 state->ext_params = NULL;
397
398 state->resultslot = slot;
399
400 /* Insert EEOP_*_FETCHSOME steps as needed */
401 ExecInitExprSlots(state, (Node *) targetList);
402
403 /* Now compile each tlist column */
404 foreach(lc, targetList)
405 {
406 TargetEntry *tle = lfirst_node(TargetEntry, lc);
407 Var *variable = NULL;
408 AttrNumber attnum = 0;
409 bool isSafeVar = false;
410
411 /*
412 * If tlist expression is a safe non-system Var, use the fast-path
413 * ASSIGN_*_VAR opcodes. "Safe" means that we don't need to apply
414 * CheckVarSlotCompatibility() during plan startup. If a source slot
415 * was provided, we make the equivalent tests here; if a slot was not
416 * provided, we assume that no check is needed because we're dealing
417 * with a non-relation-scan-level expression.
418 */
419 if (tle->expr != NULL &&
420 IsA(tle->expr, Var) &&
421 ((Var *) tle->expr)->varattno > 0 &&
422 (assignJunkEntries || !tle->resjunk))
423 {
424 /* Non-system Var, but how safe is it? */
425 variable = (Var *) tle->expr;
426 attnum = variable->varattno;
427
428 if (inputDesc == NULL)
429 isSafeVar = true; /* can't check, just assume OK */
430 else if (attnum <= inputDesc->natts)
431 {
432 Form_pg_attribute attr = TupleDescAttr(inputDesc, attnum - 1);
433
434 /*
435 * If user attribute is dropped or has a type mismatch, don't
436 * use ASSIGN_*_VAR. Instead let the normal expression
437 * machinery handle it (which'll possibly error out).
438 */
439 if (!attr->attisdropped && variable->vartype == attr->atttypid)
440 {
441 isSafeVar = true;
442 }
443 }
444 }
445
446 if (isSafeVar)
447 {
448 /* Fast-path: just generate an EEOP_ASSIGN_*_VAR step */
449 switch (variable->varno)
450 {
451 case INNER_VAR:
452 /* get the tuple from the inner node */
453 scratch.opcode = EEOP_ASSIGN_INNER_VAR;
454 break;
455
456 case OUTER_VAR:
457 /* get the tuple from the outer node */
458 scratch.opcode = EEOP_ASSIGN_OUTER_VAR;
459 break;
460
461 /* INDEX_VAR is handled by default case */
462
463 default:
464 /* get the tuple from the relation being scanned */
465 scratch.opcode = EEOP_ASSIGN_SCAN_VAR;
466 break;
467 }
468
469 scratch.d.assign_var.attnum = attnum - 1;
470 scratch.d.assign_var.resultnum = tle->resno - 1;
471 ExprEvalPushStep(state, &scratch);
472 }
473 else
474 {
475 /*
476 * Otherwise, compile the column expression normally.
477 *
478 * We can't tell the expression to evaluate directly into the
479 * result slot, as the result slot (and the exprstate for that
480 * matter) can change between executions. We instead evaluate
481 * into the ExprState's resvalue/resnull and then move.
482 */
483 ExecInitExprRec(tle->expr, state,
484 &state->resvalue, &state->resnull);
485
486 /* This makes it easy to discard resjunk results when told to. */
487 if (!assignJunkEntries && tle->resjunk)
488 continue;
489
490 /*
491 * Column might be referenced multiple times in upper nodes, so
492 * force value to R/O - but only if it could be an expanded datum.
493 */
494 if (get_typlen(exprType((Node *) tle->expr)) == -1)
495 scratch.opcode = EEOP_ASSIGN_TMP_MAKE_RO;
496 else
497 scratch.opcode = EEOP_ASSIGN_TMP;
498 scratch.d.assign_tmp.resultnum = tle->resno - 1;
499 ExprEvalPushStep(state, &scratch);
500 }
501 }
502
503 scratch.opcode = EEOP_DONE;
504 ExprEvalPushStep(state, &scratch);
505
506 ExecReadyExpr(state);
507
508 return projInfo;
509 }
510
511 /*
512 * ExecPrepareExpr --- initialize for expression execution outside a normal
513 * Plan tree context.
514 *
515 * This differs from ExecInitExpr in that we don't assume the caller is
516 * already running in the EState's per-query context. Also, we run the
517 * passed expression tree through expression_planner() to prepare it for
518 * execution. (In ordinary Plan trees the regular planning process will have
519 * made the appropriate transformations on expressions, but for standalone
520 * expressions this won't have happened.)
521 */
522 ExprState *
ExecPrepareExpr(Expr * node,EState * estate)523 ExecPrepareExpr(Expr *node, EState *estate)
524 {
525 ExprState *result;
526 MemoryContext oldcontext;
527
528 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
529
530 node = expression_planner(node);
531
532 result = ExecInitExpr(node, NULL);
533
534 MemoryContextSwitchTo(oldcontext);
535
536 return result;
537 }
538
539 /*
540 * ExecPrepareQual --- initialize for qual execution outside a normal
541 * Plan tree context.
542 *
543 * This differs from ExecInitQual in that we don't assume the caller is
544 * already running in the EState's per-query context. Also, we run the
545 * passed expression tree through expression_planner() to prepare it for
546 * execution. (In ordinary Plan trees the regular planning process will have
547 * made the appropriate transformations on expressions, but for standalone
548 * expressions this won't have happened.)
549 */
550 ExprState *
ExecPrepareQual(List * qual,EState * estate)551 ExecPrepareQual(List *qual, EState *estate)
552 {
553 ExprState *result;
554 MemoryContext oldcontext;
555
556 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
557
558 qual = (List *) expression_planner((Expr *) qual);
559
560 result = ExecInitQual(qual, NULL);
561
562 MemoryContextSwitchTo(oldcontext);
563
564 return result;
565 }
566
567 /*
568 * ExecPrepareCheck -- initialize check constraint for execution outside a
569 * normal Plan tree context.
570 *
571 * See ExecPrepareExpr() and ExecInitCheck() for details.
572 */
573 ExprState *
ExecPrepareCheck(List * qual,EState * estate)574 ExecPrepareCheck(List *qual, EState *estate)
575 {
576 ExprState *result;
577 MemoryContext oldcontext;
578
579 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
580
581 qual = (List *) expression_planner((Expr *) qual);
582
583 result = ExecInitCheck(qual, NULL);
584
585 MemoryContextSwitchTo(oldcontext);
586
587 return result;
588 }
589
590 /*
591 * Call ExecPrepareExpr() on each member of a list of Exprs, and return
592 * a list of ExprStates.
593 *
594 * See ExecPrepareExpr() for details.
595 */
596 List *
ExecPrepareExprList(List * nodes,EState * estate)597 ExecPrepareExprList(List *nodes, EState *estate)
598 {
599 List *result = NIL;
600 MemoryContext oldcontext;
601 ListCell *lc;
602
603 /* Ensure that the list cell nodes are in the right context too */
604 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
605
606 foreach(lc, nodes)
607 {
608 Expr *e = (Expr *) lfirst(lc);
609
610 result = lappend(result, ExecPrepareExpr(e, estate));
611 }
612
613 MemoryContextSwitchTo(oldcontext);
614
615 return result;
616 }
617
618 /*
619 * ExecCheck - evaluate a check constraint
620 *
621 * For check constraints, a null result is taken as TRUE, ie the constraint
622 * passes.
623 *
624 * The check constraint may have been prepared with ExecInitCheck
625 * (possibly via ExecPrepareCheck) if the caller had it in implicit-AND
626 * format, but a regular boolean expression prepared with ExecInitExpr or
627 * ExecPrepareExpr works too.
628 */
629 bool
ExecCheck(ExprState * state,ExprContext * econtext)630 ExecCheck(ExprState *state, ExprContext *econtext)
631 {
632 Datum ret;
633 bool isnull;
634
635 /* short-circuit (here and in ExecInitCheck) for empty restriction list */
636 if (state == NULL)
637 return true;
638
639 /* verify that expression was not compiled using ExecInitQual */
640 Assert(!(state->flags & EEO_FLAG_IS_QUAL));
641
642 ret = ExecEvalExprSwitchContext(state, econtext, &isnull);
643
644 if (isnull)
645 return true;
646
647 return DatumGetBool(ret);
648 }
649
650 /*
651 * Prepare a compiled expression for execution. This has to be called for
652 * every ExprState before it can be executed.
653 *
654 * NB: While this currently only calls ExecReadyInterpretedExpr(),
655 * this will likely get extended to further expression evaluation methods.
656 * Therefore this should be used instead of directly calling
657 * ExecReadyInterpretedExpr().
658 */
659 static void
ExecReadyExpr(ExprState * state)660 ExecReadyExpr(ExprState *state)
661 {
662 if (jit_compile_expr(state))
663 return;
664
665 ExecReadyInterpretedExpr(state);
666 }
667
668 /*
669 * Append the steps necessary for the evaluation of node to ExprState->steps,
670 * possibly recursing into sub-expressions of node.
671 *
672 * node - expression to evaluate
673 * state - ExprState to whose ->steps to append the necessary operations
674 * resv / resnull - where to store the result of the node into
675 */
676 static void
ExecInitExprRec(Expr * node,ExprState * state,Datum * resv,bool * resnull)677 ExecInitExprRec(Expr *node, ExprState *state,
678 Datum *resv, bool *resnull)
679 {
680 ExprEvalStep scratch = {0};
681
682 /* Guard against stack overflow due to overly complex expressions */
683 check_stack_depth();
684
685 /* Step's output location is always what the caller gave us */
686 Assert(resv != NULL && resnull != NULL);
687 scratch.resvalue = resv;
688 scratch.resnull = resnull;
689
690 /* cases should be ordered as they are in enum NodeTag */
691 switch (nodeTag(node))
692 {
693 case T_Var:
694 {
695 Var *variable = (Var *) node;
696
697 if (variable->varattno == InvalidAttrNumber)
698 {
699 /* whole-row Var */
700 ExecInitWholeRowVar(&scratch, variable, state);
701 }
702 else if (variable->varattno <= 0)
703 {
704 /* system column */
705 scratch.d.var.attnum = variable->varattno;
706 scratch.d.var.vartype = variable->vartype;
707 switch (variable->varno)
708 {
709 case INNER_VAR:
710 scratch.opcode = EEOP_INNER_SYSVAR;
711 break;
712 case OUTER_VAR:
713 scratch.opcode = EEOP_OUTER_SYSVAR;
714 break;
715
716 /* INDEX_VAR is handled by default case */
717
718 default:
719 scratch.opcode = EEOP_SCAN_SYSVAR;
720 break;
721 }
722 }
723 else
724 {
725 /* regular user column */
726 scratch.d.var.attnum = variable->varattno - 1;
727 scratch.d.var.vartype = variable->vartype;
728 switch (variable->varno)
729 {
730 case INNER_VAR:
731 scratch.opcode = EEOP_INNER_VAR;
732 break;
733 case OUTER_VAR:
734 scratch.opcode = EEOP_OUTER_VAR;
735 break;
736
737 /* INDEX_VAR is handled by default case */
738
739 default:
740 scratch.opcode = EEOP_SCAN_VAR;
741 break;
742 }
743 }
744
745 ExprEvalPushStep(state, &scratch);
746 break;
747 }
748
749 case T_Const:
750 {
751 Const *con = (Const *) node;
752
753 scratch.opcode = EEOP_CONST;
754 scratch.d.constval.value = con->constvalue;
755 scratch.d.constval.isnull = con->constisnull;
756
757 ExprEvalPushStep(state, &scratch);
758 break;
759 }
760
761 case T_Param:
762 {
763 Param *param = (Param *) node;
764 ParamListInfo params;
765
766 switch (param->paramkind)
767 {
768 case PARAM_EXEC:
769 scratch.opcode = EEOP_PARAM_EXEC;
770 scratch.d.param.paramid = param->paramid;
771 scratch.d.param.paramtype = param->paramtype;
772 ExprEvalPushStep(state, &scratch);
773 break;
774 case PARAM_EXTERN:
775
776 /*
777 * If we have a relevant ParamCompileHook, use it;
778 * otherwise compile a standard EEOP_PARAM_EXTERN
779 * step. ext_params, if supplied, takes precedence
780 * over info from the parent node's EState (if any).
781 */
782 if (state->ext_params)
783 params = state->ext_params;
784 else if (state->parent &&
785 state->parent->state)
786 params = state->parent->state->es_param_list_info;
787 else
788 params = NULL;
789 if (params && params->paramCompile)
790 {
791 params->paramCompile(params, param, state,
792 resv, resnull);
793 }
794 else
795 {
796 scratch.opcode = EEOP_PARAM_EXTERN;
797 scratch.d.param.paramid = param->paramid;
798 scratch.d.param.paramtype = param->paramtype;
799 ExprEvalPushStep(state, &scratch);
800 }
801 break;
802 default:
803 elog(ERROR, "unrecognized paramkind: %d",
804 (int) param->paramkind);
805 break;
806 }
807 break;
808 }
809
810 case T_Aggref:
811 {
812 Aggref *aggref = (Aggref *) node;
813 AggrefExprState *astate = makeNode(AggrefExprState);
814
815 scratch.opcode = EEOP_AGGREF;
816 scratch.d.aggref.astate = astate;
817 astate->aggref = aggref;
818
819 if (state->parent && IsA(state->parent, AggState))
820 {
821 AggState *aggstate = (AggState *) state->parent;
822
823 aggstate->aggs = lappend(aggstate->aggs, astate);
824 aggstate->numaggs++;
825 }
826 else
827 {
828 /* planner messed up */
829 elog(ERROR, "Aggref found in non-Agg plan node");
830 }
831
832 ExprEvalPushStep(state, &scratch);
833 break;
834 }
835
836 case T_GroupingFunc:
837 {
838 GroupingFunc *grp_node = (GroupingFunc *) node;
839 Agg *agg;
840
841 if (!state->parent || !IsA(state->parent, AggState) ||
842 !IsA(state->parent->plan, Agg))
843 elog(ERROR, "GroupingFunc found in non-Agg plan node");
844
845 scratch.opcode = EEOP_GROUPING_FUNC;
846
847 agg = (Agg *) (state->parent->plan);
848
849 if (agg->groupingSets)
850 scratch.d.grouping_func.clauses = grp_node->cols;
851 else
852 scratch.d.grouping_func.clauses = NIL;
853
854 ExprEvalPushStep(state, &scratch);
855 break;
856 }
857
858 case T_WindowFunc:
859 {
860 WindowFunc *wfunc = (WindowFunc *) node;
861 WindowFuncExprState *wfstate = makeNode(WindowFuncExprState);
862
863 wfstate->wfunc = wfunc;
864
865 if (state->parent && IsA(state->parent, WindowAggState))
866 {
867 WindowAggState *winstate = (WindowAggState *) state->parent;
868 int nfuncs;
869
870 winstate->funcs = lappend(winstate->funcs, wfstate);
871 nfuncs = ++winstate->numfuncs;
872 if (wfunc->winagg)
873 winstate->numaggs++;
874
875 /* for now initialize agg using old style expressions */
876 wfstate->args = ExecInitExprList(wfunc->args,
877 state->parent);
878 wfstate->aggfilter = ExecInitExpr(wfunc->aggfilter,
879 state->parent);
880
881 /*
882 * Complain if the windowfunc's arguments contain any
883 * windowfuncs; nested window functions are semantically
884 * nonsensical. (This should have been caught earlier,
885 * but we defend against it here anyway.)
886 */
887 if (nfuncs != winstate->numfuncs)
888 ereport(ERROR,
889 (errcode(ERRCODE_WINDOWING_ERROR),
890 errmsg("window function calls cannot be nested")));
891 }
892 else
893 {
894 /* planner messed up */
895 elog(ERROR, "WindowFunc found in non-WindowAgg plan node");
896 }
897
898 scratch.opcode = EEOP_WINDOW_FUNC;
899 scratch.d.window_func.wfstate = wfstate;
900 ExprEvalPushStep(state, &scratch);
901 break;
902 }
903
904 case T_SubscriptingRef:
905 {
906 SubscriptingRef *sbsref = (SubscriptingRef *) node;
907
908 ExecInitSubscriptingRef(&scratch, sbsref, state, resv, resnull);
909 break;
910 }
911
912 case T_FuncExpr:
913 {
914 FuncExpr *func = (FuncExpr *) node;
915
916 ExecInitFunc(&scratch, node,
917 func->args, func->funcid, func->inputcollid,
918 state);
919 ExprEvalPushStep(state, &scratch);
920 break;
921 }
922
923 case T_OpExpr:
924 {
925 OpExpr *op = (OpExpr *) node;
926
927 ExecInitFunc(&scratch, node,
928 op->args, op->opfuncid, op->inputcollid,
929 state);
930 ExprEvalPushStep(state, &scratch);
931 break;
932 }
933
934 case T_DistinctExpr:
935 {
936 DistinctExpr *op = (DistinctExpr *) node;
937
938 ExecInitFunc(&scratch, node,
939 op->args, op->opfuncid, op->inputcollid,
940 state);
941
942 /*
943 * Change opcode of call instruction to EEOP_DISTINCT.
944 *
945 * XXX: historically we've not called the function usage
946 * pgstat infrastructure - that seems inconsistent given that
947 * we do so for normal function *and* operator evaluation. If
948 * we decided to do that here, we'd probably want separate
949 * opcodes for FUSAGE or not.
950 */
951 scratch.opcode = EEOP_DISTINCT;
952 ExprEvalPushStep(state, &scratch);
953 break;
954 }
955
956 case T_NullIfExpr:
957 {
958 NullIfExpr *op = (NullIfExpr *) node;
959
960 ExecInitFunc(&scratch, node,
961 op->args, op->opfuncid, op->inputcollid,
962 state);
963
964 /*
965 * Change opcode of call instruction to EEOP_NULLIF.
966 *
967 * XXX: historically we've not called the function usage
968 * pgstat infrastructure - that seems inconsistent given that
969 * we do so for normal function *and* operator evaluation. If
970 * we decided to do that here, we'd probably want separate
971 * opcodes for FUSAGE or not.
972 */
973 scratch.opcode = EEOP_NULLIF;
974 ExprEvalPushStep(state, &scratch);
975 break;
976 }
977
978 case T_ScalarArrayOpExpr:
979 {
980 ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
981 Expr *scalararg;
982 Expr *arrayarg;
983 FmgrInfo *finfo;
984 FunctionCallInfo fcinfo;
985 AclResult aclresult;
986
987 Assert(list_length(opexpr->args) == 2);
988 scalararg = (Expr *) linitial(opexpr->args);
989 arrayarg = (Expr *) lsecond(opexpr->args);
990
991 /* Check permission to call function */
992 aclresult = pg_proc_aclcheck(opexpr->opfuncid,
993 GetUserId(),
994 ACL_EXECUTE);
995 if (aclresult != ACLCHECK_OK)
996 aclcheck_error(aclresult, OBJECT_FUNCTION,
997 get_func_name(opexpr->opfuncid));
998 InvokeFunctionExecuteHook(opexpr->opfuncid);
999
1000 /* Set up the primary fmgr lookup information */
1001 finfo = palloc0(sizeof(FmgrInfo));
1002 fcinfo = palloc0(SizeForFunctionCallInfo(2));
1003 fmgr_info(opexpr->opfuncid, finfo);
1004 fmgr_info_set_expr((Node *) node, finfo);
1005 InitFunctionCallInfoData(*fcinfo, finfo, 2,
1006 opexpr->inputcollid, NULL, NULL);
1007
1008 /* Evaluate scalar directly into left function argument */
1009 ExecInitExprRec(scalararg, state,
1010 &fcinfo->args[0].value, &fcinfo->args[0].isnull);
1011
1012 /*
1013 * Evaluate array argument into our return value. There's no
1014 * danger in that, because the return value is guaranteed to
1015 * be overwritten by EEOP_SCALARARRAYOP, and will not be
1016 * passed to any other expression.
1017 */
1018 ExecInitExprRec(arrayarg, state, resv, resnull);
1019
1020 /* And perform the operation */
1021 scratch.opcode = EEOP_SCALARARRAYOP;
1022 scratch.d.scalararrayop.element_type = InvalidOid;
1023 scratch.d.scalararrayop.useOr = opexpr->useOr;
1024 scratch.d.scalararrayop.finfo = finfo;
1025 scratch.d.scalararrayop.fcinfo_data = fcinfo;
1026 scratch.d.scalararrayop.fn_addr = finfo->fn_addr;
1027 ExprEvalPushStep(state, &scratch);
1028 break;
1029 }
1030
1031 case T_BoolExpr:
1032 {
1033 BoolExpr *boolexpr = (BoolExpr *) node;
1034 int nargs = list_length(boolexpr->args);
1035 List *adjust_jumps = NIL;
1036 int off;
1037 ListCell *lc;
1038
1039 /* allocate scratch memory used by all steps of AND/OR */
1040 if (boolexpr->boolop != NOT_EXPR)
1041 scratch.d.boolexpr.anynull = (bool *) palloc(sizeof(bool));
1042
1043 /*
1044 * For each argument evaluate the argument itself, then
1045 * perform the bool operation's appropriate handling.
1046 *
1047 * We can evaluate each argument into our result area, since
1048 * the short-circuiting logic means we only need to remember
1049 * previous NULL values.
1050 *
1051 * AND/OR is split into separate STEP_FIRST (one) / STEP (zero
1052 * or more) / STEP_LAST (one) steps, as each of those has to
1053 * perform different work. The FIRST/LAST split is valid
1054 * because AND/OR have at least two arguments.
1055 */
1056 off = 0;
1057 foreach(lc, boolexpr->args)
1058 {
1059 Expr *arg = (Expr *) lfirst(lc);
1060
1061 /* Evaluate argument into our output variable */
1062 ExecInitExprRec(arg, state, resv, resnull);
1063
1064 /* Perform the appropriate step type */
1065 switch (boolexpr->boolop)
1066 {
1067 case AND_EXPR:
1068 Assert(nargs >= 2);
1069
1070 if (off == 0)
1071 scratch.opcode = EEOP_BOOL_AND_STEP_FIRST;
1072 else if (off + 1 == nargs)
1073 scratch.opcode = EEOP_BOOL_AND_STEP_LAST;
1074 else
1075 scratch.opcode = EEOP_BOOL_AND_STEP;
1076 break;
1077 case OR_EXPR:
1078 Assert(nargs >= 2);
1079
1080 if (off == 0)
1081 scratch.opcode = EEOP_BOOL_OR_STEP_FIRST;
1082 else if (off + 1 == nargs)
1083 scratch.opcode = EEOP_BOOL_OR_STEP_LAST;
1084 else
1085 scratch.opcode = EEOP_BOOL_OR_STEP;
1086 break;
1087 case NOT_EXPR:
1088 Assert(nargs == 1);
1089
1090 scratch.opcode = EEOP_BOOL_NOT_STEP;
1091 break;
1092 default:
1093 elog(ERROR, "unrecognized boolop: %d",
1094 (int) boolexpr->boolop);
1095 break;
1096 }
1097
1098 scratch.d.boolexpr.jumpdone = -1;
1099 ExprEvalPushStep(state, &scratch);
1100 adjust_jumps = lappend_int(adjust_jumps,
1101 state->steps_len - 1);
1102 off++;
1103 }
1104
1105 /* adjust jump targets */
1106 foreach(lc, adjust_jumps)
1107 {
1108 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1109
1110 Assert(as->d.boolexpr.jumpdone == -1);
1111 as->d.boolexpr.jumpdone = state->steps_len;
1112 }
1113
1114 break;
1115 }
1116
1117 case T_SubPlan:
1118 {
1119 SubPlan *subplan = (SubPlan *) node;
1120 SubPlanState *sstate;
1121
1122 if (!state->parent)
1123 elog(ERROR, "SubPlan found with no parent plan");
1124
1125 sstate = ExecInitSubPlan(subplan, state->parent);
1126
1127 /* add SubPlanState nodes to state->parent->subPlan */
1128 state->parent->subPlan = lappend(state->parent->subPlan,
1129 sstate);
1130
1131 scratch.opcode = EEOP_SUBPLAN;
1132 scratch.d.subplan.sstate = sstate;
1133
1134 ExprEvalPushStep(state, &scratch);
1135 break;
1136 }
1137
1138 case T_AlternativeSubPlan:
1139 {
1140 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
1141 AlternativeSubPlanState *asstate;
1142
1143 if (!state->parent)
1144 elog(ERROR, "AlternativeSubPlan found with no parent plan");
1145
1146 asstate = ExecInitAlternativeSubPlan(asplan, state->parent);
1147
1148 scratch.opcode = EEOP_ALTERNATIVE_SUBPLAN;
1149 scratch.d.alternative_subplan.asstate = asstate;
1150
1151 ExprEvalPushStep(state, &scratch);
1152 break;
1153 }
1154
1155 case T_FieldSelect:
1156 {
1157 FieldSelect *fselect = (FieldSelect *) node;
1158
1159 /* evaluate row/record argument into result area */
1160 ExecInitExprRec(fselect->arg, state, resv, resnull);
1161
1162 /* and extract field */
1163 scratch.opcode = EEOP_FIELDSELECT;
1164 scratch.d.fieldselect.fieldnum = fselect->fieldnum;
1165 scratch.d.fieldselect.resulttype = fselect->resulttype;
1166 scratch.d.fieldselect.rowcache.cacheptr = NULL;
1167
1168 ExprEvalPushStep(state, &scratch);
1169 break;
1170 }
1171
1172 case T_FieldStore:
1173 {
1174 FieldStore *fstore = (FieldStore *) node;
1175 TupleDesc tupDesc;
1176 ExprEvalRowtypeCache *rowcachep;
1177 Datum *values;
1178 bool *nulls;
1179 int ncolumns;
1180 ListCell *l1,
1181 *l2;
1182
1183 /* find out the number of columns in the composite type */
1184 tupDesc = lookup_rowtype_tupdesc(fstore->resulttype, -1);
1185 ncolumns = tupDesc->natts;
1186 DecrTupleDescRefCount(tupDesc);
1187
1188 /* create workspace for column values */
1189 values = (Datum *) palloc(sizeof(Datum) * ncolumns);
1190 nulls = (bool *) palloc(sizeof(bool) * ncolumns);
1191
1192 /* create shared composite-type-lookup cache struct */
1193 rowcachep = palloc(sizeof(ExprEvalRowtypeCache));
1194 rowcachep->cacheptr = NULL;
1195
1196 /* emit code to evaluate the composite input value */
1197 ExecInitExprRec(fstore->arg, state, resv, resnull);
1198
1199 /* next, deform the input tuple into our workspace */
1200 scratch.opcode = EEOP_FIELDSTORE_DEFORM;
1201 scratch.d.fieldstore.fstore = fstore;
1202 scratch.d.fieldstore.rowcache = rowcachep;
1203 scratch.d.fieldstore.values = values;
1204 scratch.d.fieldstore.nulls = nulls;
1205 scratch.d.fieldstore.ncolumns = ncolumns;
1206 ExprEvalPushStep(state, &scratch);
1207
1208 /* evaluate new field values, store in workspace columns */
1209 forboth(l1, fstore->newvals, l2, fstore->fieldnums)
1210 {
1211 Expr *e = (Expr *) lfirst(l1);
1212 AttrNumber fieldnum = lfirst_int(l2);
1213 Datum *save_innermost_caseval;
1214 bool *save_innermost_casenull;
1215
1216 if (fieldnum <= 0 || fieldnum > ncolumns)
1217 elog(ERROR, "field number %d is out of range in FieldStore",
1218 fieldnum);
1219
1220 /*
1221 * Use the CaseTestExpr mechanism to pass down the old
1222 * value of the field being replaced; this is needed in
1223 * case the newval is itself a FieldStore or
1224 * SubscriptingRef that has to obtain and modify the old
1225 * value. It's safe to reuse the CASE mechanism because
1226 * there cannot be a CASE between here and where the value
1227 * would be needed, and a field assignment can't be within
1228 * a CASE either. (So saving and restoring
1229 * innermost_caseval is just paranoia, but let's do it
1230 * anyway.)
1231 *
1232 * Another non-obvious point is that it's safe to use the
1233 * field's values[]/nulls[] entries as both the caseval
1234 * source and the result address for this subexpression.
1235 * That's okay only because (1) both FieldStore and
1236 * SubscriptingRef evaluate their arg or refexpr inputs
1237 * first, and (2) any such CaseTestExpr is directly the
1238 * arg or refexpr input. So any read of the caseval will
1239 * occur before there's a chance to overwrite it. Also,
1240 * if multiple entries in the newvals/fieldnums lists
1241 * target the same field, they'll effectively be applied
1242 * left-to-right which is what we want.
1243 */
1244 save_innermost_caseval = state->innermost_caseval;
1245 save_innermost_casenull = state->innermost_casenull;
1246 state->innermost_caseval = &values[fieldnum - 1];
1247 state->innermost_casenull = &nulls[fieldnum - 1];
1248
1249 ExecInitExprRec(e, state,
1250 &values[fieldnum - 1],
1251 &nulls[fieldnum - 1]);
1252
1253 state->innermost_caseval = save_innermost_caseval;
1254 state->innermost_casenull = save_innermost_casenull;
1255 }
1256
1257 /* finally, form result tuple */
1258 scratch.opcode = EEOP_FIELDSTORE_FORM;
1259 scratch.d.fieldstore.fstore = fstore;
1260 scratch.d.fieldstore.rowcache = rowcachep;
1261 scratch.d.fieldstore.values = values;
1262 scratch.d.fieldstore.nulls = nulls;
1263 scratch.d.fieldstore.ncolumns = ncolumns;
1264 ExprEvalPushStep(state, &scratch);
1265 break;
1266 }
1267
1268 case T_RelabelType:
1269 {
1270 /* relabel doesn't need to do anything at runtime */
1271 RelabelType *relabel = (RelabelType *) node;
1272
1273 ExecInitExprRec(relabel->arg, state, resv, resnull);
1274 break;
1275 }
1276
1277 case T_CoerceViaIO:
1278 {
1279 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
1280 Oid iofunc;
1281 bool typisvarlena;
1282 Oid typioparam;
1283 FunctionCallInfo fcinfo_in;
1284
1285 /* evaluate argument into step's result area */
1286 ExecInitExprRec(iocoerce->arg, state, resv, resnull);
1287
1288 /*
1289 * Prepare both output and input function calls, to be
1290 * evaluated inside a single evaluation step for speed - this
1291 * can be a very common operation.
1292 *
1293 * We don't check permissions here as a type's input/output
1294 * function are assumed to be executable by everyone.
1295 */
1296 scratch.opcode = EEOP_IOCOERCE;
1297
1298 /* lookup the source type's output function */
1299 scratch.d.iocoerce.finfo_out = palloc0(sizeof(FmgrInfo));
1300 scratch.d.iocoerce.fcinfo_data_out = palloc0(SizeForFunctionCallInfo(1));
1301
1302 getTypeOutputInfo(exprType((Node *) iocoerce->arg),
1303 &iofunc, &typisvarlena);
1304 fmgr_info(iofunc, scratch.d.iocoerce.finfo_out);
1305 fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_out);
1306 InitFunctionCallInfoData(*scratch.d.iocoerce.fcinfo_data_out,
1307 scratch.d.iocoerce.finfo_out,
1308 1, InvalidOid, NULL, NULL);
1309
1310 /* lookup the result type's input function */
1311 scratch.d.iocoerce.finfo_in = palloc0(sizeof(FmgrInfo));
1312 scratch.d.iocoerce.fcinfo_data_in = palloc0(SizeForFunctionCallInfo(3));
1313
1314 getTypeInputInfo(iocoerce->resulttype,
1315 &iofunc, &typioparam);
1316 fmgr_info(iofunc, scratch.d.iocoerce.finfo_in);
1317 fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_in);
1318 InitFunctionCallInfoData(*scratch.d.iocoerce.fcinfo_data_in,
1319 scratch.d.iocoerce.finfo_in,
1320 3, InvalidOid, NULL, NULL);
1321
1322 /*
1323 * We can preload the second and third arguments for the input
1324 * function, since they're constants.
1325 */
1326 fcinfo_in = scratch.d.iocoerce.fcinfo_data_in;
1327 fcinfo_in->args[1].value = ObjectIdGetDatum(typioparam);
1328 fcinfo_in->args[1].isnull = false;
1329 fcinfo_in->args[2].value = Int32GetDatum(-1);
1330 fcinfo_in->args[2].isnull = false;
1331
1332 ExprEvalPushStep(state, &scratch);
1333 break;
1334 }
1335
1336 case T_ArrayCoerceExpr:
1337 {
1338 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
1339 Oid resultelemtype;
1340 ExprState *elemstate;
1341
1342 /* evaluate argument into step's result area */
1343 ExecInitExprRec(acoerce->arg, state, resv, resnull);
1344
1345 resultelemtype = get_element_type(acoerce->resulttype);
1346 if (!OidIsValid(resultelemtype))
1347 ereport(ERROR,
1348 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1349 errmsg("target type is not an array")));
1350
1351 /*
1352 * Construct a sub-expression for the per-element expression;
1353 * but don't ready it until after we check it for triviality.
1354 * We assume it hasn't any Var references, but does have a
1355 * CaseTestExpr representing the source array element values.
1356 */
1357 elemstate = makeNode(ExprState);
1358 elemstate->expr = acoerce->elemexpr;
1359 elemstate->parent = state->parent;
1360 elemstate->ext_params = state->ext_params;
1361
1362 elemstate->innermost_caseval = (Datum *) palloc(sizeof(Datum));
1363 elemstate->innermost_casenull = (bool *) palloc(sizeof(bool));
1364
1365 ExecInitExprRec(acoerce->elemexpr, elemstate,
1366 &elemstate->resvalue, &elemstate->resnull);
1367
1368 if (elemstate->steps_len == 1 &&
1369 elemstate->steps[0].opcode == EEOP_CASE_TESTVAL)
1370 {
1371 /* Trivial, so we need no per-element work at runtime */
1372 elemstate = NULL;
1373 }
1374 else
1375 {
1376 /* Not trivial, so append a DONE step */
1377 scratch.opcode = EEOP_DONE;
1378 ExprEvalPushStep(elemstate, &scratch);
1379 /* and ready the subexpression */
1380 ExecReadyExpr(elemstate);
1381 }
1382
1383 scratch.opcode = EEOP_ARRAYCOERCE;
1384 scratch.d.arraycoerce.elemexprstate = elemstate;
1385 scratch.d.arraycoerce.resultelemtype = resultelemtype;
1386
1387 if (elemstate)
1388 {
1389 /* Set up workspace for array_map */
1390 scratch.d.arraycoerce.amstate =
1391 (ArrayMapState *) palloc0(sizeof(ArrayMapState));
1392 }
1393 else
1394 {
1395 /* Don't need workspace if there's no subexpression */
1396 scratch.d.arraycoerce.amstate = NULL;
1397 }
1398
1399 ExprEvalPushStep(state, &scratch);
1400 break;
1401 }
1402
1403 case T_ConvertRowtypeExpr:
1404 {
1405 ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
1406 ExprEvalRowtypeCache *rowcachep;
1407
1408 /* cache structs must be out-of-line for space reasons */
1409 rowcachep = palloc(2 * sizeof(ExprEvalRowtypeCache));
1410 rowcachep[0].cacheptr = NULL;
1411 rowcachep[1].cacheptr = NULL;
1412
1413 /* evaluate argument into step's result area */
1414 ExecInitExprRec(convert->arg, state, resv, resnull);
1415
1416 /* and push conversion step */
1417 scratch.opcode = EEOP_CONVERT_ROWTYPE;
1418 scratch.d.convert_rowtype.inputtype =
1419 exprType((Node *) convert->arg);
1420 scratch.d.convert_rowtype.outputtype = convert->resulttype;
1421 scratch.d.convert_rowtype.incache = &rowcachep[0];
1422 scratch.d.convert_rowtype.outcache = &rowcachep[1];
1423 scratch.d.convert_rowtype.map = NULL;
1424
1425 ExprEvalPushStep(state, &scratch);
1426 break;
1427 }
1428
1429 /* note that CaseWhen expressions are handled within this block */
1430 case T_CaseExpr:
1431 {
1432 CaseExpr *caseExpr = (CaseExpr *) node;
1433 List *adjust_jumps = NIL;
1434 Datum *caseval = NULL;
1435 bool *casenull = NULL;
1436 ListCell *lc;
1437
1438 /*
1439 * If there's a test expression, we have to evaluate it and
1440 * save the value where the CaseTestExpr placeholders can find
1441 * it.
1442 */
1443 if (caseExpr->arg != NULL)
1444 {
1445 /* Evaluate testexpr into caseval/casenull workspace */
1446 caseval = palloc(sizeof(Datum));
1447 casenull = palloc(sizeof(bool));
1448
1449 ExecInitExprRec(caseExpr->arg, state,
1450 caseval, casenull);
1451
1452 /*
1453 * Since value might be read multiple times, force to R/O
1454 * - but only if it could be an expanded datum.
1455 */
1456 if (get_typlen(exprType((Node *) caseExpr->arg)) == -1)
1457 {
1458 /* change caseval in-place */
1459 scratch.opcode = EEOP_MAKE_READONLY;
1460 scratch.resvalue = caseval;
1461 scratch.resnull = casenull;
1462 scratch.d.make_readonly.value = caseval;
1463 scratch.d.make_readonly.isnull = casenull;
1464 ExprEvalPushStep(state, &scratch);
1465 /* restore normal settings of scratch fields */
1466 scratch.resvalue = resv;
1467 scratch.resnull = resnull;
1468 }
1469 }
1470
1471 /*
1472 * Prepare to evaluate each of the WHEN clauses in turn; as
1473 * soon as one is true we return the value of the
1474 * corresponding THEN clause. If none are true then we return
1475 * the value of the ELSE clause, or NULL if there is none.
1476 */
1477 foreach(lc, caseExpr->args)
1478 {
1479 CaseWhen *when = (CaseWhen *) lfirst(lc);
1480 Datum *save_innermost_caseval;
1481 bool *save_innermost_casenull;
1482 int whenstep;
1483
1484 /*
1485 * Make testexpr result available to CaseTestExpr nodes
1486 * within the condition. We must save and restore prior
1487 * setting of innermost_caseval fields, in case this node
1488 * is itself within a larger CASE.
1489 *
1490 * If there's no test expression, we don't actually need
1491 * to save and restore these fields; but it's less code to
1492 * just do so unconditionally.
1493 */
1494 save_innermost_caseval = state->innermost_caseval;
1495 save_innermost_casenull = state->innermost_casenull;
1496 state->innermost_caseval = caseval;
1497 state->innermost_casenull = casenull;
1498
1499 /* evaluate condition into CASE's result variables */
1500 ExecInitExprRec(when->expr, state, resv, resnull);
1501
1502 state->innermost_caseval = save_innermost_caseval;
1503 state->innermost_casenull = save_innermost_casenull;
1504
1505 /* If WHEN result isn't true, jump to next CASE arm */
1506 scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
1507 scratch.d.jump.jumpdone = -1; /* computed later */
1508 ExprEvalPushStep(state, &scratch);
1509 whenstep = state->steps_len - 1;
1510
1511 /*
1512 * If WHEN result is true, evaluate THEN result, storing
1513 * it into the CASE's result variables.
1514 */
1515 ExecInitExprRec(when->result, state, resv, resnull);
1516
1517 /* Emit JUMP step to jump to end of CASE's code */
1518 scratch.opcode = EEOP_JUMP;
1519 scratch.d.jump.jumpdone = -1; /* computed later */
1520 ExprEvalPushStep(state, &scratch);
1521
1522 /*
1523 * Don't know address for that jump yet, compute once the
1524 * whole CASE expression is built.
1525 */
1526 adjust_jumps = lappend_int(adjust_jumps,
1527 state->steps_len - 1);
1528
1529 /*
1530 * But we can set WHEN test's jump target now, to make it
1531 * jump to the next WHEN subexpression or the ELSE.
1532 */
1533 state->steps[whenstep].d.jump.jumpdone = state->steps_len;
1534 }
1535
1536 /* transformCaseExpr always adds a default */
1537 Assert(caseExpr->defresult);
1538
1539 /* evaluate ELSE expr into CASE's result variables */
1540 ExecInitExprRec(caseExpr->defresult, state,
1541 resv, resnull);
1542
1543 /* adjust jump targets */
1544 foreach(lc, adjust_jumps)
1545 {
1546 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1547
1548 Assert(as->opcode == EEOP_JUMP);
1549 Assert(as->d.jump.jumpdone == -1);
1550 as->d.jump.jumpdone = state->steps_len;
1551 }
1552
1553 break;
1554 }
1555
1556 case T_CaseTestExpr:
1557 {
1558 /*
1559 * Read from location identified by innermost_caseval. Note
1560 * that innermost_caseval could be NULL, if this node isn't
1561 * actually within a CaseExpr, ArrayCoerceExpr, etc structure.
1562 * That can happen because some parts of the system abuse
1563 * CaseTestExpr to cause a read of a value externally supplied
1564 * in econtext->caseValue_datum. We'll take care of that
1565 * scenario at runtime.
1566 */
1567 scratch.opcode = EEOP_CASE_TESTVAL;
1568 scratch.d.casetest.value = state->innermost_caseval;
1569 scratch.d.casetest.isnull = state->innermost_casenull;
1570
1571 ExprEvalPushStep(state, &scratch);
1572 break;
1573 }
1574
1575 case T_ArrayExpr:
1576 {
1577 ArrayExpr *arrayexpr = (ArrayExpr *) node;
1578 int nelems = list_length(arrayexpr->elements);
1579 ListCell *lc;
1580 int elemoff;
1581
1582 /*
1583 * Evaluate by computing each element, and then forming the
1584 * array. Elements are computed into scratch arrays
1585 * associated with the ARRAYEXPR step.
1586 */
1587 scratch.opcode = EEOP_ARRAYEXPR;
1588 scratch.d.arrayexpr.elemvalues =
1589 (Datum *) palloc(sizeof(Datum) * nelems);
1590 scratch.d.arrayexpr.elemnulls =
1591 (bool *) palloc(sizeof(bool) * nelems);
1592 scratch.d.arrayexpr.nelems = nelems;
1593
1594 /* fill remaining fields of step */
1595 scratch.d.arrayexpr.multidims = arrayexpr->multidims;
1596 scratch.d.arrayexpr.elemtype = arrayexpr->element_typeid;
1597
1598 /* do one-time catalog lookup for type info */
1599 get_typlenbyvalalign(arrayexpr->element_typeid,
1600 &scratch.d.arrayexpr.elemlength,
1601 &scratch.d.arrayexpr.elembyval,
1602 &scratch.d.arrayexpr.elemalign);
1603
1604 /* prepare to evaluate all arguments */
1605 elemoff = 0;
1606 foreach(lc, arrayexpr->elements)
1607 {
1608 Expr *e = (Expr *) lfirst(lc);
1609
1610 ExecInitExprRec(e, state,
1611 &scratch.d.arrayexpr.elemvalues[elemoff],
1612 &scratch.d.arrayexpr.elemnulls[elemoff]);
1613 elemoff++;
1614 }
1615
1616 /* and then collect all into an array */
1617 ExprEvalPushStep(state, &scratch);
1618 break;
1619 }
1620
1621 case T_RowExpr:
1622 {
1623 RowExpr *rowexpr = (RowExpr *) node;
1624 int nelems = list_length(rowexpr->args);
1625 TupleDesc tupdesc;
1626 int i;
1627 ListCell *l;
1628
1629 /* Build tupdesc to describe result tuples */
1630 if (rowexpr->row_typeid == RECORDOID)
1631 {
1632 /* generic record, use types of given expressions */
1633 tupdesc = ExecTypeFromExprList(rowexpr->args);
1634 }
1635 else
1636 {
1637 /* it's been cast to a named type, use that */
1638 tupdesc = lookup_rowtype_tupdesc_copy(rowexpr->row_typeid, -1);
1639 }
1640 /* In either case, adopt RowExpr's column aliases */
1641 ExecTypeSetColNames(tupdesc, rowexpr->colnames);
1642 /* Bless the tupdesc in case it's now of type RECORD */
1643 BlessTupleDesc(tupdesc);
1644
1645 /*
1646 * In the named-type case, the tupdesc could have more columns
1647 * than are in the args list, since the type might have had
1648 * columns added since the ROW() was parsed. We want those
1649 * extra columns to go to nulls, so we make sure that the
1650 * workspace arrays are large enough and then initialize any
1651 * extra columns to read as NULLs.
1652 */
1653 Assert(nelems <= tupdesc->natts);
1654 nelems = Max(nelems, tupdesc->natts);
1655
1656 /*
1657 * Evaluate by first building datums for each field, and then
1658 * a final step forming the composite datum.
1659 */
1660 scratch.opcode = EEOP_ROW;
1661 scratch.d.row.tupdesc = tupdesc;
1662
1663 /* space for the individual field datums */
1664 scratch.d.row.elemvalues =
1665 (Datum *) palloc(sizeof(Datum) * nelems);
1666 scratch.d.row.elemnulls =
1667 (bool *) palloc(sizeof(bool) * nelems);
1668 /* as explained above, make sure any extra columns are null */
1669 memset(scratch.d.row.elemnulls, true, sizeof(bool) * nelems);
1670
1671 /* Set up evaluation, skipping any deleted columns */
1672 i = 0;
1673 foreach(l, rowexpr->args)
1674 {
1675 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
1676 Expr *e = (Expr *) lfirst(l);
1677
1678 if (!att->attisdropped)
1679 {
1680 /*
1681 * Guard against ALTER COLUMN TYPE on rowtype since
1682 * the RowExpr was created. XXX should we check
1683 * typmod too? Not sure we can be sure it'll be the
1684 * same.
1685 */
1686 if (exprType((Node *) e) != att->atttypid)
1687 ereport(ERROR,
1688 (errcode(ERRCODE_DATATYPE_MISMATCH),
1689 errmsg("ROW() column has type %s instead of type %s",
1690 format_type_be(exprType((Node *) e)),
1691 format_type_be(att->atttypid))));
1692 }
1693 else
1694 {
1695 /*
1696 * Ignore original expression and insert a NULL. We
1697 * don't really care what type of NULL it is, so
1698 * always make an int4 NULL.
1699 */
1700 e = (Expr *) makeNullConst(INT4OID, -1, InvalidOid);
1701 }
1702
1703 /* Evaluate column expr into appropriate workspace slot */
1704 ExecInitExprRec(e, state,
1705 &scratch.d.row.elemvalues[i],
1706 &scratch.d.row.elemnulls[i]);
1707 i++;
1708 }
1709
1710 /* And finally build the row value */
1711 ExprEvalPushStep(state, &scratch);
1712 break;
1713 }
1714
1715 case T_RowCompareExpr:
1716 {
1717 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1718 int nopers = list_length(rcexpr->opnos);
1719 List *adjust_jumps = NIL;
1720 ListCell *l_left_expr,
1721 *l_right_expr,
1722 *l_opno,
1723 *l_opfamily,
1724 *l_inputcollid;
1725 ListCell *lc;
1726
1727 /*
1728 * Iterate over each field, prepare comparisons. To handle
1729 * NULL results, prepare jumps to after the expression. If a
1730 * comparison yields a != 0 result, jump to the final step.
1731 */
1732 Assert(list_length(rcexpr->largs) == nopers);
1733 Assert(list_length(rcexpr->rargs) == nopers);
1734 Assert(list_length(rcexpr->opfamilies) == nopers);
1735 Assert(list_length(rcexpr->inputcollids) == nopers);
1736
1737 forfive(l_left_expr, rcexpr->largs,
1738 l_right_expr, rcexpr->rargs,
1739 l_opno, rcexpr->opnos,
1740 l_opfamily, rcexpr->opfamilies,
1741 l_inputcollid, rcexpr->inputcollids)
1742 {
1743 Expr *left_expr = (Expr *) lfirst(l_left_expr);
1744 Expr *right_expr = (Expr *) lfirst(l_right_expr);
1745 Oid opno = lfirst_oid(l_opno);
1746 Oid opfamily = lfirst_oid(l_opfamily);
1747 Oid inputcollid = lfirst_oid(l_inputcollid);
1748 int strategy;
1749 Oid lefttype;
1750 Oid righttype;
1751 Oid proc;
1752 FmgrInfo *finfo;
1753 FunctionCallInfo fcinfo;
1754
1755 get_op_opfamily_properties(opno, opfamily, false,
1756 &strategy,
1757 &lefttype,
1758 &righttype);
1759 proc = get_opfamily_proc(opfamily,
1760 lefttype,
1761 righttype,
1762 BTORDER_PROC);
1763 if (!OidIsValid(proc))
1764 elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
1765 BTORDER_PROC, lefttype, righttype, opfamily);
1766
1767 /* Set up the primary fmgr lookup information */
1768 finfo = palloc0(sizeof(FmgrInfo));
1769 fcinfo = palloc0(SizeForFunctionCallInfo(2));
1770 fmgr_info(proc, finfo);
1771 fmgr_info_set_expr((Node *) node, finfo);
1772 InitFunctionCallInfoData(*fcinfo, finfo, 2,
1773 inputcollid, NULL, NULL);
1774
1775 /*
1776 * If we enforced permissions checks on index support
1777 * functions, we'd need to make a check here. But the
1778 * index support machinery doesn't do that, and thus
1779 * neither does this code.
1780 */
1781
1782 /* evaluate left and right args directly into fcinfo */
1783 ExecInitExprRec(left_expr, state,
1784 &fcinfo->args[0].value, &fcinfo->args[0].isnull);
1785 ExecInitExprRec(right_expr, state,
1786 &fcinfo->args[1].value, &fcinfo->args[1].isnull);
1787
1788 scratch.opcode = EEOP_ROWCOMPARE_STEP;
1789 scratch.d.rowcompare_step.finfo = finfo;
1790 scratch.d.rowcompare_step.fcinfo_data = fcinfo;
1791 scratch.d.rowcompare_step.fn_addr = finfo->fn_addr;
1792 /* jump targets filled below */
1793 scratch.d.rowcompare_step.jumpnull = -1;
1794 scratch.d.rowcompare_step.jumpdone = -1;
1795
1796 ExprEvalPushStep(state, &scratch);
1797 adjust_jumps = lappend_int(adjust_jumps,
1798 state->steps_len - 1);
1799 }
1800
1801 /*
1802 * We could have a zero-column rowtype, in which case the rows
1803 * necessarily compare equal.
1804 */
1805 if (nopers == 0)
1806 {
1807 scratch.opcode = EEOP_CONST;
1808 scratch.d.constval.value = Int32GetDatum(0);
1809 scratch.d.constval.isnull = false;
1810 ExprEvalPushStep(state, &scratch);
1811 }
1812
1813 /* Finally, examine the last comparison result */
1814 scratch.opcode = EEOP_ROWCOMPARE_FINAL;
1815 scratch.d.rowcompare_final.rctype = rcexpr->rctype;
1816 ExprEvalPushStep(state, &scratch);
1817
1818 /* adjust jump targets */
1819 foreach(lc, adjust_jumps)
1820 {
1821 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1822
1823 Assert(as->opcode == EEOP_ROWCOMPARE_STEP);
1824 Assert(as->d.rowcompare_step.jumpdone == -1);
1825 Assert(as->d.rowcompare_step.jumpnull == -1);
1826
1827 /* jump to comparison evaluation */
1828 as->d.rowcompare_step.jumpdone = state->steps_len - 1;
1829 /* jump to the following expression */
1830 as->d.rowcompare_step.jumpnull = state->steps_len;
1831 }
1832
1833 break;
1834 }
1835
1836 case T_CoalesceExpr:
1837 {
1838 CoalesceExpr *coalesce = (CoalesceExpr *) node;
1839 List *adjust_jumps = NIL;
1840 ListCell *lc;
1841
1842 /* We assume there's at least one arg */
1843 Assert(coalesce->args != NIL);
1844
1845 /*
1846 * Prepare evaluation of all coalesced arguments, after each
1847 * one push a step that short-circuits if not null.
1848 */
1849 foreach(lc, coalesce->args)
1850 {
1851 Expr *e = (Expr *) lfirst(lc);
1852
1853 /* evaluate argument, directly into result datum */
1854 ExecInitExprRec(e, state, resv, resnull);
1855
1856 /* if it's not null, skip to end of COALESCE expr */
1857 scratch.opcode = EEOP_JUMP_IF_NOT_NULL;
1858 scratch.d.jump.jumpdone = -1; /* adjust later */
1859 ExprEvalPushStep(state, &scratch);
1860
1861 adjust_jumps = lappend_int(adjust_jumps,
1862 state->steps_len - 1);
1863 }
1864
1865 /*
1866 * No need to add a constant NULL return - we only can get to
1867 * the end of the expression if a NULL already is being
1868 * returned.
1869 */
1870
1871 /* adjust jump targets */
1872 foreach(lc, adjust_jumps)
1873 {
1874 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
1875
1876 Assert(as->opcode == EEOP_JUMP_IF_NOT_NULL);
1877 Assert(as->d.jump.jumpdone == -1);
1878 as->d.jump.jumpdone = state->steps_len;
1879 }
1880
1881 break;
1882 }
1883
1884 case T_MinMaxExpr:
1885 {
1886 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
1887 int nelems = list_length(minmaxexpr->args);
1888 TypeCacheEntry *typentry;
1889 FmgrInfo *finfo;
1890 FunctionCallInfo fcinfo;
1891 ListCell *lc;
1892 int off;
1893
1894 /* Look up the btree comparison function for the datatype */
1895 typentry = lookup_type_cache(minmaxexpr->minmaxtype,
1896 TYPECACHE_CMP_PROC);
1897 if (!OidIsValid(typentry->cmp_proc))
1898 ereport(ERROR,
1899 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1900 errmsg("could not identify a comparison function for type %s",
1901 format_type_be(minmaxexpr->minmaxtype))));
1902
1903 /*
1904 * If we enforced permissions checks on index support
1905 * functions, we'd need to make a check here. But the index
1906 * support machinery doesn't do that, and thus neither does
1907 * this code.
1908 */
1909
1910 /* Perform function lookup */
1911 finfo = palloc0(sizeof(FmgrInfo));
1912 fcinfo = palloc0(SizeForFunctionCallInfo(2));
1913 fmgr_info(typentry->cmp_proc, finfo);
1914 fmgr_info_set_expr((Node *) node, finfo);
1915 InitFunctionCallInfoData(*fcinfo, finfo, 2,
1916 minmaxexpr->inputcollid, NULL, NULL);
1917
1918 scratch.opcode = EEOP_MINMAX;
1919 /* allocate space to store arguments */
1920 scratch.d.minmax.values =
1921 (Datum *) palloc(sizeof(Datum) * nelems);
1922 scratch.d.minmax.nulls =
1923 (bool *) palloc(sizeof(bool) * nelems);
1924 scratch.d.minmax.nelems = nelems;
1925
1926 scratch.d.minmax.op = minmaxexpr->op;
1927 scratch.d.minmax.finfo = finfo;
1928 scratch.d.minmax.fcinfo_data = fcinfo;
1929
1930 /* evaluate expressions into minmax->values/nulls */
1931 off = 0;
1932 foreach(lc, minmaxexpr->args)
1933 {
1934 Expr *e = (Expr *) lfirst(lc);
1935
1936 ExecInitExprRec(e, state,
1937 &scratch.d.minmax.values[off],
1938 &scratch.d.minmax.nulls[off]);
1939 off++;
1940 }
1941
1942 /* and push the final comparison */
1943 ExprEvalPushStep(state, &scratch);
1944 break;
1945 }
1946
1947 case T_SQLValueFunction:
1948 {
1949 SQLValueFunction *svf = (SQLValueFunction *) node;
1950
1951 scratch.opcode = EEOP_SQLVALUEFUNCTION;
1952 scratch.d.sqlvaluefunction.svf = svf;
1953
1954 ExprEvalPushStep(state, &scratch);
1955 break;
1956 }
1957
1958 case T_XmlExpr:
1959 {
1960 XmlExpr *xexpr = (XmlExpr *) node;
1961 int nnamed = list_length(xexpr->named_args);
1962 int nargs = list_length(xexpr->args);
1963 int off;
1964 ListCell *arg;
1965
1966 scratch.opcode = EEOP_XMLEXPR;
1967 scratch.d.xmlexpr.xexpr = xexpr;
1968
1969 /* allocate space for storing all the arguments */
1970 if (nnamed)
1971 {
1972 scratch.d.xmlexpr.named_argvalue =
1973 (Datum *) palloc(sizeof(Datum) * nnamed);
1974 scratch.d.xmlexpr.named_argnull =
1975 (bool *) palloc(sizeof(bool) * nnamed);
1976 }
1977 else
1978 {
1979 scratch.d.xmlexpr.named_argvalue = NULL;
1980 scratch.d.xmlexpr.named_argnull = NULL;
1981 }
1982
1983 if (nargs)
1984 {
1985 scratch.d.xmlexpr.argvalue =
1986 (Datum *) palloc(sizeof(Datum) * nargs);
1987 scratch.d.xmlexpr.argnull =
1988 (bool *) palloc(sizeof(bool) * nargs);
1989 }
1990 else
1991 {
1992 scratch.d.xmlexpr.argvalue = NULL;
1993 scratch.d.xmlexpr.argnull = NULL;
1994 }
1995
1996 /* prepare argument execution */
1997 off = 0;
1998 foreach(arg, xexpr->named_args)
1999 {
2000 Expr *e = (Expr *) lfirst(arg);
2001
2002 ExecInitExprRec(e, state,
2003 &scratch.d.xmlexpr.named_argvalue[off],
2004 &scratch.d.xmlexpr.named_argnull[off]);
2005 off++;
2006 }
2007
2008 off = 0;
2009 foreach(arg, xexpr->args)
2010 {
2011 Expr *e = (Expr *) lfirst(arg);
2012
2013 ExecInitExprRec(e, state,
2014 &scratch.d.xmlexpr.argvalue[off],
2015 &scratch.d.xmlexpr.argnull[off]);
2016 off++;
2017 }
2018
2019 /* and evaluate the actual XML expression */
2020 ExprEvalPushStep(state, &scratch);
2021 break;
2022 }
2023
2024 case T_NullTest:
2025 {
2026 NullTest *ntest = (NullTest *) node;
2027
2028 if (ntest->nulltesttype == IS_NULL)
2029 {
2030 if (ntest->argisrow)
2031 scratch.opcode = EEOP_NULLTEST_ROWISNULL;
2032 else
2033 scratch.opcode = EEOP_NULLTEST_ISNULL;
2034 }
2035 else if (ntest->nulltesttype == IS_NOT_NULL)
2036 {
2037 if (ntest->argisrow)
2038 scratch.opcode = EEOP_NULLTEST_ROWISNOTNULL;
2039 else
2040 scratch.opcode = EEOP_NULLTEST_ISNOTNULL;
2041 }
2042 else
2043 {
2044 elog(ERROR, "unrecognized nulltesttype: %d",
2045 (int) ntest->nulltesttype);
2046 }
2047 /* initialize cache in case it's a row test */
2048 scratch.d.nulltest_row.rowcache.cacheptr = NULL;
2049
2050 /* first evaluate argument into result variable */
2051 ExecInitExprRec(ntest->arg, state,
2052 resv, resnull);
2053
2054 /* then push the test of that argument */
2055 ExprEvalPushStep(state, &scratch);
2056 break;
2057 }
2058
2059 case T_BooleanTest:
2060 {
2061 BooleanTest *btest = (BooleanTest *) node;
2062
2063 /*
2064 * Evaluate argument, directly into result datum. That's ok,
2065 * because resv/resnull is definitely not used anywhere else,
2066 * and will get overwritten by the below EEOP_BOOLTEST_IS_*
2067 * step.
2068 */
2069 ExecInitExprRec(btest->arg, state, resv, resnull);
2070
2071 switch (btest->booltesttype)
2072 {
2073 case IS_TRUE:
2074 scratch.opcode = EEOP_BOOLTEST_IS_TRUE;
2075 break;
2076 case IS_NOT_TRUE:
2077 scratch.opcode = EEOP_BOOLTEST_IS_NOT_TRUE;
2078 break;
2079 case IS_FALSE:
2080 scratch.opcode = EEOP_BOOLTEST_IS_FALSE;
2081 break;
2082 case IS_NOT_FALSE:
2083 scratch.opcode = EEOP_BOOLTEST_IS_NOT_FALSE;
2084 break;
2085 case IS_UNKNOWN:
2086 /* Same as scalar IS NULL test */
2087 scratch.opcode = EEOP_NULLTEST_ISNULL;
2088 break;
2089 case IS_NOT_UNKNOWN:
2090 /* Same as scalar IS NOT NULL test */
2091 scratch.opcode = EEOP_NULLTEST_ISNOTNULL;
2092 break;
2093 default:
2094 elog(ERROR, "unrecognized booltesttype: %d",
2095 (int) btest->booltesttype);
2096 }
2097
2098 ExprEvalPushStep(state, &scratch);
2099 break;
2100 }
2101
2102 case T_CoerceToDomain:
2103 {
2104 CoerceToDomain *ctest = (CoerceToDomain *) node;
2105
2106 ExecInitCoerceToDomain(&scratch, ctest, state,
2107 resv, resnull);
2108 break;
2109 }
2110
2111 case T_CoerceToDomainValue:
2112 {
2113 /*
2114 * Read from location identified by innermost_domainval. Note
2115 * that innermost_domainval could be NULL, if we're compiling
2116 * a standalone domain check rather than one embedded in a
2117 * larger expression. In that case we must read from
2118 * econtext->domainValue_datum. We'll take care of that
2119 * scenario at runtime.
2120 */
2121 scratch.opcode = EEOP_DOMAIN_TESTVAL;
2122 /* we share instruction union variant with case testval */
2123 scratch.d.casetest.value = state->innermost_domainval;
2124 scratch.d.casetest.isnull = state->innermost_domainnull;
2125
2126 ExprEvalPushStep(state, &scratch);
2127 break;
2128 }
2129
2130 case T_CurrentOfExpr:
2131 {
2132 scratch.opcode = EEOP_CURRENTOFEXPR;
2133 ExprEvalPushStep(state, &scratch);
2134 break;
2135 }
2136
2137 case T_NextValueExpr:
2138 {
2139 NextValueExpr *nve = (NextValueExpr *) node;
2140
2141 scratch.opcode = EEOP_NEXTVALUEEXPR;
2142 scratch.d.nextvalueexpr.seqid = nve->seqid;
2143 scratch.d.nextvalueexpr.seqtypid = nve->typeId;
2144
2145 ExprEvalPushStep(state, &scratch);
2146 break;
2147 }
2148
2149 default:
2150 elog(ERROR, "unrecognized node type: %d",
2151 (int) nodeTag(node));
2152 break;
2153 }
2154 }
2155
2156 /*
2157 * Add another expression evaluation step to ExprState->steps.
2158 *
2159 * Note that this potentially re-allocates es->steps, therefore no pointer
2160 * into that array may be used while the expression is still being built.
2161 */
2162 void
ExprEvalPushStep(ExprState * es,const ExprEvalStep * s)2163 ExprEvalPushStep(ExprState *es, const ExprEvalStep *s)
2164 {
2165 if (es->steps_alloc == 0)
2166 {
2167 es->steps_alloc = 16;
2168 es->steps = palloc(sizeof(ExprEvalStep) * es->steps_alloc);
2169 }
2170 else if (es->steps_alloc == es->steps_len)
2171 {
2172 es->steps_alloc *= 2;
2173 es->steps = repalloc(es->steps,
2174 sizeof(ExprEvalStep) * es->steps_alloc);
2175 }
2176
2177 memcpy(&es->steps[es->steps_len++], s, sizeof(ExprEvalStep));
2178 }
2179
2180 /*
2181 * Perform setup necessary for the evaluation of a function-like expression,
2182 * appending argument evaluation steps to the steps list in *state, and
2183 * setting up *scratch so it is ready to be pushed.
2184 *
2185 * *scratch is not pushed here, so that callers may override the opcode,
2186 * which is useful for function-like cases like DISTINCT.
2187 */
2188 static void
ExecInitFunc(ExprEvalStep * scratch,Expr * node,List * args,Oid funcid,Oid inputcollid,ExprState * state)2189 ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args, Oid funcid,
2190 Oid inputcollid, ExprState *state)
2191 {
2192 int nargs = list_length(args);
2193 AclResult aclresult;
2194 FmgrInfo *flinfo;
2195 FunctionCallInfo fcinfo;
2196 int argno;
2197 ListCell *lc;
2198
2199 /* Check permission to call function */
2200 aclresult = pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE);
2201 if (aclresult != ACLCHECK_OK)
2202 aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(funcid));
2203 InvokeFunctionExecuteHook(funcid);
2204
2205 /*
2206 * Safety check on nargs. Under normal circumstances this should never
2207 * fail, as parser should check sooner. But possibly it might fail if
2208 * server has been compiled with FUNC_MAX_ARGS smaller than some functions
2209 * declared in pg_proc?
2210 */
2211 if (nargs > FUNC_MAX_ARGS)
2212 ereport(ERROR,
2213 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
2214 errmsg_plural("cannot pass more than %d argument to a function",
2215 "cannot pass more than %d arguments to a function",
2216 FUNC_MAX_ARGS,
2217 FUNC_MAX_ARGS)));
2218
2219 /* Allocate function lookup data and parameter workspace for this call */
2220 scratch->d.func.finfo = palloc0(sizeof(FmgrInfo));
2221 scratch->d.func.fcinfo_data = palloc0(SizeForFunctionCallInfo(nargs));
2222 flinfo = scratch->d.func.finfo;
2223 fcinfo = scratch->d.func.fcinfo_data;
2224
2225 /* Set up the primary fmgr lookup information */
2226 fmgr_info(funcid, flinfo);
2227 fmgr_info_set_expr((Node *) node, flinfo);
2228
2229 /* Initialize function call parameter structure too */
2230 InitFunctionCallInfoData(*fcinfo, flinfo,
2231 nargs, inputcollid, NULL, NULL);
2232
2233 /* Keep extra copies of this info to save an indirection at runtime */
2234 scratch->d.func.fn_addr = flinfo->fn_addr;
2235 scratch->d.func.nargs = nargs;
2236
2237 /* We only support non-set functions here */
2238 if (flinfo->fn_retset)
2239 ereport(ERROR,
2240 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2241 errmsg("set-valued function called in context that cannot accept a set"),
2242 state->parent ?
2243 executor_errposition(state->parent->state,
2244 exprLocation((Node *) node)) : 0));
2245
2246 /* Build code to evaluate arguments directly into the fcinfo struct */
2247 argno = 0;
2248 foreach(lc, args)
2249 {
2250 Expr *arg = (Expr *) lfirst(lc);
2251
2252 if (IsA(arg, Const))
2253 {
2254 /*
2255 * Don't evaluate const arguments every round; especially
2256 * interesting for constants in comparisons.
2257 */
2258 Const *con = (Const *) arg;
2259
2260 fcinfo->args[argno].value = con->constvalue;
2261 fcinfo->args[argno].isnull = con->constisnull;
2262 }
2263 else
2264 {
2265 ExecInitExprRec(arg, state,
2266 &fcinfo->args[argno].value,
2267 &fcinfo->args[argno].isnull);
2268 }
2269 argno++;
2270 }
2271
2272 /* Insert appropriate opcode depending on strictness and stats level */
2273 if (pgstat_track_functions <= flinfo->fn_stats)
2274 {
2275 if (flinfo->fn_strict && nargs > 0)
2276 scratch->opcode = EEOP_FUNCEXPR_STRICT;
2277 else
2278 scratch->opcode = EEOP_FUNCEXPR;
2279 }
2280 else
2281 {
2282 if (flinfo->fn_strict && nargs > 0)
2283 scratch->opcode = EEOP_FUNCEXPR_STRICT_FUSAGE;
2284 else
2285 scratch->opcode = EEOP_FUNCEXPR_FUSAGE;
2286 }
2287 }
2288
2289 /*
2290 * Add expression steps deforming the ExprState's inner/outer/scan slots
2291 * as much as required by the expression.
2292 */
2293 static void
ExecInitExprSlots(ExprState * state,Node * node)2294 ExecInitExprSlots(ExprState *state, Node *node)
2295 {
2296 LastAttnumInfo info = {0, 0, 0};
2297
2298 /*
2299 * Figure out which attributes we're going to need.
2300 */
2301 get_last_attnums_walker(node, &info);
2302
2303 ExecPushExprSlots(state, &info);
2304 }
2305
2306 /*
2307 * Add steps deforming the ExprState's inner/out/scan slots as much as
2308 * indicated by info. This is useful when building an ExprState covering more
2309 * than one expression.
2310 */
2311 static void
ExecPushExprSlots(ExprState * state,LastAttnumInfo * info)2312 ExecPushExprSlots(ExprState *state, LastAttnumInfo *info)
2313 {
2314 ExprEvalStep scratch = {0};
2315
2316 scratch.resvalue = NULL;
2317 scratch.resnull = NULL;
2318
2319 /* Emit steps as needed */
2320 if (info->last_inner > 0)
2321 {
2322 scratch.opcode = EEOP_INNER_FETCHSOME;
2323 scratch.d.fetch.last_var = info->last_inner;
2324 scratch.d.fetch.fixed = false;
2325 scratch.d.fetch.kind = NULL;
2326 scratch.d.fetch.known_desc = NULL;
2327 if (ExecComputeSlotInfo(state, &scratch))
2328 ExprEvalPushStep(state, &scratch);
2329 }
2330 if (info->last_outer > 0)
2331 {
2332 scratch.opcode = EEOP_OUTER_FETCHSOME;
2333 scratch.d.fetch.last_var = info->last_outer;
2334 scratch.d.fetch.fixed = false;
2335 scratch.d.fetch.kind = NULL;
2336 scratch.d.fetch.known_desc = NULL;
2337 if (ExecComputeSlotInfo(state, &scratch))
2338 ExprEvalPushStep(state, &scratch);
2339 }
2340 if (info->last_scan > 0)
2341 {
2342 scratch.opcode = EEOP_SCAN_FETCHSOME;
2343 scratch.d.fetch.last_var = info->last_scan;
2344 scratch.d.fetch.fixed = false;
2345 scratch.d.fetch.kind = NULL;
2346 scratch.d.fetch.known_desc = NULL;
2347 if (ExecComputeSlotInfo(state, &scratch))
2348 ExprEvalPushStep(state, &scratch);
2349 }
2350 }
2351
2352 /*
2353 * get_last_attnums_walker: expression walker for ExecInitExprSlots
2354 */
2355 static bool
get_last_attnums_walker(Node * node,LastAttnumInfo * info)2356 get_last_attnums_walker(Node *node, LastAttnumInfo *info)
2357 {
2358 if (node == NULL)
2359 return false;
2360 if (IsA(node, Var))
2361 {
2362 Var *variable = (Var *) node;
2363 AttrNumber attnum = variable->varattno;
2364
2365 switch (variable->varno)
2366 {
2367 case INNER_VAR:
2368 info->last_inner = Max(info->last_inner, attnum);
2369 break;
2370
2371 case OUTER_VAR:
2372 info->last_outer = Max(info->last_outer, attnum);
2373 break;
2374
2375 /* INDEX_VAR is handled by default case */
2376
2377 default:
2378 info->last_scan = Max(info->last_scan, attnum);
2379 break;
2380 }
2381 return false;
2382 }
2383
2384 /*
2385 * Don't examine the arguments or filters of Aggrefs or WindowFuncs,
2386 * because those do not represent expressions to be evaluated within the
2387 * calling expression's econtext. GroupingFunc arguments are never
2388 * evaluated at all.
2389 */
2390 if (IsA(node, Aggref))
2391 return false;
2392 if (IsA(node, WindowFunc))
2393 return false;
2394 if (IsA(node, GroupingFunc))
2395 return false;
2396 return expression_tree_walker(node, get_last_attnums_walker,
2397 (void *) info);
2398 }
2399
2400 /*
2401 * Compute additional information for EEOP_*_FETCHSOME ops.
2402 *
2403 * The goal is to determine whether a slot is 'fixed', that is, every
2404 * evaluation of the expression will have the same type of slot, with an
2405 * equivalent descriptor.
2406 *
2407 * Returns true if the deforming step is required, false otherwise.
2408 */
2409 static bool
ExecComputeSlotInfo(ExprState * state,ExprEvalStep * op)2410 ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op)
2411 {
2412 PlanState *parent = state->parent;
2413 TupleDesc desc = NULL;
2414 const TupleTableSlotOps *tts_ops = NULL;
2415 bool isfixed = false;
2416 ExprEvalOp opcode = op->opcode;
2417
2418 Assert(opcode == EEOP_INNER_FETCHSOME ||
2419 opcode == EEOP_OUTER_FETCHSOME ||
2420 opcode == EEOP_SCAN_FETCHSOME);
2421
2422 if (op->d.fetch.known_desc != NULL)
2423 {
2424 desc = op->d.fetch.known_desc;
2425 tts_ops = op->d.fetch.kind;
2426 isfixed = op->d.fetch.kind != NULL;
2427 }
2428 else if (!parent)
2429 {
2430 isfixed = false;
2431 }
2432 else if (opcode == EEOP_INNER_FETCHSOME)
2433 {
2434 PlanState *is = innerPlanState(parent);
2435
2436 if (parent->inneropsset && !parent->inneropsfixed)
2437 {
2438 isfixed = false;
2439 }
2440 else if (parent->inneropsset && parent->innerops)
2441 {
2442 isfixed = true;
2443 tts_ops = parent->innerops;
2444 desc = ExecGetResultType(is);
2445 }
2446 else if (is)
2447 {
2448 tts_ops = ExecGetResultSlotOps(is, &isfixed);
2449 desc = ExecGetResultType(is);
2450 }
2451 }
2452 else if (opcode == EEOP_OUTER_FETCHSOME)
2453 {
2454 PlanState *os = outerPlanState(parent);
2455
2456 if (parent->outeropsset && !parent->outeropsfixed)
2457 {
2458 isfixed = false;
2459 }
2460 else if (parent->outeropsset && parent->outerops)
2461 {
2462 isfixed = true;
2463 tts_ops = parent->outerops;
2464 desc = ExecGetResultType(os);
2465 }
2466 else if (os)
2467 {
2468 tts_ops = ExecGetResultSlotOps(os, &isfixed);
2469 desc = ExecGetResultType(os);
2470 }
2471 }
2472 else if (opcode == EEOP_SCAN_FETCHSOME)
2473 {
2474 desc = parent->scandesc;
2475
2476 if (parent->scanops)
2477 tts_ops = parent->scanops;
2478
2479 if (parent->scanopsset)
2480 isfixed = parent->scanopsfixed;
2481 }
2482
2483 if (isfixed && desc != NULL && tts_ops != NULL)
2484 {
2485 op->d.fetch.fixed = true;
2486 op->d.fetch.kind = tts_ops;
2487 op->d.fetch.known_desc = desc;
2488 }
2489 else
2490 {
2491 op->d.fetch.fixed = false;
2492 op->d.fetch.kind = NULL;
2493 op->d.fetch.known_desc = NULL;
2494 }
2495
2496 /* if the slot is known to always virtual we never need to deform */
2497 if (op->d.fetch.fixed && op->d.fetch.kind == &TTSOpsVirtual)
2498 return false;
2499
2500 return true;
2501 }
2502
2503 /*
2504 * Prepare step for the evaluation of a whole-row variable.
2505 * The caller still has to push the step.
2506 */
2507 static void
ExecInitWholeRowVar(ExprEvalStep * scratch,Var * variable,ExprState * state)2508 ExecInitWholeRowVar(ExprEvalStep *scratch, Var *variable, ExprState *state)
2509 {
2510 PlanState *parent = state->parent;
2511
2512 /* fill in all but the target */
2513 scratch->opcode = EEOP_WHOLEROW;
2514 scratch->d.wholerow.var = variable;
2515 scratch->d.wholerow.first = true;
2516 scratch->d.wholerow.slow = false;
2517 scratch->d.wholerow.tupdesc = NULL; /* filled at runtime */
2518 scratch->d.wholerow.junkFilter = NULL;
2519
2520 /*
2521 * If the input tuple came from a subquery, it might contain "resjunk"
2522 * columns (such as GROUP BY or ORDER BY columns), which we don't want to
2523 * keep in the whole-row result. We can get rid of such columns by
2524 * passing the tuple through a JunkFilter --- but to make one, we have to
2525 * lay our hands on the subquery's targetlist. Fortunately, there are not
2526 * very many cases where this can happen, and we can identify all of them
2527 * by examining our parent PlanState. We assume this is not an issue in
2528 * standalone expressions that don't have parent plans. (Whole-row Vars
2529 * can occur in such expressions, but they will always be referencing
2530 * table rows.)
2531 */
2532 if (parent)
2533 {
2534 PlanState *subplan = NULL;
2535
2536 switch (nodeTag(parent))
2537 {
2538 case T_SubqueryScanState:
2539 subplan = ((SubqueryScanState *) parent)->subplan;
2540 break;
2541 case T_CteScanState:
2542 subplan = ((CteScanState *) parent)->cteplanstate;
2543 break;
2544 default:
2545 break;
2546 }
2547
2548 if (subplan)
2549 {
2550 bool junk_filter_needed = false;
2551 ListCell *tlist;
2552
2553 /* Detect whether subplan tlist actually has any junk columns */
2554 foreach(tlist, subplan->plan->targetlist)
2555 {
2556 TargetEntry *tle = (TargetEntry *) lfirst(tlist);
2557
2558 if (tle->resjunk)
2559 {
2560 junk_filter_needed = true;
2561 break;
2562 }
2563 }
2564
2565 /* If so, build the junkfilter now */
2566 if (junk_filter_needed)
2567 {
2568 scratch->d.wholerow.junkFilter =
2569 ExecInitJunkFilter(subplan->plan->targetlist,
2570 ExecInitExtraTupleSlot(parent->state, NULL,
2571 &TTSOpsVirtual));
2572 }
2573 }
2574 }
2575 }
2576
2577 /*
2578 * Prepare evaluation of a SubscriptingRef expression.
2579 */
2580 static void
ExecInitSubscriptingRef(ExprEvalStep * scratch,SubscriptingRef * sbsref,ExprState * state,Datum * resv,bool * resnull)2581 ExecInitSubscriptingRef(ExprEvalStep *scratch, SubscriptingRef *sbsref,
2582 ExprState *state, Datum *resv, bool *resnull)
2583 {
2584 bool isAssignment = (sbsref->refassgnexpr != NULL);
2585 SubscriptingRefState *sbsrefstate = palloc0(sizeof(SubscriptingRefState));
2586 List *adjust_jumps = NIL;
2587 ListCell *lc;
2588 int i;
2589
2590 /* Fill constant fields of SubscriptingRefState */
2591 sbsrefstate->isassignment = isAssignment;
2592 sbsrefstate->refelemtype = sbsref->refelemtype;
2593 sbsrefstate->refattrlength = get_typlen(sbsref->refcontainertype);
2594 get_typlenbyvalalign(sbsref->refelemtype,
2595 &sbsrefstate->refelemlength,
2596 &sbsrefstate->refelembyval,
2597 &sbsrefstate->refelemalign);
2598
2599 /*
2600 * Evaluate array input. It's safe to do so into resv/resnull, because we
2601 * won't use that as target for any of the other subexpressions, and it'll
2602 * be overwritten by the final EEOP_SBSREF_FETCH/ASSIGN step, which is
2603 * pushed last.
2604 */
2605 ExecInitExprRec(sbsref->refexpr, state, resv, resnull);
2606
2607 /*
2608 * If refexpr yields NULL, and it's a fetch, then result is NULL. We can
2609 * implement this with just JUMP_IF_NULL, since we evaluated the array
2610 * into the desired target location.
2611 */
2612 if (!isAssignment)
2613 {
2614 scratch->opcode = EEOP_JUMP_IF_NULL;
2615 scratch->d.jump.jumpdone = -1; /* adjust later */
2616 ExprEvalPushStep(state, scratch);
2617 adjust_jumps = lappend_int(adjust_jumps,
2618 state->steps_len - 1);
2619 }
2620
2621 /* Verify subscript list lengths are within limit */
2622 if (list_length(sbsref->refupperindexpr) > MAXDIM)
2623 ereport(ERROR,
2624 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2625 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
2626 list_length(sbsref->refupperindexpr), MAXDIM)));
2627
2628 if (list_length(sbsref->reflowerindexpr) > MAXDIM)
2629 ereport(ERROR,
2630 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2631 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
2632 list_length(sbsref->reflowerindexpr), MAXDIM)));
2633
2634 /* Evaluate upper subscripts */
2635 i = 0;
2636 foreach(lc, sbsref->refupperindexpr)
2637 {
2638 Expr *e = (Expr *) lfirst(lc);
2639
2640 /* When slicing, individual subscript bounds can be omitted */
2641 if (!e)
2642 {
2643 sbsrefstate->upperprovided[i] = false;
2644 i++;
2645 continue;
2646 }
2647
2648 sbsrefstate->upperprovided[i] = true;
2649
2650 /* Each subscript is evaluated into subscriptvalue/subscriptnull */
2651 ExecInitExprRec(e, state,
2652 &sbsrefstate->subscriptvalue, &sbsrefstate->subscriptnull);
2653
2654 /* ... and then SBSREF_SUBSCRIPT saves it into step's workspace */
2655 scratch->opcode = EEOP_SBSREF_SUBSCRIPT;
2656 scratch->d.sbsref_subscript.state = sbsrefstate;
2657 scratch->d.sbsref_subscript.off = i;
2658 scratch->d.sbsref_subscript.isupper = true;
2659 scratch->d.sbsref_subscript.jumpdone = -1; /* adjust later */
2660 ExprEvalPushStep(state, scratch);
2661 adjust_jumps = lappend_int(adjust_jumps,
2662 state->steps_len - 1);
2663 i++;
2664 }
2665 sbsrefstate->numupper = i;
2666
2667 /* Evaluate lower subscripts similarly */
2668 i = 0;
2669 foreach(lc, sbsref->reflowerindexpr)
2670 {
2671 Expr *e = (Expr *) lfirst(lc);
2672
2673 /* When slicing, individual subscript bounds can be omitted */
2674 if (!e)
2675 {
2676 sbsrefstate->lowerprovided[i] = false;
2677 i++;
2678 continue;
2679 }
2680
2681 sbsrefstate->lowerprovided[i] = true;
2682
2683 /* Each subscript is evaluated into subscriptvalue/subscriptnull */
2684 ExecInitExprRec(e, state,
2685 &sbsrefstate->subscriptvalue, &sbsrefstate->subscriptnull);
2686
2687 /* ... and then SBSREF_SUBSCRIPT saves it into step's workspace */
2688 scratch->opcode = EEOP_SBSREF_SUBSCRIPT;
2689 scratch->d.sbsref_subscript.state = sbsrefstate;
2690 scratch->d.sbsref_subscript.off = i;
2691 scratch->d.sbsref_subscript.isupper = false;
2692 scratch->d.sbsref_subscript.jumpdone = -1; /* adjust later */
2693 ExprEvalPushStep(state, scratch);
2694 adjust_jumps = lappend_int(adjust_jumps,
2695 state->steps_len - 1);
2696 i++;
2697 }
2698 sbsrefstate->numlower = i;
2699
2700 /* Should be impossible if parser is sane, but check anyway: */
2701 if (sbsrefstate->numlower != 0 &&
2702 sbsrefstate->numupper != sbsrefstate->numlower)
2703 elog(ERROR, "upper and lower index lists are not same length");
2704
2705 if (isAssignment)
2706 {
2707 Datum *save_innermost_caseval;
2708 bool *save_innermost_casenull;
2709
2710 /*
2711 * We might have a nested-assignment situation, in which the
2712 * refassgnexpr is itself a FieldStore or SubscriptingRef that needs
2713 * to obtain and modify the previous value of the array element or
2714 * slice being replaced. If so, we have to extract that value from
2715 * the array and pass it down via the CaseTestExpr mechanism. It's
2716 * safe to reuse the CASE mechanism because there cannot be a CASE
2717 * between here and where the value would be needed, and an array
2718 * assignment can't be within a CASE either. (So saving and restoring
2719 * innermost_caseval is just paranoia, but let's do it anyway.)
2720 *
2721 * Since fetching the old element might be a nontrivial expense, do it
2722 * only if the argument actually needs it.
2723 */
2724 if (isAssignmentIndirectionExpr(sbsref->refassgnexpr))
2725 {
2726 scratch->opcode = EEOP_SBSREF_OLD;
2727 scratch->d.sbsref.state = sbsrefstate;
2728 ExprEvalPushStep(state, scratch);
2729 }
2730
2731 /* SBSREF_OLD puts extracted value into prevvalue/prevnull */
2732 save_innermost_caseval = state->innermost_caseval;
2733 save_innermost_casenull = state->innermost_casenull;
2734 state->innermost_caseval = &sbsrefstate->prevvalue;
2735 state->innermost_casenull = &sbsrefstate->prevnull;
2736
2737 /* evaluate replacement value into replacevalue/replacenull */
2738 ExecInitExprRec(sbsref->refassgnexpr, state,
2739 &sbsrefstate->replacevalue, &sbsrefstate->replacenull);
2740
2741 state->innermost_caseval = save_innermost_caseval;
2742 state->innermost_casenull = save_innermost_casenull;
2743
2744 /* and perform the assignment */
2745 scratch->opcode = EEOP_SBSREF_ASSIGN;
2746 scratch->d.sbsref.state = sbsrefstate;
2747 ExprEvalPushStep(state, scratch);
2748
2749 }
2750 else
2751 {
2752 /* array fetch is much simpler */
2753 scratch->opcode = EEOP_SBSREF_FETCH;
2754 scratch->d.sbsref.state = sbsrefstate;
2755 ExprEvalPushStep(state, scratch);
2756
2757 }
2758
2759 /* adjust jump targets */
2760 foreach(lc, adjust_jumps)
2761 {
2762 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
2763
2764 if (as->opcode == EEOP_SBSREF_SUBSCRIPT)
2765 {
2766 Assert(as->d.sbsref_subscript.jumpdone == -1);
2767 as->d.sbsref_subscript.jumpdone = state->steps_len;
2768 }
2769 else
2770 {
2771 Assert(as->opcode == EEOP_JUMP_IF_NULL);
2772 Assert(as->d.jump.jumpdone == -1);
2773 as->d.jump.jumpdone = state->steps_len;
2774 }
2775 }
2776 }
2777
2778 /*
2779 * Helper for preparing SubscriptingRef expressions for evaluation: is expr
2780 * a nested FieldStore or SubscriptingRef that needs the old element value
2781 * passed down?
2782 *
2783 * (We could use this in FieldStore too, but in that case passing the old
2784 * value is so cheap there's no need.)
2785 *
2786 * Note: it might seem that this needs to recurse, but in most cases it does
2787 * not; the CaseTestExpr, if any, will be directly the arg or refexpr of the
2788 * top-level node. Nested-assignment situations give rise to expression
2789 * trees in which each level of assignment has its own CaseTestExpr, and the
2790 * recursive structure appears within the newvals or refassgnexpr field.
2791 * There is an exception, though: if the array is an array-of-domain, we will
2792 * have a CoerceToDomain as the refassgnexpr, and we need to be able to look
2793 * through that.
2794 */
2795 static bool
isAssignmentIndirectionExpr(Expr * expr)2796 isAssignmentIndirectionExpr(Expr *expr)
2797 {
2798 if (expr == NULL)
2799 return false; /* just paranoia */
2800 if (IsA(expr, FieldStore))
2801 {
2802 FieldStore *fstore = (FieldStore *) expr;
2803
2804 if (fstore->arg && IsA(fstore->arg, CaseTestExpr))
2805 return true;
2806 }
2807 else if (IsA(expr, SubscriptingRef))
2808 {
2809 SubscriptingRef *sbsRef = (SubscriptingRef *) expr;
2810
2811 if (sbsRef->refexpr && IsA(sbsRef->refexpr, CaseTestExpr))
2812 return true;
2813 }
2814 else if (IsA(expr, CoerceToDomain))
2815 {
2816 CoerceToDomain *cd = (CoerceToDomain *) expr;
2817
2818 return isAssignmentIndirectionExpr(cd->arg);
2819 }
2820 return false;
2821 }
2822
2823 /*
2824 * Prepare evaluation of a CoerceToDomain expression.
2825 */
2826 static void
ExecInitCoerceToDomain(ExprEvalStep * scratch,CoerceToDomain * ctest,ExprState * state,Datum * resv,bool * resnull)2827 ExecInitCoerceToDomain(ExprEvalStep *scratch, CoerceToDomain *ctest,
2828 ExprState *state, Datum *resv, bool *resnull)
2829 {
2830 DomainConstraintRef *constraint_ref;
2831 Datum *domainval = NULL;
2832 bool *domainnull = NULL;
2833 ListCell *l;
2834
2835 scratch->d.domaincheck.resulttype = ctest->resulttype;
2836 /* we'll allocate workspace only if needed */
2837 scratch->d.domaincheck.checkvalue = NULL;
2838 scratch->d.domaincheck.checknull = NULL;
2839
2840 /*
2841 * Evaluate argument - it's fine to directly store it into resv/resnull,
2842 * if there's constraint failures there'll be errors, otherwise it's what
2843 * needs to be returned.
2844 */
2845 ExecInitExprRec(ctest->arg, state, resv, resnull);
2846
2847 /*
2848 * Note: if the argument is of varlena type, it could be a R/W expanded
2849 * object. We want to return the R/W pointer as the final result, but we
2850 * have to pass a R/O pointer as the value to be tested by any functions
2851 * in check expressions. We don't bother to emit a MAKE_READONLY step
2852 * unless there's actually at least one check expression, though. Until
2853 * we've tested that, domainval/domainnull are NULL.
2854 */
2855
2856 /*
2857 * Collect the constraints associated with the domain.
2858 *
2859 * Note: before PG v10 we'd recheck the set of constraints during each
2860 * evaluation of the expression. Now we bake them into the ExprState
2861 * during executor initialization. That means we don't need typcache.c to
2862 * provide compiled exprs.
2863 */
2864 constraint_ref = (DomainConstraintRef *)
2865 palloc(sizeof(DomainConstraintRef));
2866 InitDomainConstraintRef(ctest->resulttype,
2867 constraint_ref,
2868 CurrentMemoryContext,
2869 false);
2870
2871 /*
2872 * Compile code to check each domain constraint. NOTNULL constraints can
2873 * just be applied on the resv/resnull value, but for CHECK constraints we
2874 * need more pushups.
2875 */
2876 foreach(l, constraint_ref->constraints)
2877 {
2878 DomainConstraintState *con = (DomainConstraintState *) lfirst(l);
2879 Datum *save_innermost_domainval;
2880 bool *save_innermost_domainnull;
2881
2882 scratch->d.domaincheck.constraintname = con->name;
2883
2884 switch (con->constrainttype)
2885 {
2886 case DOM_CONSTRAINT_NOTNULL:
2887 scratch->opcode = EEOP_DOMAIN_NOTNULL;
2888 ExprEvalPushStep(state, scratch);
2889 break;
2890 case DOM_CONSTRAINT_CHECK:
2891 /* Allocate workspace for CHECK output if we didn't yet */
2892 if (scratch->d.domaincheck.checkvalue == NULL)
2893 {
2894 scratch->d.domaincheck.checkvalue =
2895 (Datum *) palloc(sizeof(Datum));
2896 scratch->d.domaincheck.checknull =
2897 (bool *) palloc(sizeof(bool));
2898 }
2899
2900 /*
2901 * If first time through, determine where CoerceToDomainValue
2902 * nodes should read from.
2903 */
2904 if (domainval == NULL)
2905 {
2906 /*
2907 * Since value might be read multiple times, force to R/O
2908 * - but only if it could be an expanded datum.
2909 */
2910 if (get_typlen(ctest->resulttype) == -1)
2911 {
2912 ExprEvalStep scratch2 = {0};
2913
2914 /* Yes, so make output workspace for MAKE_READONLY */
2915 domainval = (Datum *) palloc(sizeof(Datum));
2916 domainnull = (bool *) palloc(sizeof(bool));
2917
2918 /* Emit MAKE_READONLY */
2919 scratch2.opcode = EEOP_MAKE_READONLY;
2920 scratch2.resvalue = domainval;
2921 scratch2.resnull = domainnull;
2922 scratch2.d.make_readonly.value = resv;
2923 scratch2.d.make_readonly.isnull = resnull;
2924 ExprEvalPushStep(state, &scratch2);
2925 }
2926 else
2927 {
2928 /* No, so it's fine to read from resv/resnull */
2929 domainval = resv;
2930 domainnull = resnull;
2931 }
2932 }
2933
2934 /*
2935 * Set up value to be returned by CoerceToDomainValue nodes.
2936 * We must save and restore innermost_domainval/null fields,
2937 * in case this node is itself within a check expression for
2938 * another domain.
2939 */
2940 save_innermost_domainval = state->innermost_domainval;
2941 save_innermost_domainnull = state->innermost_domainnull;
2942 state->innermost_domainval = domainval;
2943 state->innermost_domainnull = domainnull;
2944
2945 /* evaluate check expression value */
2946 ExecInitExprRec(con->check_expr, state,
2947 scratch->d.domaincheck.checkvalue,
2948 scratch->d.domaincheck.checknull);
2949
2950 state->innermost_domainval = save_innermost_domainval;
2951 state->innermost_domainnull = save_innermost_domainnull;
2952
2953 /* now test result */
2954 scratch->opcode = EEOP_DOMAIN_CHECK;
2955 ExprEvalPushStep(state, scratch);
2956
2957 break;
2958 default:
2959 elog(ERROR, "unrecognized constraint type: %d",
2960 (int) con->constrainttype);
2961 break;
2962 }
2963 }
2964 }
2965
2966 /*
2967 * Build transition/combine function invocations for all aggregate transition
2968 * / combination function invocations in a grouping sets phase. This has to
2969 * invoke all sort based transitions in a phase (if doSort is true), all hash
2970 * based transitions (if doHash is true), or both (both true).
2971 *
2972 * The resulting expression will, for each set of transition values, first
2973 * check for filters, evaluate aggregate input, check that that input is not
2974 * NULL for a strict transition function, and then finally invoke the
2975 * transition for each of the concurrently computed grouping sets.
2976 *
2977 * If nullcheck is true, the generated code will check for a NULL pointer to
2978 * the array of AggStatePerGroup, and skip evaluation if so.
2979 */
2980 ExprState *
ExecBuildAggTrans(AggState * aggstate,AggStatePerPhase phase,bool doSort,bool doHash,bool nullcheck)2981 ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
2982 bool doSort, bool doHash, bool nullcheck)
2983 {
2984 ExprState *state = makeNode(ExprState);
2985 PlanState *parent = &aggstate->ss.ps;
2986 ExprEvalStep scratch = {0};
2987 bool isCombine = DO_AGGSPLIT_COMBINE(aggstate->aggsplit);
2988 LastAttnumInfo deform = {0, 0, 0};
2989
2990 state->expr = (Expr *) aggstate;
2991 state->parent = parent;
2992
2993 scratch.resvalue = &state->resvalue;
2994 scratch.resnull = &state->resnull;
2995
2996 /*
2997 * First figure out which slots, and how many columns from each, we're
2998 * going to need.
2999 */
3000 for (int transno = 0; transno < aggstate->numtrans; transno++)
3001 {
3002 AggStatePerTrans pertrans = &aggstate->pertrans[transno];
3003
3004 get_last_attnums_walker((Node *) pertrans->aggref->aggdirectargs,
3005 &deform);
3006 get_last_attnums_walker((Node *) pertrans->aggref->args,
3007 &deform);
3008 get_last_attnums_walker((Node *) pertrans->aggref->aggorder,
3009 &deform);
3010 get_last_attnums_walker((Node *) pertrans->aggref->aggdistinct,
3011 &deform);
3012 get_last_attnums_walker((Node *) pertrans->aggref->aggfilter,
3013 &deform);
3014 }
3015 ExecPushExprSlots(state, &deform);
3016
3017 /*
3018 * Emit instructions for each transition value / grouping set combination.
3019 */
3020 for (int transno = 0; transno < aggstate->numtrans; transno++)
3021 {
3022 AggStatePerTrans pertrans = &aggstate->pertrans[transno];
3023 FunctionCallInfo trans_fcinfo = pertrans->transfn_fcinfo;
3024 List *adjust_bailout = NIL;
3025 NullableDatum *strictargs = NULL;
3026 bool *strictnulls = NULL;
3027 int argno;
3028 ListCell *bail;
3029
3030 /*
3031 * If filter present, emit. Do so before evaluating the input, to
3032 * avoid potentially unneeded computations, or even worse, unintended
3033 * side-effects. When combining, all the necessary filtering has
3034 * already been done.
3035 */
3036 if (pertrans->aggref->aggfilter && !isCombine)
3037 {
3038 /* evaluate filter expression */
3039 ExecInitExprRec(pertrans->aggref->aggfilter, state,
3040 &state->resvalue, &state->resnull);
3041 /* and jump out if false */
3042 scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
3043 scratch.d.jump.jumpdone = -1; /* adjust later */
3044 ExprEvalPushStep(state, &scratch);
3045 adjust_bailout = lappend_int(adjust_bailout,
3046 state->steps_len - 1);
3047 }
3048
3049 /*
3050 * Evaluate arguments to aggregate/combine function.
3051 */
3052 argno = 0;
3053 if (isCombine)
3054 {
3055 /*
3056 * Combining two aggregate transition values. Instead of directly
3057 * coming from a tuple the input is a, potentially deserialized,
3058 * transition value.
3059 */
3060 TargetEntry *source_tle;
3061
3062 Assert(pertrans->numSortCols == 0);
3063 Assert(list_length(pertrans->aggref->args) == 1);
3064
3065 strictargs = trans_fcinfo->args + 1;
3066 source_tle = (TargetEntry *) linitial(pertrans->aggref->args);
3067
3068 /*
3069 * deserialfn_oid will be set if we must deserialize the input
3070 * state before calling the combine function.
3071 */
3072 if (!OidIsValid(pertrans->deserialfn_oid))
3073 {
3074 /*
3075 * Start from 1, since the 0th arg will be the transition
3076 * value
3077 */
3078 ExecInitExprRec(source_tle->expr, state,
3079 &trans_fcinfo->args[argno + 1].value,
3080 &trans_fcinfo->args[argno + 1].isnull);
3081 }
3082 else
3083 {
3084 FunctionCallInfo ds_fcinfo = pertrans->deserialfn_fcinfo;
3085
3086 /* evaluate argument */
3087 ExecInitExprRec(source_tle->expr, state,
3088 &ds_fcinfo->args[0].value,
3089 &ds_fcinfo->args[0].isnull);
3090
3091 /* Dummy second argument for type-safety reasons */
3092 ds_fcinfo->args[1].value = PointerGetDatum(NULL);
3093 ds_fcinfo->args[1].isnull = false;
3094
3095 /*
3096 * Don't call a strict deserialization function with NULL
3097 * input
3098 */
3099 if (pertrans->deserialfn.fn_strict)
3100 scratch.opcode = EEOP_AGG_STRICT_DESERIALIZE;
3101 else
3102 scratch.opcode = EEOP_AGG_DESERIALIZE;
3103
3104 scratch.d.agg_deserialize.fcinfo_data = ds_fcinfo;
3105 scratch.d.agg_deserialize.jumpnull = -1; /* adjust later */
3106 scratch.resvalue = &trans_fcinfo->args[argno + 1].value;
3107 scratch.resnull = &trans_fcinfo->args[argno + 1].isnull;
3108
3109 ExprEvalPushStep(state, &scratch);
3110 /* don't add an adjustment unless the function is strict */
3111 if (pertrans->deserialfn.fn_strict)
3112 adjust_bailout = lappend_int(adjust_bailout,
3113 state->steps_len - 1);
3114
3115 /* restore normal settings of scratch fields */
3116 scratch.resvalue = &state->resvalue;
3117 scratch.resnull = &state->resnull;
3118 }
3119 argno++;
3120 }
3121 else if (pertrans->numSortCols == 0)
3122 {
3123 ListCell *arg;
3124
3125 /*
3126 * Normal transition function without ORDER BY / DISTINCT.
3127 */
3128 strictargs = trans_fcinfo->args + 1;
3129
3130 foreach(arg, pertrans->aggref->args)
3131 {
3132 TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
3133
3134 /*
3135 * Start from 1, since the 0th arg will be the transition
3136 * value
3137 */
3138 ExecInitExprRec(source_tle->expr, state,
3139 &trans_fcinfo->args[argno + 1].value,
3140 &trans_fcinfo->args[argno + 1].isnull);
3141 argno++;
3142 }
3143 }
3144 else if (pertrans->numInputs == 1)
3145 {
3146 /*
3147 * DISTINCT and/or ORDER BY case, with a single column sorted on.
3148 */
3149 TargetEntry *source_tle =
3150 (TargetEntry *) linitial(pertrans->aggref->args);
3151
3152 Assert(list_length(pertrans->aggref->args) == 1);
3153
3154 ExecInitExprRec(source_tle->expr, state,
3155 &state->resvalue,
3156 &state->resnull);
3157 strictnulls = &state->resnull;
3158 argno++;
3159 }
3160 else
3161 {
3162 /*
3163 * DISTINCT and/or ORDER BY case, with multiple columns sorted on.
3164 */
3165 Datum *values = pertrans->sortslot->tts_values;
3166 bool *nulls = pertrans->sortslot->tts_isnull;
3167 ListCell *arg;
3168
3169 strictnulls = nulls;
3170
3171 foreach(arg, pertrans->aggref->args)
3172 {
3173 TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
3174
3175 ExecInitExprRec(source_tle->expr, state,
3176 &values[argno], &nulls[argno]);
3177 argno++;
3178 }
3179 }
3180 Assert(pertrans->numInputs == argno);
3181
3182 /*
3183 * For a strict transfn, nothing happens when there's a NULL input; we
3184 * just keep the prior transValue. This is true for both plain and
3185 * sorted/distinct aggregates.
3186 */
3187 if (trans_fcinfo->flinfo->fn_strict && pertrans->numTransInputs > 0)
3188 {
3189 if (strictnulls)
3190 scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_NULLS;
3191 else
3192 scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_ARGS;
3193 scratch.d.agg_strict_input_check.nulls = strictnulls;
3194 scratch.d.agg_strict_input_check.args = strictargs;
3195 scratch.d.agg_strict_input_check.jumpnull = -1; /* adjust later */
3196 scratch.d.agg_strict_input_check.nargs = pertrans->numTransInputs;
3197 ExprEvalPushStep(state, &scratch);
3198 adjust_bailout = lappend_int(adjust_bailout,
3199 state->steps_len - 1);
3200 }
3201
3202 /*
3203 * Call transition function (once for each concurrently evaluated
3204 * grouping set). Do so for both sort and hash based computations, as
3205 * applicable.
3206 */
3207 if (doSort)
3208 {
3209 int processGroupingSets = Max(phase->numsets, 1);
3210 int setoff = 0;
3211
3212 for (int setno = 0; setno < processGroupingSets; setno++)
3213 {
3214 ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
3215 pertrans, transno, setno, setoff, false,
3216 nullcheck);
3217 setoff++;
3218 }
3219 }
3220
3221 if (doHash)
3222 {
3223 int numHashes = aggstate->num_hashes;
3224 int setoff;
3225
3226 /* in MIXED mode, there'll be preceding transition values */
3227 if (aggstate->aggstrategy != AGG_HASHED)
3228 setoff = aggstate->maxsets;
3229 else
3230 setoff = 0;
3231
3232 for (int setno = 0; setno < numHashes; setno++)
3233 {
3234 ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
3235 pertrans, transno, setno, setoff, true,
3236 nullcheck);
3237 setoff++;
3238 }
3239 }
3240
3241 /* adjust early bail out jump target(s) */
3242 foreach(bail, adjust_bailout)
3243 {
3244 ExprEvalStep *as = &state->steps[lfirst_int(bail)];
3245
3246 if (as->opcode == EEOP_JUMP_IF_NOT_TRUE)
3247 {
3248 Assert(as->d.jump.jumpdone == -1);
3249 as->d.jump.jumpdone = state->steps_len;
3250 }
3251 else if (as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS ||
3252 as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
3253 {
3254 Assert(as->d.agg_strict_input_check.jumpnull == -1);
3255 as->d.agg_strict_input_check.jumpnull = state->steps_len;
3256 }
3257 else if (as->opcode == EEOP_AGG_STRICT_DESERIALIZE)
3258 {
3259 Assert(as->d.agg_deserialize.jumpnull == -1);
3260 as->d.agg_deserialize.jumpnull = state->steps_len;
3261 }
3262 else
3263 Assert(false);
3264 }
3265 }
3266
3267 scratch.resvalue = NULL;
3268 scratch.resnull = NULL;
3269 scratch.opcode = EEOP_DONE;
3270 ExprEvalPushStep(state, &scratch);
3271
3272 ExecReadyExpr(state);
3273
3274 return state;
3275 }
3276
3277 /*
3278 * Build transition/combine function invocation for a single transition
3279 * value. This is separated from ExecBuildAggTrans() because there are
3280 * multiple callsites (hash and sort in some grouping set cases).
3281 */
3282 static void
ExecBuildAggTransCall(ExprState * state,AggState * aggstate,ExprEvalStep * scratch,FunctionCallInfo fcinfo,AggStatePerTrans pertrans,int transno,int setno,int setoff,bool ishash,bool nullcheck)3283 ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
3284 ExprEvalStep *scratch,
3285 FunctionCallInfo fcinfo, AggStatePerTrans pertrans,
3286 int transno, int setno, int setoff, bool ishash,
3287 bool nullcheck)
3288 {
3289 ExprContext *aggcontext;
3290 int adjust_jumpnull = -1;
3291
3292 if (ishash)
3293 aggcontext = aggstate->hashcontext;
3294 else
3295 aggcontext = aggstate->aggcontexts[setno];
3296
3297 /* add check for NULL pointer? */
3298 if (nullcheck)
3299 {
3300 scratch->opcode = EEOP_AGG_PLAIN_PERGROUP_NULLCHECK;
3301 scratch->d.agg_plain_pergroup_nullcheck.setoff = setoff;
3302 /* adjust later */
3303 scratch->d.agg_plain_pergroup_nullcheck.jumpnull = -1;
3304 ExprEvalPushStep(state, scratch);
3305 adjust_jumpnull = state->steps_len - 1;
3306 }
3307
3308 /*
3309 * Determine appropriate transition implementation.
3310 *
3311 * For non-ordered aggregates:
3312 *
3313 * If the initial value for the transition state doesn't exist in the
3314 * pg_aggregate table then we will let the first non-NULL value returned
3315 * from the outer procNode become the initial value. (This is useful for
3316 * aggregates like max() and min().) The noTransValue flag signals that we
3317 * need to do so. If true, generate a
3318 * EEOP_AGG_INIT_STRICT_PLAIN_TRANS{,_BYVAL} step. This step also needs to
3319 * do the work described next:
3320 *
3321 * If the function is strict, but does have an initial value, choose
3322 * EEOP_AGG_STRICT_PLAIN_TRANS{,_BYVAL}, which skips the transition
3323 * function if the transition value has become NULL (because a previous
3324 * transition function returned NULL). This step also needs to do the work
3325 * described next:
3326 *
3327 * Otherwise we call EEOP_AGG_PLAIN_TRANS{,_BYVAL}, which does not have to
3328 * perform either of the above checks.
3329 *
3330 * Having steps with overlapping responsibilities is not nice, but
3331 * aggregations are very performance sensitive, making this worthwhile.
3332 *
3333 * For ordered aggregates:
3334 *
3335 * Only need to choose between the faster path for a single orderred
3336 * column, and the one between multiple columns. Checking strictness etc
3337 * is done when finalizing the aggregate. See
3338 * process_ordered_aggregate_{single, multi} and
3339 * advance_transition_function.
3340 */
3341 if (pertrans->numSortCols == 0)
3342 {
3343 if (pertrans->transtypeByVal)
3344 {
3345 if (fcinfo->flinfo->fn_strict &&
3346 pertrans->initValueIsNull)
3347 scratch->opcode = EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL;
3348 else if (fcinfo->flinfo->fn_strict)
3349 scratch->opcode = EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL;
3350 else
3351 scratch->opcode = EEOP_AGG_PLAIN_TRANS_BYVAL;
3352 }
3353 else
3354 {
3355 if (fcinfo->flinfo->fn_strict &&
3356 pertrans->initValueIsNull)
3357 scratch->opcode = EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF;
3358 else if (fcinfo->flinfo->fn_strict)
3359 scratch->opcode = EEOP_AGG_PLAIN_TRANS_STRICT_BYREF;
3360 else
3361 scratch->opcode = EEOP_AGG_PLAIN_TRANS_BYREF;
3362 }
3363 }
3364 else if (pertrans->numInputs == 1)
3365 scratch->opcode = EEOP_AGG_ORDERED_TRANS_DATUM;
3366 else
3367 scratch->opcode = EEOP_AGG_ORDERED_TRANS_TUPLE;
3368
3369 scratch->d.agg_trans.pertrans = pertrans;
3370 scratch->d.agg_trans.setno = setno;
3371 scratch->d.agg_trans.setoff = setoff;
3372 scratch->d.agg_trans.transno = transno;
3373 scratch->d.agg_trans.aggcontext = aggcontext;
3374 ExprEvalPushStep(state, scratch);
3375
3376 /* fix up jumpnull */
3377 if (adjust_jumpnull != -1)
3378 {
3379 ExprEvalStep *as = &state->steps[adjust_jumpnull];
3380
3381 Assert(as->opcode == EEOP_AGG_PLAIN_PERGROUP_NULLCHECK);
3382 Assert(as->d.agg_plain_pergroup_nullcheck.jumpnull == -1);
3383 as->d.agg_plain_pergroup_nullcheck.jumpnull = state->steps_len;
3384 }
3385 }
3386
3387 /*
3388 * Build equality expression that can be evaluated using ExecQual(), returning
3389 * true if the expression context's inner/outer tuple are NOT DISTINCT. I.e
3390 * two nulls match, a null and a not-null don't match.
3391 *
3392 * desc: tuple descriptor of the to-be-compared tuples
3393 * numCols: the number of attributes to be examined
3394 * keyColIdx: array of attribute column numbers
3395 * eqFunctions: array of function oids of the equality functions to use
3396 * parent: parent executor node
3397 */
3398 ExprState *
ExecBuildGroupingEqual(TupleDesc ldesc,TupleDesc rdesc,const TupleTableSlotOps * lops,const TupleTableSlotOps * rops,int numCols,const AttrNumber * keyColIdx,const Oid * eqfunctions,const Oid * collations,PlanState * parent)3399 ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
3400 const TupleTableSlotOps *lops, const TupleTableSlotOps *rops,
3401 int numCols,
3402 const AttrNumber *keyColIdx,
3403 const Oid *eqfunctions,
3404 const Oid *collations,
3405 PlanState *parent)
3406 {
3407 ExprState *state = makeNode(ExprState);
3408 ExprEvalStep scratch = {0};
3409 int maxatt = -1;
3410 List *adjust_jumps = NIL;
3411 ListCell *lc;
3412
3413 /*
3414 * When no columns are actually compared, the result's always true. See
3415 * special case in ExecQual().
3416 */
3417 if (numCols == 0)
3418 return NULL;
3419
3420 state->expr = NULL;
3421 state->flags = EEO_FLAG_IS_QUAL;
3422 state->parent = parent;
3423
3424 scratch.resvalue = &state->resvalue;
3425 scratch.resnull = &state->resnull;
3426
3427 /* compute max needed attribute */
3428 for (int natt = 0; natt < numCols; natt++)
3429 {
3430 int attno = keyColIdx[natt];
3431
3432 if (attno > maxatt)
3433 maxatt = attno;
3434 }
3435 Assert(maxatt >= 0);
3436
3437 /* push deform steps */
3438 scratch.opcode = EEOP_INNER_FETCHSOME;
3439 scratch.d.fetch.last_var = maxatt;
3440 scratch.d.fetch.fixed = false;
3441 scratch.d.fetch.known_desc = ldesc;
3442 scratch.d.fetch.kind = lops;
3443 if (ExecComputeSlotInfo(state, &scratch))
3444 ExprEvalPushStep(state, &scratch);
3445
3446 scratch.opcode = EEOP_OUTER_FETCHSOME;
3447 scratch.d.fetch.last_var = maxatt;
3448 scratch.d.fetch.fixed = false;
3449 scratch.d.fetch.known_desc = rdesc;
3450 scratch.d.fetch.kind = rops;
3451 if (ExecComputeSlotInfo(state, &scratch))
3452 ExprEvalPushStep(state, &scratch);
3453
3454 /*
3455 * Start comparing at the last field (least significant sort key). That's
3456 * the most likely to be different if we are dealing with sorted input.
3457 */
3458 for (int natt = numCols; --natt >= 0;)
3459 {
3460 int attno = keyColIdx[natt];
3461 Form_pg_attribute latt = TupleDescAttr(ldesc, attno - 1);
3462 Form_pg_attribute ratt = TupleDescAttr(rdesc, attno - 1);
3463 Oid foid = eqfunctions[natt];
3464 Oid collid = collations[natt];
3465 FmgrInfo *finfo;
3466 FunctionCallInfo fcinfo;
3467 AclResult aclresult;
3468
3469 /* Check permission to call function */
3470 aclresult = pg_proc_aclcheck(foid, GetUserId(), ACL_EXECUTE);
3471 if (aclresult != ACLCHECK_OK)
3472 aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
3473
3474 InvokeFunctionExecuteHook(foid);
3475
3476 /* Set up the primary fmgr lookup information */
3477 finfo = palloc0(sizeof(FmgrInfo));
3478 fcinfo = palloc0(SizeForFunctionCallInfo(2));
3479 fmgr_info(foid, finfo);
3480 fmgr_info_set_expr(NULL, finfo);
3481 InitFunctionCallInfoData(*fcinfo, finfo, 2,
3482 collid, NULL, NULL);
3483
3484 /* left arg */
3485 scratch.opcode = EEOP_INNER_VAR;
3486 scratch.d.var.attnum = attno - 1;
3487 scratch.d.var.vartype = latt->atttypid;
3488 scratch.resvalue = &fcinfo->args[0].value;
3489 scratch.resnull = &fcinfo->args[0].isnull;
3490 ExprEvalPushStep(state, &scratch);
3491
3492 /* right arg */
3493 scratch.opcode = EEOP_OUTER_VAR;
3494 scratch.d.var.attnum = attno - 1;
3495 scratch.d.var.vartype = ratt->atttypid;
3496 scratch.resvalue = &fcinfo->args[1].value;
3497 scratch.resnull = &fcinfo->args[1].isnull;
3498 ExprEvalPushStep(state, &scratch);
3499
3500 /* evaluate distinctness */
3501 scratch.opcode = EEOP_NOT_DISTINCT;
3502 scratch.d.func.finfo = finfo;
3503 scratch.d.func.fcinfo_data = fcinfo;
3504 scratch.d.func.fn_addr = finfo->fn_addr;
3505 scratch.d.func.nargs = 2;
3506 scratch.resvalue = &state->resvalue;
3507 scratch.resnull = &state->resnull;
3508 ExprEvalPushStep(state, &scratch);
3509
3510 /* then emit EEOP_QUAL to detect if result is false (or null) */
3511 scratch.opcode = EEOP_QUAL;
3512 scratch.d.qualexpr.jumpdone = -1;
3513 scratch.resvalue = &state->resvalue;
3514 scratch.resnull = &state->resnull;
3515 ExprEvalPushStep(state, &scratch);
3516 adjust_jumps = lappend_int(adjust_jumps,
3517 state->steps_len - 1);
3518 }
3519
3520 /* adjust jump targets */
3521 foreach(lc, adjust_jumps)
3522 {
3523 ExprEvalStep *as = &state->steps[lfirst_int(lc)];
3524
3525 Assert(as->opcode == EEOP_QUAL);
3526 Assert(as->d.qualexpr.jumpdone == -1);
3527 as->d.qualexpr.jumpdone = state->steps_len;
3528 }
3529
3530 scratch.resvalue = NULL;
3531 scratch.resnull = NULL;
3532 scratch.opcode = EEOP_DONE;
3533 ExprEvalPushStep(state, &scratch);
3534
3535 ExecReadyExpr(state);
3536
3537 return state;
3538 }
3539