LCOV - code coverage report
Current view: top level - src/backend/executor - execExpr.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 97.1 % 2361 2293
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 33 33
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 75.7 % 1206 913

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

Generated by: LCOV version 2.3.2-1