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

Generated by: LCOV version 1.13