LCOV - code coverage report
Current view: top level - src/backend/executor - execExprInterp.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 84.0 % 2486 2088
Test Date: 2026-01-26 10:56:24 Functions: 84.3 % 83 70
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 67.5 % 1226 827

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * execExprInterp.c
       4                 :             :  *        Interpreted evaluation of an expression step list.
       5                 :             :  *
       6                 :             :  * This file provides either a "direct threaded" (for gcc, clang and
       7                 :             :  * compatible) or a "switch threaded" (for all compilers) implementation of
       8                 :             :  * expression evaluation.  The former is amongst the fastest known methods
       9                 :             :  * of interpreting programs without resorting to assembly level work, or
      10                 :             :  * just-in-time compilation, but it requires support for computed gotos.
      11                 :             :  * The latter is amongst the fastest approaches doable in standard C.
      12                 :             :  *
      13                 :             :  * In either case we use ExprEvalStep->opcode to dispatch to the code block
      14                 :             :  * within ExecInterpExpr() that implements the specific opcode type.
      15                 :             :  *
      16                 :             :  * Switch-threading uses a plain switch() statement to perform the
      17                 :             :  * dispatch.  This has the advantages of being plain C and allowing the
      18                 :             :  * compiler to warn if implementation of a specific opcode has been forgotten.
      19                 :             :  * The disadvantage is that dispatches will, as commonly implemented by
      20                 :             :  * compilers, happen from a single location, requiring more jumps and causing
      21                 :             :  * bad branch prediction.
      22                 :             :  *
      23                 :             :  * In direct threading, we use gcc's label-as-values extension - also adopted
      24                 :             :  * by some other compilers - to replace ExprEvalStep->opcode with the address
      25                 :             :  * of the block implementing the instruction. Dispatch to the next instruction
      26                 :             :  * is done by a "computed goto".  This allows for better branch prediction
      27                 :             :  * (as the jumps are happening from different locations) and fewer jumps
      28                 :             :  * (as no preparatory jump to a common dispatch location is needed).
      29                 :             :  *
      30                 :             :  * When using direct threading, ExecReadyInterpretedExpr will replace
      31                 :             :  * each step's opcode field with the address of the relevant code block and
      32                 :             :  * ExprState->flags will contain EEO_FLAG_DIRECT_THREADED to remember that
      33                 :             :  * that's been done.
      34                 :             :  *
      35                 :             :  * For very simple instructions the overhead of the full interpreter
      36                 :             :  * "startup", as minimal as it is, is noticeable.  Therefore
      37                 :             :  * ExecReadyInterpretedExpr will choose to implement certain simple
      38                 :             :  * opcode patterns using special fast-path routines (ExecJust*).
      39                 :             :  *
      40                 :             :  * Complex or uncommon instructions are not implemented in-line in
      41                 :             :  * ExecInterpExpr(), rather we call out to a helper function appearing later
      42                 :             :  * in this file.  For one reason, there'd not be a noticeable performance
      43                 :             :  * benefit, but more importantly those complex routines are intended to be
      44                 :             :  * shared between different expression evaluation approaches.  For instance
      45                 :             :  * a JIT compiler would generate calls to them.  (This is why they are
      46                 :             :  * exported rather than being "static" in this file.)
      47                 :             :  *
      48                 :             :  *
      49                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      50                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      51                 :             :  *
      52                 :             :  * IDENTIFICATION
      53                 :             :  *        src/backend/executor/execExprInterp.c
      54                 :             :  *
      55                 :             :  *-------------------------------------------------------------------------
      56                 :             :  */
      57                 :             : #include "postgres.h"
      58                 :             : 
      59                 :             : #include "access/heaptoast.h"
      60                 :             : #include "catalog/pg_type.h"
      61                 :             : #include "commands/sequence.h"
      62                 :             : #include "executor/execExpr.h"
      63                 :             : #include "executor/nodeSubplan.h"
      64                 :             : #include "funcapi.h"
      65                 :             : #include "miscadmin.h"
      66                 :             : #include "nodes/miscnodes.h"
      67                 :             : #include "nodes/nodeFuncs.h"
      68                 :             : #include "pgstat.h"
      69                 :             : #include "utils/array.h"
      70                 :             : #include "utils/builtins.h"
      71                 :             : #include "utils/date.h"
      72                 :             : #include "utils/datum.h"
      73                 :             : #include "utils/expandedrecord.h"
      74                 :             : #include "utils/json.h"
      75                 :             : #include "utils/jsonfuncs.h"
      76                 :             : #include "utils/jsonpath.h"
      77                 :             : #include "utils/lsyscache.h"
      78                 :             : #include "utils/memutils.h"
      79                 :             : #include "utils/timestamp.h"
      80                 :             : #include "utils/typcache.h"
      81                 :             : #include "utils/xml.h"
      82                 :             : 
      83                 :             : /*
      84                 :             :  * Use computed-goto-based opcode dispatch when computed gotos are available.
      85                 :             :  * But use a separate symbol so that it's easy to adjust locally in this file
      86                 :             :  * for development and testing.
      87                 :             :  */
      88                 :             : #ifdef HAVE_COMPUTED_GOTO
      89                 :             : #define EEO_USE_COMPUTED_GOTO
      90                 :             : #endif                                                  /* HAVE_COMPUTED_GOTO */
      91                 :             : 
      92                 :             : /*
      93                 :             :  * Macros for opcode dispatch.
      94                 :             :  *
      95                 :             :  * EEO_SWITCH - just hides the switch if not in use.
      96                 :             :  * EEO_CASE - labels the implementation of named expression step type.
      97                 :             :  * EEO_DISPATCH - jump to the implementation of the step type for 'op'.
      98                 :             :  * EEO_OPCODE - compute opcode required by used expression evaluation method.
      99                 :             :  * EEO_NEXT - increment 'op' and jump to correct next step type.
     100                 :             :  * EEO_JUMP - jump to the specified step number within the current expression.
     101                 :             :  */
     102                 :             : #if defined(EEO_USE_COMPUTED_GOTO)
     103                 :             : 
     104                 :             : /* struct for jump target -> opcode lookup table */
     105                 :             : typedef struct ExprEvalOpLookup
     106                 :             : {
     107                 :             :         const void *opcode;
     108                 :             :         ExprEvalOp      op;
     109                 :             : } ExprEvalOpLookup;
     110                 :             : 
     111                 :             : /* to make dispatch_table accessible outside ExecInterpExpr() */
     112                 :             : static const void **dispatch_table = NULL;
     113                 :             : 
     114                 :             : /* jump target -> opcode lookup table */
     115                 :             : static ExprEvalOpLookup reverse_dispatch_table[EEOP_LAST];
     116                 :             : 
     117                 :             : #define EEO_SWITCH()
     118                 :             : #define EEO_CASE(name)          CASE_##name:
     119                 :             : #define EEO_DISPATCH()          goto *((void *) op->opcode)
     120                 :             : #define EEO_OPCODE(opcode)      ((intptr_t) dispatch_table[opcode])
     121                 :             : 
     122                 :             : #else                                                   /* !EEO_USE_COMPUTED_GOTO */
     123                 :             : 
     124                 :             : #define EEO_SWITCH()            starteval: switch ((ExprEvalOp) op->opcode)
     125                 :             : #define EEO_CASE(name)          case name:
     126                 :             : #define EEO_DISPATCH()          goto starteval
     127                 :             : #define EEO_OPCODE(opcode)      (opcode)
     128                 :             : 
     129                 :             : #endif                                                  /* EEO_USE_COMPUTED_GOTO */
     130                 :             : 
     131                 :             : #define EEO_NEXT() \
     132                 :             :         do { \
     133                 :             :                 op++; \
     134                 :             :                 EEO_DISPATCH(); \
     135                 :             :         } while (0)
     136                 :             : 
     137                 :             : #define EEO_JUMP(stepno) \
     138                 :             :         do { \
     139                 :             :                 op = &state->steps[stepno]; \
     140                 :             :                 EEO_DISPATCH(); \
     141                 :             :         } while (0)
     142                 :             : 
     143                 :             : 
     144                 :             : static Datum ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull);
     145                 :             : static void ExecInitInterpreter(void);
     146                 :             : 
     147                 :             : /* support functions */
     148                 :             : static void CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype);
     149                 :             : static void CheckOpSlotCompatibility(ExprEvalStep *op, TupleTableSlot *slot);
     150                 :             : static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod,
     151                 :             :                                                                         ExprEvalRowtypeCache *rowcache,
     152                 :             :                                                                         bool *changed);
     153                 :             : static void ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op,
     154                 :             :                                                            ExprContext *econtext, bool checkisnull);
     155                 :             : 
     156                 :             : /* fast-path evaluation functions */
     157                 :             : static Datum ExecJustInnerVar(ExprState *state, ExprContext *econtext, bool *isnull);
     158                 :             : static Datum ExecJustOuterVar(ExprState *state, ExprContext *econtext, bool *isnull);
     159                 :             : static Datum ExecJustScanVar(ExprState *state, ExprContext *econtext, bool *isnull);
     160                 :             : static Datum ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull);
     161                 :             : static Datum ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull);
     162                 :             : static Datum ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull);
     163                 :             : static Datum ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull);
     164                 :             : static Datum ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull);
     165                 :             : static Datum ExecJustInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
     166                 :             : static Datum ExecJustOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
     167                 :             : static Datum ExecJustScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
     168                 :             : static Datum ExecJustAssignInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
     169                 :             : static Datum ExecJustAssignOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
     170                 :             : static Datum ExecJustAssignScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
     171                 :             : static Datum ExecJustHashInnerVarWithIV(ExprState *state, ExprContext *econtext, bool *isnull);
     172                 :             : static Datum ExecJustHashOuterVar(ExprState *state, ExprContext *econtext, bool *isnull);
     173                 :             : static Datum ExecJustHashInnerVar(ExprState *state, ExprContext *econtext, bool *isnull);
     174                 :             : static Datum ExecJustHashOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
     175                 :             : static Datum ExecJustHashInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull);
     176                 :             : static Datum ExecJustHashOuterVarStrict(ExprState *state, ExprContext *econtext, bool *isnull);
     177                 :             : 
     178                 :             : /* execution helper functions */
     179                 :             : static pg_attribute_always_inline void ExecAggPlainTransByVal(AggState *aggstate,
     180                 :             :                                                                                                                           AggStatePerTrans pertrans,
     181                 :             :                                                                                                                           AggStatePerGroup pergroup,
     182                 :             :                                                                                                                           ExprContext *aggcontext,
     183                 :             :                                                                                                                           int setno);
     184                 :             : static pg_attribute_always_inline void ExecAggPlainTransByRef(AggState *aggstate,
     185                 :             :                                                                                                                           AggStatePerTrans pertrans,
     186                 :             :                                                                                                                           AggStatePerGroup pergroup,
     187                 :             :                                                                                                                           ExprContext *aggcontext,
     188                 :             :                                                                                                                           int setno);
     189                 :             : static char *ExecGetJsonValueItemString(JsonbValue *item, bool *resnull);
     190                 :             : 
     191                 :             : /*
     192                 :             :  * ScalarArrayOpExprHashEntry
     193                 :             :  *              Hash table entry type used during EEOP_HASHED_SCALARARRAYOP
     194                 :             :  */
     195                 :             : typedef struct ScalarArrayOpExprHashEntry
     196                 :             : {
     197                 :             :         Datum           key;
     198                 :             :         uint32          status;                 /* hash status */
     199                 :             :         uint32          hash;                   /* hash value (cached) */
     200                 :             : } ScalarArrayOpExprHashEntry;
     201                 :             : 
     202                 :             : #define SH_PREFIX saophash
     203                 :             : #define SH_ELEMENT_TYPE ScalarArrayOpExprHashEntry
     204                 :             : #define SH_KEY_TYPE Datum
     205                 :             : #define SH_SCOPE static inline
     206                 :             : #define SH_DECLARE
     207                 :             : #include "lib/simplehash.h"
     208                 :             : 
     209                 :             : static bool saop_hash_element_match(struct saophash_hash *tb, Datum key1,
     210                 :             :                                                                         Datum key2);
     211                 :             : static uint32 saop_element_hash(struct saophash_hash *tb, Datum key);
     212                 :             : 
     213                 :             : /*
     214                 :             :  * ScalarArrayOpExprHashTable
     215                 :             :  *              Hash table for EEOP_HASHED_SCALARARRAYOP
     216                 :             :  */
     217                 :             : typedef struct ScalarArrayOpExprHashTable
     218                 :             : {
     219                 :             :         saophash_hash *hashtab;         /* underlying hash table */
     220                 :             :         struct ExprEvalStep *op;
     221                 :             :         FmgrInfo        hash_finfo;             /* function's lookup data */
     222                 :             :         FunctionCallInfoBaseData hash_fcinfo_data;      /* arguments etc */
     223                 :             : } ScalarArrayOpExprHashTable;
     224                 :             : 
     225                 :             : /* Define parameters for ScalarArrayOpExpr hash table code generation. */
     226                 :             : #define SH_PREFIX saophash
     227                 :             : #define SH_ELEMENT_TYPE ScalarArrayOpExprHashEntry
     228                 :             : #define SH_KEY_TYPE Datum
     229                 :             : #define SH_KEY key
     230                 :             : #define SH_HASH_KEY(tb, key) saop_element_hash(tb, key)
     231                 :             : #define SH_EQUAL(tb, a, b) saop_hash_element_match(tb, a, b)
     232                 :             : #define SH_SCOPE static inline
     233                 :             : #define SH_STORE_HASH
     234                 :             : #define SH_GET_HASH(tb, a) a->hash
     235                 :             : #define SH_DEFINE
     236                 :             : #include "lib/simplehash.h"
     237                 :             : 
     238                 :             : /*
     239                 :             :  * Prepare ExprState for interpreted execution.
     240                 :             :  */
     241                 :             : void
     242                 :     1126690 : ExecReadyInterpretedExpr(ExprState *state)
     243                 :             : {
     244                 :             :         /* Ensure one-time interpreter setup has been done */
     245                 :     1126690 :         ExecInitInterpreter();
     246                 :             : 
     247                 :             :         /* Simple validity checks on expression */
     248         [ +  - ]:     1126690 :         Assert(state->steps_len >= 1);
     249   [ +  +  +  - ]:     1126690 :         Assert(state->steps[state->steps_len - 1].opcode == EEOP_DONE_RETURN ||
     250                 :             :                    state->steps[state->steps_len - 1].opcode == EEOP_DONE_NO_RETURN);
     251                 :             : 
     252                 :             :         /*
     253                 :             :          * Don't perform redundant initialization. This is unreachable in current
     254                 :             :          * cases, but might be hit if there's additional expression evaluation
     255                 :             :          * methods that rely on interpreted execution to work.
     256                 :             :          */
     257         [ -  + ]:     1126690 :         if (state->flags & EEO_FLAG_INTERPRETER_INITIALIZED)
     258                 :           0 :                 return;
     259                 :             : 
     260                 :             :         /*
     261                 :             :          * First time through, check whether attribute matches Var.  Might not be
     262                 :             :          * ok anymore, due to schema changes. We do that by setting up a callback
     263                 :             :          * that does checking on the first call, which then sets the evalfunc
     264                 :             :          * callback to the actual method of execution.
     265                 :             :          */
     266                 :     1126690 :         state->evalfunc = ExecInterpExprStillValid;
     267                 :             : 
     268                 :             :         /* DIRECT_THREADED should not already be set */
     269         [ +  - ]:     1126690 :         Assert((state->flags & EEO_FLAG_DIRECT_THREADED) == 0);
     270                 :             : 
     271                 :             :         /*
     272                 :             :          * There shouldn't be any errors before the expression is fully
     273                 :             :          * initialized, and even if so, it'd lead to the expression being
     274                 :             :          * abandoned.  So we can set the flag now and save some code.
     275                 :             :          */
     276                 :     1126690 :         state->flags |= EEO_FLAG_INTERPRETER_INITIALIZED;
     277                 :             : 
     278                 :             :         /*
     279                 :             :          * Select fast-path evalfuncs for very simple expressions.  "Starting up"
     280                 :             :          * the full interpreter is a measurable overhead for these, and these
     281                 :             :          * patterns occur often enough to be worth optimizing.
     282                 :             :          */
     283         [ +  + ]:     1126690 :         if (state->steps_len == 5)
     284                 :             :         {
     285                 :      425941 :                 ExprEvalOp      step0 = state->steps[0].opcode;
     286                 :      425941 :                 ExprEvalOp      step1 = state->steps[1].opcode;
     287                 :      425941 :                 ExprEvalOp      step2 = state->steps[2].opcode;
     288                 :      425941 :                 ExprEvalOp      step3 = state->steps[3].opcode;
     289                 :             : 
     290         [ +  + ]:      425941 :                 if (step0 == EEOP_INNER_FETCHSOME &&
     291         [ +  + ]:         968 :                         step1 == EEOP_HASHDATUM_SET_INITVAL &&
     292   [ +  -  -  + ]:         134 :                         step2 == EEOP_INNER_VAR &&
     293                 :         134 :                         step3 == EEOP_HASHDATUM_NEXT32)
     294                 :             :                 {
     295                 :         134 :                         state->evalfunc_private = (void *) ExecJustHashInnerVarWithIV;
     296                 :         134 :                         return;
     297                 :             :                 }
     298         [ +  + ]:      425941 :         }
     299         [ +  + ]:      700749 :         else if (state->steps_len == 4)
     300                 :             :         {
     301                 :       13485 :                 ExprEvalOp      step0 = state->steps[0].opcode;
     302                 :       13485 :                 ExprEvalOp      step1 = state->steps[1].opcode;
     303                 :       13485 :                 ExprEvalOp      step2 = state->steps[2].opcode;
     304                 :             : 
     305         [ +  + ]:       13485 :                 if (step0 == EEOP_OUTER_FETCHSOME &&
     306   [ +  +  +  + ]:        2247 :                         step1 == EEOP_OUTER_VAR &&
     307                 :        2099 :                         step2 == EEOP_HASHDATUM_FIRST)
     308                 :             :                 {
     309                 :         208 :                         state->evalfunc_private = (void *) ExecJustHashOuterVar;
     310                 :         208 :                         return;
     311                 :             :                 }
     312         [ +  + ]:       13277 :                 else if (step0 == EEOP_INNER_FETCHSOME &&
     313   [ +  +  +  + ]:         655 :                                  step1 == EEOP_INNER_VAR &&
     314                 :         377 :                                  step2 == EEOP_HASHDATUM_FIRST)
     315                 :             :                 {
     316                 :         375 :                         state->evalfunc_private = (void *) ExecJustHashInnerVar;
     317                 :         375 :                         return;
     318                 :             :                 }
     319         [ +  + ]:       12902 :                 else if (step0 == EEOP_OUTER_FETCHSOME &&
     320   [ +  +  +  + ]:        2039 :                                  step1 == EEOP_OUTER_VAR &&
     321                 :        1891 :                                  step2 == EEOP_HASHDATUM_FIRST_STRICT)
     322                 :             :                 {
     323                 :        1697 :                         state->evalfunc_private = (void *) ExecJustHashOuterVarStrict;
     324                 :        1697 :                         return;
     325                 :             :                 }
     326         [ +  + ]:       13485 :         }
     327         [ +  + ]:      687264 :         else if (state->steps_len == 3)
     328                 :             :         {
     329                 :       47371 :                 ExprEvalOp      step0 = state->steps[0].opcode;
     330                 :       47371 :                 ExprEvalOp      step1 = state->steps[1].opcode;
     331                 :             : 
     332   [ +  +  +  + ]:       47371 :                 if (step0 == EEOP_INNER_FETCHSOME &&
     333                 :        1576 :                         step1 == EEOP_INNER_VAR)
     334                 :             :                 {
     335                 :         534 :                         state->evalfunc_private = ExecJustInnerVar;
     336                 :         534 :                         return;
     337                 :             :                 }
     338   [ +  +  +  + ]:       46837 :                 else if (step0 == EEOP_OUTER_FETCHSOME &&
     339                 :        2276 :                                  step1 == EEOP_OUTER_VAR)
     340                 :             :                 {
     341                 :         914 :                         state->evalfunc_private = ExecJustOuterVar;
     342                 :         914 :                         return;
     343                 :             :                 }
     344   [ +  +  +  + ]:       45923 :                 else if (step0 == EEOP_SCAN_FETCHSOME &&
     345                 :        4301 :                                  step1 == EEOP_SCAN_VAR)
     346                 :             :                 {
     347                 :           3 :                         state->evalfunc_private = ExecJustScanVar;
     348                 :           3 :                         return;
     349                 :             :                 }
     350   [ +  +  -  + ]:       45920 :                 else if (step0 == EEOP_INNER_FETCHSOME &&
     351                 :        1042 :                                  step1 == EEOP_ASSIGN_INNER_VAR)
     352                 :             :                 {
     353                 :        1042 :                         state->evalfunc_private = ExecJustAssignInnerVar;
     354                 :        1042 :                         return;
     355                 :             :                 }
     356   [ +  +  -  + ]:       44878 :                 else if (step0 == EEOP_OUTER_FETCHSOME &&
     357                 :        1362 :                                  step1 == EEOP_ASSIGN_OUTER_VAR)
     358                 :             :                 {
     359                 :        1362 :                         state->evalfunc_private = ExecJustAssignOuterVar;
     360                 :        1362 :                         return;
     361                 :             :                 }
     362   [ +  +  -  + ]:       43516 :                 else if (step0 == EEOP_SCAN_FETCHSOME &&
     363                 :        4298 :                                  step1 == EEOP_ASSIGN_SCAN_VAR)
     364                 :             :                 {
     365                 :        4298 :                         state->evalfunc_private = ExecJustAssignScanVar;
     366                 :        4298 :                         return;
     367                 :             :                 }
     368   [ +  +  -  + ]:       39224 :                 else if (step0 == EEOP_CASE_TESTVAL &&
     369         [ +  + ]:          51 :                                  (step1 == EEOP_FUNCEXPR_STRICT ||
     370         [ +  + ]:          37 :                                   step1 == EEOP_FUNCEXPR_STRICT_1 ||
     371                 :           6 :                                   step1 == EEOP_FUNCEXPR_STRICT_2))
     372                 :             :                 {
     373                 :          45 :                         state->evalfunc_private = ExecJustApplyFuncToCase;
     374                 :          45 :                         return;
     375                 :             :                 }
     376   [ +  +  +  + ]:       39173 :                 else if (step0 == EEOP_INNER_VAR &&
     377                 :         197 :                                  step1 == EEOP_HASHDATUM_FIRST)
     378                 :             :                 {
     379                 :         196 :                         state->evalfunc_private = (void *) ExecJustHashInnerVarVirt;
     380                 :         196 :                         return;
     381                 :             :                 }
     382   [ +  +  +  + ]:       38977 :                 else if (step0 == EEOP_OUTER_VAR &&
     383                 :        5464 :                                  step1 == EEOP_HASHDATUM_FIRST)
     384                 :             :                 {
     385                 :         534 :                         state->evalfunc_private = (void *) ExecJustHashOuterVarVirt;
     386                 :         534 :                         return;
     387                 :             :                 }
     388         [ +  + ]:       47371 :         }
     389         [ +  + ]:      639893 :         else if (state->steps_len == 2)
     390                 :             :         {
     391                 :      179403 :                 ExprEvalOp      step0 = state->steps[0].opcode;
     392                 :             : 
     393         [ +  + ]:      179403 :                 if (step0 == EEOP_CONST)
     394                 :             :                 {
     395                 :       33743 :                         state->evalfunc_private = ExecJustConst;
     396                 :       33743 :                         return;
     397                 :             :                 }
     398         [ +  + ]:      145660 :                 else if (step0 == EEOP_INNER_VAR)
     399                 :             :                 {
     400                 :          27 :                         state->evalfunc_private = ExecJustInnerVarVirt;
     401                 :          27 :                         return;
     402                 :             :                 }
     403         [ +  + ]:      145633 :                 else if (step0 == EEOP_OUTER_VAR)
     404                 :             :                 {
     405                 :         340 :                         state->evalfunc_private = ExecJustOuterVarVirt;
     406                 :         340 :                         return;
     407                 :             :                 }
     408         [ -  + ]:      145293 :                 else if (step0 == EEOP_SCAN_VAR)
     409                 :             :                 {
     410                 :           0 :                         state->evalfunc_private = ExecJustScanVarVirt;
     411                 :           0 :                         return;
     412                 :             :                 }
     413         [ +  + ]:      145293 :                 else if (step0 == EEOP_ASSIGN_INNER_VAR)
     414                 :             :                 {
     415                 :          55 :                         state->evalfunc_private = ExecJustAssignInnerVarVirt;
     416                 :          55 :                         return;
     417                 :             :                 }
     418         [ +  + ]:      145238 :                 else if (step0 == EEOP_ASSIGN_OUTER_VAR)
     419                 :             :                 {
     420                 :         497 :                         state->evalfunc_private = ExecJustAssignOuterVarVirt;
     421                 :         497 :                         return;
     422                 :             :                 }
     423         [ +  + ]:      144741 :                 else if (step0 == EEOP_ASSIGN_SCAN_VAR)
     424                 :             :                 {
     425                 :         166 :                         state->evalfunc_private = ExecJustAssignScanVarVirt;
     426                 :         166 :                         return;
     427                 :             :                 }
     428         [ +  + ]:      179403 :         }
     429                 :             : 
     430                 :             : #if defined(EEO_USE_COMPUTED_GOTO)
     431                 :             : 
     432                 :             :         /*
     433                 :             :          * In the direct-threaded implementation, replace each opcode with the
     434                 :             :          * address to jump to.  (Use ExecEvalStepOp() to get back the opcode.)
     435                 :             :          */
     436         [ +  + ]:     6640210 :         for (int off = 0; off < state->steps_len; off++)
     437                 :             :         {
     438                 :     5559690 :                 ExprEvalStep *op = &state->steps[off];
     439                 :             : 
     440                 :     5559690 :                 op->opcode = EEO_OPCODE(op->opcode);
     441                 :     5559690 :         }
     442                 :             : 
     443                 :     1080520 :         state->flags |= EEO_FLAG_DIRECT_THREADED;
     444                 :             : #endif                                                  /* EEO_USE_COMPUTED_GOTO */
     445                 :             : 
     446                 :     1080520 :         state->evalfunc_private = ExecInterpExpr;
     447                 :     1126690 : }
     448                 :             : 
     449                 :             : 
     450                 :             : /*
     451                 :             :  * Evaluate expression identified by "state" in the execution context
     452                 :             :  * given by "econtext".  *isnull is set to the is-null flag for the result,
     453                 :             :  * and the Datum value is the function result.
     454                 :             :  *
     455                 :             :  * As a special case, return the dispatch table's address if state is NULL.
     456                 :             :  * This is used by ExecInitInterpreter to set up the dispatch_table global.
     457                 :             :  * (Only applies when EEO_USE_COMPUTED_GOTO is defined.)
     458                 :             :  */
     459                 :             : static Datum
     460                 :    29092010 : ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
     461                 :             : {
     462                 :             :         ExprEvalStep *op;
     463                 :             :         TupleTableSlot *resultslot;
     464                 :             :         TupleTableSlot *innerslot;
     465                 :             :         TupleTableSlot *outerslot;
     466                 :             :         TupleTableSlot *scanslot;
     467                 :             :         TupleTableSlot *oldslot;
     468                 :             :         TupleTableSlot *newslot;
     469                 :             : 
     470                 :             :         /*
     471                 :             :          * This array has to be in the same order as enum ExprEvalOp.
     472                 :             :          */
     473                 :             : #if defined(EEO_USE_COMPUTED_GOTO)
     474                 :             :         static const void *const dispatch_table[] = {
     475                 :             :                 &&CASE_EEOP_DONE_RETURN,
     476                 :             :                 &&CASE_EEOP_DONE_NO_RETURN,
     477                 :             :                 &&CASE_EEOP_INNER_FETCHSOME,
     478                 :             :                 &&CASE_EEOP_OUTER_FETCHSOME,
     479                 :             :                 &&CASE_EEOP_SCAN_FETCHSOME,
     480                 :             :                 &&CASE_EEOP_OLD_FETCHSOME,
     481                 :             :                 &&CASE_EEOP_NEW_FETCHSOME,
     482                 :             :                 &&CASE_EEOP_INNER_VAR,
     483                 :             :                 &&CASE_EEOP_OUTER_VAR,
     484                 :             :                 &&CASE_EEOP_SCAN_VAR,
     485                 :             :                 &&CASE_EEOP_OLD_VAR,
     486                 :             :                 &&CASE_EEOP_NEW_VAR,
     487                 :             :                 &&CASE_EEOP_INNER_SYSVAR,
     488                 :             :                 &&CASE_EEOP_OUTER_SYSVAR,
     489                 :             :                 &&CASE_EEOP_SCAN_SYSVAR,
     490                 :             :                 &&CASE_EEOP_OLD_SYSVAR,
     491                 :             :                 &&CASE_EEOP_NEW_SYSVAR,
     492                 :             :                 &&CASE_EEOP_WHOLEROW,
     493                 :             :                 &&CASE_EEOP_ASSIGN_INNER_VAR,
     494                 :             :                 &&CASE_EEOP_ASSIGN_OUTER_VAR,
     495                 :             :                 &&CASE_EEOP_ASSIGN_SCAN_VAR,
     496                 :             :                 &&CASE_EEOP_ASSIGN_OLD_VAR,
     497                 :             :                 &&CASE_EEOP_ASSIGN_NEW_VAR,
     498                 :             :                 &&CASE_EEOP_ASSIGN_TMP,
     499                 :             :                 &&CASE_EEOP_ASSIGN_TMP_MAKE_RO,
     500                 :             :                 &&CASE_EEOP_CONST,
     501                 :             :                 &&CASE_EEOP_FUNCEXPR,
     502                 :             :                 &&CASE_EEOP_FUNCEXPR_STRICT,
     503                 :             :                 &&CASE_EEOP_FUNCEXPR_STRICT_1,
     504                 :             :                 &&CASE_EEOP_FUNCEXPR_STRICT_2,
     505                 :             :                 &&CASE_EEOP_FUNCEXPR_FUSAGE,
     506                 :             :                 &&CASE_EEOP_FUNCEXPR_STRICT_FUSAGE,
     507                 :             :                 &&CASE_EEOP_BOOL_AND_STEP_FIRST,
     508                 :             :                 &&CASE_EEOP_BOOL_AND_STEP,
     509                 :             :                 &&CASE_EEOP_BOOL_AND_STEP_LAST,
     510                 :             :                 &&CASE_EEOP_BOOL_OR_STEP_FIRST,
     511                 :             :                 &&CASE_EEOP_BOOL_OR_STEP,
     512                 :             :                 &&CASE_EEOP_BOOL_OR_STEP_LAST,
     513                 :             :                 &&CASE_EEOP_BOOL_NOT_STEP,
     514                 :             :                 &&CASE_EEOP_QUAL,
     515                 :             :                 &&CASE_EEOP_JUMP,
     516                 :             :                 &&CASE_EEOP_JUMP_IF_NULL,
     517                 :             :                 &&CASE_EEOP_JUMP_IF_NOT_NULL,
     518                 :             :                 &&CASE_EEOP_JUMP_IF_NOT_TRUE,
     519                 :             :                 &&CASE_EEOP_NULLTEST_ISNULL,
     520                 :             :                 &&CASE_EEOP_NULLTEST_ISNOTNULL,
     521                 :             :                 &&CASE_EEOP_NULLTEST_ROWISNULL,
     522                 :             :                 &&CASE_EEOP_NULLTEST_ROWISNOTNULL,
     523                 :             :                 &&CASE_EEOP_BOOLTEST_IS_TRUE,
     524                 :             :                 &&CASE_EEOP_BOOLTEST_IS_NOT_TRUE,
     525                 :             :                 &&CASE_EEOP_BOOLTEST_IS_FALSE,
     526                 :             :                 &&CASE_EEOP_BOOLTEST_IS_NOT_FALSE,
     527                 :             :                 &&CASE_EEOP_PARAM_EXEC,
     528                 :             :                 &&CASE_EEOP_PARAM_EXTERN,
     529                 :             :                 &&CASE_EEOP_PARAM_CALLBACK,
     530                 :             :                 &&CASE_EEOP_PARAM_SET,
     531                 :             :                 &&CASE_EEOP_CASE_TESTVAL,
     532                 :             :                 &&CASE_EEOP_CASE_TESTVAL_EXT,
     533                 :             :                 &&CASE_EEOP_MAKE_READONLY,
     534                 :             :                 &&CASE_EEOP_IOCOERCE,
     535                 :             :                 &&CASE_EEOP_IOCOERCE_SAFE,
     536                 :             :                 &&CASE_EEOP_DISTINCT,
     537                 :             :                 &&CASE_EEOP_NOT_DISTINCT,
     538                 :             :                 &&CASE_EEOP_NULLIF,
     539                 :             :                 &&CASE_EEOP_SQLVALUEFUNCTION,
     540                 :             :                 &&CASE_EEOP_CURRENTOFEXPR,
     541                 :             :                 &&CASE_EEOP_NEXTVALUEEXPR,
     542                 :             :                 &&CASE_EEOP_RETURNINGEXPR,
     543                 :             :                 &&CASE_EEOP_ARRAYEXPR,
     544                 :             :                 &&CASE_EEOP_ARRAYCOERCE,
     545                 :             :                 &&CASE_EEOP_ROW,
     546                 :             :                 &&CASE_EEOP_ROWCOMPARE_STEP,
     547                 :             :                 &&CASE_EEOP_ROWCOMPARE_FINAL,
     548                 :             :                 &&CASE_EEOP_MINMAX,
     549                 :             :                 &&CASE_EEOP_FIELDSELECT,
     550                 :             :                 &&CASE_EEOP_FIELDSTORE_DEFORM,
     551                 :             :                 &&CASE_EEOP_FIELDSTORE_FORM,
     552                 :             :                 &&CASE_EEOP_SBSREF_SUBSCRIPTS,
     553                 :             :                 &&CASE_EEOP_SBSREF_OLD,
     554                 :             :                 &&CASE_EEOP_SBSREF_ASSIGN,
     555                 :             :                 &&CASE_EEOP_SBSREF_FETCH,
     556                 :             :                 &&CASE_EEOP_DOMAIN_TESTVAL,
     557                 :             :                 &&CASE_EEOP_DOMAIN_TESTVAL_EXT,
     558                 :             :                 &&CASE_EEOP_DOMAIN_NOTNULL,
     559                 :             :                 &&CASE_EEOP_DOMAIN_CHECK,
     560                 :             :                 &&CASE_EEOP_HASHDATUM_SET_INITVAL,
     561                 :             :                 &&CASE_EEOP_HASHDATUM_FIRST,
     562                 :             :                 &&CASE_EEOP_HASHDATUM_FIRST_STRICT,
     563                 :             :                 &&CASE_EEOP_HASHDATUM_NEXT32,
     564                 :             :                 &&CASE_EEOP_HASHDATUM_NEXT32_STRICT,
     565                 :             :                 &&CASE_EEOP_CONVERT_ROWTYPE,
     566                 :             :                 &&CASE_EEOP_SCALARARRAYOP,
     567                 :             :                 &&CASE_EEOP_HASHED_SCALARARRAYOP,
     568                 :             :                 &&CASE_EEOP_XMLEXPR,
     569                 :             :                 &&CASE_EEOP_JSON_CONSTRUCTOR,
     570                 :             :                 &&CASE_EEOP_IS_JSON,
     571                 :             :                 &&CASE_EEOP_JSONEXPR_PATH,
     572                 :             :                 &&CASE_EEOP_JSONEXPR_COERCION,
     573                 :             :                 &&CASE_EEOP_JSONEXPR_COERCION_FINISH,
     574                 :             :                 &&CASE_EEOP_AGGREF,
     575                 :             :                 &&CASE_EEOP_GROUPING_FUNC,
     576                 :             :                 &&CASE_EEOP_WINDOW_FUNC,
     577                 :             :                 &&CASE_EEOP_MERGE_SUPPORT_FUNC,
     578                 :             :                 &&CASE_EEOP_SUBPLAN,
     579                 :             :                 &&CASE_EEOP_AGG_STRICT_DESERIALIZE,
     580                 :             :                 &&CASE_EEOP_AGG_DESERIALIZE,
     581                 :             :                 &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS,
     582                 :             :                 &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1,
     583                 :             :                 &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_NULLS,
     584                 :             :                 &&CASE_EEOP_AGG_PLAIN_PERGROUP_NULLCHECK,
     585                 :             :                 &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL,
     586                 :             :                 &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL,
     587                 :             :                 &&CASE_EEOP_AGG_PLAIN_TRANS_BYVAL,
     588                 :             :                 &&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF,
     589                 :             :                 &&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYREF,
     590                 :             :                 &&CASE_EEOP_AGG_PLAIN_TRANS_BYREF,
     591                 :             :                 &&CASE_EEOP_AGG_PRESORTED_DISTINCT_SINGLE,
     592                 :             :                 &&CASE_EEOP_AGG_PRESORTED_DISTINCT_MULTI,
     593                 :             :                 &&CASE_EEOP_AGG_ORDERED_TRANS_DATUM,
     594                 :             :                 &&CASE_EEOP_AGG_ORDERED_TRANS_TUPLE,
     595                 :             :                 &&CASE_EEOP_LAST
     596                 :             :         };
     597                 :             : 
     598                 :             :         StaticAssertDecl(lengthof(dispatch_table) == EEOP_LAST + 1,
     599                 :             :                                          "dispatch_table out of whack with ExprEvalOp");
     600                 :             : 
     601         [ +  + ]:    29092010 :         if (unlikely(state == NULL))
     602                 :         726 :                 return PointerGetDatum(dispatch_table);
     603                 :             : #else
     604                 :             :         Assert(state != NULL);
     605                 :             : #endif                                                  /* EEO_USE_COMPUTED_GOTO */
     606                 :             : 
     607                 :             :         /* setup state */
     608                 :    29091284 :         op = state->steps;
     609                 :    29091284 :         resultslot = state->resultslot;
     610                 :    29091284 :         innerslot = econtext->ecxt_innertuple;
     611                 :    29091284 :         outerslot = econtext->ecxt_outertuple;
     612                 :    29091284 :         scanslot = econtext->ecxt_scantuple;
     613                 :    29091284 :         oldslot = econtext->ecxt_oldtuple;
     614                 :    29091284 :         newslot = econtext->ecxt_newtuple;
     615                 :             : 
     616                 :             : #if defined(EEO_USE_COMPUTED_GOTO)
     617                 :    29091284 :         EEO_DISPATCH();
     618                 :             : #endif
     619                 :             : 
     620                 :             :         EEO_SWITCH()
     621                 :             :         {
     622                 :             :                 EEO_CASE(EEOP_DONE_RETURN)
     623                 :             :                 {
     624                 :    18614430 :                         *isnull = state->resnull;
     625                 :    18614430 :                         return state->resvalue;
     626                 :             :                 }
     627                 :             : 
     628                 :             :                 EEO_CASE(EEOP_DONE_NO_RETURN)
     629                 :             :                 {
     630         [ +  - ]:    10476852 :                         Assert(isnull == NULL);
     631                 :    10476852 :                         return (Datum) 0;
     632                 :             :                 }
     633                 :             : 
     634                 :             :                 EEO_CASE(EEOP_INNER_FETCHSOME)
     635                 :             :                 {
     636                 :     4669232 :                         CheckOpSlotCompatibility(op, innerslot);
     637                 :             : 
     638                 :     4669232 :                         slot_getsomeattrs(innerslot, op->d.fetch.last_var);
     639                 :             : 
     640                 :     4669232 :                         EEO_NEXT();
     641                 :           0 :                 }
     642                 :             : 
     643                 :             :                 EEO_CASE(EEOP_OUTER_FETCHSOME)
     644                 :             :                 {
     645                 :     4440713 :                         CheckOpSlotCompatibility(op, outerslot);
     646                 :             : 
     647                 :     4440713 :                         slot_getsomeattrs(outerslot, op->d.fetch.last_var);
     648                 :             : 
     649                 :     4440713 :                         EEO_NEXT();
     650                 :           0 :                 }
     651                 :             : 
     652                 :             :                 EEO_CASE(EEOP_SCAN_FETCHSOME)
     653                 :             :                 {
     654                 :    14477368 :                         CheckOpSlotCompatibility(op, scanslot);
     655                 :             : 
     656                 :    14477368 :                         slot_getsomeattrs(scanslot, op->d.fetch.last_var);
     657                 :             : 
     658                 :    14477368 :                         EEO_NEXT();
     659                 :           0 :                 }
     660                 :             : 
     661                 :             :                 EEO_CASE(EEOP_OLD_FETCHSOME)
     662                 :             :                 {
     663                 :          61 :                         CheckOpSlotCompatibility(op, oldslot);
     664                 :             : 
     665                 :          61 :                         slot_getsomeattrs(oldslot, op->d.fetch.last_var);
     666                 :             : 
     667                 :          61 :                         EEO_NEXT();
     668                 :           0 :                 }
     669                 :             : 
     670                 :             :                 EEO_CASE(EEOP_NEW_FETCHSOME)
     671                 :             :                 {
     672                 :          62 :                         CheckOpSlotCompatibility(op, newslot);
     673                 :             : 
     674                 :          62 :                         slot_getsomeattrs(newslot, op->d.fetch.last_var);
     675                 :             : 
     676                 :          62 :                         EEO_NEXT();
     677                 :           0 :                 }
     678                 :             : 
     679                 :             :                 EEO_CASE(EEOP_INNER_VAR)
     680                 :             :                 {
     681                 :     6401752 :                         int                     attnum = op->d.var.attnum;
     682                 :             : 
     683                 :             :                         /*
     684                 :             :                          * Since we already extracted all referenced columns from the
     685                 :             :                          * tuple with a FETCHSOME step, we can just grab the value
     686                 :             :                          * directly out of the slot's decomposed-data arrays.  But let's
     687                 :             :                          * have an Assert to check that that did happen.
     688                 :             :                          */
     689         [ +  - ]:     6401752 :                         Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
     690                 :     6401752 :                         *op->resvalue = innerslot->tts_values[attnum];
     691                 :     6401752 :                         *op->resnull = innerslot->tts_isnull[attnum];
     692                 :             : 
     693                 :     6401752 :                         EEO_NEXT();
     694                 :           0 :                 }
     695                 :             : 
     696                 :             :                 EEO_CASE(EEOP_OUTER_VAR)
     697                 :             :                 {
     698                 :    10675787 :                         int                     attnum = op->d.var.attnum;
     699                 :             : 
     700                 :             :                         /* See EEOP_INNER_VAR comments */
     701                 :             : 
     702         [ +  - ]:    10675787 :                         Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
     703                 :    10675787 :                         *op->resvalue = outerslot->tts_values[attnum];
     704                 :    10675787 :                         *op->resnull = outerslot->tts_isnull[attnum];
     705                 :             : 
     706                 :    10675787 :                         EEO_NEXT();
     707                 :           0 :                 }
     708                 :             : 
     709                 :             :                 EEO_CASE(EEOP_SCAN_VAR)
     710                 :             :                 {
     711                 :    15405152 :                         int                     attnum = op->d.var.attnum;
     712                 :             : 
     713                 :             :                         /* See EEOP_INNER_VAR comments */
     714                 :             : 
     715         [ +  - ]:    15405152 :                         Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
     716                 :    15405152 :                         *op->resvalue = scanslot->tts_values[attnum];
     717                 :    15405152 :                         *op->resnull = scanslot->tts_isnull[attnum];
     718                 :             : 
     719                 :    15405152 :                         EEO_NEXT();
     720                 :           0 :                 }
     721                 :             : 
     722                 :             :                 EEO_CASE(EEOP_OLD_VAR)
     723                 :             :                 {
     724                 :          55 :                         int                     attnum = op->d.var.attnum;
     725                 :             : 
     726                 :             :                         /* See EEOP_INNER_VAR comments */
     727                 :             : 
     728         [ +  - ]:          55 :                         Assert(attnum >= 0 && attnum < oldslot->tts_nvalid);
     729                 :          55 :                         *op->resvalue = oldslot->tts_values[attnum];
     730                 :          55 :                         *op->resnull = oldslot->tts_isnull[attnum];
     731                 :             : 
     732                 :          55 :                         EEO_NEXT();
     733                 :           0 :                 }
     734                 :             : 
     735                 :             :                 EEO_CASE(EEOP_NEW_VAR)
     736                 :             :                 {
     737                 :          56 :                         int                     attnum = op->d.var.attnum;
     738                 :             : 
     739                 :             :                         /* See EEOP_INNER_VAR comments */
     740                 :             : 
     741         [ +  - ]:          56 :                         Assert(attnum >= 0 && attnum < newslot->tts_nvalid);
     742                 :          56 :                         *op->resvalue = newslot->tts_values[attnum];
     743                 :          56 :                         *op->resnull = newslot->tts_isnull[attnum];
     744                 :             : 
     745                 :          56 :                         EEO_NEXT();
     746                 :           0 :                 }
     747                 :             : 
     748                 :             :                 EEO_CASE(EEOP_INNER_SYSVAR)
     749                 :             :                 {
     750                 :           1 :                         ExecEvalSysVar(state, op, econtext, innerslot);
     751                 :           1 :                         EEO_NEXT();
     752                 :           0 :                 }
     753                 :             : 
     754                 :             :                 EEO_CASE(EEOP_OUTER_SYSVAR)
     755                 :             :                 {
     756                 :           2 :                         ExecEvalSysVar(state, op, econtext, outerslot);
     757                 :           2 :                         EEO_NEXT();
     758                 :           0 :                 }
     759                 :             : 
     760                 :             :                 EEO_CASE(EEOP_SCAN_SYSVAR)
     761                 :             :                 {
     762                 :     1030640 :                         ExecEvalSysVar(state, op, econtext, scanslot);
     763                 :     1030640 :                         EEO_NEXT();
     764                 :           0 :                 }
     765                 :             : 
     766                 :             :                 EEO_CASE(EEOP_OLD_SYSVAR)
     767                 :             :                 {
     768                 :          38 :                         ExecEvalSysVar(state, op, econtext, oldslot);
     769                 :          38 :                         EEO_NEXT();
     770                 :           0 :                 }
     771                 :             : 
     772                 :             :                 EEO_CASE(EEOP_NEW_SYSVAR)
     773                 :             :                 {
     774                 :          38 :                         ExecEvalSysVar(state, op, econtext, newslot);
     775                 :          38 :                         EEO_NEXT();
     776                 :           0 :                 }
     777                 :             : 
     778                 :             :                 EEO_CASE(EEOP_WHOLEROW)
     779                 :             :                 {
     780                 :             :                         /* too complex for an inline implementation */
     781                 :        2723 :                         ExecEvalWholeRowVar(state, op, econtext);
     782                 :             : 
     783                 :        2723 :                         EEO_NEXT();
     784                 :           0 :                 }
     785                 :             : 
     786                 :             :                 EEO_CASE(EEOP_ASSIGN_INNER_VAR)
     787                 :             :                 {
     788                 :      690081 :                         int                     resultnum = op->d.assign_var.resultnum;
     789                 :      690081 :                         int                     attnum = op->d.assign_var.attnum;
     790                 :             : 
     791                 :             :                         /*
     792                 :             :                          * We do not need CheckVarSlotCompatibility here; that was taken
     793                 :             :                          * care of at compilation time.  But see EEOP_INNER_VAR comments.
     794                 :             :                          */
     795         [ +  - ]:      690081 :                         Assert(attnum >= 0 && attnum < innerslot->tts_nvalid);
     796         [ +  - ]:      690081 :                         Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
     797                 :      690081 :                         resultslot->tts_values[resultnum] = innerslot->tts_values[attnum];
     798                 :      690081 :                         resultslot->tts_isnull[resultnum] = innerslot->tts_isnull[attnum];
     799                 :             : 
     800                 :      690081 :                         EEO_NEXT();
     801                 :           0 :                 }
     802                 :             : 
     803                 :             :                 EEO_CASE(EEOP_ASSIGN_OUTER_VAR)
     804                 :             :                 {
     805                 :     1271737 :                         int                     resultnum = op->d.assign_var.resultnum;
     806                 :     1271737 :                         int                     attnum = op->d.assign_var.attnum;
     807                 :             : 
     808                 :             :                         /*
     809                 :             :                          * We do not need CheckVarSlotCompatibility here; that was taken
     810                 :             :                          * care of at compilation time.  But see EEOP_INNER_VAR comments.
     811                 :             :                          */
     812         [ +  - ]:     1271737 :                         Assert(attnum >= 0 && attnum < outerslot->tts_nvalid);
     813         [ +  - ]:     1271737 :                         Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
     814                 :     1271737 :                         resultslot->tts_values[resultnum] = outerslot->tts_values[attnum];
     815                 :     1271737 :                         resultslot->tts_isnull[resultnum] = outerslot->tts_isnull[attnum];
     816                 :             : 
     817                 :     1271737 :                         EEO_NEXT();
     818                 :           0 :                 }
     819                 :             : 
     820                 :             :                 EEO_CASE(EEOP_ASSIGN_SCAN_VAR)
     821                 :             :                 {
     822                 :     4483452 :                         int                     resultnum = op->d.assign_var.resultnum;
     823                 :     4483452 :                         int                     attnum = op->d.assign_var.attnum;
     824                 :             : 
     825                 :             :                         /*
     826                 :             :                          * We do not need CheckVarSlotCompatibility here; that was taken
     827                 :             :                          * care of at compilation time.  But see EEOP_INNER_VAR comments.
     828                 :             :                          */
     829         [ +  - ]:     4483452 :                         Assert(attnum >= 0 && attnum < scanslot->tts_nvalid);
     830         [ +  - ]:     4483452 :                         Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
     831                 :     4483452 :                         resultslot->tts_values[resultnum] = scanslot->tts_values[attnum];
     832                 :     4483452 :                         resultslot->tts_isnull[resultnum] = scanslot->tts_isnull[attnum];
     833                 :             : 
     834                 :     4483452 :                         EEO_NEXT();
     835                 :           0 :                 }
     836                 :             : 
     837                 :             :                 EEO_CASE(EEOP_ASSIGN_OLD_VAR)
     838                 :             :                 {
     839                 :         128 :                         int                     resultnum = op->d.assign_var.resultnum;
     840                 :         128 :                         int                     attnum = op->d.assign_var.attnum;
     841                 :             : 
     842                 :             :                         /*
     843                 :             :                          * We do not need CheckVarSlotCompatibility here; that was taken
     844                 :             :                          * care of at compilation time.  But see EEOP_INNER_VAR comments.
     845                 :             :                          */
     846         [ +  - ]:         128 :                         Assert(attnum >= 0 && attnum < oldslot->tts_nvalid);
     847         [ +  - ]:         128 :                         Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
     848                 :         128 :                         resultslot->tts_values[resultnum] = oldslot->tts_values[attnum];
     849                 :         128 :                         resultslot->tts_isnull[resultnum] = oldslot->tts_isnull[attnum];
     850                 :             : 
     851                 :         128 :                         EEO_NEXT();
     852                 :           0 :                 }
     853                 :             : 
     854                 :             :                 EEO_CASE(EEOP_ASSIGN_NEW_VAR)
     855                 :             :                 {
     856                 :         129 :                         int                     resultnum = op->d.assign_var.resultnum;
     857                 :         129 :                         int                     attnum = op->d.assign_var.attnum;
     858                 :             : 
     859                 :             :                         /*
     860                 :             :                          * We do not need CheckVarSlotCompatibility here; that was taken
     861                 :             :                          * care of at compilation time.  But see EEOP_INNER_VAR comments.
     862                 :             :                          */
     863         [ +  - ]:         129 :                         Assert(attnum >= 0 && attnum < newslot->tts_nvalid);
     864         [ +  - ]:         129 :                         Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
     865                 :         129 :                         resultslot->tts_values[resultnum] = newslot->tts_values[attnum];
     866                 :         129 :                         resultslot->tts_isnull[resultnum] = newslot->tts_isnull[attnum];
     867                 :             : 
     868                 :         129 :                         EEO_NEXT();
     869                 :           0 :                 }
     870                 :             : 
     871                 :             :                 EEO_CASE(EEOP_ASSIGN_TMP)
     872                 :             :                 {
     873                 :     4316357 :                         int                     resultnum = op->d.assign_tmp.resultnum;
     874                 :             : 
     875         [ +  - ]:     4316357 :                         Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
     876                 :     4316357 :                         resultslot->tts_values[resultnum] = state->resvalue;
     877                 :     4316357 :                         resultslot->tts_isnull[resultnum] = state->resnull;
     878                 :             : 
     879                 :     4316357 :                         EEO_NEXT();
     880                 :           0 :                 }
     881                 :             : 
     882                 :             :                 EEO_CASE(EEOP_ASSIGN_TMP_MAKE_RO)
     883                 :             :                 {
     884                 :     1002585 :                         int                     resultnum = op->d.assign_tmp.resultnum;
     885                 :             : 
     886         [ +  - ]:     1002585 :                         Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts);
     887                 :     1002585 :                         resultslot->tts_isnull[resultnum] = state->resnull;
     888         [ +  + ]:     1002585 :                         if (!resultslot->tts_isnull[resultnum])
     889                 :      876854 :                                 resultslot->tts_values[resultnum] =
     890                 :      876854 :                                         MakeExpandedObjectReadOnlyInternal(state->resvalue);
     891                 :             :                         else
     892                 :      125731 :                                 resultslot->tts_values[resultnum] = state->resvalue;
     893                 :             : 
     894                 :     1002585 :                         EEO_NEXT();
     895                 :           0 :                 }
     896                 :             : 
     897                 :             :                 EEO_CASE(EEOP_CONST)
     898                 :             :                 {
     899                 :     2320978 :                         *op->resnull = op->d.constval.isnull;
     900                 :     2320978 :                         *op->resvalue = op->d.constval.value;
     901                 :             : 
     902                 :     2320978 :                         EEO_NEXT();
     903                 :           0 :                 }
     904                 :             : 
     905                 :             :                 /*
     906                 :             :                  * Function-call implementations. Arguments have previously been
     907                 :             :                  * evaluated directly into fcinfo->args.
     908                 :             :                  *
     909                 :             :                  * As both STRICT checks and function-usage are noticeable performance
     910                 :             :                  * wise, and function calls are a very hot-path (they also back
     911                 :             :                  * operators!), it's worth having so many separate opcodes.
     912                 :             :                  *
     913                 :             :                  * Note: the reason for using a temporary variable "d", here and in
     914                 :             :                  * other places, is that some compilers think "*op->resvalue = f();"
     915                 :             :                  * requires them to evaluate op->resvalue into a register before
     916                 :             :                  * calling f(), just in case f() is able to modify op->resvalue
     917                 :             :                  * somehow.  The extra line of code can save a useless register spill
     918                 :             :                  * and reload across the function call.
     919                 :             :                  */
     920                 :             :                 EEO_CASE(EEOP_FUNCEXPR)
     921                 :             :                 {
     922                 :       71330 :                         FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
     923                 :             :                         Datum           d;
     924                 :             : 
     925                 :       71330 :                         fcinfo->isnull = false;
     926                 :       71330 :                         d = op->d.func.fn_addr(fcinfo);
     927                 :       71330 :                         *op->resvalue = d;
     928                 :       71330 :                         *op->resnull = fcinfo->isnull;
     929                 :             : 
     930                 :       71330 :                         EEO_NEXT();
     931                 :           0 :                 }
     932                 :             : 
     933                 :             :                 /* strict function call with more than two arguments */
     934                 :             :                 EEO_CASE(EEOP_FUNCEXPR_STRICT)
     935                 :             :                 {
     936                 :       56405 :                         FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
     937                 :       56405 :                         NullableDatum *args = fcinfo->args;
     938                 :       56405 :                         int                     nargs = op->d.func.nargs;
     939                 :             :                         Datum           d;
     940                 :             : 
     941         [ -  + ]:       56405 :                         Assert(nargs > 2);
     942                 :             : 
     943                 :             :                         /* strict function, so check for NULL args */
     944         [ +  + ]:      204333 :                         for (int argno = 0; argno < nargs; argno++)
     945                 :             :                         {
     946         [ +  + ]:      155655 :                                 if (args[argno].isnull)
     947                 :             :                                 {
     948                 :        7727 :                                         *op->resnull = true;
     949                 :        7727 :                                         goto strictfail;
     950                 :             :                                 }
     951                 :      147928 :                         }
     952                 :       48678 :                         fcinfo->isnull = false;
     953                 :       48678 :                         d = op->d.func.fn_addr(fcinfo);
     954                 :       48678 :                         *op->resvalue = d;
     955                 :       48678 :                         *op->resnull = fcinfo->isnull;
     956                 :             : 
     957                 :             :         strictfail:
     958                 :       56405 :                         EEO_NEXT();
     959                 :           0 :                 }
     960                 :             : 
     961                 :             :                 /* strict function call with one argument */
     962                 :             :                 EEO_CASE(EEOP_FUNCEXPR_STRICT_1)
     963                 :             :                 {
     964                 :     1431459 :                         FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
     965                 :     1431459 :                         NullableDatum *args = fcinfo->args;
     966                 :             : 
     967         [ -  + ]:     1431459 :                         Assert(op->d.func.nargs == 1);
     968                 :             : 
     969                 :             :                         /* strict function, so check for NULL args */
     970         [ +  + ]:     1431459 :                         if (args[0].isnull)
     971                 :        5371 :                                 *op->resnull = true;
     972                 :             :                         else
     973                 :             :                         {
     974                 :             :                                 Datum           d;
     975                 :             : 
     976                 :     1426088 :                                 fcinfo->isnull = false;
     977                 :     1426088 :                                 d = op->d.func.fn_addr(fcinfo);
     978                 :     1426088 :                                 *op->resvalue = d;
     979                 :     1426088 :                                 *op->resnull = fcinfo->isnull;
     980                 :             :                         }
     981                 :             : 
     982                 :     1431459 :                         EEO_NEXT();
     983                 :           0 :                 }
     984                 :             : 
     985                 :             :                 /* strict function call with two arguments */
     986                 :             :                 EEO_CASE(EEOP_FUNCEXPR_STRICT_2)
     987                 :             :                 {
     988                 :    19102373 :                         FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
     989                 :    19102373 :                         NullableDatum *args = fcinfo->args;
     990                 :             : 
     991         [ -  + ]:    19102373 :                         Assert(op->d.func.nargs == 2);
     992                 :             : 
     993                 :             :                         /* strict function, so check for NULL args */
     994   [ +  +  +  + ]:    19102373 :                         if (args[0].isnull || args[1].isnull)
     995                 :      139070 :                                 *op->resnull = true;
     996                 :             :                         else
     997                 :             :                         {
     998                 :             :                                 Datum           d;
     999                 :             : 
    1000                 :    18963303 :                                 fcinfo->isnull = false;
    1001                 :    18963303 :                                 d = op->d.func.fn_addr(fcinfo);
    1002                 :    18963303 :                                 *op->resvalue = d;
    1003                 :    18963303 :                                 *op->resnull = fcinfo->isnull;
    1004                 :             :                         }
    1005                 :             : 
    1006                 :    19102373 :                         EEO_NEXT();
    1007                 :           0 :                 }
    1008                 :             : 
    1009                 :             :                 EEO_CASE(EEOP_FUNCEXPR_FUSAGE)
    1010                 :             :                 {
    1011                 :             :                         /* not common enough to inline */
    1012                 :           8 :                         ExecEvalFuncExprFusage(state, op, econtext);
    1013                 :             : 
    1014                 :           8 :                         EEO_NEXT();
    1015                 :           0 :                 }
    1016                 :             : 
    1017                 :             :                 EEO_CASE(EEOP_FUNCEXPR_STRICT_FUSAGE)
    1018                 :             :                 {
    1019                 :             :                         /* not common enough to inline */
    1020                 :           1 :                         ExecEvalFuncExprStrictFusage(state, op, econtext);
    1021                 :             : 
    1022                 :           1 :                         EEO_NEXT();
    1023                 :           0 :                 }
    1024                 :             : 
    1025                 :             :                 /*
    1026                 :             :                  * If any of its clauses is FALSE, an AND's result is FALSE regardless
    1027                 :             :                  * of the states of the rest of the clauses, so we can stop evaluating
    1028                 :             :                  * and return FALSE immediately.  If none are FALSE and one or more is
    1029                 :             :                  * NULL, we return NULL; otherwise we return TRUE.  This makes sense
    1030                 :             :                  * when you interpret NULL as "don't know": perhaps one of the "don't
    1031                 :             :                  * knows" would have been FALSE if we'd known its value.  Only when
    1032                 :             :                  * all the inputs are known to be TRUE can we state confidently that
    1033                 :             :                  * the AND's result is TRUE.
    1034                 :             :                  */
    1035                 :             :                 EEO_CASE(EEOP_BOOL_AND_STEP_FIRST)
    1036                 :             :                 {
    1037                 :      115682 :                         *op->d.boolexpr.anynull = false;
    1038                 :             : 
    1039                 :             :                         /*
    1040                 :             :                          * EEOP_BOOL_AND_STEP_FIRST resets anynull, otherwise it's the
    1041                 :             :                          * same as EEOP_BOOL_AND_STEP - so fall through to that.
    1042                 :             :                          */
    1043                 :             : 
    1044                 :             :                         /* FALL THROUGH */
    1045                 :      115682 :                 }
    1046                 :             : 
    1047                 :             :                 EEO_CASE(EEOP_BOOL_AND_STEP)
    1048                 :             :                 {
    1049         [ +  + ]:      119699 :                         if (*op->resnull)
    1050                 :             :                         {
    1051                 :           8 :                                 *op->d.boolexpr.anynull = true;
    1052                 :           8 :                         }
    1053         [ +  + ]:      119691 :                         else if (!DatumGetBool(*op->resvalue))
    1054                 :             :                         {
    1055                 :             :                                 /* result is already set to FALSE, need not change it */
    1056                 :             :                                 /* bail out early */
    1057                 :       95766 :                                 EEO_JUMP(op->d.boolexpr.jumpdone);
    1058                 :           0 :                         }
    1059                 :             : 
    1060                 :       23933 :                         EEO_NEXT();
    1061                 :           0 :                 }
    1062                 :             : 
    1063                 :             :                 EEO_CASE(EEOP_BOOL_AND_STEP_LAST)
    1064                 :             :                 {
    1065         [ +  + ]:       19916 :                         if (*op->resnull)
    1066                 :             :                         {
    1067                 :             :                                 /* result is already set to NULL, need not change it */
    1068                 :          15 :                         }
    1069         [ +  + ]:       19901 :                         else if (!DatumGetBool(*op->resvalue))
    1070                 :             :                         {
    1071                 :             :                                 /* result is already set to FALSE, need not change it */
    1072                 :             : 
    1073                 :             :                                 /*
    1074                 :             :                                  * No point jumping early to jumpdone - would be same target
    1075                 :             :                                  * (as this is the last argument to the AND expression),
    1076                 :             :                                  * except more expensive.
    1077                 :             :                                  */
    1078                 :       11359 :                         }
    1079         [ +  + ]:        8542 :                         else if (*op->d.boolexpr.anynull)
    1080                 :             :                         {
    1081                 :           2 :                                 *op->resvalue = (Datum) 0;
    1082                 :           2 :                                 *op->resnull = true;
    1083                 :           2 :                         }
    1084                 :             :                         else
    1085                 :             :                         {
    1086                 :             :                                 /* result is already set to TRUE, need not change it */
    1087                 :             :                         }
    1088                 :             : 
    1089                 :       19916 :                         EEO_NEXT();
    1090                 :           0 :                 }
    1091                 :             : 
    1092                 :             :                 /*
    1093                 :             :                  * If any of its clauses is TRUE, an OR's result is TRUE regardless of
    1094                 :             :                  * the states of the rest of the clauses, so we can stop evaluating
    1095                 :             :                  * and return TRUE immediately.  If none are TRUE and one or more is
    1096                 :             :                  * NULL, we return NULL; otherwise we return FALSE.  This makes sense
    1097                 :             :                  * when you interpret NULL as "don't know": perhaps one of the "don't
    1098                 :             :                  * knows" would have been TRUE if we'd known its value.  Only when all
    1099                 :             :                  * the inputs are known to be FALSE can we state confidently that the
    1100                 :             :                  * OR's result is FALSE.
    1101                 :             :                  */
    1102                 :             :                 EEO_CASE(EEOP_BOOL_OR_STEP_FIRST)
    1103                 :             :                 {
    1104                 :      371408 :                         *op->d.boolexpr.anynull = false;
    1105                 :             : 
    1106                 :             :                         /*
    1107                 :             :                          * EEOP_BOOL_OR_STEP_FIRST resets anynull, otherwise it's the same
    1108                 :             :                          * as EEOP_BOOL_OR_STEP - so fall through to that.
    1109                 :             :                          */
    1110                 :             : 
    1111                 :             :                         /* FALL THROUGH */
    1112                 :      371408 :                 }
    1113                 :             : 
    1114                 :             :                 EEO_CASE(EEOP_BOOL_OR_STEP)
    1115                 :             :                 {
    1116         [ +  + ]:      599655 :                         if (*op->resnull)
    1117                 :             :                         {
    1118                 :       26178 :                                 *op->d.boolexpr.anynull = true;
    1119                 :       26178 :                         }
    1120         [ +  + ]:      573477 :                         else if (DatumGetBool(*op->resvalue))
    1121                 :             :                         {
    1122                 :             :                                 /* result is already set to TRUE, need not change it */
    1123                 :             :                                 /* bail out early */
    1124                 :       69918 :                                 EEO_JUMP(op->d.boolexpr.jumpdone);
    1125                 :           0 :                         }
    1126                 :             : 
    1127                 :      529737 :                         EEO_NEXT();
    1128                 :           0 :                 }
    1129                 :             : 
    1130                 :             :                 EEO_CASE(EEOP_BOOL_OR_STEP_LAST)
    1131                 :             :                 {
    1132         [ +  + ]:      301490 :                         if (*op->resnull)
    1133                 :             :                         {
    1134                 :             :                                 /* result is already set to NULL, need not change it */
    1135                 :       24278 :                         }
    1136         [ +  + ]:      277212 :                         else if (DatumGetBool(*op->resvalue))
    1137                 :             :                         {
    1138                 :             :                                 /* result is already set to TRUE, need not change it */
    1139                 :             : 
    1140                 :             :                                 /*
    1141                 :             :                                  * No point jumping to jumpdone - would be same target (as
    1142                 :             :                                  * this is the last argument to the AND expression), except
    1143                 :             :                                  * more expensive.
    1144                 :             :                                  */
    1145                 :        5711 :                         }
    1146         [ +  + ]:      271501 :                         else if (*op->d.boolexpr.anynull)
    1147                 :             :                         {
    1148                 :        1127 :                                 *op->resvalue = (Datum) 0;
    1149                 :        1127 :                                 *op->resnull = true;
    1150                 :        1127 :                         }
    1151                 :             :                         else
    1152                 :             :                         {
    1153                 :             :                                 /* result is already set to FALSE, need not change it */
    1154                 :             :                         }
    1155                 :             : 
    1156                 :      301490 :                         EEO_NEXT();
    1157                 :           0 :                 }
    1158                 :             : 
    1159                 :             :                 EEO_CASE(EEOP_BOOL_NOT_STEP)
    1160                 :             :                 {
    1161                 :             :                         /*
    1162                 :             :                          * Evaluation of 'not' is simple... if expr is false, then return
    1163                 :             :                          * 'true' and vice versa.  It's safe to do this even on a
    1164                 :             :                          * nominally null value, so we ignore resnull; that means that
    1165                 :             :                          * NULL in produces NULL out, which is what we want.
    1166                 :             :                          */
    1167                 :      139047 :                         *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
    1168                 :             : 
    1169                 :      139047 :                         EEO_NEXT();
    1170                 :           0 :                 }
    1171                 :             : 
    1172                 :             :                 EEO_CASE(EEOP_QUAL)
    1173                 :             :                 {
    1174                 :             :                         /* simplified version of BOOL_AND_STEP for use by ExecQual() */
    1175                 :             : 
    1176                 :             :                         /* If argument (also result) is false or null ... */
    1177   [ +  +  +  + ]:    16956919 :                         if (*op->resnull ||
    1178                 :    16820429 :                                 !DatumGetBool(*op->resvalue))
    1179                 :             :                         {
    1180                 :             :                                 /* ... bail out early, returning FALSE */
    1181                 :    10807437 :                                 *op->resnull = false;
    1182                 :    10807437 :                                 *op->resvalue = BoolGetDatum(false);
    1183                 :    10807437 :                                 EEO_JUMP(op->d.qualexpr.jumpdone);
    1184                 :           0 :                         }
    1185                 :             : 
    1186                 :             :                         /*
    1187                 :             :                          * Otherwise, leave the TRUE value in place, in case this is the
    1188                 :             :                          * last qual.  Then, TRUE is the correct answer.
    1189                 :             :                          */
    1190                 :             : 
    1191                 :     6149482 :                         EEO_NEXT();
    1192                 :           0 :                 }
    1193                 :             : 
    1194                 :             :                 EEO_CASE(EEOP_JUMP)
    1195                 :             :                 {
    1196                 :             :                         /* Unconditionally jump to target step */
    1197                 :       16772 :                         EEO_JUMP(op->d.jump.jumpdone);
    1198                 :           0 :                 }
    1199                 :             : 
    1200                 :             :                 EEO_CASE(EEOP_JUMP_IF_NULL)
    1201                 :             :                 {
    1202                 :             :                         /* Transfer control if current result is null */
    1203         [ +  + ]:       45607 :                         if (*op->resnull)
    1204                 :         521 :                                 EEO_JUMP(op->d.jump.jumpdone);
    1205                 :             : 
    1206                 :       45086 :                         EEO_NEXT();
    1207                 :           0 :                 }
    1208                 :             : 
    1209                 :             :                 EEO_CASE(EEOP_JUMP_IF_NOT_NULL)
    1210                 :             :                 {
    1211                 :             :                         /* Transfer control if current result is non-null */
    1212         [ +  + ]:       56052 :                         if (!*op->resnull)
    1213                 :       33214 :                                 EEO_JUMP(op->d.jump.jumpdone);
    1214                 :             : 
    1215                 :       22838 :                         EEO_NEXT();
    1216                 :           0 :                 }
    1217                 :             : 
    1218                 :             :                 EEO_CASE(EEOP_JUMP_IF_NOT_TRUE)
    1219                 :             :                 {
    1220                 :             :                         /* Transfer control if current result is null or false */
    1221   [ +  +  +  + ]:      179196 :                         if (*op->resnull || !DatumGetBool(*op->resvalue))
    1222                 :      142978 :                                 EEO_JUMP(op->d.jump.jumpdone);
    1223                 :             : 
    1224                 :       36218 :                         EEO_NEXT();
    1225                 :           0 :                 }
    1226                 :             : 
    1227                 :             :                 EEO_CASE(EEOP_NULLTEST_ISNULL)
    1228                 :             :                 {
    1229                 :      102049 :                         *op->resvalue = BoolGetDatum(*op->resnull);
    1230                 :      102049 :                         *op->resnull = false;
    1231                 :             : 
    1232                 :      102049 :                         EEO_NEXT();
    1233                 :           0 :                 }
    1234                 :             : 
    1235                 :             :                 EEO_CASE(EEOP_NULLTEST_ISNOTNULL)
    1236                 :             :                 {
    1237                 :      128800 :                         *op->resvalue = BoolGetDatum(!*op->resnull);
    1238                 :      128800 :                         *op->resnull = false;
    1239                 :             : 
    1240                 :      128800 :                         EEO_NEXT();
    1241                 :           0 :                 }
    1242                 :             : 
    1243                 :             :                 EEO_CASE(EEOP_NULLTEST_ROWISNULL)
    1244                 :             :                 {
    1245                 :             :                         /* out of line implementation: too large */
    1246                 :          95 :                         ExecEvalRowNull(state, op, econtext);
    1247                 :             : 
    1248                 :          95 :                         EEO_NEXT();
    1249                 :           0 :                 }
    1250                 :             : 
    1251                 :             :                 EEO_CASE(EEOP_NULLTEST_ROWISNOTNULL)
    1252                 :             :                 {
    1253                 :             :                         /* out of line implementation: too large */
    1254                 :          59 :                         ExecEvalRowNotNull(state, op, econtext);
    1255                 :             : 
    1256                 :          59 :                         EEO_NEXT();
    1257                 :           0 :                 }
    1258                 :             : 
    1259                 :             :                 /* BooleanTest implementations for all booltesttypes */
    1260                 :             : 
    1261                 :             :                 EEO_CASE(EEOP_BOOLTEST_IS_TRUE)
    1262                 :             :                 {
    1263         [ +  + ]:          20 :                         if (*op->resnull)
    1264                 :             :                         {
    1265                 :           4 :                                 *op->resvalue = BoolGetDatum(false);
    1266                 :           4 :                                 *op->resnull = false;
    1267                 :           4 :                         }
    1268                 :             :                         /* else, input value is the correct output as well */
    1269                 :             : 
    1270                 :          20 :                         EEO_NEXT();
    1271                 :           0 :                 }
    1272                 :             : 
    1273                 :             :                 EEO_CASE(EEOP_BOOLTEST_IS_NOT_TRUE)
    1274                 :             :                 {
    1275         [ +  + ]:          94 :                         if (*op->resnull)
    1276                 :             :                         {
    1277                 :          11 :                                 *op->resvalue = BoolGetDatum(true);
    1278                 :          11 :                                 *op->resnull = false;
    1279                 :          11 :                         }
    1280                 :             :                         else
    1281                 :          83 :                                 *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
    1282                 :             : 
    1283                 :          94 :                         EEO_NEXT();
    1284                 :           0 :                 }
    1285                 :             : 
    1286                 :             :                 EEO_CASE(EEOP_BOOLTEST_IS_FALSE)
    1287                 :             :                 {
    1288         [ +  + ]:          13 :                         if (*op->resnull)
    1289                 :             :                         {
    1290                 :           2 :                                 *op->resvalue = BoolGetDatum(false);
    1291                 :           2 :                                 *op->resnull = false;
    1292                 :           2 :                         }
    1293                 :             :                         else
    1294                 :          11 :                                 *op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue));
    1295                 :             : 
    1296                 :          13 :                         EEO_NEXT();
    1297                 :           0 :                 }
    1298                 :             : 
    1299                 :             :                 EEO_CASE(EEOP_BOOLTEST_IS_NOT_FALSE)
    1300                 :             :                 {
    1301         [ +  + ]:          90 :                         if (*op->resnull)
    1302                 :             :                         {
    1303                 :           7 :                                 *op->resvalue = BoolGetDatum(true);
    1304                 :           7 :                                 *op->resnull = false;
    1305                 :           7 :                         }
    1306                 :             :                         /* else, input value is the correct output as well */
    1307                 :             : 
    1308                 :          90 :                         EEO_NEXT();
    1309                 :           0 :                 }
    1310                 :             : 
    1311                 :             :                 EEO_CASE(EEOP_PARAM_EXEC)
    1312                 :             :                 {
    1313                 :             :                         /* out of line implementation: too large */
    1314                 :      904019 :                         ExecEvalParamExec(state, op, econtext);
    1315                 :             : 
    1316                 :      904019 :                         EEO_NEXT();
    1317                 :           0 :                 }
    1318                 :             : 
    1319                 :             :                 EEO_CASE(EEOP_PARAM_EXTERN)
    1320                 :             :                 {
    1321                 :             :                         /* out of line implementation: too large */
    1322                 :     5558256 :                         ExecEvalParamExtern(state, op, econtext);
    1323                 :     5558256 :                         EEO_NEXT();
    1324                 :           0 :                 }
    1325                 :             : 
    1326                 :             :                 EEO_CASE(EEOP_PARAM_CALLBACK)
    1327                 :             :                 {
    1328                 :             :                         /* allow an extension module to supply a PARAM_EXTERN value */
    1329                 :       52272 :                         op->d.cparam.paramfunc(state, op, econtext);
    1330                 :       52272 :                         EEO_NEXT();
    1331                 :           0 :                 }
    1332                 :             : 
    1333                 :             :                 EEO_CASE(EEOP_PARAM_SET)
    1334                 :             :                 {
    1335                 :             :                         /* out of line, unlikely to matter performance-wise */
    1336                 :       30603 :                         ExecEvalParamSet(state, op, econtext);
    1337                 :       30603 :                         EEO_NEXT();
    1338                 :           0 :                 }
    1339                 :             : 
    1340                 :             :                 EEO_CASE(EEOP_CASE_TESTVAL)
    1341                 :             :                 {
    1342                 :        6419 :                         *op->resvalue = *op->d.casetest.value;
    1343                 :        6419 :                         *op->resnull = *op->d.casetest.isnull;
    1344                 :             : 
    1345                 :        6419 :                         EEO_NEXT();
    1346                 :           0 :                 }
    1347                 :             : 
    1348                 :             :                 EEO_CASE(EEOP_CASE_TESTVAL_EXT)
    1349                 :             :                 {
    1350                 :        1035 :                         *op->resvalue = econtext->caseValue_datum;
    1351                 :        1035 :                         *op->resnull = econtext->caseValue_isNull;
    1352                 :             : 
    1353                 :        1035 :                         EEO_NEXT();
    1354                 :           0 :                 }
    1355                 :             : 
    1356                 :             :                 EEO_CASE(EEOP_MAKE_READONLY)
    1357                 :             :                 {
    1358                 :             :                         /*
    1359                 :             :                          * Force a varlena value that might be read multiple times to R/O
    1360                 :             :                          */
    1361         [ +  + ]:         583 :                         if (!*op->d.make_readonly.isnull)
    1362                 :         575 :                                 *op->resvalue =
    1363                 :         575 :                                         MakeExpandedObjectReadOnlyInternal(*op->d.make_readonly.value);
    1364                 :         583 :                         *op->resnull = *op->d.make_readonly.isnull;
    1365                 :             : 
    1366                 :         583 :                         EEO_NEXT();
    1367                 :           0 :                 }
    1368                 :             : 
    1369                 :             :                 EEO_CASE(EEOP_IOCOERCE)
    1370                 :             :                 {
    1371                 :             :                         /*
    1372                 :             :                          * Evaluate a CoerceViaIO node.  This can be quite a hot path, so
    1373                 :             :                          * inline as much work as possible.  The source value is in our
    1374                 :             :                          * result variable.
    1375                 :             :                          *
    1376                 :             :                          * Also look at ExecEvalCoerceViaIOSafe() if you change anything
    1377                 :             :                          * here.
    1378                 :             :                          */
    1379                 :             :                         char       *str;
    1380                 :             : 
    1381                 :             :                         /* call output function (similar to OutputFunctionCall) */
    1382         [ +  + ]:      701877 :                         if (*op->resnull)
    1383                 :             :                         {
    1384                 :             :                                 /* output functions are not called on nulls */
    1385                 :       10310 :                                 str = NULL;
    1386                 :       10310 :                         }
    1387                 :             :                         else
    1388                 :             :                         {
    1389                 :             :                                 FunctionCallInfo fcinfo_out;
    1390                 :             : 
    1391                 :      691567 :                                 fcinfo_out = op->d.iocoerce.fcinfo_data_out;
    1392                 :      691567 :                                 fcinfo_out->args[0].value = *op->resvalue;
    1393                 :      691567 :                                 fcinfo_out->args[0].isnull = false;
    1394                 :             : 
    1395                 :      691567 :                                 fcinfo_out->isnull = false;
    1396                 :      691567 :                                 str = DatumGetCString(FunctionCallInvoke(fcinfo_out));
    1397                 :             : 
    1398                 :             :                                 /* OutputFunctionCall assumes result isn't null */
    1399         [ +  - ]:      691567 :                                 Assert(!fcinfo_out->isnull);
    1400                 :             :                         }
    1401                 :             : 
    1402                 :             :                         /* call input function (similar to InputFunctionCall) */
    1403   [ +  +  +  + ]:      701877 :                         if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL)
    1404                 :             :                         {
    1405                 :             :                                 FunctionCallInfo fcinfo_in;
    1406                 :             :                                 Datum           d;
    1407                 :             : 
    1408                 :      691581 :                                 fcinfo_in = op->d.iocoerce.fcinfo_data_in;
    1409                 :      691581 :                                 fcinfo_in->args[0].value = PointerGetDatum(str);
    1410                 :      691581 :                                 fcinfo_in->args[0].isnull = *op->resnull;
    1411                 :             :                                 /* second and third arguments are already set up */
    1412                 :             : 
    1413                 :      691581 :                                 fcinfo_in->isnull = false;
    1414                 :      691581 :                                 d = FunctionCallInvoke(fcinfo_in);
    1415                 :      691581 :                                 *op->resvalue = d;
    1416                 :             : 
    1417                 :             :                                 /* Should get null result if and only if str is NULL */
    1418         [ +  + ]:      691581 :                                 if (str == NULL)
    1419                 :             :                                 {
    1420         [ +  - ]:          18 :                                         Assert(*op->resnull);
    1421         [ -  + ]:          18 :                                         Assert(fcinfo_in->isnull);
    1422                 :          18 :                                 }
    1423                 :             :                                 else
    1424                 :             :                                 {
    1425         [ -  + ]:      691563 :                                         Assert(!*op->resnull);
    1426         [ -  + ]:      691563 :                                         Assert(!fcinfo_in->isnull);
    1427                 :             :                                 }
    1428                 :      691581 :                         }
    1429                 :             : 
    1430                 :      701877 :                         EEO_NEXT();
    1431                 :           0 :                 }
    1432                 :             : 
    1433                 :             :                 EEO_CASE(EEOP_IOCOERCE_SAFE)
    1434                 :             :                 {
    1435                 :           0 :                         ExecEvalCoerceViaIOSafe(state, op);
    1436                 :           0 :                         EEO_NEXT();
    1437                 :           0 :                 }
    1438                 :             : 
    1439                 :             :                 EEO_CASE(EEOP_DISTINCT)
    1440                 :             :                 {
    1441                 :             :                         /*
    1442                 :             :                          * IS DISTINCT FROM must evaluate arguments (already done into
    1443                 :             :                          * fcinfo->args) to determine whether they are NULL; if either is
    1444                 :             :                          * NULL then the result is determined.  If neither is NULL, then
    1445                 :             :                          * proceed to evaluate the comparison function, which is just the
    1446                 :             :                          * type's standard equality operator.  We need not care whether
    1447                 :             :                          * that function is strict.  Because the handling of nulls is
    1448                 :             :                          * different, we can't just reuse EEOP_FUNCEXPR.
    1449                 :             :                          */
    1450                 :       53643 :                         FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
    1451                 :             : 
    1452                 :             :                         /* check function arguments for NULLness */
    1453   [ +  +  +  + ]:       53643 :                         if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
    1454                 :             :                         {
    1455                 :             :                                 /* Both NULL? Then is not distinct... */
    1456                 :          46 :                                 *op->resvalue = BoolGetDatum(false);
    1457                 :          46 :                                 *op->resnull = false;
    1458                 :          46 :                         }
    1459   [ +  +  +  + ]:       53597 :                         else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
    1460                 :             :                         {
    1461                 :             :                                 /* Only one is NULL? Then is distinct... */
    1462                 :          31 :                                 *op->resvalue = BoolGetDatum(true);
    1463                 :          31 :                                 *op->resnull = false;
    1464                 :          31 :                         }
    1465                 :             :                         else
    1466                 :             :                         {
    1467                 :             :                                 /* Neither null, so apply the equality function */
    1468                 :             :                                 Datum           eqresult;
    1469                 :             : 
    1470                 :       53566 :                                 fcinfo->isnull = false;
    1471                 :       53566 :                                 eqresult = op->d.func.fn_addr(fcinfo);
    1472                 :             :                                 /* Must invert result of "="; safe to do even if null */
    1473                 :       53566 :                                 *op->resvalue = BoolGetDatum(!DatumGetBool(eqresult));
    1474                 :       53566 :                                 *op->resnull = fcinfo->isnull;
    1475                 :             :                         }
    1476                 :             : 
    1477                 :       53643 :                         EEO_NEXT();
    1478                 :           0 :                 }
    1479                 :             : 
    1480                 :             :                 /* see EEOP_DISTINCT for comments, this is just inverted */
    1481                 :             :                 EEO_CASE(EEOP_NOT_DISTINCT)
    1482                 :             :                 {
    1483                 :     2596995 :                         FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
    1484                 :             : 
    1485   [ +  +  +  + ]:     2596995 :                         if (fcinfo->args[0].isnull && fcinfo->args[1].isnull)
    1486                 :             :                         {
    1487                 :       21169 :                                 *op->resvalue = BoolGetDatum(true);
    1488                 :       21169 :                                 *op->resnull = false;
    1489                 :       21169 :                         }
    1490   [ +  +  +  + ]:     2575826 :                         else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull)
    1491                 :             :                         {
    1492                 :          70 :                                 *op->resvalue = BoolGetDatum(false);
    1493                 :          70 :                                 *op->resnull = false;
    1494                 :          70 :                         }
    1495                 :             :                         else
    1496                 :             :                         {
    1497                 :             :                                 Datum           eqresult;
    1498                 :             : 
    1499                 :     2575756 :                                 fcinfo->isnull = false;
    1500                 :     2575756 :                                 eqresult = op->d.func.fn_addr(fcinfo);
    1501                 :     2575756 :                                 *op->resvalue = eqresult;
    1502                 :     2575756 :                                 *op->resnull = fcinfo->isnull;
    1503                 :             :                         }
    1504                 :             : 
    1505                 :     2596995 :                         EEO_NEXT();
    1506                 :           0 :                 }
    1507                 :             : 
    1508                 :             :                 EEO_CASE(EEOP_NULLIF)
    1509                 :             :                 {
    1510                 :             :                         /*
    1511                 :             :                          * The arguments are already evaluated into fcinfo->args.
    1512                 :             :                          */
    1513                 :        1186 :                         FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
    1514                 :        1186 :                         Datum           save_arg0 = fcinfo->args[0].value;
    1515                 :             : 
    1516                 :             :                         /* if either argument is NULL they can't be equal */
    1517   [ +  +  +  + ]:        1186 :                         if (!fcinfo->args[0].isnull && !fcinfo->args[1].isnull)
    1518                 :             :                         {
    1519                 :             :                                 Datum           result;
    1520                 :             : 
    1521                 :             :                                 /*
    1522                 :             :                                  * If first argument is of varlena type, it might be an
    1523                 :             :                                  * expanded datum.  We need to ensure that the value passed to
    1524                 :             :                                  * the comparison function is a read-only pointer.  However,
    1525                 :             :                                  * if we end by returning the first argument, that will be the
    1526                 :             :                                  * original read-write pointer if it was read-write.
    1527                 :             :                                  */
    1528         [ +  + ]:        1176 :                                 if (op->d.func.make_ro)
    1529                 :          40 :                                         fcinfo->args[0].value =
    1530                 :          40 :                                                 MakeExpandedObjectReadOnlyInternal(save_arg0);
    1531                 :             : 
    1532                 :        1176 :                                 fcinfo->isnull = false;
    1533                 :        1176 :                                 result = op->d.func.fn_addr(fcinfo);
    1534                 :             : 
    1535                 :             :                                 /* if the arguments are equal return null */
    1536   [ +  -  +  + ]:        1176 :                                 if (!fcinfo->isnull && DatumGetBool(result))
    1537                 :             :                                 {
    1538                 :          32 :                                         *op->resvalue = (Datum) 0;
    1539                 :          32 :                                         *op->resnull = true;
    1540                 :             : 
    1541                 :          32 :                                         EEO_NEXT();
    1542                 :           0 :                                 }
    1543                 :        1144 :                         }
    1544                 :             : 
    1545                 :             :                         /* Arguments aren't equal, so return the first one */
    1546                 :        1154 :                         *op->resvalue = save_arg0;
    1547                 :        1154 :                         *op->resnull = fcinfo->args[0].isnull;
    1548                 :             : 
    1549                 :        1154 :                         EEO_NEXT();
    1550                 :           0 :                 }
    1551                 :             : 
    1552                 :             :                 EEO_CASE(EEOP_SQLVALUEFUNCTION)
    1553                 :             :                 {
    1554                 :             :                         /*
    1555                 :             :                          * Doesn't seem worthwhile to have an inline implementation
    1556                 :             :                          * efficiency-wise.
    1557                 :             :                          */
    1558                 :        1262 :                         ExecEvalSQLValueFunction(state, op);
    1559                 :             : 
    1560                 :        1262 :                         EEO_NEXT();
    1561                 :           0 :                 }
    1562                 :             : 
    1563                 :             :                 EEO_CASE(EEOP_CURRENTOFEXPR)
    1564                 :             :                 {
    1565                 :             :                         /* error invocation uses space, and shouldn't ever occur */
    1566                 :           0 :                         ExecEvalCurrentOfExpr(state, op);
    1567                 :             : 
    1568                 :           0 :                         EEO_NEXT();
    1569                 :           0 :                 }
    1570                 :             : 
    1571                 :             :                 EEO_CASE(EEOP_NEXTVALUEEXPR)
    1572                 :             :                 {
    1573                 :             :                         /*
    1574                 :             :                          * Doesn't seem worthwhile to have an inline implementation
    1575                 :             :                          * efficiency-wise.
    1576                 :             :                          */
    1577                 :         170 :                         ExecEvalNextValueExpr(state, op);
    1578                 :             : 
    1579                 :         170 :                         EEO_NEXT();
    1580                 :           0 :                 }
    1581                 :             : 
    1582                 :             :                 EEO_CASE(EEOP_RETURNINGEXPR)
    1583                 :             :                 {
    1584                 :             :                         /*
    1585                 :             :                          * The next op actually evaluates the expression.  If the OLD/NEW
    1586                 :             :                          * row doesn't exist, skip that and return NULL.
    1587                 :             :                          */
    1588         [ +  + ]:         143 :                         if (state->flags & op->d.returningexpr.nullflag)
    1589                 :             :                         {
    1590                 :          28 :                                 *op->resvalue = (Datum) 0;
    1591                 :          28 :                                 *op->resnull = true;
    1592                 :             : 
    1593                 :          28 :                                 EEO_JUMP(op->d.returningexpr.jumpdone);
    1594                 :           0 :                         }
    1595                 :             : 
    1596                 :         115 :                         EEO_NEXT();
    1597                 :           0 :                 }
    1598                 :             : 
    1599                 :             :                 EEO_CASE(EEOP_ARRAYEXPR)
    1600                 :             :                 {
    1601                 :             :                         /* too complex for an inline implementation */
    1602                 :      120667 :                         ExecEvalArrayExpr(state, op);
    1603                 :             : 
    1604                 :      120667 :                         EEO_NEXT();
    1605                 :           0 :                 }
    1606                 :             : 
    1607                 :             :                 EEO_CASE(EEOP_ARRAYCOERCE)
    1608                 :             :                 {
    1609                 :             :                         /* too complex for an inline implementation */
    1610                 :       11120 :                         ExecEvalArrayCoerce(state, op, econtext);
    1611                 :             : 
    1612                 :       11120 :                         EEO_NEXT();
    1613                 :           0 :                 }
    1614                 :             : 
    1615                 :             :                 EEO_CASE(EEOP_ROW)
    1616                 :             :                 {
    1617                 :             :                         /* too complex for an inline implementation */
    1618                 :        6717 :                         ExecEvalRow(state, op);
    1619                 :             : 
    1620                 :        6717 :                         EEO_NEXT();
    1621                 :           0 :                 }
    1622                 :             : 
    1623                 :             :                 EEO_CASE(EEOP_ROWCOMPARE_STEP)
    1624                 :             :                 {
    1625                 :       34782 :                         FunctionCallInfo fcinfo = op->d.rowcompare_step.fcinfo_data;
    1626                 :             :                         Datum           d;
    1627                 :             : 
    1628                 :             :                         /* force NULL result if strict fn and NULL input */
    1629   [ +  -  +  + ]:       69564 :                         if (op->d.rowcompare_step.finfo->fn_strict &&
    1630         [ +  - ]:       34782 :                                 (fcinfo->args[0].isnull || fcinfo->args[1].isnull))
    1631                 :             :                         {
    1632                 :           3 :                                 *op->resnull = true;
    1633                 :           3 :                                 EEO_JUMP(op->d.rowcompare_step.jumpnull);
    1634                 :           0 :                         }
    1635                 :             : 
    1636                 :             :                         /* Apply comparison function */
    1637                 :       34779 :                         fcinfo->isnull = false;
    1638                 :       34779 :                         d = op->d.rowcompare_step.fn_addr(fcinfo);
    1639                 :       34779 :                         *op->resvalue = d;
    1640                 :             : 
    1641                 :             :                         /* force NULL result if NULL function result */
    1642         [ +  - ]:       34779 :                         if (fcinfo->isnull)
    1643                 :             :                         {
    1644                 :           0 :                                 *op->resnull = true;
    1645                 :           0 :                                 EEO_JUMP(op->d.rowcompare_step.jumpnull);
    1646                 :           0 :                         }
    1647                 :       34779 :                         *op->resnull = false;
    1648                 :             : 
    1649                 :             :                         /* If unequal, no need to compare remaining columns */
    1650         [ +  + ]:       34779 :                         if (DatumGetInt32(*op->resvalue) != 0)
    1651                 :             :                         {
    1652                 :       15752 :                                 EEO_JUMP(op->d.rowcompare_step.jumpdone);
    1653                 :           0 :                         }
    1654                 :             : 
    1655                 :       19027 :                         EEO_NEXT();
    1656                 :           0 :                 }
    1657                 :             : 
    1658                 :             :                 EEO_CASE(EEOP_ROWCOMPARE_FINAL)
    1659                 :             :                 {
    1660                 :       15752 :                         int32           cmpresult = DatumGetInt32(*op->resvalue);
    1661                 :       15752 :                         CompareType cmptype = op->d.rowcompare_final.cmptype;
    1662                 :             : 
    1663                 :       15752 :                         *op->resnull = false;
    1664   [ +  +  -  +  :       15752 :                         switch (cmptype)
                      + ]
    1665                 :             :                         {
    1666                 :             :                                         /* EQ and NE cases aren't allowed here */
    1667                 :             :                                 case COMPARE_LT:
    1668                 :        5734 :                                         *op->resvalue = BoolGetDatum(cmpresult < 0);
    1669                 :        5734 :                                         break;
    1670                 :             :                                 case COMPARE_LE:
    1671                 :       10000 :                                         *op->resvalue = BoolGetDatum(cmpresult <= 0);
    1672                 :       10000 :                                         break;
    1673                 :             :                                 case COMPARE_GE:
    1674                 :           1 :                                         *op->resvalue = BoolGetDatum(cmpresult >= 0);
    1675                 :           1 :                                         break;
    1676                 :             :                                 case COMPARE_GT:
    1677                 :          17 :                                         *op->resvalue = BoolGetDatum(cmpresult > 0);
    1678                 :          17 :                                         break;
    1679                 :             :                                 default:
    1680                 :           0 :                                         Assert(false);
    1681                 :           0 :                                         break;
    1682                 :             :                         }
    1683                 :             : 
    1684                 :       15752 :                         EEO_NEXT();
    1685                 :           0 :                 }
    1686                 :             : 
    1687                 :             :                 EEO_CASE(EEOP_MINMAX)
    1688                 :             :                 {
    1689                 :             :                         /* too complex for an inline implementation */
    1690                 :         154 :                         ExecEvalMinMax(state, op);
    1691                 :             : 
    1692                 :         154 :                         EEO_NEXT();
    1693                 :           0 :                 }
    1694                 :             : 
    1695                 :             :                 EEO_CASE(EEOP_FIELDSELECT)
    1696                 :             :                 {
    1697                 :             :                         /* too complex for an inline implementation */
    1698                 :       65917 :                         ExecEvalFieldSelect(state, op, econtext);
    1699                 :             : 
    1700                 :       65917 :                         EEO_NEXT();
    1701                 :           0 :                 }
    1702                 :             : 
    1703                 :             :                 EEO_CASE(EEOP_FIELDSTORE_DEFORM)
    1704                 :             :                 {
    1705                 :             :                         /* too complex for an inline implementation */
    1706                 :          85 :                         ExecEvalFieldStoreDeForm(state, op, econtext);
    1707                 :             : 
    1708                 :          85 :                         EEO_NEXT();
    1709                 :           0 :                 }
    1710                 :             : 
    1711                 :             :                 EEO_CASE(EEOP_FIELDSTORE_FORM)
    1712                 :             :                 {
    1713                 :             :                         /* too complex for an inline implementation */
    1714                 :          85 :                         ExecEvalFieldStoreForm(state, op, econtext);
    1715                 :             : 
    1716                 :          85 :                         EEO_NEXT();
    1717                 :           0 :                 }
    1718                 :             : 
    1719                 :             :                 EEO_CASE(EEOP_SBSREF_SUBSCRIPTS)
    1720                 :             :                 {
    1721                 :             :                         /* Precheck SubscriptingRef subscript(s) */
    1722         [ +  + ]:       43587 :                         if (op->d.sbsref_subscript.subscriptfunc(state, op, econtext))
    1723                 :             :                         {
    1724                 :       43582 :                                 EEO_NEXT();
    1725                 :           0 :                         }
    1726                 :             :                         else
    1727                 :             :                         {
    1728                 :             :                                 /* Subscript is null, short-circuit SubscriptingRef to NULL */
    1729                 :           5 :                                 EEO_JUMP(op->d.sbsref_subscript.jumpdone);
    1730                 :             :                         }
    1731                 :         307 :                 }
    1732                 :             : 
    1733                 :             :                 EEO_CASE(EEOP_SBSREF_OLD)
    1734                 :             :                         EEO_CASE(EEOP_SBSREF_ASSIGN)
    1735                 :             :                         EEO_CASE(EEOP_SBSREF_FETCH)
    1736                 :             :                 {
    1737                 :             :                         /* Perform a SubscriptingRef fetch or assignment */
    1738                 :       43622 :                         op->d.sbsref.subscriptfunc(state, op, econtext);
    1739                 :             : 
    1740                 :       43622 :                         EEO_NEXT();
    1741                 :           0 :                 }
    1742                 :             : 
    1743                 :             :                 EEO_CASE(EEOP_CONVERT_ROWTYPE)
    1744                 :             :                 {
    1745                 :             :                         /* too complex for an inline implementation */
    1746                 :         967 :                         ExecEvalConvertRowtype(state, op, econtext);
    1747                 :             : 
    1748                 :         967 :                         EEO_NEXT();
    1749                 :           0 :                 }
    1750                 :             : 
    1751                 :             :                 EEO_CASE(EEOP_SCALARARRAYOP)
    1752                 :             :                 {
    1753                 :             :                         /* too complex for an inline implementation */
    1754                 :      532013 :                         ExecEvalScalarArrayOp(state, op);
    1755                 :             : 
    1756                 :      532013 :                         EEO_NEXT();
    1757                 :           0 :                 }
    1758                 :             : 
    1759                 :             :                 EEO_CASE(EEOP_HASHED_SCALARARRAYOP)
    1760                 :             :                 {
    1761                 :             :                         /* too complex for an inline implementation */
    1762                 :         572 :                         ExecEvalHashedScalarArrayOp(state, op, econtext);
    1763                 :             : 
    1764                 :         572 :                         EEO_NEXT();
    1765                 :           0 :                 }
    1766                 :             : 
    1767                 :             :                 EEO_CASE(EEOP_DOMAIN_TESTVAL)
    1768                 :             :                 {
    1769                 :         981 :                         *op->resvalue = *op->d.casetest.value;
    1770                 :         981 :                         *op->resnull = *op->d.casetest.isnull;
    1771                 :             : 
    1772                 :         981 :                         EEO_NEXT();
    1773                 :           0 :                 }
    1774                 :             : 
    1775                 :             :                 EEO_CASE(EEOP_DOMAIN_TESTVAL_EXT)
    1776                 :             :                 {
    1777                 :        1316 :                         *op->resvalue = econtext->domainValue_datum;
    1778                 :        1316 :                         *op->resnull = econtext->domainValue_isNull;
    1779                 :             : 
    1780                 :        1316 :                         EEO_NEXT();
    1781                 :           0 :                 }
    1782                 :             : 
    1783                 :             :                 EEO_CASE(EEOP_DOMAIN_NOTNULL)
    1784                 :             :                 {
    1785                 :             :                         /* too complex for an inline implementation */
    1786                 :          41 :                         ExecEvalConstraintNotNull(state, op);
    1787                 :             : 
    1788                 :          41 :                         EEO_NEXT();
    1789                 :           0 :                 }
    1790                 :             : 
    1791                 :             :                 EEO_CASE(EEOP_DOMAIN_CHECK)
    1792                 :             :                 {
    1793                 :             :                         /* too complex for an inline implementation */
    1794                 :         851 :                         ExecEvalConstraintCheck(state, op);
    1795                 :             : 
    1796                 :         851 :                         EEO_NEXT();
    1797                 :           0 :                 }
    1798                 :             : 
    1799                 :             :                 EEO_CASE(EEOP_HASHDATUM_SET_INITVAL)
    1800                 :             :                 {
    1801                 :       33340 :                         *op->resvalue = op->d.hashdatum_initvalue.init_value;
    1802                 :       33340 :                         *op->resnull = false;
    1803                 :             : 
    1804                 :       33340 :                         EEO_NEXT();
    1805                 :           0 :                 }
    1806                 :             : 
    1807                 :             :                 EEO_CASE(EEOP_HASHDATUM_FIRST)
    1808                 :             :                 {
    1809                 :      509300 :                         FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data;
    1810                 :             : 
    1811                 :             :                         /*
    1812                 :             :                          * Save the Datum on non-null inputs, otherwise store 0 so that
    1813                 :             :                          * subsequent NEXT32 operations combine with an initialized value.
    1814                 :             :                          */
    1815         [ +  + ]:      509300 :                         if (!fcinfo->args[0].isnull)
    1816                 :      501242 :                                 *op->resvalue = op->d.hashdatum.fn_addr(fcinfo);
    1817                 :             :                         else
    1818                 :        8058 :                                 *op->resvalue = (Datum) 0;
    1819                 :             : 
    1820                 :      509300 :                         *op->resnull = false;
    1821                 :             : 
    1822                 :      509300 :                         EEO_NEXT();
    1823                 :           0 :                 }
    1824                 :             : 
    1825                 :             :                 EEO_CASE(EEOP_HASHDATUM_FIRST_STRICT)
    1826                 :             :                 {
    1827                 :     1963966 :                         FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data;
    1828                 :             : 
    1829         [ +  + ]:     1963966 :                         if (fcinfo->args[0].isnull)
    1830                 :             :                         {
    1831                 :             :                                 /*
    1832                 :             :                                  * With strict we have the expression return NULL instead of
    1833                 :             :                                  * ignoring NULL input values.  We've nothing more to do after
    1834                 :             :                                  * finding a NULL.
    1835                 :             :                                  */
    1836                 :          87 :                                 *op->resnull = true;
    1837                 :          87 :                                 *op->resvalue = (Datum) 0;
    1838                 :          87 :                                 EEO_JUMP(op->d.hashdatum.jumpdone);
    1839                 :           0 :                         }
    1840                 :             : 
    1841                 :             :                         /* execute the hash function and save the resulting value */
    1842                 :     1963879 :                         *op->resvalue = op->d.hashdatum.fn_addr(fcinfo);
    1843                 :     1963879 :                         *op->resnull = false;
    1844                 :             : 
    1845                 :     1963879 :                         EEO_NEXT();
    1846                 :           0 :                 }
    1847                 :             : 
    1848                 :             :                 EEO_CASE(EEOP_HASHDATUM_NEXT32)
    1849                 :             :                 {
    1850                 :      866956 :                         FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data;
    1851                 :             :                         uint32          existinghash;
    1852                 :             : 
    1853                 :      866956 :                         existinghash = DatumGetUInt32(op->d.hashdatum.iresult->value);
    1854                 :             :                         /* combine successive hash values by rotating */
    1855                 :      866956 :                         existinghash = pg_rotate_left32(existinghash, 1);
    1856                 :             : 
    1857                 :             :                         /* leave the hash value alone on NULL inputs */
    1858         [ +  + ]:      866956 :                         if (!fcinfo->args[0].isnull)
    1859                 :             :                         {
    1860                 :             :                                 uint32          hashvalue;
    1861                 :             : 
    1862                 :             :                                 /* execute hash func and combine with previous hash value */
    1863                 :      841010 :                                 hashvalue = DatumGetUInt32(op->d.hashdatum.fn_addr(fcinfo));
    1864                 :      841010 :                                 existinghash = existinghash ^ hashvalue;
    1865                 :      841010 :                         }
    1866                 :             : 
    1867                 :      866956 :                         *op->resvalue = UInt32GetDatum(existinghash);
    1868                 :      866956 :                         *op->resnull = false;
    1869                 :             : 
    1870                 :      866956 :                         EEO_NEXT();
    1871                 :           0 :                 }
    1872                 :             : 
    1873                 :             :                 EEO_CASE(EEOP_HASHDATUM_NEXT32_STRICT)
    1874                 :             :                 {
    1875                 :      265757 :                         FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data;
    1876                 :             : 
    1877         [ +  + ]:      265757 :                         if (fcinfo->args[0].isnull)
    1878                 :             :                         {
    1879                 :             :                                 /*
    1880                 :             :                                  * With strict we have the expression return NULL instead of
    1881                 :             :                                  * ignoring NULL input values.  We've nothing more to do after
    1882                 :             :                                  * finding a NULL.
    1883                 :             :                                  */
    1884                 :           7 :                                 *op->resnull = true;
    1885                 :           7 :                                 *op->resvalue = (Datum) 0;
    1886                 :           7 :                                 EEO_JUMP(op->d.hashdatum.jumpdone);
    1887                 :           0 :                         }
    1888                 :             :                         else
    1889                 :             :                         {
    1890                 :             :                                 uint32          existinghash;
    1891                 :             :                                 uint32          hashvalue;
    1892                 :             : 
    1893                 :      265750 :                                 existinghash = DatumGetUInt32(op->d.hashdatum.iresult->value);
    1894                 :             :                                 /* combine successive hash values by rotating */
    1895                 :      265750 :                                 existinghash = pg_rotate_left32(existinghash, 1);
    1896                 :             : 
    1897                 :             :                                 /* execute hash func and combine with previous hash value */
    1898                 :      265750 :                                 hashvalue = DatumGetUInt32(op->d.hashdatum.fn_addr(fcinfo));
    1899                 :      265750 :                                 *op->resvalue = UInt32GetDatum(existinghash ^ hashvalue);
    1900                 :      265750 :                                 *op->resnull = false;
    1901                 :             :                         }
    1902                 :             : 
    1903                 :      265750 :                         EEO_NEXT();
    1904                 :           0 :                 }
    1905                 :             : 
    1906                 :             :                 EEO_CASE(EEOP_XMLEXPR)
    1907                 :             :                 {
    1908                 :             :                         /* too complex for an inline implementation */
    1909                 :        7610 :                         ExecEvalXmlExpr(state, op);
    1910                 :             : 
    1911                 :        7610 :                         EEO_NEXT();
    1912                 :           0 :                 }
    1913                 :             : 
    1914                 :             :                 EEO_CASE(EEOP_JSON_CONSTRUCTOR)
    1915                 :             :                 {
    1916                 :             :                         /* too complex for an inline implementation */
    1917                 :          94 :                         ExecEvalJsonConstructor(state, op, econtext);
    1918                 :          94 :                         EEO_NEXT();
    1919                 :           0 :                 }
    1920                 :             : 
    1921                 :             :                 EEO_CASE(EEOP_IS_JSON)
    1922                 :             :                 {
    1923                 :             :                         /* too complex for an inline implementation */
    1924                 :         453 :                         ExecEvalJsonIsPredicate(state, op);
    1925                 :             : 
    1926                 :         453 :                         EEO_NEXT();
    1927                 :           0 :                 }
    1928                 :             : 
    1929                 :             :                 EEO_CASE(EEOP_JSONEXPR_PATH)
    1930                 :             :                 {
    1931                 :             :                         /* too complex for an inline implementation */
    1932                 :         883 :                         EEO_JUMP(ExecEvalJsonExprPath(state, op, econtext));
    1933                 :           0 :                 }
    1934                 :             : 
    1935                 :             :                 EEO_CASE(EEOP_JSONEXPR_COERCION)
    1936                 :             :                 {
    1937                 :             :                         /* too complex for an inline implementation */
    1938                 :         275 :                         ExecEvalJsonCoercion(state, op, econtext);
    1939                 :             : 
    1940                 :         275 :                         EEO_NEXT();
    1941                 :           0 :                 }
    1942                 :             : 
    1943                 :             :                 EEO_CASE(EEOP_JSONEXPR_COERCION_FINISH)
    1944                 :             :                 {
    1945                 :             :                         /* too complex for an inline implementation */
    1946                 :         270 :                         ExecEvalJsonCoercionFinish(state, op);
    1947                 :             : 
    1948                 :         270 :                         EEO_NEXT();
    1949                 :           0 :                 }
    1950                 :             : 
    1951                 :             :                 EEO_CASE(EEOP_AGGREF)
    1952                 :             :                 {
    1953                 :             :                         /*
    1954                 :             :                          * Returns a Datum whose value is the precomputed aggregate value
    1955                 :             :                          * found in the given expression context.
    1956                 :             :                          */
    1957                 :      129219 :                         int                     aggno = op->d.aggref.aggno;
    1958                 :             : 
    1959         [ +  - ]:      129219 :                         Assert(econtext->ecxt_aggvalues != NULL);
    1960                 :             : 
    1961                 :      129219 :                         *op->resvalue = econtext->ecxt_aggvalues[aggno];
    1962                 :      129219 :                         *op->resnull = econtext->ecxt_aggnulls[aggno];
    1963                 :             : 
    1964                 :      129219 :                         EEO_NEXT();
    1965                 :           0 :                 }
    1966                 :             : 
    1967                 :             :                 EEO_CASE(EEOP_GROUPING_FUNC)
    1968                 :             :                 {
    1969                 :             :                         /* too complex/uncommon for an inline implementation */
    1970                 :         319 :                         ExecEvalGroupingFunc(state, op);
    1971                 :             : 
    1972                 :         319 :                         EEO_NEXT();
    1973                 :           0 :                 }
    1974                 :             : 
    1975                 :             :                 EEO_CASE(EEOP_WINDOW_FUNC)
    1976                 :             :                 {
    1977                 :             :                         /*
    1978                 :             :                          * Like Aggref, just return a precomputed value from the econtext.
    1979                 :             :                          */
    1980                 :      192152 :                         WindowFuncExprState *wfunc = op->d.window_func.wfstate;
    1981                 :             : 
    1982         [ -  + ]:      192152 :                         Assert(econtext->ecxt_aggvalues != NULL);
    1983                 :             : 
    1984                 :      192152 :                         *op->resvalue = econtext->ecxt_aggvalues[wfunc->wfuncno];
    1985                 :      192152 :                         *op->resnull = econtext->ecxt_aggnulls[wfunc->wfuncno];
    1986                 :             : 
    1987                 :      192152 :                         EEO_NEXT();
    1988                 :           0 :                 }
    1989                 :             : 
    1990                 :             :                 EEO_CASE(EEOP_MERGE_SUPPORT_FUNC)
    1991                 :             :                 {
    1992                 :             :                         /* too complex/uncommon for an inline implementation */
    1993                 :          74 :                         ExecEvalMergeSupportFunc(state, op, econtext);
    1994                 :             : 
    1995                 :          74 :                         EEO_NEXT();
    1996                 :           0 :                 }
    1997                 :             : 
    1998                 :             :                 EEO_CASE(EEOP_SUBPLAN)
    1999                 :             :                 {
    2000                 :             :                         /* too complex for an inline implementation */
    2001                 :       97550 :                         ExecEvalSubPlan(state, op, econtext);
    2002                 :             : 
    2003                 :       97550 :                         EEO_NEXT();
    2004                 :           0 :                 }
    2005                 :             : 
    2006                 :             :                 /* evaluate a strict aggregate deserialization function */
    2007                 :             :                 EEO_CASE(EEOP_AGG_STRICT_DESERIALIZE)
    2008                 :             :                 {
    2009                 :             :                         /* Don't call a strict deserialization function with NULL input */
    2010         [ +  + ]:         111 :                         if (op->d.agg_deserialize.fcinfo_data->args[0].isnull)
    2011                 :          12 :                                 EEO_JUMP(op->d.agg_deserialize.jumpnull);
    2012                 :             : 
    2013                 :             :                         /* fallthrough */
    2014                 :          99 :                 }
    2015                 :             : 
    2016                 :             :                 /* evaluate aggregate deserialization function (non-strict portion) */
    2017                 :             :                 EEO_CASE(EEOP_AGG_DESERIALIZE)
    2018                 :             :                 {
    2019                 :          99 :                         FunctionCallInfo fcinfo = op->d.agg_deserialize.fcinfo_data;
    2020                 :          99 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2021                 :             :                         MemoryContext oldContext;
    2022                 :             : 
    2023                 :             :                         /*
    2024                 :             :                          * We run the deserialization functions in per-input-tuple memory
    2025                 :             :                          * context.
    2026                 :             :                          */
    2027                 :          99 :                         oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
    2028                 :          99 :                         fcinfo->isnull = false;
    2029                 :          99 :                         *op->resvalue = FunctionCallInvoke(fcinfo);
    2030                 :          99 :                         *op->resnull = fcinfo->isnull;
    2031                 :          99 :                         MemoryContextSwitchTo(oldContext);
    2032                 :             : 
    2033                 :          99 :                         EEO_NEXT();
    2034                 :           0 :                 }
    2035                 :             : 
    2036                 :             :                 /*
    2037                 :             :                  * Check that a strict aggregate transition / combination function's
    2038                 :             :                  * input is not NULL.
    2039                 :             :                  */
    2040                 :             : 
    2041                 :             :                 /* when checking more than one argument */
    2042                 :             :                 EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS)
    2043                 :             :                 {
    2044                 :       40283 :                         NullableDatum *args = op->d.agg_strict_input_check.args;
    2045                 :       40283 :                         int                     nargs = op->d.agg_strict_input_check.nargs;
    2046                 :             : 
    2047         [ -  + ]:       40283 :                         Assert(nargs > 1);
    2048                 :             : 
    2049         [ +  + ]:      120863 :                         for (int argno = 0; argno < nargs; argno++)
    2050                 :             :                         {
    2051         [ +  + ]:       80587 :                                 if (args[argno].isnull)
    2052                 :           7 :                                         EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
    2053                 :       80580 :                         }
    2054                 :       40276 :                         EEO_NEXT();
    2055                 :           0 :                 }
    2056                 :             : 
    2057                 :             :                 /* special case for just one argument */
    2058                 :             :                 EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1)
    2059                 :             :                 {
    2060                 :      913039 :                         NullableDatum *args = op->d.agg_strict_input_check.args;
    2061                 :      913039 :                         PG_USED_FOR_ASSERTS_ONLY int nargs = op->d.agg_strict_input_check.nargs;
    2062                 :             : 
    2063         [ -  + ]:      913039 :                         Assert(nargs == 1);
    2064                 :             : 
    2065         [ +  + ]:      913039 :                         if (args[0].isnull)
    2066                 :       23709 :                                 EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
    2067                 :      889330 :                         EEO_NEXT();
    2068                 :           0 :                 }
    2069                 :             : 
    2070                 :             :                 EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
    2071                 :             :                 {
    2072                 :       62784 :                         bool       *nulls = op->d.agg_strict_input_check.nulls;
    2073                 :       62784 :                         int                     nargs = op->d.agg_strict_input_check.nargs;
    2074                 :             : 
    2075         [ +  + ]:      118068 :                         for (int argno = 0; argno < nargs; argno++)
    2076                 :             :                         {
    2077         [ +  + ]:       62784 :                                 if (nulls[argno])
    2078                 :        7500 :                                         EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
    2079                 :       55284 :                         }
    2080                 :       55284 :                         EEO_NEXT();
    2081                 :           0 :                 }
    2082                 :             : 
    2083                 :             :                 /*
    2084                 :             :                  * Check for a NULL pointer to the per-group states.
    2085                 :             :                  */
    2086                 :             : 
    2087                 :             :                 EEO_CASE(EEOP_AGG_PLAIN_PERGROUP_NULLCHECK)
    2088                 :             :                 {
    2089                 :      434402 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2090                 :      434402 :                         AggStatePerGroup pergroup_allaggs =
    2091                 :      434402 :                                 aggstate->all_pergroups[op->d.agg_plain_pergroup_nullcheck.setoff];
    2092                 :             : 
    2093         [ +  + ]:      434402 :                         if (pergroup_allaggs == NULL)
    2094                 :      340164 :                                 EEO_JUMP(op->d.agg_plain_pergroup_nullcheck.jumpnull);
    2095                 :             : 
    2096                 :       94238 :                         EEO_NEXT();
    2097                 :           0 :                 }
    2098                 :             : 
    2099                 :             :                 /*
    2100                 :             :                  * Different types of aggregate transition functions are implemented
    2101                 :             :                  * as different types of steps, to avoid incurring unnecessary
    2102                 :             :                  * overhead.  There's a step type for each valid combination of having
    2103                 :             :                  * a by value / by reference transition type, [not] needing to the
    2104                 :             :                  * initialize the transition value for the first row in a group from
    2105                 :             :                  * input, and [not] strict transition function.
    2106                 :             :                  *
    2107                 :             :                  * Could optimize further by splitting off by-reference for
    2108                 :             :                  * fixed-length types, but currently that doesn't seem worth it.
    2109                 :             :                  */
    2110                 :             : 
    2111                 :             :                 EEO_CASE(EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL)
    2112                 :             :                 {
    2113                 :      259892 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2114                 :      259892 :                         AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
    2115                 :      259892 :                         AggStatePerGroup pergroup =
    2116                 :      259892 :                                 &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
    2117                 :             : 
    2118         [ -  + ]:      259892 :                         Assert(pertrans->transtypeByVal);
    2119                 :             : 
    2120         [ +  + ]:      259892 :                         if (pergroup->noTransValue)
    2121                 :             :                         {
    2122                 :             :                                 /* If transValue has not yet been initialized, do so now. */
    2123                 :        2566 :                                 ExecAggInitGroup(aggstate, pertrans, pergroup,
    2124                 :        1283 :                                                                  op->d.agg_trans.aggcontext);
    2125                 :             :                                 /* copied trans value from input, done this round */
    2126                 :        1283 :                         }
    2127         [ -  + ]:      258609 :                         else if (likely(!pergroup->transValueIsNull))
    2128                 :             :                         {
    2129                 :             :                                 /* invoke transition function, unless prevented by strictness */
    2130                 :      517218 :                                 ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
    2131                 :      258609 :                                                                            op->d.agg_trans.aggcontext,
    2132                 :      258609 :                                                                            op->d.agg_trans.setno);
    2133                 :      258609 :                         }
    2134                 :             : 
    2135                 :      259892 :                         EEO_NEXT();
    2136                 :           0 :                 }
    2137                 :             : 
    2138                 :             :                 /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
    2139                 :             :                 EEO_CASE(EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL)
    2140                 :             :                 {
    2141                 :     3170065 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2142                 :     3170065 :                         AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
    2143                 :     3170065 :                         AggStatePerGroup pergroup =
    2144                 :     3170065 :                                 &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
    2145                 :             : 
    2146         [ -  + ]:     3170065 :                         Assert(pertrans->transtypeByVal);
    2147                 :             : 
    2148         [ +  + ]:     3170065 :                         if (likely(!pergroup->transValueIsNull))
    2149                 :     6320124 :                                 ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
    2150                 :     3160062 :                                                                            op->d.agg_trans.aggcontext,
    2151                 :     3160062 :                                                                            op->d.agg_trans.setno);
    2152                 :             : 
    2153                 :     3170065 :                         EEO_NEXT();
    2154                 :           0 :                 }
    2155                 :             : 
    2156                 :             :                 /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
    2157                 :             :                 EEO_CASE(EEOP_AGG_PLAIN_TRANS_BYVAL)
    2158                 :             :                 {
    2159                 :     1654417 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2160                 :     1654417 :                         AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
    2161                 :     1654417 :                         AggStatePerGroup pergroup =
    2162                 :     1654417 :                                 &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
    2163                 :             : 
    2164         [ -  + ]:     1654417 :                         Assert(pertrans->transtypeByVal);
    2165                 :             : 
    2166                 :     3308834 :                         ExecAggPlainTransByVal(aggstate, pertrans, pergroup,
    2167                 :     1654417 :                                                                    op->d.agg_trans.aggcontext,
    2168                 :     1654417 :                                                                    op->d.agg_trans.setno);
    2169                 :             : 
    2170                 :     1654417 :                         EEO_NEXT();
    2171                 :           0 :                 }
    2172                 :             : 
    2173                 :             :                 /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
    2174                 :             :                 EEO_CASE(EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF)
    2175                 :             :                 {
    2176                 :       51033 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2177                 :       51033 :                         AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
    2178                 :       51033 :                         AggStatePerGroup pergroup =
    2179                 :       51033 :                                 &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
    2180                 :             : 
    2181         [ -  + ]:       51033 :                         Assert(!pertrans->transtypeByVal);
    2182                 :             : 
    2183         [ +  + ]:       51033 :                         if (pergroup->noTransValue)
    2184                 :       17140 :                                 ExecAggInitGroup(aggstate, pertrans, pergroup,
    2185                 :        8570 :                                                                  op->d.agg_trans.aggcontext);
    2186         [ +  + ]:       42463 :                         else if (likely(!pergroup->transValueIsNull))
    2187                 :       84924 :                                 ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
    2188                 :       42462 :                                                                            op->d.agg_trans.aggcontext,
    2189                 :       42462 :                                                                            op->d.agg_trans.setno);
    2190                 :             : 
    2191                 :       51031 :                         EEO_NEXT();
    2192                 :           0 :                 }
    2193                 :             : 
    2194                 :             :                 /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
    2195                 :             :                 EEO_CASE(EEOP_AGG_PLAIN_TRANS_STRICT_BYREF)
    2196                 :             :                 {
    2197                 :      439168 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2198                 :      439168 :                         AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
    2199                 :      439168 :                         AggStatePerGroup pergroup =
    2200                 :      439168 :                                 &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
    2201                 :             : 
    2202         [ -  + ]:      439168 :                         Assert(!pertrans->transtypeByVal);
    2203                 :             : 
    2204         [ +  - ]:      439168 :                         if (likely(!pergroup->transValueIsNull))
    2205                 :      878336 :                                 ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
    2206                 :      439168 :                                                                            op->d.agg_trans.aggcontext,
    2207                 :      439168 :                                                                            op->d.agg_trans.setno);
    2208                 :      439168 :                         EEO_NEXT();
    2209                 :           0 :                 }
    2210                 :             : 
    2211                 :             :                 /* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */
    2212                 :             :                 EEO_CASE(EEOP_AGG_PLAIN_TRANS_BYREF)
    2213                 :             :                 {
    2214                 :        4062 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2215                 :        4062 :                         AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
    2216                 :        4062 :                         AggStatePerGroup pergroup =
    2217                 :        4062 :                                 &aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno];
    2218                 :             : 
    2219         [ -  + ]:        4062 :                         Assert(!pertrans->transtypeByVal);
    2220                 :             : 
    2221                 :        8124 :                         ExecAggPlainTransByRef(aggstate, pertrans, pergroup,
    2222                 :        4062 :                                                                    op->d.agg_trans.aggcontext,
    2223                 :        4062 :                                                                    op->d.agg_trans.setno);
    2224                 :             : 
    2225                 :        4062 :                         EEO_NEXT();
    2226                 :           0 :                 }
    2227                 :             : 
    2228                 :             :                 EEO_CASE(EEOP_AGG_PRESORTED_DISTINCT_SINGLE)
    2229                 :             :                 {
    2230                 :       60577 :                         AggStatePerTrans pertrans = op->d.agg_presorted_distinctcheck.pertrans;
    2231                 :       60577 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2232                 :             : 
    2233         [ +  + ]:       60577 :                         if (ExecEvalPreOrderedDistinctSingle(aggstate, pertrans))
    2234                 :       16655 :                                 EEO_NEXT();
    2235                 :             :                         else
    2236                 :       43922 :                                 EEO_JUMP(op->d.agg_presorted_distinctcheck.jumpdistinct);
    2237                 :           0 :                 }
    2238                 :             : 
    2239                 :             :                 EEO_CASE(EEOP_AGG_PRESORTED_DISTINCT_MULTI)
    2240                 :             :                 {
    2241                 :         120 :                         AggState   *aggstate = castNode(AggState, state->parent);
    2242                 :         120 :                         AggStatePerTrans pertrans = op->d.agg_presorted_distinctcheck.pertrans;
    2243                 :             : 
    2244         [ +  + ]:         120 :                         if (ExecEvalPreOrderedDistinctMulti(aggstate, pertrans))
    2245                 :          52 :                                 EEO_NEXT();
    2246                 :             :                         else
    2247                 :          68 :                                 EEO_JUMP(op->d.agg_presorted_distinctcheck.jumpdistinct);
    2248                 :           0 :                 }
    2249                 :             : 
    2250                 :             :                 /* process single-column ordered aggregate datum */
    2251                 :             :                 EEO_CASE(EEOP_AGG_ORDERED_TRANS_DATUM)
    2252                 :             :                 {
    2253                 :             :                         /* too complex for an inline implementation */
    2254                 :      137362 :                         ExecEvalAggOrderedTransDatum(state, op, econtext);
    2255                 :             : 
    2256                 :      137362 :                         EEO_NEXT();
    2257                 :           0 :                 }
    2258                 :             : 
    2259                 :             :                 /* process multi-column ordered aggregate tuple */
    2260                 :             :                 EEO_CASE(EEOP_AGG_ORDERED_TRANS_TUPLE)
    2261                 :             :                 {
    2262                 :             :                         /* too complex for an inline implementation */
    2263                 :          36 :                         ExecEvalAggOrderedTransTuple(state, op, econtext);
    2264                 :             : 
    2265                 :          36 :                         EEO_NEXT();
    2266                 :           0 :                 }
    2267                 :             : 
    2268                 :             :                 EEO_CASE(EEOP_LAST)
    2269                 :             :                 {
    2270                 :             :                         /* unreachable */
    2271                 :           0 :                         Assert(false);
    2272                 :           0 :                         goto out_error;
    2273                 :             :                 }
    2274                 :             :         }
    2275                 :             : 
    2276                 :             : out_error:
    2277                 :           0 :         pg_unreachable();
    2278                 :             :         return (Datum) 0;
    2279                 :    29092008 : }
    2280                 :             : 
    2281                 :             : /*
    2282                 :             :  * Expression evaluation callback that performs extra checks before executing
    2283                 :             :  * the expression. Declared extern so other methods of execution can use it
    2284                 :             :  * too.
    2285                 :             :  */
    2286                 :             : Datum
    2287                 :      963463 : ExecInterpExprStillValid(ExprState *state, ExprContext *econtext, bool *isNull)
    2288                 :             : {
    2289                 :             :         /*
    2290                 :             :          * First time through, check whether attribute matches Var.  Might not be
    2291                 :             :          * ok anymore, due to schema changes.
    2292                 :             :          */
    2293                 :      963463 :         CheckExprStillValid(state, econtext);
    2294                 :             : 
    2295                 :             :         /* skip the check during further executions */
    2296                 :      963463 :         state->evalfunc = (ExprStateEvalFunc) state->evalfunc_private;
    2297                 :             : 
    2298                 :             :         /* and actually execute */
    2299                 :      963463 :         return state->evalfunc(state, econtext, isNull);
    2300                 :             : }
    2301                 :             : 
    2302                 :             : /*
    2303                 :             :  * Check that an expression is still valid in the face of potential schema
    2304                 :             :  * changes since the plan has been created.
    2305                 :             :  */
    2306                 :             : void
    2307                 :      963459 : CheckExprStillValid(ExprState *state, ExprContext *econtext)
    2308                 :             : {
    2309                 :      963459 :         TupleTableSlot *innerslot;
    2310                 :      963459 :         TupleTableSlot *outerslot;
    2311                 :      963459 :         TupleTableSlot *scanslot;
    2312                 :      963459 :         TupleTableSlot *oldslot;
    2313                 :      963459 :         TupleTableSlot *newslot;
    2314                 :             : 
    2315                 :      963459 :         innerslot = econtext->ecxt_innertuple;
    2316                 :      963459 :         outerslot = econtext->ecxt_outertuple;
    2317                 :      963459 :         scanslot = econtext->ecxt_scantuple;
    2318                 :      963459 :         oldslot = econtext->ecxt_oldtuple;
    2319                 :      963459 :         newslot = econtext->ecxt_newtuple;
    2320                 :             : 
    2321         [ +  + ]:     5639016 :         for (int i = 0; i < state->steps_len; i++)
    2322                 :             :         {
    2323                 :     4675557 :                 ExprEvalStep *op = &state->steps[i];
    2324                 :             : 
    2325   [ +  +  +  +  :     4675557 :                 switch (ExecEvalStepOp(state, op))
                   +  + ]
    2326                 :             :                 {
    2327                 :             :                         case EEOP_INNER_VAR:
    2328                 :             :                                 {
    2329                 :       10352 :                                         int                     attnum = op->d.var.attnum;
    2330                 :             : 
    2331                 :       10352 :                                         CheckVarSlotCompatibility(innerslot, attnum + 1, op->d.var.vartype);
    2332                 :             :                                         break;
    2333                 :       10352 :                                 }
    2334                 :             : 
    2335                 :             :                         case EEOP_OUTER_VAR:
    2336                 :             :                                 {
    2337                 :       21790 :                                         int                     attnum = op->d.var.attnum;
    2338                 :             : 
    2339                 :       21790 :                                         CheckVarSlotCompatibility(outerslot, attnum + 1, op->d.var.vartype);
    2340                 :             :                                         break;
    2341                 :       21790 :                                 }
    2342                 :             : 
    2343                 :             :                         case EEOP_SCAN_VAR:
    2344                 :             :                                 {
    2345                 :      330703 :                                         int                     attnum = op->d.var.attnum;
    2346                 :             : 
    2347                 :      330703 :                                         CheckVarSlotCompatibility(scanslot, attnum + 1, op->d.var.vartype);
    2348                 :             :                                         break;
    2349                 :      330703 :                                 }
    2350                 :             : 
    2351                 :             :                         case EEOP_OLD_VAR:
    2352                 :             :                                 {
    2353                 :          29 :                                         int                     attnum = op->d.var.attnum;
    2354                 :             : 
    2355                 :          29 :                                         CheckVarSlotCompatibility(oldslot, attnum + 1, op->d.var.vartype);
    2356                 :             :                                         break;
    2357                 :          29 :                                 }
    2358                 :             : 
    2359                 :             :                         case EEOP_NEW_VAR:
    2360                 :             :                                 {
    2361                 :          29 :                                         int                     attnum = op->d.var.attnum;
    2362                 :             : 
    2363                 :          29 :                                         CheckVarSlotCompatibility(newslot, attnum + 1, op->d.var.vartype);
    2364                 :             :                                         break;
    2365                 :          29 :                                 }
    2366                 :             :                         default:
    2367                 :     4312654 :                                 break;
    2368                 :             :                 }
    2369                 :     4675557 :         }
    2370                 :      963459 : }
    2371                 :             : 
    2372                 :             : /*
    2373                 :             :  * Check whether a user attribute in a slot can be referenced by a Var
    2374                 :             :  * expression.  This should succeed unless there have been schema changes
    2375                 :             :  * since the expression tree has been created.
    2376                 :             :  */
    2377                 :             : static void
    2378                 :      362903 : CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype)
    2379                 :             : {
    2380                 :             :         /*
    2381                 :             :          * What we have to check for here is the possibility of an attribute
    2382                 :             :          * having been dropped or changed in type since the plan tree was created.
    2383                 :             :          * Ideally the plan will get invalidated and not re-used, but just in
    2384                 :             :          * case, we keep these defenses.  Fortunately it's sufficient to check
    2385                 :             :          * once on the first time through.
    2386                 :             :          *
    2387                 :             :          * Note: ideally we'd check typmod as well as typid, but that seems
    2388                 :             :          * impractical at the moment: in many cases the tupdesc will have been
    2389                 :             :          * generated by ExecTypeFromTL(), and that can't guarantee to generate an
    2390                 :             :          * accurate typmod in all cases, because some expression node types don't
    2391                 :             :          * carry typmod.  Fortunately, for precisely that reason, there should be
    2392                 :             :          * no places with a critical dependency on the typmod of a value.
    2393                 :             :          *
    2394                 :             :          * System attributes don't require checking since their types never
    2395                 :             :          * change.
    2396                 :             :          */
    2397         [ -  + ]:      362903 :         if (attnum > 0)
    2398                 :             :         {
    2399                 :      362903 :                 TupleDesc       slot_tupdesc = slot->tts_tupleDescriptor;
    2400                 :      362903 :                 Form_pg_attribute attr;
    2401                 :             : 
    2402         [ +  - ]:      362903 :                 if (attnum > slot_tupdesc->natts) /* should never happen */
    2403   [ #  #  #  # ]:           0 :                         elog(ERROR, "attribute number %d exceeds number of columns %d",
    2404                 :             :                                  attnum, slot_tupdesc->natts);
    2405                 :             : 
    2406                 :      362903 :                 attr = TupleDescAttr(slot_tupdesc, attnum - 1);
    2407                 :             : 
    2408                 :             :                 /* Internal error: somebody forgot to expand it. */
    2409         [ +  - ]:      362903 :                 if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
    2410   [ #  #  #  # ]:           0 :                         elog(ERROR, "unexpected virtual generated column reference");
    2411                 :             : 
    2412         [ +  + ]:      362903 :                 if (attr->attisdropped)
    2413   [ +  -  +  - ]:           2 :                         ereport(ERROR,
    2414                 :             :                                         (errcode(ERRCODE_UNDEFINED_COLUMN),
    2415                 :             :                                          errmsg("attribute %d of type %s has been dropped",
    2416                 :             :                                                         attnum, format_type_be(slot_tupdesc->tdtypeid))));
    2417                 :             : 
    2418         [ +  + ]:      362901 :                 if (vartype != attr->atttypid)
    2419   [ +  -  +  - ]:           2 :                         ereport(ERROR,
    2420                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
    2421                 :             :                                          errmsg("attribute %d of type %s has wrong type",
    2422                 :             :                                                         attnum, format_type_be(slot_tupdesc->tdtypeid)),
    2423                 :             :                                          errdetail("Table has type %s, but query expects %s.",
    2424                 :             :                                                            format_type_be(attr->atttypid),
    2425                 :             :                                                            format_type_be(vartype))));
    2426                 :      362899 :         }
    2427                 :      362899 : }
    2428                 :             : 
    2429                 :             : /*
    2430                 :             :  * Verify that the slot is compatible with a EEOP_*_FETCHSOME operation.
    2431                 :             :  */
    2432                 :             : static void
    2433                 :    28141412 : CheckOpSlotCompatibility(ExprEvalStep *op, TupleTableSlot *slot)
    2434                 :             : {
    2435                 :             : #ifdef USE_ASSERT_CHECKING
    2436                 :             :         /* there's nothing to check */
    2437         [ +  + ]:    28141412 :         if (!op->d.fetch.fixed)
    2438                 :     1810757 :                 return;
    2439                 :             : 
    2440                 :             :         /*
    2441                 :             :          * Should probably fixed at some point, but for now it's easier to allow
    2442                 :             :          * buffer and heap tuples to be used interchangeably.
    2443                 :             :          */
    2444   [ +  +  +  - ]:    26330655 :         if (slot->tts_ops == &TTSOpsBufferHeapTuple &&
    2445                 :    17832384 :                 op->d.fetch.kind == &TTSOpsHeapTuple)
    2446                 :           0 :                 return;
    2447   [ -  +  #  # ]:    26330655 :         if (slot->tts_ops == &TTSOpsHeapTuple &&
    2448                 :           0 :                 op->d.fetch.kind == &TTSOpsBufferHeapTuple)
    2449                 :           0 :                 return;
    2450                 :             : 
    2451                 :             :         /*
    2452                 :             :          * At the moment we consider it OK if a virtual slot is used instead of a
    2453                 :             :          * specific type of slot, as a virtual slot never needs to be deformed.
    2454                 :             :          */
    2455         [ +  + ]:    26330655 :         if (slot->tts_ops == &TTSOpsVirtual)
    2456                 :       21848 :                 return;
    2457                 :             : 
    2458         [ +  - ]:    26308807 :         Assert(op->d.fetch.kind == slot->tts_ops);
    2459                 :             : #endif
    2460                 :    28141412 : }
    2461                 :             : 
    2462                 :             : /*
    2463                 :             :  * get_cached_rowtype: utility function to lookup a rowtype tupdesc
    2464                 :             :  *
    2465                 :             :  * type_id, typmod: identity of the rowtype
    2466                 :             :  * rowcache: space for caching identity info
    2467                 :             :  *              (rowcache->cacheptr must be initialized to NULL)
    2468                 :             :  * changed: if not NULL, *changed is set to true on any update
    2469                 :             :  *
    2470                 :             :  * The returned TupleDesc is not guaranteed pinned; caller must pin it
    2471                 :             :  * to use it across any operation that might incur cache invalidation,
    2472                 :             :  * including for example detoasting of input tuples.
    2473                 :             :  * (The TupleDesc is always refcounted, so just use IncrTupleDescRefCount.)
    2474                 :             :  *
    2475                 :             :  * NOTE: because composite types can change contents, we must be prepared
    2476                 :             :  * to re-do this during any node execution; cannot call just once during
    2477                 :             :  * expression initialization.
    2478                 :             :  */
    2479                 :             : static TupleDesc
    2480                 :       68005 : get_cached_rowtype(Oid type_id, int32 typmod,
    2481                 :             :                                    ExprEvalRowtypeCache *rowcache,
    2482                 :             :                                    bool *changed)
    2483                 :             : {
    2484         [ +  + ]:       68005 :         if (type_id != RECORDOID)
    2485                 :             :         {
    2486                 :             :                 /*
    2487                 :             :                  * It's a named composite type, so use the regular typcache.  Do a
    2488                 :             :                  * lookup first time through, or if the composite type changed.  Note:
    2489                 :             :                  * "tupdesc_id == 0" may look redundant, but it protects against the
    2490                 :             :                  * admittedly-theoretical possibility that type_id was RECORDOID the
    2491                 :             :                  * last time through, so that the cacheptr isn't TypeCacheEntry *.
    2492                 :             :                  */
    2493                 :        5108 :                 TypeCacheEntry *typentry = (TypeCacheEntry *) rowcache->cacheptr;
    2494                 :             : 
    2495   [ +  +  -  +  :        5108 :                 if (unlikely(typentry == NULL ||
                   +  + ]
    2496                 :             :                                          rowcache->tupdesc_id == 0 ||
    2497                 :             :                                          typentry->tupDesc_identifier != rowcache->tupdesc_id))
    2498                 :             :                 {
    2499                 :        1055 :                         typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC);
    2500         [ +  - ]:        1055 :                         if (typentry->tupDesc == NULL)
    2501   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
    2502                 :             :                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2503                 :             :                                                  errmsg("type %s is not composite",
    2504                 :             :                                                                 format_type_be(type_id))));
    2505                 :        1055 :                         rowcache->cacheptr = typentry;
    2506                 :        1055 :                         rowcache->tupdesc_id = typentry->tupDesc_identifier;
    2507         [ +  + ]:        1055 :                         if (changed)
    2508                 :         142 :                                 *changed = true;
    2509                 :        1055 :                 }
    2510                 :        5108 :                 return typentry->tupDesc;
    2511                 :        5108 :         }
    2512                 :             :         else
    2513                 :             :         {
    2514                 :             :                 /*
    2515                 :             :                  * A RECORD type, once registered, doesn't change for the life of the
    2516                 :             :                  * backend.  So we don't need a typcache entry as such, which is good
    2517                 :             :                  * because there isn't one.  It's possible that the caller is asking
    2518                 :             :                  * about a different type than before, though.
    2519                 :             :                  */
    2520                 :       62897 :                 TupleDesc       tupDesc = (TupleDesc) rowcache->cacheptr;
    2521                 :             : 
    2522   [ +  +  +  -  :       62897 :                 if (unlikely(tupDesc == NULL ||
             -  +  +  + ]
    2523                 :             :                                          rowcache->tupdesc_id != 0 ||
    2524                 :             :                                          type_id != tupDesc->tdtypeid ||
    2525                 :             :                                          typmod != tupDesc->tdtypmod))
    2526                 :             :                 {
    2527                 :         178 :                         tupDesc = lookup_rowtype_tupdesc(type_id, typmod);
    2528                 :             :                         /* Drop pin acquired by lookup_rowtype_tupdesc */
    2529         [ +  + ]:         178 :                         ReleaseTupleDesc(tupDesc);
    2530                 :         178 :                         rowcache->cacheptr = tupDesc;
    2531                 :         178 :                         rowcache->tupdesc_id = 0;    /* not a valid value for non-RECORD */
    2532         [ +  - ]:         178 :                         if (changed)
    2533                 :           0 :                                 *changed = true;
    2534                 :         178 :                 }
    2535                 :       62897 :                 return tupDesc;
    2536                 :       62897 :         }
    2537                 :       68005 : }
    2538                 :             : 
    2539                 :             : 
    2540                 :             : /*
    2541                 :             :  * Fast-path functions, for very simple expressions
    2542                 :             :  */
    2543                 :             : 
    2544                 :             : /* implementation of ExecJust(Inner|Outer|Scan)Var */
    2545                 :             : static pg_attribute_always_inline Datum
    2546                 :           0 : ExecJustVarImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
    2547                 :             : {
    2548                 :           0 :         ExprEvalStep *op = &state->steps[1];
    2549                 :           0 :         int                     attnum = op->d.var.attnum + 1;
    2550                 :             : 
    2551                 :           0 :         CheckOpSlotCompatibility(&state->steps[0], slot);
    2552                 :             : 
    2553                 :             :         /*
    2554                 :             :          * Since we use slot_getattr(), we don't need to implement the FETCHSOME
    2555                 :             :          * step explicitly, and we also needn't Assert that the attnum is in range
    2556                 :             :          * --- slot_getattr() will take care of any problems.
    2557                 :             :          */
    2558                 :           0 :         return slot_getattr(slot, attnum, isnull);
    2559                 :           0 : }
    2560                 :             : 
    2561                 :             : /* Simple reference to inner Var */
    2562                 :             : static Datum
    2563                 :      460514 : ExecJustInnerVar(ExprState *state, ExprContext *econtext, bool *isnull)
    2564                 :             : {
    2565                 :      460514 :         return ExecJustVarImpl(state, econtext->ecxt_innertuple, isnull);
    2566                 :             : }
    2567                 :             : 
    2568                 :             : /* Simple reference to outer Var */
    2569                 :             : static Datum
    2570                 :      245460 : ExecJustOuterVar(ExprState *state, ExprContext *econtext, bool *isnull)
    2571                 :             : {
    2572                 :      245460 :         return ExecJustVarImpl(state, econtext->ecxt_outertuple, isnull);
    2573                 :             : }
    2574                 :             : 
    2575                 :             : /* Simple reference to scan Var */
    2576                 :             : static Datum
    2577                 :           2 : ExecJustScanVar(ExprState *state, ExprContext *econtext, bool *isnull)
    2578                 :             : {
    2579                 :           2 :         return ExecJustVarImpl(state, econtext->ecxt_scantuple, isnull);
    2580                 :             : }
    2581                 :             : 
    2582                 :             : /* implementation of ExecJustAssign(Inner|Outer|Scan)Var */
    2583                 :             : static pg_attribute_always_inline Datum
    2584                 :           0 : ExecJustAssignVarImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull)
    2585                 :             : {
    2586                 :           0 :         ExprEvalStep *op = &state->steps[1];
    2587                 :           0 :         int                     attnum = op->d.assign_var.attnum + 1;
    2588                 :           0 :         int                     resultnum = op->d.assign_var.resultnum;
    2589                 :           0 :         TupleTableSlot *outslot = state->resultslot;
    2590                 :             : 
    2591                 :           0 :         CheckOpSlotCompatibility(&state->steps[0], inslot);
    2592                 :             : 
    2593                 :             :         /*
    2594                 :             :          * We do not need CheckVarSlotCompatibility here; that was taken care of
    2595                 :             :          * at compilation time.
    2596                 :             :          *
    2597                 :             :          * Since we use slot_getattr(), we don't need to implement the FETCHSOME
    2598                 :             :          * step explicitly, and we also needn't Assert that the attnum is in range
    2599                 :             :          * --- slot_getattr() will take care of any problems.  Nonetheless, check
    2600                 :             :          * that resultnum is in range.
    2601                 :             :          */
    2602         [ #  # ]:           0 :         Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts);
    2603                 :           0 :         outslot->tts_values[resultnum] =
    2604                 :           0 :                 slot_getattr(inslot, attnum, &outslot->tts_isnull[resultnum]);
    2605                 :           0 :         return 0;
    2606                 :           0 : }
    2607                 :             : 
    2608                 :             : /* Evaluate inner Var and assign to appropriate column of result tuple */
    2609                 :             : static Datum
    2610                 :        6711 : ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull)
    2611                 :             : {
    2612                 :        6711 :         return ExecJustAssignVarImpl(state, econtext->ecxt_innertuple, isnull);
    2613                 :             : }
    2614                 :             : 
    2615                 :             : /* Evaluate outer Var and assign to appropriate column of result tuple */
    2616                 :             : static Datum
    2617                 :      173555 : ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull)
    2618                 :             : {
    2619                 :      173555 :         return ExecJustAssignVarImpl(state, econtext->ecxt_outertuple, isnull);
    2620                 :             : }
    2621                 :             : 
    2622                 :             : /* Evaluate scan Var and assign to appropriate column of result tuple */
    2623                 :             : static Datum
    2624                 :     2020642 : ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull)
    2625                 :             : {
    2626                 :     2020642 :         return ExecJustAssignVarImpl(state, econtext->ecxt_scantuple, isnull);
    2627                 :             : }
    2628                 :             : 
    2629                 :             : /* Evaluate CASE_TESTVAL and apply a strict function to it */
    2630                 :             : static Datum
    2631                 :         110 : ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull)
    2632                 :             : {
    2633                 :         110 :         ExprEvalStep *op = &state->steps[0];
    2634                 :         110 :         FunctionCallInfo fcinfo;
    2635                 :         110 :         NullableDatum *args;
    2636                 :         110 :         int                     nargs;
    2637                 :         110 :         Datum           d;
    2638                 :             : 
    2639                 :             :         /*
    2640                 :             :          * XXX with some redesign of the CaseTestExpr mechanism, maybe we could
    2641                 :             :          * get rid of this data shuffling?
    2642                 :             :          */
    2643                 :         110 :         *op->resvalue = *op->d.casetest.value;
    2644                 :         110 :         *op->resnull = *op->d.casetest.isnull;
    2645                 :             : 
    2646                 :         110 :         op++;
    2647                 :             : 
    2648                 :         110 :         nargs = op->d.func.nargs;
    2649                 :         110 :         fcinfo = op->d.func.fcinfo_data;
    2650                 :         110 :         args = fcinfo->args;
    2651                 :             : 
    2652                 :             :         /* strict function, so check for NULL args */
    2653   [ +  +  +  + ]:         274 :         for (int argno = 0; argno < nargs; argno++)
    2654                 :             :         {
    2655         [ +  + ]:         164 :                 if (args[argno].isnull)
    2656                 :             :                 {
    2657                 :           2 :                         *isnull = true;
    2658                 :           2 :                         return (Datum) 0;
    2659                 :             :                 }
    2660                 :         162 :         }
    2661                 :         108 :         fcinfo->isnull = false;
    2662                 :         108 :         d = op->d.func.fn_addr(fcinfo);
    2663                 :         108 :         *isnull = fcinfo->isnull;
    2664                 :         108 :         return d;
    2665                 :         110 : }
    2666                 :             : 
    2667                 :             : /* Simple Const expression */
    2668                 :             : static Datum
    2669                 :      161452 : ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull)
    2670                 :             : {
    2671                 :      161452 :         ExprEvalStep *op = &state->steps[0];
    2672                 :             : 
    2673                 :      161452 :         *isnull = op->d.constval.isnull;
    2674                 :      322904 :         return op->d.constval.value;
    2675                 :      161452 : }
    2676                 :             : 
    2677                 :             : /* implementation of ExecJust(Inner|Outer|Scan)VarVirt */
    2678                 :             : static pg_attribute_always_inline Datum
    2679                 :           0 : ExecJustVarVirtImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
    2680                 :             : {
    2681                 :           0 :         ExprEvalStep *op = &state->steps[0];
    2682                 :           0 :         int                     attnum = op->d.var.attnum;
    2683                 :             : 
    2684                 :             :         /*
    2685                 :             :          * As it is guaranteed that a virtual slot is used, there never is a need
    2686                 :             :          * to perform tuple deforming (nor would it be possible). Therefore
    2687                 :             :          * execExpr.c has not emitted an EEOP_*_FETCHSOME step. Verify, as much as
    2688                 :             :          * possible, that that determination was accurate.
    2689                 :             :          */
    2690         [ #  # ]:           0 :         Assert(TTS_IS_VIRTUAL(slot));
    2691         [ #  # ]:           0 :         Assert(TTS_FIXED(slot));
    2692         [ #  # ]:           0 :         Assert(attnum >= 0 && attnum < slot->tts_nvalid);
    2693                 :             : 
    2694                 :           0 :         *isnull = slot->tts_isnull[attnum];
    2695                 :             : 
    2696                 :           0 :         return slot->tts_values[attnum];
    2697                 :           0 : }
    2698                 :             : 
    2699                 :             : /* Like ExecJustInnerVar, optimized for virtual slots */
    2700                 :             : static Datum
    2701                 :       66284 : ExecJustInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
    2702                 :             : {
    2703                 :       66284 :         return ExecJustVarVirtImpl(state, econtext->ecxt_innertuple, isnull);
    2704                 :             : }
    2705                 :             : 
    2706                 :             : /* Like ExecJustOuterVar, optimized for virtual slots */
    2707                 :             : static Datum
    2708                 :      188024 : ExecJustOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
    2709                 :             : {
    2710                 :      188024 :         return ExecJustVarVirtImpl(state, econtext->ecxt_outertuple, isnull);
    2711                 :             : }
    2712                 :             : 
    2713                 :             : /* Like ExecJustScanVar, optimized for virtual slots */
    2714                 :             : static Datum
    2715                 :           0 : ExecJustScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
    2716                 :             : {
    2717                 :           0 :         return ExecJustVarVirtImpl(state, econtext->ecxt_scantuple, isnull);
    2718                 :             : }
    2719                 :             : 
    2720                 :             : /* implementation of ExecJustAssign(Inner|Outer|Scan)VarVirt */
    2721                 :             : static pg_attribute_always_inline Datum
    2722                 :           0 : ExecJustAssignVarVirtImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull)
    2723                 :             : {
    2724                 :           0 :         ExprEvalStep *op = &state->steps[0];
    2725                 :           0 :         int                     attnum = op->d.assign_var.attnum;
    2726                 :           0 :         int                     resultnum = op->d.assign_var.resultnum;
    2727                 :           0 :         TupleTableSlot *outslot = state->resultslot;
    2728                 :             : 
    2729                 :             :         /* see ExecJustVarVirtImpl for comments */
    2730                 :             : 
    2731         [ #  # ]:           0 :         Assert(TTS_IS_VIRTUAL(inslot));
    2732         [ #  # ]:           0 :         Assert(TTS_FIXED(inslot));
    2733         [ #  # ]:           0 :         Assert(attnum >= 0 && attnum < inslot->tts_nvalid);
    2734         [ #  # ]:           0 :         Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts);
    2735                 :             : 
    2736                 :           0 :         outslot->tts_values[resultnum] = inslot->tts_values[attnum];
    2737                 :           0 :         outslot->tts_isnull[resultnum] = inslot->tts_isnull[attnum];
    2738                 :             : 
    2739                 :           0 :         return 0;
    2740                 :           0 : }
    2741                 :             : 
    2742                 :             : /* Like ExecJustAssignInnerVar, optimized for virtual slots */
    2743                 :             : static Datum
    2744                 :       20170 : ExecJustAssignInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
    2745                 :             : {
    2746                 :       20170 :         return ExecJustAssignVarVirtImpl(state, econtext->ecxt_innertuple, isnull);
    2747                 :             : }
    2748                 :             : 
    2749                 :             : /* Like ExecJustAssignOuterVar, optimized for virtual slots */
    2750                 :             : static Datum
    2751                 :      146113 : ExecJustAssignOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
    2752                 :             : {
    2753                 :      146113 :         return ExecJustAssignVarVirtImpl(state, econtext->ecxt_outertuple, isnull);
    2754                 :             : }
    2755                 :             : 
    2756                 :             : /* Like ExecJustAssignScanVar, optimized for virtual slots */
    2757                 :             : static Datum
    2758                 :       11174 : ExecJustAssignScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull)
    2759                 :             : {
    2760                 :       11174 :         return ExecJustAssignVarVirtImpl(state, econtext->ecxt_scantuple, isnull);
    2761                 :             : }
    2762                 :             : 
    2763                 :             : /*
    2764                 :             :  * implementation for hashing an inner Var, seeding with an initial value.
    2765                 :             :  */
    2766                 :             : static Datum
    2767                 :      407639 : ExecJustHashInnerVarWithIV(ExprState *state, ExprContext *econtext,
    2768                 :             :                                                    bool *isnull)
    2769                 :             : {
    2770                 :      407639 :         ExprEvalStep *fetchop = &state->steps[0];
    2771                 :      407639 :         ExprEvalStep *setivop = &state->steps[1];
    2772                 :      407639 :         ExprEvalStep *innervar = &state->steps[2];
    2773                 :      407639 :         ExprEvalStep *hashop = &state->steps[3];
    2774                 :      407639 :         FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data;
    2775                 :      407639 :         int                     attnum = innervar->d.var.attnum;
    2776                 :      407639 :         uint32          hashkey;
    2777                 :             : 
    2778                 :      407639 :         CheckOpSlotCompatibility(fetchop, econtext->ecxt_innertuple);
    2779                 :      407639 :         slot_getsomeattrs(econtext->ecxt_innertuple, fetchop->d.fetch.last_var);
    2780                 :             : 
    2781                 :      407639 :         fcinfo->args[0].value = econtext->ecxt_innertuple->tts_values[attnum];
    2782                 :      407639 :         fcinfo->args[0].isnull = econtext->ecxt_innertuple->tts_isnull[attnum];
    2783                 :             : 
    2784                 :      407639 :         hashkey = DatumGetUInt32(setivop->d.hashdatum_initvalue.init_value);
    2785                 :      407639 :         hashkey = pg_rotate_left32(hashkey, 1);
    2786                 :             : 
    2787         [ +  + ]:      407639 :         if (!fcinfo->args[0].isnull)
    2788                 :             :         {
    2789                 :      407489 :                 uint32          hashvalue;
    2790                 :             : 
    2791                 :      407489 :                 hashvalue = DatumGetUInt32(hashop->d.hashdatum.fn_addr(fcinfo));
    2792                 :      407489 :                 hashkey = hashkey ^ hashvalue;
    2793                 :      407489 :         }
    2794                 :             : 
    2795                 :      407639 :         *isnull = false;
    2796                 :      815278 :         return UInt32GetDatum(hashkey);
    2797                 :      407639 : }
    2798                 :             : 
    2799                 :             : /* implementation of ExecJustHash(Inner|Outer)Var */
    2800                 :             : static pg_attribute_always_inline Datum
    2801                 :           0 : ExecJustHashVarImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
    2802                 :             : {
    2803                 :           0 :         ExprEvalStep *fetchop = &state->steps[0];
    2804                 :           0 :         ExprEvalStep *var = &state->steps[1];
    2805                 :           0 :         ExprEvalStep *hashop = &state->steps[2];
    2806                 :           0 :         FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data;
    2807                 :           0 :         int                     attnum = var->d.var.attnum;
    2808                 :             : 
    2809                 :           0 :         CheckOpSlotCompatibility(fetchop, slot);
    2810                 :           0 :         slot_getsomeattrs(slot, fetchop->d.fetch.last_var);
    2811                 :             : 
    2812                 :           0 :         fcinfo->args[0].value = slot->tts_values[attnum];
    2813                 :           0 :         fcinfo->args[0].isnull = slot->tts_isnull[attnum];
    2814                 :             : 
    2815                 :           0 :         *isnull = false;
    2816                 :             : 
    2817         [ #  # ]:           0 :         if (!fcinfo->args[0].isnull)
    2818                 :           0 :                 return hashop->d.hashdatum.fn_addr(fcinfo);
    2819                 :             :         else
    2820                 :           0 :                 return (Datum) 0;
    2821                 :           0 : }
    2822                 :             : 
    2823                 :             : /* implementation for hashing an outer Var */
    2824                 :             : static Datum
    2825                 :       94939 : ExecJustHashOuterVar(ExprState *state, ExprContext *econtext, bool *isnull)
    2826                 :             : {
    2827                 :       94939 :         return ExecJustHashVarImpl(state, econtext->ecxt_outertuple, isnull);
    2828                 :             : }
    2829                 :             : 
    2830                 :             : /* implementation for hashing an inner Var */
    2831                 :             : static Datum
    2832                 :      368110 : ExecJustHashInnerVar(ExprState *state, ExprContext *econtext, bool *isnull)
    2833                 :             : {
    2834                 :      368110 :         return ExecJustHashVarImpl(state, econtext->ecxt_innertuple, isnull);
    2835                 :             : }
    2836                 :             : 
    2837                 :             : /* implementation of ExecJustHash(Inner|Outer)VarVirt */
    2838                 :             : static pg_attribute_always_inline Datum
    2839                 :           0 : ExecJustHashVarVirtImpl(ExprState *state, TupleTableSlot *slot, bool *isnull)
    2840                 :             : {
    2841                 :           0 :         ExprEvalStep *var = &state->steps[0];
    2842                 :           0 :         ExprEvalStep *hashop = &state->steps[1];
    2843                 :           0 :         FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data;
    2844                 :           0 :         int                     attnum = var->d.var.attnum;
    2845                 :             : 
    2846                 :           0 :         fcinfo->args[0].value = slot->tts_values[attnum];
    2847                 :           0 :         fcinfo->args[0].isnull = slot->tts_isnull[attnum];
    2848                 :             : 
    2849                 :           0 :         *isnull = false;
    2850                 :             : 
    2851         [ #  # ]:           0 :         if (!fcinfo->args[0].isnull)
    2852                 :           0 :                 return hashop->d.hashdatum.fn_addr(fcinfo);
    2853                 :             :         else
    2854                 :           0 :                 return (Datum) 0;
    2855                 :           0 : }
    2856                 :             : 
    2857                 :             : /* Like ExecJustHashInnerVar, optimized for virtual slots */
    2858                 :             : static Datum
    2859                 :      123009 : ExecJustHashInnerVarVirt(ExprState *state, ExprContext *econtext,
    2860                 :             :                                                  bool *isnull)
    2861                 :             : {
    2862                 :      123009 :         return ExecJustHashVarVirtImpl(state, econtext->ecxt_innertuple, isnull);
    2863                 :             : }
    2864                 :             : 
    2865                 :             : /* Like ExecJustHashOuterVar, optimized for virtual slots */
    2866                 :             : static Datum
    2867                 :      303978 : ExecJustHashOuterVarVirt(ExprState *state, ExprContext *econtext,
    2868                 :             :                                                  bool *isnull)
    2869                 :             : {
    2870                 :      303978 :         return ExecJustHashVarVirtImpl(state, econtext->ecxt_outertuple, isnull);
    2871                 :             : }
    2872                 :             : 
    2873                 :             : /*
    2874                 :             :  * implementation for hashing an outer Var.  Returns NULL on NULL input.
    2875                 :             :  */
    2876                 :             : static Datum
    2877                 :      776404 : ExecJustHashOuterVarStrict(ExprState *state, ExprContext *econtext,
    2878                 :             :                                                    bool *isnull)
    2879                 :             : {
    2880                 :      776404 :         ExprEvalStep *fetchop = &state->steps[0];
    2881                 :      776404 :         ExprEvalStep *var = &state->steps[1];
    2882                 :      776404 :         ExprEvalStep *hashop = &state->steps[2];
    2883                 :      776404 :         FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data;
    2884                 :      776404 :         int                     attnum = var->d.var.attnum;
    2885                 :             : 
    2886                 :      776404 :         CheckOpSlotCompatibility(fetchop, econtext->ecxt_outertuple);
    2887                 :      776404 :         slot_getsomeattrs(econtext->ecxt_outertuple, fetchop->d.fetch.last_var);
    2888                 :             : 
    2889                 :      776404 :         fcinfo->args[0].value = econtext->ecxt_outertuple->tts_values[attnum];
    2890                 :      776404 :         fcinfo->args[0].isnull = econtext->ecxt_outertuple->tts_isnull[attnum];
    2891                 :             : 
    2892         [ +  + ]:      776404 :         if (!fcinfo->args[0].isnull)
    2893                 :             :         {
    2894                 :      776359 :                 *isnull = false;
    2895                 :      776359 :                 return hashop->d.hashdatum.fn_addr(fcinfo);
    2896                 :             :         }
    2897                 :             :         else
    2898                 :             :         {
    2899                 :             :                 /* return NULL on NULL input */
    2900                 :          45 :                 *isnull = true;
    2901                 :          45 :                 return (Datum) 0;
    2902                 :             :         }
    2903                 :      776404 : }
    2904                 :             : 
    2905                 :             : #if defined(EEO_USE_COMPUTED_GOTO)
    2906                 :             : /*
    2907                 :             :  * Comparator used when building address->opcode lookup table for
    2908                 :             :  * ExecEvalStepOp() in the threaded dispatch case.
    2909                 :             :  */
    2910                 :             : static int
    2911                 :    30091415 : dispatch_compare_ptr(const void *a, const void *b)
    2912                 :             : {
    2913                 :    30091415 :         const ExprEvalOpLookup *la = (const ExprEvalOpLookup *) a;
    2914                 :    30091415 :         const ExprEvalOpLookup *lb = (const ExprEvalOpLookup *) b;
    2915                 :             : 
    2916         [ +  + ]:    30091415 :         if (la->opcode < lb->opcode)
    2917                 :    14503320 :                 return -1;
    2918         [ +  + ]:    15588095 :         else if (la->opcode > lb->opcode)
    2919                 :    10995173 :                 return 1;
    2920                 :     4592922 :         return 0;
    2921                 :    30091415 : }
    2922                 :             : #endif
    2923                 :             : 
    2924                 :             : /*
    2925                 :             :  * Do one-time initialization of interpretation machinery.
    2926                 :             :  */
    2927                 :             : static void
    2928                 :     1126690 : ExecInitInterpreter(void)
    2929                 :             : {
    2930                 :             : #if defined(EEO_USE_COMPUTED_GOTO)
    2931                 :             :         /* Set up externally-visible pointer to dispatch table */
    2932         [ +  + ]:     1126690 :         if (dispatch_table == NULL)
    2933                 :             :         {
    2934                 :         726 :                 dispatch_table = (const void **)
    2935                 :         726 :                         DatumGetPointer(ExecInterpExpr(NULL, NULL, NULL));
    2936                 :             : 
    2937                 :             :                 /* build reverse lookup table */
    2938         [ +  + ]:       87846 :                 for (int i = 0; i < EEOP_LAST; i++)
    2939                 :             :                 {
    2940                 :       87120 :                         reverse_dispatch_table[i].opcode = dispatch_table[i];
    2941                 :       87120 :                         reverse_dispatch_table[i].op = (ExprEvalOp) i;
    2942                 :       87120 :                 }
    2943                 :             : 
    2944                 :             :                 /* make it bsearch()able */
    2945                 :         726 :                 qsort(reverse_dispatch_table,
    2946                 :             :                           EEOP_LAST /* nmembers */ ,
    2947                 :             :                           sizeof(ExprEvalOpLookup),
    2948                 :             :                           dispatch_compare_ptr);
    2949                 :         726 :         }
    2950                 :             : #endif
    2951                 :     1126690 : }
    2952                 :             : 
    2953                 :             : /*
    2954                 :             :  * Function to return the opcode of an expression step.
    2955                 :             :  *
    2956                 :             :  * When direct-threading is in use, ExprState->opcode isn't easily
    2957                 :             :  * decipherable. This function returns the appropriate enum member.
    2958                 :             :  */
    2959                 :             : ExprEvalOp
    2960                 :     4675557 : ExecEvalStepOp(ExprState *state, ExprEvalStep *op)
    2961                 :             : {
    2962                 :             : #if defined(EEO_USE_COMPUTED_GOTO)
    2963         [ +  + ]:     4675557 :         if (state->flags & EEO_FLAG_DIRECT_THREADED)
    2964                 :             :         {
    2965                 :     4592922 :                 ExprEvalOpLookup key;
    2966                 :     4592922 :                 ExprEvalOpLookup *res;
    2967                 :             : 
    2968                 :     4592922 :                 key.opcode = (void *) op->opcode;
    2969                 :     4592922 :                 res = bsearch(&key,
    2970                 :             :                                           reverse_dispatch_table,
    2971                 :             :                                           EEOP_LAST /* nmembers */ ,
    2972                 :             :                                           sizeof(ExprEvalOpLookup),
    2973                 :             :                                           dispatch_compare_ptr);
    2974         [ +  - ]:     4592922 :                 Assert(res);                    /* unknown ops shouldn't get looked up */
    2975                 :     4592922 :                 return res->op;
    2976                 :     4592922 :         }
    2977                 :             : #endif
    2978                 :       82635 :         return (ExprEvalOp) op->opcode;
    2979                 :     4675557 : }
    2980                 :             : 
    2981                 :             : 
    2982                 :             : /*
    2983                 :             :  * Out-of-line helper functions for complex instructions.
    2984                 :             :  */
    2985                 :             : 
    2986                 :             : /*
    2987                 :             :  * Evaluate EEOP_FUNCEXPR_FUSAGE
    2988                 :             :  */
    2989                 :             : void
    2990                 :           8 : ExecEvalFuncExprFusage(ExprState *state, ExprEvalStep *op,
    2991                 :             :                                            ExprContext *econtext)
    2992                 :             : {
    2993                 :           8 :         FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
    2994                 :           8 :         PgStat_FunctionCallUsage fcusage;
    2995                 :           8 :         Datum           d;
    2996                 :             : 
    2997                 :           8 :         pgstat_init_function_usage(fcinfo, &fcusage);
    2998                 :             : 
    2999                 :           8 :         fcinfo->isnull = false;
    3000                 :           8 :         d = op->d.func.fn_addr(fcinfo);
    3001                 :           8 :         *op->resvalue = d;
    3002                 :           8 :         *op->resnull = fcinfo->isnull;
    3003                 :             : 
    3004                 :           8 :         pgstat_end_function_usage(&fcusage, true);
    3005                 :           8 : }
    3006                 :             : 
    3007                 :             : /*
    3008                 :             :  * Evaluate EEOP_FUNCEXPR_STRICT_FUSAGE
    3009                 :             :  */
    3010                 :             : void
    3011                 :           1 : ExecEvalFuncExprStrictFusage(ExprState *state, ExprEvalStep *op,
    3012                 :             :                                                          ExprContext *econtext)
    3013                 :             : {
    3014                 :             : 
    3015                 :           1 :         FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
    3016                 :           1 :         PgStat_FunctionCallUsage fcusage;
    3017                 :           1 :         NullableDatum *args = fcinfo->args;
    3018                 :           1 :         int                     nargs = op->d.func.nargs;
    3019                 :           1 :         Datum           d;
    3020                 :             : 
    3021                 :             :         /* strict function, so check for NULL args */
    3022   [ +  +  -  + ]:           3 :         for (int argno = 0; argno < nargs; argno++)
    3023                 :             :         {
    3024         [ -  + ]:           2 :                 if (args[argno].isnull)
    3025                 :             :                 {
    3026                 :           0 :                         *op->resnull = true;
    3027                 :           0 :                         return;
    3028                 :             :                 }
    3029                 :           2 :         }
    3030                 :             : 
    3031                 :           1 :         pgstat_init_function_usage(fcinfo, &fcusage);
    3032                 :             : 
    3033                 :           1 :         fcinfo->isnull = false;
    3034                 :           1 :         d = op->d.func.fn_addr(fcinfo);
    3035                 :           1 :         *op->resvalue = d;
    3036                 :           1 :         *op->resnull = fcinfo->isnull;
    3037                 :             : 
    3038                 :           1 :         pgstat_end_function_usage(&fcusage, true);
    3039         [ -  + ]:           1 : }
    3040                 :             : 
    3041                 :             : /*
    3042                 :             :  * Evaluate a PARAM_EXEC parameter.
    3043                 :             :  *
    3044                 :             :  * PARAM_EXEC params (internal executor parameters) are stored in the
    3045                 :             :  * ecxt_param_exec_vals array, and can be accessed by array index.
    3046                 :             :  */
    3047                 :             : void
    3048                 :      904019 : ExecEvalParamExec(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3049                 :             : {
    3050                 :      904019 :         ParamExecData *prm;
    3051                 :             : 
    3052                 :      904019 :         prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]);
    3053         [ +  + ]:      904019 :         if (unlikely(prm->execPlan != NULL))
    3054                 :             :         {
    3055                 :             :                 /* Parameter not evaluated yet, so go do it */
    3056                 :         887 :                 ExecSetParamPlan(prm->execPlan, econtext);
    3057                 :             :                 /* ExecSetParamPlan should have processed this param... */
    3058         [ +  - ]:         887 :                 Assert(prm->execPlan == NULL);
    3059                 :         887 :         }
    3060                 :      904019 :         *op->resvalue = prm->value;
    3061                 :      904019 :         *op->resnull = prm->isnull;
    3062                 :      904019 : }
    3063                 :             : 
    3064                 :             : /*
    3065                 :             :  * Evaluate a PARAM_EXTERN parameter.
    3066                 :             :  *
    3067                 :             :  * PARAM_EXTERN parameters must be sought in ecxt_param_list_info.
    3068                 :             :  */
    3069                 :             : void
    3070                 :     5558256 : ExecEvalParamExtern(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3071                 :             : {
    3072                 :     5558256 :         ParamListInfo paramInfo = econtext->ecxt_param_list_info;
    3073                 :     5558256 :         int                     paramId = op->d.param.paramid;
    3074                 :             : 
    3075   [ +  -  -  +  :     5558256 :         if (likely(paramInfo &&
                   +  - ]
    3076                 :             :                            paramId > 0 && paramId <= paramInfo->numParams))
    3077                 :             :         {
    3078                 :     5558256 :                 ParamExternData *prm;
    3079                 :     5558256 :                 ParamExternData prmdata;
    3080                 :             : 
    3081                 :             :                 /* give hook a chance in case parameter is dynamic */
    3082         [ -  + ]:     5558256 :                 if (paramInfo->paramFetch != NULL)
    3083                 :           0 :                         prm = paramInfo->paramFetch(paramInfo, paramId, false, &prmdata);
    3084                 :             :                 else
    3085                 :     5558256 :                         prm = &paramInfo->params[paramId - 1];
    3086                 :             : 
    3087         [ +  - ]:     5558256 :                 if (likely(OidIsValid(prm->ptype)))
    3088                 :             :                 {
    3089                 :             :                         /* safety check in case hook did something unexpected */
    3090         [ +  - ]:     5558256 :                         if (unlikely(prm->ptype != op->d.param.paramtype))
    3091   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
    3092                 :             :                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    3093                 :             :                                                  errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
    3094                 :             :                                                                 paramId,
    3095                 :             :                                                                 format_type_be(prm->ptype),
    3096                 :             :                                                                 format_type_be(op->d.param.paramtype))));
    3097                 :     5558256 :                         *op->resvalue = prm->value;
    3098                 :     5558256 :                         *op->resnull = prm->isnull;
    3099                 :     5558256 :                         return;
    3100                 :             :                 }
    3101         [ +  - ]:     5558256 :         }
    3102                 :             : 
    3103   [ #  #  #  # ]:           0 :         ereport(ERROR,
    3104                 :             :                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    3105                 :             :                          errmsg("no value found for parameter %d", paramId)));
    3106         [ -  + ]:     5558256 : }
    3107                 :             : 
    3108                 :             : /*
    3109                 :             :  * Set value of a param (currently always PARAM_EXEC) from
    3110                 :             :  * op->res{value,null}.
    3111                 :             :  */
    3112                 :             : void
    3113                 :       30603 : ExecEvalParamSet(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3114                 :             : {
    3115                 :       30603 :         ParamExecData *prm;
    3116                 :             : 
    3117                 :       30603 :         prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]);
    3118                 :             : 
    3119                 :             :         /* Shouldn't have a pending evaluation anymore */
    3120         [ +  - ]:       30603 :         Assert(prm->execPlan == NULL);
    3121                 :             : 
    3122                 :       30603 :         prm->value = *op->resvalue;
    3123                 :       30603 :         prm->isnull = *op->resnull;
    3124                 :       30603 : }
    3125                 :             : 
    3126                 :             : /*
    3127                 :             :  * Evaluate a CoerceViaIO node in soft-error mode.
    3128                 :             :  *
    3129                 :             :  * The source value is in op's result variable.
    3130                 :             :  *
    3131                 :             :  * Note: This implements EEOP_IOCOERCE_SAFE. If you change anything here,
    3132                 :             :  * also look at the inline code for EEOP_IOCOERCE.
    3133                 :             :  */
    3134                 :             : void
    3135                 :           0 : ExecEvalCoerceViaIOSafe(ExprState *state, ExprEvalStep *op)
    3136                 :             : {
    3137                 :           0 :         char       *str;
    3138                 :             : 
    3139                 :             :         /* call output function (similar to OutputFunctionCall) */
    3140         [ #  # ]:           0 :         if (*op->resnull)
    3141                 :             :         {
    3142                 :             :                 /* output functions are not called on nulls */
    3143                 :           0 :                 str = NULL;
    3144                 :           0 :         }
    3145                 :             :         else
    3146                 :             :         {
    3147                 :           0 :                 FunctionCallInfo fcinfo_out;
    3148                 :             : 
    3149                 :           0 :                 fcinfo_out = op->d.iocoerce.fcinfo_data_out;
    3150                 :           0 :                 fcinfo_out->args[0].value = *op->resvalue;
    3151                 :           0 :                 fcinfo_out->args[0].isnull = false;
    3152                 :             : 
    3153                 :           0 :                 fcinfo_out->isnull = false;
    3154                 :           0 :                 str = DatumGetCString(FunctionCallInvoke(fcinfo_out));
    3155                 :             : 
    3156                 :             :                 /* OutputFunctionCall assumes result isn't null */
    3157         [ #  # ]:           0 :                 Assert(!fcinfo_out->isnull);
    3158                 :           0 :         }
    3159                 :             : 
    3160                 :             :         /* call input function (similar to InputFunctionCallSafe) */
    3161   [ #  #  #  # ]:           0 :         if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL)
    3162                 :             :         {
    3163                 :           0 :                 FunctionCallInfo fcinfo_in;
    3164                 :             : 
    3165                 :           0 :                 fcinfo_in = op->d.iocoerce.fcinfo_data_in;
    3166                 :           0 :                 fcinfo_in->args[0].value = PointerGetDatum(str);
    3167                 :           0 :                 fcinfo_in->args[0].isnull = *op->resnull;
    3168                 :             :                 /* second and third arguments are already set up */
    3169                 :             : 
    3170                 :             :                 /* ErrorSaveContext must be present. */
    3171         [ #  # ]:           0 :                 Assert(IsA(fcinfo_in->context, ErrorSaveContext));
    3172                 :             : 
    3173                 :           0 :                 fcinfo_in->isnull = false;
    3174                 :           0 :                 *op->resvalue = FunctionCallInvoke(fcinfo_in);
    3175                 :             : 
    3176   [ #  #  #  #  :           0 :                 if (SOFT_ERROR_OCCURRED(fcinfo_in->context))
                   #  # ]
    3177                 :             :                 {
    3178                 :           0 :                         *op->resnull = true;
    3179                 :           0 :                         *op->resvalue = (Datum) 0;
    3180                 :           0 :                         return;
    3181                 :             :                 }
    3182                 :             : 
    3183                 :             :                 /* Should get null result if and only if str is NULL */
    3184         [ #  # ]:           0 :                 if (str == NULL)
    3185         [ #  # ]:           0 :                         Assert(*op->resnull);
    3186                 :             :                 else
    3187         [ #  # ]:           0 :                         Assert(!*op->resnull);
    3188         [ #  # ]:           0 :         }
    3189         [ #  # ]:           0 : }
    3190                 :             : 
    3191                 :             : /*
    3192                 :             :  * Evaluate a SQLValueFunction expression.
    3193                 :             :  */
    3194                 :             : void
    3195                 :        1262 : ExecEvalSQLValueFunction(ExprState *state, ExprEvalStep *op)
    3196                 :             : {
    3197                 :        1262 :         LOCAL_FCINFO(fcinfo, 0);
    3198                 :        1262 :         SQLValueFunction *svf = op->d.sqlvaluefunction.svf;
    3199                 :             : 
    3200                 :        1262 :         *op->resnull = false;
    3201                 :             : 
    3202                 :             :         /*
    3203                 :             :          * Note: current_schema() can return NULL.  current_user() etc currently
    3204                 :             :          * cannot, but might as well code those cases the same way for safety.
    3205                 :             :          */
    3206   [ +  +  +  +  :        1262 :         switch (svf->op)
          +  +  -  +  +  
                      + ]
    3207                 :             :         {
    3208                 :             :                 case SVFOP_CURRENT_DATE:
    3209                 :           8 :                         *op->resvalue = DateADTGetDatum(GetSQLCurrentDate());
    3210                 :           8 :                         break;
    3211                 :             :                 case SVFOP_CURRENT_TIME:
    3212                 :             :                 case SVFOP_CURRENT_TIME_N:
    3213                 :           4 :                         *op->resvalue = TimeTzADTPGetDatum(GetSQLCurrentTime(svf->typmod));
    3214                 :           4 :                         break;
    3215                 :             :                 case SVFOP_CURRENT_TIMESTAMP:
    3216                 :             :                 case SVFOP_CURRENT_TIMESTAMP_N:
    3217                 :          19 :                         *op->resvalue = TimestampTzGetDatum(GetSQLCurrentTimestamp(svf->typmod));
    3218                 :          19 :                         break;
    3219                 :             :                 case SVFOP_LOCALTIME:
    3220                 :             :                 case SVFOP_LOCALTIME_N:
    3221                 :           4 :                         *op->resvalue = TimeADTGetDatum(GetSQLLocalTime(svf->typmod));
    3222                 :           4 :                         break;
    3223                 :             :                 case SVFOP_LOCALTIMESTAMP:
    3224                 :             :                 case SVFOP_LOCALTIMESTAMP_N:
    3225                 :          11 :                         *op->resvalue = TimestampGetDatum(GetSQLLocalTimestamp(svf->typmod));
    3226                 :          11 :                         break;
    3227                 :             :                 case SVFOP_CURRENT_ROLE:
    3228                 :             :                 case SVFOP_CURRENT_USER:
    3229                 :             :                 case SVFOP_USER:
    3230                 :        1194 :                         InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
    3231                 :        1194 :                         *op->resvalue = current_user(fcinfo);
    3232                 :        1194 :                         *op->resnull = fcinfo->isnull;
    3233                 :        1194 :                         break;
    3234                 :             :                 case SVFOP_SESSION_USER:
    3235                 :          17 :                         InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
    3236                 :          17 :                         *op->resvalue = session_user(fcinfo);
    3237                 :          17 :                         *op->resnull = fcinfo->isnull;
    3238                 :          17 :                         break;
    3239                 :             :                 case SVFOP_CURRENT_CATALOG:
    3240                 :           2 :                         InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
    3241                 :           2 :                         *op->resvalue = current_database(fcinfo);
    3242                 :           2 :                         *op->resnull = fcinfo->isnull;
    3243                 :           2 :                         break;
    3244                 :             :                 case SVFOP_CURRENT_SCHEMA:
    3245                 :           3 :                         InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
    3246                 :           3 :                         *op->resvalue = current_schema(fcinfo);
    3247                 :           3 :                         *op->resnull = fcinfo->isnull;
    3248                 :           3 :                         break;
    3249                 :             :         }
    3250                 :        1262 : }
    3251                 :             : 
    3252                 :             : /*
    3253                 :             :  * Raise error if a CURRENT OF expression is evaluated.
    3254                 :             :  *
    3255                 :             :  * The planner should convert CURRENT OF into a TidScan qualification, or some
    3256                 :             :  * other special handling in a ForeignScan node.  So we have to be able to do
    3257                 :             :  * ExecInitExpr on a CurrentOfExpr, but we shouldn't ever actually execute it.
    3258                 :             :  * If we get here, we suppose we must be dealing with CURRENT OF on a foreign
    3259                 :             :  * table whose FDW doesn't handle it, and complain accordingly.
    3260                 :             :  */
    3261                 :             : void
    3262                 :           0 : ExecEvalCurrentOfExpr(ExprState *state, ExprEvalStep *op)
    3263                 :             : {
    3264   [ #  #  #  # ]:           0 :         ereport(ERROR,
    3265                 :             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3266                 :             :                          errmsg("WHERE CURRENT OF is not supported for this table type")));
    3267                 :           0 : }
    3268                 :             : 
    3269                 :             : /*
    3270                 :             :  * Evaluate NextValueExpr.
    3271                 :             :  */
    3272                 :             : void
    3273                 :         170 : ExecEvalNextValueExpr(ExprState *state, ExprEvalStep *op)
    3274                 :             : {
    3275                 :         170 :         int64           newval = nextval_internal(op->d.nextvalueexpr.seqid, false);
    3276                 :             : 
    3277   [ +  +  +  - ]:         170 :         switch (op->d.nextvalueexpr.seqtypid)
    3278                 :             :         {
    3279                 :             :                 case INT2OID:
    3280                 :           5 :                         *op->resvalue = Int16GetDatum((int16) newval);
    3281                 :           5 :                         break;
    3282                 :             :                 case INT4OID:
    3283                 :         152 :                         *op->resvalue = Int32GetDatum((int32) newval);
    3284                 :         152 :                         break;
    3285                 :             :                 case INT8OID:
    3286                 :          13 :                         *op->resvalue = Int64GetDatum(newval);
    3287                 :          13 :                         break;
    3288                 :             :                 default:
    3289   [ #  #  #  # ]:           0 :                         elog(ERROR, "unsupported sequence type %u",
    3290                 :             :                                  op->d.nextvalueexpr.seqtypid);
    3291                 :           0 :         }
    3292                 :         170 :         *op->resnull = false;
    3293                 :         170 : }
    3294                 :             : 
    3295                 :             : /*
    3296                 :             :  * Evaluate NullTest / IS NULL for rows.
    3297                 :             :  */
    3298                 :             : void
    3299                 :          95 : ExecEvalRowNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3300                 :             : {
    3301                 :          95 :         ExecEvalRowNullInt(state, op, econtext, true);
    3302                 :          95 : }
    3303                 :             : 
    3304                 :             : /*
    3305                 :             :  * Evaluate NullTest / IS NOT NULL for rows.
    3306                 :             :  */
    3307                 :             : void
    3308                 :          59 : ExecEvalRowNotNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3309                 :             : {
    3310                 :          59 :         ExecEvalRowNullInt(state, op, econtext, false);
    3311                 :          59 : }
    3312                 :             : 
    3313                 :             : /* Common code for IS [NOT] NULL on a row value */
    3314                 :             : static void
    3315                 :         154 : ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op,
    3316                 :             :                                    ExprContext *econtext, bool checkisnull)
    3317                 :             : {
    3318                 :         154 :         Datum           value = *op->resvalue;
    3319                 :         154 :         bool            isnull = *op->resnull;
    3320                 :         154 :         HeapTupleHeader tuple;
    3321                 :         154 :         Oid                     tupType;
    3322                 :         154 :         int32           tupTypmod;
    3323                 :         154 :         TupleDesc       tupDesc;
    3324                 :         154 :         HeapTupleData tmptup;
    3325                 :             : 
    3326                 :         154 :         *op->resnull = false;
    3327                 :             : 
    3328                 :             :         /* NULL row variables are treated just as NULL scalar columns */
    3329         [ +  + ]:         154 :         if (isnull)
    3330                 :             :         {
    3331                 :          18 :                 *op->resvalue = BoolGetDatum(checkisnull);
    3332                 :          18 :                 return;
    3333                 :             :         }
    3334                 :             : 
    3335                 :             :         /*
    3336                 :             :          * The SQL standard defines IS [NOT] NULL for a non-null rowtype argument
    3337                 :             :          * as:
    3338                 :             :          *
    3339                 :             :          * "R IS NULL" is true if every field is the null value.
    3340                 :             :          *
    3341                 :             :          * "R IS NOT NULL" is true if no field is the null value.
    3342                 :             :          *
    3343                 :             :          * This definition is (apparently intentionally) not recursive; so our
    3344                 :             :          * tests on the fields are primitive attisnull tests, not recursive checks
    3345                 :             :          * to see if they are all-nulls or no-nulls rowtypes.
    3346                 :             :          *
    3347                 :             :          * The standard does not consider the possibility of zero-field rows, but
    3348                 :             :          * here we consider them to vacuously satisfy both predicates.
    3349                 :             :          */
    3350                 :             : 
    3351                 :         136 :         tuple = DatumGetHeapTupleHeader(value);
    3352                 :             : 
    3353                 :         136 :         tupType = HeapTupleHeaderGetTypeId(tuple);
    3354                 :         136 :         tupTypmod = HeapTupleHeaderGetTypMod(tuple);
    3355                 :             : 
    3356                 :             :         /* Lookup tupdesc if first time through or if type changes */
    3357                 :         272 :         tupDesc = get_cached_rowtype(tupType, tupTypmod,
    3358                 :         136 :                                                                  &op->d.nulltest_row.rowcache, NULL);
    3359                 :             : 
    3360                 :             :         /*
    3361                 :             :          * heap_attisnull needs a HeapTuple not a bare HeapTupleHeader.
    3362                 :             :          */
    3363                 :         136 :         tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
    3364                 :         136 :         tmptup.t_data = tuple;
    3365                 :             : 
    3366   [ +  +  +  + ]:         409 :         for (int att = 1; att <= tupDesc->natts; att++)
    3367                 :             :         {
    3368                 :             :                 /* ignore dropped columns */
    3369         [ -  + ]:         273 :                 if (TupleDescCompactAttr(tupDesc, att - 1)->attisdropped)
    3370                 :           0 :                         continue;
    3371         [ +  + ]:         273 :                 if (heap_attisnull(&tmptup, att, tupDesc))
    3372                 :             :                 {
    3373                 :             :                         /* null field disproves IS NOT NULL */
    3374         [ +  + ]:           9 :                         if (!checkisnull)
    3375                 :             :                         {
    3376                 :           5 :                                 *op->resvalue = BoolGetDatum(false);
    3377                 :           5 :                                 return;
    3378                 :             :                         }
    3379                 :           4 :                 }
    3380                 :             :                 else
    3381                 :             :                 {
    3382                 :             :                         /* non-null field disproves IS NULL */
    3383         [ +  + ]:         264 :                         if (checkisnull)
    3384                 :             :                         {
    3385                 :          75 :                                 *op->resvalue = BoolGetDatum(false);
    3386                 :          75 :                                 return;
    3387                 :             :                         }
    3388                 :             :                 }
    3389                 :         193 :         }
    3390                 :             : 
    3391                 :          56 :         *op->resvalue = BoolGetDatum(true);
    3392         [ -  + ]:         154 : }
    3393                 :             : 
    3394                 :             : /*
    3395                 :             :  * Evaluate an ARRAY[] expression.
    3396                 :             :  *
    3397                 :             :  * The individual array elements (or subarrays) have already been evaluated
    3398                 :             :  * into op->d.arrayexpr.elemvalues[]/elemnulls[].
    3399                 :             :  */
    3400                 :             : void
    3401                 :      120667 : ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op)
    3402                 :             : {
    3403                 :      120667 :         ArrayType  *result;
    3404                 :      120667 :         Oid                     element_type = op->d.arrayexpr.elemtype;
    3405                 :      120667 :         int                     nelems = op->d.arrayexpr.nelems;
    3406                 :      120667 :         int                     ndims = 0;
    3407                 :      120667 :         int                     dims[MAXDIM];
    3408                 :      120667 :         int                     lbs[MAXDIM];
    3409                 :             : 
    3410                 :             :         /* Set non-null as default */
    3411                 :      120667 :         *op->resnull = false;
    3412                 :             : 
    3413         [ +  + ]:      120667 :         if (!op->d.arrayexpr.multidims)
    3414                 :             :         {
    3415                 :             :                 /* Elements are presumably of scalar type */
    3416                 :      120606 :                 Datum      *dvalues = op->d.arrayexpr.elemvalues;
    3417                 :      120606 :                 bool       *dnulls = op->d.arrayexpr.elemnulls;
    3418                 :             : 
    3419                 :             :                 /* setup for 1-D array of the given length */
    3420                 :      120606 :                 ndims = 1;
    3421                 :      120606 :                 dims[0] = nelems;
    3422                 :      120606 :                 lbs[0] = 1;
    3423                 :             : 
    3424                 :      241212 :                 result = construct_md_array(dvalues, dnulls, ndims, dims, lbs,
    3425                 :      120606 :                                                                         element_type,
    3426                 :      120606 :                                                                         op->d.arrayexpr.elemlength,
    3427                 :      120606 :                                                                         op->d.arrayexpr.elembyval,
    3428                 :      120606 :                                                                         op->d.arrayexpr.elemalign);
    3429                 :      120606 :         }
    3430                 :             :         else
    3431                 :             :         {
    3432                 :             :                 /* Must be nested array expressions */
    3433                 :          61 :                 int                     nbytes = 0;
    3434                 :          61 :                 int                     nitems;
    3435                 :          61 :                 int                     outer_nelems = 0;
    3436                 :          61 :                 int                     elem_ndims = 0;
    3437                 :          61 :                 int                *elem_dims = NULL;
    3438                 :          61 :                 int                *elem_lbs = NULL;
    3439                 :          61 :                 bool            firstone = true;
    3440                 :          61 :                 bool            havenulls = false;
    3441                 :          61 :                 bool            haveempty = false;
    3442                 :          61 :                 char      **subdata;
    3443                 :          61 :                 bits8     **subbitmaps;
    3444                 :          61 :                 int                *subbytes;
    3445                 :          61 :                 int                *subnitems;
    3446                 :          61 :                 int32           dataoffset;
    3447                 :          61 :                 char       *dat;
    3448                 :          61 :                 int                     iitem;
    3449                 :             : 
    3450                 :          61 :                 subdata = (char **) palloc(nelems * sizeof(char *));
    3451                 :          61 :                 subbitmaps = (bits8 **) palloc(nelems * sizeof(bits8 *));
    3452                 :          61 :                 subbytes = (int *) palloc(nelems * sizeof(int));
    3453                 :          61 :                 subnitems = (int *) palloc(nelems * sizeof(int));
    3454                 :             : 
    3455                 :             :                 /* loop through and get data area from each element */
    3456         [ +  + ]:         167 :                 for (int elemoff = 0; elemoff < nelems; elemoff++)
    3457                 :             :                 {
    3458                 :         106 :                         Datum           arraydatum;
    3459                 :         106 :                         bool            eisnull;
    3460                 :         106 :                         ArrayType  *array;
    3461                 :         106 :                         int                     this_ndims;
    3462                 :             : 
    3463                 :         106 :                         arraydatum = op->d.arrayexpr.elemvalues[elemoff];
    3464                 :         106 :                         eisnull = op->d.arrayexpr.elemnulls[elemoff];
    3465                 :             : 
    3466                 :             :                         /* temporarily ignore null subarrays */
    3467         [ -  + ]:         106 :                         if (eisnull)
    3468                 :             :                         {
    3469                 :           0 :                                 haveempty = true;
    3470                 :           0 :                                 continue;
    3471                 :             :                         }
    3472                 :             : 
    3473                 :         106 :                         array = DatumGetArrayTypeP(arraydatum);
    3474                 :             : 
    3475                 :             :                         /* run-time double-check on element type */
    3476         [ +  - ]:         106 :                         if (element_type != ARR_ELEMTYPE(array))
    3477   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
    3478                 :             :                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    3479                 :             :                                                  errmsg("cannot merge incompatible arrays"),
    3480                 :             :                                                  errdetail("Array with element type %s cannot be "
    3481                 :             :                                                                    "included in ARRAY construct with element type %s.",
    3482                 :             :                                                                    format_type_be(ARR_ELEMTYPE(array)),
    3483                 :             :                                                                    format_type_be(element_type))));
    3484                 :             : 
    3485                 :         106 :                         this_ndims = ARR_NDIM(array);
    3486                 :             :                         /* temporarily ignore zero-dimensional subarrays */
    3487         [ -  + ]:         106 :                         if (this_ndims <= 0)
    3488                 :             :                         {
    3489                 :           0 :                                 haveempty = true;
    3490                 :           0 :                                 continue;
    3491                 :             :                         }
    3492                 :             : 
    3493         [ +  + ]:         106 :                         if (firstone)
    3494                 :             :                         {
    3495                 :             :                                 /* Get sub-array details from first member */
    3496                 :          61 :                                 elem_ndims = this_ndims;
    3497                 :          61 :                                 ndims = elem_ndims + 1;
    3498         [ +  - ]:          61 :                                 if (ndims <= 0 || ndims > MAXDIM)
    3499   [ #  #  #  # ]:           0 :                                         ereport(ERROR,
    3500                 :             :                                                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    3501                 :             :                                                          errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
    3502                 :             :                                                                         ndims, MAXDIM)));
    3503                 :             : 
    3504                 :          61 :                                 elem_dims = (int *) palloc(elem_ndims * sizeof(int));
    3505                 :          61 :                                 memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int));
    3506                 :          61 :                                 elem_lbs = (int *) palloc(elem_ndims * sizeof(int));
    3507                 :          61 :                                 memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int));
    3508                 :             : 
    3509                 :          61 :                                 firstone = false;
    3510                 :          61 :                         }
    3511                 :             :                         else
    3512                 :             :                         {
    3513                 :             :                                 /* Check other sub-arrays are compatible */
    3514         [ +  - ]:          45 :                                 if (elem_ndims != this_ndims ||
    3515                 :          90 :                                         memcmp(elem_dims, ARR_DIMS(array),
    3516                 :          90 :                                                    elem_ndims * sizeof(int)) != 0 ||
    3517                 :          90 :                                         memcmp(elem_lbs, ARR_LBOUND(array),
    3518                 :          90 :                                                    elem_ndims * sizeof(int)) != 0)
    3519   [ #  #  #  # ]:           0 :                                         ereport(ERROR,
    3520                 :             :                                                         (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
    3521                 :             :                                                          errmsg("multidimensional arrays must have array "
    3522                 :             :                                                                         "expressions with matching dimensions")));
    3523                 :             :                         }
    3524                 :             : 
    3525         [ +  + ]:         106 :                         subdata[outer_nelems] = ARR_DATA_PTR(array);
    3526         [ +  + ]:         106 :                         subbitmaps[outer_nelems] = ARR_NULLBITMAP(array);
    3527         [ +  + ]:         106 :                         subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
    3528                 :         106 :                         nbytes += subbytes[outer_nelems];
    3529                 :             :                         /* check for overflow of total request */
    3530         [ +  - ]:         106 :                         if (!AllocSizeIsValid(nbytes))
    3531   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
    3532                 :             :                                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    3533                 :             :                                                  errmsg("array size exceeds the maximum allowed (%d)",
    3534                 :             :                                                                 (int) MaxAllocSize)));
    3535                 :         212 :                         subnitems[outer_nelems] = ArrayGetNItems(this_ndims,
    3536                 :         106 :                                                                                                          ARR_DIMS(array));
    3537                 :         106 :                         havenulls |= ARR_HASNULL(array);
    3538                 :         106 :                         outer_nelems++;
    3539         [ -  + ]:         106 :                 }
    3540                 :             : 
    3541                 :             :                 /*
    3542                 :             :                  * If all items were null or empty arrays, return an empty array;
    3543                 :             :                  * otherwise, if some were and some weren't, raise error.  (Note: we
    3544                 :             :                  * must special-case this somehow to avoid trying to generate a 1-D
    3545                 :             :                  * array formed from empty arrays.  It's not ideal...)
    3546                 :             :                  */
    3547         [ +  - ]:          61 :                 if (haveempty)
    3548                 :             :                 {
    3549         [ #  # ]:           0 :                         if (ndims == 0)         /* didn't find any nonempty array */
    3550                 :             :                         {
    3551                 :           0 :                                 *op->resvalue = PointerGetDatum(construct_empty_array(element_type));
    3552                 :           0 :                                 return;
    3553                 :             :                         }
    3554   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    3555                 :             :                                         (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
    3556                 :             :                                          errmsg("multidimensional arrays must have array "
    3557                 :             :                                                         "expressions with matching dimensions")));
    3558                 :           0 :                 }
    3559                 :             : 
    3560                 :             :                 /* setup for multi-D array */
    3561                 :          61 :                 dims[0] = outer_nelems;
    3562                 :          61 :                 lbs[0] = 1;
    3563         [ +  + ]:         150 :                 for (int i = 1; i < ndims; i++)
    3564                 :             :                 {
    3565                 :          89 :                         dims[i] = elem_dims[i - 1];
    3566                 :          89 :                         lbs[i] = elem_lbs[i - 1];
    3567                 :          89 :                 }
    3568                 :             : 
    3569                 :             :                 /* check for subscript overflow */
    3570                 :          61 :                 nitems = ArrayGetNItems(ndims, dims);
    3571                 :          61 :                 ArrayCheckBounds(ndims, dims, lbs);
    3572                 :             : 
    3573         [ +  + ]:          61 :                 if (havenulls)
    3574                 :             :                 {
    3575                 :           1 :                         dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
    3576                 :           1 :                         nbytes += dataoffset;
    3577                 :           1 :                 }
    3578                 :             :                 else
    3579                 :             :                 {
    3580                 :          60 :                         dataoffset = 0;         /* marker for no null bitmap */
    3581                 :          60 :                         nbytes += ARR_OVERHEAD_NONULLS(ndims);
    3582                 :             :                 }
    3583                 :             : 
    3584                 :          61 :                 result = (ArrayType *) palloc0(nbytes);
    3585                 :          61 :                 SET_VARSIZE(result, nbytes);
    3586                 :          61 :                 result->ndim = ndims;
    3587                 :          61 :                 result->dataoffset = dataoffset;
    3588                 :          61 :                 result->elemtype = element_type;
    3589                 :          61 :                 memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
    3590                 :          61 :                 memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
    3591                 :             : 
    3592         [ +  + ]:          61 :                 dat = ARR_DATA_PTR(result);
    3593                 :          61 :                 iitem = 0;
    3594         [ +  + ]:         167 :                 for (int i = 0; i < outer_nelems; i++)
    3595                 :             :                 {
    3596                 :         106 :                         memcpy(dat, subdata[i], subbytes[i]);
    3597                 :         106 :                         dat += subbytes[i];
    3598         [ +  + ]:         106 :                         if (havenulls)
    3599         [ -  + ]:           2 :                                 array_bitmap_copy(ARR_NULLBITMAP(result), iitem,
    3600                 :           2 :                                                                   subbitmaps[i], 0,
    3601                 :           2 :                                                                   subnitems[i]);
    3602                 :         106 :                         iitem += subnitems[i];
    3603                 :         106 :                 }
    3604         [ -  + ]:          61 :         }
    3605                 :             : 
    3606                 :      120667 :         *op->resvalue = PointerGetDatum(result);
    3607                 :      120667 : }
    3608                 :             : 
    3609                 :             : /*
    3610                 :             :  * Evaluate an ArrayCoerceExpr expression.
    3611                 :             :  *
    3612                 :             :  * Source array is in step's result variable.
    3613                 :             :  */
    3614                 :             : void
    3615                 :       11120 : ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3616                 :             : {
    3617                 :       11120 :         Datum           arraydatum;
    3618                 :             : 
    3619                 :             :         /* NULL array -> NULL result */
    3620         [ +  + ]:       11120 :         if (*op->resnull)
    3621                 :           3 :                 return;
    3622                 :             : 
    3623                 :       11117 :         arraydatum = *op->resvalue;
    3624                 :             : 
    3625                 :             :         /*
    3626                 :             :          * If it's binary-compatible, modify the element type in the array header,
    3627                 :             :          * but otherwise leave the array as we received it.
    3628                 :             :          */
    3629         [ +  + ]:       11117 :         if (op->d.arraycoerce.elemexprstate == NULL)
    3630                 :             :         {
    3631                 :             :                 /* Detoast input array if necessary, and copy in any case */
    3632                 :       11065 :                 ArrayType  *array = DatumGetArrayTypePCopy(arraydatum);
    3633                 :             : 
    3634                 :       11065 :                 ARR_ELEMTYPE(array) = op->d.arraycoerce.resultelemtype;
    3635                 :       11065 :                 *op->resvalue = PointerGetDatum(array);
    3636                 :             :                 return;
    3637                 :       11065 :         }
    3638                 :             : 
    3639                 :             :         /*
    3640                 :             :          * Use array_map to apply the sub-expression to each array element.
    3641                 :             :          */
    3642                 :         104 :         *op->resvalue = array_map(arraydatum,
    3643                 :          52 :                                                           op->d.arraycoerce.elemexprstate,
    3644                 :          52 :                                                           econtext,
    3645                 :          52 :                                                           op->d.arraycoerce.resultelemtype,
    3646                 :          52 :                                                           op->d.arraycoerce.amstate);
    3647         [ -  + ]:       11120 : }
    3648                 :             : 
    3649                 :             : /*
    3650                 :             :  * Evaluate a ROW() expression.
    3651                 :             :  *
    3652                 :             :  * The individual columns have already been evaluated into
    3653                 :             :  * op->d.row.elemvalues[]/elemnulls[].
    3654                 :             :  */
    3655                 :             : void
    3656                 :        6717 : ExecEvalRow(ExprState *state, ExprEvalStep *op)
    3657                 :             : {
    3658                 :        6717 :         HeapTuple       tuple;
    3659                 :             : 
    3660                 :             :         /* build tuple from evaluated field values */
    3661                 :       13434 :         tuple = heap_form_tuple(op->d.row.tupdesc,
    3662                 :        6717 :                                                         op->d.row.elemvalues,
    3663                 :        6717 :                                                         op->d.row.elemnulls);
    3664                 :             : 
    3665                 :        6717 :         *op->resvalue = HeapTupleGetDatum(tuple);
    3666                 :        6717 :         *op->resnull = false;
    3667                 :        6717 : }
    3668                 :             : 
    3669                 :             : /*
    3670                 :             :  * Evaluate GREATEST() or LEAST() expression (note this is *not* MIN()/MAX()).
    3671                 :             :  *
    3672                 :             :  * All of the to-be-compared expressions have already been evaluated into
    3673                 :             :  * op->d.minmax.values[]/nulls[].
    3674                 :             :  */
    3675                 :             : void
    3676                 :         154 : ExecEvalMinMax(ExprState *state, ExprEvalStep *op)
    3677                 :             : {
    3678                 :         154 :         Datum      *values = op->d.minmax.values;
    3679                 :         154 :         bool       *nulls = op->d.minmax.nulls;
    3680                 :         154 :         FunctionCallInfo fcinfo = op->d.minmax.fcinfo_data;
    3681                 :         154 :         MinMaxOp        operator = op->d.minmax.op;
    3682                 :             : 
    3683                 :             :         /* set at initialization */
    3684         [ +  - ]:         154 :         Assert(fcinfo->args[0].isnull == false);
    3685         [ +  - ]:         154 :         Assert(fcinfo->args[1].isnull == false);
    3686                 :             : 
    3687                 :             :         /* default to null result */
    3688                 :         154 :         *op->resnull = true;
    3689                 :             : 
    3690         [ +  + ]:         511 :         for (int off = 0; off < op->d.minmax.nelems; off++)
    3691                 :             :         {
    3692                 :             :                 /* ignore NULL inputs */
    3693         [ +  + ]:         357 :                 if (nulls[off])
    3694                 :           6 :                         continue;
    3695                 :             : 
    3696         [ +  + ]:         351 :                 if (*op->resnull)
    3697                 :             :                 {
    3698                 :             :                         /* first nonnull input, adopt value */
    3699                 :         154 :                         *op->resvalue = values[off];
    3700                 :         154 :                         *op->resnull = false;
    3701                 :         154 :                 }
    3702                 :             :                 else
    3703                 :             :                 {
    3704                 :         197 :                         int                     cmpresult;
    3705                 :             : 
    3706                 :             :                         /* apply comparison function */
    3707                 :         197 :                         fcinfo->args[0].value = *op->resvalue;
    3708                 :         197 :                         fcinfo->args[1].value = values[off];
    3709                 :             : 
    3710                 :         197 :                         fcinfo->isnull = false;
    3711                 :         197 :                         cmpresult = DatumGetInt32(FunctionCallInvoke(fcinfo));
    3712         [ -  + ]:         197 :                         if (fcinfo->isnull) /* probably should not happen */
    3713                 :           0 :                                 continue;
    3714                 :             : 
    3715   [ +  +  +  + ]:         197 :                         if (cmpresult > 0 && operator == IS_LEAST)
    3716                 :          25 :                                 *op->resvalue = values[off];
    3717   [ +  +  +  + ]:         172 :                         else if (cmpresult < 0 && operator == IS_GREATEST)
    3718                 :          30 :                                 *op->resvalue = values[off];
    3719      [ -  -  + ]:         197 :                 }
    3720                 :         351 :         }
    3721                 :         154 : }
    3722                 :             : 
    3723                 :             : /*
    3724                 :             :  * Evaluate a FieldSelect node.
    3725                 :             :  *
    3726                 :             :  * Source record is in step's result variable.
    3727                 :             :  */
    3728                 :             : void
    3729                 :       65917 : ExecEvalFieldSelect(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3730                 :             : {
    3731                 :       65917 :         AttrNumber      fieldnum = op->d.fieldselect.fieldnum;
    3732                 :       65917 :         Datum           tupDatum;
    3733                 :       65917 :         HeapTupleHeader tuple;
    3734                 :       65917 :         Oid                     tupType;
    3735                 :       65917 :         int32           tupTypmod;
    3736                 :       65917 :         TupleDesc       tupDesc;
    3737                 :       65917 :         Form_pg_attribute attr;
    3738                 :       65917 :         HeapTupleData tmptup;
    3739                 :             : 
    3740                 :             :         /* NULL record -> NULL result */
    3741         [ +  + ]:       65917 :         if (*op->resnull)
    3742                 :          17 :                 return;
    3743                 :             : 
    3744                 :       65900 :         tupDatum = *op->resvalue;
    3745                 :             : 
    3746                 :             :         /* We can special-case expanded records for speed */
    3747         [ +  + ]:       65900 :         if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(tupDatum)))
    3748                 :             :         {
    3749                 :          79 :                 ExpandedRecordHeader *erh = (ExpandedRecordHeader *) DatumGetEOHP(tupDatum);
    3750                 :             : 
    3751         [ +  - ]:          79 :                 Assert(erh->er_magic == ER_MAGIC);
    3752                 :             : 
    3753                 :             :                 /* Extract record's TupleDesc */
    3754                 :          79 :                 tupDesc = expanded_record_get_tupdesc(erh);
    3755                 :             : 
    3756                 :             :                 /*
    3757                 :             :                  * Find field's attr record.  Note we don't support system columns
    3758                 :             :                  * here: a datum tuple doesn't have valid values for most of the
    3759                 :             :                  * interesting system columns anyway.
    3760                 :             :                  */
    3761         [ +  - ]:          79 :                 if (fieldnum <= 0)           /* should never happen */
    3762   [ #  #  #  # ]:           0 :                         elog(ERROR, "unsupported reference to system column %d in FieldSelect",
    3763                 :             :                                  fieldnum);
    3764         [ +  - ]:          79 :                 if (fieldnum > tupDesc->natts)    /* should never happen */
    3765   [ #  #  #  # ]:           0 :                         elog(ERROR, "attribute number %d exceeds number of columns %d",
    3766                 :             :                                  fieldnum, tupDesc->natts);
    3767                 :          79 :                 attr = TupleDescAttr(tupDesc, fieldnum - 1);
    3768                 :             : 
    3769                 :             :                 /* Check for dropped column, and force a NULL result if so */
    3770         [ -  + ]:          79 :                 if (attr->attisdropped)
    3771                 :             :                 {
    3772                 :           0 :                         *op->resnull = true;
    3773                 :           0 :                         return;
    3774                 :             :                 }
    3775                 :             : 
    3776                 :             :                 /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
    3777                 :             :                 /* As in CheckVarSlotCompatibility, we should but can't check typmod */
    3778         [ +  - ]:          79 :                 if (op->d.fieldselect.resulttype != attr->atttypid)
    3779   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    3780                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
    3781                 :             :                                          errmsg("attribute %d has wrong type", fieldnum),
    3782                 :             :                                          errdetail("Table has type %s, but query expects %s.",
    3783                 :             :                                                            format_type_be(attr->atttypid),
    3784                 :             :                                                            format_type_be(op->d.fieldselect.resulttype))));
    3785                 :             : 
    3786                 :             :                 /* extract the field */
    3787                 :         158 :                 *op->resvalue = expanded_record_get_field(erh, fieldnum,
    3788                 :          79 :                                                                                                   op->resnull);
    3789         [ -  + ]:          79 :         }
    3790                 :             :         else
    3791                 :             :         {
    3792                 :             :                 /* Get the composite datum and extract its type fields */
    3793                 :       65821 :                 tuple = DatumGetHeapTupleHeader(tupDatum);
    3794                 :             : 
    3795                 :       65821 :                 tupType = HeapTupleHeaderGetTypeId(tuple);
    3796                 :       65821 :                 tupTypmod = HeapTupleHeaderGetTypMod(tuple);
    3797                 :             : 
    3798                 :             :                 /* Lookup tupdesc if first time through or if type changes */
    3799                 :      131642 :                 tupDesc = get_cached_rowtype(tupType, tupTypmod,
    3800                 :       65821 :                                                                          &op->d.fieldselect.rowcache, NULL);
    3801                 :             : 
    3802                 :             :                 /*
    3803                 :             :                  * Find field's attr record.  Note we don't support system columns
    3804                 :             :                  * here: a datum tuple doesn't have valid values for most of the
    3805                 :             :                  * interesting system columns anyway.
    3806                 :             :                  */
    3807         [ +  - ]:       65821 :                 if (fieldnum <= 0)           /* should never happen */
    3808   [ #  #  #  # ]:           0 :                         elog(ERROR, "unsupported reference to system column %d in FieldSelect",
    3809                 :             :                                  fieldnum);
    3810         [ +  - ]:       65821 :                 if (fieldnum > tupDesc->natts)    /* should never happen */
    3811   [ #  #  #  # ]:           0 :                         elog(ERROR, "attribute number %d exceeds number of columns %d",
    3812                 :             :                                  fieldnum, tupDesc->natts);
    3813                 :       65821 :                 attr = TupleDescAttr(tupDesc, fieldnum - 1);
    3814                 :             : 
    3815                 :             :                 /* Check for dropped column, and force a NULL result if so */
    3816         [ -  + ]:       65821 :                 if (attr->attisdropped)
    3817                 :             :                 {
    3818                 :           0 :                         *op->resnull = true;
    3819                 :           0 :                         return;
    3820                 :             :                 }
    3821                 :             : 
    3822                 :             :                 /* Check for type mismatch --- possible after ALTER COLUMN TYPE? */
    3823                 :             :                 /* As in CheckVarSlotCompatibility, we should but can't check typmod */
    3824         [ +  - ]:       65821 :                 if (op->d.fieldselect.resulttype != attr->atttypid)
    3825   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    3826                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
    3827                 :             :                                          errmsg("attribute %d has wrong type", fieldnum),
    3828                 :             :                                          errdetail("Table has type %s, but query expects %s.",
    3829                 :             :                                                            format_type_be(attr->atttypid),
    3830                 :             :                                                            format_type_be(op->d.fieldselect.resulttype))));
    3831                 :             : 
    3832                 :             :                 /* heap_getattr needs a HeapTuple not a bare HeapTupleHeader */
    3833                 :       65821 :                 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
    3834                 :       65821 :                 tmptup.t_data = tuple;
    3835                 :             : 
    3836                 :             :                 /* extract the field */
    3837                 :       65821 :                 *op->resvalue = heap_getattr(&tmptup,
    3838                 :       65821 :                                                                          fieldnum,
    3839                 :       65821 :                                                                          tupDesc,
    3840                 :       65821 :                                                                          op->resnull);
    3841                 :             :         }
    3842         [ -  + ]:       65917 : }
    3843                 :             : 
    3844                 :             : /*
    3845                 :             :  * Deform source tuple, filling in the step's values/nulls arrays, before
    3846                 :             :  * evaluating individual new values as part of a FieldStore expression.
    3847                 :             :  * Subsequent steps will overwrite individual elements of the values/nulls
    3848                 :             :  * arrays with the new field values, and then FIELDSTORE_FORM will build the
    3849                 :             :  * new tuple value.
    3850                 :             :  *
    3851                 :             :  * Source record is in step's result variable.
    3852                 :             :  */
    3853                 :             : void
    3854                 :          85 : ExecEvalFieldStoreDeForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3855                 :             : {
    3856         [ +  + ]:          85 :         if (*op->resnull)
    3857                 :             :         {
    3858                 :             :                 /* Convert null input tuple into an all-nulls row */
    3859                 :          42 :                 memset(op->d.fieldstore.nulls, true,
    3860                 :             :                            op->d.fieldstore.ncolumns * sizeof(bool));
    3861                 :          42 :         }
    3862                 :             :         else
    3863                 :             :         {
    3864                 :             :                 /*
    3865                 :             :                  * heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader. We
    3866                 :             :                  * set all the fields in the struct just in case.
    3867                 :             :                  */
    3868                 :          43 :                 Datum           tupDatum = *op->resvalue;
    3869                 :          43 :                 HeapTupleHeader tuphdr;
    3870                 :          43 :                 HeapTupleData tmptup;
    3871                 :          43 :                 TupleDesc       tupDesc;
    3872                 :             : 
    3873                 :          43 :                 tuphdr = DatumGetHeapTupleHeader(tupDatum);
    3874                 :          43 :                 tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
    3875                 :          43 :                 ItemPointerSetInvalid(&(tmptup.t_self));
    3876                 :          43 :                 tmptup.t_tableOid = InvalidOid;
    3877                 :          43 :                 tmptup.t_data = tuphdr;
    3878                 :             : 
    3879                 :             :                 /*
    3880                 :             :                  * Lookup tupdesc if first time through or if type changes.  Because
    3881                 :             :                  * we don't pin the tupdesc, we must not do this lookup until after
    3882                 :             :                  * doing DatumGetHeapTupleHeader: that could do database access while
    3883                 :             :                  * detoasting the datum.
    3884                 :             :                  */
    3885                 :          86 :                 tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1,
    3886                 :          43 :                                                                          op->d.fieldstore.rowcache, NULL);
    3887                 :             : 
    3888                 :             :                 /* Check that current tupdesc doesn't have more fields than allocated */
    3889         [ +  - ]:          43 :                 if (unlikely(tupDesc->natts > op->d.fieldstore.ncolumns))
    3890   [ #  #  #  # ]:           0 :                         elog(ERROR, "too many columns in composite type %u",
    3891                 :             :                                  op->d.fieldstore.fstore->resulttype);
    3892                 :             : 
    3893                 :          86 :                 heap_deform_tuple(&tmptup, tupDesc,
    3894                 :          43 :                                                   op->d.fieldstore.values,
    3895                 :          43 :                                                   op->d.fieldstore.nulls);
    3896                 :          43 :         }
    3897                 :          85 : }
    3898                 :             : 
    3899                 :             : /*
    3900                 :             :  * Compute the new composite datum after each individual field value of a
    3901                 :             :  * FieldStore expression has been evaluated.
    3902                 :             :  */
    3903                 :             : void
    3904                 :          85 : ExecEvalFieldStoreForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3905                 :             : {
    3906                 :          85 :         TupleDesc       tupDesc;
    3907                 :          85 :         HeapTuple       tuple;
    3908                 :             : 
    3909                 :             :         /* Lookup tupdesc (should be valid already) */
    3910                 :         170 :         tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1,
    3911                 :          85 :                                                                  op->d.fieldstore.rowcache, NULL);
    3912                 :             : 
    3913                 :         170 :         tuple = heap_form_tuple(tupDesc,
    3914                 :          85 :                                                         op->d.fieldstore.values,
    3915                 :          85 :                                                         op->d.fieldstore.nulls);
    3916                 :             : 
    3917                 :          85 :         *op->resvalue = HeapTupleGetDatum(tuple);
    3918                 :          85 :         *op->resnull = false;
    3919                 :          85 : }
    3920                 :             : 
    3921                 :             : /*
    3922                 :             :  * Evaluate a rowtype coercion operation.
    3923                 :             :  * This may require rearranging field positions.
    3924                 :             :  *
    3925                 :             :  * Source record is in step's result variable.
    3926                 :             :  */
    3927                 :             : void
    3928                 :         967 : ExecEvalConvertRowtype(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    3929                 :             : {
    3930                 :         967 :         HeapTuple       result;
    3931                 :         967 :         Datum           tupDatum;
    3932                 :         967 :         HeapTupleHeader tuple;
    3933                 :         967 :         HeapTupleData tmptup;
    3934                 :         967 :         TupleDesc       indesc,
    3935                 :             :                                 outdesc;
    3936                 :         967 :         bool            changed = false;
    3937                 :             : 
    3938                 :             :         /* NULL in -> NULL out */
    3939         [ +  + ]:         967 :         if (*op->resnull)
    3940                 :           7 :                 return;
    3941                 :             : 
    3942                 :         960 :         tupDatum = *op->resvalue;
    3943                 :         960 :         tuple = DatumGetHeapTupleHeader(tupDatum);
    3944                 :             : 
    3945                 :             :         /*
    3946                 :             :          * Lookup tupdescs if first time through or if type changes.  We'd better
    3947                 :             :          * pin them since type conversion functions could do catalog lookups and
    3948                 :             :          * hence cause cache invalidation.
    3949                 :             :          */
    3950                 :        1920 :         indesc = get_cached_rowtype(op->d.convert_rowtype.inputtype, -1,
    3951                 :         960 :                                                                 op->d.convert_rowtype.incache,
    3952                 :             :                                                                 &changed);
    3953                 :         960 :         IncrTupleDescRefCount(indesc);
    3954                 :        1920 :         outdesc = get_cached_rowtype(op->d.convert_rowtype.outputtype, -1,
    3955                 :         960 :                                                                  op->d.convert_rowtype.outcache,
    3956                 :             :                                                                  &changed);
    3957                 :         960 :         IncrTupleDescRefCount(outdesc);
    3958                 :             : 
    3959                 :             :         /*
    3960                 :             :          * We used to be able to assert that incoming tuples are marked with
    3961                 :             :          * exactly the rowtype of indesc.  However, now that ExecEvalWholeRowVar
    3962                 :             :          * might change the tuples' marking to plain RECORD due to inserting
    3963                 :             :          * aliases, we can only make this weak test:
    3964                 :             :          */
    3965   [ -  +  #  # ]:         960 :         Assert(HeapTupleHeaderGetTypeId(tuple) == indesc->tdtypeid ||
    3966                 :             :                    HeapTupleHeaderGetTypeId(tuple) == RECORDOID);
    3967                 :             : 
    3968                 :             :         /* if first time through, or after change, initialize conversion map */
    3969         [ +  + ]:         960 :         if (changed)
    3970                 :             :         {
    3971                 :          71 :                 MemoryContext old_cxt;
    3972                 :             : 
    3973                 :             :                 /* allocate map in long-lived memory context */
    3974                 :          71 :                 old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
    3975                 :             : 
    3976                 :             :                 /* prepare map from old to new attribute numbers */
    3977                 :          71 :                 op->d.convert_rowtype.map = convert_tuples_by_name(indesc, outdesc);
    3978                 :             : 
    3979                 :          71 :                 MemoryContextSwitchTo(old_cxt);
    3980                 :          71 :         }
    3981                 :             : 
    3982                 :             :         /* Following steps need a HeapTuple not a bare HeapTupleHeader */
    3983                 :         960 :         tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
    3984                 :         960 :         tmptup.t_data = tuple;
    3985                 :             : 
    3986         [ +  + ]:         960 :         if (op->d.convert_rowtype.map != NULL)
    3987                 :             :         {
    3988                 :             :                 /* Full conversion with attribute rearrangement needed */
    3989                 :          60 :                 result = execute_attr_map_tuple(&tmptup, op->d.convert_rowtype.map);
    3990                 :             :                 /* Result already has appropriate composite-datum header fields */
    3991                 :          60 :                 *op->resvalue = HeapTupleGetDatum(result);
    3992                 :          60 :         }
    3993                 :             :         else
    3994                 :             :         {
    3995                 :             :                 /*
    3996                 :             :                  * The tuple is physically compatible as-is, but we need to insert the
    3997                 :             :                  * destination rowtype OID in its composite-datum header field, so we
    3998                 :             :                  * have to copy it anyway.  heap_copy_tuple_as_datum() is convenient
    3999                 :             :                  * for this since it will both make the physical copy and insert the
    4000                 :             :                  * correct composite header fields.  Note that we aren't expecting to
    4001                 :             :                  * have to flatten any toasted fields: the input was a composite
    4002                 :             :                  * datum, so it shouldn't contain any.  So heap_copy_tuple_as_datum()
    4003                 :             :                  * is overkill here, but its check for external fields is cheap.
    4004                 :             :                  */
    4005                 :         900 :                 *op->resvalue = heap_copy_tuple_as_datum(&tmptup, outdesc);
    4006                 :             :         }
    4007                 :             : 
    4008                 :         960 :         DecrTupleDescRefCount(indesc);
    4009                 :         960 :         DecrTupleDescRefCount(outdesc);
    4010         [ -  + ]:         967 : }
    4011                 :             : 
    4012                 :             : /*
    4013                 :             :  * Evaluate "scalar op ANY/ALL (array)".
    4014                 :             :  *
    4015                 :             :  * Source array is in our result area, scalar arg is already evaluated into
    4016                 :             :  * fcinfo->args[0].
    4017                 :             :  *
    4018                 :             :  * The operator always yields boolean, and we combine the results across all
    4019                 :             :  * array elements using OR and AND (for ANY and ALL respectively).  Of course
    4020                 :             :  * we short-circuit as soon as the result is known.
    4021                 :             :  */
    4022                 :             : void
    4023                 :      532013 : ExecEvalScalarArrayOp(ExprState *state, ExprEvalStep *op)
    4024                 :             : {
    4025                 :      532013 :         FunctionCallInfo fcinfo = op->d.scalararrayop.fcinfo_data;
    4026                 :      532013 :         bool            useOr = op->d.scalararrayop.useOr;
    4027                 :      532013 :         bool            strictfunc = op->d.scalararrayop.finfo->fn_strict;
    4028                 :      532013 :         ArrayType  *arr;
    4029                 :      532013 :         int                     nitems;
    4030                 :      532013 :         Datum           result;
    4031                 :      532013 :         bool            resultnull;
    4032                 :      532013 :         int16           typlen;
    4033                 :      532013 :         bool            typbyval;
    4034                 :      532013 :         char            typalign;
    4035                 :      532013 :         char       *s;
    4036                 :      532013 :         bits8      *bitmap;
    4037                 :      532013 :         int                     bitmask;
    4038                 :             : 
    4039                 :             :         /*
    4040                 :             :          * If the array is NULL then we return NULL --- it's not very meaningful
    4041                 :             :          * to do anything else, even if the operator isn't strict.
    4042                 :             :          */
    4043         [ +  + ]:      532013 :         if (*op->resnull)
    4044                 :          48 :                 return;
    4045                 :             : 
    4046                 :             :         /* Else okay to fetch and detoast the array */
    4047                 :      531965 :         arr = DatumGetArrayTypeP(*op->resvalue);
    4048                 :             : 
    4049                 :             :         /*
    4050                 :             :          * If the array is empty, we return either FALSE or TRUE per the useOr
    4051                 :             :          * flag.  This is correct even if the scalar is NULL; since we would
    4052                 :             :          * evaluate the operator zero times, it matters not whether it would want
    4053                 :             :          * to return NULL.
    4054                 :             :          */
    4055                 :      531965 :         nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
    4056         [ +  + ]:      531965 :         if (nitems <= 0)
    4057                 :             :         {
    4058                 :        1854 :                 *op->resvalue = BoolGetDatum(!useOr);
    4059                 :        1854 :                 *op->resnull = false;
    4060                 :        1854 :                 return;
    4061                 :             :         }
    4062                 :             : 
    4063                 :             :         /*
    4064                 :             :          * If the scalar is NULL, and the function is strict, return NULL; no
    4065                 :             :          * point in iterating the loop.
    4066                 :             :          */
    4067   [ +  +  +  + ]:      530111 :         if (fcinfo->args[0].isnull && strictfunc)
    4068                 :             :         {
    4069                 :         110 :                 *op->resnull = true;
    4070                 :         110 :                 return;
    4071                 :             :         }
    4072                 :             : 
    4073                 :             :         /*
    4074                 :             :          * We arrange to look up info about the element type only once per series
    4075                 :             :          * of calls, assuming the element type doesn't change underneath us.
    4076                 :             :          */
    4077         [ +  + ]:      530001 :         if (op->d.scalararrayop.element_type != ARR_ELEMTYPE(arr))
    4078                 :             :         {
    4079                 :        2754 :                 get_typlenbyvalalign(ARR_ELEMTYPE(arr),
    4080                 :        1377 :                                                          &op->d.scalararrayop.typlen,
    4081                 :        1377 :                                                          &op->d.scalararrayop.typbyval,
    4082                 :        1377 :                                                          &op->d.scalararrayop.typalign);
    4083                 :        1377 :                 op->d.scalararrayop.element_type = ARR_ELEMTYPE(arr);
    4084                 :        1377 :         }
    4085                 :             : 
    4086                 :      530001 :         typlen = op->d.scalararrayop.typlen;
    4087                 :      530001 :         typbyval = op->d.scalararrayop.typbyval;
    4088                 :      530001 :         typalign = op->d.scalararrayop.typalign;
    4089                 :             : 
    4090                 :             :         /* Initialize result appropriately depending on useOr */
    4091                 :      530001 :         result = BoolGetDatum(!useOr);
    4092                 :      530001 :         resultnull = false;
    4093                 :             : 
    4094                 :             :         /* Loop over the array elements */
    4095         [ +  + ]:      530001 :         s = (char *) ARR_DATA_PTR(arr);
    4096         [ +  + ]:      530001 :         bitmap = ARR_NULLBITMAP(arr);
    4097                 :      530001 :         bitmask = 1;
    4098                 :             : 
    4099         [ +  + ]:     1613430 :         for (int i = 0; i < nitems; i++)
    4100                 :             :         {
    4101                 :     1083429 :                 Datum           elt;
    4102                 :     1083429 :                 Datum           thisresult;
    4103                 :             : 
    4104                 :             :                 /* Get array element, checking for NULL */
    4105   [ +  +  +  + ]:     1083429 :                 if (bitmap && (*bitmap & bitmask) == 0)
    4106                 :             :                 {
    4107                 :       35808 :                         fcinfo->args[1].value = (Datum) 0;
    4108                 :       35808 :                         fcinfo->args[1].isnull = true;
    4109                 :       35808 :                 }
    4110                 :             :                 else
    4111                 :             :                 {
    4112                 :     1047621 :                         elt = fetch_att(s, typbyval, typlen);
    4113   [ +  +  -  +  :     1047621 :                         s = att_addlength_pointer(s, typlen, s);
                   #  # ]
    4114   [ +  +  +  +  :     1047621 :                         s = (char *) att_align_nominal(s, typalign);
             +  +  -  + ]
    4115                 :     1047621 :                         fcinfo->args[1].value = elt;
    4116                 :     1047621 :                         fcinfo->args[1].isnull = false;
    4117                 :             :                 }
    4118                 :             : 
    4119                 :             :                 /* Call comparison function */
    4120   [ +  +  +  + ]:     1083429 :                 if (fcinfo->args[1].isnull && strictfunc)
    4121                 :             :                 {
    4122                 :       35804 :                         fcinfo->isnull = true;
    4123                 :       35804 :                         thisresult = (Datum) 0;
    4124                 :       35804 :                 }
    4125                 :             :                 else
    4126                 :             :                 {
    4127                 :     1047625 :                         fcinfo->isnull = false;
    4128                 :     1047625 :                         thisresult = op->d.scalararrayop.fn_addr(fcinfo);
    4129                 :             :                 }
    4130                 :             : 
    4131                 :             :                 /* Combine results per OR or AND semantics */
    4132         [ +  + ]:     1083429 :                 if (fcinfo->isnull)
    4133                 :       35820 :                         resultnull = true;
    4134         [ +  + ]:     1047609 :                 else if (useOr)
    4135                 :             :                 {
    4136         [ +  + ]:      907928 :                         if (DatumGetBool(thisresult))
    4137                 :             :                         {
    4138                 :      117329 :                                 result = BoolGetDatum(true);
    4139                 :      117329 :                                 resultnull = false;
    4140                 :      117329 :                                 break;                  /* needn't look at any more elements */
    4141                 :             :                         }
    4142                 :      790599 :                 }
    4143                 :             :                 else
    4144                 :             :                 {
    4145         [ +  + ]:      139681 :                         if (!DatumGetBool(thisresult))
    4146                 :             :                         {
    4147                 :      106532 :                                 result = BoolGetDatum(false);
    4148                 :      106532 :                                 resultnull = false;
    4149                 :      106532 :                                 break;                  /* needn't look at any more elements */
    4150                 :             :                         }
    4151                 :             :                 }
    4152                 :             : 
    4153                 :             :                 /* advance bitmap pointer if any */
    4154         [ +  + ]:      859568 :                 if (bitmap)
    4155                 :             :                 {
    4156                 :      127679 :                         bitmask <<= 1;
    4157         [ +  + ]:      127679 :                         if (bitmask == 0x100)
    4158                 :             :                         {
    4159                 :         125 :                                 bitmap++;
    4160                 :         125 :                                 bitmask = 1;
    4161                 :         125 :                         }
    4162                 :      127679 :                 }
    4163         [ +  + ]:     1083429 :         }
    4164                 :             : 
    4165                 :      530001 :         *op->resvalue = result;
    4166                 :      530001 :         *op->resnull = resultnull;
    4167         [ -  + ]:      532013 : }
    4168                 :             : 
    4169                 :             : /*
    4170                 :             :  * Hash function for scalar array hash op elements.
    4171                 :             :  *
    4172                 :             :  * We use the element type's default hash opclass, and the column collation
    4173                 :             :  * if the type is collation-sensitive.
    4174                 :             :  */
    4175                 :             : static uint32
    4176                 :           0 : saop_element_hash(struct saophash_hash *tb, Datum key)
    4177                 :             : {
    4178                 :           0 :         ScalarArrayOpExprHashTable *elements_tab = (ScalarArrayOpExprHashTable *) tb->private_data;
    4179                 :           0 :         FunctionCallInfo fcinfo = &elements_tab->hash_fcinfo_data;
    4180                 :           0 :         Datum           hash;
    4181                 :             : 
    4182                 :           0 :         fcinfo->args[0].value = key;
    4183                 :           0 :         fcinfo->args[0].isnull = false;
    4184                 :             : 
    4185                 :           0 :         hash = elements_tab->hash_finfo.fn_addr(fcinfo);
    4186                 :             : 
    4187                 :           0 :         return DatumGetUInt32(hash);
    4188                 :           0 : }
    4189                 :             : 
    4190                 :             : /*
    4191                 :             :  * Matching function for scalar array hash op elements, to be used in hashtable
    4192                 :             :  * lookups.
    4193                 :             :  */
    4194                 :             : static bool
    4195                 :           0 : saop_hash_element_match(struct saophash_hash *tb, Datum key1, Datum key2)
    4196                 :             : {
    4197                 :           0 :         Datum           result;
    4198                 :             : 
    4199                 :           0 :         ScalarArrayOpExprHashTable *elements_tab = (ScalarArrayOpExprHashTable *) tb->private_data;
    4200                 :           0 :         FunctionCallInfo fcinfo = elements_tab->op->d.hashedscalararrayop.fcinfo_data;
    4201                 :             : 
    4202                 :           0 :         fcinfo->args[0].value = key1;
    4203                 :           0 :         fcinfo->args[0].isnull = false;
    4204                 :           0 :         fcinfo->args[1].value = key2;
    4205                 :           0 :         fcinfo->args[1].isnull = false;
    4206                 :             : 
    4207                 :           0 :         result = elements_tab->op->d.hashedscalararrayop.finfo->fn_addr(fcinfo);
    4208                 :             : 
    4209                 :           0 :         return DatumGetBool(result);
    4210                 :           0 : }
    4211                 :             : 
    4212                 :             : /*
    4213                 :             :  * Evaluate "scalar op ANY (const array)".
    4214                 :             :  *
    4215                 :             :  * Similar to ExecEvalScalarArrayOp, but optimized for faster repeat lookups
    4216                 :             :  * by building a hashtable on the first lookup.  This hashtable will be reused
    4217                 :             :  * by subsequent lookups.  Unlike ExecEvalScalarArrayOp, this version only
    4218                 :             :  * supports OR semantics.
    4219                 :             :  *
    4220                 :             :  * Source array is in our result area, scalar arg is already evaluated into
    4221                 :             :  * fcinfo->args[0].
    4222                 :             :  *
    4223                 :             :  * The operator always yields boolean.
    4224                 :             :  */
    4225                 :             : void
    4226                 :         572 : ExecEvalHashedScalarArrayOp(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    4227                 :             : {
    4228                 :         572 :         ScalarArrayOpExprHashTable *elements_tab = op->d.hashedscalararrayop.elements_tab;
    4229                 :         572 :         FunctionCallInfo fcinfo = op->d.hashedscalararrayop.fcinfo_data;
    4230                 :         572 :         bool            inclause = op->d.hashedscalararrayop.inclause;
    4231                 :         572 :         bool            strictfunc = op->d.hashedscalararrayop.finfo->fn_strict;
    4232                 :         572 :         Datum           scalar = fcinfo->args[0].value;
    4233                 :         572 :         bool            scalar_isnull = fcinfo->args[0].isnull;
    4234                 :         572 :         Datum           result;
    4235                 :         572 :         bool            resultnull;
    4236                 :         572 :         bool            hashfound;
    4237                 :             : 
    4238                 :             :         /* We don't setup a hashed scalar array op if the array const is null. */
    4239         [ +  - ]:         572 :         Assert(!*op->resnull);
    4240                 :             : 
    4241                 :             :         /*
    4242                 :             :          * If the scalar is NULL, and the function is strict, return NULL; no
    4243                 :             :          * point in executing the search.
    4244                 :             :          */
    4245   [ +  +  +  + ]:         572 :         if (fcinfo->args[0].isnull && strictfunc)
    4246                 :             :         {
    4247                 :           4 :                 *op->resnull = true;
    4248                 :           4 :                 return;
    4249                 :             :         }
    4250                 :             : 
    4251                 :             :         /* Build the hash table on first evaluation */
    4252         [ +  + ]:         568 :         if (elements_tab == NULL)
    4253                 :             :         {
    4254                 :          15 :                 ScalarArrayOpExpr *saop;
    4255                 :          15 :                 int16           typlen;
    4256                 :          15 :                 bool            typbyval;
    4257                 :          15 :                 char            typalign;
    4258                 :          15 :                 int                     nitems;
    4259                 :          15 :                 bool            has_nulls = false;
    4260                 :          15 :                 char       *s;
    4261                 :          15 :                 bits8      *bitmap;
    4262                 :          15 :                 int                     bitmask;
    4263                 :          15 :                 MemoryContext oldcontext;
    4264                 :          15 :                 ArrayType  *arr;
    4265                 :             : 
    4266                 :          15 :                 saop = op->d.hashedscalararrayop.saop;
    4267                 :             : 
    4268                 :          15 :                 arr = DatumGetArrayTypeP(*op->resvalue);
    4269                 :          15 :                 nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
    4270                 :             : 
    4271                 :          15 :                 get_typlenbyvalalign(ARR_ELEMTYPE(arr),
    4272                 :             :                                                          &typlen,
    4273                 :             :                                                          &typbyval,
    4274                 :             :                                                          &typalign);
    4275                 :             : 
    4276                 :          15 :                 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
    4277                 :             : 
    4278                 :          15 :                 elements_tab = (ScalarArrayOpExprHashTable *)
    4279                 :          15 :                         palloc0(offsetof(ScalarArrayOpExprHashTable, hash_fcinfo_data) +
    4280                 :             :                                         SizeForFunctionCallInfo(1));
    4281                 :          15 :                 op->d.hashedscalararrayop.elements_tab = elements_tab;
    4282                 :          15 :                 elements_tab->op = op;
    4283                 :             : 
    4284                 :          15 :                 fmgr_info(saop->hashfuncid, &elements_tab->hash_finfo);
    4285                 :          15 :                 fmgr_info_set_expr((Node *) saop, &elements_tab->hash_finfo);
    4286                 :             : 
    4287                 :          15 :                 InitFunctionCallInfoData(elements_tab->hash_fcinfo_data,
    4288                 :             :                                                                  &elements_tab->hash_finfo,
    4289                 :             :                                                                  1,
    4290                 :             :                                                                  saop->inputcollid,
    4291                 :             :                                                                  NULL,
    4292                 :             :                                                                  NULL);
    4293                 :             : 
    4294                 :             :                 /*
    4295                 :             :                  * Create the hash table sizing it according to the number of elements
    4296                 :             :                  * in the array.  This does assume that the array has no duplicates.
    4297                 :             :                  * If the array happens to contain many duplicate values then it'll
    4298                 :             :                  * just mean that we sized the table a bit on the large side.
    4299                 :             :                  */
    4300                 :          30 :                 elements_tab->hashtab = saophash_create(CurrentMemoryContext, nitems,
    4301                 :          15 :                                                                                                 elements_tab);
    4302                 :             : 
    4303                 :          15 :                 MemoryContextSwitchTo(oldcontext);
    4304                 :             : 
    4305         [ +  + ]:          15 :                 s = (char *) ARR_DATA_PTR(arr);
    4306         [ +  + ]:          15 :                 bitmap = ARR_NULLBITMAP(arr);
    4307                 :          15 :                 bitmask = 1;
    4308         [ +  + ]:         170 :                 for (int i = 0; i < nitems; i++)
    4309                 :             :                 {
    4310                 :             :                         /* Get array element, checking for NULL. */
    4311   [ +  +  +  + ]:         155 :                         if (bitmap && (*bitmap & bitmask) == 0)
    4312                 :             :                         {
    4313                 :          29 :                                 has_nulls = true;
    4314                 :          29 :                         }
    4315                 :             :                         else
    4316                 :             :                         {
    4317                 :         126 :                                 Datum           element;
    4318                 :             : 
    4319                 :         126 :                                 element = fetch_att(s, typbyval, typlen);
    4320   [ +  +  -  +  :         126 :                                 s = att_addlength_pointer(s, typlen, s);
                   #  # ]
    4321   [ +  +  +  -  :         126 :                                 s = (char *) att_align_nominal(s, typalign);
             #  #  #  # ]
    4322                 :             : 
    4323                 :         126 :                                 saophash_insert(elements_tab->hashtab, element, &hashfound);
    4324                 :         126 :                         }
    4325                 :             : 
    4326                 :             :                         /* Advance bitmap pointer if any. */
    4327         [ +  + ]:         155 :                         if (bitmap)
    4328                 :             :                         {
    4329                 :          95 :                                 bitmask <<= 1;
    4330         [ +  + ]:          95 :                                 if (bitmask == 0x100)
    4331                 :             :                                 {
    4332                 :           9 :                                         bitmap++;
    4333                 :           9 :                                         bitmask = 1;
    4334                 :           9 :                                 }
    4335                 :          95 :                         }
    4336                 :         155 :                 }
    4337                 :             : 
    4338                 :             :                 /*
    4339                 :             :                  * Remember if we had any nulls so that we know if we need to execute
    4340                 :             :                  * non-strict functions with a null lhs value if no match is found.
    4341                 :             :                  */
    4342                 :          15 :                 op->d.hashedscalararrayop.has_nulls = has_nulls;
    4343                 :          15 :         }
    4344                 :             : 
    4345                 :             :         /* Check the hash to see if we have a match. */
    4346                 :         568 :         hashfound = NULL != saophash_lookup(elements_tab->hashtab, scalar);
    4347                 :             : 
    4348                 :             :         /* the result depends on if the clause is an IN or NOT IN clause */
    4349         [ +  + ]:         568 :         if (inclause)
    4350                 :           7 :                 result = BoolGetDatum(hashfound);       /* IN */
    4351                 :             :         else
    4352                 :         561 :                 result = BoolGetDatum(!hashfound);      /* NOT IN */
    4353                 :             : 
    4354                 :         568 :         resultnull = false;
    4355                 :             : 
    4356                 :             :         /*
    4357                 :             :          * If we didn't find a match in the array, we still might need to handle
    4358                 :             :          * the possibility of null values.  We didn't put any NULLs into the
    4359                 :             :          * hashtable, but instead marked if we found any when building the table
    4360                 :             :          * in has_nulls.
    4361                 :             :          */
    4362   [ +  +  +  + ]:         568 :         if (!hashfound && op->d.hashedscalararrayop.has_nulls)
    4363                 :             :         {
    4364         [ +  + ]:           7 :                 if (strictfunc)
    4365                 :             :                 {
    4366                 :             : 
    4367                 :             :                         /*
    4368                 :             :                          * We have nulls in the array so a non-null lhs and no match must
    4369                 :             :                          * yield NULL.
    4370                 :             :                          */
    4371                 :           4 :                         result = (Datum) 0;
    4372                 :           4 :                         resultnull = true;
    4373                 :           4 :                 }
    4374                 :             :                 else
    4375                 :             :                 {
    4376                 :             :                         /*
    4377                 :             :                          * Execute function will null rhs just once.
    4378                 :             :                          *
    4379                 :             :                          * The hash lookup path will have scribbled on the lhs argument so
    4380                 :             :                          * we need to set it up also (even though we entered this function
    4381                 :             :                          * with it already set).
    4382                 :             :                          */
    4383                 :           3 :                         fcinfo->args[0].value = scalar;
    4384                 :           3 :                         fcinfo->args[0].isnull = scalar_isnull;
    4385                 :           3 :                         fcinfo->args[1].value = (Datum) 0;
    4386                 :           3 :                         fcinfo->args[1].isnull = true;
    4387                 :             : 
    4388                 :           3 :                         result = op->d.hashedscalararrayop.finfo->fn_addr(fcinfo);
    4389                 :           3 :                         resultnull = fcinfo->isnull;
    4390                 :             : 
    4391                 :             :                         /*
    4392                 :             :                          * Reverse the result for NOT IN clauses since the above function
    4393                 :             :                          * is the equality function and we need not-equals.
    4394                 :             :                          */
    4395         [ +  + ]:           3 :                         if (!inclause)
    4396                 :           2 :                                 result = BoolGetDatum(!DatumGetBool(result));
    4397                 :             :                 }
    4398                 :           7 :         }
    4399                 :             : 
    4400                 :         568 :         *op->resvalue = result;
    4401                 :         568 :         *op->resnull = resultnull;
    4402         [ -  + ]:         572 : }
    4403                 :             : 
    4404                 :             : /*
    4405                 :             :  * Evaluate a NOT NULL domain constraint.
    4406                 :             :  */
    4407                 :             : void
    4408                 :          55 : ExecEvalConstraintNotNull(ExprState *state, ExprEvalStep *op)
    4409                 :             : {
    4410         [ +  + ]:          55 :         if (*op->resnull)
    4411         [ +  + ]:          28 :                 errsave((Node *) op->d.domaincheck.escontext,
    4412                 :             :                                 (errcode(ERRCODE_NOT_NULL_VIOLATION),
    4413                 :             :                                  errmsg("domain %s does not allow null values",
    4414                 :             :                                                 format_type_be(op->d.domaincheck.resulttype)),
    4415                 :             :                                  errdatatype(op->d.domaincheck.resulttype)));
    4416                 :          27 : }
    4417                 :             : 
    4418                 :             : /*
    4419                 :             :  * Evaluate a CHECK domain constraint.
    4420                 :             :  */
    4421                 :             : void
    4422                 :         914 : ExecEvalConstraintCheck(ExprState *state, ExprEvalStep *op)
    4423                 :             : {
    4424   [ +  +  +  + ]:         914 :         if (!*op->d.domaincheck.checknull &&
    4425                 :         871 :                 !DatumGetBool(*op->d.domaincheck.checkvalue))
    4426         [ +  + ]:         131 :                 errsave((Node *) op->d.domaincheck.escontext,
    4427                 :             :                                 (errcode(ERRCODE_CHECK_VIOLATION),
    4428                 :             :                                  errmsg("value for domain %s violates check constraint \"%s\"",
    4429                 :             :                                                 format_type_be(op->d.domaincheck.resulttype),
    4430                 :             :                                                 op->d.domaincheck.constraintname),
    4431                 :             :                                  errdomainconstraint(op->d.domaincheck.resulttype,
    4432                 :             :                                                                          op->d.domaincheck.constraintname)));
    4433                 :         788 : }
    4434                 :             : 
    4435                 :             : /*
    4436                 :             :  * Evaluate the various forms of XmlExpr.
    4437                 :             :  *
    4438                 :             :  * Arguments have been evaluated into named_argvalue/named_argnull
    4439                 :             :  * and/or argvalue/argnull arrays.
    4440                 :             :  */
    4441                 :             : void
    4442                 :        7627 : ExecEvalXmlExpr(ExprState *state, ExprEvalStep *op)
    4443                 :             : {
    4444                 :        7627 :         XmlExpr    *xexpr = op->d.xmlexpr.xexpr;
    4445                 :        7627 :         Datum           value;
    4446                 :             : 
    4447                 :        7627 :         *op->resnull = true;         /* until we get a result */
    4448                 :        7627 :         *op->resvalue = (Datum) 0;
    4449                 :             : 
    4450   [ +  +  +  +  :        7627 :         switch (xexpr->op)
             +  +  +  +  
                      - ]
    4451                 :             :         {
    4452                 :             :                 case IS_XMLCONCAT:
    4453                 :             :                         {
    4454                 :           9 :                                 Datum      *argvalue = op->d.xmlexpr.argvalue;
    4455                 :           9 :                                 bool       *argnull = op->d.xmlexpr.argnull;
    4456                 :           9 :                                 List       *values = NIL;
    4457                 :             : 
    4458         [ +  + ]:          29 :                                 for (int i = 0; i < list_length(xexpr->args); i++)
    4459                 :             :                                 {
    4460         [ +  + ]:          20 :                                         if (!argnull[i])
    4461                 :          15 :                                                 values = lappend(values, DatumGetPointer(argvalue[i]));
    4462                 :          20 :                                 }
    4463                 :             : 
    4464         [ +  + ]:           9 :                                 if (values != NIL)
    4465                 :             :                                 {
    4466                 :           7 :                                         *op->resvalue = PointerGetDatum(xmlconcat(values));
    4467                 :           7 :                                         *op->resnull = false;
    4468                 :           7 :                                 }
    4469                 :           9 :                         }
    4470                 :           9 :                         break;
    4471                 :             : 
    4472                 :             :                 case IS_XMLFOREST:
    4473                 :             :                         {
    4474                 :        3756 :                                 Datum      *argvalue = op->d.xmlexpr.named_argvalue;
    4475                 :        3756 :                                 bool       *argnull = op->d.xmlexpr.named_argnull;
    4476                 :        3756 :                                 StringInfoData buf;
    4477                 :        3756 :                                 ListCell   *lc;
    4478                 :        3756 :                                 ListCell   *lc2;
    4479                 :        3756 :                                 int                     i;
    4480                 :             : 
    4481                 :        3756 :                                 initStringInfo(&buf);
    4482                 :             : 
    4483                 :        3756 :                                 i = 0;
    4484   [ +  -  +  +  :       26272 :                                 forboth(lc, xexpr->named_args, lc2, xexpr->arg_names)
          +  -  +  +  +  
                +  +  + ]
    4485                 :             :                                 {
    4486                 :       22516 :                                         Expr       *e = (Expr *) lfirst(lc);
    4487                 :       22516 :                                         char       *argname = strVal(lfirst(lc2));
    4488                 :             : 
    4489         [ +  + ]:       22516 :                                         if (!argnull[i])
    4490                 :             :                                         {
    4491                 :       18804 :                                                 value = argvalue[i];
    4492                 :       18804 :                                                 appendStringInfo(&buf, "<%s>%s</%s>",
    4493                 :       18804 :                                                                                  argname,
    4494                 :       37608 :                                                                                  map_sql_value_to_xml_value(value,
    4495                 :       18804 :                                                                                                                                         exprType((Node *) e), true),
    4496                 :       18804 :                                                                                  argname);
    4497                 :       18804 :                                                 *op->resnull = false;
    4498                 :       18804 :                                         }
    4499                 :       22516 :                                         i++;
    4500                 :       22516 :                                 }
    4501                 :             : 
    4502         [ +  - ]:        3756 :                                 if (!*op->resnull)
    4503                 :             :                                 {
    4504                 :        3756 :                                         text       *result;
    4505                 :             : 
    4506                 :        3756 :                                         result = cstring_to_text_with_len(buf.data, buf.len);
    4507                 :        3756 :                                         *op->resvalue = PointerGetDatum(result);
    4508                 :        3756 :                                 }
    4509                 :             : 
    4510                 :        3756 :                                 pfree(buf.data);
    4511                 :        3756 :                         }
    4512                 :        3756 :                         break;
    4513                 :             : 
    4514                 :             :                 case IS_XMLELEMENT:
    4515                 :        7564 :                         *op->resvalue = PointerGetDatum(xmlelement(xexpr,
    4516                 :        3782 :                                                                                                            op->d.xmlexpr.named_argvalue,
    4517                 :        3782 :                                                                                                            op->d.xmlexpr.named_argnull,
    4518                 :        3782 :                                                                                                            op->d.xmlexpr.argvalue,
    4519                 :        3782 :                                                                                                            op->d.xmlexpr.argnull));
    4520                 :        3782 :                         *op->resnull = false;
    4521                 :        3782 :                         break;
    4522                 :             : 
    4523                 :             :                 case IS_XMLPARSE:
    4524                 :             :                         {
    4525                 :          22 :                                 Datum      *argvalue = op->d.xmlexpr.argvalue;
    4526                 :          22 :                                 bool       *argnull = op->d.xmlexpr.argnull;
    4527                 :          22 :                                 text       *data;
    4528                 :          22 :                                 bool            preserve_whitespace;
    4529                 :             : 
    4530                 :             :                                 /* arguments are known to be text, bool */
    4531         [ +  - ]:          22 :                                 Assert(list_length(xexpr->args) == 2);
    4532                 :             : 
    4533         [ -  + ]:          22 :                                 if (argnull[0])
    4534                 :           0 :                                         return;
    4535                 :          22 :                                 value = argvalue[0];
    4536                 :          22 :                                 data = DatumGetTextPP(value);
    4537                 :             : 
    4538         [ -  + ]:          22 :                                 if (argnull[1]) /* probably can't happen */
    4539                 :           0 :                                         return;
    4540                 :          22 :                                 value = argvalue[1];
    4541                 :          22 :                                 preserve_whitespace = DatumGetBool(value);
    4542                 :             : 
    4543                 :          44 :                                 *op->resvalue = PointerGetDatum(xmlparse(data,
    4544                 :          22 :                                                                                                                  xexpr->xmloption,
    4545                 :          22 :                                                                                                                  preserve_whitespace));
    4546                 :          22 :                                 *op->resnull = false;
    4547         [ +  + ]:          22 :                         }
    4548                 :          14 :                         break;
    4549                 :             : 
    4550                 :             :                 case IS_XMLPI:
    4551                 :             :                         {
    4552                 :          12 :                                 text       *arg;
    4553                 :          12 :                                 bool            isnull;
    4554                 :             : 
    4555                 :             :                                 /* optional argument is known to be text */
    4556         [ +  - ]:          12 :                                 Assert(list_length(xexpr->args) <= 1);
    4557                 :             : 
    4558         [ +  + ]:          12 :                                 if (xexpr->args)
    4559                 :             :                                 {
    4560                 :           7 :                                         isnull = op->d.xmlexpr.argnull[0];
    4561         [ +  + ]:           7 :                                         if (isnull)
    4562                 :           3 :                                                 arg = NULL;
    4563                 :             :                                         else
    4564                 :           4 :                                                 arg = DatumGetTextPP(op->d.xmlexpr.argvalue[0]);
    4565                 :           7 :                                 }
    4566                 :             :                                 else
    4567                 :             :                                 {
    4568                 :           5 :                                         arg = NULL;
    4569                 :           5 :                                         isnull = false;
    4570                 :             :                                 }
    4571                 :             : 
    4572                 :          24 :                                 *op->resvalue = PointerGetDatum(xmlpi(xexpr->name,
    4573                 :          12 :                                                                                                           arg,
    4574                 :          12 :                                                                                                           isnull,
    4575                 :          12 :                                                                                                           op->resnull));
    4576                 :          12 :                         }
    4577                 :          12 :                         break;
    4578                 :             : 
    4579                 :             :                 case IS_XMLROOT:
    4580                 :             :                         {
    4581                 :          10 :                                 Datum      *argvalue = op->d.xmlexpr.argvalue;
    4582                 :          10 :                                 bool       *argnull = op->d.xmlexpr.argnull;
    4583                 :          10 :                                 xmltype    *data;
    4584                 :          10 :                                 text       *version;
    4585                 :          10 :                                 int                     standalone;
    4586                 :             : 
    4587                 :             :                                 /* arguments are known to be xml, text, int */
    4588         [ +  - ]:          10 :                                 Assert(list_length(xexpr->args) == 3);
    4589                 :             : 
    4590         [ -  + ]:          10 :                                 if (argnull[0])
    4591                 :           0 :                                         return;
    4592                 :          10 :                                 data = DatumGetXmlP(argvalue[0]);
    4593                 :             : 
    4594         [ +  + ]:          10 :                                 if (argnull[1])
    4595                 :           6 :                                         version = NULL;
    4596                 :             :                                 else
    4597                 :           4 :                                         version = DatumGetTextPP(argvalue[1]);
    4598                 :             : 
    4599         [ +  - ]:          10 :                                 Assert(!argnull[2]);    /* always present */
    4600                 :          10 :                                 standalone = DatumGetInt32(argvalue[2]);
    4601                 :             : 
    4602                 :          20 :                                 *op->resvalue = PointerGetDatum(xmlroot(data,
    4603                 :          10 :                                                                                                                 version,
    4604                 :          10 :                                                                                                                 standalone));
    4605                 :          10 :                                 *op->resnull = false;
    4606         [ -  + ]:          10 :                         }
    4607                 :          10 :                         break;
    4608                 :             : 
    4609                 :             :                 case IS_XMLSERIALIZE:
    4610                 :             :                         {
    4611                 :          32 :                                 Datum      *argvalue = op->d.xmlexpr.argvalue;
    4612                 :          32 :                                 bool       *argnull = op->d.xmlexpr.argnull;
    4613                 :             : 
    4614                 :             :                                 /* argument type is known to be xml */
    4615         [ +  - ]:          32 :                                 Assert(list_length(xexpr->args) == 1);
    4616                 :             : 
    4617         [ +  + ]:          32 :                                 if (argnull[0])
    4618                 :           2 :                                         return;
    4619                 :          30 :                                 value = argvalue[0];
    4620                 :             : 
    4621                 :          30 :                                 *op->resvalue =
    4622                 :          60 :                                         PointerGetDatum(xmltotext_with_options(DatumGetXmlP(value),
    4623                 :          30 :                                                                                                                    xexpr->xmloption,
    4624                 :          30 :                                                                                                                    xexpr->indent));
    4625                 :          30 :                                 *op->resnull = false;
    4626         [ +  + ]:          32 :                         }
    4627                 :          25 :                         break;
    4628                 :             : 
    4629                 :             :                 case IS_DOCUMENT:
    4630                 :             :                         {
    4631                 :           4 :                                 Datum      *argvalue = op->d.xmlexpr.argvalue;
    4632                 :           4 :                                 bool       *argnull = op->d.xmlexpr.argnull;
    4633                 :             : 
    4634                 :             :                                 /* optional argument is known to be xml */
    4635         [ +  - ]:           4 :                                 Assert(list_length(xexpr->args) == 1);
    4636                 :             : 
    4637         [ -  + ]:           4 :                                 if (argnull[0])
    4638                 :           0 :                                         return;
    4639                 :           4 :                                 value = argvalue[0];
    4640                 :             : 
    4641                 :           4 :                                 *op->resvalue =
    4642                 :           4 :                                         BoolGetDatum(xml_is_document(DatumGetXmlP(value)));
    4643                 :           4 :                                 *op->resnull = false;
    4644         [ -  + ]:           4 :                         }
    4645                 :           4 :                         break;
    4646                 :             : 
    4647                 :             :                 default:
    4648   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized XML operation");
    4649                 :           0 :                         break;
    4650                 :             :         }
    4651         [ -  + ]:        7627 : }
    4652                 :             : 
    4653                 :             : /*
    4654                 :             :  * Evaluate a JSON constructor expression.
    4655                 :             :  */
    4656                 :             : void
    4657                 :         107 : ExecEvalJsonConstructor(ExprState *state, ExprEvalStep *op,
    4658                 :             :                                                 ExprContext *econtext)
    4659                 :             : {
    4660                 :         107 :         Datum           res;
    4661                 :         107 :         JsonConstructorExprState *jcstate = op->d.json_constructor.jcstate;
    4662                 :         107 :         JsonConstructorExpr *ctor = jcstate->constructor;
    4663                 :         107 :         bool            is_jsonb = ctor->returning->format->format_type == JS_FORMAT_JSONB;
    4664                 :         107 :         bool            isnull = false;
    4665                 :             : 
    4666         [ +  + ]:         107 :         if (ctor->type == JSCTOR_JSON_ARRAY)
    4667                 :          70 :                 res = (is_jsonb ?
    4668                 :             :                            jsonb_build_array_worker :
    4669                 :          35 :                            json_build_array_worker) (jcstate->nargs,
    4670                 :          35 :                                                                                  jcstate->arg_values,
    4671                 :          35 :                                                                                  jcstate->arg_nulls,
    4672                 :          35 :                                                                                  jcstate->arg_types,
    4673                 :          35 :                                                                                  jcstate->constructor->absent_on_null);
    4674         [ +  + ]:          72 :         else if (ctor->type == JSCTOR_JSON_OBJECT)
    4675                 :         118 :                 res = (is_jsonb ?
    4676                 :             :                            jsonb_build_object_worker :
    4677                 :          59 :                            json_build_object_worker) (jcstate->nargs,
    4678                 :          59 :                                                                                   jcstate->arg_values,
    4679                 :          59 :                                                                                   jcstate->arg_nulls,
    4680                 :          59 :                                                                                   jcstate->arg_types,
    4681                 :          59 :                                                                                   jcstate->constructor->absent_on_null,
    4682                 :          59 :                                                                                   jcstate->constructor->unique);
    4683         [ +  + ]:          13 :         else if (ctor->type == JSCTOR_JSON_SCALAR)
    4684                 :             :         {
    4685         [ +  + ]:          12 :                 if (jcstate->arg_nulls[0])
    4686                 :             :                 {
    4687                 :           2 :                         res = (Datum) 0;
    4688                 :           2 :                         isnull = true;
    4689                 :           2 :                 }
    4690                 :             :                 else
    4691                 :             :                 {
    4692                 :          10 :                         Datum           value = jcstate->arg_values[0];
    4693                 :          10 :                         Oid                     outfuncid = jcstate->arg_type_cache[0].outfuncid;
    4694                 :          20 :                         JsonTypeCategory category = (JsonTypeCategory)
    4695                 :          10 :                                 jcstate->arg_type_cache[0].category;
    4696                 :             : 
    4697         [ -  + ]:          10 :                         if (is_jsonb)
    4698                 :           0 :                                 res = datum_to_jsonb(value, category, outfuncid);
    4699                 :             :                         else
    4700                 :          10 :                                 res = datum_to_json(value, category, outfuncid);
    4701                 :          10 :                 }
    4702                 :          12 :         }
    4703         [ +  - ]:           1 :         else if (ctor->type == JSCTOR_JSON_PARSE)
    4704                 :             :         {
    4705         [ -  + ]:           1 :                 if (jcstate->arg_nulls[0])
    4706                 :             :                 {
    4707                 :           0 :                         res = (Datum) 0;
    4708                 :           0 :                         isnull = true;
    4709                 :           0 :                 }
    4710                 :             :                 else
    4711                 :             :                 {
    4712                 :           1 :                         Datum           value = jcstate->arg_values[0];
    4713                 :           1 :                         text       *js = DatumGetTextP(value);
    4714                 :             : 
    4715         [ -  + ]:           1 :                         if (is_jsonb)
    4716                 :           0 :                                 res = jsonb_from_text(js, true);
    4717                 :             :                         else
    4718                 :             :                         {
    4719                 :           1 :                                 (void) json_validate(js, true, true);
    4720                 :           1 :                                 res = value;
    4721                 :             :                         }
    4722                 :           1 :                 }
    4723                 :           1 :         }
    4724                 :             :         else
    4725   [ #  #  #  # ]:           0 :                 elog(ERROR, "invalid JsonConstructorExpr type %d", ctor->type);
    4726                 :             : 
    4727                 :         107 :         *op->resvalue = res;
    4728                 :         107 :         *op->resnull = isnull;
    4729                 :         107 : }
    4730                 :             : 
    4731                 :             : /*
    4732                 :             :  * Evaluate a IS JSON predicate.
    4733                 :             :  */
    4734                 :             : void
    4735                 :         453 : ExecEvalJsonIsPredicate(ExprState *state, ExprEvalStep *op)
    4736                 :             : {
    4737                 :         453 :         JsonIsPredicate *pred = op->d.is_json.pred;
    4738                 :         453 :         Datum           js = *op->resvalue;
    4739                 :         453 :         Oid                     exprtype;
    4740                 :         453 :         bool            res;
    4741                 :             : 
    4742         [ +  + ]:         453 :         if (*op->resnull)
    4743                 :             :         {
    4744                 :          17 :                 *op->resvalue = BoolGetDatum(false);
    4745                 :          17 :                 return;
    4746                 :             :         }
    4747                 :             : 
    4748                 :         436 :         exprtype = exprType(pred->expr);
    4749                 :             : 
    4750   [ +  +  +  + ]:         436 :         if (exprtype == TEXTOID || exprtype == JSONOID)
    4751                 :             :         {
    4752                 :         348 :                 text       *json = DatumGetTextP(js);
    4753                 :             : 
    4754         [ +  + ]:         348 :                 if (pred->item_type == JS_TYPE_ANY)
    4755                 :         237 :                         res = true;
    4756                 :             :                 else
    4757                 :             :                 {
    4758   [ +  +  +  + ]:         111 :                         switch (json_get_first_token(json, false))
    4759                 :             :                         {
    4760                 :             :                                 case JSON_TOKEN_OBJECT_START:
    4761                 :          48 :                                         res = pred->item_type == JS_TYPE_OBJECT;
    4762                 :          48 :                                         break;
    4763                 :             :                                 case JSON_TOKEN_ARRAY_START:
    4764                 :          21 :                                         res = pred->item_type == JS_TYPE_ARRAY;
    4765                 :          21 :                                         break;
    4766                 :             :                                 case JSON_TOKEN_STRING:
    4767                 :             :                                 case JSON_TOKEN_NUMBER:
    4768                 :             :                                 case JSON_TOKEN_TRUE:
    4769                 :             :                                 case JSON_TOKEN_FALSE:
    4770                 :             :                                 case JSON_TOKEN_NULL:
    4771                 :          36 :                                         res = pred->item_type == JS_TYPE_SCALAR;
    4772                 :          36 :                                         break;
    4773                 :             :                                 default:
    4774                 :           6 :                                         res = false;
    4775                 :           6 :                                         break;
    4776                 :             :                         }
    4777                 :             :                 }
    4778                 :             : 
    4779                 :             :                 /*
    4780                 :             :                  * Do full parsing pass only for uniqueness check or for JSON text
    4781                 :             :                  * validation.
    4782                 :             :                  */
    4783   [ +  +  +  +  :         348 :                 if (res && (pred->unique_keys || exprtype == TEXTOID))
                   +  + ]
    4784                 :         217 :                         res = json_validate(json, pred->unique_keys, false);
    4785                 :         348 :         }
    4786         [ +  - ]:          88 :         else if (exprtype == JSONBOID)
    4787                 :             :         {
    4788         [ +  + ]:          88 :                 if (pred->item_type == JS_TYPE_ANY)
    4789                 :          55 :                         res = true;
    4790                 :             :                 else
    4791                 :             :                 {
    4792                 :          33 :                         Jsonb      *jb = DatumGetJsonbP(js);
    4793                 :             : 
    4794   [ +  -  +  + ]:          33 :                         switch (pred->item_type)
    4795                 :             :                         {
    4796                 :             :                                 case JS_TYPE_OBJECT:
    4797                 :          11 :                                         res = JB_ROOT_IS_OBJECT(jb);
    4798                 :          11 :                                         break;
    4799                 :             :                                 case JS_TYPE_ARRAY:
    4800         [ +  + ]:          11 :                                         res = JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb);
    4801                 :          11 :                                         break;
    4802                 :             :                                 case JS_TYPE_SCALAR:
    4803         [ +  + ]:          11 :                                         res = JB_ROOT_IS_ARRAY(jb) && JB_ROOT_IS_SCALAR(jb);
    4804                 :          11 :                                         break;
    4805                 :             :                                 default:
    4806                 :           0 :                                         res = false;
    4807                 :           0 :                                         break;
    4808                 :             :                         }
    4809                 :          33 :                 }
    4810                 :             : 
    4811                 :             :                 /* Key uniqueness check is redundant for jsonb */
    4812                 :          88 :         }
    4813                 :             :         else
    4814                 :           0 :                 res = false;
    4815                 :             : 
    4816                 :         436 :         *op->resvalue = BoolGetDatum(res);
    4817         [ -  + ]:         453 : }
    4818                 :             : 
    4819                 :             : /*
    4820                 :             :  * Evaluate a jsonpath against a document, both of which must have been
    4821                 :             :  * evaluated and their values saved in op->d.jsonexpr.jsestate.
    4822                 :             :  *
    4823                 :             :  * If an error occurs during JsonPath* evaluation or when coercing its result
    4824                 :             :  * to the RETURNING type, JsonExprState.error is set to true, provided the
    4825                 :             :  * ON ERROR behavior is not ERROR.  Similarly, if JsonPath{Query|Value}() found
    4826                 :             :  * no matching items, JsonExprState.empty is set to true, provided the ON EMPTY
    4827                 :             :  * behavior is not ERROR.  That is to signal to the subsequent steps that check
    4828                 :             :  * those flags to return the ON ERROR / ON EMPTY expression.
    4829                 :             :  *
    4830                 :             :  * Return value is the step address to be performed next.  It will be one of
    4831                 :             :  * jump_error, jump_empty, jump_eval_coercion, or jump_end, all given in
    4832                 :             :  * op->d.jsonexpr.jsestate.
    4833                 :             :  */
    4834                 :             : int
    4835                 :         862 : ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
    4836                 :             :                                          ExprContext *econtext)
    4837                 :             : {
    4838                 :         862 :         JsonExprState *jsestate = op->d.jsonexpr.jsestate;
    4839                 :         862 :         JsonExpr   *jsexpr = jsestate->jsexpr;
    4840                 :         862 :         Datum           item;
    4841                 :         862 :         JsonPath   *path;
    4842                 :         862 :         bool            throw_error = jsexpr->on_error->btype == JSON_BEHAVIOR_ERROR;
    4843                 :         862 :         bool            error = false,
    4844                 :         862 :                                 empty = false;
    4845                 :         862 :         int                     jump_eval_coercion = jsestate->jump_eval_coercion;
    4846                 :         862 :         char       *val_string = NULL;
    4847                 :             : 
    4848                 :         862 :         item = jsestate->formatted_expr.value;
    4849                 :         862 :         path = DatumGetJsonPathP(jsestate->pathspec.value);
    4850                 :             : 
    4851                 :             :         /* Set error/empty to false. */
    4852                 :         862 :         memset(&jsestate->error, 0, sizeof(NullableDatum));
    4853                 :         862 :         memset(&jsestate->empty, 0, sizeof(NullableDatum));
    4854                 :             : 
    4855                 :             :         /* Also reset ErrorSaveContext contents for the next row. */
    4856         [ +  + ]:         862 :         if (jsestate->escontext.details_wanted)
    4857                 :             :         {
    4858                 :         152 :                 jsestate->escontext.error_data = NULL;
    4859                 :         152 :                 jsestate->escontext.details_wanted = false;
    4860                 :         152 :         }
    4861                 :         862 :         jsestate->escontext.error_occurred = false;
    4862                 :             : 
    4863   [ +  +  +  - ]:         862 :         switch (jsexpr->op)
    4864                 :             :         {
    4865                 :             :                 case JSON_EXISTS_OP:
    4866                 :             :                         {
    4867                 :         194 :                                 bool            exists = JsonPathExists(item, path,
    4868         [ +  + ]:          97 :                                                                                                         !throw_error ? &error : NULL,
    4869                 :          97 :                                                                                                         jsestate->args);
    4870                 :             : 
    4871         [ +  + ]:          97 :                                 if (!error)
    4872                 :             :                                 {
    4873                 :          70 :                                         *op->resnull = false;
    4874                 :          70 :                                         *op->resvalue = BoolGetDatum(exists);
    4875                 :          70 :                                 }
    4876                 :          97 :                         }
    4877                 :          97 :                         break;
    4878                 :             : 
    4879                 :             :                 case JSON_QUERY_OP:
    4880                 :         778 :                         *op->resvalue = JsonPathQuery(item, path, jsexpr->wrapper, &empty,
    4881         [ +  + ]:         389 :                                                                                   !throw_error ? &error : NULL,
    4882                 :         389 :                                                                                   jsestate->args,
    4883                 :         389 :                                                                                   jsexpr->column_name);
    4884                 :             : 
    4885                 :         389 :                         *op->resnull = (DatumGetPointer(*op->resvalue) == NULL);
    4886                 :         389 :                         break;
    4887                 :             : 
    4888                 :             :                 case JSON_VALUE_OP:
    4889                 :             :                         {
    4890                 :         752 :                                 JsonbValue *jbv = JsonPathValue(item, path, &empty,
    4891         [ +  + ]:         376 :                                                                                                 !throw_error ? &error : NULL,
    4892                 :         376 :                                                                                                 jsestate->args,
    4893                 :         376 :                                                                                                 jsexpr->column_name);
    4894                 :             : 
    4895         [ +  + ]:         376 :                                 if (jbv == NULL)
    4896                 :             :                                 {
    4897                 :             :                                         /* Will be coerced with json_populate_type(), if needed. */
    4898                 :          88 :                                         *op->resvalue = (Datum) 0;
    4899                 :          88 :                                         *op->resnull = true;
    4900                 :          88 :                                 }
    4901   [ +  +  -  + ]:         288 :                                 else if (!error && !empty)
    4902                 :             :                                 {
    4903   [ +  +  +  + ]:         283 :                                         if (jsexpr->returning->typid == JSONOID ||
    4904                 :         278 :                                                 jsexpr->returning->typid == JSONBOID)
    4905                 :             :                                         {
    4906                 :           9 :                                                 val_string = DatumGetCString(DirectFunctionCall1(jsonb_out,
    4907                 :             :                                                                                                                                                  JsonbPGetDatum(JsonbValueToJsonb(jbv))));
    4908                 :           9 :                                         }
    4909         [ +  + ]:         274 :                                         else if (jsexpr->use_json_coercion)
    4910                 :             :                                         {
    4911                 :          17 :                                                 *op->resvalue = JsonbPGetDatum(JsonbValueToJsonb(jbv));
    4912                 :          17 :                                                 *op->resnull = false;
    4913                 :          17 :                                         }
    4914                 :             :                                         else
    4915                 :             :                                         {
    4916                 :         257 :                                                 val_string = ExecGetJsonValueItemString(jbv, op->resnull);
    4917                 :             : 
    4918                 :             :                                                 /*
    4919                 :             :                                                  * Simply convert to the default RETURNING type (text)
    4920                 :             :                                                  * if no coercion needed.
    4921                 :             :                                                  */
    4922         [ +  + ]:         257 :                                                 if (!jsexpr->use_io_coercion)
    4923                 :          19 :                                                         *op->resvalue = DirectFunctionCall1(textin,
    4924                 :             :                                                                                                                                 CStringGetDatum(val_string));
    4925                 :             :                                         }
    4926                 :         283 :                                 }
    4927                 :             :                                 break;
    4928                 :         376 :                         }
    4929                 :             : 
    4930                 :             :                         /* JSON_TABLE_OP can't happen here */
    4931                 :             : 
    4932                 :             :                 default:
    4933   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized SQL/JSON expression op %d",
    4934                 :             :                                  (int) jsexpr->op);
    4935                 :           0 :                         return false;
    4936                 :             :         }
    4937                 :             : 
    4938                 :             :         /*
    4939                 :             :          * Coerce the result value to the RETURNING type by calling its input
    4940                 :             :          * function.
    4941                 :             :          */
    4942   [ +  +  +  + ]:         862 :         if (!*op->resnull && jsexpr->use_io_coercion)
    4943                 :             :         {
    4944                 :         247 :                 FunctionCallInfo fcinfo;
    4945                 :             : 
    4946         [ -  + ]:         247 :                 Assert(jump_eval_coercion == -1);
    4947                 :         247 :                 fcinfo = jsestate->input_fcinfo;
    4948         [ +  - ]:         247 :                 Assert(fcinfo != NULL);
    4949         [ +  - ]:         247 :                 Assert(val_string != NULL);
    4950                 :         247 :                 fcinfo->args[0].value = PointerGetDatum(val_string);
    4951                 :         247 :                 fcinfo->args[0].isnull = *op->resnull;
    4952                 :             : 
    4953                 :             :                 /*
    4954                 :             :                  * Second and third arguments are already set up in
    4955                 :             :                  * ExecInitJsonExpr().
    4956                 :             :                  */
    4957                 :             : 
    4958                 :         247 :                 fcinfo->isnull = false;
    4959                 :         247 :                 *op->resvalue = FunctionCallInvoke(fcinfo);
    4960   [ +  +  +  -  :         247 :                 if (SOFT_ERROR_OCCURRED(&jsestate->escontext))
                   +  + ]
    4961                 :          31 :                         error = true;
    4962                 :         247 :         }
    4963                 :             : 
    4964                 :             :         /*
    4965                 :             :          * When setting up the ErrorSaveContext (if needed) for capturing the
    4966                 :             :          * errors that occur when coercing the JsonBehavior expression, set
    4967                 :             :          * details_wanted to be able to show the actual error message as the
    4968                 :             :          * DETAIL of the error message that tells that it is the JsonBehavior
    4969                 :             :          * expression that caused the error; see ExecEvalJsonCoercionFinish().
    4970                 :             :          */
    4971                 :             : 
    4972                 :             :         /* Handle ON EMPTY. */
    4973         [ +  + ]:         862 :         if (empty)
    4974                 :             :         {
    4975                 :          92 :                 *op->resvalue = (Datum) 0;
    4976                 :          92 :                 *op->resnull = true;
    4977         [ +  - ]:          92 :                 if (jsexpr->on_empty)
    4978                 :             :                 {
    4979         [ +  + ]:          92 :                         if (jsexpr->on_empty->btype != JSON_BEHAVIOR_ERROR)
    4980                 :             :                         {
    4981                 :          82 :                                 jsestate->empty.value = BoolGetDatum(true);
    4982                 :             :                                 /* Set up to catch coercion errors of the ON EMPTY value. */
    4983                 :          82 :                                 jsestate->escontext.error_occurred = false;
    4984                 :          82 :                                 jsestate->escontext.details_wanted = true;
    4985                 :             :                                 /* Jump to end if the ON EMPTY behavior is to return NULL */
    4986         [ +  + ]:          82 :                                 return jsestate->jump_empty >= 0 ? jsestate->jump_empty : jsestate->jump_end;
    4987                 :             :                         }
    4988                 :          10 :                 }
    4989         [ #  # ]:           0 :                 else if (jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR)
    4990                 :             :                 {
    4991                 :           0 :                         jsestate->error.value = BoolGetDatum(true);
    4992                 :             :                         /* Set up to catch coercion errors of the ON ERROR value. */
    4993                 :           0 :                         jsestate->escontext.error_occurred = false;
    4994                 :           0 :                         jsestate->escontext.details_wanted = true;
    4995         [ #  # ]:           0 :                         Assert(!throw_error);
    4996                 :             :                         /* Jump to end if the ON ERROR behavior is to return NULL */
    4997         [ #  # ]:           0 :                         return jsestate->jump_error >= 0 ? jsestate->jump_error : jsestate->jump_end;
    4998                 :             :                 }
    4999                 :             : 
    5000         [ +  + ]:          10 :                 if (jsexpr->column_name)
    5001   [ +  -  +  - ]:           2 :                         ereport(ERROR,
    5002                 :             :                                         errcode(ERRCODE_NO_SQL_JSON_ITEM),
    5003                 :             :                                         errmsg("no SQL/JSON item found for specified path of column \"%s\"",
    5004                 :             :                                                    jsexpr->column_name));
    5005                 :             :                 else
    5006   [ +  -  +  - ]:           8 :                         ereport(ERROR,
    5007                 :             :                                         errcode(ERRCODE_NO_SQL_JSON_ITEM),
    5008                 :             :                                         errmsg("no SQL/JSON item found for specified path"));
    5009                 :           0 :         }
    5010                 :             : 
    5011                 :             :         /*
    5012                 :             :          * ON ERROR. Wouldn't get here if the behavior is ERROR, because they
    5013                 :             :          * would have already been thrown.
    5014                 :             :          */
    5015         [ +  + ]:         770 :         if (error)
    5016                 :             :         {
    5017         [ +  - ]:          89 :                 Assert(!throw_error);
    5018                 :          89 :                 *op->resvalue = (Datum) 0;
    5019                 :          89 :                 *op->resnull = true;
    5020                 :          89 :                 jsestate->error.value = BoolGetDatum(true);
    5021                 :             :                 /* Set up to catch coercion errors of the ON ERROR value. */
    5022                 :          89 :                 jsestate->escontext.error_occurred = false;
    5023                 :          89 :                 jsestate->escontext.details_wanted = true;
    5024                 :             :                 /* Jump to end if the ON ERROR behavior is to return NULL */
    5025         [ +  + ]:          89 :                 return jsestate->jump_error >= 0 ? jsestate->jump_error : jsestate->jump_end;
    5026                 :             :         }
    5027                 :             : 
    5028         [ +  + ]:         681 :         return jump_eval_coercion >= 0 ? jump_eval_coercion : jsestate->jump_end;
    5029                 :         852 : }
    5030                 :             : 
    5031                 :             : /*
    5032                 :             :  * Convert the given JsonbValue to its C string representation
    5033                 :             :  *
    5034                 :             :  * *resnull is set if the JsonbValue is a jbvNull.
    5035                 :             :  */
    5036                 :             : static char *
    5037                 :         257 : ExecGetJsonValueItemString(JsonbValue *item, bool *resnull)
    5038                 :             : {
    5039                 :         257 :         *resnull = false;
    5040                 :             : 
    5041                 :             :         /* get coercion state reference and datum of the corresponding SQL type */
    5042   [ -  -  +  +  :         257 :         switch (item->type)
                +  +  - ]
    5043                 :             :         {
    5044                 :             :                 case jbvNull:
    5045                 :           0 :                         *resnull = true;
    5046                 :           0 :                         return NULL;
    5047                 :             : 
    5048                 :             :                 case jbvString:
    5049                 :             :                         {
    5050                 :          52 :                                 char       *str = palloc(item->val.string.len + 1);
    5051                 :             : 
    5052                 :          52 :                                 memcpy(str, item->val.string.val, item->val.string.len);
    5053                 :          52 :                                 str[item->val.string.len] = '\0';
    5054                 :          52 :                                 return str;
    5055                 :          52 :                         }
    5056                 :             : 
    5057                 :             :                 case jbvNumeric:
    5058                 :         186 :                         return DatumGetCString(DirectFunctionCall1(numeric_out,
    5059                 :             :                                                                                                            NumericGetDatum(item->val.numeric)));
    5060                 :             : 
    5061                 :             :                 case jbvBool:
    5062                 :          12 :                         return DatumGetCString(DirectFunctionCall1(boolout,
    5063                 :             :                                                                                                            BoolGetDatum(item->val.boolean)));
    5064                 :             : 
    5065                 :             :                 case jbvDatetime:
    5066   [ +  +  +  +  :           7 :                         switch (item->val.datetime.typid)
                   +  - ]
    5067                 :             :                         {
    5068                 :             :                                 case DATEOID:
    5069                 :           1 :                                         return DatumGetCString(DirectFunctionCall1(date_out,
    5070                 :             :                                                                                                                            item->val.datetime.value));
    5071                 :             :                                 case TIMEOID:
    5072                 :           1 :                                         return DatumGetCString(DirectFunctionCall1(time_out,
    5073                 :             :                                                                                                                            item->val.datetime.value));
    5074                 :             :                                 case TIMETZOID:
    5075                 :           1 :                                         return DatumGetCString(DirectFunctionCall1(timetz_out,
    5076                 :             :                                                                                                                            item->val.datetime.value));
    5077                 :             :                                 case TIMESTAMPOID:
    5078                 :           1 :                                         return DatumGetCString(DirectFunctionCall1(timestamp_out,
    5079                 :             :                                                                                                                            item->val.datetime.value));
    5080                 :             :                                 case TIMESTAMPTZOID:
    5081                 :           3 :                                         return DatumGetCString(DirectFunctionCall1(timestamptz_out,
    5082                 :             :                                                                                                                            item->val.datetime.value));
    5083                 :             :                                 default:
    5084   [ #  #  #  # ]:           0 :                                         elog(ERROR, "unexpected jsonb datetime type oid %u",
    5085                 :             :                                                  item->val.datetime.typid);
    5086                 :           0 :                         }
    5087                 :           0 :                         break;
    5088                 :             : 
    5089                 :             :                 case jbvArray:
    5090                 :             :                 case jbvObject:
    5091                 :             :                 case jbvBinary:
    5092                 :           0 :                         return DatumGetCString(DirectFunctionCall1(jsonb_out,
    5093                 :             :                                                                                                            JsonbPGetDatum(JsonbValueToJsonb(item))));
    5094                 :             : 
    5095                 :             :                 default:
    5096   [ #  #  #  # ]:           0 :                         elog(ERROR, "unexpected jsonb value type %d", item->type);
    5097                 :           0 :         }
    5098                 :             : 
    5099                 :           0 :         Assert(false);
    5100                 :           0 :         *resnull = true;
    5101                 :           0 :         return NULL;
    5102                 :         257 : }
    5103                 :             : 
    5104                 :             : /*
    5105                 :             :  * Coerce a jsonb value produced by ExecEvalJsonExprPath() or an ON ERROR /
    5106                 :             :  * ON EMPTY behavior expression to the target type.
    5107                 :             :  *
    5108                 :             :  * Any soft errors that occur here will be checked by
    5109                 :             :  * EEOP_JSONEXPR_COERCION_FINISH that will run after this.
    5110                 :             :  */
    5111                 :             : void
    5112                 :         300 : ExecEvalJsonCoercion(ExprState *state, ExprEvalStep *op,
    5113                 :             :                                          ExprContext *econtext)
    5114                 :             : {
    5115                 :         300 :         ErrorSaveContext *escontext = op->d.jsonexpr_coercion.escontext;
    5116                 :             : 
    5117                 :             :         /*
    5118                 :             :          * Prepare to call json_populate_type() to coerce the boolean result of
    5119                 :             :          * JSON_EXISTS_OP to the target type.  If the target type is integer or a
    5120                 :             :          * domain over integer, call the boolean-to-integer cast function instead,
    5121                 :             :          * because the integer's input function (which is what
    5122                 :             :          * json_populate_type() calls to coerce to scalar target types) doesn't
    5123                 :             :          * accept boolean literals as valid input.  We only have a special case
    5124                 :             :          * for integer and domains thereof as it seems common to use those types
    5125                 :             :          * for EXISTS columns in JSON_TABLE().
    5126                 :             :          */
    5127         [ +  + ]:         300 :         if (op->d.jsonexpr_coercion.exists_coerce)
    5128                 :             :         {
    5129         [ +  + ]:          29 :                 if (op->d.jsonexpr_coercion.exists_cast_to_int)
    5130                 :             :                 {
    5131                 :             :                         /* Check domain constraints if any. */
    5132   [ +  +  +  + ]:          20 :                         if (op->d.jsonexpr_coercion.exists_check_domain &&
    5133                 :           8 :                                 !domain_check_safe(*op->resvalue, *op->resnull,
    5134                 :           4 :                                                                    op->d.jsonexpr_coercion.targettype,
    5135                 :           4 :                                                                    &op->d.jsonexpr_coercion.json_coercion_cache,
    5136                 :           4 :                                                                    econtext->ecxt_per_query_memory,
    5137                 :           4 :                                                                    (Node *) escontext))
    5138                 :             :                         {
    5139                 :           3 :                                 *op->resnull = true;
    5140                 :           3 :                                 *op->resvalue = (Datum) 0;
    5141                 :           3 :                         }
    5142                 :             :                         else
    5143                 :          17 :                                 *op->resvalue = DirectFunctionCall1(bool_int4, *op->resvalue);
    5144                 :          20 :                         return;
    5145                 :             :                 }
    5146                 :             : 
    5147         [ +  + ]:           9 :                 *op->resvalue = DirectFunctionCall1(jsonb_in,
    5148                 :             :                                                                                         DatumGetBool(*op->resvalue) ?
    5149                 :             :                                                                                         CStringGetDatum("true") :
    5150                 :             :                                                                                         CStringGetDatum("false"));
    5151                 :           9 :         }
    5152                 :             : 
    5153                 :         560 :         *op->resvalue = json_populate_type(*op->resvalue, JSONBOID,
    5154                 :         280 :                                                                            op->d.jsonexpr_coercion.targettype,
    5155                 :         280 :                                                                            op->d.jsonexpr_coercion.targettypmod,
    5156                 :         280 :                                                                            &op->d.jsonexpr_coercion.json_coercion_cache,
    5157                 :         280 :                                                                            econtext->ecxt_per_query_memory,
    5158                 :         280 :                                                                            op->resnull,
    5159                 :         280 :                                                                            op->d.jsonexpr_coercion.omit_quotes,
    5160                 :         280 :                                                                            (Node *) escontext);
    5161         [ -  + ]:         300 : }
    5162                 :             : 
    5163                 :             : static char *
    5164                 :          13 : GetJsonBehaviorValueString(JsonBehavior *behavior)
    5165                 :             : {
    5166                 :             :         /*
    5167                 :             :          * The order of array elements must correspond to the order of
    5168                 :             :          * JsonBehaviorType members.
    5169                 :             :          */
    5170                 :          13 :         const char *behavior_names[] =
    5171                 :             :         {
    5172                 :             :                 "NULL",
    5173                 :             :                 "ERROR",
    5174                 :             :                 "EMPTY",
    5175                 :             :                 "TRUE",
    5176                 :             :                 "FALSE",
    5177                 :             :                 "UNKNOWN",
    5178                 :             :                 "EMPTY ARRAY",
    5179                 :             :                 "EMPTY OBJECT",
    5180                 :             :                 "DEFAULT"
    5181                 :             :         };
    5182                 :             : 
    5183                 :          26 :         return pstrdup(behavior_names[behavior->btype]);
    5184                 :          13 : }
    5185                 :             : 
    5186                 :             : /*
    5187                 :             :  * Checks if an error occurred in ExecEvalJsonCoercion().  If so, this sets
    5188                 :             :  * JsonExprState.error to trigger the ON ERROR handling steps, unless the
    5189                 :             :  * error is thrown when coercing a JsonBehavior value.
    5190                 :             :  */
    5191                 :             : void
    5192                 :         283 : ExecEvalJsonCoercionFinish(ExprState *state, ExprEvalStep *op)
    5193                 :             : {
    5194                 :         283 :         JsonExprState *jsestate = op->d.jsonexpr.jsestate;
    5195                 :             : 
    5196   [ +  -  +  -  :         283 :         if (SOFT_ERROR_OCCURRED(&jsestate->escontext))
                   +  + ]
    5197                 :             :         {
    5198                 :             :                 /*
    5199                 :             :                  * jsestate->error or jsestate->empty being set means that the error
    5200                 :             :                  * occurred when coercing the JsonBehavior value.  Throw the error in
    5201                 :             :                  * that case with the actual coercion error message shown in the
    5202                 :             :                  * DETAIL part.
    5203                 :             :                  */
    5204         [ +  + ]:          92 :                 if (DatumGetBool(jsestate->error.value))
    5205   [ +  -  +  - ]:          10 :                         ereport(ERROR,
    5206                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
    5207                 :             :                         /*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */
    5208                 :             :                                          errmsg("could not coerce %s expression (%s) to the RETURNING type",
    5209                 :             :                                                         "ON ERROR",
    5210                 :             :                                                         GetJsonBehaviorValueString(jsestate->jsexpr->on_error)),
    5211                 :             :                                          errdetail("%s", jsestate->escontext.error_data->message)));
    5212         [ +  + ]:          82 :                 else if (DatumGetBool(jsestate->empty.value))
    5213   [ +  -  +  - ]:           3 :                         ereport(ERROR,
    5214                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
    5215                 :             :                         /*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */
    5216                 :             :                                          errmsg("could not coerce %s expression (%s) to the RETURNING type",
    5217                 :             :                                                         "ON EMPTY",
    5218                 :             :                                                         GetJsonBehaviorValueString(jsestate->jsexpr->on_empty)),
    5219                 :             :                                          errdetail("%s", jsestate->escontext.error_data->message)));
    5220                 :             : 
    5221                 :          79 :                 *op->resvalue = (Datum) 0;
    5222                 :          79 :                 *op->resnull = true;
    5223                 :             : 
    5224                 :          79 :                 jsestate->error.value = BoolGetDatum(true);
    5225                 :             : 
    5226                 :             :                 /*
    5227                 :             :                  * Reset for next use such as for catching errors when coercing a
    5228                 :             :                  * JsonBehavior expression.
    5229                 :             :                  */
    5230                 :          79 :                 jsestate->escontext.error_occurred = false;
    5231                 :          79 :                 jsestate->escontext.details_wanted = true;
    5232                 :          79 :         }
    5233                 :         270 : }
    5234                 :             : 
    5235                 :             : /*
    5236                 :             :  * ExecEvalGroupingFunc
    5237                 :             :  *
    5238                 :             :  * Computes a bitmask with a bit for each (unevaluated) argument expression
    5239                 :             :  * (rightmost arg is least significant bit).
    5240                 :             :  *
    5241                 :             :  * A bit is set if the corresponding expression is NOT part of the set of
    5242                 :             :  * grouping expressions in the current grouping set.
    5243                 :             :  */
    5244                 :             : void
    5245                 :         319 : ExecEvalGroupingFunc(ExprState *state, ExprEvalStep *op)
    5246                 :             : {
    5247                 :         319 :         AggState   *aggstate = castNode(AggState, state->parent);
    5248                 :         319 :         int                     result = 0;
    5249                 :         319 :         Bitmapset  *grouped_cols = aggstate->grouped_cols;
    5250                 :         319 :         ListCell   *lc;
    5251                 :             : 
    5252   [ +  +  +  +  :         772 :         foreach(lc, op->d.grouping_func.clauses)
                   +  + ]
    5253                 :             :         {
    5254                 :         453 :                 int                     attnum = lfirst_int(lc);
    5255                 :             : 
    5256                 :         453 :                 result <<= 1;
    5257                 :             : 
    5258         [ +  + ]:         453 :                 if (!bms_is_member(attnum, grouped_cols))
    5259                 :         181 :                         result |= 1;
    5260                 :         453 :         }
    5261                 :             : 
    5262                 :         319 :         *op->resvalue = Int32GetDatum(result);
    5263                 :         319 :         *op->resnull = false;
    5264                 :         319 : }
    5265                 :             : 
    5266                 :             : /*
    5267                 :             :  * ExecEvalMergeSupportFunc
    5268                 :             :  *
    5269                 :             :  * Returns information about the current MERGE action for its RETURNING list.
    5270                 :             :  */
    5271                 :             : void
    5272                 :          74 : ExecEvalMergeSupportFunc(ExprState *state, ExprEvalStep *op,
    5273                 :             :                                                  ExprContext *econtext)
    5274                 :             : {
    5275                 :          74 :         ModifyTableState *mtstate = castNode(ModifyTableState, state->parent);
    5276                 :          74 :         MergeActionState *relaction = mtstate->mt_merge_action;
    5277                 :             : 
    5278         [ +  - ]:          74 :         if (!relaction)
    5279   [ #  #  #  # ]:           0 :                 elog(ERROR, "no merge action in progress");
    5280                 :             : 
    5281                 :             :         /* Return the MERGE action ("INSERT", "UPDATE", or "DELETE") */
    5282   [ +  +  +  -  :          74 :         switch (relaction->mas_action->commandType)
                      - ]
    5283                 :             :         {
    5284                 :             :                 case CMD_INSERT:
    5285                 :          25 :                         *op->resvalue = PointerGetDatum(cstring_to_text_with_len("INSERT", 6));
    5286                 :          25 :                         *op->resnull = false;
    5287                 :          25 :                         break;
    5288                 :             :                 case CMD_UPDATE:
    5289                 :          29 :                         *op->resvalue = PointerGetDatum(cstring_to_text_with_len("UPDATE", 6));
    5290                 :          29 :                         *op->resnull = false;
    5291                 :          29 :                         break;
    5292                 :             :                 case CMD_DELETE:
    5293                 :          20 :                         *op->resvalue = PointerGetDatum(cstring_to_text_with_len("DELETE", 6));
    5294                 :          20 :                         *op->resnull = false;
    5295                 :          20 :                         break;
    5296                 :             :                 case CMD_NOTHING:
    5297   [ #  #  #  # ]:           0 :                         elog(ERROR, "unexpected merge action: DO NOTHING");
    5298                 :           0 :                         break;
    5299                 :             :                 default:
    5300   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized commandType: %d",
    5301                 :             :                                  (int) relaction->mas_action->commandType);
    5302                 :           0 :         }
    5303                 :          74 : }
    5304                 :             : 
    5305                 :             : /*
    5306                 :             :  * Hand off evaluation of a subplan to nodeSubplan.c
    5307                 :             :  */
    5308                 :             : void
    5309                 :       97550 : ExecEvalSubPlan(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    5310                 :             : {
    5311                 :       97550 :         SubPlanState *sstate = op->d.subplan.sstate;
    5312                 :             : 
    5313                 :             :         /* could potentially be nested, so make sure there's enough stack */
    5314                 :       97550 :         check_stack_depth();
    5315                 :             : 
    5316                 :       97550 :         *op->resvalue = ExecSubPlan(sstate, econtext, op->resnull);
    5317                 :       97550 : }
    5318                 :             : 
    5319                 :             : /*
    5320                 :             :  * Evaluate a wholerow Var expression.
    5321                 :             :  *
    5322                 :             :  * Returns a Datum whose value is the value of a whole-row range variable
    5323                 :             :  * with respect to given expression context.
    5324                 :             :  */
    5325                 :             : void
    5326                 :        2723 : ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
    5327                 :             : {
    5328                 :        2723 :         Var                *variable = op->d.wholerow.var;
    5329                 :        2723 :         TupleTableSlot *slot = NULL;
    5330                 :        2723 :         TupleDesc       output_tupdesc;
    5331                 :        2723 :         MemoryContext oldcontext;
    5332                 :        2723 :         HeapTupleHeader dtuple;
    5333                 :        2723 :         HeapTuple       tuple;
    5334                 :             : 
    5335                 :             :         /* This was checked by ExecInitExpr */
    5336         [ +  - ]:        2723 :         Assert(variable->varattno == InvalidAttrNumber);
    5337                 :             : 
    5338                 :             :         /* Get the input slot we want */
    5339      [ +  +  + ]:        2723 :         switch (variable->varno)
    5340                 :             :         {
    5341                 :             :                 case INNER_VAR:
    5342                 :             :                         /* get the tuple from the inner node */
    5343                 :          15 :                         slot = econtext->ecxt_innertuple;
    5344                 :          15 :                         break;
    5345                 :             : 
    5346                 :             :                 case OUTER_VAR:
    5347                 :             :                         /* get the tuple from the outer node */
    5348                 :           3 :                         slot = econtext->ecxt_outertuple;
    5349                 :           3 :                         break;
    5350                 :             : 
    5351                 :             :                         /* INDEX_VAR is handled by default case */
    5352                 :             : 
    5353                 :             :                 default:
    5354                 :             : 
    5355                 :             :                         /*
    5356                 :             :                          * Get the tuple from the relation being scanned.
    5357                 :             :                          *
    5358                 :             :                          * By default, this uses the "scan" tuple slot, but a wholerow Var
    5359                 :             :                          * in the RETURNING list may explicitly refer to OLD/NEW.  If the
    5360                 :             :                          * OLD/NEW row doesn't exist, we just return NULL.
    5361                 :             :                          */
    5362   [ -  +  +  + ]:        2705 :                         switch (variable->varreturningtype)
    5363                 :             :                         {
    5364                 :             :                                 case VAR_RETURNING_DEFAULT:
    5365                 :        2647 :                                         slot = econtext->ecxt_scantuple;
    5366                 :        2647 :                                         break;
    5367                 :             : 
    5368                 :             :                                 case VAR_RETURNING_OLD:
    5369         [ +  + ]:          29 :                                         if (state->flags & EEO_FLAG_OLD_IS_NULL)
    5370                 :             :                                         {
    5371                 :           8 :                                                 *op->resvalue = (Datum) 0;
    5372                 :           8 :                                                 *op->resnull = true;
    5373                 :           8 :                                                 return;
    5374                 :             :                                         }
    5375                 :          21 :                                         slot = econtext->ecxt_oldtuple;
    5376                 :          21 :                                         break;
    5377                 :             : 
    5378                 :             :                                 case VAR_RETURNING_NEW:
    5379         [ +  + ]:          29 :                                         if (state->flags & EEO_FLAG_NEW_IS_NULL)
    5380                 :             :                                         {
    5381                 :           5 :                                                 *op->resvalue = (Datum) 0;
    5382                 :           5 :                                                 *op->resnull = true;
    5383                 :           5 :                                                 return;
    5384                 :             :                                         }
    5385                 :          24 :                                         slot = econtext->ecxt_newtuple;
    5386                 :          24 :                                         break;
    5387                 :             :                         }
    5388                 :        2692 :                         break;
    5389                 :             :         }
    5390                 :             : 
    5391                 :             :         /* Apply the junkfilter if any */
    5392         [ +  + ]:        2710 :         if (op->d.wholerow.junkFilter != NULL)
    5393                 :          10 :                 slot = ExecFilterJunk(op->d.wholerow.junkFilter, slot);
    5394                 :             : 
    5395                 :             :         /*
    5396                 :             :          * If first time through, obtain tuple descriptor and check compatibility.
    5397                 :             :          *
    5398                 :             :          * XXX: It'd be great if this could be moved to the expression
    5399                 :             :          * initialization phase, but due to using slots that's currently not
    5400                 :             :          * feasible.
    5401                 :             :          */
    5402         [ +  + ]:        2710 :         if (op->d.wholerow.first)
    5403                 :             :         {
    5404                 :             :                 /* optimistically assume we don't need slow path */
    5405                 :         468 :                 op->d.wholerow.slow = false;
    5406                 :             : 
    5407                 :             :                 /*
    5408                 :             :                  * If the Var identifies a named composite type, we must check that
    5409                 :             :                  * the actual tuple type is compatible with it.
    5410                 :             :                  */
    5411         [ +  + ]:         468 :                 if (variable->vartype != RECORDOID)
    5412                 :             :                 {
    5413                 :         324 :                         TupleDesc       var_tupdesc;
    5414                 :         324 :                         TupleDesc       slot_tupdesc;
    5415                 :             : 
    5416                 :             :                         /*
    5417                 :             :                          * We really only care about numbers of attributes and data types.
    5418                 :             :                          * Also, we can ignore type mismatch on columns that are dropped
    5419                 :             :                          * in the destination type, so long as (1) the physical storage
    5420                 :             :                          * matches or (2) the actual column value is NULL.  Case (1) is
    5421                 :             :                          * helpful in some cases involving out-of-date cached plans, while
    5422                 :             :                          * case (2) is expected behavior in situations such as an INSERT
    5423                 :             :                          * into a table with dropped columns (the planner typically
    5424                 :             :                          * generates an INT4 NULL regardless of the dropped column type).
    5425                 :             :                          * If we find a dropped column and cannot verify that case (1)
    5426                 :             :                          * holds, we have to use the slow path to check (2) for each row.
    5427                 :             :                          *
    5428                 :             :                          * If vartype is a domain over composite, just look through that
    5429                 :             :                          * to the base composite type.
    5430                 :             :                          */
    5431                 :         324 :                         var_tupdesc = lookup_rowtype_tupdesc_domain(variable->vartype,
    5432                 :             :                                                                                                                 -1, false);
    5433                 :             : 
    5434                 :         324 :                         slot_tupdesc = slot->tts_tupleDescriptor;
    5435                 :             : 
    5436         [ +  - ]:         324 :                         if (var_tupdesc->natts != slot_tupdesc->natts)
    5437   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
    5438                 :             :                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    5439                 :             :                                                  errmsg("table row type and query-specified row type do not match"),
    5440                 :             :                                                  errdetail_plural("Table row contains %d attribute, but query expects %d.",
    5441                 :             :                                                                                   "Table row contains %d attributes, but query expects %d.",
    5442                 :             :                                                                                   slot_tupdesc->natts,
    5443                 :             :                                                                                   slot_tupdesc->natts,
    5444                 :             :                                                                                   var_tupdesc->natts)));
    5445                 :             : 
    5446         [ +  + ]:        1224 :                         for (int i = 0; i < var_tupdesc->natts; i++)
    5447                 :             :                         {
    5448                 :         900 :                                 Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i);
    5449                 :         900 :                                 Form_pg_attribute sattr = TupleDescAttr(slot_tupdesc, i);
    5450                 :             : 
    5451         [ +  - ]:         900 :                                 if (vattr->atttypid == sattr->atttypid)
    5452                 :         900 :                                         continue;       /* no worries */
    5453         [ #  # ]:           0 :                                 if (!vattr->attisdropped)
    5454   [ #  #  #  # ]:           0 :                                         ereport(ERROR,
    5455                 :             :                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
    5456                 :             :                                                          errmsg("table row type and query-specified row type do not match"),
    5457                 :             :                                                          errdetail("Table has type %s at ordinal position %d, but query expects %s.",
    5458                 :             :                                                                            format_type_be(sattr->atttypid),
    5459                 :             :                                                                            i + 1,
    5460                 :             :                                                                            format_type_be(vattr->atttypid))));
    5461                 :             : 
    5462   [ #  #  #  # ]:           0 :                                 if (vattr->attlen != sattr->attlen ||
    5463                 :           0 :                                         vattr->attalign != sattr->attalign)
    5464                 :           0 :                                         op->d.wholerow.slow = true; /* need to check for nulls */
    5465         [ +  - ]:         900 :                         }
    5466                 :             : 
    5467                 :             :                         /*
    5468                 :             :                          * Use the variable's declared rowtype as the descriptor for the
    5469                 :             :                          * output values.  In particular, we *must* absorb any
    5470                 :             :                          * attisdropped markings.
    5471                 :             :                          */
    5472                 :         324 :                         oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
    5473                 :         324 :                         output_tupdesc = CreateTupleDescCopy(var_tupdesc);
    5474                 :         324 :                         MemoryContextSwitchTo(oldcontext);
    5475                 :             : 
    5476         [ -  + ]:         324 :                         ReleaseTupleDesc(var_tupdesc);
    5477                 :         324 :                 }
    5478                 :             :                 else
    5479                 :             :                 {
    5480                 :             :                         /*
    5481                 :             :                          * In the RECORD case, we use the input slot's rowtype as the
    5482                 :             :                          * descriptor for the output values, modulo possibly assigning new
    5483                 :             :                          * column names below.
    5484                 :             :                          */
    5485                 :         144 :                         oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
    5486                 :         144 :                         output_tupdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor);
    5487                 :         144 :                         MemoryContextSwitchTo(oldcontext);
    5488                 :             : 
    5489                 :             :                         /*
    5490                 :             :                          * It's possible that the input slot is a relation scan slot and
    5491                 :             :                          * so is marked with that relation's rowtype.  But we're supposed
    5492                 :             :                          * to be returning RECORD, so reset to that.
    5493                 :             :                          */
    5494                 :         144 :                         output_tupdesc->tdtypeid = RECORDOID;
    5495                 :         144 :                         output_tupdesc->tdtypmod = -1;
    5496                 :             : 
    5497                 :             :                         /*
    5498                 :             :                          * We already got the correct physical datatype info above, but
    5499                 :             :                          * now we should try to find the source RTE and adopt its column
    5500                 :             :                          * aliases, since it's unlikely that the input slot has the
    5501                 :             :                          * desired names.
    5502                 :             :                          *
    5503                 :             :                          * If we can't locate the RTE, assume the column names we've got
    5504                 :             :                          * are OK.  (As of this writing, the only cases where we can't
    5505                 :             :                          * locate the RTE are in execution of trigger WHEN clauses, and
    5506                 :             :                          * then the Var will have the trigger's relation's rowtype, so its
    5507                 :             :                          * names are fine.)  Also, if the creator of the RTE didn't bother
    5508                 :             :                          * to fill in an eref field, assume our column names are OK. (This
    5509                 :             :                          * happens in COPY, and perhaps other places.)
    5510                 :             :                          */
    5511   [ +  -  -  + ]:         144 :                         if (econtext->ecxt_estate &&
    5512                 :         144 :                                 variable->varno <= econtext->ecxt_estate->es_range_table_size)
    5513                 :             :                         {
    5514                 :         288 :                                 RangeTblEntry *rte = exec_rt_fetch(variable->varno,
    5515                 :         144 :                                                                                                    econtext->ecxt_estate);
    5516                 :             : 
    5517         [ -  + ]:         144 :                                 if (rte->eref)
    5518                 :         144 :                                         ExecTypeSetColNames(output_tupdesc, rte->eref->colnames);
    5519                 :         144 :                         }
    5520                 :             :                 }
    5521                 :             : 
    5522                 :             :                 /* Bless the tupdesc if needed, and save it in the execution state */
    5523                 :         468 :                 op->d.wholerow.tupdesc = BlessTupleDesc(output_tupdesc);
    5524                 :             : 
    5525                 :         468 :                 op->d.wholerow.first = false;
    5526                 :         468 :         }
    5527                 :             : 
    5528                 :             :         /*
    5529                 :             :          * Make sure all columns of the slot are accessible in the slot's
    5530                 :             :          * Datum/isnull arrays.
    5531                 :             :          */
    5532                 :        2710 :         slot_getallattrs(slot);
    5533                 :             : 
    5534         [ +  - ]:        2710 :         if (op->d.wholerow.slow)
    5535                 :             :         {
    5536                 :             :                 /* Check to see if any dropped attributes are non-null */
    5537                 :           0 :                 TupleDesc       tupleDesc = slot->tts_tupleDescriptor;
    5538                 :           0 :                 TupleDesc       var_tupdesc = op->d.wholerow.tupdesc;
    5539                 :             : 
    5540         [ #  # ]:           0 :                 Assert(var_tupdesc->natts == tupleDesc->natts);
    5541                 :             : 
    5542         [ #  # ]:           0 :                 for (int i = 0; i < var_tupdesc->natts; i++)
    5543                 :             :                 {
    5544                 :           0 :                         CompactAttribute *vattr = TupleDescCompactAttr(var_tupdesc, i);
    5545                 :           0 :                         CompactAttribute *sattr = TupleDescCompactAttr(tupleDesc, i);
    5546                 :             : 
    5547         [ #  # ]:           0 :                         if (!vattr->attisdropped)
    5548                 :           0 :                                 continue;               /* already checked non-dropped cols */
    5549         [ #  # ]:           0 :                         if (slot->tts_isnull[i])
    5550                 :           0 :                                 continue;               /* null is always okay */
    5551         [ #  # ]:           0 :                         if (vattr->attlen != sattr->attlen ||
    5552                 :           0 :                                 vattr->attalignby != sattr->attalignby)
    5553   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
    5554                 :             :                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    5555                 :             :                                                  errmsg("table row type and query-specified row type do not match"),
    5556                 :             :                                                  errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
    5557                 :             :                                                                    i + 1)));
    5558         [ #  # ]:           0 :                 }
    5559                 :           0 :         }
    5560                 :             : 
    5561                 :             :         /*
    5562                 :             :          * Build a composite datum, making sure any toasted fields get detoasted.
    5563                 :             :          *
    5564                 :             :          * (Note: it is critical that we not change the slot's state here.)
    5565                 :             :          */
    5566                 :        5420 :         tuple = toast_build_flattened_tuple(slot->tts_tupleDescriptor,
    5567                 :        2710 :                                                                                 slot->tts_values,
    5568                 :        2710 :                                                                                 slot->tts_isnull);
    5569                 :        2710 :         dtuple = tuple->t_data;
    5570                 :             : 
    5571                 :             :         /*
    5572                 :             :          * Label the datum with the composite type info we identified before.
    5573                 :             :          *
    5574                 :             :          * (Note: we could skip doing this by passing op->d.wholerow.tupdesc to
    5575                 :             :          * the tuple build step; but that seems a tad risky so let's not.)
    5576                 :             :          */
    5577                 :        2710 :         HeapTupleHeaderSetTypeId(dtuple, op->d.wholerow.tupdesc->tdtypeid);
    5578                 :        2710 :         HeapTupleHeaderSetTypMod(dtuple, op->d.wholerow.tupdesc->tdtypmod);
    5579                 :             : 
    5580                 :        2710 :         *op->resvalue = PointerGetDatum(dtuple);
    5581                 :        2710 :         *op->resnull = false;
    5582                 :        2723 : }
    5583                 :             : 
    5584                 :             : void
    5585                 :     1030741 : ExecEvalSysVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext,
    5586                 :             :                            TupleTableSlot *slot)
    5587                 :             : {
    5588                 :     1030741 :         Datum           d;
    5589                 :             : 
    5590                 :             :         /* OLD/NEW system attribute is NULL if OLD/NEW row is NULL */
    5591         [ +  + ]:     1030741 :         if ((op->d.var.varreturningtype == VAR_RETURNING_OLD &&
    5592         [ +  + ]:          76 :                  state->flags & EEO_FLAG_OLD_IS_NULL) ||
    5593         [ +  + ]:     1030729 :                 (op->d.var.varreturningtype == VAR_RETURNING_NEW &&
    5594                 :          38 :                  state->flags & EEO_FLAG_NEW_IS_NULL))
    5595                 :             :         {
    5596                 :          50 :                 *op->resvalue = (Datum) 0;
    5597                 :          50 :                 *op->resnull = true;
    5598                 :          50 :                 return;
    5599                 :             :         }
    5600                 :             : 
    5601                 :             :         /* slot_getsysattr has sufficient defenses against bad attnums */
    5602                 :     2061382 :         d = slot_getsysattr(slot,
    5603                 :     1030691 :                                                 op->d.var.attnum,
    5604                 :     1030691 :                                                 op->resnull);
    5605                 :     1030691 :         *op->resvalue = d;
    5606                 :             :         /* this ought to be unreachable, but it's cheap enough to check */
    5607         [ +  - ]:     1030691 :         if (unlikely(*op->resnull))
    5608   [ #  #  #  # ]:           0 :                 elog(ERROR, "failed to fetch attribute from slot");
    5609         [ -  + ]:     1030717 : }
    5610                 :             : 
    5611                 :             : /*
    5612                 :             :  * Transition value has not been initialized. This is the first non-NULL input
    5613                 :             :  * value for a group. We use it as the initial value for transValue.
    5614                 :             :  */
    5615                 :             : void
    5616                 :        9853 : ExecAggInitGroup(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup,
    5617                 :             :                                  ExprContext *aggcontext)
    5618                 :             : {
    5619                 :        9853 :         FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
    5620                 :        9853 :         MemoryContext oldContext;
    5621                 :             : 
    5622                 :             :         /*
    5623                 :             :          * We must copy the datum into aggcontext if it is pass-by-ref. We do not
    5624                 :             :          * need to pfree the old transValue, since it's NULL.  (We already checked
    5625                 :             :          * that the agg's input type is binary-compatible with its transtype, so
    5626                 :             :          * straight copy here is OK.)
    5627                 :             :          */
    5628                 :        9853 :         oldContext = MemoryContextSwitchTo(aggcontext->ecxt_per_tuple_memory);
    5629                 :       19706 :         pergroup->transValue = datumCopy(fcinfo->args[1].value,
    5630                 :        9853 :                                                                          pertrans->transtypeByVal,
    5631                 :        9853 :                                                                          pertrans->transtypeLen);
    5632                 :        9853 :         pergroup->transValueIsNull = false;
    5633                 :        9853 :         pergroup->noTransValue = false;
    5634                 :        9853 :         MemoryContextSwitchTo(oldContext);
    5635                 :        9853 : }
    5636                 :             : 
    5637                 :             : /*
    5638                 :             :  * Ensure that the new transition value is stored in the aggcontext,
    5639                 :             :  * rather than the per-tuple context.  This should be invoked only when
    5640                 :             :  * we know (a) the transition data type is pass-by-reference, and (b)
    5641                 :             :  * the newValue is distinct from the oldValue.
    5642                 :             :  *
    5643                 :             :  * NB: This can change the current memory context.
    5644                 :             :  *
    5645                 :             :  * We copy the presented newValue into the aggcontext, except when the datum
    5646                 :             :  * points to a R/W expanded object that is already a child of the aggcontext,
    5647                 :             :  * in which case we need not copy.  We then delete the oldValue, if not null.
    5648                 :             :  *
    5649                 :             :  * If the presented datum points to a R/W expanded object that is a child of
    5650                 :             :  * some other context, ideally we would just reparent it under the aggcontext.
    5651                 :             :  * Unfortunately, that doesn't work easily, and it wouldn't help anyway for
    5652                 :             :  * aggregate-aware transfns.  We expect that a transfn that deals in expanded
    5653                 :             :  * objects and is aware of the memory management conventions for aggregate
    5654                 :             :  * transition values will (1) on first call, return a R/W expanded object that
    5655                 :             :  * is already in the right context, allowing us to do nothing here, and (2) on
    5656                 :             :  * subsequent calls, modify and return that same object, so that control
    5657                 :             :  * doesn't even reach here.  However, if we have a generic transfn that
    5658                 :             :  * returns a new R/W expanded object (probably in the per-tuple context),
    5659                 :             :  * reparenting that result would cause problems.  We'd pass that R/W object to
    5660                 :             :  * the next invocation of the transfn, and then it would be at liberty to
    5661                 :             :  * change or delete that object, and if it deletes it then our own attempt to
    5662                 :             :  * delete the now-old transvalue afterwards would be a double free.  We avoid
    5663                 :             :  * this problem by forcing the stored transvalue to always be a flat
    5664                 :             :  * non-expanded object unless the transfn is visibly doing aggregate-aware
    5665                 :             :  * memory management.  This is somewhat inefficient, but the best answer to
    5666                 :             :  * that is to write a smarter transfn.
    5667                 :             :  */
    5668                 :             : Datum
    5669                 :        8681 : ExecAggCopyTransValue(AggState *aggstate, AggStatePerTrans pertrans,
    5670                 :             :                                           Datum newValue, bool newValueIsNull,
    5671                 :             :                                           Datum oldValue, bool oldValueIsNull)
    5672                 :             : {
    5673         [ +  - ]:        8681 :         Assert(newValue != oldValue);
    5674                 :             : 
    5675         [ -  + ]:        8681 :         if (!newValueIsNull)
    5676                 :             :         {
    5677                 :        8681 :                 MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory);
    5678         [ +  + ]:        8681 :                 if (DatumIsReadWriteExpandedObject(newValue,
    5679                 :             :                                                                                    false,
    5680         [ +  + ]:       17161 :                                                                                    pertrans->transtypeLen) &&
    5681                 :       16961 :                         MemoryContextGetParent(DatumGetEOHP(newValue)->eoh_context) == CurrentMemoryContext)
    5682                 :             :                          /* do nothing */ ;
    5683                 :             :                 else
    5684                 :       16560 :                         newValue = datumCopy(newValue,
    5685                 :        8280 :                                                                  pertrans->transtypeByVal,
    5686                 :        8280 :                                                                  pertrans->transtypeLen);
    5687                 :        8281 :         }
    5688                 :             :         else
    5689                 :             :         {
    5690                 :             :                 /*
    5691                 :             :                  * Ensure that AggStatePerGroup->transValue ends up being 0, so
    5692                 :             :                  * callers can safely compare newValue/oldValue without having to
    5693                 :             :                  * check their respective nullness.
    5694                 :             :                  */
    5695                 :           0 :                 newValue = (Datum) 0;
    5696                 :             :         }
    5697                 :             : 
    5698         [ +  + ]:        8281 :         if (!oldValueIsNull)
    5699                 :             :         {
    5700   [ +  +  +  + ]:       25586 :                 if (DatumIsReadWriteExpandedObject(oldValue,
    5701                 :             :                                                                                    false,
    5702                 :             :                                                                                    pertrans->transtypeLen))
    5703                 :       16924 :                         DeleteExpandedObject(oldValue);
    5704                 :             :                 else
    5705                 :        8662 :                         pfree(DatumGetPointer(oldValue));
    5706                 :        8662 :         }
    5707                 :             : 
    5708                 :       25967 :         return newValue;
    5709                 :             : }
    5710                 :             : 
    5711                 :             : /*
    5712                 :             :  * ExecEvalPreOrderedDistinctSingle
    5713                 :             :  *              Returns true when the aggregate transition value Datum is distinct
    5714                 :             :  *              from the previous input Datum and returns false when the input Datum
    5715                 :             :  *              matches the previous input Datum.
    5716                 :             :  */
    5717                 :             : bool
    5718                 :       60577 : ExecEvalPreOrderedDistinctSingle(AggState *aggstate, AggStatePerTrans pertrans)
    5719                 :             : {
    5720                 :       60577 :         Datum           value = pertrans->transfn_fcinfo->args[1].value;
    5721                 :       60577 :         bool            isnull = pertrans->transfn_fcinfo->args[1].isnull;
    5722                 :             : 
    5723         [ +  + ]:       60577 :         if (!pertrans->haslast ||
    5724   [ +  +  +  + ]:      115067 :                 pertrans->lastisnull != isnull ||
    5725         [ +  + ]:       57532 :                 (!isnull && !DatumGetBool(FunctionCall2Coll(&pertrans->equalfnOne,
    5726                 :       57531 :                                                                                                         pertrans->aggCollation,
    5727                 :       57531 :                                                                                                         pertrans->lastdatum, value))))
    5728                 :             :         {
    5729   [ +  +  +  +  :       16655 :                 if (pertrans->haslast && !pertrans->inputtypeByVal &&
                   -  + ]
    5730                 :        4003 :                         !pertrans->lastisnull)
    5731                 :        4003 :                         pfree(DatumGetPointer(pertrans->lastdatum));
    5732                 :             : 
    5733                 :       16655 :                 pertrans->haslast = true;
    5734         [ +  + ]:       16655 :                 if (!isnull)
    5735                 :             :                 {
    5736                 :       16650 :                         MemoryContext oldContext;
    5737                 :             : 
    5738                 :       16650 :                         oldContext = MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory);
    5739                 :             : 
    5740                 :       33300 :                         pertrans->lastdatum = datumCopy(value, pertrans->inputtypeByVal,
    5741                 :       16650 :                                                                                         pertrans->inputtypeLen);
    5742                 :             : 
    5743                 :       16650 :                         MemoryContextSwitchTo(oldContext);
    5744                 :       16650 :                 }
    5745                 :             :                 else
    5746                 :           5 :                         pertrans->lastdatum = (Datum) 0;
    5747                 :       16655 :                 pertrans->lastisnull = isnull;
    5748                 :       16655 :                 return true;
    5749                 :             :         }
    5750                 :             : 
    5751                 :       43922 :         return false;
    5752                 :       60577 : }
    5753                 :             : 
    5754                 :             : /*
    5755                 :             :  * ExecEvalPreOrderedDistinctMulti
    5756                 :             :  *              Returns true when the aggregate input is distinct from the previous
    5757                 :             :  *              input and returns false when the input matches the previous input, or
    5758                 :             :  *              when there was no previous input.
    5759                 :             :  */
    5760                 :             : bool
    5761                 :         120 : ExecEvalPreOrderedDistinctMulti(AggState *aggstate, AggStatePerTrans pertrans)
    5762                 :             : {
    5763                 :         120 :         ExprContext *tmpcontext = aggstate->tmpcontext;
    5764                 :         120 :         bool            isdistinct = false; /* for now */
    5765                 :         120 :         TupleTableSlot *save_outer;
    5766                 :         120 :         TupleTableSlot *save_inner;
    5767                 :             : 
    5768         [ +  + ]:         470 :         for (int i = 0; i < pertrans->numTransInputs; i++)
    5769                 :             :         {
    5770                 :         350 :                 pertrans->sortslot->tts_values[i] = pertrans->transfn_fcinfo->args[i + 1].value;
    5771                 :         350 :                 pertrans->sortslot->tts_isnull[i] = pertrans->transfn_fcinfo->args[i + 1].isnull;
    5772                 :         350 :         }
    5773                 :             : 
    5774                 :         120 :         ExecClearTuple(pertrans->sortslot);
    5775                 :         120 :         pertrans->sortslot->tts_nvalid = pertrans->numInputs;
    5776                 :         120 :         ExecStoreVirtualTuple(pertrans->sortslot);
    5777                 :             : 
    5778                 :             :         /* save the previous slots before we overwrite them */
    5779                 :         120 :         save_outer = tmpcontext->ecxt_outertuple;
    5780                 :         120 :         save_inner = tmpcontext->ecxt_innertuple;
    5781                 :             : 
    5782                 :         120 :         tmpcontext->ecxt_outertuple = pertrans->sortslot;
    5783                 :         120 :         tmpcontext->ecxt_innertuple = pertrans->uniqslot;
    5784                 :             : 
    5785   [ +  +  +  + ]:         120 :         if (!pertrans->haslast ||
    5786                 :         104 :                 !ExecQual(pertrans->equalfnMulti, tmpcontext))
    5787                 :             :         {
    5788         [ +  + ]:          52 :                 if (pertrans->haslast)
    5789                 :          36 :                         ExecClearTuple(pertrans->uniqslot);
    5790                 :             : 
    5791                 :          52 :                 pertrans->haslast = true;
    5792                 :          52 :                 ExecCopySlot(pertrans->uniqslot, pertrans->sortslot);
    5793                 :             : 
    5794                 :          52 :                 isdistinct = true;
    5795                 :          52 :         }
    5796                 :             : 
    5797                 :             :         /* restore the original slots */
    5798                 :         120 :         tmpcontext->ecxt_outertuple = save_outer;
    5799                 :         120 :         tmpcontext->ecxt_innertuple = save_inner;
    5800                 :             : 
    5801                 :         240 :         return isdistinct;
    5802                 :         120 : }
    5803                 :             : 
    5804                 :             : /*
    5805                 :             :  * Invoke ordered transition function, with a datum argument.
    5806                 :             :  */
    5807                 :             : void
    5808                 :      137362 : ExecEvalAggOrderedTransDatum(ExprState *state, ExprEvalStep *op,
    5809                 :             :                                                          ExprContext *econtext)
    5810                 :             : {
    5811                 :      137362 :         AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
    5812                 :      137362 :         int                     setno = op->d.agg_trans.setno;
    5813                 :             : 
    5814                 :      274724 :         tuplesort_putdatum(pertrans->sortstates[setno],
    5815                 :      137362 :                                            *op->resvalue, *op->resnull);
    5816                 :      137362 : }
    5817                 :             : 
    5818                 :             : /*
    5819                 :             :  * Invoke ordered transition function, with a tuple argument.
    5820                 :             :  */
    5821                 :             : void
    5822                 :          36 : ExecEvalAggOrderedTransTuple(ExprState *state, ExprEvalStep *op,
    5823                 :             :                                                          ExprContext *econtext)
    5824                 :             : {
    5825                 :          36 :         AggStatePerTrans pertrans = op->d.agg_trans.pertrans;
    5826                 :          36 :         int                     setno = op->d.agg_trans.setno;
    5827                 :             : 
    5828                 :          36 :         ExecClearTuple(pertrans->sortslot);
    5829                 :          36 :         pertrans->sortslot->tts_nvalid = pertrans->numInputs;
    5830                 :          36 :         ExecStoreVirtualTuple(pertrans->sortslot);
    5831                 :          36 :         tuplesort_puttupleslot(pertrans->sortstates[setno], pertrans->sortslot);
    5832                 :          36 : }
    5833                 :             : 
    5834                 :             : /* implementation of transition function invocation for byval types */
    5835                 :             : static pg_attribute_always_inline void
    5836                 :           0 : ExecAggPlainTransByVal(AggState *aggstate, AggStatePerTrans pertrans,
    5837                 :             :                                            AggStatePerGroup pergroup,
    5838                 :             :                                            ExprContext *aggcontext, int setno)
    5839                 :             : {
    5840                 :           0 :         FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
    5841                 :           0 :         MemoryContext oldContext;
    5842                 :           0 :         Datum           newVal;
    5843                 :             : 
    5844                 :             :         /* cf. select_current_set() */
    5845                 :           0 :         aggstate->curaggcontext = aggcontext;
    5846                 :           0 :         aggstate->current_set = setno;
    5847                 :             : 
    5848                 :             :         /* set up aggstate->curpertrans for AggGetAggref() */
    5849                 :           0 :         aggstate->curpertrans = pertrans;
    5850                 :             : 
    5851                 :             :         /* invoke transition function in per-tuple context */
    5852                 :           0 :         oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
    5853                 :             : 
    5854                 :           0 :         fcinfo->args[0].value = pergroup->transValue;
    5855                 :           0 :         fcinfo->args[0].isnull = pergroup->transValueIsNull;
    5856                 :           0 :         fcinfo->isnull = false;              /* just in case transfn doesn't set it */
    5857                 :             : 
    5858                 :           0 :         newVal = FunctionCallInvoke(fcinfo);
    5859                 :             : 
    5860                 :           0 :         pergroup->transValue = newVal;
    5861                 :           0 :         pergroup->transValueIsNull = fcinfo->isnull;
    5862                 :             : 
    5863                 :           0 :         MemoryContextSwitchTo(oldContext);
    5864                 :           0 : }
    5865                 :             : 
    5866                 :             : /* implementation of transition function invocation for byref types */
    5867                 :             : static pg_attribute_always_inline void
    5868                 :           0 : ExecAggPlainTransByRef(AggState *aggstate, AggStatePerTrans pertrans,
    5869                 :             :                                            AggStatePerGroup pergroup,
    5870                 :             :                                            ExprContext *aggcontext, int setno)
    5871                 :             : {
    5872                 :           0 :         FunctionCallInfo fcinfo = pertrans->transfn_fcinfo;
    5873                 :           0 :         MemoryContext oldContext;
    5874                 :           0 :         Datum           newVal;
    5875                 :             : 
    5876                 :             :         /* cf. select_current_set() */
    5877                 :           0 :         aggstate->curaggcontext = aggcontext;
    5878                 :           0 :         aggstate->current_set = setno;
    5879                 :             : 
    5880                 :             :         /* set up aggstate->curpertrans for AggGetAggref() */
    5881                 :           0 :         aggstate->curpertrans = pertrans;
    5882                 :             : 
    5883                 :             :         /* invoke transition function in per-tuple context */
    5884                 :           0 :         oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory);
    5885                 :             : 
    5886                 :           0 :         fcinfo->args[0].value = pergroup->transValue;
    5887                 :           0 :         fcinfo->args[0].isnull = pergroup->transValueIsNull;
    5888                 :           0 :         fcinfo->isnull = false;              /* just in case transfn doesn't set it */
    5889                 :             : 
    5890                 :           0 :         newVal = FunctionCallInvoke(fcinfo);
    5891                 :             : 
    5892                 :             :         /*
    5893                 :             :          * For pass-by-ref datatype, must copy the new value into aggcontext and
    5894                 :             :          * free the prior transValue.  But if transfn returned a pointer to its
    5895                 :             :          * first input, we don't need to do anything.
    5896                 :             :          *
    5897                 :             :          * It's safe to compare newVal with pergroup->transValue without regard
    5898                 :             :          * for either being NULL, because ExecAggCopyTransValue takes care to set
    5899                 :             :          * transValue to 0 when NULL. Otherwise we could end up accidentally not
    5900                 :             :          * reparenting, when the transValue has the same numerical value as
    5901                 :             :          * newValue, despite being NULL.  This is a somewhat hot path, making it
    5902                 :             :          * undesirable to instead solve this with another branch for the common
    5903                 :             :          * case of the transition function returning its (modified) input
    5904                 :             :          * argument.
    5905                 :             :          */
    5906         [ #  # ]:           0 :         if (DatumGetPointer(newVal) != DatumGetPointer(pergroup->transValue))
    5907                 :           0 :                 newVal = ExecAggCopyTransValue(aggstate, pertrans,
    5908                 :           0 :                                                                            newVal, fcinfo->isnull,
    5909                 :           0 :                                                                            pergroup->transValue,
    5910                 :           0 :                                                                            pergroup->transValueIsNull);
    5911                 :             : 
    5912                 :           0 :         pergroup->transValue = newVal;
    5913                 :           0 :         pergroup->transValueIsNull = fcinfo->isnull;
    5914                 :             : 
    5915                 :           0 :         MemoryContextSwitchTo(oldContext);
    5916                 :           0 : }
        

Generated by: LCOV version 2.3.2-1