LCOV - differential code coverage report
Current view: top level - src/backend/nodes - nodeFuncs.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 70.4 % 2489 1753 227 203 209 97 186 616 276 675 388 744 65 131
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 32 32 21 11 31 1
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * nodeFuncs.c
       4                 :  *      Various general-purpose manipulations of Node trees
       5                 :  *
       6                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       7                 :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :  *
       9                 :  *
      10                 :  * IDENTIFICATION
      11                 :  *    src/backend/nodes/nodeFuncs.c
      12                 :  *
      13                 :  *-------------------------------------------------------------------------
      14                 :  */
      15                 : #include "postgres.h"
      16                 : 
      17                 : #include "catalog/pg_collation.h"
      18                 : #include "catalog/pg_type.h"
      19                 : #include "miscadmin.h"
      20                 : #include "nodes/execnodes.h"
      21                 : #include "nodes/makefuncs.h"
      22                 : #include "nodes/nodeFuncs.h"
      23                 : #include "nodes/pathnodes.h"
      24                 : #include "utils/builtins.h"
      25                 : #include "utils/lsyscache.h"
      26                 : 
      27                 : static bool expression_returns_set_walker(Node *node, void *context);
      28                 : static int  leftmostLoc(int loc1, int loc2);
      29                 : static bool fix_opfuncids_walker(Node *node, void *context);
      30                 : static bool planstate_walk_subplans(List *plans,
      31                 :                                     planstate_tree_walker_callback walker,
      32                 :                                     void *context);
      33                 : static bool planstate_walk_members(PlanState **planstates, int nplans,
      34                 :                                    planstate_tree_walker_callback walker,
      35                 :                                    void *context);
      36                 : 
      37                 : 
      38                 : /*
      39                 :  *  exprType -
      40                 :  *    returns the Oid of the type of the expression's result.
      41                 :  */
      42                 : Oid
      43 GIC    16213293 : exprType(const Node *expr)
      44                 : {
      45 ECB             :     Oid         type;
      46                 : 
      47 GIC    16213293 :     if (!expr)
      48 UIC           0 :         return InvalidOid;
      49 ECB             : 
      50 GBC    16213293 :     switch (nodeTag(expr))
      51                 :     {
      52 CBC     7334749 :         case T_Var:
      53 GIC     7334749 :             type = ((const Var *) expr)->vartype;
      54 CBC     7334749 :             break;
      55         2589997 :         case T_Const:
      56         2589997 :             type = ((const Const *) expr)->consttype;
      57         2589997 :             break;
      58         1197994 :         case T_Param:
      59         1197994 :             type = ((const Param *) expr)->paramtype;
      60         1197994 :             break;
      61          132047 :         case T_Aggref:
      62          132047 :             type = ((const Aggref *) expr)->aggtype;
      63          132047 :             break;
      64            1028 :         case T_GroupingFunc:
      65            1028 :             type = INT4OID;
      66            1028 :             break;
      67            8520 :         case T_WindowFunc:
      68            8520 :             type = ((const WindowFunc *) expr)->wintype;
      69            8520 :             break;
      70           56504 :         case T_SubscriptingRef:
      71           56504 :             type = ((const SubscriptingRef *) expr)->refrestype;
      72           56504 :             break;
      73         1441008 :         case T_FuncExpr:
      74         1441008 :             type = ((const FuncExpr *) expr)->funcresulttype;
      75         1441008 :             break;
      76           34616 :         case T_NamedArgExpr:
      77           34616 :             type = exprType((Node *) ((const NamedArgExpr *) expr)->arg);
      78           34616 :             break;
      79         1060839 :         case T_OpExpr:
      80         1060839 :             type = ((const OpExpr *) expr)->opresulttype;
      81         1060839 :             break;
      82             896 :         case T_DistinctExpr:
      83             896 :             type = ((const DistinctExpr *) expr)->opresulttype;
      84             896 :             break;
      85            1067 :         case T_NullIfExpr:
      86            1067 :             type = ((const NullIfExpr *) expr)->opresulttype;
      87            1067 :             break;
      88           81595 :         case T_ScalarArrayOpExpr:
      89           81595 :             type = BOOLOID;
      90           81595 :             break;
      91          242815 :         case T_BoolExpr:
      92          242815 :             type = BOOLOID;
      93          242815 :             break;
      94           73708 :         case T_SubLink:
      95 ECB             :             {
      96 CBC       73708 :                 const SubLink *sublink = (const SubLink *) expr;
      97                 : 
      98           73708 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
      99 GIC       27080 :                     sublink->subLinkType == ARRAY_SUBLINK)
     100 CBC       57257 :                 {
     101 ECB             :                     /* get the type of the subselect's first target column */
     102 CBC       57257 :                     Query      *qtree = (Query *) sublink->subselect;
     103                 :                     TargetEntry *tent;
     104 ECB             : 
     105 GIC       57257 :                     if (!qtree || !IsA(qtree, Query))
     106 UIC           0 :                         elog(ERROR, "cannot get type for untransformed sublink");
     107 CBC       57257 :                     tent = linitial_node(TargetEntry, qtree->targetList);
     108 GBC       57257 :                     Assert(!tent->resjunk);
     109 CBC       57257 :                     type = exprType((Node *) tent->expr);
     110           57257 :                     if (sublink->subLinkType == ARRAY_SUBLINK)
     111 ECB             :                     {
     112 CBC       10629 :                         type = get_promoted_array_type(type);
     113 GIC       10629 :                         if (!OidIsValid(type))
     114 LBC           0 :                             ereport(ERROR,
     115 ECB             :                                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     116 EUB             :                                      errmsg("could not find array type for data type %s",
     117                 :                                             format_type_be(exprType((Node *) tent->expr)))));
     118                 :                     }
     119                 :                 }
     120 GIC       16451 :                 else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
     121                 :                 {
     122 ECB             :                     /* MULTIEXPR is always considered to return RECORD */
     123 GIC          66 :                     type = RECORDOID;
     124                 :                 }
     125 ECB             :                 else
     126                 :                 {
     127                 :                     /* for all other sublink types, result is boolean */
     128 GIC       16385 :                     type = BOOLOID;
     129                 :                 }
     130 ECB             :             }
     131 GIC       73708 :             break;
     132           18773 :         case T_SubPlan:
     133 ECB             :             {
     134 CBC       18773 :                 const SubPlan *subplan = (const SubPlan *) expr;
     135                 : 
     136           18773 :                 if (subplan->subLinkType == EXPR_SUBLINK ||
     137 GIC        1626 :                     subplan->subLinkType == ARRAY_SUBLINK)
     138 ECB             :                 {
     139                 :                     /* get the type of the subselect's first target column */
     140 GIC       17345 :                     type = subplan->firstColType;
     141           17345 :                     if (subplan->subLinkType == ARRAY_SUBLINK)
     142 ECB             :                     {
     143 CBC         198 :                         type = get_promoted_array_type(type);
     144 GIC         198 :                         if (!OidIsValid(type))
     145 LBC           0 :                             ereport(ERROR,
     146 ECB             :                                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     147 EUB             :                                      errmsg("could not find array type for data type %s",
     148                 :                                             format_type_be(subplan->firstColType))));
     149                 :                     }
     150                 :                 }
     151 GIC        1428 :                 else if (subplan->subLinkType == MULTIEXPR_SUBLINK)
     152                 :                 {
     153 ECB             :                     /* MULTIEXPR is always considered to return RECORD */
     154 GIC          90 :                     type = RECORDOID;
     155                 :                 }
     156 ECB             :                 else
     157                 :                 {
     158                 :                     /* for all other subplan types, result is boolean */
     159 GIC        1338 :                     type = BOOLOID;
     160                 :                 }
     161 ECB             :             }
     162 GIC       18773 :             break;
     163             499 :         case T_AlternativeSubPlan:
     164 ECB             :             {
     165 CBC         499 :                 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
     166                 : 
     167 ECB             :                 /* subplans should all return the same thing */
     168 GIC         499 :                 type = exprType((Node *) linitial(asplan->subplans));
     169                 :             }
     170 CBC         499 :             break;
     171 GIC       79965 :         case T_FieldSelect:
     172 CBC       79965 :             type = ((const FieldSelect *) expr)->resulttype;
     173           79965 :             break;
     174             383 :         case T_FieldStore:
     175             383 :             type = ((const FieldStore *) expr)->resulttype;
     176             383 :             break;
     177          250206 :         case T_RelabelType:
     178          250206 :             type = ((const RelabelType *) expr)->resulttype;
     179          250206 :             break;
     180           63400 :         case T_CoerceViaIO:
     181           63400 :             type = ((const CoerceViaIO *) expr)->resulttype;
     182           63400 :             break;
     183            7498 :         case T_ArrayCoerceExpr:
     184            7498 :             type = ((const ArrayCoerceExpr *) expr)->resulttype;
     185            7498 :             break;
     186            1104 :         case T_ConvertRowtypeExpr:
     187            1104 :             type = ((const ConvertRowtypeExpr *) expr)->resulttype;
     188            1104 :             break;
     189            4056 :         case T_CollateExpr:
     190            4056 :             type = exprType((Node *) ((const CollateExpr *) expr)->arg);
     191            4056 :             break;
     192          206614 :         case T_CaseExpr:
     193          206614 :             type = ((const CaseExpr *) expr)->casetype;
     194          206614 :             break;
     195           38438 :         case T_CaseTestExpr:
     196           38438 :             type = ((const CaseTestExpr *) expr)->typeId;
     197           38438 :             break;
     198           80055 :         case T_ArrayExpr:
     199           80055 :             type = ((const ArrayExpr *) expr)->array_typeid;
     200           80055 :             break;
     201            6836 :         case T_RowExpr:
     202            6836 :             type = ((const RowExpr *) expr)->row_typeid;
     203            6836 :             break;
     204             174 :         case T_RowCompareExpr:
     205             174 :             type = BOOLOID;
     206             174 :             break;
     207           30548 :         case T_CoalesceExpr:
     208           30548 :             type = ((const CoalesceExpr *) expr)->coalescetype;
     209           30548 :             break;
     210            3084 :         case T_MinMaxExpr:
     211            3084 :             type = ((const MinMaxExpr *) expr)->minmaxtype;
     212            3084 :             break;
     213           12454 :         case T_XmlExpr:
     214           12454 :             if (((const XmlExpr *) expr)->op == IS_DOCUMENT)
     215              51 :                 type = BOOLOID;
     216           12403 :             else if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
     217 GIC         405 :                 type = TEXTOID;
     218 ECB             :             else
     219 CBC       11998 :                 type = XMLOID;
     220           12454 :             break;
     221 GNC         402 :         case T_JsonValueExpr:
     222                 :             {
     223             402 :                 const JsonValueExpr *jve = (const JsonValueExpr *) expr;
     224                 : 
     225             402 :                 type = exprType((Node *)
     226             402 :                                 (jve->formatted_expr ? jve->formatted_expr :
     227                 :                                  jve->raw_expr));
     228                 :             }
     229             402 :             break;
     230            1997 :         case T_JsonConstructorExpr:
     231            1997 :             type = ((const JsonConstructorExpr *) expr)->returning->typid;
     232            1997 :             break;
     233             730 :         case T_JsonIsPredicate:
     234             730 :             type = BOOLOID;
     235             730 :             break;
     236 GIC       34254 :         case T_NullTest:
     237 CBC       34254 :             type = BOOLOID;
     238 GIC       34254 :             break;
     239 CBC         890 :         case T_BooleanTest:
     240             890 :             type = BOOLOID;
     241 GIC         890 :             break;
     242          917761 :         case T_CoerceToDomain:
     243 CBC      917761 :             type = ((const CoerceToDomain *) expr)->resulttype;
     244          917761 :             break;
     245            2185 :         case T_CoerceToDomainValue:
     246            2185 :             type = ((const CoerceToDomainValue *) expr)->typeId;
     247            2185 :             break;
     248          188522 :         case T_SetToDefault:
     249          188522 :             type = ((const SetToDefault *) expr)->typeId;
     250          188522 :             break;
     251             121 :         case T_CurrentOfExpr:
     252             121 :             type = BOOLOID;
     253             121 :             break;
     254             557 :         case T_NextValueExpr:
     255             557 :             type = ((const NextValueExpr *) expr)->typeId;
     256             557 :             break;
     257 LBC           0 :         case T_InferenceElem:
     258 ECB             :             {
     259 LBC           0 :                 const InferenceElem *n = (const InferenceElem *) expr;
     260 ECB             : 
     261 LBC           0 :                 type = exprType((Node *) n->expr);
     262 ECB             :             }
     263 LBC           0 :             break;
     264 CBC        4404 :         case T_PlaceHolderVar:
     265            4404 :             type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr);
     266            4404 :             break;
     267 LBC           0 :         default:
     268               0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
     269 ECB             :             type = InvalidOid;  /* keep compiler quiet */
     270                 :             break;
     271 EUB             :     }
     272 GIC    16213293 :     return type;
     273 EUB             : }
     274                 : 
     275                 : /*
     276                 :  *  exprTypmod -
     277                 :  *    returns the type-specific modifier of the expression's result type,
     278 ECB             :  *    if it can be determined.  In many cases, it can't and we return -1.
     279                 :  */
     280                 : int32
     281 GBC     5892387 : exprTypmod(const Node *expr)
     282 EUB             : {
     283 GIC     5892387 :     if (!expr)
     284 UIC           0 :         return -1;
     285                 : 
     286 CBC     5892387 :     switch (nodeTag(expr))
     287                 :     {
     288 GIC     2935692 :         case T_Var:
     289         2935692 :             return ((const Var *) expr)->vartypmod;
     290          933028 :         case T_Const:
     291          933028 :             return ((const Const *) expr)->consttypmod;
     292          131554 :         case T_Param:
     293          131554 :             return ((const Param *) expr)->paramtypmod;
     294           13018 :         case T_SubscriptingRef:
     295 CBC       13018 :             return ((const SubscriptingRef *) expr)->reftypmod;
     296 GIC      701116 :         case T_FuncExpr:
     297 ECB             :             {
     298 EUB             :                 int32       coercedTypmod;
     299                 : 
     300 ECB             :                 /* Be smart about length-coercion functions... */
     301 GIC      701116 :                 if (exprIsLengthCoercion(expr, &coercedTypmod))
     302 CBC       13965 :                     return coercedTypmod;
     303 ECB             :             }
     304 CBC      687151 :             break;
     305 LBC           0 :         case T_NamedArgExpr:
     306               0 :             return exprTypmod((Node *) ((const NamedArgExpr *) expr)->arg);
     307 CBC         143 :         case T_NullIfExpr:
     308 ECB             :             {
     309                 :                 /*
     310                 :                  * Result is either first argument or NULL, so we can report
     311                 :                  * first argument's typmod if known.
     312                 :                  */
     313 GIC         143 :                 const NullIfExpr *nexpr = (const NullIfExpr *) expr;
     314                 : 
     315 CBC         143 :                 return exprTypmod((Node *) linitial(nexpr->args));
     316 ECB             :             }
     317                 :             break;
     318 CBC       12297 :         case T_SubLink:
     319 EUB             :             {
     320 GBC       12297 :                 const SubLink *sublink = (const SubLink *) expr;
     321 ECB             : 
     322 GIC       12297 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
     323            1909 :                     sublink->subLinkType == ARRAY_SUBLINK)
     324                 :                 {
     325                 :                     /* get the typmod of the subselect's first target column */
     326           12260 :                     Query      *qtree = (Query *) sublink->subselect;
     327 ECB             :                     TargetEntry *tent;
     328                 : 
     329 CBC       12260 :                     if (!qtree || !IsA(qtree, Query))
     330 UIC           0 :                         elog(ERROR, "cannot get type for untransformed sublink");
     331 GIC       12260 :                     tent = linitial_node(TargetEntry, qtree->targetList);
     332 CBC       12260 :                     Assert(!tent->resjunk);
     333 GIC       12260 :                     return exprTypmod((Node *) tent->expr);
     334 ECB             :                     /* note we don't need to care if it's an array */
     335                 :                 }
     336                 :                 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
     337                 :             }
     338 GIC          37 :             break;
     339           12905 :         case T_SubPlan:
     340 ECB             :             {
     341 GIC       12905 :                 const SubPlan *subplan = (const SubPlan *) expr;
     342                 : 
     343 CBC       12905 :                 if (subplan->subLinkType == EXPR_SUBLINK ||
     344 GBC         885 :                     subplan->subLinkType == ARRAY_SUBLINK)
     345 ECB             :                 {
     346                 :                     /* get the typmod of the subselect's first target column */
     347                 :                     /* note we don't need to care if it's an array */
     348 GIC       12155 :                     return subplan->firstColTypmod;
     349                 :                 }
     350                 :                 /* otherwise, result is RECORD or BOOLEAN, typmod is -1 */
     351                 :             }
     352 CBC         750 :             break;
     353             251 :         case T_AlternativeSubPlan:
     354                 :             {
     355             251 :                 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
     356                 : 
     357 ECB             :                 /* subplans should all return the same thing */
     358 CBC         251 :                 return exprTypmod((Node *) linitial(asplan->subplans));
     359                 :             }
     360                 :             break;
     361 GIC       26301 :         case T_FieldSelect:
     362 CBC       26301 :             return ((const FieldSelect *) expr)->resulttypmod;
     363 GIC       68162 :         case T_RelabelType:
     364           68162 :             return ((const RelabelType *) expr)->resulttypmod;
     365            3004 :         case T_ArrayCoerceExpr:
     366 CBC        3004 :             return ((const ArrayCoerceExpr *) expr)->resulttypmod;
     367              76 :         case T_CollateExpr:
     368 GIC          76 :             return exprTypmod((Node *) ((const CollateExpr *) expr)->arg);
     369 CBC       83326 :         case T_CaseExpr:
     370                 :             {
     371                 :                 /*
     372 ECB             :                  * If all the alternatives agree on type/typmod, return that
     373                 :                  * typmod, else use -1
     374                 :                  */
     375 CBC       83326 :                 const CaseExpr *cexpr = (const CaseExpr *) expr;
     376           83326 :                 Oid         casetype = cexpr->casetype;
     377 ECB             :                 int32       typmod;
     378                 :                 ListCell   *arg;
     379                 : 
     380 CBC       83326 :                 if (!cexpr->defresult)
     381 LBC           0 :                     return -1;
     382 CBC       83326 :                 if (exprType((Node *) cexpr->defresult) != casetype)
     383 LBC           0 :                     return -1;
     384 GIC       83326 :                 typmod = exprTypmod((Node *) cexpr->defresult);
     385           83326 :                 if (typmod < 0)
     386           83326 :                     return -1;  /* no point in trying harder */
     387 UIC           0 :                 foreach(arg, cexpr->args)
     388                 :                 {
     389 LBC           0 :                     CaseWhen   *w = lfirst_node(CaseWhen, arg);
     390 ECB             : 
     391 UIC           0 :                     if (exprType((Node *) w->result) != casetype)
     392               0 :                         return -1;
     393               0 :                     if (exprTypmod((Node *) w->result) != typmod)
     394 LBC           0 :                         return -1;
     395 EUB             :                 }
     396 LBC           0 :                 return typmod;
     397 EUB             :             }
     398 ECB             :             break;
     399 CBC        6750 :         case T_CaseTestExpr:
     400            6750 :             return ((const CaseTestExpr *) expr)->typeMod;
     401 GBC       16388 :         case T_ArrayExpr:
     402                 :             {
     403 EUB             :                 /*
     404                 :                  * If all the elements agree on type/typmod, return that
     405                 :                  * typmod, else use -1
     406                 :                  */
     407 GBC       16388 :                 const ArrayExpr *arrayexpr = (const ArrayExpr *) expr;
     408 EUB             :                 Oid         commontype;
     409                 :                 int32       typmod;
     410                 :                 ListCell   *elem;
     411                 : 
     412 GIC       16388 :                 if (arrayexpr->elements == NIL)
     413 CBC          91 :                     return -1;
     414           16297 :                 typmod = exprTypmod((Node *) linitial(arrayexpr->elements));
     415           16297 :                 if (typmod < 0)
     416 GIC       16288 :                     return -1;  /* no point in trying harder */
     417               9 :                 if (arrayexpr->multidims)
     418 UIC           0 :                     commontype = arrayexpr->array_typeid;
     419                 :                 else
     420 GIC           9 :                     commontype = arrayexpr->element_typeid;
     421 CBC          27 :                 foreach(elem, arrayexpr->elements)
     422                 :                 {
     423 GIC          18 :                     Node       *e = (Node *) lfirst(elem);
     424                 : 
     425              18 :                     if (exprType(e) != commontype)
     426 LBC           0 :                         return -1;
     427 CBC          18 :                     if (exprTypmod(e) != typmod)
     428 LBC           0 :                         return -1;
     429 ECB             :                 }
     430 CBC           9 :                 return typmod;
     431 ECB             :             }
     432 EUB             :             break;
     433 GIC        4492 :         case T_CoalesceExpr:
     434 ECB             :             {
     435                 :                 /*
     436                 :                  * If all the alternatives agree on type/typmod, return that
     437                 :                  * typmod, else use -1
     438                 :                  */
     439 CBC        4492 :                 const CoalesceExpr *cexpr = (const CoalesceExpr *) expr;
     440 GBC        4492 :                 Oid         coalescetype = cexpr->coalescetype;
     441 ECB             :                 int32       typmod;
     442 EUB             :                 ListCell   *arg;
     443                 : 
     444 CBC        4492 :                 if (exprType((Node *) linitial(cexpr->args)) != coalescetype)
     445 UIC           0 :                     return -1;
     446 GIC        4492 :                 typmod = exprTypmod((Node *) linitial(cexpr->args));
     447 CBC        4492 :                 if (typmod < 0)
     448 GIC        4492 :                     return -1;  /* no point in trying harder */
     449 UIC           0 :                 for_each_from(arg, cexpr->args, 1)
     450                 :                 {
     451               0 :                     Node       *e = (Node *) lfirst(arg);
     452                 : 
     453 LBC           0 :                     if (exprType(e) != coalescetype)
     454               0 :                         return -1;
     455 UIC           0 :                     if (exprTypmod(e) != typmod)
     456               0 :                         return -1;
     457                 :                 }
     458 LBC           0 :                 return typmod;
     459 EUB             :             }
     460 ECB             :             break;
     461 CBC        1507 :         case T_MinMaxExpr:
     462 ECB             :             {
     463 EUB             :                 /*
     464                 :                  * If all the alternatives agree on type/typmod, return that
     465                 :                  * typmod, else use -1
     466                 :                  */
     467 GBC        1507 :                 const MinMaxExpr *mexpr = (const MinMaxExpr *) expr;
     468            1507 :                 Oid         minmaxtype = mexpr->minmaxtype;
     469 EUB             :                 int32       typmod;
     470                 :                 ListCell   *arg;
     471                 : 
     472 GBC        1507 :                 if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
     473 UIC           0 :                     return -1;
     474 GIC        1507 :                 typmod = exprTypmod((Node *) linitial(mexpr->args));
     475 CBC        1507 :                 if (typmod < 0)
     476 GIC        1507 :                     return -1;  /* no point in trying harder */
     477 UIC           0 :                 for_each_from(arg, mexpr->args, 1)
     478                 :                 {
     479               0 :                     Node       *e = (Node *) lfirst(arg);
     480                 : 
     481 LBC           0 :                     if (exprType(e) != minmaxtype)
     482               0 :                         return -1;
     483 UIC           0 :                     if (exprTypmod(e) != typmod)
     484               0 :                         return -1;
     485                 :                 }
     486 LBC           0 :                 return typmod;
     487 EUB             :             }
     488 ECB             :             break;
     489 GNC           6 :         case T_JsonValueExpr:
     490               6 :             return exprTypmod((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
     491             755 :         case T_JsonConstructorExpr:
     492             755 :             return ((const JsonConstructorExpr *) expr)->returning->typmod;
     493 GBC      696657 :         case T_CoerceToDomain:
     494 GIC      696657 :             return ((const CoerceToDomain *) expr)->resulttypmod;
     495 GBC          32 :         case T_CoerceToDomainValue:
     496 GIC          32 :             return ((const CoerceToDomainValue *) expr)->typeMod;
     497 GBC       47729 :         case T_SetToDefault:
     498           47729 :             return ((const SetToDefault *) expr)->typeMod;
     499            2864 :         case T_PlaceHolderVar:
     500            2864 :             return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr);
     501 GIC      194334 :         default:
     502 GBC      194334 :             break;
     503                 :     }
     504 GIC      882272 :     return -1;
     505 ECB             : }
     506                 : 
     507                 : /*
     508                 :  * exprIsLengthCoercion
     509                 :  *      Detect whether an expression tree is an application of a datatype's
     510                 :  *      typmod-coercion function.  Optionally extract the result's typmod.
     511                 :  *
     512                 :  * If coercedTypmod is not NULL, the typmod is stored there if the expression
     513                 :  * is a length-coercion function, else -1 is stored there.
     514                 :  *
     515                 :  * Note that a combined type-and-length coercion will be treated as a
     516                 :  * length coercion by this routine.
     517                 :  */
     518                 : bool
     519 GIC      701772 : exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod)
     520 ECB             : {
     521 GIC      701772 :     if (coercedTypmod != NULL)
     522          701772 :         *coercedTypmod = -1;    /* default result on failure */
     523                 : 
     524                 :     /*
     525                 :      * Scalar-type length coercions are FuncExprs, array-type length coercions
     526                 :      * are ArrayCoerceExprs
     527                 :      */
     528          701772 :     if (expr && IsA(expr, FuncExpr))
     529                 :     {
     530          701772 :         const FuncExpr *func = (const FuncExpr *) expr;
     531                 :         int         nargs;
     532                 :         Const      *second_arg;
     533                 : 
     534                 :         /*
     535 ECB             :          * If it didn't come from a coercion context, reject.
     536                 :          */
     537 CBC      701772 :         if (func->funcformat != COERCE_EXPLICIT_CAST &&
     538          676902 :             func->funcformat != COERCE_IMPLICIT_CAST)
     539 GIC      550393 :             return false;
     540                 : 
     541                 :         /*
     542                 :          * If it's not a two-argument or three-argument function with the
     543                 :          * second argument being an int4 constant, it can't have been created
     544 ECB             :          * from a length coercion (it must be a type coercion, instead).
     545                 :          */
     546 CBC      151379 :         nargs = list_length(func->args);
     547 GIC      151379 :         if (nargs < 2 || nargs > 3)
     548          137370 :             return false;
     549                 : 
     550           14009 :         second_arg = (Const *) lsecond(func->args);
     551           14009 :         if (!IsA(second_arg, Const) ||
     552           14009 :             second_arg->consttype != INT4OID ||
     553 CBC       14009 :             second_arg->constisnull)
     554 LBC           0 :             return false;
     555 ECB             : 
     556                 :         /*
     557                 :          * OK, it is indeed a length-coercion function.
     558                 :          */
     559 GIC       14009 :         if (coercedTypmod != NULL)
     560           14009 :             *coercedTypmod = DatumGetInt32(second_arg->constvalue);
     561                 : 
     562 CBC       14009 :         return true;
     563 ECB             :     }
     564                 : 
     565 UIC           0 :     if (expr && IsA(expr, ArrayCoerceExpr))
     566 ECB             :     {
     567 LBC           0 :         const ArrayCoerceExpr *acoerce = (const ArrayCoerceExpr *) expr;
     568 ECB             : 
     569                 :         /* It's not a length coercion unless there's a nondefault typmod */
     570 UBC           0 :         if (acoerce->resulttypmod < 0)
     571 UIC           0 :             return false;
     572                 : 
     573                 :         /*
     574                 :          * OK, it is indeed a length-coercion expression.
     575 ECB             :          */
     576 LBC           0 :         if (coercedTypmod != NULL)
     577 UIC           0 :             *coercedTypmod = acoerce->resulttypmod;
     578 ECB             : 
     579 UIC           0 :         return true;
     580                 :     }
     581 EUB             : 
     582 UIC           0 :     return false;
     583 EUB             : }
     584                 : 
     585                 : /*
     586                 :  * applyRelabelType
     587                 :  *      Add a RelabelType node if needed to make the expression expose
     588                 :  *      the specified type, typmod, and collation.
     589                 :  *
     590                 :  * This is primarily intended to be used during planning.  Therefore, it must
     591                 :  * maintain the post-eval_const_expressions invariants that there are not
     592                 :  * adjacent RelabelTypes, and that the tree is fully const-folded (hence,
     593                 :  * we mustn't return a RelabelType atop a Const).  If we do find a Const,
     594                 :  * we'll modify it in-place if "overwrite_ok" is true; that should only be
     595                 :  * passed as true if caller knows the Const is newly generated.
     596                 :  */
     597                 : Node *
     598 GBC      113982 : applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid,
     599                 :                  CoercionForm rformat, int rlocation, bool overwrite_ok)
     600                 : {
     601                 :     /*
     602                 :      * If we find stacked RelabelTypes (eg, from foo::int::oid) we can discard
     603                 :      * all but the top one, and must do so to ensure that semantically
     604                 :      * equivalent expressions are equal().
     605                 :      */
     606 GIC      115469 :     while (arg && IsA(arg, RelabelType))
     607            1487 :         arg = (Node *) ((RelabelType *) arg)->arg;
     608                 : 
     609          113982 :     if (arg && IsA(arg, Const))
     610                 :     {
     611                 :         /* Modify the Const directly to preserve const-flatness. */
     612           75489 :         Const      *con = (Const *) arg;
     613                 : 
     614 CBC       75489 :         if (!overwrite_ok)
     615 GIC        5114 :             con = copyObject(con);
     616           75489 :         con->consttype = rtype;
     617           75489 :         con->consttypmod = rtypmod;
     618           75489 :         con->constcollid = rcollid;
     619                 :         /* We keep the Const's original location. */
     620           75489 :         return (Node *) con;
     621                 :     }
     622 CBC       40566 :     else if (exprType(arg) == rtype &&
     623            4104 :              exprTypmod(arg) == rtypmod &&
     624 GIC        2031 :              exprCollation(arg) == rcollid)
     625 ECB             :     {
     626                 :         /* Sometimes we find a nest of relabels that net out to nothing. */
     627 GIC        1134 :         return arg;
     628 ECB             :     }
     629                 :     else
     630                 :     {
     631                 :         /* Nope, gotta have a RelabelType. */
     632 CBC       37359 :         RelabelType *newrelabel = makeNode(RelabelType);
     633 ECB             : 
     634 CBC       37359 :         newrelabel->arg = (Expr *) arg;
     635 GIC       37359 :         newrelabel->resulttype = rtype;
     636 CBC       37359 :         newrelabel->resulttypmod = rtypmod;
     637 GIC       37359 :         newrelabel->resultcollid = rcollid;
     638 CBC       37359 :         newrelabel->relabelformat = rformat;
     639           37359 :         newrelabel->location = rlocation;
     640           37359 :         return (Node *) newrelabel;
     641                 :     }
     642                 : }
     643 ECB             : 
     644                 : /*
     645                 :  * relabel_to_typmod
     646                 :  *      Add a RelabelType node that changes just the typmod of the expression.
     647                 :  *
     648                 :  * Convenience function for a common usage of applyRelabelType.
     649                 :  */
     650                 : Node *
     651 CBC          18 : relabel_to_typmod(Node *expr, int32 typmod)
     652 ECB             : {
     653 CBC          18 :     return applyRelabelType(expr, exprType(expr), typmod, exprCollation(expr),
     654 ECB             :                             COERCE_EXPLICIT_CAST, -1, false);
     655                 : }
     656                 : 
     657                 : /*
     658                 :  * strip_implicit_coercions: remove implicit coercions at top level of tree
     659                 :  *
     660                 :  * This doesn't modify or copy the input expression tree, just return a
     661                 :  * pointer to a suitable place within it.
     662                 :  *
     663                 :  * Note: there isn't any useful thing we can do with a RowExpr here, so
     664                 :  * just return it unchanged, even if it's marked as an implicit coercion.
     665                 :  */
     666                 : Node *
     667 CBC      307203 : strip_implicit_coercions(Node *node)
     668                 : {
     669          307203 :     if (node == NULL)
     670 UIC           0 :         return NULL;
     671 GIC      307203 :     if (IsA(node, FuncExpr))
     672                 :     {
     673            6136 :         FuncExpr   *f = (FuncExpr *) node;
     674                 : 
     675            6136 :         if (f->funcformat == COERCE_IMPLICIT_CAST)
     676              22 :             return strip_implicit_coercions(linitial(f->args));
     677                 :     }
     678          301067 :     else if (IsA(node, RelabelType))
     679                 :     {
     680            5086 :         RelabelType *r = (RelabelType *) node;
     681                 : 
     682            5086 :         if (r->relabelformat == COERCE_IMPLICIT_CAST)
     683 CBC           7 :             return strip_implicit_coercions((Node *) r->arg);
     684                 :     }
     685          295981 :     else if (IsA(node, CoerceViaIO))
     686 EUB             :     {
     687 CBC         282 :         CoerceViaIO *c = (CoerceViaIO *) node;
     688                 : 
     689             282 :         if (c->coerceformat == COERCE_IMPLICIT_CAST)
     690 UIC           0 :             return strip_implicit_coercions((Node *) c->arg);
     691 ECB             :     }
     692 CBC      295699 :     else if (IsA(node, ArrayCoerceExpr))
     693                 :     {
     694 LBC           0 :         ArrayCoerceExpr *c = (ArrayCoerceExpr *) node;
     695                 : 
     696               0 :         if (c->coerceformat == COERCE_IMPLICIT_CAST)
     697 UIC           0 :             return strip_implicit_coercions((Node *) c->arg);
     698 ECB             :     }
     699 CBC      295699 :     else if (IsA(node, ConvertRowtypeExpr))
     700                 :     {
     701 LBC           0 :         ConvertRowtypeExpr *c = (ConvertRowtypeExpr *) node;
     702                 : 
     703               0 :         if (c->convertformat == COERCE_IMPLICIT_CAST)
     704 UIC           0 :             return strip_implicit_coercions((Node *) c->arg);
     705 ECB             :     }
     706 GBC      295699 :     else if (IsA(node, CoerceToDomain))
     707                 :     {
     708 CBC       28179 :         CoerceToDomain *c = (CoerceToDomain *) node;
     709                 : 
     710 GBC       28179 :         if (c->coercionformat == COERCE_IMPLICIT_CAST)
     711 UIC           0 :             return strip_implicit_coercions((Node *) c->arg);
     712 EUB             :     }
     713 GBC      307174 :     return node;
     714                 : }
     715 ECB             : 
     716                 : /*
     717 EUB             :  * expression_returns_set
     718                 :  *    Test whether an expression returns a set result.
     719                 :  *
     720                 :  * Because we use expression_tree_walker(), this can also be applied to
     721                 :  * whole targetlists; it'll produce true if any one of the tlist items
     722 ECB             :  * returns a set.
     723                 :  */
     724                 : bool
     725 GIC      665090 : expression_returns_set(Node *clause)
     726 ECB             : {
     727 GBC      665090 :     return expression_returns_set_walker(clause, NULL);
     728                 : }
     729 ECB             : 
     730                 : static bool
     731 GIC     3022300 : expression_returns_set_walker(Node *node, void *context)
     732                 : {
     733         3022300 :     if (node == NULL)
     734           22031 :         return false;
     735         3000269 :     if (IsA(node, FuncExpr))
     736                 :     {
     737          131610 :         FuncExpr   *expr = (FuncExpr *) node;
     738                 : 
     739          131610 :         if (expr->funcretset)
     740            3892 :             return true;
     741 ECB             :         /* else fall through to check args */
     742                 :     }
     743 CBC     2996377 :     if (IsA(node, OpExpr))
     744                 :     {
     745 GIC      623161 :         OpExpr     *expr = (OpExpr *) node;
     746                 : 
     747 CBC      623161 :         if (expr->opretset)
     748 GIC           3 :             return true;
     749 ECB             :         /* else fall through to check args */
     750                 :     }
     751                 : 
     752                 :     /*
     753                 :      * If you add any more cases that return sets, also fix
     754                 :      * expression_returns_set_rows() in clauses.c and IS_SRF_CALL() in
     755                 :      * tlist.c.
     756                 :      */
     757                 : 
     758                 :     /* Avoid recursion for some cases that parser checks not to return a set */
     759 CBC     2996374 :     if (IsA(node, Aggref))
     760 GIC         490 :         return false;
     761 CBC     2995884 :     if (IsA(node, GroupingFunc))
     762 GIC          18 :         return false;
     763 CBC     2995866 :     if (IsA(node, WindowFunc))
     764              15 :         return false;
     765                 : 
     766 GIC     2995851 :     return expression_tree_walker(node, expression_returns_set_walker,
     767                 :                                   context);
     768                 : }
     769                 : 
     770                 : 
     771                 : /*
     772                 :  *  exprCollation -
     773                 :  *    returns the Oid of the collation of the expression's result.
     774                 :  *
     775 ECB             :  * Note: expression nodes that can invoke functions generally have an
     776                 :  * "inputcollid" field, which is what the function should use as collation.
     777                 :  * That is the resolved common collation of the node's inputs.  It is often
     778                 :  * but not always the same as the result collation; in particular, if the
     779                 :  * function produces a non-collatable result type from collatable inputs
     780                 :  * or vice versa, the two are different.
     781                 :  */
     782                 : Oid
     783 GIC     7274479 : exprCollation(const Node *expr)
     784                 : {
     785                 :     Oid         coll;
     786                 : 
     787         7274479 :     if (!expr)
     788 UIC           0 :         return InvalidOid;
     789                 : 
     790 GIC     7274479 :     switch (nodeTag(expr))
     791                 :     {
     792         4915290 :         case T_Var:
     793         4915290 :             coll = ((const Var *) expr)->varcollid;
     794         4915290 :             break;
     795         1345354 :         case T_Const:
     796         1345354 :             coll = ((const Const *) expr)->constcollid;
     797         1345354 :             break;
     798          239070 :         case T_Param:
     799 CBC      239070 :             coll = ((const Param *) expr)->paramcollid;
     800 GIC      239070 :             break;
     801           40851 :         case T_Aggref:
     802           40851 :             coll = ((const Aggref *) expr)->aggcollid;
     803 CBC       40851 :             break;
     804 GBC         381 :         case T_GroupingFunc:
     805 GIC         381 :             coll = InvalidOid;
     806 CBC         381 :             break;
     807 GIC        2139 :         case T_WindowFunc:
     808 CBC        2139 :             coll = ((const WindowFunc *) expr)->wincollid;
     809            2139 :             break;
     810            2999 :         case T_SubscriptingRef:
     811            2999 :             coll = ((const SubscriptingRef *) expr)->refcollid;
     812            2999 :             break;
     813          212836 :         case T_FuncExpr:
     814          212836 :             coll = ((const FuncExpr *) expr)->funccollid;
     815          212836 :             break;
     816 LBC           0 :         case T_NamedArgExpr:
     817               0 :             coll = exprCollation((Node *) ((const NamedArgExpr *) expr)->arg);
     818               0 :             break;
     819 CBC       52484 :         case T_OpExpr:
     820           52484 :             coll = ((const OpExpr *) expr)->opcollid;
     821           52484 :             break;
     822              63 :         case T_DistinctExpr:
     823              63 :             coll = ((const DistinctExpr *) expr)->opcollid;
     824              63 :             break;
     825             147 :         case T_NullIfExpr:
     826             147 :             coll = ((const NullIfExpr *) expr)->opcollid;
     827             147 :             break;
     828            7267 :         case T_ScalarArrayOpExpr:
     829 ECB             :             /* ScalarArrayOpExpr's result is boolean ... */
     830 CBC        7267 :             coll = InvalidOid;  /* ... so it has no collation */
     831            7267 :             break;
     832 GBC        2899 :         case T_BoolExpr:
     833 EUB             :             /* BoolExpr's result is boolean ... */
     834 GBC        2899 :             coll = InvalidOid;  /* ... so it has no collation */
     835 CBC        2899 :             break;
     836            7818 :         case T_SubLink:
     837 ECB             :             {
     838 CBC        7818 :                 const SubLink *sublink = (const SubLink *) expr;
     839 ECB             : 
     840 CBC        7818 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
     841             664 :                     sublink->subLinkType == ARRAY_SUBLINK)
     842            7787 :                 {
     843 ECB             :                     /* get the collation of subselect's first target column */
     844 CBC        7787 :                     Query      *qtree = (Query *) sublink->subselect;
     845                 :                     TargetEntry *tent;
     846 ECB             : 
     847 CBC        7787 :                     if (!qtree || !IsA(qtree, Query))
     848 LBC           0 :                         elog(ERROR, "cannot get collation for untransformed sublink");
     849 GIC        7787 :                     tent = linitial_node(TargetEntry, qtree->targetList);
     850 CBC        7787 :                     Assert(!tent->resjunk);
     851            7787 :                     coll = exprCollation((Node *) tent->expr);
     852 ECB             :                     /* collation doesn't change if it's converted to array */
     853                 :                 }
     854                 :                 else
     855                 :                 {
     856                 :                     /* otherwise, SubLink's result is RECORD or BOOLEAN */
     857 CBC          31 :                     coll = InvalidOid;  /* ... so it has no collation */
     858 ECB             :                 }
     859                 :             }
     860 CBC        7818 :             break;
     861 GIC        7614 :         case T_SubPlan:
     862                 :             {
     863 CBC        7614 :                 const SubPlan *subplan = (const SubPlan *) expr;
     864 EUB             : 
     865 CBC        7614 :                 if (subplan->subLinkType == EXPR_SUBLINK ||
     866             147 :                     subplan->subLinkType == ARRAY_SUBLINK)
     867 ECB             :                 {
     868                 :                     /* get the collation of subselect's first target column */
     869 GIC        7527 :                     coll = subplan->firstColCollation;
     870                 :                     /* collation doesn't change if it's converted to array */
     871                 :                 }
     872                 :                 else
     873 ECB             :                 {
     874                 :                     /* otherwise, SubPlan's result is RECORD or BOOLEAN */
     875 GIC          87 :                     coll = InvalidOid;  /* ... so it has no collation */
     876 ECB             :                 }
     877                 :             }
     878 GIC        7614 :             break;
     879 LBC           0 :         case T_AlternativeSubPlan:
     880                 :             {
     881               0 :                 const AlternativeSubPlan *asplan = (const AlternativeSubPlan *) expr;
     882 ECB             : 
     883                 :                 /* subplans should all return the same thing */
     884 UIC           0 :                 coll = exprCollation((Node *) linitial(asplan->subplans));
     885 ECB             :             }
     886 UIC           0 :             break;
     887 GIC       16171 :         case T_FieldSelect:
     888           16171 :             coll = ((const FieldSelect *) expr)->resultcollid;
     889           16171 :             break;
     890              29 :         case T_FieldStore:
     891 ECB             :             /* FieldStore's result is composite ... */
     892 GIC          29 :             coll = InvalidOid;  /* ... so it has no collation */
     893              29 :             break;
     894 CBC       37862 :         case T_RelabelType:
     895 GBC       37862 :             coll = ((const RelabelType *) expr)->resultcollid;
     896 GIC       37862 :             break;
     897 GBC       15192 :         case T_CoerceViaIO:
     898 GIC       15192 :             coll = ((const CoerceViaIO *) expr)->resultcollid;
     899           15192 :             break;
     900 GBC         919 :         case T_ArrayCoerceExpr:
     901 GIC         919 :             coll = ((const ArrayCoerceExpr *) expr)->resultcollid;
     902 GBC         919 :             break;
     903 CBC         236 :         case T_ConvertRowtypeExpr:
     904 ECB             :             /* ConvertRowtypeExpr's result is composite ... */
     905 CBC         236 :             coll = InvalidOid;  /* ... so it has no collation */
     906             236 :             break;
     907 GIC          58 :         case T_CollateExpr:
     908 CBC          58 :             coll = ((const CollateExpr *) expr)->collOid;
     909              58 :             break;
     910           35528 :         case T_CaseExpr:
     911           35528 :             coll = ((const CaseExpr *) expr)->casecollid;
     912           35528 :             break;
     913           42003 :         case T_CaseTestExpr:
     914           42003 :             coll = ((const CaseTestExpr *) expr)->collation;
     915           42003 :             break;
     916           12228 :         case T_ArrayExpr:
     917           12228 :             coll = ((const ArrayExpr *) expr)->array_collid;
     918           12228 :             break;
     919            1974 :         case T_RowExpr:
     920                 :             /* RowExpr's result is composite ... */
     921            1974 :             coll = InvalidOid;  /* ... so it has no collation */
     922            1974 :             break;
     923              33 :         case T_RowCompareExpr:
     924 ECB             :             /* RowCompareExpr's result is boolean ... */
     925 CBC          33 :             coll = InvalidOid;  /* ... so it has no collation */
     926              33 :             break;
     927            1687 :         case T_CoalesceExpr:
     928            1687 :             coll = ((const CoalesceExpr *) expr)->coalescecollid;
     929            1687 :             break;
     930            1445 :         case T_MinMaxExpr:
     931            1445 :             coll = ((const MinMaxExpr *) expr)->minmaxcollid;
     932            1445 :             break;
     933 GIC         328 :         case T_XmlExpr:
     934 ECB             : 
     935                 :             /*
     936                 :              * XMLSERIALIZE returns text from non-collatable inputs, so its
     937                 :              * collation is always default.  The other cases return boolean or
     938                 :              * XML, which are non-collatable.
     939                 :              */
     940 CBC         328 :             if (((const XmlExpr *) expr)->op == IS_XMLSERIALIZE)
     941              73 :                 coll = DEFAULT_COLLATION_OID;
     942 ECB             :             else
     943 GIC         255 :                 coll = InvalidOid;
     944             328 :             break;
     945 GNC           6 :         case T_JsonValueExpr:
     946               6 :             coll = exprCollation((Node *) ((const JsonValueExpr *) expr)->formatted_expr);
     947               6 :             break;
     948             439 :         case T_JsonConstructorExpr:
     949                 :             {
     950             439 :                 const JsonConstructorExpr *ctor = (const JsonConstructorExpr *) expr;
     951                 : 
     952             439 :                 if (ctor->coercion)
     953              75 :                     coll = exprCollation((Node *) ctor->coercion);
     954                 :                 else
     955             364 :                     coll = InvalidOid;
     956                 :             }
     957             439 :             break;
     958             130 :         case T_JsonIsPredicate:
     959                 :             /* IS JSON's result is boolean ... */
     960             130 :             coll = InvalidOid;  /* ... so it has no collation */
     961             130 :             break;
     962 GIC        1173 :         case T_NullTest:
     963                 :             /* NullTest's result is boolean ... */
     964            1173 :             coll = InvalidOid;  /* ... so it has no collation */
     965            1173 :             break;
     966 CBC         176 :         case T_BooleanTest:
     967 ECB             :             /* BooleanTest's result is boolean ... */
     968 GIC         176 :             coll = InvalidOid;  /* ... so it has no collation */
     969 CBC         176 :             break;
     970          219343 :         case T_CoerceToDomain:
     971          219343 :             coll = ((const CoerceToDomain *) expr)->resultcollid;
     972          219343 :             break;
     973             850 :         case T_CoerceToDomainValue:
     974             850 :             coll = ((const CoerceToDomainValue *) expr)->collation;
     975 GIC         850 :             break;
     976 CBC       47569 :         case T_SetToDefault:
     977 GIC       47569 :             coll = ((const SetToDefault *) expr)->collation;
     978 CBC       47569 :             break;
     979             121 :         case T_CurrentOfExpr:
     980                 :             /* CurrentOfExpr's result is boolean ... */
     981             121 :             coll = InvalidOid;  /* ... so it has no collation */
     982 GIC         121 :             break;
     983 CBC         143 :         case T_NextValueExpr:
     984 ECB             :             /* NextValueExpr's result is an integer type ... */
     985 GIC         143 :             coll = InvalidOid;  /* ... so it has no collation */
     986 CBC         143 :             break;
     987 LBC           0 :         case T_InferenceElem:
     988               0 :             coll = exprCollation((Node *) ((const InferenceElem *) expr)->expr);
     989 UIC           0 :             break;
     990 CBC        1624 :         case T_PlaceHolderVar:
     991            1624 :             coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
     992            1624 :             break;
     993 UIC           0 :         default:
     994 LBC           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
     995 ECB             :             coll = InvalidOid;  /* keep compiler quiet */
     996                 :             break;
     997                 :     }
     998 CBC     7274479 :     return coll;
     999 ECB             : }
    1000                 : 
    1001                 : /*
    1002                 :  *  exprInputCollation -
    1003                 :  *    returns the Oid of the collation a function should use, if available.
    1004                 :  *
    1005                 :  * Result is InvalidOid if the node type doesn't store this information.
    1006                 :  */
    1007                 : Oid
    1008 CBC         649 : exprInputCollation(const Node *expr)
    1009 ECB             : {
    1010                 :     Oid         coll;
    1011                 : 
    1012 CBC         649 :     if (!expr)
    1013 UBC           0 :         return InvalidOid;
    1014 EUB             : 
    1015 GBC         649 :     switch (nodeTag(expr))
    1016 ECB             :     {
    1017 LBC           0 :         case T_Aggref:
    1018               0 :             coll = ((const Aggref *) expr)->inputcollid;
    1019 UBC           0 :             break;
    1020               0 :         case T_WindowFunc:
    1021 UIC           0 :             coll = ((const WindowFunc *) expr)->inputcollid;
    1022               0 :             break;
    1023 GIC          43 :         case T_FuncExpr:
    1024 CBC          43 :             coll = ((const FuncExpr *) expr)->inputcollid;
    1025 GIC          43 :             break;
    1026             164 :         case T_OpExpr:
    1027             164 :             coll = ((const OpExpr *) expr)->inputcollid;
    1028             164 :             break;
    1029               3 :         case T_DistinctExpr:
    1030               3 :             coll = ((const DistinctExpr *) expr)->inputcollid;
    1031               3 :             break;
    1032               6 :         case T_NullIfExpr:
    1033               6 :             coll = ((const NullIfExpr *) expr)->inputcollid;
    1034 CBC           6 :             break;
    1035 GIC           3 :         case T_ScalarArrayOpExpr:
    1036               3 :             coll = ((const ScalarArrayOpExpr *) expr)->inputcollid;
    1037               3 :             break;
    1038 CBC           3 :         case T_MinMaxExpr:
    1039 GBC           3 :             coll = ((const MinMaxExpr *) expr)->inputcollid;
    1040 GIC           3 :             break;
    1041 CBC         427 :         default:
    1042 GIC         427 :             coll = InvalidOid;
    1043 GBC         427 :             break;
    1044 EUB             :     }
    1045 GBC         649 :     return coll;
    1046 EUB             : }
    1047                 : 
    1048                 : /*
    1049 ECB             :  *  exprSetCollation -
    1050                 :  *    Assign collation information to an expression tree node.
    1051                 :  *
    1052                 :  * Note: since this is only used during parse analysis, we don't need to
    1053                 :  * worry about subplans or PlaceHolderVars.
    1054                 :  */
    1055                 : void
    1056 CBC     1702639 : exprSetCollation(Node *expr, Oid collation)
    1057 ECB             : {
    1058 CBC     1702639 :     switch (nodeTag(expr))
    1059 ECB             :     {
    1060 LBC           0 :         case T_Var:
    1061               0 :             ((Var *) expr)->varcollid = collation;
    1062               0 :             break;
    1063               0 :         case T_Const:
    1064               0 :             ((Const *) expr)->constcollid = collation;
    1065               0 :             break;
    1066               0 :         case T_Param:
    1067               0 :             ((Param *) expr)->paramcollid = collation;
    1068               0 :             break;
    1069 CBC       22995 :         case T_Aggref:
    1070 GIC       22995 :             ((Aggref *) expr)->aggcollid = collation;
    1071 CBC       22995 :             break;
    1072 GIC         157 :         case T_GroupingFunc:
    1073             157 :             Assert(!OidIsValid(collation));
    1074             157 :             break;
    1075            1692 :         case T_WindowFunc:
    1076            1692 :             ((WindowFunc *) expr)->wincollid = collation;
    1077            1692 :             break;
    1078            9320 :         case T_SubscriptingRef:
    1079            9320 :             ((SubscriptingRef *) expr)->refcollid = collation;
    1080            9320 :             break;
    1081          445359 :         case T_FuncExpr:
    1082 CBC      445359 :             ((FuncExpr *) expr)->funccollid = collation;
    1083 GIC      445359 :             break;
    1084 CBC       17299 :         case T_NamedArgExpr:
    1085 GIC       17299 :             Assert(collation == exprCollation((Node *) ((NamedArgExpr *) expr)->arg));
    1086 GBC       17299 :             break;
    1087          483582 :         case T_OpExpr:
    1088          483582 :             ((OpExpr *) expr)->opcollid = collation;
    1089          483582 :             break;
    1090             407 :         case T_DistinctExpr:
    1091             407 :             ((DistinctExpr *) expr)->opcollid = collation;
    1092             407 :             break;
    1093             400 :         case T_NullIfExpr:
    1094             400 :             ((NullIfExpr *) expr)->opcollid = collation;
    1095 CBC         400 :             break;
    1096           34469 :         case T_ScalarArrayOpExpr:
    1097 ECB             :             /* ScalarArrayOpExpr's result is boolean ... */
    1098 CBC       34469 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1099           34469 :             break;
    1100          119367 :         case T_BoolExpr:
    1101 ECB             :             /* BoolExpr's result is boolean ... */
    1102 CBC      119367 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1103          119367 :             break;
    1104           31686 :         case T_SubLink:
    1105 ECB             : #ifdef USE_ASSERT_CHECKING
    1106                 :             {
    1107 CBC       31686 :                 SubLink    *sublink = (SubLink *) expr;
    1108 ECB             : 
    1109 CBC       31686 :                 if (sublink->subLinkType == EXPR_SUBLINK ||
    1110           12472 :                     sublink->subLinkType == ARRAY_SUBLINK)
    1111           23450 :                 {
    1112 ECB             :                     /* get the collation of subselect's first target column */
    1113 CBC       23450 :                     Query      *qtree = (Query *) sublink->subselect;
    1114 ECB             :                     TargetEntry *tent;
    1115                 : 
    1116 CBC       23450 :                     if (!qtree || !IsA(qtree, Query))
    1117 LBC           0 :                         elog(ERROR, "cannot set collation for untransformed sublink");
    1118 CBC       23450 :                     tent = linitial_node(TargetEntry, qtree->targetList);
    1119           23450 :                     Assert(!tent->resjunk);
    1120           23450 :                     Assert(collation == exprCollation((Node *) tent->expr));
    1121 ECB             :                 }
    1122                 :                 else
    1123                 :                 {
    1124                 :                     /* otherwise, result is RECORD or BOOLEAN */
    1125 CBC        8236 :                     Assert(!OidIsValid(collation));
    1126 ECB             :                 }
    1127                 :             }
    1128                 : #endif                          /* USE_ASSERT_CHECKING */
    1129 CBC       31686 :             break;
    1130 LBC           0 :         case T_FieldSelect:
    1131 UIC           0 :             ((FieldSelect *) expr)->resultcollid = collation;
    1132               0 :             break;
    1133 CBC         197 :         case T_FieldStore:
    1134                 :             /* FieldStore's result is composite ... */
    1135             197 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1136             197 :             break;
    1137          101003 :         case T_RelabelType:
    1138 GIC      101003 :             ((RelabelType *) expr)->resultcollid = collation;
    1139 CBC      101003 :             break;
    1140 GIC       17884 :         case T_CoerceViaIO:
    1141           17884 :             ((CoerceViaIO *) expr)->resultcollid = collation;
    1142 CBC       17884 :             break;
    1143 GBC        3511 :         case T_ArrayCoerceExpr:
    1144 CBC        3511 :             ((ArrayCoerceExpr *) expr)->resultcollid = collation;
    1145            3511 :             break;
    1146              30 :         case T_ConvertRowtypeExpr:
    1147                 :             /* ConvertRowtypeExpr's result is composite ... */
    1148 GIC          30 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1149              30 :             break;
    1150           62273 :         case T_CaseExpr:
    1151 CBC       62273 :             ((CaseExpr *) expr)->casecollid = collation;
    1152 GIC       62273 :             break;
    1153           31809 :         case T_ArrayExpr:
    1154           31809 :             ((ArrayExpr *) expr)->array_collid = collation;
    1155 CBC       31809 :             break;
    1156 UBC           0 :         case T_RowExpr:
    1157 EUB             :             /* RowExpr's result is composite ... */
    1158 UBC           0 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1159 LBC           0 :             break;
    1160 UIC           0 :         case T_RowCompareExpr:
    1161 ECB             :             /* RowCompareExpr's result is boolean ... */
    1162 LBC           0 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1163               0 :             break;
    1164 CBC       14800 :         case T_CoalesceExpr:
    1165           14800 :             ((CoalesceExpr *) expr)->coalescecollid = collation;
    1166           14800 :             break;
    1167             169 :         case T_MinMaxExpr:
    1168             169 :             ((MinMaxExpr *) expr)->minmaxcollid = collation;
    1169             169 :             break;
    1170             378 :         case T_XmlExpr:
    1171             378 :             Assert((((XmlExpr *) expr)->op == IS_XMLSERIALIZE) ?
    1172 ECB             :                    (collation == DEFAULT_COLLATION_OID) :
    1173                 :                    (collation == InvalidOid));
    1174 CBC         378 :             break;
    1175 GNC          78 :         case T_JsonValueExpr:
    1176              78 :             exprSetCollation((Node *) ((JsonValueExpr *) expr)->formatted_expr,
    1177                 :                              collation);
    1178              78 :             break;
    1179             394 :         case T_JsonConstructorExpr:
    1180                 :             {
    1181             394 :                 JsonConstructorExpr *ctor = (JsonConstructorExpr *) expr;
    1182                 : 
    1183             394 :                 if (ctor->coercion)
    1184              81 :                     exprSetCollation((Node *) ctor->coercion, collation);
    1185                 :                 else
    1186             313 :                     Assert(!OidIsValid(collation)); /* result is always a
    1187                 :                                                      * json[b] type */
    1188                 :             }
    1189             394 :             break;
    1190             161 :         case T_JsonIsPredicate:
    1191             161 :             Assert(!OidIsValid(collation)); /* result is always boolean */
    1192             161 :             break;
    1193 CBC       15939 :         case T_NullTest:
    1194 ECB             :             /* NullTest's result is boolean ... */
    1195 GBC       15939 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1196 GIC       15939 :             break;
    1197 GBC         211 :         case T_BooleanTest:
    1198 EUB             :             /* BooleanTest's result is boolean ... */
    1199 GBC         211 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1200 GIC         211 :             break;
    1201 GBC      287069 :         case T_CoerceToDomain:
    1202          287069 :             ((CoerceToDomain *) expr)->resultcollid = collation;
    1203 CBC      287069 :             break;
    1204 LBC           0 :         case T_CoerceToDomainValue:
    1205               0 :             ((CoerceToDomainValue *) expr)->collation = collation;
    1206               0 :             break;
    1207               0 :         case T_SetToDefault:
    1208               0 :             ((SetToDefault *) expr)->collation = collation;
    1209               0 :             break;
    1210               0 :         case T_CurrentOfExpr:
    1211                 :             /* CurrentOfExpr's result is boolean ... */
    1212 UIC           0 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1213 LBC           0 :             break;
    1214               0 :         case T_NextValueExpr:
    1215 ECB             :             /* NextValueExpr's result is an integer type ... */
    1216 UIC           0 :             Assert(!OidIsValid(collation)); /* ... so never set a collation */
    1217 LBC           0 :             break;
    1218               0 :         default:
    1219 UIC           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
    1220 ECB             :             break;
    1221                 :     }
    1222 CBC     1702639 : }
    1223 ECB             : 
    1224                 : /*
    1225                 :  *  exprSetInputCollation -
    1226                 :  *    Assign input-collation information to an expression tree node.
    1227                 :  *
    1228                 :  * This is a no-op for node types that don't store their input collation.
    1229                 :  * Note we omit RowCompareExpr, which needs special treatment since it
    1230                 :  * contains multiple input collation OIDs.
    1231                 :  */
    1232                 : void
    1233 GIC     1415411 : exprSetInputCollation(Node *expr, Oid inputcollation)
    1234 ECB             : {
    1235 CBC     1415411 :     switch (nodeTag(expr))
    1236 ECB             :     {
    1237 GIC       22995 :         case T_Aggref:
    1238 CBC       22995 :             ((Aggref *) expr)->inputcollid = inputcollation;
    1239           22995 :             break;
    1240            1692 :         case T_WindowFunc:
    1241            1692 :             ((WindowFunc *) expr)->inputcollid = inputcollation;
    1242            1692 :             break;
    1243 GBC      445305 :         case T_FuncExpr:
    1244          445305 :             ((FuncExpr *) expr)->inputcollid = inputcollation;
    1245          445305 :             break;
    1246          483582 :         case T_OpExpr:
    1247          483582 :             ((OpExpr *) expr)->inputcollid = inputcollation;
    1248          483582 :             break;
    1249             407 :         case T_DistinctExpr:
    1250 GIC         407 :             ((DistinctExpr *) expr)->inputcollid = inputcollation;
    1251 GBC         407 :             break;
    1252             400 :         case T_NullIfExpr:
    1253             400 :             ((NullIfExpr *) expr)->inputcollid = inputcollation;
    1254 GIC         400 :             break;
    1255 GBC       34469 :         case T_ScalarArrayOpExpr:
    1256           34469 :             ((ScalarArrayOpExpr *) expr)->inputcollid = inputcollation;
    1257           34469 :             break;
    1258             169 :         case T_MinMaxExpr:
    1259 GIC         169 :             ((MinMaxExpr *) expr)->inputcollid = inputcollation;
    1260             169 :             break;
    1261 CBC      426392 :         default:
    1262 GIC      426392 :             break;
    1263                 :     }
    1264         1415411 : }
    1265                 : 
    1266                 : 
    1267                 : /*
    1268                 :  *  exprLocation -
    1269                 :  *    returns the parse location of an expression tree, for error reports
    1270                 :  *
    1271                 :  * -1 is returned if the location can't be determined.
    1272 ECB             :  *
    1273                 :  * For expressions larger than a single token, the intent here is to
    1274                 :  * return the location of the expression's leftmost token, not necessarily
    1275                 :  * the topmost Node's location field.  For example, an OpExpr's location
    1276                 :  * field will point at the operator name, but if it is not a prefix operator
    1277                 :  * then we should return the location of the left-hand operand instead.
    1278                 :  * The reason is that we want to reference the entire expression not just
    1279                 :  * that operator, and pointing to its start seems to be the most natural way.
    1280                 :  *
    1281                 :  * The location is not perfect --- for example, since the grammar doesn't
    1282                 :  * explicitly represent parentheses in the parsetree, given something that
    1283                 :  * had been written "(a + b) * c" we are going to point at "a" not "(".
    1284                 :  * But it should be plenty good enough for error reporting purposes.
    1285                 :  *
    1286                 :  * You might think that this code is overly general, for instance why check
    1287                 :  * the operands of a FuncExpr node, when the function name can be expected
    1288                 :  * to be to the left of them?  There are a couple of reasons.  The grammar
    1289                 :  * sometimes builds expressions that aren't quite what the user wrote;
    1290                 :  * for instance x IS NOT BETWEEN ... becomes a NOT-expression whose keyword
    1291                 :  * pointer is to the right of its leftmost argument.  Also, nodes that were
    1292                 :  * inserted implicitly by parse analysis (such as FuncExprs for implicit
    1293                 :  * coercions) will have location -1, and so we can have odd combinations of
    1294                 :  * known and unknown locations in a tree.
    1295                 :  */
    1296                 : int
    1297 CBC     4205274 : exprLocation(const Node *expr)
    1298 ECB             : {
    1299                 :     int         loc;
    1300                 : 
    1301 CBC     4205274 :     if (expr == NULL)
    1302 GIC       84838 :         return -1;
    1303 CBC     4120436 :     switch (nodeTag(expr))
    1304                 :     {
    1305 GIC          15 :         case T_RangeVar:
    1306              15 :             loc = ((const RangeVar *) expr)->location;
    1307              15 :             break;
    1308 UIC           0 :         case T_TableFunc:
    1309               0 :             loc = ((const TableFunc *) expr)->location;
    1310               0 :             break;
    1311 GIC     1898934 :         case T_Var:
    1312         1898934 :             loc = ((const Var *) expr)->location;
    1313         1898934 :             break;
    1314         1143054 :         case T_Const:
    1315         1143054 :             loc = ((const Const *) expr)->location;
    1316         1143054 :             break;
    1317          207222 :         case T_Param:
    1318          207222 :             loc = ((const Param *) expr)->location;
    1319          207222 :             break;
    1320            4264 :         case T_Aggref:
    1321                 :             /* function name should always be the first thing */
    1322            4264 :             loc = ((const Aggref *) expr)->location;
    1323            4264 :             break;
    1324              35 :         case T_GroupingFunc:
    1325              35 :             loc = ((const GroupingFunc *) expr)->location;
    1326              35 :             break;
    1327              18 :         case T_WindowFunc:
    1328                 :             /* function name should always be the first thing */
    1329              18 :             loc = ((const WindowFunc *) expr)->location;
    1330              18 :             break;
    1331             410 :         case T_SubscriptingRef:
    1332                 :             /* just use container argument's location */
    1333             410 :             loc = exprLocation((Node *) ((const SubscriptingRef *) expr)->refexpr);
    1334             410 :             break;
    1335          173315 :         case T_FuncExpr:
    1336 ECB             :             {
    1337 GIC      173315 :                 const FuncExpr *fexpr = (const FuncExpr *) expr;
    1338                 : 
    1339                 :                 /* consider both function name and leftmost arg */
    1340 CBC      173315 :                 loc = leftmostLoc(fexpr->location,
    1341          173315 :                                   exprLocation((Node *) fexpr->args));
    1342 ECB             :             }
    1343 GIC      173315 :             break;
    1344 CBC           3 :         case T_NamedArgExpr:
    1345 ECB             :             {
    1346 CBC           3 :                 const NamedArgExpr *na = (const NamedArgExpr *) expr;
    1347 EUB             : 
    1348                 :                 /* consider both argument name and value */
    1349 GBC           3 :                 loc = leftmostLoc(na->location,
    1350 CBC           3 :                                   exprLocation((Node *) na->arg));
    1351 ECB             :             }
    1352 CBC           3 :             break;
    1353           13287 :         case T_OpExpr:
    1354 ECB             :         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
    1355                 :         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
    1356                 :             {
    1357 CBC       13287 :                 const OpExpr *opexpr = (const OpExpr *) expr;
    1358 ECB             : 
    1359                 :                 /* consider both operator name and leftmost arg */
    1360 GIC       13287 :                 loc = leftmostLoc(opexpr->location,
    1361 CBC       13287 :                                   exprLocation((Node *) opexpr->args));
    1362 ECB             :             }
    1363 CBC       13287 :             break;
    1364 LBC           0 :         case T_ScalarArrayOpExpr:
    1365 ECB             :             {
    1366 LBC           0 :                 const ScalarArrayOpExpr *saopexpr = (const ScalarArrayOpExpr *) expr;
    1367                 : 
    1368 ECB             :                 /* consider both operator name and leftmost arg */
    1369 LBC           0 :                 loc = leftmostLoc(saopexpr->location,
    1370               0 :                                   exprLocation((Node *) saopexpr->args));
    1371                 :             }
    1372               0 :             break;
    1373 CBC         606 :         case T_BoolExpr:
    1374 ECB             :             {
    1375 GIC         606 :                 const BoolExpr *bexpr = (const BoolExpr *) expr;
    1376 ECB             : 
    1377                 :                 /*
    1378                 :                  * Same as above, to handle either NOT or AND/OR.  We can't
    1379                 :                  * special-case NOT because of the way that it's used for
    1380                 :                  * things like IS NOT BETWEEN.
    1381                 :                  */
    1382 CBC         606 :                 loc = leftmostLoc(bexpr->location,
    1383             606 :                                   exprLocation((Node *) bexpr->args));
    1384                 :             }
    1385             606 :             break;
    1386 GIC        3399 :         case T_SubLink:
    1387                 :             {
    1388 CBC        3399 :                 const SubLink *sublink = (const SubLink *) expr;
    1389 ECB             : 
    1390                 :                 /* check the testexpr, if any, and the operator/keyword */
    1391 CBC        3399 :                 loc = leftmostLoc(exprLocation(sublink->testexpr),
    1392            3399 :                                   sublink->location);
    1393                 :             }
    1394 GIC        3399 :             break;
    1395            8915 :         case T_FieldSelect:
    1396 ECB             :             /* just use argument's location */
    1397 GIC        8915 :             loc = exprLocation((Node *) ((const FieldSelect *) expr)->arg);
    1398            8915 :             break;
    1399 LBC           0 :         case T_FieldStore:
    1400 ECB             :             /* just use argument's location */
    1401 UIC           0 :             loc = exprLocation((Node *) ((const FieldStore *) expr)->arg);
    1402 LBC           0 :             break;
    1403 GBC        7874 :         case T_RelabelType:
    1404                 :             {
    1405            7874 :                 const RelabelType *rexpr = (const RelabelType *) expr;
    1406                 : 
    1407                 :                 /* Much as above */
    1408            7874 :                 loc = leftmostLoc(rexpr->location,
    1409            7874 :                                   exprLocation((Node *) rexpr->arg));
    1410                 :             }
    1411            7874 :             break;
    1412 CBC       22722 :         case T_CoerceViaIO:
    1413                 :             {
    1414           22722 :                 const CoerceViaIO *cexpr = (const CoerceViaIO *) expr;
    1415                 : 
    1416                 :                 /* Much as above */
    1417 GIC       22722 :                 loc = leftmostLoc(cexpr->location,
    1418           22722 :                                   exprLocation((Node *) cexpr->arg));
    1419                 :             }
    1420           22722 :             break;
    1421 LBC           0 :         case T_ArrayCoerceExpr:
    1422 ECB             :             {
    1423 UIC           0 :                 const ArrayCoerceExpr *cexpr = (const ArrayCoerceExpr *) expr;
    1424 ECB             : 
    1425                 :                 /* Much as above */
    1426 UIC           0 :                 loc = leftmostLoc(cexpr->location,
    1427 LBC           0 :                                   exprLocation((Node *) cexpr->arg));
    1428                 :             }
    1429 UIC           0 :             break;
    1430 CBC           3 :         case T_ConvertRowtypeExpr:
    1431 ECB             :             {
    1432 GIC           3 :                 const ConvertRowtypeExpr *cexpr = (const ConvertRowtypeExpr *) expr;
    1433 ECB             : 
    1434                 :                 /* Much as above */
    1435 GIC           3 :                 loc = leftmostLoc(cexpr->location,
    1436 CBC           3 :                                   exprLocation((Node *) cexpr->arg));
    1437 ECB             :             }
    1438 GBC           3 :             break;
    1439 GIC          21 :         case T_CollateExpr:
    1440 EUB             :             /* just use argument's location */
    1441 GBC          21 :             loc = exprLocation((Node *) ((const CollateExpr *) expr)->arg);
    1442 CBC          21 :             break;
    1443 GIC       29812 :         case T_CaseExpr:
    1444 ECB             :             /* CASE keyword should always be the first thing */
    1445 GIC       29812 :             loc = ((const CaseExpr *) expr)->location;
    1446           29812 :             break;
    1447 LBC           0 :         case T_CaseWhen:
    1448 ECB             :             /* WHEN keyword should always be the first thing */
    1449 UIC           0 :             loc = ((const CaseWhen *) expr)->location;
    1450 LBC           0 :             break;
    1451 CBC         204 :         case T_ArrayExpr:
    1452                 :             /* the location points at ARRAY or [, which must be leftmost */
    1453             204 :             loc = ((const ArrayExpr *) expr)->location;
    1454 GIC         204 :             break;
    1455             110 :         case T_RowExpr:
    1456 ECB             :             /* the location points at ROW or (, which must be leftmost */
    1457 CBC         110 :             loc = ((const RowExpr *) expr)->location;
    1458 GIC         110 :             break;
    1459 LBC           0 :         case T_RowCompareExpr:
    1460 EUB             :             /* just use leftmost argument's location */
    1461 UIC           0 :             loc = exprLocation((Node *) ((const RowCompareExpr *) expr)->largs);
    1462 UBC           0 :             break;
    1463 GIC        6982 :         case T_CoalesceExpr:
    1464                 :             /* COALESCE keyword should always be the first thing */
    1465 GBC        6982 :             loc = ((const CoalesceExpr *) expr)->location;
    1466            6982 :             break;
    1467 GIC           9 :         case T_MinMaxExpr:
    1468 EUB             :             /* GREATEST/LEAST keyword should always be the first thing */
    1469 CBC           9 :             loc = ((const MinMaxExpr *) expr)->location;
    1470 GIC           9 :             break;
    1471 CBC          95 :         case T_XmlExpr:
    1472                 :             {
    1473              95 :                 const XmlExpr *xexpr = (const XmlExpr *) expr;
    1474 ECB             : 
    1475                 :                 /* consider both function name and leftmost arg */
    1476 CBC          95 :                 loc = leftmostLoc(xexpr->location,
    1477              95 :                                   exprLocation((Node *) xexpr->args));
    1478 ECB             :             }
    1479 GIC          95 :             break;
    1480 UNC           0 :         case T_JsonFormat:
    1481               0 :             loc = ((const JsonFormat *) expr)->location;
    1482               0 :             break;
    1483               0 :         case T_JsonValueExpr:
    1484               0 :             loc = exprLocation((Node *) ((const JsonValueExpr *) expr)->raw_expr);
    1485               0 :             break;
    1486 GNC          48 :         case T_JsonConstructorExpr:
    1487              48 :             loc = ((const JsonConstructorExpr *) expr)->location;
    1488              48 :             break;
    1489 UNC           0 :         case T_JsonIsPredicate:
    1490               0 :             loc = ((const JsonIsPredicate *) expr)->location;
    1491               0 :             break;
    1492 CBC         606 :         case T_NullTest:
    1493 ECB             :             {
    1494 GBC         606 :                 const NullTest *nexpr = (const NullTest *) expr;
    1495                 : 
    1496 EUB             :                 /* Much as above */
    1497 GBC         606 :                 loc = leftmostLoc(nexpr->location,
    1498 CBC         606 :                                   exprLocation((Node *) nexpr->arg));
    1499                 :             }
    1500             606 :             break;
    1501 LBC           0 :         case T_BooleanTest:
    1502 ECB             :             {
    1503 UIC           0 :                 const BooleanTest *bexpr = (const BooleanTest *) expr;
    1504 ECB             : 
    1505                 :                 /* Much as above */
    1506 UBC           0 :                 loc = leftmostLoc(bexpr->location,
    1507 UIC           0 :                                   exprLocation((Node *) bexpr->arg));
    1508 EUB             :             }
    1509 UBC           0 :             break;
    1510 CBC      283650 :         case T_CoerceToDomain:
    1511                 :             {
    1512          283650 :                 const CoerceToDomain *cexpr = (const CoerceToDomain *) expr;
    1513 ECB             : 
    1514                 :                 /* Much as above */
    1515 GIC      283650 :                 loc = leftmostLoc(cexpr->location,
    1516 CBC      283650 :                                   exprLocation((Node *) cexpr->arg));
    1517 ECB             :             }
    1518 CBC      283650 :             break;
    1519 GIC         935 :         case T_CoerceToDomainValue:
    1520 CBC         935 :             loc = ((const CoerceToDomainValue *) expr)->location;
    1521 GIC         935 :             break;
    1522           94406 :         case T_SetToDefault:
    1523 CBC       94406 :             loc = ((const SetToDefault *) expr)->location;
    1524           94406 :             break;
    1525 UIC           0 :         case T_TargetEntry:
    1526 ECB             :             /* just use argument's location */
    1527 UBC           0 :             loc = exprLocation((Node *) ((const TargetEntry *) expr)->expr);
    1528               0 :             break;
    1529 GBC           9 :         case T_IntoClause:
    1530 EUB             :             /* use the contained RangeVar's location --- close enough */
    1531 GBC           9 :             loc = exprLocation((Node *) ((const IntoClause *) expr)->rel);
    1532               9 :             break;
    1533 CBC      109027 :         case T_List:
    1534 ECB             :             {
    1535                 :                 /* report location of first list member that has a location */
    1536 EUB             :                 ListCell   *lc;
    1537                 : 
    1538 GBC      109027 :                 loc = -1;       /* just to suppress compiler warning */
    1539 CBC      110822 :                 foreach(lc, (const List *) expr)
    1540                 :                 {
    1541          109787 :                     loc = exprLocation((Node *) lfirst(lc));
    1542 GIC      109787 :                     if (loc >= 0)
    1543          107992 :                         break;
    1544 ECB             :                 }
    1545                 :             }
    1546 GIC      109027 :             break;
    1547 CBC        2720 :         case T_A_Expr:
    1548 EUB             :             {
    1549 GIC        2720 :                 const A_Expr *aexpr = (const A_Expr *) expr;
    1550 EUB             : 
    1551                 :                 /* use leftmost of operator or left operand (if any) */
    1552                 :                 /* we assume right operand can't be to left of operator */
    1553 GBC        2720 :                 loc = leftmostLoc(aexpr->location,
    1554            2720 :                                   exprLocation(aexpr->lexpr));
    1555                 :             }
    1556            2720 :             break;
    1557 CBC       33523 :         case T_ColumnRef:
    1558 GIC       33523 :             loc = ((const ColumnRef *) expr)->location;
    1559 CBC       33523 :             break;
    1560 UIC           0 :         case T_ParamRef:
    1561               0 :             loc = ((const ParamRef *) expr)->location;
    1562 LBC           0 :             break;
    1563 CBC       25734 :         case T_A_Const:
    1564 GIC       25734 :             loc = ((const A_Const *) expr)->location;
    1565 CBC       25734 :             break;
    1566            1742 :         case T_FuncCall:
    1567 ECB             :             {
    1568 CBC        1742 :                 const FuncCall *fc = (const FuncCall *) expr;
    1569 ECB             : 
    1570                 :                 /* consider both function name and leftmost arg */
    1571                 :                 /* (we assume any ORDER BY nodes must be to right of name) */
    1572 GBC        1742 :                 loc = leftmostLoc(fc->location,
    1573 GIC        1742 :                                   exprLocation((Node *) fc->args));
    1574 EUB             :             }
    1575 GBC        1742 :             break;
    1576 LBC           0 :         case T_A_ArrayExpr:
    1577                 :             /* the location points at ARRAY or [, which must be leftmost */
    1578               0 :             loc = ((const A_ArrayExpr *) expr)->location;
    1579               0 :             break;
    1580 CBC           6 :         case T_ResTarget:
    1581                 :             /* we need not examine the contained expression (if any) */
    1582 GIC           6 :             loc = ((const ResTarget *) expr)->location;
    1583               6 :             break;
    1584 UIC           0 :         case T_MultiAssignRef:
    1585 LBC           0 :             loc = exprLocation(((const MultiAssignRef *) expr)->source);
    1586               0 :             break;
    1587 GIC        3361 :         case T_TypeCast:
    1588 ECB             :             {
    1589 CBC        3361 :                 const TypeCast *tc = (const TypeCast *) expr;
    1590 ECB             : 
    1591                 :                 /*
    1592                 :                  * This could represent CAST(), ::, or TypeName 'literal', so
    1593                 :                  * any of the components might be leftmost.
    1594                 :                  */
    1595 GIC        3361 :                 loc = exprLocation(tc->arg);
    1596 CBC        3361 :                 loc = leftmostLoc(loc, tc->typeName->location);
    1597 GIC        3361 :                 loc = leftmostLoc(loc, tc->location);
    1598                 :             }
    1599            3361 :             break;
    1600 CBC         178 :         case T_CollateClause:
    1601 ECB             :             /* just use argument's location */
    1602 GIC         178 :             loc = exprLocation(((const CollateClause *) expr)->arg);
    1603 CBC         178 :             break;
    1604               3 :         case T_SortBy:
    1605 ECB             :             /* just use argument's location (ignore operator, if any) */
    1606 CBC           3 :             loc = exprLocation(((const SortBy *) expr)->node);
    1607 GBC           3 :             break;
    1608 UBC           0 :         case T_WindowDef:
    1609               0 :             loc = ((const WindowDef *) expr)->location;
    1610 LBC           0 :             break;
    1611               0 :         case T_RangeTableSample:
    1612               0 :             loc = ((const RangeTableSample *) expr)->location;
    1613               0 :             break;
    1614 UIC           0 :         case T_TypeName:
    1615 LBC           0 :             loc = ((const TypeName *) expr)->location;
    1616 UIC           0 :             break;
    1617 GIC           9 :         case T_ColumnDef:
    1618               9 :             loc = ((const ColumnDef *) expr)->location;
    1619 CBC           9 :             break;
    1620 LBC           0 :         case T_Constraint:
    1621 UIC           0 :             loc = ((const Constraint *) expr)->location;
    1622 LBC           0 :             break;
    1623 UBC           0 :         case T_FunctionParameter:
    1624                 :             /* just use typename's location */
    1625               0 :             loc = exprLocation((Node *) ((const FunctionParameter *) expr)->argType);
    1626               0 :             break;
    1627 LBC           0 :         case T_XmlSerialize:
    1628                 :             /* XMLSERIALIZE keyword should always be the first thing */
    1629               0 :             loc = ((const XmlSerialize *) expr)->location;
    1630               0 :             break;
    1631 GBC           9 :         case T_GroupingSet:
    1632               9 :             loc = ((const GroupingSet *) expr)->location;
    1633               9 :             break;
    1634 LBC           0 :         case T_WithClause:
    1635 UIC           0 :             loc = ((const WithClause *) expr)->location;
    1636 LBC           0 :             break;
    1637 UIC           0 :         case T_InferClause:
    1638               0 :             loc = ((const InferClause *) expr)->location;
    1639               0 :             break;
    1640 GIC           3 :         case T_OnConflictClause:
    1641               3 :             loc = ((const OnConflictClause *) expr)->location;
    1642 CBC           3 :             break;
    1643 LBC           0 :         case T_CTESearchClause:
    1644               0 :             loc = ((const CTESearchClause *) expr)->location;
    1645 UIC           0 :             break;
    1646 LBC           0 :         case T_CTECycleClause:
    1647               0 :             loc = ((const CTECycleClause *) expr)->location;
    1648 UIC           0 :             break;
    1649 LBC           0 :         case T_CommonTableExpr:
    1650               0 :             loc = ((const CommonTableExpr *) expr)->location;
    1651               0 :             break;
    1652 UNC           0 :         case T_JsonKeyValue:
    1653                 :             /* just use the key's location */
    1654               0 :             loc = exprLocation((Node *) ((const JsonKeyValue *) expr)->key);
    1655               0 :             break;
    1656               0 :         case T_JsonObjectConstructor:
    1657               0 :             loc = ((const JsonObjectConstructor *) expr)->location;
    1658               0 :             break;
    1659               0 :         case T_JsonArrayConstructor:
    1660               0 :             loc = ((const JsonArrayConstructor *) expr)->location;
    1661               0 :             break;
    1662               0 :         case T_JsonArrayQueryConstructor:
    1663               0 :             loc = ((const JsonArrayQueryConstructor *) expr)->location;
    1664               0 :             break;
    1665               0 :         case T_JsonAggConstructor:
    1666               0 :             loc = ((const JsonAggConstructor *) expr)->location;
    1667               0 :             break;
    1668               0 :         case T_JsonObjectAgg:
    1669               0 :             loc = exprLocation((Node *) ((const JsonObjectAgg *) expr)->constructor);
    1670               0 :             break;
    1671               0 :         case T_JsonArrayAgg:
    1672               0 :             loc = exprLocation((Node *) ((const JsonArrayAgg *) expr)->constructor);
    1673               0 :             break;
    1674 UIC           0 :         case T_PlaceHolderVar:
    1675 ECB             :             /* just use argument's location */
    1676 LBC           0 :             loc = exprLocation((Node *) ((const PlaceHolderVar *) expr)->phexpr);
    1677 UBC           0 :             break;
    1678               0 :         case T_InferenceElem:
    1679 EUB             :             /* just use nested expr's location */
    1680 UBC           0 :             loc = exprLocation((Node *) ((const InferenceElem *) expr)->expr);
    1681               0 :             break;
    1682               0 :         case T_PartitionElem:
    1683               0 :             loc = ((const PartitionElem *) expr)->location;
    1684               0 :             break;
    1685               0 :         case T_PartitionSpec:
    1686 LBC           0 :             loc = ((const PartitionSpec *) expr)->location;
    1687               0 :             break;
    1688 CBC          24 :         case T_PartitionBoundSpec:
    1689 GBC          24 :             loc = ((const PartitionBoundSpec *) expr)->location;
    1690              24 :             break;
    1691               9 :         case T_PartitionRangeDatum:
    1692               9 :             loc = ((const PartitionRangeDatum *) expr)->location;
    1693 GIC           9 :             break;
    1694 GBC       43125 :         default:
    1695 EUB             :             /* for any other node type it's just unknown... */
    1696 GBC       43125 :             loc = -1;
    1697 GIC       43125 :             break;
    1698 EUB             :     }
    1699 GBC     4120436 :     return loc;
    1700 ECB             : }
    1701                 : 
    1702                 : /*
    1703 EUB             :  * leftmostLoc - support for exprLocation
    1704                 :  *
    1705                 :  * Take the minimum of two parse location values, but ignore unknowns
    1706                 :  */
    1707                 : static int
    1708 GBC      516744 : leftmostLoc(int loc1, int loc2)
    1709 ECB             : {
    1710 CBC      516744 :     if (loc1 < 0)
    1711           59325 :         return loc2;
    1712 GBC      457419 :     else if (loc2 < 0)
    1713           81450 :         return loc1;
    1714 EUB             :     else
    1715 GBC      375969 :         return Min(loc1, loc2);
    1716 EUB             : }
    1717                 : 
    1718                 : 
    1719                 : /*
    1720                 :  * fix_opfuncids
    1721                 :  *    Calculate opfuncid field from opno for each OpExpr node in given tree.
    1722                 :  *    The given tree can be anything expression_tree_walker handles.
    1723                 :  *
    1724                 :  * The argument is modified in-place.  (This is OK since we'd want the
    1725                 :  * same change for any node, even if it gets visited more than once due to
    1726                 :  * shared structure.)
    1727                 :  */
    1728                 : void
    1729 GBC      266170 : fix_opfuncids(Node *node)
    1730 EUB             : {
    1731                 :     /* This tree walk requires no special setup, so away we go... */
    1732 GBC      266170 :     fix_opfuncids_walker(node, NULL);
    1733          266170 : }
    1734 EUB             : 
    1735                 : static bool
    1736 GBC      530626 : fix_opfuncids_walker(Node *node, void *context)
    1737 EUB             : {
    1738 GBC      530626 :     if (node == NULL)
    1739           67068 :         return false;
    1740          463558 :     if (IsA(node, OpExpr))
    1741           26487 :         set_opfuncid((OpExpr *) node);
    1742          437071 :     else if (IsA(node, DistinctExpr))
    1743               3 :         set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
    1744 GIC      437068 :     else if (IsA(node, NullIfExpr))
    1745 GBC          34 :         set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
    1746          437034 :     else if (IsA(node, ScalarArrayOpExpr))
    1747            1206 :         set_sa_opfuncid((ScalarArrayOpExpr *) node);
    1748 GIC      463558 :     return expression_tree_walker(node, fix_opfuncids_walker, context);
    1749 EUB             : }
    1750                 : 
    1751                 : /*
    1752                 :  * set_opfuncid
    1753                 :  *      Set the opfuncid (procedure OID) in an OpExpr node,
    1754                 :  *      if it hasn't been set already.
    1755                 :  *
    1756                 :  * Because of struct equivalence, this can also be used for
    1757 ECB             :  * DistinctExpr and NullIfExpr nodes.
    1758                 :  */
    1759                 : void
    1760 CBC     1410775 : set_opfuncid(OpExpr *opexpr)
    1761 ECB             : {
    1762 CBC     1410775 :     if (opexpr->opfuncid == InvalidOid)
    1763           89170 :         opexpr->opfuncid = get_opcode(opexpr->opno);
    1764 GIC     1410775 : }
    1765 ECB             : 
    1766                 : /*
    1767                 :  * set_sa_opfuncid
    1768                 :  *      As above, for ScalarArrayOpExpr nodes.
    1769                 :  */
    1770                 : void
    1771 GIC       74204 : set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
    1772                 : {
    1773           74204 :     if (opexpr->opfuncid == InvalidOid)
    1774             230 :         opexpr->opfuncid = get_opcode(opexpr->opno);
    1775           74204 : }
    1776                 : 
    1777 ECB             : 
    1778                 : /*
    1779                 :  *  check_functions_in_node -
    1780                 :  *    apply checker() to each function OID contained in given expression node
    1781                 :  *
    1782                 :  * Returns true if the checker() function does; for nodes representing more
    1783                 :  * than one function call, returns true if the checker() function does so
    1784                 :  * for any of those functions.  Returns false if node does not invoke any
    1785                 :  * SQL-visible function.  Caller must not pass node == NULL.
    1786                 :  *
    1787                 :  * This function examines only the given node; it does not recurse into any
    1788                 :  * sub-expressions.  Callers typically prefer to keep control of the recursion
    1789                 :  * for themselves, in case additional checks should be made, or because they
    1790                 :  * have special rules about which parts of the tree need to be visited.
    1791                 :  *
    1792                 :  * Note: we ignore MinMaxExpr, XmlExpr, CoerceToDomain, and NextValueExpr
    1793                 :  * nodes, because they do not contain SQL function OIDs.  However, they can
    1794                 :  * invoke SQL-visible functions, so callers should take thought about how
    1795                 :  * to treat them.
    1796                 :  */
    1797                 : bool
    1798 CBC     7888798 : check_functions_in_node(Node *node, check_function_callback checker,
    1799                 :                         void *context)
    1800                 : {
    1801         7888798 :     switch (nodeTag(node))
    1802 ECB             :     {
    1803 GIC       42808 :         case T_Aggref:
    1804                 :             {
    1805 CBC       42808 :                 Aggref     *expr = (Aggref *) node;
    1806                 : 
    1807           42808 :                 if (checker(expr->aggfnoid, context))
    1808             526 :                     return true;
    1809 ECB             :             }
    1810 CBC       42282 :             break;
    1811            3350 :         case T_WindowFunc:
    1812 ECB             :             {
    1813 CBC        3350 :                 WindowFunc *expr = (WindowFunc *) node;
    1814 ECB             : 
    1815 CBC        3350 :                 if (checker(expr->winfnoid, context))
    1816              75 :                     return true;
    1817 ECB             :             }
    1818 GIC        3275 :             break;
    1819          256386 :         case T_FuncExpr:
    1820                 :             {
    1821          256386 :                 FuncExpr   *expr = (FuncExpr *) node;
    1822                 : 
    1823          256386 :                 if (checker(expr->funcid, context))
    1824           51680 :                     return true;
    1825                 :             }
    1826          204706 :             break;
    1827          512999 :         case T_OpExpr:
    1828                 :         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
    1829 ECB             :         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
    1830                 :             {
    1831 CBC      512999 :                 OpExpr     *expr = (OpExpr *) node;
    1832 ECB             : 
    1833                 :                 /* Set opfuncid if it wasn't set already */
    1834 GIC      512999 :                 set_opfuncid(expr);
    1835          512999 :                 if (checker(expr->opfuncid, context))
    1836             369 :                     return true;
    1837                 :             }
    1838          512630 :             break;
    1839           24120 :         case T_ScalarArrayOpExpr:
    1840 ECB             :             {
    1841 GIC       24120 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    1842 ECB             : 
    1843 CBC       24120 :                 set_sa_opfuncid(expr);
    1844           24120 :                 if (checker(expr->opfuncid, context))
    1845 GIC          45 :                     return true;
    1846                 :             }
    1847           24075 :             break;
    1848           14845 :         case T_CoerceViaIO:
    1849                 :             {
    1850           14845 :                 CoerceViaIO *expr = (CoerceViaIO *) node;
    1851                 :                 Oid         iofunc;
    1852                 :                 Oid         typioparam;
    1853                 :                 bool        typisvarlena;
    1854                 : 
    1855                 :                 /* check the result type's input function */
    1856           14845 :                 getTypeInputInfo(expr->resulttype,
    1857                 :                                  &iofunc, &typioparam);
    1858           14845 :                 if (checker(iofunc, context))
    1859             335 :                     return true;
    1860                 :                 /* check the input type's output function */
    1861           14823 :                 getTypeOutputInfo(exprType((Node *) expr->arg),
    1862                 :                                   &iofunc, &typisvarlena);
    1863           14823 :                 if (checker(iofunc, context))
    1864             313 :                     return true;
    1865                 :             }
    1866           14510 :             break;
    1867 CBC         117 :         case T_RowCompareExpr:
    1868                 :             {
    1869 GIC         117 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    1870 ECB             :                 ListCell   *opid;
    1871                 : 
    1872 CBC         402 :                 foreach(opid, rcexpr->opnos)
    1873                 :                 {
    1874             285 :                     Oid         opfuncid = get_opcode(lfirst_oid(opid));
    1875                 : 
    1876             285 :                     if (checker(opfuncid, context))
    1877 LBC           0 :                         return true;
    1878                 :                 }
    1879 ECB             :             }
    1880 CBC         117 :             break;
    1881 GIC     7034173 :         default:
    1882 CBC     7034173 :             break;
    1883                 :     }
    1884         7835768 :     return false;
    1885 ECB             : }
    1886                 : 
    1887                 : 
    1888                 : /*
    1889                 :  * Standard expression-tree walking support
    1890                 :  *
    1891                 :  * We used to have near-duplicate code in many different routines that
    1892                 :  * understood how to recurse through an expression node tree.  That was
    1893                 :  * a pain to maintain, and we frequently had bugs due to some particular
    1894                 :  * routine neglecting to support a particular node type.  In most cases,
    1895                 :  * these routines only actually care about certain node types, and don't
    1896                 :  * care about other types except insofar as they have to recurse through
    1897                 :  * non-primitive node types.  Therefore, we now provide generic tree-walking
    1898                 :  * logic to consolidate the redundant "boilerplate" code.  There are
    1899                 :  * two versions: expression_tree_walker() and expression_tree_mutator().
    1900                 :  */
    1901                 : 
    1902                 : /*
    1903                 :  * expression_tree_walker() is designed to support routines that traverse
    1904                 :  * a tree in a read-only fashion (although it will also work for routines
    1905                 :  * that modify nodes in-place but never add/delete/replace nodes).
    1906                 :  * A walker routine should look like this:
    1907                 :  *
    1908                 :  * bool my_walker (Node *node, my_struct *context)
    1909                 :  * {
    1910                 :  *      if (node == NULL)
    1911                 :  *          return false;
    1912                 :  *      // check for nodes that special work is required for, eg:
    1913                 :  *      if (IsA(node, Var))
    1914                 :  *      {
    1915                 :  *          ... do special actions for Var nodes
    1916                 :  *      }
    1917                 :  *      else if (IsA(node, ...))
    1918                 :  *      {
    1919                 :  *          ... do special actions for other node types
    1920                 :  *      }
    1921                 :  *      // for any node type not specially processed, do:
    1922                 :  *      return expression_tree_walker(node, my_walker, (void *) context);
    1923                 :  * }
    1924                 :  *
    1925                 :  * The "context" argument points to a struct that holds whatever context
    1926                 :  * information the walker routine needs --- it can be used to return data
    1927                 :  * gathered by the walker, too.  This argument is not touched by
    1928                 :  * expression_tree_walker, but it is passed down to recursive sub-invocations
    1929                 :  * of my_walker.  The tree walk is started from a setup routine that
    1930                 :  * fills in the appropriate context struct, calls my_walker with the top-level
    1931                 :  * node of the tree, and then examines the results.
    1932                 :  *
    1933                 :  * The walker routine should return "false" to continue the tree walk, or
    1934                 :  * "true" to abort the walk and immediately return "true" to the top-level
    1935                 :  * caller.  This can be used to short-circuit the traversal if the walker
    1936                 :  * has found what it came for.  "false" is returned to the top-level caller
    1937                 :  * iff no invocation of the walker returned "true".
    1938                 :  *
    1939                 :  * The node types handled by expression_tree_walker include all those
    1940                 :  * normally found in target lists and qualifier clauses during the planning
    1941                 :  * stage.  In particular, it handles List nodes since a cnf-ified qual clause
    1942                 :  * will have List structure at the top level, and it handles TargetEntry nodes
    1943                 :  * so that a scan of a target list can be handled without additional code.
    1944                 :  * Also, RangeTblRef, FromExpr, JoinExpr, and SetOperationStmt nodes are
    1945                 :  * handled, so that query jointrees and setOperation trees can be processed
    1946 EUB             :  * without additional code.
    1947                 :  *
    1948                 :  * expression_tree_walker will handle SubLink nodes by recursing normally
    1949 ECB             :  * into the "testexpr" subtree (which is an expression belonging to the outer
    1950                 :  * plan).  It will also call the walker on the sub-Query node; however, when
    1951                 :  * expression_tree_walker itself is called on a Query node, it does nothing
    1952                 :  * and returns "false".  The net effect is that unless the walker does
    1953                 :  * something special at a Query node, sub-selects will not be visited during
    1954                 :  * an expression tree walk. This is exactly the behavior wanted in many cases
    1955                 :  * --- and for those walkers that do want to recurse into sub-selects, special
    1956                 :  * behavior is typically needed anyway at the entry to a sub-select (such as
    1957                 :  * incrementing a depth counter). A walker that wants to examine sub-selects
    1958                 :  * should include code along the lines of:
    1959                 :  *
    1960                 :  *      if (IsA(node, Query))
    1961                 :  *      {
    1962                 :  *          adjust context for subquery;
    1963                 :  *          result = query_tree_walker((Query *) node, my_walker, context,
    1964                 :  *                                     0); // adjust flags as needed
    1965                 :  *          restore context if needed;
    1966                 :  *          return result;
    1967                 :  *      }
    1968                 :  *
    1969                 :  * query_tree_walker is a convenience routine (see below) that calls the
    1970                 :  * walker on all the expression subtrees of the given Query node.
    1971                 :  *
    1972                 :  * expression_tree_walker will handle SubPlan nodes by recursing normally
    1973                 :  * into the "testexpr" and the "args" list (which are expressions belonging to
    1974                 :  * the outer plan).  It will not touch the completed subplan, however.  Since
    1975                 :  * there is no link to the original Query, it is not possible to recurse into
    1976                 :  * subselects of an already-planned expression tree.  This is OK for current
    1977                 :  * uses, but may need to be revisited in future.
    1978                 :  */
    1979                 : 
    1980                 : bool
    1981 GNC    51209230 : expression_tree_walker_impl(Node *node,
    1982                 :                             tree_walker_callback walker,
    1983                 :                             void *context)
    1984                 : {
    1985                 :     ListCell   *temp;
    1986                 : 
    1987                 :     /*
    1988                 :      * The walker has already visited the current node, and so we need only
    1989                 :      * recurse into any sub-nodes it has.
    1990                 :      *
    1991                 :      * We assume that the walker is not interested in List nodes per se, so
    1992                 :      * when we expect a List we just recurse directly to self without
    1993                 :      * bothering to call the walker.
    1994                 :      */
    1995                 : #define WALK(n) walker((Node *) (n), context)
    1996                 : 
    1997                 : #define LIST_WALK(l) expression_tree_walker_impl((Node *) (l), walker, context)
    1998                 : 
    1999 GIC    51209230 :     if (node == NULL)
    2000         1099597 :         return false;
    2001                 : 
    2002                 :     /* Guard against stack overflow due to overly complex expressions */
    2003        50109633 :     check_stack_depth();
    2004                 : 
    2005        50109630 :     switch (nodeTag(node))
    2006                 :     {
    2007        18345149 :         case T_Var:
    2008                 :         case T_Const:
    2009                 :         case T_Param:
    2010                 :         case T_CaseTestExpr:
    2011                 :         case T_CoerceToDomainValue:
    2012                 :         case T_SetToDefault:
    2013                 :         case T_CurrentOfExpr:
    2014                 :         case T_NextValueExpr:
    2015                 :         case T_RangeTblRef:
    2016                 :         case T_SortGroupClause:
    2017                 :         case T_CTESearchClause:
    2018                 :             /* primitive node types with no expression subnodes */
    2019        18345149 :             break;
    2020            3162 :         case T_WithCheckOption:
    2021 GNC        3162 :             return WALK(((WithCheckOption *) node)->qual);
    2022 GIC      138826 :         case T_Aggref:
    2023                 :             {
    2024          138826 :                 Aggref     *expr = (Aggref *) node;
    2025                 : 
    2026                 :                 /* recurse directly on Lists */
    2027 GNC      138826 :                 if (LIST_WALK(expr->aggdirectargs))
    2028 UIC           0 :                     return true;
    2029 GNC      138826 :                 if (LIST_WALK(expr->args))
    2030 GIC        9175 :                     return true;
    2031 GNC      129651 :                 if (LIST_WALK(expr->aggorder))
    2032 UIC           0 :                     return true;
    2033 GNC      129651 :                 if (LIST_WALK(expr->aggdistinct))
    2034 UIC           0 :                     return true;
    2035 GNC      129651 :                 if (WALK(expr->aggfilter))
    2036 GIC          31 :                     return true;
    2037                 :             }
    2038          129620 :             break;
    2039            1444 :         case T_GroupingFunc:
    2040                 :             {
    2041            1444 :                 GroupingFunc *grouping = (GroupingFunc *) node;
    2042                 : 
    2043 GNC        1444 :                 if (LIST_WALK(grouping->args))
    2044 GIC           6 :                     return true;
    2045                 :             }
    2046            1438 :             break;
    2047            8583 :         case T_WindowFunc:
    2048 ECB             :             {
    2049 GIC        8583 :                 WindowFunc *expr = (WindowFunc *) node;
    2050                 : 
    2051                 :                 /* recurse directly on List */
    2052 GNC        8583 :                 if (LIST_WALK(expr->args))
    2053 GIC         309 :                     return true;
    2054 GNC        8274 :                 if (WALK(expr->aggfilter))
    2055 GIC           6 :                     return true;
    2056                 :             }
    2057            8268 :             break;
    2058           77121 :         case T_SubscriptingRef:
    2059                 :             {
    2060           77121 :                 SubscriptingRef *sbsref = (SubscriptingRef *) node;
    2061                 : 
    2062                 :                 /* recurse directly for upper/lower container index lists */
    2063 GNC       77121 :                 if (LIST_WALK(sbsref->refupperindexpr))
    2064 CBC        4468 :                     return true;
    2065 GNC       72653 :                 if (LIST_WALK(sbsref->reflowerindexpr))
    2066 UIC           0 :                     return true;
    2067 ECB             :                 /* walker must see the refexpr and refassgnexpr, however */
    2068 GNC       72653 :                 if (WALK(sbsref->refexpr))
    2069 CBC        2171 :                     return true;
    2070                 : 
    2071 GNC       70482 :                 if (WALK(sbsref->refassgnexpr))
    2072 GIC          56 :                     return true;
    2073                 :             }
    2074           70426 :             break;
    2075         2345213 :         case T_FuncExpr:
    2076                 :             {
    2077         2345213 :                 FuncExpr   *expr = (FuncExpr *) node;
    2078                 : 
    2079 GNC     2345213 :                 if (LIST_WALK(expr->args))
    2080 GIC       31561 :                     return true;
    2081                 :             }
    2082 CBC     2313649 :             break;
    2083           35516 :         case T_NamedArgExpr:
    2084 GNC       35516 :             return WALK(((NamedArgExpr *) node)->arg);
    2085 CBC     4003993 :         case T_OpExpr:
    2086                 :         case T_DistinctExpr:    /* struct-equivalent to OpExpr */
    2087 ECB             :         case T_NullIfExpr:      /* struct-equivalent to OpExpr */
    2088                 :             {
    2089 GIC     4003993 :                 OpExpr     *expr = (OpExpr *) node;
    2090 ECB             : 
    2091 GNC     4003993 :                 if (LIST_WALK(expr->args))
    2092 CBC       28950 :                     return true;
    2093 ECB             :             }
    2094 GBC     3974986 :             break;
    2095 CBC      272779 :         case T_ScalarArrayOpExpr:
    2096 EUB             :             {
    2097 CBC      272779 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    2098 ECB             : 
    2099 GNC      272779 :                 if (LIST_WALK(expr->args))
    2100 CBC       16764 :                     return true;
    2101                 :             }
    2102          256015 :             break;
    2103 GIC      657963 :         case T_BoolExpr:
    2104 ECB             :             {
    2105 CBC      657963 :                 BoolExpr   *expr = (BoolExpr *) node;
    2106                 : 
    2107 GNC      657963 :                 if (LIST_WALK(expr->args))
    2108 GIC        8215 :                     return true;
    2109 ECB             :             }
    2110 GIC      649745 :             break;
    2111          163080 :         case T_SubLink:
    2112 ECB             :             {
    2113 CBC      163080 :                 SubLink    *sublink = (SubLink *) node;
    2114 ECB             : 
    2115 GNC      163080 :                 if (WALK(sublink->testexpr))
    2116 GIC          24 :                     return true;
    2117 ECB             : 
    2118                 :                 /*
    2119                 :                  * Also invoke the walker on the sublink's Query node, so it
    2120                 :                  * can recurse into the sub-query if it wants to.
    2121                 :                  */
    2122 GNC      163056 :                 return WALK(sublink->subselect);
    2123 ECB             :             }
    2124                 :             break;
    2125 CBC       37843 :         case T_SubPlan:
    2126 EUB             :             {
    2127 GIC       37843 :                 SubPlan    *subplan = (SubPlan *) node;
    2128 ECB             : 
    2129                 :                 /* recurse into the testexpr, but not into the Plan */
    2130 GNC       37843 :                 if (WALK(subplan->testexpr))
    2131 CBC          30 :                     return true;
    2132 ECB             :                 /* also examine args list */
    2133 GNC       37813 :                 if (LIST_WALK(subplan->args))
    2134 CBC         198 :                     return true;
    2135                 :             }
    2136           37615 :             break;
    2137 GIC        3043 :         case T_AlternativeSubPlan:
    2138 GNC        3043 :             return LIST_WALK(((AlternativeSubPlan *) node)->subplans);
    2139 CBC      162861 :         case T_FieldSelect:
    2140 GNC      162861 :             return WALK(((FieldSelect *) node)->arg);
    2141 CBC        1087 :         case T_FieldStore:
    2142 ECB             :             {
    2143 CBC        1087 :                 FieldStore *fstore = (FieldStore *) node;
    2144 ECB             : 
    2145 GNC        1087 :                 if (WALK(fstore->arg))
    2146 UIC           0 :                     return true;
    2147 GNC        1087 :                 if (WALK(fstore->newvals))
    2148 CBC           3 :                     return true;
    2149                 :             }
    2150            1084 :             break;
    2151          598609 :         case T_RelabelType:
    2152 GNC      598609 :             return WALK(((RelabelType *) node)->arg);
    2153 CBC      110780 :         case T_CoerceViaIO:
    2154 GNC      110780 :             return WALK(((CoerceViaIO *) node)->arg);
    2155 GIC       25153 :         case T_ArrayCoerceExpr:
    2156 ECB             :             {
    2157 GIC       25153 :                 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
    2158 ECB             : 
    2159 GNC       25153 :                 if (WALK(acoerce->arg))
    2160 GIC        1573 :                     return true;
    2161 GNC       23580 :                 if (WALK(acoerce->elemexpr))
    2162 CBC          12 :                     return true;
    2163                 :             }
    2164           23568 :             break;
    2165 GIC        1406 :         case T_ConvertRowtypeExpr:
    2166 GNC        1406 :             return WALK(((ConvertRowtypeExpr *) node)->arg);
    2167 CBC       13413 :         case T_CollateExpr:
    2168 GNC       13413 :             return WALK(((CollateExpr *) node)->arg);
    2169 CBC      207969 :         case T_CaseExpr:
    2170 ECB             :             {
    2171 GIC      207969 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    2172 ECB             : 
    2173 GNC      207969 :                 if (WALK(caseexpr->arg))
    2174 CBC          32 :                     return true;
    2175 ECB             :                 /* we assume walker doesn't care about CaseWhens, either */
    2176 GIC      568178 :                 foreach(temp, caseexpr->args)
    2177                 :                 {
    2178          364207 :                     CaseWhen   *when = lfirst_node(CaseWhen, temp);
    2179                 : 
    2180 GNC      364207 :                     if (WALK(when->expr))
    2181 CBC        3966 :                         return true;
    2182 GNC      362678 :                     if (WALK(when->result))
    2183 GIC        2437 :                         return true;
    2184 ECB             :                 }
    2185 GNC      203971 :                 if (WALK(caseexpr->defresult))
    2186 CBC        2877 :                     return true;
    2187                 :             }
    2188 GIC      201094 :             break;
    2189 CBC      182968 :         case T_ArrayExpr:
    2190 GNC      182968 :             return WALK(((ArrayExpr *) node)->elements);
    2191 GIC       14438 :         case T_RowExpr:
    2192 ECB             :             /* Assume colnames isn't interesting */
    2193 GNC       14438 :             return WALK(((RowExpr *) node)->args);
    2194 GIC         789 :         case T_RowCompareExpr:
    2195 ECB             :             {
    2196 CBC         789 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    2197 ECB             : 
    2198 GNC         789 :                 if (WALK(rcexpr->largs))
    2199 LBC           0 :                     return true;
    2200 GNC         789 :                 if (WALK(rcexpr->rargs))
    2201 UIC           0 :                     return true;
    2202 ECB             :             }
    2203 GIC         789 :             break;
    2204 CBC       58764 :         case T_CoalesceExpr:
    2205 GNC       58764 :             return WALK(((CoalesceExpr *) node)->args);
    2206 CBC        2816 :         case T_MinMaxExpr:
    2207 GNC        2816 :             return WALK(((MinMaxExpr *) node)->args);
    2208 GIC        2499 :         case T_XmlExpr:
    2209 ECB             :             {
    2210 CBC        2499 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    2211 ECB             : 
    2212 GNC        2499 :                 if (WALK(xexpr->named_args))
    2213 CBC           6 :                     return true;
    2214 ECB             :                 /* we assume walker doesn't care about arg_names */
    2215 GNC        2493 :                 if (WALK(xexpr->args))
    2216 CBC          12 :                     return true;
    2217                 :             }
    2218            2481 :             break;
    2219 GNC         438 :         case T_JsonValueExpr:
    2220                 :             {
    2221             438 :                 JsonValueExpr *jve = (JsonValueExpr *) node;
    2222                 : 
    2223             438 :                 if (WALK(jve->raw_expr))
    2224 UNC           0 :                     return true;
    2225 GNC         438 :                 if (WALK(jve->formatted_expr))
    2226 UNC           0 :                     return true;
    2227                 :             }
    2228 GNC         438 :             break;
    2229            2931 :         case T_JsonConstructorExpr:
    2230                 :             {
    2231            2931 :                 JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
    2232                 : 
    2233            2931 :                 if (WALK(ctor->args))
    2234 UNC           0 :                     return true;
    2235 GNC        2931 :                 if (WALK(ctor->func))
    2236               9 :                     return true;
    2237            2922 :                 if (WALK(ctor->coercion))
    2238 UNC           0 :                     return true;
    2239                 :             }
    2240 GNC        2922 :             break;
    2241            1122 :         case T_JsonIsPredicate:
    2242            1122 :             return WALK(((JsonIsPredicate *) node)->expr);
    2243 CBC      155074 :         case T_NullTest:
    2244 GNC      155074 :             return WALK(((NullTest *) node)->arg);
    2245 CBC        3042 :         case T_BooleanTest:
    2246 GNC        3042 :             return WALK(((BooleanTest *) node)->arg);
    2247 CBC      879189 :         case T_CoerceToDomain:
    2248 GNC      879189 :             return WALK(((CoerceToDomain *) node)->arg);
    2249 CBC     8444763 :         case T_TargetEntry:
    2250 GNC     8444763 :             return WALK(((TargetEntry *) node)->expr);
    2251 CBC       68279 :         case T_Query:
    2252 ECB             :             /* Do nothing with a sub-Query, per discussion above */
    2253 GIC       68279 :             break;
    2254 CBC         333 :         case T_WindowClause:
    2255                 :             {
    2256             333 :                 WindowClause *wc = (WindowClause *) node;
    2257 ECB             : 
    2258 GNC         333 :                 if (WALK(wc->partitionClause))
    2259 LBC           0 :                     return true;
    2260 GNC         333 :                 if (WALK(wc->orderClause))
    2261 LBC           0 :                     return true;
    2262 GNC         333 :                 if (WALK(wc->startOffset))
    2263 LBC           0 :                     return true;
    2264 GNC         333 :                 if (WALK(wc->endOffset))
    2265 LBC           0 :                     return true;
    2266 ECB             :             }
    2267 GIC         333 :             break;
    2268 CBC          42 :         case T_CTECycleClause:
    2269 ECB             :             {
    2270 GIC          42 :                 CTECycleClause *cc = (CTECycleClause *) node;
    2271 ECB             : 
    2272 GNC          42 :                 if (WALK(cc->cycle_mark_value))
    2273 LBC           0 :                     return true;
    2274 GNC          42 :                 if (WALK(cc->cycle_mark_default))
    2275 UIC           0 :                     return true;
    2276 ECB             :             }
    2277 CBC          42 :             break;
    2278 GIC        3450 :         case T_CommonTableExpr:
    2279 ECB             :             {
    2280 GIC        3450 :                 CommonTableExpr *cte = (CommonTableExpr *) node;
    2281 ECB             : 
    2282 EUB             :                 /*
    2283 ECB             :                  * Invoke the walker on the CTE's Query node, so it can
    2284 EUB             :                  * recurse into the sub-query if it wants to.
    2285                 :                  */
    2286 GNC        3450 :                 if (WALK(cte->ctequery))
    2287 CBC          46 :                     return true;
    2288 ECB             : 
    2289 GNC        3404 :                 if (WALK(cte->search_clause))
    2290 LBC           0 :                     return true;
    2291 GNC        3404 :                 if (WALK(cte->cycle_clause))
    2292 UIC           0 :                     return true;
    2293 ECB             :             }
    2294 GIC        3404 :             break;
    2295 UNC           0 :         case T_JsonKeyValue:
    2296                 :             {
    2297               0 :                 JsonKeyValue *kv = (JsonKeyValue *) node;
    2298                 : 
    2299               0 :                 if (WALK(kv->key))
    2300               0 :                     return true;
    2301               0 :                 if (WALK(kv->value))
    2302               0 :                     return true;
    2303                 :             }
    2304               0 :             break;
    2305               0 :         case T_JsonObjectConstructor:
    2306                 :             {
    2307               0 :                 JsonObjectConstructor *ctor = (JsonObjectConstructor *) node;
    2308                 : 
    2309               0 :                 if (LIST_WALK(ctor->exprs))
    2310               0 :                     return true;
    2311                 :             }
    2312               0 :             break;
    2313               0 :         case T_JsonArrayConstructor:
    2314                 :             {
    2315               0 :                 JsonArrayConstructor *ctor = (JsonArrayConstructor *) node;
    2316                 : 
    2317               0 :                 if (LIST_WALK(ctor->exprs))
    2318               0 :                     return true;
    2319                 :             }
    2320               0 :             break;
    2321               0 :         case T_JsonArrayQueryConstructor:
    2322                 :             {
    2323               0 :                 JsonArrayQueryConstructor *ctor = (JsonArrayQueryConstructor *) node;
    2324                 : 
    2325               0 :                 if (WALK(ctor->query))
    2326               0 :                     return true;
    2327                 :             }
    2328               0 :             break;
    2329               0 :         case T_JsonAggConstructor:
    2330                 :             {
    2331               0 :                 JsonAggConstructor *ctor = (JsonAggConstructor *) node;
    2332                 : 
    2333               0 :                 if (WALK(ctor->agg_filter))
    2334               0 :                     return true;
    2335               0 :                 if (WALK(ctor->agg_order))
    2336               0 :                     return true;
    2337               0 :                 if (WALK(ctor->over))
    2338               0 :                     return true;
    2339                 :             }
    2340               0 :             break;
    2341               0 :         case T_JsonObjectAgg:
    2342                 :             {
    2343               0 :                 JsonObjectAgg *ctor = (JsonObjectAgg *) node;
    2344                 : 
    2345               0 :                 if (WALK(ctor->constructor))
    2346               0 :                     return true;
    2347               0 :                 if (WALK(ctor->arg))
    2348               0 :                     return true;
    2349                 :             }
    2350               0 :             break;
    2351               0 :         case T_JsonArrayAgg:
    2352                 :             {
    2353               0 :                 JsonArrayAgg *ctor = (JsonArrayAgg *) node;
    2354                 : 
    2355               0 :                 if (WALK(ctor->constructor))
    2356               0 :                     return true;
    2357               0 :                 if (WALK(ctor->arg))
    2358               0 :                     return true;
    2359                 :             }
    2360               0 :             break;
    2361                 : 
    2362 CBC        1715 :         case T_PartitionBoundSpec:
    2363 ECB             :             {
    2364 GIC        1715 :                 PartitionBoundSpec *pbs = (PartitionBoundSpec *) node;
    2365 ECB             : 
    2366 GNC        1715 :                 if (WALK(pbs->listdatums))
    2367 UIC           0 :                     return true;
    2368 GNC        1715 :                 if (WALK(pbs->lowerdatums))
    2369 LBC           0 :                     return true;
    2370 GNC        1715 :                 if (WALK(pbs->upperdatums))
    2371 LBC           0 :                     return true;
    2372                 :             }
    2373 CBC        1715 :             break;
    2374 GBC        2418 :         case T_PartitionRangeDatum:
    2375 ECB             :             {
    2376 GBC        2418 :                 PartitionRangeDatum *prd = (PartitionRangeDatum *) node;
    2377                 : 
    2378 GNC        2418 :                 if (WALK(prd->value))
    2379 LBC           0 :                     return true;
    2380                 :             }
    2381 CBC        2418 :             break;
    2382 GIC    11863660 :         case T_List:
    2383 CBC    39801875 :             foreach(temp, (List *) node)
    2384 EUB             :             {
    2385 GNC    28285622 :                 if (WALK(lfirst(temp)))
    2386 CBC      347257 :                     return true;
    2387 ECB             :             }
    2388 GBC    11516253 :             break;
    2389 GIC      834636 :         case T_FromExpr:
    2390 ECB             :             {
    2391 CBC      834636 :                 FromExpr   *from = (FromExpr *) node;
    2392 ECB             : 
    2393 GNC      834636 :                 if (LIST_WALK(from->fromlist))
    2394 CBC       25058 :                     return true;
    2395 GNC      809578 :                 if (WALK(from->quals))
    2396 CBC        1083 :                     return true;
    2397 ECB             :             }
    2398 CBC      808486 :             break;
    2399            1207 :         case T_OnConflictExpr:
    2400 ECB             :             {
    2401 CBC        1207 :                 OnConflictExpr *onconflict = (OnConflictExpr *) node;
    2402                 : 
    2403 GNC        1207 :                 if (WALK(onconflict->arbiterElems))
    2404 LBC           0 :                     return true;
    2405 GNC        1207 :                 if (WALK(onconflict->arbiterWhere))
    2406 LBC           0 :                     return true;
    2407 GNC        1207 :                 if (WALK(onconflict->onConflictSet))
    2408 LBC           0 :                     return true;
    2409 GNC        1207 :                 if (WALK(onconflict->onConflictWhere))
    2410 LBC           0 :                     return true;
    2411 GNC        1207 :                 if (WALK(onconflict->exclRelTlist))
    2412 LBC           0 :                     return true;
    2413 EUB             :             }
    2414 CBC        1207 :             break;
    2415 GBC        1159 :         case T_MergeAction:
    2416                 :             {
    2417 CBC        1159 :                 MergeAction *action = (MergeAction *) node;
    2418 ECB             : 
    2419 GNC        1159 :                 if (WALK(action->qual))
    2420 CBC          58 :                     return true;
    2421 GNC        1101 :                 if (WALK(action->targetList))
    2422 CBC         128 :                     return true;
    2423 EUB             :             }
    2424 CBC         973 :             break;
    2425 UBC           0 :         case T_PartitionPruneStepOp:
    2426                 :             {
    2427 LBC           0 :                 PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
    2428 ECB             : 
    2429 UNC           0 :                 if (WALK(opstep->exprs))
    2430 LBC           0 :                     return true;
    2431                 :             }
    2432 UIC           0 :             break;
    2433               0 :         case T_PartitionPruneStepCombine:
    2434                 :             /* no expression subnodes */
    2435               0 :             break;
    2436 CBC      233100 :         case T_JoinExpr:
    2437 ECB             :             {
    2438 GIC      233100 :                 JoinExpr   *join = (JoinExpr *) node;
    2439 ECB             : 
    2440 GNC      233100 :                 if (WALK(join->larg))
    2441 CBC        5693 :                     return true;
    2442 GNC      227407 :                 if (WALK(join->rarg))
    2443 GIC        6930 :                     return true;
    2444 GNC      220477 :                 if (WALK(join->quals))
    2445 GBC          30 :                     return true;
    2446                 : 
    2447 EUB             :                 /*
    2448                 :                  * alias clause, using list are deemed uninteresting.
    2449                 :                  */
    2450                 :             }
    2451 GBC      220447 :             break;
    2452           29597 :         case T_SetOperationStmt:
    2453                 :             {
    2454           29597 :                 SetOperationStmt *setop = (SetOperationStmt *) node;
    2455 EUB             : 
    2456 GNC       29597 :                 if (WALK(setop->larg))
    2457 UBC           0 :                     return true;
    2458 GNC       29597 :                 if (WALK(setop->rarg))
    2459 UBC           0 :                     return true;
    2460 EUB             : 
    2461                 :                 /* groupClauses are deemed uninteresting */
    2462                 :             }
    2463 GBC       29597 :             break;
    2464 UIC           0 :         case T_IndexClause:
    2465 EUB             :             {
    2466 UIC           0 :                 IndexClause *iclause = (IndexClause *) node;
    2467 EUB             : 
    2468 UNC           0 :                 if (WALK(iclause->rinfo))
    2469 UIC           0 :                     return true;
    2470 UNC           0 :                 if (LIST_WALK(iclause->indexquals))
    2471 UIC           0 :                     return true;
    2472 EUB             :             }
    2473 UIC           0 :             break;
    2474 GBC        7826 :         case T_PlaceHolderVar:
    2475 GNC        7826 :             return WALK(((PlaceHolderVar *) node)->phexpr);
    2476 GIC        1236 :         case T_InferenceElem:
    2477 GNC        1236 :             return WALK(((InferenceElem *) node)->expr);
    2478 GBC         404 :         case T_AppendRelInfo:
    2479                 :             {
    2480             404 :                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
    2481                 : 
    2482 GNC         404 :                 if (LIST_WALK(appinfo->translated_vars))
    2483 UBC           0 :                     return true;
    2484 EUB             :             }
    2485 GBC         404 :             break;
    2486 UBC           0 :         case T_PlaceHolderInfo:
    2487 UNC           0 :             return WALK(((PlaceHolderInfo *) node)->ph_var);
    2488 GBC       95854 :         case T_RangeTblFunction:
    2489 GNC       95854 :             return WALK(((RangeTblFunction *) node)->funcexpr);
    2490 GIC         301 :         case T_TableSampleClause:
    2491 EUB             :             {
    2492 GIC         301 :                 TableSampleClause *tsc = (TableSampleClause *) node;
    2493 EUB             : 
    2494 GNC         301 :                 if (LIST_WALK(tsc->args))
    2495 UBC           0 :                     return true;
    2496 GNC         301 :                 if (WALK(tsc->repeatable))
    2497 UBC           0 :                     return true;
    2498 EUB             :             }
    2499 GIC         301 :             break;
    2500 GBC         587 :         case T_TableFunc:
    2501                 :             {
    2502             587 :                 TableFunc  *tf = (TableFunc *) node;
    2503 EUB             : 
    2504 GNC         587 :                 if (WALK(tf->ns_uris))
    2505 UBC           0 :                     return true;
    2506 GNC         587 :                 if (WALK(tf->docexpr))
    2507 UBC           0 :                     return true;
    2508 GNC         587 :                 if (WALK(tf->rowexpr))
    2509 LBC           0 :                     return true;
    2510 GNC         587 :                 if (WALK(tf->colexprs))
    2511 LBC           0 :                     return true;
    2512 GNC         587 :                 if (WALK(tf->coldefexprs))
    2513 LBC           0 :                     return true;
    2514 EUB             :             }
    2515 CBC         587 :             break;
    2516 UBC           0 :         default:
    2517 LBC           0 :             elog(ERROR, "unrecognized node type: %d",
    2518 EUB             :                  (int) nodeTag(node));
    2519                 :             break;
    2520 ECB             :     }
    2521 CBC    38673733 :     return false;
    2522                 : 
    2523                 :     /* The WALK() macro can be re-used below, but LIST_WALK() not so much */
    2524                 : #undef LIST_WALK
    2525                 : }
    2526 ECB             : 
    2527                 : /*
    2528                 :  * query_tree_walker --- initiate a walk of a Query's expressions
    2529 EUB             :  *
    2530                 :  * This routine exists just to reduce the number of places that need to know
    2531 ECB             :  * where all the expression subtrees of a Query are.  Note it can be used
    2532                 :  * for starting a walk at top level of a Query regardless of whether the
    2533                 :  * walker intends to descend into subqueries.  It is also useful for
    2534                 :  * descending into subqueries within a walker.
    2535                 :  *
    2536                 :  * Some callers want to suppress visitation of certain items in the sub-Query,
    2537                 :  * typically because they need to process them specially, or don't actually
    2538                 :  * want to recurse into subqueries.  This is supported by the flags argument,
    2539                 :  * which is the bitwise OR of flag values to add or suppress visitation of
    2540                 :  * indicated items.  (More flag bits may be added as needed.)
    2541                 :  */
    2542                 : bool
    2543 GNC      971536 : query_tree_walker_impl(Query *query,
    2544                 :                        tree_walker_callback walker,
    2545                 :                        void *context,
    2546                 :                        int flags)
    2547                 : {
    2548 CBC      971536 :     Assert(query != NULL && IsA(query, Query));
    2549 ECB             : 
    2550                 :     /*
    2551                 :      * We don't walk any utilityStmt here. However, we can't easily assert
    2552                 :      * that it is absent, since there are at least two code paths by which
    2553                 :      * action statements from CREATE RULE end up here, and NOTIFY is allowed
    2554 EUB             :      * in a rule action.
    2555 ECB             :      */
    2556 EUB             : 
    2557 GNC      971536 :     if (WALK(query->targetList))
    2558 GBC      138098 :         return true;
    2559 GNC      833417 :     if (WALK(query->withCheckOptions))
    2560 UBC           0 :         return true;
    2561 GNC      833417 :     if (WALK(query->onConflict))
    2562 UBC           0 :         return true;
    2563 GNC      833417 :     if (WALK(query->mergeActionList))
    2564 CBC         186 :         return true;
    2565 GNC      833231 :     if (WALK(query->returningList))
    2566 GIC          37 :         return true;
    2567 GNC      833194 :     if (WALK(query->jointree))
    2568 GIC       25937 :         return true;
    2569 GNC      807248 :     if (WALK(query->setOperations))
    2570 LBC           0 :         return true;
    2571 GNC      807248 :     if (WALK(query->havingQual))
    2572 LBC           0 :         return true;
    2573 GNC      807248 :     if (WALK(query->limitOffset))
    2574 CBC           3 :         return true;
    2575 GNC      807245 :     if (WALK(query->limitCount))
    2576 UIC           0 :         return true;
    2577 EUB             : 
    2578                 :     /*
    2579                 :      * Most callers aren't interested in SortGroupClause nodes since those
    2580                 :      * don't contain actual expressions. However they do contain OIDs which
    2581                 :      * may be needed by dependency walkers etc.
    2582                 :      */
    2583 GBC      807245 :     if ((flags & QTW_EXAMINE_SORTGROUP))
    2584                 :     {
    2585 GNC      100773 :         if (WALK(query->groupClause))
    2586 LBC           0 :             return true;
    2587 GNC      100773 :         if (WALK(query->windowClause))
    2588 LBC           0 :             return true;
    2589 GNC      100773 :         if (WALK(query->sortClause))
    2590 LBC           0 :             return true;
    2591 GNC      100773 :         if (WALK(query->distinctClause))
    2592 LBC           0 :             return true;
    2593 ECB             :     }
    2594                 :     else
    2595                 :     {
    2596                 :         /*
    2597                 :          * But we need to walk the expressions under WindowClause nodes even
    2598                 :          * if we're not interested in SortGroupClause nodes.
    2599                 :          */
    2600                 :         ListCell   *lc;
    2601                 : 
    2602 CBC      709451 :         foreach(lc, query->windowClause)
    2603                 :         {
    2604            2982 :             WindowClause *wc = lfirst_node(WindowClause, lc);
    2605                 : 
    2606 GNC        2982 :             if (WALK(wc->startOffset))
    2607 GBC           3 :                 return true;
    2608 GNC        2979 :             if (WALK(wc->endOffset))
    2609 UBC           0 :                 return true;
    2610                 :         }
    2611                 :     }
    2612                 : 
    2613 ECB             :     /*
    2614 EUB             :      * groupingSets and rowMarks are not walked:
    2615                 :      *
    2616                 :      * groupingSets contain only ressortgrouprefs (integers) which are
    2617                 :      * meaningless without the corresponding groupClause or tlist.
    2618                 :      * Accordingly, any walker that needs to care about them needs to handle
    2619                 :      * them itself in its Query processing.
    2620                 :      *
    2621                 :      * rowMarks is not walked because it contains only rangetable indexes (and
    2622                 :      * flags etc.) and therefore should be handled at Query level similarly.
    2623                 :      */
    2624 ECB             : 
    2625 CBC      807242 :     if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
    2626 ECB             :     {
    2627 GNC      442280 :         if (WALK(query->cteList))
    2628 CBC          43 :             return true;
    2629                 :     }
    2630          807199 :     if (!(flags & QTW_IGNORE_RANGE_TABLE))
    2631                 :     {
    2632          469266 :         if (range_table_walker(query->rtable, walker, context, flags))
    2633 GBC        7545 :             return true;
    2634                 :     }
    2635 CBC      799654 :     return false;
    2636 EUB             : }
    2637                 : 
    2638 ECB             : /*
    2639                 :  * range_table_walker is just the part of query_tree_walker that scans
    2640                 :  * a query's rangetable.  This is split out since it can be useful on
    2641                 :  * its own.
    2642                 :  */
    2643                 : bool
    2644 GNC      469891 : range_table_walker_impl(List *rtable,
    2645                 :                         tree_walker_callback walker,
    2646                 :                         void *context,
    2647                 :                         int flags)
    2648                 : {
    2649 ECB             :     ListCell   *rt;
    2650                 : 
    2651 GIC     1309552 :     foreach(rt, rtable)
    2652 ECB             :     {
    2653 GIC      847206 :         RangeTblEntry *rte = lfirst_node(RangeTblEntry, rt);
    2654 ECB             : 
    2655 GBC      847206 :         if (range_table_entry_walker(rte, walker, context, flags))
    2656 CBC        7545 :             return true;
    2657 EUB             :     }
    2658 CBC      462346 :     return false;
    2659 EUB             : }
    2660 ECB             : 
    2661 EUB             : /*
    2662 ECB             :  * Some callers even want to scan the expressions in individual RTEs.
    2663 EUB             :  */
    2664                 : bool
    2665 GNC      847218 : range_table_entry_walker_impl(RangeTblEntry *rte,
    2666                 :                               tree_walker_callback walker,
    2667                 :                               void *context,
    2668                 :                               int flags)
    2669                 : {
    2670                 :     /*
    2671 ECB             :      * Walkers might need to examine the RTE node itself either before or
    2672                 :      * after visiting its contents (or, conceivably, both).  Note that if you
    2673                 :      * specify neither flag, the walker won't be called on the RTE at all.
    2674                 :      */
    2675 GIC      847218 :     if (flags & QTW_EXAMINE_RTES_BEFORE)
    2676 GNC       42606 :         if (WALK(rte))
    2677 GIC           6 :             return true;
    2678                 : 
    2679          847212 :     switch (rte->rtekind)
    2680                 :     {
    2681          541953 :         case RTE_RELATION:
    2682 GNC      541953 :             if (WALK(rte->tablesample))
    2683 UIC           0 :                 return true;
    2684 GIC      541953 :             break;
    2685           84675 :         case RTE_SUBQUERY:
    2686           84675 :             if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
    2687 GNC       83565 :                 if (WALK(rte->subquery))
    2688 GIC         410 :                     return true;
    2689           84265 :             break;
    2690          146211 :         case RTE_JOIN:
    2691          146211 :             if (!(flags & QTW_IGNORE_JOINALIASES))
    2692 GNC       58030 :                 if (WALK(rte->joinaliasvars))
    2693 LBC           0 :                     return true;
    2694 GIC      146211 :             break;
    2695           54983 :         case RTE_FUNCTION:
    2696 GNC       54983 :             if (WALK(rte->functions))
    2697 GIC        7106 :                 return true;
    2698 CBC       47877 :             break;
    2699 GIC         193 :         case RTE_TABLEFUNC:
    2700 GNC         193 :             if (WALK(rte->tablefunc))
    2701 UIC           0 :                 return true;
    2702 GIC         193 :             break;
    2703            7670 :         case RTE_VALUES:
    2704 GNC        7670 :             if (WALK(rte->values_lists))
    2705 GIC          35 :                 return true;
    2706            7635 :             break;
    2707 CBC       11527 :         case RTE_CTE:
    2708 ECB             :         case RTE_NAMEDTUPLESTORE:
    2709                 :         case RTE_RESULT:
    2710 EUB             :             /* nothing to do */
    2711 CBC       11527 :             break;
    2712 EUB             :     }
    2713 ECB             : 
    2714 GNC      839661 :     if (WALK(rte->securityQuals))
    2715 LBC           0 :         return true;
    2716 ECB             : 
    2717 CBC      839661 :     if (flags & QTW_EXAMINE_RTES_AFTER)
    2718 GNC        9125 :         if (WALK(rte))
    2719 LBC           0 :             return true;
    2720 EUB             : 
    2721 CBC      839661 :     return false;
    2722 EUB             : }
    2723 ECB             : 
    2724                 : 
    2725                 : /*
    2726 EUB             :  * expression_tree_mutator() is designed to support routines that make a
    2727                 :  * modified copy of an expression tree, with some nodes being added,
    2728                 :  * removed, or replaced by new subtrees.  The original tree is (normally)
    2729                 :  * not changed.  Each recursion level is responsible for returning a copy of
    2730                 :  * (or appropriately modified substitute for) the subtree it is handed.
    2731                 :  * A mutator routine should look like this:
    2732                 :  *
    2733 ECB             :  * Node * my_mutator (Node *node, my_struct *context)
    2734                 :  * {
    2735                 :  *      if (node == NULL)
    2736 EUB             :  *          return NULL;
    2737 ECB             :  *      // check for nodes that special work is required for, eg:
    2738 EUB             :  *      if (IsA(node, Var))
    2739 ECB             :  *      {
    2740 EUB             :  *          ... create and return modified copy of Var node
    2741 ECB             :  *      }
    2742 EUB             :  *      else if (IsA(node, ...))
    2743                 :  *      {
    2744                 :  *          ... do special transformations of other node types
    2745                 :  *      }
    2746                 :  *      // for any node type not specially processed, do:
    2747                 :  *      return expression_tree_mutator(node, my_mutator, (void *) context);
    2748                 :  * }
    2749                 :  *
    2750                 :  * The "context" argument points to a struct that holds whatever context
    2751                 :  * information the mutator routine needs --- it can be used to return extra
    2752 ECB             :  * data gathered by the mutator, too.  This argument is not touched by
    2753                 :  * expression_tree_mutator, but it is passed down to recursive sub-invocations
    2754                 :  * of my_mutator.  The tree walk is started from a setup routine that
    2755                 :  * fills in the appropriate context struct, calls my_mutator with the
    2756                 :  * top-level node of the tree, and does any required post-processing.
    2757                 :  *
    2758                 :  * Each level of recursion must return an appropriately modified Node.
    2759 EUB             :  * If expression_tree_mutator() is called, it will make an exact copy
    2760                 :  * of the given Node, but invoke my_mutator() to copy the sub-node(s)
    2761                 :  * of that Node.  In this way, my_mutator() has full control over the
    2762                 :  * copying process but need not directly deal with expression trees
    2763                 :  * that it has no interest in.
    2764                 :  *
    2765                 :  * Just as for expression_tree_walker, the node types handled by
    2766                 :  * expression_tree_mutator include all those normally found in target lists
    2767                 :  * and qualifier clauses during the planning stage.
    2768                 :  *
    2769                 :  * expression_tree_mutator will handle SubLink nodes by recursing normally
    2770                 :  * into the "testexpr" subtree (which is an expression belonging to the outer
    2771                 :  * plan).  It will also call the mutator on the sub-Query node; however, when
    2772                 :  * expression_tree_mutator itself is called on a Query node, it does nothing
    2773                 :  * and returns the unmodified Query node.  The net effect is that unless the
    2774                 :  * mutator does something special at a Query node, sub-selects will not be
    2775 ECB             :  * visited or modified; the original sub-select will be linked to by the new
    2776                 :  * SubLink node.  Mutators that want to descend into sub-selects will usually
    2777                 :  * do so by recognizing Query nodes and calling query_tree_mutator (below).
    2778                 :  *
    2779                 :  * expression_tree_mutator will handle a SubPlan node by recursing into the
    2780                 :  * "testexpr" and the "args" list (which belong to the outer plan), but it
    2781                 :  * will simply copy the link to the inner plan, since that's typically what
    2782                 :  * expression tree mutators want.  A mutator that wants to modify the subplan
    2783                 :  * can force appropriate behavior by recognizing SubPlan expression nodes
    2784                 :  * and doing the right thing.
    2785                 :  */
    2786                 : 
    2787                 : Node *
    2788 GNC     6888969 : expression_tree_mutator_impl(Node *node,
    2789                 :                              tree_mutator_callback mutator,
    2790                 :                              void *context)
    2791                 : {
    2792                 :     /*
    2793                 :      * The mutator has already decided not to modify the current node, but we
    2794 ECB             :      * must call the mutator for any sub-nodes.
    2795                 :      */
    2796                 : 
    2797                 : #define FLATCOPY(newnode, node, nodetype)  \
    2798                 :     ( (newnode) = (nodetype *) palloc(sizeof(nodetype)), \
    2799                 :       memcpy((newnode), (node), sizeof(nodetype)) )
    2800                 : 
    2801                 : #define MUTATE(newfield, oldfield, fieldtype)  \
    2802                 :         ( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )
    2803                 : 
    2804 GIC     6888969 :     if (node == NULL)
    2805 CBC       56398 :         return NULL;
    2806 ECB             : 
    2807                 :     /* Guard against stack overflow due to overly complex expressions */
    2808 CBC     6832571 :     check_stack_depth();
    2809                 : 
    2810 GIC     6832571 :     switch (nodeTag(node))
    2811                 :     {
    2812                 :             /*
    2813                 :              * Primitive node types with no expression subnodes.  Var and
    2814                 :              * Const are frequent enough to deserve special cases, the others
    2815 ECB             :              * we just use copyObject for.
    2816                 :              */
    2817 GIC     1099413 :         case T_Var:
    2818                 :             {
    2819         1099413 :                 Var        *var = (Var *) node;
    2820                 :                 Var        *newnode;
    2821                 : 
    2822         1099413 :                 FLATCOPY(newnode, var, Var);
    2823                 :                 /* Assume we need not copy the varnullingrels bitmapset */
    2824         1099413 :                 return (Node *) newnode;
    2825                 :             }
    2826 ECB             :             break;
    2827 CBC     1184883 :         case T_Const:
    2828 ECB             :             {
    2829 GIC     1184883 :                 Const      *oldnode = (Const *) node;
    2830 ECB             :                 Const      *newnode;
    2831                 : 
    2832 CBC     1184883 :                 FLATCOPY(newnode, oldnode, Const);
    2833 ECB             :                 /* XXX we don't bother with datumCopy; should we? */
    2834 GBC     1184883 :                 return (Node *) newnode;
    2835 ECB             :             }
    2836                 :             break;
    2837 CBC       46239 :         case T_Param:
    2838 ECB             :         case T_CaseTestExpr:
    2839                 :         case T_JsonFormat:
    2840                 :         case T_CoerceToDomainValue:
    2841                 :         case T_SetToDefault:
    2842                 :         case T_CurrentOfExpr:
    2843                 :         case T_NextValueExpr:
    2844 EUB             :         case T_RangeTblRef:
    2845 ECB             :         case T_SortGroupClause:
    2846                 :         case T_CTESearchClause:
    2847 CBC       46239 :             return (Node *) copyObject(node);
    2848             554 :         case T_WithCheckOption:
    2849 ECB             :             {
    2850 CBC         554 :                 WithCheckOption *wco = (WithCheckOption *) node;
    2851 ECB             :                 WithCheckOption *newnode;
    2852 EUB             : 
    2853 CBC         554 :                 FLATCOPY(newnode, wco, WithCheckOption);
    2854             554 :                 MUTATE(newnode->qual, wco->qual, Node *);
    2855             554 :                 return (Node *) newnode;
    2856 ECB             :             }
    2857 CBC       52514 :         case T_Aggref:
    2858 ECB             :             {
    2859 GIC       52514 :                 Aggref     *aggref = (Aggref *) node;
    2860                 :                 Aggref     *newnode;
    2861                 : 
    2862 CBC       52514 :                 FLATCOPY(newnode, aggref, Aggref);
    2863                 :                 /* assume mutation doesn't change types of arguments */
    2864 GIC       52514 :                 newnode->aggargtypes = list_copy(aggref->aggargtypes);
    2865 CBC       52514 :                 MUTATE(newnode->aggdirectargs, aggref->aggdirectargs, List *);
    2866 GBC       52514 :                 MUTATE(newnode->args, aggref->args, List *);
    2867 GIC       52514 :                 MUTATE(newnode->aggorder, aggref->aggorder, List *);
    2868 CBC       52514 :                 MUTATE(newnode->aggdistinct, aggref->aggdistinct, List *);
    2869           52514 :                 MUTATE(newnode->aggfilter, aggref->aggfilter, Expr *);
    2870 GBC       52514 :                 return (Node *) newnode;
    2871                 :             }
    2872 ECB             :             break;
    2873 GIC         515 :         case T_GroupingFunc:
    2874                 :             {
    2875             515 :                 GroupingFunc *grouping = (GroupingFunc *) node;
    2876                 :                 GroupingFunc *newnode;
    2877                 : 
    2878             515 :                 FLATCOPY(newnode, grouping, GroupingFunc);
    2879             515 :                 MUTATE(newnode->args, grouping->args, List *);
    2880                 : 
    2881                 :                 /*
    2882                 :                  * We assume here that mutating the arguments does not change
    2883                 :                  * the semantics, i.e. that the arguments are not mutated in a
    2884                 :                  * way that makes them semantically different from their
    2885                 :                  * previously matching expressions in the GROUP BY clause.
    2886                 :                  *
    2887                 :                  * If a mutator somehow wanted to do this, it would have to
    2888                 :                  * handle the refs and cols lists itself as appropriate.
    2889                 :                  */
    2890             515 :                 newnode->refs = list_copy(grouping->refs);
    2891             515 :                 newnode->cols = list_copy(grouping->cols);
    2892                 : 
    2893             515 :                 return (Node *) newnode;
    2894                 :             }
    2895                 :             break;
    2896            1974 :         case T_WindowFunc:
    2897                 :             {
    2898            1974 :                 WindowFunc *wfunc = (WindowFunc *) node;
    2899                 :                 WindowFunc *newnode;
    2900                 : 
    2901            1974 :                 FLATCOPY(newnode, wfunc, WindowFunc);
    2902            1974 :                 MUTATE(newnode->args, wfunc->args, List *);
    2903            1974 :                 MUTATE(newnode->aggfilter, wfunc->aggfilter, Expr *);
    2904            1974 :                 return (Node *) newnode;
    2905                 :             }
    2906                 :             break;
    2907           13269 :         case T_SubscriptingRef:
    2908                 :             {
    2909           13269 :                 SubscriptingRef *sbsref = (SubscriptingRef *) node;
    2910                 :                 SubscriptingRef *newnode;
    2911                 : 
    2912           13269 :                 FLATCOPY(newnode, sbsref, SubscriptingRef);
    2913           13269 :                 MUTATE(newnode->refupperindexpr, sbsref->refupperindexpr,
    2914                 :                        List *);
    2915           13269 :                 MUTATE(newnode->reflowerindexpr, sbsref->reflowerindexpr,
    2916                 :                        List *);
    2917           13269 :                 MUTATE(newnode->refexpr, sbsref->refexpr,
    2918                 :                        Expr *);
    2919           13269 :                 MUTATE(newnode->refassgnexpr, sbsref->refassgnexpr,
    2920                 :                        Expr *);
    2921                 : 
    2922           13269 :                 return (Node *) newnode;
    2923                 :             }
    2924                 :             break;
    2925          138167 :         case T_FuncExpr:
    2926                 :             {
    2927          138167 :                 FuncExpr   *expr = (FuncExpr *) node;
    2928                 :                 FuncExpr   *newnode;
    2929                 : 
    2930          138167 :                 FLATCOPY(newnode, expr, FuncExpr);
    2931          138167 :                 MUTATE(newnode->args, expr->args, List *);
    2932          138167 :                 return (Node *) newnode;
    2933                 :             }
    2934                 :             break;
    2935 UIC           0 :         case T_NamedArgExpr:
    2936                 :             {
    2937               0 :                 NamedArgExpr *nexpr = (NamedArgExpr *) node;
    2938                 :                 NamedArgExpr *newnode;
    2939 ECB             : 
    2940 UIC           0 :                 FLATCOPY(newnode, nexpr, NamedArgExpr);
    2941               0 :                 MUTATE(newnode->arg, nexpr->arg, Expr *);
    2942               0 :                 return (Node *) newnode;
    2943                 :             }
    2944                 :             break;
    2945 GIC      430942 :         case T_OpExpr:
    2946                 :             {
    2947          430942 :                 OpExpr     *expr = (OpExpr *) node;
    2948                 :                 OpExpr     *newnode;
    2949                 : 
    2950          430942 :                 FLATCOPY(newnode, expr, OpExpr);
    2951          430942 :                 MUTATE(newnode->args, expr->args, List *);
    2952          430942 :                 return (Node *) newnode;
    2953                 :             }
    2954                 :             break;
    2955 CBC         842 :         case T_DistinctExpr:
    2956 ECB             :             {
    2957 GIC         842 :                 DistinctExpr *expr = (DistinctExpr *) node;
    2958                 :                 DistinctExpr *newnode;
    2959 ECB             : 
    2960 GIC         842 :                 FLATCOPY(newnode, expr, DistinctExpr);
    2961 CBC         842 :                 MUTATE(newnode->args, expr->args, List *);
    2962 GIC         842 :                 return (Node *) newnode;
    2963                 :             }
    2964                 :             break;
    2965             135 :         case T_NullIfExpr:
    2966                 :             {
    2967             135 :                 NullIfExpr *expr = (NullIfExpr *) node;
    2968 ECB             :                 NullIfExpr *newnode;
    2969                 : 
    2970 CBC         135 :                 FLATCOPY(newnode, expr, NullIfExpr);
    2971 GIC         135 :                 MUTATE(newnode->args, expr->args, List *);
    2972             135 :                 return (Node *) newnode;
    2973 ECB             :             }
    2974                 :             break;
    2975 CBC       33036 :         case T_ScalarArrayOpExpr:
    2976                 :             {
    2977 GIC       33036 :                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    2978 ECB             :                 ScalarArrayOpExpr *newnode;
    2979                 : 
    2980 CBC       33036 :                 FLATCOPY(newnode, expr, ScalarArrayOpExpr);
    2981 GIC       33036 :                 MUTATE(newnode->args, expr->args, List *);
    2982           33036 :                 return (Node *) newnode;
    2983 ECB             :             }
    2984                 :             break;
    2985 CBC       49767 :         case T_BoolExpr:
    2986                 :             {
    2987 GIC       49767 :                 BoolExpr   *expr = (BoolExpr *) node;
    2988 ECB             :                 BoolExpr   *newnode;
    2989                 : 
    2990 GIC       49767 :                 FLATCOPY(newnode, expr, BoolExpr);
    2991           49767 :                 MUTATE(newnode->args, expr->args, List *);
    2992           49764 :                 return (Node *) newnode;
    2993                 :             }
    2994                 :             break;
    2995           25009 :         case T_SubLink:
    2996                 :             {
    2997           25009 :                 SubLink    *sublink = (SubLink *) node;
    2998 ECB             :                 SubLink    *newnode;
    2999                 : 
    3000 GIC       25009 :                 FLATCOPY(newnode, sublink, SubLink);
    3001 CBC       25009 :                 MUTATE(newnode->testexpr, sublink->testexpr, Node *);
    3002                 : 
    3003                 :                 /*
    3004 ECB             :                  * Also invoke the mutator on the sublink's Query node, so it
    3005                 :                  * can recurse into the sub-query if it wants to.
    3006                 :                  */
    3007 GIC       25009 :                 MUTATE(newnode->subselect, sublink->subselect, Node *);
    3008 CBC       25009 :                 return (Node *) newnode;
    3009                 :             }
    3010 ECB             :             break;
    3011 GIC        6345 :         case T_SubPlan:
    3012                 :             {
    3013 CBC        6345 :                 SubPlan    *subplan = (SubPlan *) node;
    3014                 :                 SubPlan    *newnode;
    3015 ECB             : 
    3016 CBC        6345 :                 FLATCOPY(newnode, subplan, SubPlan);
    3017 ECB             :                 /* transform testexpr */
    3018 CBC        6345 :                 MUTATE(newnode->testexpr, subplan->testexpr, Node *);
    3019 ECB             :                 /* transform args list (params to be passed to subplan) */
    3020 CBC        6345 :                 MUTATE(newnode->args, subplan->args, List *);
    3021 ECB             :                 /* but not the sub-Plan itself, which is referenced as-is */
    3022 GIC        6345 :                 return (Node *) newnode;
    3023                 :             }
    3024 ECB             :             break;
    3025 GIC          72 :         case T_AlternativeSubPlan:
    3026 ECB             :             {
    3027 GIC          72 :                 AlternativeSubPlan *asplan = (AlternativeSubPlan *) node;
    3028                 :                 AlternativeSubPlan *newnode;
    3029 ECB             : 
    3030 CBC          72 :                 FLATCOPY(newnode, asplan, AlternativeSubPlan);
    3031 GIC          72 :                 MUTATE(newnode->subplans, asplan->subplans, List *);
    3032              72 :                 return (Node *) newnode;
    3033                 :             }
    3034                 :             break;
    3035            1802 :         case T_FieldSelect:
    3036                 :             {
    3037            1802 :                 FieldSelect *fselect = (FieldSelect *) node;
    3038                 :                 FieldSelect *newnode;
    3039                 : 
    3040            1802 :                 FLATCOPY(newnode, fselect, FieldSelect);
    3041 CBC        1802 :                 MUTATE(newnode->arg, fselect->arg, Expr *);
    3042            1802 :                 return (Node *) newnode;
    3043                 :             }
    3044 ECB             :             break;
    3045 GIC         146 :         case T_FieldStore:
    3046                 :             {
    3047 CBC         146 :                 FieldStore *fstore = (FieldStore *) node;
    3048                 :                 FieldStore *newnode;
    3049 ECB             : 
    3050 GIC         146 :                 FLATCOPY(newnode, fstore, FieldStore);
    3051             146 :                 MUTATE(newnode->arg, fstore->arg, Expr *);
    3052 CBC         146 :                 MUTATE(newnode->newvals, fstore->newvals, List *);
    3053             146 :                 newnode->fieldnums = list_copy(fstore->fieldnums);
    3054             146 :                 return (Node *) newnode;
    3055 ECB             :             }
    3056                 :             break;
    3057 GIC       48649 :         case T_RelabelType:
    3058 ECB             :             {
    3059 GIC       48649 :                 RelabelType *relabel = (RelabelType *) node;
    3060 ECB             :                 RelabelType *newnode;
    3061                 : 
    3062 GIC       48649 :                 FLATCOPY(newnode, relabel, RelabelType);
    3063 CBC       48649 :                 MUTATE(newnode->arg, relabel->arg, Expr *);
    3064           48649 :                 return (Node *) newnode;
    3065                 :             }
    3066 ECB             :             break;
    3067 GIC       10971 :         case T_CoerceViaIO:
    3068 ECB             :             {
    3069 GIC       10971 :                 CoerceViaIO *iocoerce = (CoerceViaIO *) node;
    3070 ECB             :                 CoerceViaIO *newnode;
    3071                 : 
    3072 GIC       10971 :                 FLATCOPY(newnode, iocoerce, CoerceViaIO);
    3073 CBC       10971 :                 MUTATE(newnode->arg, iocoerce->arg, Expr *);
    3074 GIC       10971 :                 return (Node *) newnode;
    3075                 :             }
    3076 ECB             :             break;
    3077 GIC        4462 :         case T_ArrayCoerceExpr:
    3078 ECB             :             {
    3079 GIC        4462 :                 ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
    3080                 :                 ArrayCoerceExpr *newnode;
    3081 ECB             : 
    3082 CBC        4462 :                 FLATCOPY(newnode, acoerce, ArrayCoerceExpr);
    3083            4462 :                 MUTATE(newnode->arg, acoerce->arg, Expr *);
    3084 GIC        4462 :                 MUTATE(newnode->elemexpr, acoerce->elemexpr, Expr *);
    3085            4462 :                 return (Node *) newnode;
    3086 EUB             :             }
    3087                 :             break;
    3088 GBC          77 :         case T_ConvertRowtypeExpr:
    3089                 :             {
    3090 GIC          77 :                 ConvertRowtypeExpr *convexpr = (ConvertRowtypeExpr *) node;
    3091 EUB             :                 ConvertRowtypeExpr *newnode;
    3092                 : 
    3093 GBC          77 :                 FLATCOPY(newnode, convexpr, ConvertRowtypeExpr);
    3094 GIC          77 :                 MUTATE(newnode->arg, convexpr->arg, Expr *);
    3095              77 :                 return (Node *) newnode;
    3096 ECB             :             }
    3097                 :             break;
    3098 CBC        2721 :         case T_CollateExpr:
    3099                 :             {
    3100 GIC        2721 :                 CollateExpr *collate = (CollateExpr *) node;
    3101 ECB             :                 CollateExpr *newnode;
    3102                 : 
    3103 CBC        2721 :                 FLATCOPY(newnode, collate, CollateExpr);
    3104 GIC        2721 :                 MUTATE(newnode->arg, collate->arg, Expr *);
    3105            2721 :                 return (Node *) newnode;
    3106 ECB             :             }
    3107                 :             break;
    3108 CBC       22867 :         case T_CaseExpr:
    3109                 :             {
    3110 GIC       22867 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    3111 ECB             :                 CaseExpr   *newnode;
    3112                 : 
    3113 CBC       22867 :                 FLATCOPY(newnode, caseexpr, CaseExpr);
    3114 GIC       22867 :                 MUTATE(newnode->arg, caseexpr->arg, Expr *);
    3115           22867 :                 MUTATE(newnode->args, caseexpr->args, List *);
    3116 CBC       22867 :                 MUTATE(newnode->defresult, caseexpr->defresult, Expr *);
    3117 GIC       22867 :                 return (Node *) newnode;
    3118 ECB             :             }
    3119                 :             break;
    3120 GIC       31167 :         case T_CaseWhen:
    3121 ECB             :             {
    3122 CBC       31167 :                 CaseWhen   *casewhen = (CaseWhen *) node;
    3123 ECB             :                 CaseWhen   *newnode;
    3124                 : 
    3125 GIC       31167 :                 FLATCOPY(newnode, casewhen, CaseWhen);
    3126 CBC       31167 :                 MUTATE(newnode->expr, casewhen->expr, Expr *);
    3127 GIC       31167 :                 MUTATE(newnode->result, casewhen->result, Expr *);
    3128 CBC       31167 :                 return (Node *) newnode;
    3129                 :             }
    3130                 :             break;
    3131           18705 :         case T_ArrayExpr:
    3132 ECB             :             {
    3133 CBC       18705 :                 ArrayExpr  *arrayexpr = (ArrayExpr *) node;
    3134                 :                 ArrayExpr  *newnode;
    3135                 : 
    3136           18705 :                 FLATCOPY(newnode, arrayexpr, ArrayExpr);
    3137 GIC       18705 :                 MUTATE(newnode->elements, arrayexpr->elements, List *);
    3138 CBC       18705 :                 return (Node *) newnode;
    3139                 :             }
    3140                 :             break;
    3141            3954 :         case T_RowExpr:
    3142 ECB             :             {
    3143 CBC        3954 :                 RowExpr    *rowexpr = (RowExpr *) node;
    3144                 :                 RowExpr    *newnode;
    3145                 : 
    3146            3954 :                 FLATCOPY(newnode, rowexpr, RowExpr);
    3147 GIC        3954 :                 MUTATE(newnode->args, rowexpr->args, List *);
    3148 ECB             :                 /* Assume colnames needn't be duplicated */
    3149 GIC        3954 :                 return (Node *) newnode;
    3150                 :             }
    3151 ECB             :             break;
    3152 CBC         156 :         case T_RowCompareExpr:
    3153                 :             {
    3154 GIC         156 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    3155                 :                 RowCompareExpr *newnode;
    3156                 : 
    3157             156 :                 FLATCOPY(newnode, rcexpr, RowCompareExpr);
    3158 CBC         156 :                 MUTATE(newnode->largs, rcexpr->largs, List *);
    3159             156 :                 MUTATE(newnode->rargs, rcexpr->rargs, List *);
    3160 GIC         156 :                 return (Node *) newnode;
    3161                 :             }
    3162 ECB             :             break;
    3163 GIC        4630 :         case T_CoalesceExpr:
    3164 ECB             :             {
    3165 GIC        4630 :                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
    3166                 :                 CoalesceExpr *newnode;
    3167 ECB             : 
    3168 GIC        4630 :                 FLATCOPY(newnode, coalesceexpr, CoalesceExpr);
    3169 CBC        4630 :                 MUTATE(newnode->args, coalesceexpr->args, List *);
    3170 GIC        4630 :                 return (Node *) newnode;
    3171 ECB             :             }
    3172                 :             break;
    3173 CBC         717 :         case T_MinMaxExpr:
    3174                 :             {
    3175 GIC         717 :                 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
    3176 ECB             :                 MinMaxExpr *newnode;
    3177                 : 
    3178 CBC         717 :                 FLATCOPY(newnode, minmaxexpr, MinMaxExpr);
    3179 GIC         717 :                 MUTATE(newnode->args, minmaxexpr->args, List *);
    3180             717 :                 return (Node *) newnode;
    3181 ECB             :             }
    3182                 :             break;
    3183 CBC         399 :         case T_XmlExpr:
    3184                 :             {
    3185 GIC         399 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    3186 ECB             :                 XmlExpr    *newnode;
    3187                 : 
    3188 CBC         399 :                 FLATCOPY(newnode, xexpr, XmlExpr);
    3189 GIC         399 :                 MUTATE(newnode->named_args, xexpr->named_args, List *);
    3190                 :                 /* assume mutator does not care about arg_names */
    3191 CBC         399 :                 MUTATE(newnode->args, xexpr->args, List *);
    3192             399 :                 return (Node *) newnode;
    3193 ECB             :             }
    3194                 :             break;
    3195 GNC         631 :         case T_JsonReturning:
    3196                 :             {
    3197             631 :                 JsonReturning *jr = (JsonReturning *) node;
    3198                 :                 JsonReturning *newnode;
    3199                 : 
    3200             631 :                 FLATCOPY(newnode, jr, JsonReturning);
    3201             631 :                 MUTATE(newnode->format, jr->format, JsonFormat *);
    3202                 : 
    3203             631 :                 return (Node *) newnode;
    3204                 :             }
    3205              90 :         case T_JsonValueExpr:
    3206                 :             {
    3207              90 :                 JsonValueExpr *jve = (JsonValueExpr *) node;
    3208                 :                 JsonValueExpr *newnode;
    3209                 : 
    3210              90 :                 FLATCOPY(newnode, jve, JsonValueExpr);
    3211              90 :                 MUTATE(newnode->raw_expr, jve->raw_expr, Expr *);
    3212              90 :                 MUTATE(newnode->formatted_expr, jve->formatted_expr, Expr *);
    3213              90 :                 MUTATE(newnode->format, jve->format, JsonFormat *);
    3214                 : 
    3215              90 :                 return (Node *) newnode;
    3216                 :             }
    3217             631 :         case T_JsonConstructorExpr:
    3218                 :             {
    3219             631 :                 JsonConstructorExpr *jce = (JsonConstructorExpr *) node;
    3220                 :                 JsonConstructorExpr *newnode;
    3221                 : 
    3222             631 :                 FLATCOPY(newnode, jce, JsonConstructorExpr);
    3223             631 :                 MUTATE(newnode->args, jce->args, List *);
    3224             631 :                 MUTATE(newnode->func, jce->func, Expr *);
    3225             631 :                 MUTATE(newnode->coercion, jce->coercion, Expr *);
    3226             631 :                 MUTATE(newnode->returning, jce->returning, JsonReturning *);
    3227                 : 
    3228             631 :                 return (Node *) newnode;
    3229                 :             }
    3230             229 :         case T_JsonIsPredicate:
    3231                 :             {
    3232             229 :                 JsonIsPredicate *pred = (JsonIsPredicate *) node;
    3233                 :                 JsonIsPredicate *newnode;
    3234                 : 
    3235             229 :                 FLATCOPY(newnode, pred, JsonIsPredicate);
    3236             229 :                 MUTATE(newnode->expr, pred->expr, Node *);
    3237             229 :                 MUTATE(newnode->format, pred->format, JsonFormat *);
    3238                 : 
    3239             229 :                 return (Node *) newnode;
    3240                 :             }
    3241 GIC       19660 :         case T_NullTest:
    3242 ECB             :             {
    3243 GIC       19660 :                 NullTest   *ntest = (NullTest *) node;
    3244 ECB             :                 NullTest   *newnode;
    3245                 : 
    3246 GIC       19660 :                 FLATCOPY(newnode, ntest, NullTest);
    3247 CBC       19660 :                 MUTATE(newnode->arg, ntest->arg, Expr *);
    3248           19660 :                 return (Node *) newnode;
    3249 ECB             :             }
    3250                 :             break;
    3251 CBC         459 :         case T_BooleanTest:
    3252                 :             {
    3253 GIC         459 :                 BooleanTest *btest = (BooleanTest *) node;
    3254 ECB             :                 BooleanTest *newnode;
    3255                 : 
    3256 CBC         459 :                 FLATCOPY(newnode, btest, BooleanTest);
    3257 GIC         459 :                 MUTATE(newnode->arg, btest->arg, Expr *);
    3258             459 :                 return (Node *) newnode;
    3259 ECB             :             }
    3260                 :             break;
    3261 CBC        8760 :         case T_CoerceToDomain:
    3262                 :             {
    3263 GIC        8760 :                 CoerceToDomain *ctest = (CoerceToDomain *) node;
    3264 ECB             :                 CoerceToDomain *newnode;
    3265                 : 
    3266 CBC        8760 :                 FLATCOPY(newnode, ctest, CoerceToDomain);
    3267 GIC        8760 :                 MUTATE(newnode->arg, ctest->arg, Expr *);
    3268            8760 :                 return (Node *) newnode;
    3269 ECB             :             }
    3270                 :             break;
    3271 CBC     1510741 :         case T_TargetEntry:
    3272                 :             {
    3273 GIC     1510741 :                 TargetEntry *targetentry = (TargetEntry *) node;
    3274 ECB             :                 TargetEntry *newnode;
    3275                 : 
    3276 CBC     1510741 :                 FLATCOPY(newnode, targetentry, TargetEntry);
    3277 GIC     1510741 :                 MUTATE(newnode->expr, targetentry->expr, Expr *);
    3278         1509206 :                 return (Node *) newnode;
    3279 ECB             :             }
    3280                 :             break;
    3281 CBC       17280 :         case T_Query:
    3282 ECB             :             /* Do nothing with a sub-Query, per discussion above */
    3283 GIC       17280 :             return node;
    3284 UIC           0 :         case T_WindowClause:
    3285 ECB             :             {
    3286 UIC           0 :                 WindowClause *wc = (WindowClause *) node;
    3287 ECB             :                 WindowClause *newnode;
    3288                 : 
    3289 UIC           0 :                 FLATCOPY(newnode, wc, WindowClause);
    3290 LBC           0 :                 MUTATE(newnode->partitionClause, wc->partitionClause, List *);
    3291               0 :                 MUTATE(newnode->orderClause, wc->orderClause, List *);
    3292               0 :                 MUTATE(newnode->startOffset, wc->startOffset, Node *);
    3293 UIC           0 :                 MUTATE(newnode->endOffset, wc->endOffset, Node *);
    3294               0 :                 return (Node *) newnode;
    3295 ECB             :             }
    3296                 :             break;
    3297 LBC           0 :         case T_CTECycleClause:
    3298                 :             {
    3299 UIC           0 :                 CTECycleClause *cc = (CTECycleClause *) node;
    3300 ECB             :                 CTECycleClause *newnode;
    3301                 : 
    3302 LBC           0 :                 FLATCOPY(newnode, cc, CTECycleClause);
    3303 UIC           0 :                 MUTATE(newnode->cycle_mark_value, cc->cycle_mark_value, Node *);
    3304               0 :                 MUTATE(newnode->cycle_mark_default, cc->cycle_mark_default, Node *);
    3305 LBC           0 :                 return (Node *) newnode;
    3306                 :             }
    3307 ECB             :             break;
    3308 GIC          36 :         case T_CommonTableExpr:
    3309                 :             {
    3310 CBC          36 :                 CommonTableExpr *cte = (CommonTableExpr *) node;
    3311 ECB             :                 CommonTableExpr *newnode;
    3312                 : 
    3313 CBC          36 :                 FLATCOPY(newnode, cte, CommonTableExpr);
    3314 ECB             : 
    3315                 :                 /*
    3316                 :                  * Also invoke the mutator on the CTE's Query node, so it can
    3317                 :                  * recurse into the sub-query if it wants to.
    3318                 :                  */
    3319 CBC          36 :                 MUTATE(newnode->ctequery, cte->ctequery, Node *);
    3320                 : 
    3321 GIC          36 :                 MUTATE(newnode->search_clause, cte->search_clause, CTESearchClause *);
    3322 CBC          36 :                 MUTATE(newnode->cycle_clause, cte->cycle_clause, CTECycleClause *);
    3323 ECB             : 
    3324 CBC          36 :                 return (Node *) newnode;
    3325 ECB             :             }
    3326                 :             break;
    3327 UIC           0 :         case T_PartitionBoundSpec:
    3328 ECB             :             {
    3329 UIC           0 :                 PartitionBoundSpec *pbs = (PartitionBoundSpec *) node;
    3330 ECB             :                 PartitionBoundSpec *newnode;
    3331                 : 
    3332 UIC           0 :                 FLATCOPY(newnode, pbs, PartitionBoundSpec);
    3333 LBC           0 :                 MUTATE(newnode->listdatums, pbs->listdatums, List *);
    3334               0 :                 MUTATE(newnode->lowerdatums, pbs->lowerdatums, List *);
    3335               0 :                 MUTATE(newnode->upperdatums, pbs->upperdatums, List *);
    3336 UIC           0 :                 return (Node *) newnode;
    3337                 :             }
    3338 ECB             :             break;
    3339 UIC           0 :         case T_PartitionRangeDatum:
    3340 ECB             :             {
    3341 UIC           0 :                 PartitionRangeDatum *prd = (PartitionRangeDatum *) node;
    3342                 :                 PartitionRangeDatum *newnode;
    3343 ECB             : 
    3344 LBC           0 :                 FLATCOPY(newnode, prd, PartitionRangeDatum);
    3345 UIC           0 :                 MUTATE(newnode->value, prd->value, Node *);
    3346 LBC           0 :                 return (Node *) newnode;
    3347                 :             }
    3348                 :             break;
    3349 CBC     1981328 :         case T_List:
    3350                 :             {
    3351 ECB             :                 /*
    3352                 :                  * We assume the mutator isn't interested in the list nodes
    3353                 :                  * per se, so just invoke it on each list element. NOTE: this
    3354                 :                  * would fail badly on a list with integer elements!
    3355                 :                  */
    3356                 :                 List       *resultlist;
    3357                 :                 ListCell   *temp;
    3358                 : 
    3359 GIC     1981328 :                 resultlist = NIL;
    3360 CBC     6255008 :                 foreach(temp, (List *) node)
    3361                 :                 {
    3362         4273680 :                     resultlist = lappend(resultlist,
    3363 GIC     4275274 :                                          mutator((Node *) lfirst(temp),
    3364                 :                                                  context));
    3365 ECB             :                 }
    3366 CBC     1979734 :                 return (Node *) resultlist;
    3367 ECB             :             }
    3368                 :             break;
    3369 GIC       11300 :         case T_FromExpr:
    3370 ECB             :             {
    3371 GIC       11300 :                 FromExpr   *from = (FromExpr *) node;
    3372 ECB             :                 FromExpr   *newnode;
    3373                 : 
    3374 GIC       11300 :                 FLATCOPY(newnode, from, FromExpr);
    3375 CBC       11300 :                 MUTATE(newnode->fromlist, from->fromlist, List *);
    3376           11300 :                 MUTATE(newnode->quals, from->quals, Node *);
    3377           11300 :                 return (Node *) newnode;
    3378                 :             }
    3379                 :             break;
    3380             174 :         case T_OnConflictExpr:
    3381                 :             {
    3382             174 :                 OnConflictExpr *oc = (OnConflictExpr *) node;
    3383                 :                 OnConflictExpr *newnode;
    3384                 : 
    3385             174 :                 FLATCOPY(newnode, oc, OnConflictExpr);
    3386             174 :                 MUTATE(newnode->arbiterElems, oc->arbiterElems, List *);
    3387 GIC         174 :                 MUTATE(newnode->arbiterWhere, oc->arbiterWhere, Node *);
    3388 CBC         174 :                 MUTATE(newnode->onConflictSet, oc->onConflictSet, List *);
    3389             174 :                 MUTATE(newnode->onConflictWhere, oc->onConflictWhere, Node *);
    3390 GIC         174 :                 MUTATE(newnode->exclRelTlist, oc->exclRelTlist, List *);
    3391                 : 
    3392 CBC         174 :                 return (Node *) newnode;
    3393                 :             }
    3394 ECB             :             break;
    3395 UIC           0 :         case T_MergeAction:
    3396                 :             {
    3397 LBC           0 :                 MergeAction *action = (MergeAction *) node;
    3398 ECB             :                 MergeAction *newnode;
    3399                 : 
    3400 LBC           0 :                 FLATCOPY(newnode, action, MergeAction);
    3401 UIC           0 :                 MUTATE(newnode->qual, action->qual, Node *);
    3402 LBC           0 :                 MUTATE(newnode->targetList, action->targetList, List *);
    3403                 : 
    3404               0 :                 return (Node *) newnode;
    3405                 :             }
    3406                 :             break;
    3407               0 :         case T_PartitionPruneStepOp:
    3408 ECB             :             {
    3409 LBC           0 :                 PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node;
    3410 ECB             :                 PartitionPruneStepOp *newnode;
    3411                 : 
    3412 LBC           0 :                 FLATCOPY(newnode, opstep, PartitionPruneStepOp);
    3413 UIC           0 :                 MUTATE(newnode->exprs, opstep->exprs, List *);
    3414 ECB             : 
    3415 UIC           0 :                 return (Node *) newnode;
    3416 ECB             :             }
    3417                 :             break;
    3418 UIC           0 :         case T_PartitionPruneStepCombine:
    3419 ECB             :             /* no expression sub-nodes */
    3420 LBC           0 :             return (Node *) copyObject(node);
    3421 CBC        1478 :         case T_JoinExpr:
    3422 ECB             :             {
    3423 CBC        1478 :                 JoinExpr   *join = (JoinExpr *) node;
    3424                 :                 JoinExpr   *newnode;
    3425 ECB             : 
    3426 GIC        1478 :                 FLATCOPY(newnode, join, JoinExpr);
    3427 CBC        1478 :                 MUTATE(newnode->larg, join->larg, Node *);
    3428 GIC        1478 :                 MUTATE(newnode->rarg, join->rarg, Node *);
    3429 CBC        1478 :                 MUTATE(newnode->quals, join->quals, Node *);
    3430                 :                 /* We do not mutate alias or using by default */
    3431 GIC        1478 :                 return (Node *) newnode;
    3432 ECB             :             }
    3433                 :             break;
    3434 CBC          75 :         case T_SetOperationStmt:
    3435                 :             {
    3436              75 :                 SetOperationStmt *setop = (SetOperationStmt *) node;
    3437                 :                 SetOperationStmt *newnode;
    3438 ECB             : 
    3439 GIC          75 :                 FLATCOPY(newnode, setop, SetOperationStmt);
    3440 CBC          75 :                 MUTATE(newnode->larg, setop->larg, Node *);
    3441 GIC          75 :                 MUTATE(newnode->rarg, setop->rarg, Node *);
    3442                 :                 /* We do not mutate groupClauses by default */
    3443 CBC          75 :                 return (Node *) newnode;
    3444 ECB             :             }
    3445                 :             break;
    3446 GIC        2532 :         case T_IndexClause:
    3447                 :             {
    3448 CBC        2532 :                 IndexClause *iclause = (IndexClause *) node;
    3449                 :                 IndexClause *newnode;
    3450 ECB             : 
    3451 GIC        2532 :                 FLATCOPY(newnode, iclause, IndexClause);
    3452            2532 :                 MUTATE(newnode->rinfo, iclause->rinfo, RestrictInfo *);
    3453 CBC        2532 :                 MUTATE(newnode->indexquals, iclause->indexquals, List *);
    3454            2532 :                 return (Node *) newnode;
    3455 ECB             :             }
    3456                 :             break;
    3457 GIC        3088 :         case T_PlaceHolderVar:
    3458 ECB             :             {
    3459 GIC        3088 :                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
    3460 ECB             :                 PlaceHolderVar *newnode;
    3461                 : 
    3462 GIC        3088 :                 FLATCOPY(newnode, phv, PlaceHolderVar);
    3463 CBC        3088 :                 MUTATE(newnode->phexpr, phv->phexpr, Expr *);
    3464                 :                 /* Assume we need not copy the relids bitmapsets */
    3465            3088 :                 return (Node *) newnode;
    3466                 :             }
    3467                 :             break;
    3468            1042 :         case T_InferenceElem:
    3469                 :             {
    3470            1042 :                 InferenceElem *inferenceelemdexpr = (InferenceElem *) node;
    3471                 :                 InferenceElem *newnode;
    3472                 : 
    3473            1042 :                 FLATCOPY(newnode, inferenceelemdexpr, InferenceElem);
    3474            1042 :                 MUTATE(newnode->expr, newnode->expr, Node *);
    3475            1042 :                 return (Node *) newnode;
    3476                 :             }
    3477                 :             break;
    3478            2847 :         case T_AppendRelInfo:
    3479                 :             {
    3480            2847 :                 AppendRelInfo *appinfo = (AppendRelInfo *) node;
    3481 EUB             :                 AppendRelInfo *newnode;
    3482                 : 
    3483 GBC        2847 :                 FLATCOPY(newnode, appinfo, AppendRelInfo);
    3484 GIC        2847 :                 MUTATE(newnode->translated_vars, appinfo->translated_vars, List *);
    3485                 :                 /* Assume nothing need be done with parent_colnos[] */
    3486 GBC        2847 :                 return (Node *) newnode;
    3487 EUB             :             }
    3488                 :             break;
    3489 UBC           0 :         case T_PlaceHolderInfo:
    3490 EUB             :             {
    3491 UBC           0 :                 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) node;
    3492                 :                 PlaceHolderInfo *newnode;
    3493                 : 
    3494               0 :                 FLATCOPY(newnode, phinfo, PlaceHolderInfo);
    3495 UIC           0 :                 MUTATE(newnode->ph_var, phinfo->ph_var, PlaceHolderVar *);
    3496 EUB             :                 /* Assume we need not copy the relids bitmapsets */
    3497 UIC           0 :                 return (Node *) newnode;
    3498                 :             }
    3499 EUB             :             break;
    3500 GBC       34699 :         case T_RangeTblFunction:
    3501 EUB             :             {
    3502 GBC       34699 :                 RangeTblFunction *rtfunc = (RangeTblFunction *) node;
    3503                 :                 RangeTblFunction *newnode;
    3504                 : 
    3505 CBC       34699 :                 FLATCOPY(newnode, rtfunc, RangeTblFunction);
    3506 GIC       34699 :                 MUTATE(newnode->funcexpr, rtfunc->funcexpr, Node *);
    3507 ECB             :                 /* Assume we need not copy the coldef info lists */
    3508 GIC       34699 :                 return (Node *) newnode;
    3509                 :             }
    3510 ECB             :             break;
    3511 GIC         167 :         case T_TableSampleClause:
    3512                 :             {
    3513             167 :                 TableSampleClause *tsc = (TableSampleClause *) node;
    3514                 :                 TableSampleClause *newnode;
    3515                 : 
    3516 CBC         167 :                 FLATCOPY(newnode, tsc, TableSampleClause);
    3517 GIC         167 :                 MUTATE(newnode->args, tsc->args, List *);
    3518 CBC         167 :                 MUTATE(newnode->repeatable, tsc->repeatable, Expr *);
    3519             167 :                 return (Node *) newnode;
    3520                 :             }
    3521 ECB             :             break;
    3522 GIC         225 :         case T_TableFunc:
    3523                 :             {
    3524 GBC         225 :                 TableFunc  *tf = (TableFunc *) node;
    3525                 :                 TableFunc  *newnode;
    3526 EUB             : 
    3527 GIC         225 :                 FLATCOPY(newnode, tf, TableFunc);
    3528             225 :                 MUTATE(newnode->ns_uris, tf->ns_uris, List *);
    3529 GBC         225 :                 MUTATE(newnode->docexpr, tf->docexpr, Node *);
    3530             225 :                 MUTATE(newnode->rowexpr, tf->rowexpr, Node *);
    3531             225 :                 MUTATE(newnode->colexprs, tf->colexprs, List *);
    3532             225 :                 MUTATE(newnode->coldefexprs, tf->coldefexprs, List *);
    3533             225 :                 return (Node *) newnode;
    3534                 :             }
    3535                 :             break;
    3536 UBC           0 :         default:
    3537 UIC           0 :             elog(ERROR, "unrecognized node type: %d",
    3538 EUB             :                  (int) nodeTag(node));
    3539                 :             break;
    3540                 :     }
    3541                 :     /* can't get here, but keep compiler happy */
    3542                 :     return NULL;
    3543                 : }
    3544                 : 
    3545                 : 
    3546 ECB             : /*
    3547                 :  * query_tree_mutator --- initiate modification of a Query's expressions
    3548                 :  *
    3549                 :  * This routine exists just to reduce the number of places that need to know
    3550                 :  * where all the expression subtrees of a Query are.  Note it can be used
    3551                 :  * for starting a walk at top level of a Query regardless of whether the
    3552                 :  * mutator intends to descend into subqueries.  It is also useful for
    3553                 :  * descending into subqueries within a mutator.
    3554                 :  *
    3555                 :  * Some callers want to suppress mutating of certain items in the Query,
    3556                 :  * typically because they need to process them specially, or don't actually
    3557                 :  * want to recurse into subqueries.  This is supported by the flags argument,
    3558                 :  * which is the bitwise OR of flag values to suppress mutating of
    3559                 :  * indicated items.  (More flag bits may be added as needed.)
    3560                 :  *
    3561                 :  * Normally the top-level Query node itself is copied, but some callers want
    3562                 :  * it to be modified in-place; they must pass QTW_DONT_COPY_QUERY in flags.
    3563                 :  * All modified substructure is safely copied in any case.
    3564                 :  */
    3565                 : Query *
    3566 GNC       11198 : query_tree_mutator_impl(Query *query,
    3567                 :                         tree_mutator_callback mutator,
    3568                 :                         void *context,
    3569                 :                         int flags)
    3570                 : {
    3571 CBC       11198 :     Assert(query != NULL && IsA(query, Query));
    3572 ECB             : 
    3573 CBC       11198 :     if (!(flags & QTW_DONT_COPY_QUERY))
    3574 ECB             :     {
    3575                 :         Query      *newquery;
    3576                 : 
    3577 CBC       11198 :         FLATCOPY(newquery, query, Query);
    3578 GIC       11198 :         query = newquery;
    3579 ECB             :     }
    3580                 : 
    3581 GIC       11198 :     MUTATE(query->targetList, query->targetList, List *);
    3582 CBC       11198 :     MUTATE(query->withCheckOptions, query->withCheckOptions, List *);
    3583           11198 :     MUTATE(query->onConflict, query->onConflict, OnConflictExpr *);
    3584           11198 :     MUTATE(query->mergeActionList, query->mergeActionList, List *);
    3585           11198 :     MUTATE(query->returningList, query->returningList, List *);
    3586           11198 :     MUTATE(query->jointree, query->jointree, FromExpr *);
    3587           11198 :     MUTATE(query->setOperations, query->setOperations, Node *);
    3588 GIC       11198 :     MUTATE(query->havingQual, query->havingQual, Node *);
    3589 CBC       11198 :     MUTATE(query->limitOffset, query->limitOffset, Node *);
    3590 GIC       11198 :     MUTATE(query->limitCount, query->limitCount, Node *);
    3591                 : 
    3592 EUB             :     /*
    3593                 :      * Most callers aren't interested in SortGroupClause nodes since those
    3594                 :      * don't contain actual expressions. However they do contain OIDs, which
    3595                 :      * may be of interest to some mutators.
    3596                 :      */
    3597                 : 
    3598 GBC       11198 :     if ((flags & QTW_EXAMINE_SORTGROUP))
    3599 EUB             :     {
    3600 UIC           0 :         MUTATE(query->groupClause, query->groupClause, List *);
    3601 UBC           0 :         MUTATE(query->windowClause, query->windowClause, List *);
    3602 UIC           0 :         MUTATE(query->sortClause, query->sortClause, List *);
    3603               0 :         MUTATE(query->distinctClause, query->distinctClause, List *);
    3604 EUB             :     }
    3605                 :     else
    3606                 :     {
    3607                 :         /*
    3608                 :          * But we need to mutate the expressions under WindowClause nodes even
    3609                 :          * if we're not interested in SortGroupClause nodes.
    3610                 :          */
    3611                 :         List       *resultlist;
    3612                 :         ListCell   *temp;
    3613                 : 
    3614 GIC       11198 :         resultlist = NIL;
    3615 GBC       11207 :         foreach(temp, query->windowClause)
    3616                 :         {
    3617               9 :             WindowClause *wc = lfirst_node(WindowClause, temp);
    3618 ECB             :             WindowClause *newnode;
    3619                 : 
    3620 CBC           9 :             FLATCOPY(newnode, wc, WindowClause);
    3621 GIC           9 :             MUTATE(newnode->startOffset, wc->startOffset, Node *);
    3622               9 :             MUTATE(newnode->endOffset, wc->endOffset, Node *);
    3623 ECB             : 
    3624 CBC           9 :             resultlist = lappend(resultlist, (Node *) newnode);
    3625 ECB             :         }
    3626 CBC       11198 :         query->windowClause = resultlist;
    3627                 :     }
    3628 ECB             : 
    3629                 :     /*
    3630                 :      * groupingSets and rowMarks are not mutated:
    3631                 :      *
    3632                 :      * groupingSets contain only ressortgroup refs (integers) which are
    3633                 :      * meaningless without the groupClause or tlist. Accordingly, any mutator
    3634                 :      * that needs to care about them needs to handle them itself in its Query
    3635                 :      * processing.
    3636                 :      *
    3637                 :      * rowMarks contains only rangetable indexes (and flags etc.) and
    3638                 :      * therefore should be handled at Query level similarly.
    3639                 :      */
    3640                 : 
    3641 GIC       11198 :     if (!(flags & QTW_IGNORE_CTE_SUBQUERIES))
    3642           11198 :         MUTATE(query->cteList, query->cteList, List *);
    3643 ECB             :     else                        /* else copy CTE list as-is */
    3644 UIC           0 :         query->cteList = copyObject(query->cteList);
    3645 CBC       11198 :     query->rtable = range_table_mutator(query->rtable,
    3646                 :                                         mutator, context, flags);
    3647 GIC       11198 :     return query;
    3648 ECB             : }
    3649                 : 
    3650                 : /*
    3651                 :  * range_table_mutator is just the part of query_tree_mutator that processes
    3652                 :  * a query's rangetable.  This is split out since it can be useful on
    3653                 :  * its own.
    3654                 :  */
    3655                 : List *
    3656 GNC       11198 : range_table_mutator_impl(List *rtable,
    3657                 :                          tree_mutator_callback mutator,
    3658                 :                          void *context,
    3659                 :                          int flags)
    3660 ECB             : {
    3661 GIC       11198 :     List       *newrt = NIL;
    3662 ECB             :     ListCell   *rt;
    3663                 : 
    3664 GIC       30329 :     foreach(rt, rtable)
    3665 ECB             :     {
    3666 GIC       19131 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
    3667 ECB             :         RangeTblEntry *newrte;
    3668                 : 
    3669 GIC       19131 :         FLATCOPY(newrte, rte, RangeTblEntry);
    3670 CBC       19131 :         switch (rte->rtekind)
    3671 ECB             :         {
    3672 CBC       14015 :             case RTE_RELATION:
    3673 GIC       14015 :                 MUTATE(newrte->tablesample, rte->tablesample,
    3674                 :                        TableSampleClause *);
    3675 ECB             :                 /* we don't bother to copy eref, aliases, etc; OK? */
    3676 GIC       14015 :                 break;
    3677 CBC         993 :             case RTE_SUBQUERY:
    3678 GIC         993 :                 if (!(flags & QTW_IGNORE_RT_SUBQUERIES))
    3679             993 :                     MUTATE(newrte->subquery, rte->subquery, Query *);
    3680 ECB             :                 else
    3681                 :                 {
    3682                 :                     /* else, copy RT subqueries as-is */
    3683 LBC           0 :                     newrte->subquery = copyObject(rte->subquery);
    3684                 :                 }
    3685 GIC         993 :                 break;
    3686 GBC        1477 :             case RTE_JOIN:
    3687 GIC        1477 :                 if (!(flags & QTW_IGNORE_JOINALIASES))
    3688 GBC        1305 :                     MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
    3689                 :                 else
    3690                 :                 {
    3691 EUB             :                     /* else, copy join aliases as-is */
    3692 GBC         172 :                     newrte->joinaliasvars = copyObject(rte->joinaliasvars);
    3693                 :                 }
    3694            1477 :                 break;
    3695 GIC        2289 :             case RTE_FUNCTION:
    3696            2289 :                 MUTATE(newrte->functions, rte->functions, List *);
    3697 CBC        2289 :                 break;
    3698 UIC           0 :             case RTE_TABLEFUNC:
    3699 LBC           0 :                 MUTATE(newrte->tablefunc, rte->tablefunc, TableFunc *);
    3700 UIC           0 :                 break;
    3701 GIC         204 :             case RTE_VALUES:
    3702 CBC         204 :                 MUTATE(newrte->values_lists, rte->values_lists, List *);
    3703             204 :                 break;
    3704 GIC         153 :             case RTE_CTE:
    3705 ECB             :             case RTE_NAMEDTUPLESTORE:
    3706                 :             case RTE_RESULT:
    3707                 :                 /* nothing to do */
    3708 CBC         153 :                 break;
    3709                 :         }
    3710           19131 :         MUTATE(newrte->securityQuals, rte->securityQuals, List *);
    3711 GIC       19131 :         newrt = lappend(newrt, newrte);
    3712                 :     }
    3713 CBC       11198 :     return newrt;
    3714 ECB             : }
    3715                 : 
    3716                 : /*
    3717                 :  * query_or_expression_tree_walker --- hybrid form
    3718                 :  *
    3719                 :  * This routine will invoke query_tree_walker if called on a Query node,
    3720                 :  * else will invoke the walker directly.  This is a useful way of starting
    3721                 :  * the recursion when the walker's normal change of state is not appropriate
    3722                 :  * for the outermost Query node.
    3723                 :  */
    3724                 : bool
    3725 GNC     1529337 : query_or_expression_tree_walker_impl(Node *node,
    3726                 :                                      tree_walker_callback walker,
    3727                 :                                      void *context,
    3728                 :                                      int flags)
    3729 ECB             : {
    3730 CBC     1529337 :     if (node && IsA(node, Query))
    3731 GIC      175771 :         return query_tree_walker((Query *) node,
    3732                 :                                  walker,
    3733 EUB             :                                  context,
    3734                 :                                  flags);
    3735                 :     else
    3736 GNC     1353566 :         return WALK(node);
    3737                 : }
    3738                 : 
    3739                 : /*
    3740                 :  * query_or_expression_tree_mutator --- hybrid form
    3741                 :  *
    3742                 :  * This routine will invoke query_tree_mutator if called on a Query node,
    3743                 :  * else will invoke the mutator directly.  This is a useful way of starting
    3744                 :  * the recursion when the mutator's normal change of state is not appropriate
    3745                 :  * for the outermost Query node.
    3746                 :  */
    3747                 : Node *
    3748           80267 : query_or_expression_tree_mutator_impl(Node *node,
    3749                 :                                       tree_mutator_callback mutator,
    3750                 :                                       void *context,
    3751                 :                                       int flags)
    3752                 : {
    3753 GIC       80267 :     if (node && IsA(node, Query))
    3754            2579 :         return (Node *) query_tree_mutator((Query *) node,
    3755                 :                                            mutator,
    3756                 :                                            context,
    3757                 :                                            flags);
    3758                 :     else
    3759           77688 :         return mutator(node, context);
    3760                 : }
    3761                 : 
    3762                 : 
    3763 ECB             : /*
    3764                 :  * raw_expression_tree_walker --- walk raw parse trees
    3765                 :  *
    3766                 :  * This has exactly the same API as expression_tree_walker, but instead of
    3767                 :  * walking post-analysis parse trees, it knows how to walk the node types
    3768                 :  * found in raw grammar output.  (There is not currently any need for a
    3769                 :  * combined walker, so we keep them separate in the name of efficiency.)
    3770                 :  * Unlike expression_tree_walker, there is no special rule about query
    3771                 :  * boundaries: we descend to everything that's possibly interesting.
    3772                 :  *
    3773                 :  * Currently, the node type coverage here extends only to DML statements
    3774                 :  * (SELECT/INSERT/UPDATE/DELETE/MERGE) and nodes that can appear in them,
    3775                 :  * because this is used mainly during analysis of CTEs, and only DML
    3776                 :  * statements can appear in CTEs.
    3777                 :  */
    3778                 : bool
    3779 GNC       38418 : raw_expression_tree_walker_impl(Node *node,
    3780                 :                                 tree_walker_callback walker,
    3781                 :                                 void *context)
    3782 ECB             : {
    3783                 :     ListCell   *temp;
    3784                 : 
    3785                 :     /*
    3786                 :      * The walker has already visited the current node, and so we need only
    3787                 :      * recurse into any sub-nodes it has.
    3788                 :      */
    3789 GIC       38418 :     if (node == NULL)
    3790 UIC           0 :         return false;
    3791                 : 
    3792                 :     /* Guard against stack overflow due to overly complex expressions */
    3793 GIC       38418 :     check_stack_depth();
    3794                 : 
    3795 CBC       38418 :     switch (nodeTag(node))
    3796                 :     {
    3797 GNC        3927 :         case T_JsonFormat:
    3798 EUB             :         case T_SetToDefault:
    3799                 :         case T_CurrentOfExpr:
    3800                 :         case T_Integer:
    3801                 :         case T_Float:
    3802                 :         case T_Boolean:
    3803                 :         case T_String:
    3804                 :         case T_BitString:
    3805                 :         case T_ParamRef:
    3806                 :         case T_A_Const:
    3807                 :         case T_A_Star:
    3808                 :             /* primitive node types with no subnodes */
    3809 GIC        3927 :             break;
    3810             135 :         case T_Alias:
    3811 ECB             :             /* we assume the colnames list isn't interesting */
    3812 CBC         135 :             break;
    3813 UIC           0 :         case T_RangeVar:
    3814 UNC           0 :             return WALK(((RangeVar *) node)->alias);
    3815 UIC           0 :         case T_GroupingFunc:
    3816 UNC           0 :             return WALK(((GroupingFunc *) node)->args);
    3817 CBC          21 :         case T_SubLink:
    3818 ECB             :             {
    3819 CBC          21 :                 SubLink    *sublink = (SubLink *) node;
    3820                 : 
    3821 GNC          21 :                 if (WALK(sublink->testexpr))
    3822 UIC           0 :                     return true;
    3823 ECB             :                 /* we assume the operName is not interesting */
    3824 GNC          21 :                 if (WALK(sublink->subselect))
    3825 UIC           0 :                     return true;
    3826                 :             }
    3827 GIC          21 :             break;
    3828 UIC           0 :         case T_CaseExpr:
    3829                 :             {
    3830               0 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    3831                 : 
    3832 UNC           0 :                 if (WALK(caseexpr->arg))
    3833 UIC           0 :                     return true;
    3834                 :                 /* we assume walker doesn't care about CaseWhens, either */
    3835               0 :                 foreach(temp, caseexpr->args)
    3836                 :                 {
    3837               0 :                     CaseWhen   *when = lfirst_node(CaseWhen, temp);
    3838 ECB             : 
    3839 UNC           0 :                     if (WALK(when->expr))
    3840 UIC           0 :                         return true;
    3841 UNC           0 :                     if (WALK(when->result))
    3842 LBC           0 :                         return true;
    3843                 :                 }
    3844 UNC           0 :                 if (WALK(caseexpr->defresult))
    3845 UIC           0 :                     return true;
    3846                 :             }
    3847               0 :             break;
    3848 GIC          54 :         case T_RowExpr:
    3849                 :             /* Assume colnames isn't interesting */
    3850 GNC          54 :             return WALK(((RowExpr *) node)->args);
    3851 UIC           0 :         case T_CoalesceExpr:
    3852 UNC           0 :             return WALK(((CoalesceExpr *) node)->args);
    3853 LBC           0 :         case T_MinMaxExpr:
    3854 UNC           0 :             return WALK(((MinMaxExpr *) node)->args);
    3855 UIC           0 :         case T_XmlExpr:
    3856                 :             {
    3857               0 :                 XmlExpr    *xexpr = (XmlExpr *) node;
    3858 ECB             : 
    3859 UNC           0 :                 if (WALK(xexpr->named_args))
    3860 UIC           0 :                     return true;
    3861 ECB             :                 /* we assume walker doesn't care about arg_names */
    3862 UNC           0 :                 if (WALK(xexpr->args))
    3863 LBC           0 :                     return true;
    3864                 :             }
    3865 UIC           0 :             break;
    3866 UNC           0 :         case T_JsonReturning:
    3867               0 :             return WALK(((JsonReturning *) node)->format);
    3868               0 :         case T_JsonValueExpr:
    3869                 :             {
    3870               0 :                 JsonValueExpr *jve = (JsonValueExpr *) node;
    3871                 : 
    3872               0 :                 if (WALK(jve->raw_expr))
    3873               0 :                     return true;
    3874               0 :                 if (WALK(jve->formatted_expr))
    3875               0 :                     return true;
    3876               0 :                 if (WALK(jve->format))
    3877               0 :                     return true;
    3878                 :             }
    3879               0 :             break;
    3880               0 :         case T_JsonConstructorExpr:
    3881                 :             {
    3882               0 :                 JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
    3883                 : 
    3884               0 :                 if (WALK(ctor->args))
    3885               0 :                     return true;
    3886               0 :                 if (WALK(ctor->func))
    3887               0 :                     return true;
    3888               0 :                 if (WALK(ctor->coercion))
    3889               0 :                     return true;
    3890               0 :                 if (WALK(ctor->returning))
    3891               0 :                     return true;
    3892                 :             }
    3893               0 :             break;
    3894               0 :         case T_JsonIsPredicate:
    3895               0 :             return WALK(((JsonIsPredicate *) node)->expr);
    3896 LBC           0 :         case T_NullTest:
    3897 UNC           0 :             return WALK(((NullTest *) node)->arg);
    3898 UIC           0 :         case T_BooleanTest:
    3899 UNC           0 :             return WALK(((BooleanTest *) node)->arg);
    3900 CBC         813 :         case T_JoinExpr:
    3901                 :             {
    3902 GIC         813 :                 JoinExpr   *join = (JoinExpr *) node;
    3903 ECB             : 
    3904 GNC         813 :                 if (WALK(join->larg))
    3905 LBC           0 :                     return true;
    3906 GNC         813 :                 if (WALK(join->rarg))
    3907 UIC           0 :                     return true;
    3908 GNC         813 :                 if (WALK(join->quals))
    3909 UIC           0 :                     return true;
    3910 GNC         813 :                 if (WALK(join->alias))
    3911 UIC           0 :                     return true;
    3912 ECB             :                 /* using list is deemed uninteresting */
    3913                 :             }
    3914 CBC         813 :             break;
    3915 LBC           0 :         case T_IntoClause:
    3916                 :             {
    3917 UIC           0 :                 IntoClause *into = (IntoClause *) node;
    3918                 : 
    3919 UNC           0 :                 if (WALK(into->rel))
    3920 UIC           0 :                     return true;
    3921 ECB             :                 /* colNames, options are deemed uninteresting */
    3922                 :                 /* viewQuery should be null in raw parsetree, but check it */
    3923 UNC           0 :                 if (WALK(into->viewQuery))
    3924 LBC           0 :                     return true;
    3925 EUB             :             }
    3926 UBC           0 :             break;
    3927 GBC        7257 :         case T_List:
    3928 CBC       19245 :             foreach(temp, (List *) node)
    3929 ECB             :             {
    3930 GNC       12030 :                 if (WALK((Node *) lfirst(temp)))
    3931 LBC           0 :                     return true;
    3932                 :             }
    3933 GIC        7215 :             break;
    3934              21 :         case T_InsertStmt:
    3935 ECB             :             {
    3936 GIC          21 :                 InsertStmt *stmt = (InsertStmt *) node;
    3937 ECB             : 
    3938 GNC          21 :                 if (WALK(stmt->relation))
    3939 UIC           0 :                     return true;
    3940 GNC          21 :                 if (WALK(stmt->cols))
    3941 UIC           0 :                     return true;
    3942 GNC          21 :                 if (WALK(stmt->selectStmt))
    3943 UIC           0 :                     return true;
    3944 GNC          21 :                 if (WALK(stmt->onConflictClause))
    3945 UIC           0 :                     return true;
    3946 GNC          21 :                 if (WALK(stmt->returningList))
    3947 UIC           0 :                     return true;
    3948 GNC          21 :                 if (WALK(stmt->withClause))
    3949 UIC           0 :                     return true;
    3950                 :             }
    3951 GIC          21 :             break;
    3952 LBC           0 :         case T_DeleteStmt:
    3953                 :             {
    3954 UIC           0 :                 DeleteStmt *stmt = (DeleteStmt *) node;
    3955                 : 
    3956 UNC           0 :                 if (WALK(stmt->relation))
    3957 LBC           0 :                     return true;
    3958 UNC           0 :                 if (WALK(stmt->usingClause))
    3959 UIC           0 :                     return true;
    3960 UNC           0 :                 if (WALK(stmt->whereClause))
    3961 UIC           0 :                     return true;
    3962 UNC           0 :                 if (WALK(stmt->returningList))
    3963 LBC           0 :                     return true;
    3964 UNC           0 :                 if (WALK(stmt->withClause))
    3965 UIC           0 :                     return true;
    3966                 :             }
    3967               0 :             break;
    3968 GIC           3 :         case T_UpdateStmt:
    3969                 :             {
    3970               3 :                 UpdateStmt *stmt = (UpdateStmt *) node;
    3971                 : 
    3972 GNC           3 :                 if (WALK(stmt->relation))
    3973 UIC           0 :                     return true;
    3974 GNC           3 :                 if (WALK(stmt->targetList))
    3975 LBC           0 :                     return true;
    3976 GNC           3 :                 if (WALK(stmt->whereClause))
    3977 UIC           0 :                     return true;
    3978 GNC           3 :                 if (WALK(stmt->fromClause))
    3979 UIC           0 :                     return true;
    3980 GNC           3 :                 if (WALK(stmt->returningList))
    3981 LBC           0 :                     return true;
    3982 GNC           3 :                 if (WALK(stmt->withClause))
    3983 UIC           0 :                     return true;
    3984                 :             }
    3985 GIC           3 :             break;
    3986 LBC           0 :         case T_MergeStmt:
    3987                 :             {
    3988 UIC           0 :                 MergeStmt  *stmt = (MergeStmt *) node;
    3989                 : 
    3990 UNC           0 :                 if (WALK(stmt->relation))
    3991 UIC           0 :                     return true;
    3992 UNC           0 :                 if (WALK(stmt->sourceRelation))
    3993 UIC           0 :                     return true;
    3994 UNC           0 :                 if (WALK(stmt->joinCondition))
    3995 UIC           0 :                     return true;
    3996 UNC           0 :                 if (WALK(stmt->mergeWhenClauses))
    3997 UIC           0 :                     return true;
    3998 UNC           0 :                 if (WALK(stmt->withClause))
    3999 UIC           0 :                     return true;
    4000                 :             }
    4001               0 :             break;
    4002               0 :         case T_MergeWhenClause:
    4003                 :             {
    4004               0 :                 MergeWhenClause *mergeWhenClause = (MergeWhenClause *) node;
    4005                 : 
    4006 UNC           0 :                 if (WALK(mergeWhenClause->condition))
    4007 UIC           0 :                     return true;
    4008 UNC           0 :                 if (WALK(mergeWhenClause->targetList))
    4009 UIC           0 :                     return true;
    4010 UNC           0 :                 if (WALK(mergeWhenClause->values))
    4011 UIC           0 :                     return true;
    4012                 :             }
    4013               0 :             break;
    4014 GIC        3369 :         case T_SelectStmt:
    4015                 :             {
    4016 CBC        3369 :                 SelectStmt *stmt = (SelectStmt *) node;
    4017 EUB             : 
    4018 GNC        3369 :                 if (WALK(stmt->distinctClause))
    4019 UIC           0 :                     return true;
    4020 GNC        3369 :                 if (WALK(stmt->intoClause))
    4021 UIC           0 :                     return true;
    4022 GNC        3369 :                 if (WALK(stmt->targetList))
    4023 UIC           0 :                     return true;
    4024 GNC        3366 :                 if (WALK(stmt->fromClause))
    4025 UIC           0 :                     return true;
    4026 GNC        3330 :                 if (WALK(stmt->whereClause))
    4027 UIC           0 :                     return true;
    4028 GNC        3327 :                 if (WALK(stmt->groupClause))
    4029 UIC           0 :                     return true;
    4030 GNC        3327 :                 if (WALK(stmt->havingClause))
    4031 UIC           0 :                     return true;
    4032 GNC        3327 :                 if (WALK(stmt->windowClause))
    4033 UIC           0 :                     return true;
    4034 GNC        3327 :                 if (WALK(stmt->valuesLists))
    4035 UIC           0 :                     return true;
    4036 GNC        3327 :                 if (WALK(stmt->sortClause))
    4037 LBC           0 :                     return true;
    4038 GNC        3327 :                 if (WALK(stmt->limitOffset))
    4039 LBC           0 :                     return true;
    4040 GNC        3327 :                 if (WALK(stmt->limitCount))
    4041 UBC           0 :                     return true;
    4042 GNC        3327 :                 if (WALK(stmt->lockingClause))
    4043 UBC           0 :                     return true;
    4044 GNC        3327 :                 if (WALK(stmt->withClause))
    4045 UIC           0 :                     return true;
    4046 GNC        3327 :                 if (WALK(stmt->larg))
    4047 UIC           0 :                     return true;
    4048 GNC        3327 :                 if (WALK(stmt->rarg))
    4049 UBC           0 :                     return true;
    4050                 :             }
    4051 CBC        3321 :             break;
    4052 UBC           0 :         case T_PLAssignStmt:
    4053                 :             {
    4054 LBC           0 :                 PLAssignStmt *stmt = (PLAssignStmt *) node;
    4055 EUB             : 
    4056 UNC           0 :                 if (WALK(stmt->indirection))
    4057 UBC           0 :                     return true;
    4058 UNC           0 :                 if (WALK(stmt->val))
    4059 UBC           0 :                     return true;
    4060 EUB             :             }
    4061 UIC           0 :             break;
    4062 GBC        5193 :         case T_A_Expr:
    4063                 :             {
    4064            5193 :                 A_Expr     *expr = (A_Expr *) node;
    4065                 : 
    4066 GNC        5193 :                 if (WALK(expr->lexpr))
    4067 UBC           0 :                     return true;
    4068 GNC        5193 :                 if (WALK(expr->rexpr))
    4069 UBC           0 :                     return true;
    4070                 :                 /* operator name is deemed uninteresting */
    4071 EUB             :             }
    4072 GBC        5193 :             break;
    4073 GIC        1440 :         case T_BoolExpr:
    4074 EUB             :             {
    4075 CBC        1440 :                 BoolExpr   *expr = (BoolExpr *) node;
    4076                 : 
    4077 GNC        1440 :                 if (WALK(expr->args))
    4078 UBC           0 :                     return true;
    4079 EUB             :             }
    4080 GBC        1440 :             break;
    4081           10743 :         case T_ColumnRef:
    4082 EUB             :             /* we assume the fields contain nothing interesting */
    4083 GIC       10743 :             break;
    4084 GBC         120 :         case T_FuncCall:
    4085                 :             {
    4086             120 :                 FuncCall   *fcall = (FuncCall *) node;
    4087 EUB             : 
    4088 GNC         120 :                 if (WALK(fcall->args))
    4089 UBC           0 :                     return true;
    4090 GNC         120 :                 if (WALK(fcall->agg_order))
    4091 UIC           0 :                     return true;
    4092 GNC         120 :                 if (WALK(fcall->agg_filter))
    4093 UBC           0 :                     return true;
    4094 GNC         120 :                 if (WALK(fcall->over))
    4095 UBC           0 :                     return true;
    4096                 :                 /* function name is deemed uninteresting */
    4097 EUB             :             }
    4098 GIC         120 :             break;
    4099 UBC           0 :         case T_NamedArgExpr:
    4100 UNC           0 :             return WALK(((NamedArgExpr *) node)->arg);
    4101 UBC           0 :         case T_A_Indices:
    4102 EUB             :             {
    4103 UBC           0 :                 A_Indices  *indices = (A_Indices *) node;
    4104 EUB             : 
    4105 UNC           0 :                 if (WALK(indices->lidx))
    4106 UBC           0 :                     return true;
    4107 UNC           0 :                 if (WALK(indices->uidx))
    4108 UIC           0 :                     return true;
    4109 EUB             :             }
    4110 UIC           0 :             break;
    4111 UBC           0 :         case T_A_Indirection:
    4112 EUB             :             {
    4113 UBC           0 :                 A_Indirection *indir = (A_Indirection *) node;
    4114 EUB             : 
    4115 UNC           0 :                 if (WALK(indir->arg))
    4116 UBC           0 :                     return true;
    4117 UNC           0 :                 if (WALK(indir->indirection))
    4118 UBC           0 :                     return true;
    4119                 :             }
    4120               0 :             break;
    4121 GBC          42 :         case T_A_ArrayExpr:
    4122 GNC          42 :             return WALK(((A_ArrayExpr *) node)->elements);
    4123 GBC        3606 :         case T_ResTarget:
    4124 EUB             :             {
    4125 GBC        3606 :                 ResTarget  *rt = (ResTarget *) node;
    4126 EUB             : 
    4127 GNC        3606 :                 if (WALK(rt->indirection))
    4128 UIC           0 :                     return true;
    4129 GNC        3606 :                 if (WALK(rt->val))
    4130 UIC           0 :                     return true;
    4131 ECB             :             }
    4132 GBC        3603 :             break;
    4133 LBC           0 :         case T_MultiAssignRef:
    4134 UNC           0 :             return WALK(((MultiAssignRef *) node)->source);
    4135 CBC         732 :         case T_TypeCast:
    4136 EUB             :             {
    4137 CBC         732 :                 TypeCast   *tc = (TypeCast *) node;
    4138 EUB             : 
    4139 GNC         732 :                 if (WALK(tc->arg))
    4140 UIC           0 :                     return true;
    4141 GNC         732 :                 if (WALK(tc->typeName))
    4142 UBC           0 :                     return true;
    4143                 :             }
    4144 GBC         732 :             break;
    4145 GIC          48 :         case T_CollateClause:
    4146 GNC          48 :             return WALK(((CollateClause *) node)->arg);
    4147 GBC           3 :         case T_SortBy:
    4148 GNC           3 :             return WALK(((SortBy *) node)->node);
    4149 GIC          12 :         case T_WindowDef:
    4150 EUB             :             {
    4151 GBC          12 :                 WindowDef  *wd = (WindowDef *) node;
    4152                 : 
    4153 GNC          12 :                 if (WALK(wd->partitionClause))
    4154 LBC           0 :                     return true;
    4155 GNC          12 :                 if (WALK(wd->orderClause))
    4156 UIC           0 :                     return true;
    4157 GNC          12 :                 if (WALK(wd->startOffset))
    4158 UBC           0 :                     return true;
    4159 GNC          12 :                 if (WALK(wd->endOffset))
    4160 LBC           0 :                     return true;
    4161 ECB             :             }
    4162 GIC          12 :             break;
    4163 CBC         102 :         case T_RangeSubselect:
    4164                 :             {
    4165             102 :                 RangeSubselect *rs = (RangeSubselect *) node;
    4166 EUB             : 
    4167 GNC         102 :                 if (WALK(rs->subquery))
    4168 UBC           0 :                     return true;
    4169 GNC          99 :                 if (WALK(rs->alias))
    4170 UBC           0 :                     return true;
    4171 ECB             :             }
    4172 GBC          99 :             break;
    4173 CBC          36 :         case T_RangeFunction:
    4174 EUB             :             {
    4175 CBC          36 :                 RangeFunction *rf = (RangeFunction *) node;
    4176 EUB             : 
    4177 GNC          36 :                 if (WALK(rf->functions))
    4178 LBC           0 :                     return true;
    4179 GNC          36 :                 if (WALK(rf->alias))
    4180 UIC           0 :                     return true;
    4181 GNC          36 :                 if (WALK(rf->coldeflist))
    4182 UIC           0 :                     return true;
    4183 EUB             :             }
    4184 GBC          36 :             break;
    4185 UBC           0 :         case T_RangeTableSample:
    4186 EUB             :             {
    4187 UBC           0 :                 RangeTableSample *rts = (RangeTableSample *) node;
    4188 EUB             : 
    4189 UNC           0 :                 if (WALK(rts->relation))
    4190 UBC           0 :                     return true;
    4191 EUB             :                 /* method name is deemed uninteresting */
    4192 UNC           0 :                 if (WALK(rts->args))
    4193 UIC           0 :                     return true;
    4194 UNC           0 :                 if (WALK(rts->repeatable))
    4195 LBC           0 :                     return true;
    4196                 :             }
    4197               0 :             break;
    4198 UIC           0 :         case T_RangeTableFunc:
    4199 ECB             :             {
    4200 UBC           0 :                 RangeTableFunc *rtf = (RangeTableFunc *) node;
    4201 ECB             : 
    4202 UNC           0 :                 if (WALK(rtf->docexpr))
    4203 LBC           0 :                     return true;
    4204 UNC           0 :                 if (WALK(rtf->rowexpr))
    4205 LBC           0 :                     return true;
    4206 UNC           0 :                 if (WALK(rtf->namespaces))
    4207 LBC           0 :                     return true;
    4208 UNC           0 :                 if (WALK(rtf->columns))
    4209 LBC           0 :                     return true;
    4210 UNC           0 :                 if (WALK(rtf->alias))
    4211 UIC           0 :                     return true;
    4212 ECB             :             }
    4213 UBC           0 :             break;
    4214 UIC           0 :         case T_RangeTableFuncCol:
    4215 EUB             :             {
    4216 UIC           0 :                 RangeTableFuncCol *rtfc = (RangeTableFuncCol *) node;
    4217 EUB             : 
    4218 UNC           0 :                 if (WALK(rtfc->colexpr))
    4219 UBC           0 :                     return true;
    4220 UNC           0 :                 if (WALK(rtfc->coldefexpr))
    4221 UBC           0 :                     return true;
    4222 EUB             :             }
    4223 UBC           0 :             break;
    4224 GBC         732 :         case T_TypeName:
    4225 EUB             :             {
    4226 GBC         732 :                 TypeName   *tn = (TypeName *) node;
    4227                 : 
    4228 GNC         732 :                 if (WALK(tn->typmods))
    4229 UBC           0 :                     return true;
    4230 GNC         732 :                 if (WALK(tn->arrayBounds))
    4231 UBC           0 :                     return true;
    4232                 :                 /* type name itself is deemed uninteresting */
    4233 EUB             :             }
    4234 GBC         732 :             break;
    4235 UBC           0 :         case T_ColumnDef:
    4236 EUB             :             {
    4237 UBC           0 :                 ColumnDef  *coldef = (ColumnDef *) node;
    4238 EUB             : 
    4239 UNC           0 :                 if (WALK(coldef->typeName))
    4240 UBC           0 :                     return true;
    4241 UNC           0 :                 if (WALK(coldef->compression))
    4242 UIC           0 :                     return true;
    4243 UNC           0 :                 if (WALK(coldef->raw_default))
    4244 UIC           0 :                     return true;
    4245 UNC           0 :                 if (WALK(coldef->collClause))
    4246 UBC           0 :                     return true;
    4247 ECB             :                 /* for now, constraints are ignored */
    4248 EUB             :             }
    4249 LBC           0 :             break;
    4250 UBC           0 :         case T_IndexElem:
    4251 ECB             :             {
    4252 UBC           0 :                 IndexElem  *indelem = (IndexElem *) node;
    4253 ECB             : 
    4254 UNC           0 :                 if (WALK(indelem->expr))
    4255 LBC           0 :                     return true;
    4256 EUB             :                 /* collation and opclass names are deemed uninteresting */
    4257 ECB             :             }
    4258 UBC           0 :             break;
    4259 LBC           0 :         case T_GroupingSet:
    4260 UNC           0 :             return WALK(((GroupingSet *) node)->content);
    4261 CBC           3 :         case T_LockingClause:
    4262 GNC           3 :             return WALK(((LockingClause *) node)->lockedRels);
    4263 LBC           0 :         case T_XmlSerialize:
    4264 EUB             :             {
    4265 LBC           0 :                 XmlSerialize *xs = (XmlSerialize *) node;
    4266 EUB             : 
    4267 UNC           0 :                 if (WALK(xs->expr))
    4268 UBC           0 :                     return true;
    4269 UNC           0 :                 if (WALK(xs->typeName))
    4270 UBC           0 :                     return true;
    4271 ECB             :             }
    4272 UBC           0 :             break;
    4273 LBC           0 :         case T_WithClause:
    4274 UNC           0 :             return WALK(((WithClause *) node)->ctes);
    4275 LBC           0 :         case T_InferClause:
    4276 EUB             :             {
    4277 UIC           0 :                 InferClause *stmt = (InferClause *) node;
    4278 ECB             : 
    4279 UNC           0 :                 if (WALK(stmt->indexElems))
    4280 UIC           0 :                     return true;
    4281 UNC           0 :                 if (WALK(stmt->whereClause))
    4282 UIC           0 :                     return true;
    4283 EUB             :             }
    4284 UBC           0 :             break;
    4285               0 :         case T_OnConflictClause:
    4286 EUB             :             {
    4287 UIC           0 :                 OnConflictClause *stmt = (OnConflictClause *) node;
    4288 EUB             : 
    4289 UNC           0 :                 if (WALK(stmt->infer))
    4290 UIC           0 :                     return true;
    4291 UNC           0 :                 if (WALK(stmt->targetList))
    4292 UIC           0 :                     return true;
    4293 UNC           0 :                 if (WALK(stmt->whereClause))
    4294 UBC           0 :                     return true;
    4295 ECB             :             }
    4296 UBC           0 :             break;
    4297 GIC           6 :         case T_CommonTableExpr:
    4298                 :             /* search_clause and cycle_clause are not interesting here */
    4299 GNC           6 :             return WALK(((CommonTableExpr *) node)->ctequery);
    4300 UNC           0 :         case T_JsonOutput:
    4301                 :             {
    4302               0 :                 JsonOutput *out = (JsonOutput *) node;
    4303                 : 
    4304               0 :                 if (WALK(out->typeName))
    4305               0 :                     return true;
    4306               0 :                 if (WALK(out->returning))
    4307               0 :                     return true;
    4308                 :             }
    4309               0 :             break;
    4310               0 :         case T_JsonKeyValue:
    4311                 :             {
    4312               0 :                 JsonKeyValue *jkv = (JsonKeyValue *) node;
    4313                 : 
    4314               0 :                 if (WALK(jkv->key))
    4315               0 :                     return true;
    4316               0 :                 if (WALK(jkv->value))
    4317               0 :                     return true;
    4318                 :             }
    4319               0 :             break;
    4320               0 :         case T_JsonObjectConstructor:
    4321                 :             {
    4322               0 :                 JsonObjectConstructor *joc = (JsonObjectConstructor *) node;
    4323                 : 
    4324               0 :                 if (WALK(joc->output))
    4325               0 :                     return true;
    4326               0 :                 if (WALK(joc->exprs))
    4327               0 :                     return true;
    4328                 :             }
    4329               0 :             break;
    4330               0 :         case T_JsonArrayConstructor:
    4331                 :             {
    4332               0 :                 JsonArrayConstructor *jac = (JsonArrayConstructor *) node;
    4333                 : 
    4334               0 :                 if (WALK(jac->output))
    4335               0 :                     return true;
    4336               0 :                 if (WALK(jac->exprs))
    4337               0 :                     return true;
    4338                 :             }
    4339               0 :             break;
    4340               0 :         case T_JsonAggConstructor:
    4341                 :             {
    4342               0 :                 JsonAggConstructor *ctor = (JsonAggConstructor *) node;
    4343                 : 
    4344               0 :                 if (WALK(ctor->output))
    4345               0 :                     return true;
    4346               0 :                 if (WALK(ctor->agg_order))
    4347               0 :                     return true;
    4348               0 :                 if (WALK(ctor->agg_filter))
    4349               0 :                     return true;
    4350               0 :                 if (WALK(ctor->over))
    4351               0 :                     return true;
    4352                 :             }
    4353               0 :             break;
    4354               0 :         case T_JsonObjectAgg:
    4355                 :             {
    4356               0 :                 JsonObjectAgg *joa = (JsonObjectAgg *) node;
    4357                 : 
    4358               0 :                 if (WALK(joa->constructor))
    4359               0 :                     return true;
    4360               0 :                 if (WALK(joa->arg))
    4361               0 :                     return true;
    4362                 :             }
    4363               0 :             break;
    4364               0 :         case T_JsonArrayAgg:
    4365                 :             {
    4366               0 :                 JsonArrayAgg *jaa = (JsonArrayAgg *) node;
    4367                 : 
    4368               0 :                 if (WALK(jaa->constructor))
    4369               0 :                     return true;
    4370               0 :                 if (WALK(jaa->arg))
    4371               0 :                     return true;
    4372                 :             }
    4373               0 :             break;
    4374               0 :         case T_JsonArrayQueryConstructor:
    4375                 :             {
    4376               0 :                 JsonArrayQueryConstructor *jaqc = (JsonArrayQueryConstructor *) node;
    4377                 : 
    4378               0 :                 if (WALK(jaqc->output))
    4379               0 :                     return true;
    4380               0 :                 if (WALK(jaqc->query))
    4381               0 :                     return true;
    4382                 :             }
    4383               0 :             break;
    4384 LBC           0 :         default:
    4385 UIC           0 :             elog(ERROR, "unrecognized node type: %d",
    4386 ECB             :                  (int) nodeTag(node));
    4387                 :             break;
    4388                 :     }
    4389 GBC       38166 :     return false;
    4390                 : }
    4391 ECB             : 
    4392                 : /*
    4393                 :  * planstate_tree_walker --- walk plan state trees
    4394                 :  *
    4395                 :  * The walker has already visited the current node, and so we need only
    4396                 :  * recurse into any sub-nodes it has.
    4397                 :  */
    4398                 : bool
    4399 GNC      573189 : planstate_tree_walker_impl(PlanState *planstate,
    4400                 :                            planstate_tree_walker_callback walker,
    4401                 :                            void *context)
    4402 EUB             : {
    4403 CBC      573189 :     Plan       *plan = planstate->plan;
    4404 EUB             :     ListCell   *lc;
    4405 ECB             : 
    4406                 :     /* We don't need implicit coercions to Node here */
    4407                 : #define PSWALK(n) walker(n, context)
    4408                 : 
    4409 EUB             :     /* Guard against stack overflow due to overly complex plan trees */
    4410 GIC      573189 :     check_stack_depth();
    4411                 : 
    4412 ECB             :     /* initPlan-s */
    4413 GBC      573189 :     if (planstate_walk_subplans(planstate->initPlan, walker, context))
    4414 UBC           0 :         return true;
    4415 EUB             : 
    4416                 :     /* lefttree */
    4417 GBC      573189 :     if (outerPlanState(planstate))
    4418                 :     {
    4419 GNC      212626 :         if (PSWALK(outerPlanState(planstate)))
    4420 UBC           0 :             return true;
    4421 EUB             :     }
    4422                 : 
    4423                 :     /* righttree */
    4424 GBC      573189 :     if (innerPlanState(planstate))
    4425 EUB             :     {
    4426 GNC       53210 :         if (PSWALK(innerPlanState(planstate)))
    4427 UBC           0 :             return true;
    4428                 :     }
    4429 EUB             : 
    4430                 :     /* special child plans */
    4431 GBC      573189 :     switch (nodeTag(plan))
    4432 EUB             :     {
    4433 GIC        7106 :         case T_Append:
    4434 GBC        7106 :             if (planstate_walk_members(((AppendState *) planstate)->appendplans,
    4435 ECB             :                                        ((AppendState *) planstate)->as_nplans,
    4436                 :                                        walker, context))
    4437 LBC           0 :                 return true;
    4438 GIC        7106 :             break;
    4439 CBC         225 :         case T_MergeAppend:
    4440 GIC         225 :             if (planstate_walk_members(((MergeAppendState *) planstate)->mergeplans,
    4441 ECB             :                                        ((MergeAppendState *) planstate)->ms_nplans,
    4442 EUB             :                                        walker, context))
    4443 LBC           0 :                 return true;
    4444 GBC         225 :             break;
    4445 GIC          38 :         case T_BitmapAnd:
    4446 CBC          38 :             if (planstate_walk_members(((BitmapAndState *) planstate)->bitmapplans,
    4447 EUB             :                                        ((BitmapAndState *) planstate)->nplans,
    4448                 :                                        walker, context))
    4449 LBC           0 :                 return true;
    4450 GIC          38 :             break;
    4451 CBC         132 :         case T_BitmapOr:
    4452 GIC         132 :             if (planstate_walk_members(((BitmapOrState *) planstate)->bitmapplans,
    4453 ECB             :                                        ((BitmapOrState *) planstate)->nplans,
    4454 EUB             :                                        walker, context))
    4455 LBC           0 :                 return true;
    4456 GBC         132 :             break;
    4457 GIC        4710 :         case T_SubqueryScan:
    4458 GNC        4710 :             if (PSWALK(((SubqueryScanState *) planstate)->subplan))
    4459 LBC           0 :                 return true;
    4460 CBC        4710 :             break;
    4461 LBC           0 :         case T_CustomScan:
    4462               0 :             foreach(lc, ((CustomScanState *) planstate)->custom_ps)
    4463 ECB             :             {
    4464 UNC           0 :                 if (PSWALK(lfirst(lc)))
    4465 LBC           0 :                     return true;
    4466                 :             }
    4467               0 :             break;
    4468 GBC      560978 :         default:
    4469 CBC      560978 :             break;
    4470 EUB             :     }
    4471 ECB             : 
    4472 EUB             :     /* subPlan-s */
    4473 CBC      573189 :     if (planstate_walk_subplans(planstate->subPlan, walker, context))
    4474 UBC           0 :         return true;
    4475                 : 
    4476 CBC      573189 :     return false;
    4477 ECB             : }
    4478                 : 
    4479                 : /*
    4480                 :  * Walk a list of SubPlans (or initPlans, which also use SubPlan nodes).
    4481                 :  */
    4482 EUB             : static bool
    4483 CBC     1146378 : planstate_walk_subplans(List *plans,
    4484                 :                         planstate_tree_walker_callback walker,
    4485                 :                         void *context)
    4486 ECB             : {
    4487                 :     ListCell   *lc;
    4488                 : 
    4489 CBC     1165282 :     foreach(lc, plans)
    4490                 :     {
    4491           18904 :         SubPlanState *sps = lfirst_node(SubPlanState, lc);
    4492 EUB             : 
    4493 GNC       18904 :         if (PSWALK(sps->planstate))
    4494 UBC           0 :             return true;
    4495 ECB             :     }
    4496 EUB             : 
    4497 GIC     1146378 :     return false;
    4498 ECB             : }
    4499 EUB             : 
    4500                 : /*
    4501                 :  * Walk the constituent plans of a ModifyTable, Append, MergeAppend,
    4502                 :  * BitmapAnd, or BitmapOr node.
    4503                 :  */
    4504                 : static bool
    4505 GIC        7501 : planstate_walk_members(PlanState **planstates, int nplans,
    4506                 :                        planstate_tree_walker_callback walker,
    4507                 :                        void *context)
    4508 EUB             : {
    4509                 :     int         j;
    4510                 : 
    4511 GIC       29876 :     for (j = 0; j < nplans; j++)
    4512 EUB             :     {
    4513 GNC       22375 :         if (PSWALK(planstates[j]))
    4514 UIC           0 :             return true;
    4515 EUB             :     }
    4516                 : 
    4517 GBC        7501 :     return false;
    4518 EUB             : }
        

Generated by: LCOV version v1.16-55-g56c0a2a