LCOV - code coverage report
Current view: top level - src/backend/executor - execExpr.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 1791 1849 96.9 %
Date: 2024-10-07 01:12:31 Functions: 32 32 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14